update CLI

This commit is contained in:
Laila van Reenen 2024-04-08 13:39:24 +02:00
parent 87ca5d0e72
commit feec2a0a91
4 changed files with 107 additions and 11 deletions

View File

@ -1,13 +1,16 @@
#include "CLI.h" #include "CLI.h"
#include "../FIFOBuffChar.h" #include <stddef.h>
#include <stdlib.h>
#include "../FIFOBuff/FIFOBuffChar.h"
void (*CLI_lineOut)(char* line); void (*CLI_lineOut)(char* line);
CMDList_t* CMDList; CMDList_t* CMDList;
FIFOBuffChar_t* FIFO; FIFOBuffChar_t* FIFO;
// initilize and register the lineout print function // 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; CLI_lineOut = lineOut;
CMDList = cmdList; CMDList = cmdList;
@ -42,7 +45,7 @@ char* fifoToString(FIFOBuffChar_t* fifo)
int tryExecute(FIFOBuffChar_t* fifo) int tryExecute(FIFOBuffChar_t* fifo)
{ {
char* line = fifoToString(fifo); char* line = fifoToString(fifo);
CMD_t cmd = CMDList_get(CMDList, line); CMD_t* cmd = CMDList_get(CMDList, line);
if (cmd != NULL) if (cmd != NULL)
{ {
@ -51,6 +54,7 @@ int tryExecute(FIFOBuffChar_t* fifo)
} }
else else
{ {
printf("command not found: %s\n", line);
return false; return false;
} }
} }
@ -58,6 +62,7 @@ int tryExecute(FIFOBuffChar_t* fifo)
// to recive a single caracter // to recive a single caracter
bool CLI_charIn(char c) bool CLI_charIn(char c)
{ {
bool ok = true;
if ((c >= 'a') && (c <= 'z')) if ((c >= 'a') && (c <= 'z'))
{ {
c &= (~0x20); // convert to uppercase c &= (~0x20); // convert to uppercase
@ -66,13 +71,16 @@ bool CLI_charIn(char c)
if ( //TODO: update list of accepted characters if ( //TODO: update list of accepted characters
((c >= 'A') && (c <= 'Z')) ((c >= 'A') && (c <= 'Z'))
|| ((c >= '0') && (c <= '9')) || ((c >= '0') && (c <= '9'))
|| (c = ' ') || (c == ' ')
|| (c = '-') || (c == '-')
|| (c = '_') || (c == '_')
) )
{ {
FIFOBuffChar_put(FIFO, c); // save char in buffer 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 else
{ {
@ -80,10 +88,13 @@ bool CLI_charIn(char c)
{ {
case '\n': case '\n':
case '\r': case '\r':
(*CLI_lineOut)(&c); // echo to terminal if (CLI_lineOut != NULL)
{ // echo to terminal
(*CLI_lineOut)(&c);
}
FIFOBuffChar_t* fifo = FIFO; FIFOBuffChar_t* fifo = FIFO;
FIFO = FIFOBuffChar_create(); FIFO = FIFOBuffChar_create();
tryExecute(fifo); ok = tryExecute(fifo);
FIFOBuffChar_delete(fifo); FIFOBuffChar_delete(fifo);
break; break;
@ -92,4 +103,6 @@ bool CLI_charIn(char c)
break; break;
} }
} }
return ok;
} }

View File

@ -5,10 +5,10 @@
#include "../CMDList/CMDList.h" #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 // 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(); bool CLI_deinit();
// to recive a single caracter // to recive a single caracter

75
CMDList/printList.c Normal file
View File

@ -0,0 +1,75 @@
#ifndef PRINTCMDLIST_H
#define PRINTCMDLIST_H
#include "CMDList.h"
#include "printList.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
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

8
CMDList/printList.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef PRINTCMDLIST_H
#define PRINTCMDLIST_H
#include "CMDList.h"
extern void printCMDList(CMDList_t* list, char* indent);
#endif