Merge branch 'master' into Dennis_buffer
This commit is contained in:
@@ -3,12 +3,13 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
#include "CMDList.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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));
|
||||
@@ -10,52 +14,143 @@ CMDList_t* CMDList_init()
|
||||
}
|
||||
|
||||
// free up the full tree from memory
|
||||
// does not free up the commands
|
||||
int CMDList_deinit(CMDList_t *list)
|
||||
{
|
||||
void* chars[26] = list;
|
||||
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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
int returnCode = 0;
|
||||
char* read_p = cmd->cmd;
|
||||
CMDList_t** list_p = (CMDList_t**)list;
|
||||
|
||||
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 - cmd < 500)
|
||||
)
|
||||
if (read_p == NULL)
|
||||
{ // cmd is missing a string for the commnad name
|
||||
returnCode = -1;
|
||||
}
|
||||
|
||||
while (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');
|
||||
|
||||
if (*list_p == NULL)
|
||||
{ // it does not point to another list
|
||||
// allocate a new list for it
|
||||
*list_p = CMDList_init();
|
||||
if (list_p == NULL)
|
||||
{ // allocation faild (memory full?)
|
||||
returnCode = -2;
|
||||
}
|
||||
|
||||
// update list_p for the next list
|
||||
list_p = (CMDList_t**)(*list_p);
|
||||
}
|
||||
else
|
||||
{
|
||||
read_p = *((CMDList_t**)read_p)
|
||||
{ // it points to an other list
|
||||
// update pointer to this list
|
||||
list_p = (CMDList_t**)(*list_p);
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // invalid caracter
|
||||
break;
|
||||
}
|
||||
|
||||
// read next character
|
||||
read_p++;
|
||||
}
|
||||
|
||||
if (returnCode >= 0)
|
||||
{ // no errors
|
||||
|
||||
if (((CMDList_t*)list_p)->cmd != NULL)
|
||||
{ // replacing a command
|
||||
returnCode = 1;
|
||||
}
|
||||
// add command in it's spot
|
||||
((CMDList_t*)list_p)->cmd = cmd;
|
||||
}
|
||||
|
||||
|
||||
return returnCode;
|
||||
}
|
||||
|
||||
// search command in the tree
|
||||
int CMDList_get(CMDList_t *list, char* cmd);
|
||||
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 = (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
|
||||
|
||||
// get memory address of the caracter in the CMDList
|
||||
list_p += (c - 'A');
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // invalid caracter
|
||||
break;
|
||||
}
|
||||
|
||||
// read next character
|
||||
read_p++;
|
||||
}
|
||||
|
||||
if (found)
|
||||
{ // list found for command
|
||||
if (((CMDList_t*)list_p)->cmd == NULL)
|
||||
{ // command incomplete
|
||||
found = false;
|
||||
cmd_object = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd_object = ((CMDList_t*)list_p)->cmd;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd_object = NULL;
|
||||
}
|
||||
|
||||
return cmd_object;
|
||||
}
|
||||
|
||||
@@ -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,9 +46,9 @@ 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);
|
||||
extern CMD_t* CMDList_get(CMDList_t *list, char* cmd);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user