From feec2a0a91f1987eccb28d88cf5282b4b85fbaa1 Mon Sep 17 00:00:00 2001 From: FReenen Date: Mon, 8 Apr 2024 13:39:24 +0200 Subject: [PATCH] update CLI --- CLI/CLI.c | 31 +++++++++++++------ CLI/CLI.h | 4 +-- CMDList/printList.c | 75 +++++++++++++++++++++++++++++++++++++++++++++ CMDList/printList.h | 8 +++++ 4 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 CMDList/printList.c create mode 100644 CMDList/printList.h diff --git a/CLI/CLI.c b/CLI/CLI.c index 8fbe350..f2021f2 100644 --- a/CLI/CLI.c +++ b/CLI/CLI.c @@ -1,13 +1,16 @@ #include "CLI.h" -#include "../FIFOBuffChar.h" +#include +#include + +#include "../FIFOBuff/FIFOBuffChar.h" void (*CLI_lineOut)(char* line); CMDList_t* CMDList; FIFOBuffChar_t* FIFO; // initilize and register the lineout print function -bool CLI_init(CLI_lineoutFn_t* lineOut, CMDList_t* cmdList) +bool CLI_init(CLI_lineOutFn lineOut, CMDList_t* cmdList) { CLI_lineOut = lineOut; CMDList = cmdList; @@ -42,7 +45,7 @@ char* fifoToString(FIFOBuffChar_t* fifo) int tryExecute(FIFOBuffChar_t* fifo) { char* line = fifoToString(fifo); - CMD_t cmd = CMDList_get(CMDList, line); + CMD_t* cmd = CMDList_get(CMDList, line); if (cmd != NULL) { @@ -51,6 +54,7 @@ int tryExecute(FIFOBuffChar_t* fifo) } else { + printf("command not found: %s\n", line); return false; } } @@ -58,6 +62,7 @@ int tryExecute(FIFOBuffChar_t* fifo) // to recive a single caracter bool CLI_charIn(char c) { + bool ok = true; if ((c >= 'a') && (c <= 'z')) { c &= (~0x20); // convert to uppercase @@ -66,13 +71,16 @@ bool CLI_charIn(char c) if ( //TODO: update list of accepted characters ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) - || (c = ' ') - || (c = '-') - || (c = '_') + || (c == ' ') + || (c == '-') + || (c == '_') ) { FIFOBuffChar_put(FIFO, c); // save char in buffer - (*CLI_lineOut)(&c); // echo to terminal + if (CLI_lineOut != NULL) + { // echo to terminal + (*CLI_lineOut)(&c); + } } else { @@ -80,10 +88,13 @@ bool CLI_charIn(char c) { case '\n': case '\r': - (*CLI_lineOut)(&c); // echo to terminal + if (CLI_lineOut != NULL) + { // echo to terminal + (*CLI_lineOut)(&c); + } FIFOBuffChar_t* fifo = FIFO; FIFO = FIFOBuffChar_create(); - tryExecute(fifo); + ok = tryExecute(fifo); FIFOBuffChar_delete(fifo); break; @@ -92,4 +103,6 @@ bool CLI_charIn(char c) break; } } + + return ok; } diff --git a/CLI/CLI.h b/CLI/CLI.h index e7d72e6..763dfad 100644 --- a/CLI/CLI.h +++ b/CLI/CLI.h @@ -5,10 +5,10 @@ #include "../CMDList/CMDList.h" -typedef CLI_lineOutFn(char* line) CLI_lineoutFn_t; +typedef int (*CLI_lineOutFn)(const char* line); // initilize and register the lineout print function -bool CLI_init(CLI_lineoutFn_t* lineOut, CMDList_t* cmdList); +bool CLI_init(CLI_lineOutFn lineOut, CMDList_t* cmdList); bool CLI_deinit(); // to recive a single caracter diff --git a/CMDList/printList.c b/CMDList/printList.c new file mode 100644 index 0000000..a10d073 --- /dev/null +++ b/CMDList/printList.c @@ -0,0 +1,75 @@ +#ifndef PRINTCMDLIST_H +#define PRINTCMDLIST_H + +#include "CMDList.h" +#include "printList.h" + +#include +#include +#include +#include + +void printCMD(CMDList_t* list, char c, char* indent) +{ + char* cmd; + if (list->cmd == NULL) + { + cmd = "(no cmd)"; + } + else + { + cmd = list->cmd->cmd; + } + + printf("%s%c -> %s\n", indent, c, cmd); + + char* indent_p = indent; + while (*indent_p != 0) + { + indent_p++; + } + *indent_p = ' '; + *(indent_p+1) = ' '; + *(indent_p+2) = 0; + + printCMDList(list, indent); + + *indent_p = 0; +} + +void printCMDList(CMDList_t* list, char* indent) +{ + if (indent == NULL) + { + indent = (char*)malloc(100 * sizeof(char)); + *indent = 0; + } + if (list->a != NULL) printCMD((CMDList_t*)list->a, 'a', indent); + if (list->b != NULL) printCMD((CMDList_t*)list->b, 'b', indent); + if (list->c != NULL) printCMD((CMDList_t*)list->c, 'c', indent); + if (list->d != NULL) printCMD((CMDList_t*)list->d, 'd', indent); + if (list->e != NULL) printCMD((CMDList_t*)list->e, 'e', indent); + if (list->f != NULL) printCMD((CMDList_t*)list->f, 'f', indent); + if (list->g != NULL) printCMD((CMDList_t*)list->g, 'g', indent); + if (list->h != NULL) printCMD((CMDList_t*)list->h, 'h', indent); + if (list->i != NULL) printCMD((CMDList_t*)list->i, 'i', indent); + if (list->j != NULL) printCMD((CMDList_t*)list->j, 'j', indent); + if (list->k != NULL) printCMD((CMDList_t*)list->k, 'k', indent); + if (list->l != NULL) printCMD((CMDList_t*)list->l, 'l', indent); + if (list->m != NULL) printCMD((CMDList_t*)list->m, 'm', indent); + if (list->n != NULL) printCMD((CMDList_t*)list->n, 'n', indent); + if (list->o != NULL) printCMD((CMDList_t*)list->o, 'o', indent); + if (list->p != NULL) printCMD((CMDList_t*)list->p, 'p', indent); + if (list->q != NULL) printCMD((CMDList_t*)list->q, 'q', indent); + if (list->r != NULL) printCMD((CMDList_t*)list->r, 'r', indent); + if (list->s != NULL) printCMD((CMDList_t*)list->s, 's', indent); + if (list->t != NULL) printCMD((CMDList_t*)list->t, 't', indent); + if (list->u != NULL) printCMD((CMDList_t*)list->u, 'u', indent); + if (list->v != NULL) printCMD((CMDList_t*)list->v, 'v', indent); + if (list->w != NULL) printCMD((CMDList_t*)list->w, 'w', indent); + if (list->x != NULL) printCMD((CMDList_t*)list->x, 'x', indent); + if (list->y != NULL) printCMD((CMDList_t*)list->y, 'y', indent); + if (list->z != NULL) printCMD((CMDList_t*)list->z, 'z', indent); +} + +#endif diff --git a/CMDList/printList.h b/CMDList/printList.h new file mode 100644 index 0000000..59856e0 --- /dev/null +++ b/CMDList/printList.h @@ -0,0 +1,8 @@ +#ifndef PRINTCMDLIST_H +#define PRINTCMDLIST_H + +#include "CMDList.h" + +extern void printCMDList(CMDList_t* list, char* indent); + +#endif