From ec42dc5d3d53964842f29d342e661161811276d9 Mon Sep 17 00:00:00 2001 From: FReenen Date: Fri, 5 Apr 2024 22:39:13 +0200 Subject: [PATCH 1/7] fix some errors in CMDList --- CMDList/CMDList.c | 20 +++++++++++--------- CMDList/CMDList.h | 12 ++++++++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index 251323e..dde5b50 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -1,7 +1,9 @@ #include "CMDList.h" +#include +#include -// initilises a CMDList_t with all null pointers +// initilises a CMDList_t with all NULL pointers CMDList_t* CMDList_init() { CMDList_t *list = malloc(sizeof(CMDList_t)); @@ -12,11 +14,11 @@ CMDList_t* CMDList_init() // free up the full tree from memory int CMDList_deinit(CMDList_t *list) { - void* chars[26] = list; + CMDList_t* chars[26]; //TODO: cast list to array for (int i = 0; i < 26; i++) { - if (chars[i] != null) + if (chars[i] != NULL) { CMDList_deinit(chars[i]); } @@ -27,31 +29,31 @@ int CMDList_deinit(CMDList_t *list) } // add a command in the command tree -int CMDList_add(CMDList_t *list, void* cmd) +int CMDList_add(CMDList_t *list, CMD_t* cmd) { char* read_p = cmd; CMDList_t* list_p = list; while ( - (read_p != null) + (read_p != NULL) && (*read_p != ' ') // space ends the command && (*read_p != '\n') // end of line && (*read_p != '\r') // end of line && (*read_p != 0) // end of string - && (read_p - cmd < 500) + && (read_p - (char*)cmd < 500) ) { char c = *read_p & (~0x20); // to uppercase if ((*read_p >= 'A') && (*read_p <= 'Z')) { - read_p += (read_p - 'A') * sizeof(CMDList_t*); - if (*((CMDList_t**)read_p) == null) + read_p += (*read_p - 'A') * sizeof(CMDList_t*); + if (*((CMDList_t**)read_p) == NULL) { read_p = malloc(sizeof(CMDList_t)); } else { - read_p = *((CMDList_t**)read_p) + read_p = *((CMDList_t**)read_p); } } } diff --git a/CMDList/CMDList.h b/CMDList/CMDList.h index 9732524..b19004f 100644 --- a/CMDList/CMDList.h +++ b/CMDList/CMDList.h @@ -1,6 +1,11 @@ #ifndef CMDLIST_h #define CMDLIST_h +typedef struct CMD_t { + char* cmd; + void (*fn)(char* line); +} CMD_t; + typedef struct CMDList_s { void* a; void* b; @@ -28,9 +33,12 @@ typedef struct CMDList_s { void* x; void* y; void* z; - CMD_t cmd; + CMD_t* cmd; } CMDList_t; + + + // initilises a CMDList_t with all null pointers extern CMDList_t* CMDList_init(); @@ -38,7 +46,7 @@ extern CMDList_t* CMDList_init(); extern int CMDList_deinit(CMDList_t *list); // add a command in the command tree -extern int CMDList_add(CMDList_t *list, void* cmd); +extern int CMDList_add(CMDList_t *list, CMD_t* cmd); // search command in the tree extern int CMDList_get(CMDList_t *list, char* cmd); From 8d6469075cd0176e48621327ddd99403798809c2 Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 6 Apr 2024 00:03:13 +0200 Subject: [PATCH 2/7] update CMDList_add --- CMDList/CMDList.c | 60 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index dde5b50..dd89e2b 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -31,32 +31,60 @@ int CMDList_deinit(CMDList_t *list) // add a command in the command tree int CMDList_add(CMDList_t *list, CMD_t* cmd) { - char* read_p = cmd; + int returnCode = 0; + char* read_p = cmd->cmd; CMDList_t* list_p = list; + if (read_p == NULL) + { + returnCode = -1; + } + while ( - (read_p != NULL) - && (*read_p != ' ') // space ends the command - && (*read_p != '\n') // end of line - && (*read_p != '\r') // end of line - && (*read_p != 0) // end of string - && (read_p - (char*)cmd < 500) + ((read_p - cmd->cmd) < (500 * sizeof(char))) + && (returnCode == 0) ) { - char c = *read_p & (~0x20); // to uppercase - if ((*read_p >= 'A') && (*read_p <= 'Z')) - { - read_p += (*read_p - 'A') * sizeof(CMDList_t*); - if (*((CMDList_t**)read_p) == NULL) - { - read_p = malloc(sizeof(CMDList_t)); + char c = *read_p & (~0x20); // convert to uppercase + if ((c >= 'A') && (c <= 'Z')) + { // valid character + + // get memory address of the caracter in the CMDList + list_p += (c - 'A') * sizeof(CMDList_t*); + + if (*((CMDList_t**)list_p) == NULL) + { // it does not point to another list + // allocate a new list for it + *((CMDList_t**)list_p) = malloc(sizeof(CMDList_t)); + if (list_p == NULL) + { // allocation faild (memory full?) + returnCode = -2; + } } else - { - read_p = *((CMDList_t**)read_p); + { // it points to an other list + // update pointer to this list + *((CMDList_t**)list_p) = *((CMDList_t**)read_p); } + // update list_p for the next list + list_p = *((CMDList_t**)list_p); } + else + { // invalid caracter + break; + } + + // read next character + read_p += sizeof(char); } + + if (returnCode >= 0) + { // no errors + // add command in it's spot + list_p->cmd = cmd; + } + + return returnCode; } // search command in the tree From d99ad2ad6db94b16adc53d87716475c0210b440b Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 6 Apr 2024 01:42:50 +0200 Subject: [PATCH 3/7] trying to fix CMDList --- CMDList/CMDList.c | 125 ++++++++++++++++++++++++++++++++++++++-------- CMDList/CMDList.h | 2 +- 2 files changed, 106 insertions(+), 21 deletions(-) diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index dd89e2b..0b7d272 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -1,7 +1,9 @@ #include "CMDList.h" #include +#include #include +#include // initilises a CMDList_t with all NULL pointers CMDList_t* CMDList_init() @@ -33,41 +35,121 @@ int CMDList_add(CMDList_t *list, CMD_t* cmd) { int returnCode = 0; char* read_p = cmd->cmd; - CMDList_t* list_p = list; + CMDList_t** list_p = (CMDList_t**)list; if (read_p == NULL) - { + { // cmd is missing a string for the commnad name returnCode = -1; } - while ( - ((read_p - cmd->cmd) < (500 * sizeof(char))) - && (returnCode == 0) - ) + while (returnCode == 0) { char c = *read_p & (~0x20); // convert to uppercase if ((c >= 'A') && (c <= 'Z')) { // valid character // get memory address of the caracter in the CMDList - list_p += (c - 'A') * sizeof(CMDList_t*); + list_p += (c - 'A'); - if (*((CMDList_t**)list_p) == NULL) + if (*list_p == NULL) { // it does not point to another list + printf("create new sub list\n"); // allocate a new list for it - *((CMDList_t**)list_p) = malloc(sizeof(CMDList_t)); + *list_p = CMDList_init(); if (list_p == NULL) { // allocation faild (memory full?) returnCode = -2; + printf("ERROR: failt to init CMDList\n"); } + + // update list_p for the next list + list_p = (CMDList_t**)(*list_p); + } + else + { // it points to an other list + printf("move to sub list\n"); + // update pointer to this list + list_p = (CMDList_t**)(*list_p); + } + } + else + { // invalid caracter + printf("invaled char\n"); + break; + } + + // read next character + read_p += 1; + } + + printf("return code: %i\n", returnCode); + if (returnCode >= 0) + { // no errors + // add command in it's spot + (**list_p).cmd = cmd; + } + + printf("return code: %i\n", returnCode); + + return returnCode; +} + +// search command in the tree +CMD_t* CMDList_get(CMDList_t *list, char* cmd) +{ + bool found = true; + CMD_t* cmd_object = NULL; + char* read_p = cmd; + CMDList_t* list_p = list; + + while (found) + { + char c = *read_p & (~0x20); // convert to uppercase + if ((c >= 'A') && (c <= 'Z')) + { // valid character + + // get memory address of the caracter in the CMDList + // list_p += (c - 'A') * sizeof(CMDList_t*); + + switch (c) + { + case 'A': list_p = list_p->a; break; + case 'B': list_p = list_p->b; break; + case 'C': list_p = list_p->c; break; + case 'D': list_p = list_p->d; break; + case 'E': list_p = list_p->e; break; + case 'F': list_p = list_p->f; break; + case 'G': list_p = list_p->g; break; + case 'H': list_p = list_p->h; break; + case 'I': list_p = list_p->i; break; + case 'J': list_p = list_p->j; break; + case 'K': list_p = list_p->k; break; + case 'L': list_p = list_p->l; break; + case 'M': list_p = list_p->m; break; + case 'N': list_p = list_p->n; break; + case 'O': list_p = list_p->o; break; + case 'P': list_p = list_p->p; break; + case 'Q': list_p = list_p->q; break; + case 'R': list_p = list_p->r; break; + case 'S': list_p = list_p->s; break; + case 'T': list_p = list_p->t; break; + case 'U': list_p = list_p->u; break; + case 'V': list_p = list_p->v; break; + case 'W': list_p = list_p->w; break; + case 'X': list_p = list_p->x; break; + case 'Y': list_p = list_p->y; break; + case 'Z': list_p = list_p->z; break; + } + + if (*((CMDList_t**)list_p) == NULL) + { // no command found + found = false; } else { // it points to an other list // update pointer to this list - *((CMDList_t**)list_p) = *((CMDList_t**)read_p); + list_p = *((CMDList_t**)list_p); } - // update list_p for the next list - list_p = *((CMDList_t**)list_p); } else { // invalid caracter @@ -78,14 +160,17 @@ int CMDList_add(CMDList_t *list, CMD_t* cmd) read_p += sizeof(char); } - if (returnCode >= 0) - { // no errors - // add command in it's spot - list_p->cmd = cmd; + if (found) + { // list found for command + if (list_p->cmd == NULL) + { // command incomplete + found = false; + } + else + { + cmd_object = list_p->cmd; + } } - return returnCode; + return cmd_object; } - -// search command in the tree -int CMDList_get(CMDList_t *list, char* cmd); diff --git a/CMDList/CMDList.h b/CMDList/CMDList.h index b19004f..df580c8 100644 --- a/CMDList/CMDList.h +++ b/CMDList/CMDList.h @@ -49,6 +49,6 @@ extern int CMDList_deinit(CMDList_t *list); extern int CMDList_add(CMDList_t *list, CMD_t* cmd); // search command in the tree -extern int CMDList_get(CMDList_t *list, char* cmd); +extern CMD_t* CMDList_get(CMDList_t *list, char* cmd); #endif \ No newline at end of file From b350e78f7d934389c6b7412b23c106f212411ba7 Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 6 Apr 2024 13:36:47 +0200 Subject: [PATCH 4/7] fix segmentaion fault in CMDList --- CMDList/CMDList.c | 50 ++++++++++++----------------------------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index 0b7d272..bf494c5 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -53,13 +53,11 @@ int CMDList_add(CMDList_t *list, CMD_t* cmd) if (*list_p == NULL) { // it does not point to another list - printf("create new sub list\n"); // allocate a new list for it *list_p = CMDList_init(); if (list_p == NULL) { // allocation faild (memory full?) returnCode = -2; - printf("ERROR: failt to init CMDList\n"); } // update list_p for the next list @@ -67,14 +65,12 @@ int CMDList_add(CMDList_t *list, CMD_t* cmd) } else { // it points to an other list - printf("move to sub list\n"); // update pointer to this list list_p = (CMDList_t**)(*list_p); } } else { // invalid caracter - printf("invaled char\n"); break; } @@ -82,14 +78,17 @@ int CMDList_add(CMDList_t *list, CMD_t* cmd) read_p += 1; } - printf("return code: %i\n", returnCode); if (returnCode >= 0) { // no errors + + if (((CMDList_t*)list_p)->cmd != NULL) + { // replacing a command + returnCode = 1; + } // add command in it's spot - (**list_p).cmd = cmd; + ((CMDList_t*)list_p)->cmd = cmd; } - printf("return code: %i\n", returnCode); return returnCode; } @@ -109,37 +108,7 @@ CMD_t* CMDList_get(CMDList_t *list, char* cmd) { // valid character // get memory address of the caracter in the CMDList - // list_p += (c - 'A') * sizeof(CMDList_t*); - - switch (c) - { - case 'A': list_p = list_p->a; break; - case 'B': list_p = list_p->b; break; - case 'C': list_p = list_p->c; break; - case 'D': list_p = list_p->d; break; - case 'E': list_p = list_p->e; break; - case 'F': list_p = list_p->f; break; - case 'G': list_p = list_p->g; break; - case 'H': list_p = list_p->h; break; - case 'I': list_p = list_p->i; break; - case 'J': list_p = list_p->j; break; - case 'K': list_p = list_p->k; break; - case 'L': list_p = list_p->l; break; - case 'M': list_p = list_p->m; break; - case 'N': list_p = list_p->n; break; - case 'O': list_p = list_p->o; break; - case 'P': list_p = list_p->p; break; - case 'Q': list_p = list_p->q; break; - case 'R': list_p = list_p->r; break; - case 'S': list_p = list_p->s; break; - case 'T': list_p = list_p->t; break; - case 'U': list_p = list_p->u; break; - case 'V': list_p = list_p->v; break; - case 'W': list_p = list_p->w; break; - case 'X': list_p = list_p->x; break; - case 'Y': list_p = list_p->y; break; - case 'Z': list_p = list_p->z; break; - } + list_p += (c - 'A'); if (*((CMDList_t**)list_p) == NULL) { // no command found @@ -165,12 +134,17 @@ CMD_t* CMDList_get(CMDList_t *list, char* cmd) if (list_p->cmd == NULL) { // command incomplete found = false; + cmd_object = NULL; } else { cmd_object = list_p->cmd; } } + else + { + cmd_object = NULL; + } return cmd_object; } From 1fee11fdba7372881c1b88120c89dd077c411d07 Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 6 Apr 2024 13:59:18 +0200 Subject: [PATCH 5/7] update CMDList_get --- CMDList/CMDList.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index bf494c5..ad2f030 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -75,7 +75,7 @@ int CMDList_add(CMDList_t *list, CMD_t* cmd) } // read next character - read_p += 1; + read_p++; } if (returnCode >= 0) @@ -99,10 +99,11 @@ CMD_t* CMDList_get(CMDList_t *list, char* cmd) bool found = true; CMD_t* cmd_object = NULL; char* read_p = cmd; - CMDList_t* list_p = list; + CMDList_t** list_p = (CMDList_t**)list; while (found) { + //TODO: mayby make a copy before converting to upper case char c = *read_p & (~0x20); // convert to uppercase if ((c >= 'A') && (c <= 'Z')) { // valid character @@ -110,14 +111,14 @@ CMD_t* CMDList_get(CMDList_t *list, char* cmd) // get memory address of the caracter in the CMDList list_p += (c - 'A'); - if (*((CMDList_t**)list_p) == NULL) + if (*list_p == NULL) { // no command found found = false; } else { // it points to an other list // update pointer to this list - list_p = *((CMDList_t**)list_p); + list_p = (CMDList_t**)(*list_p); } } else @@ -126,19 +127,19 @@ CMD_t* CMDList_get(CMDList_t *list, char* cmd) } // read next character - read_p += sizeof(char); + read_p++; } if (found) { // list found for command - if (list_p->cmd == NULL) + if (((CMDList_t*)list_p)->cmd == NULL) { // command incomplete found = false; cmd_object = NULL; } else { - cmd_object = list_p->cmd; + cmd_object = ((CMDList_t*)list_p)->cmd; } } else From 8a8348fc598024bb54e3049b5b97e1e247e28e38 Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 6 Apr 2024 14:21:49 +0200 Subject: [PATCH 6/7] try fix the deinit, but no luck --- CMDList/CMDList.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/CMDList/CMDList.c b/CMDList/CMDList.c index ad2f030..ff9cca1 100644 --- a/CMDList/CMDList.c +++ b/CMDList/CMDList.c @@ -14,19 +14,24 @@ CMDList_t* CMDList_init() } // free up the full tree from memory +// does not free up the commands int CMDList_deinit(CMDList_t *list) { - CMDList_t* chars[26]; //TODO: cast list to array + CMDList_t** list_p = (CMDList_t**)list; + // printf("deinit %p\n", (void*)list); + // printf("deinit e %p\n", list->e); for (int i = 0; i < 26; i++) { - if (chars[i] != NULL) + if (*(list_p + i) != NULL) { - CMDList_deinit(chars[i]); + // printf("deinit %i\n", i); + CMDList_deinit(*(list_p + i)); } } - free(list); + //TODO: fix "free(): invalid pointer" + // free(list); return 0; } From 96e48e2523733856e7ddd9d55d77b31e5c0264a6 Mon Sep 17 00:00:00 2001 From: FReenen Date: Sat, 6 Apr 2024 17:21:03 +0200 Subject: [PATCH 7/7] update CLI header --- CLI/CLI.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CLI/CLI.h b/CLI/CLI.h index a3b65a8..e7d72e6 100644 --- a/CLI/CLI.h +++ b/CLI/CLI.h @@ -3,12 +3,13 @@ #include -#include "../CMDList.h" +#include "../CMDList/CMDList.h" typedef CLI_lineOutFn(char* line) CLI_lineoutFn_t; // initilize and register the lineout print function -bool init(CLI_lineoutFn_t* lineOut, CMDList_t* cmdList); +bool CLI_init(CLI_lineoutFn_t* lineOut, CMDList_t* cmdList); +bool CLI_deinit(); // to recive a single caracter bool CLI_charIn(char c);