Skip to content

Commit

Permalink
register command and print header added
Browse files Browse the repository at this point in the history
  • Loading branch information
the-this-pointer committed Apr 13, 2023
1 parent e6d0cbe commit 5e0e696
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
10 changes: 6 additions & 4 deletions inc/shellb.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
typedef struct
{
const char* start;
uint8_t len;
} argv_t;

typedef void(*shellb_cmd_func_t)(int argc, argv_t* argv);
Expand All @@ -26,10 +25,9 @@ typedef struct {
typedef struct {
uint8_t cmd_count;
shellb_cmd_t* cmd_list;
void* _next;
} shellb_cmd_table_t;

extern shellb_cmd_table_t shellb_g_cmd_table;

void shellb_platform_uninit();
void shellb_platform_write(const char* str);
void shellb_platform_read();
Expand All @@ -38,7 +36,11 @@ void shellb_init();
void shellb_uninit();
void shellb_wait_for_cmd();
void shellb_process_cmd();
void shellb_print_header();
void shellb_print_error(const char* error);
void sheelb_register_cmd(shellb_cmd_table_t* commands);

uint8_t shellb_create_argv(argv_t* argv);
shellb_cmd_t* shellb_get_cmd(const char* name, uint8_t len);
shellb_cmd_t* shellb_get_cmd(const char* name, uint8_t argc);

#endif //SHELLB_H
1 change: 0 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
int main() {
shellb_init();

shellb_wait_for_cmd();
shellb_wait_for_cmd();

shellb_uninit();
Expand Down
82 changes: 66 additions & 16 deletions src/shellb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#include <stdbool.h>

char shellb_g_buffer[SHELLB_BUFFER_SIZE];
shellb_cmd_table_t shellb_g_cmd_table;
shellb_cmd_table_t* shellb_cmd_table;

void shellb_init()
{
shellb_cmd_table = NULL;

shellb_print_header();
}

void shellb_uninit()
Expand All @@ -24,49 +27,96 @@ void shellb_process_cmd()
{
argv_t argv[SHELLB_MAX_ARGS] = {0};
uint8_t argc = shellb_create_argv(argv);
if (argc == 0)
return;
if (argc == 0) {
shellb_print_error("invalid input");
goto wait;
}

shellb_cmd_t *cmd = shellb_get_cmd(argv[0].start, argv[0].len);
if (!cmd)
return;
shellb_cmd_t *cmd = shellb_get_cmd(argv[0].start, argc);
if (!cmd) {
shellb_print_error("invalid command");
goto wait;
}

cmd->func(argc, argv);
wait:
shellb_wait_for_cmd();
}

void shellb_print_header()
{
shellb_platform_write("=============================\n");
shellb_platform_write(" __ __ \n"
" ( / _ /// _) \n"
"__)/)(-((/(_) \n"
" \n");
shellb_platform_write("Build: " __DATE__ " " __TIME__ "\n");
shellb_platform_write("=============================\n");
}

void shellb_print_error(const char* error)
{
shellb_platform_write("\nERROR: ");
shellb_platform_write(error);
shellb_platform_write("\n");
}

void sheelb_register_cmd(shellb_cmd_table_t* commands)
{
shellb_cmd_table_t* curr = shellb_cmd_table;
if (!curr)
{
curr = commands;
return;
}

while (curr->_next)
curr = curr->_next;

curr->_next = commands;
}

uint8_t shellb_create_argv(argv_t* argv)
{
uint8_t argc = 0;
bool qouted = false;
bool cmd = false;
char* curr = shellb_g_buffer;
while(*curr != '\0')
{
if (*curr == '"') {
qouted ^= 1;
}

if (argv[argc].len == 0)
if (!cmd)
argv[argc].start = curr;

if ((*curr == ' ' && !qouted && argv[argc].len > 0) || (*curr == '"' && !qouted)) {
if ((*curr == ' ' && !qouted && cmd) || (*curr == '"' && !qouted)) {
argc++;
*curr = '\0';
cmd = false;
if (argc >= SHELLB_MAX_ARGS)
break;
}

argv[argc].len++;
else if (!cmd && *curr != ' ' && !(*curr == '"' && qouted))
cmd = true;
curr++;
}
argc++;
return argc;
}

shellb_cmd_t* shellb_get_cmd(const char* name, uint8_t len)
shellb_cmd_t* shellb_get_cmd(const char* name, uint8_t argc)
{
for (int i = 0; i < shellb_g_cmd_table.cmd_count; ++i) {
shellb_cmd_t* curr = &shellb_g_cmd_table.cmd_list[i];
uint8_t cmp_len = len <= strlen(curr->name)? len: strlen(curr->name);
if (strncasecmp(name, curr->name, cmp_len) == 0)
return curr;
shellb_cmd_table_t* curr_table = shellb_cmd_table;
while (curr_table) {
for (int i = 0; i < curr_table->cmd_count; ++i) {
shellb_cmd_t* curr = &curr_table->cmd_list[i];
uint8_t cmp_len = strlen(name) <= strlen(curr->name)? strlen(name): strlen(curr->name);
if (strncasecmp(name, curr->name, cmp_len) == 0 && argc >= curr->argc_min && argc <= curr->argc_max)
return curr;
}
curr_table = curr_table->_next;
}

return NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/shellb_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ void shellb_platform_write(const char* str)

void* shellb_read_thread_func(void* arg)
{
scanf("%s", arg);
scanf(" %[^\n]", arg);
shellb_g_reading = false;
shellb_process_cmd();
pthread_exit(NULL);
}
Expand Down

0 comments on commit 5e0e696

Please sign in to comment.