Skip to content

Commit

Permalink
Implemented cli help command
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldegrasse committed Aug 10, 2020
1 parent d145c5a commit 8fc0b5b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
13 changes: 11 additions & 2 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
* This code assumes that the connected terminal emulates a VT-100.
*/


/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/System.h>

#include <stdbool.h>
#include <string.h>
#include <stdio.h>
Expand Down Expand Up @@ -52,7 +57,11 @@ void cli_printf(CLIContext *context, const char *format, ...) {
// Init variable args list.
va_start(args, format);
// Print to buffer and destroy varargs list.
num_print = vsnprintf(output_buf, PRINT_BUFLEN, format, args);
/*
* Note: newlib's implementation of vnsprintf appears to require a heap,
* which we do not have. Use the xdc version.
*/
num_print = System_vsnprintf(output_buf, PRINT_BUFLEN, format, args);
va_end(args);
context->cli_write(output_buf, num_print);
}
Expand Down Expand Up @@ -295,4 +304,4 @@ static void move_line_index(CLIContext *context, bool forwards) {
new_idx = CLI_BUFCNT - 1;
context->line_idx = new_idx;
}
}
}
2 changes: 1 addition & 1 deletion cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/** CLI configuration parameters */
#define CLI_MAX_LINE 80 // max command length
#define CLI_HISTORY 3 // max number of past commands to store
#define PRINT_BUFLEN 80 // size of printf buffer to use.
#define PRINT_BUFLEN 256 // size of printf buffer to use.

#define CLI_BUFCNT CLI_HISTORY + 2 // Used internally for CLI buffer length

Expand Down
76 changes: 55 additions & 21 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,40 @@

typedef struct {
char *cmd_name;
int (*cmd_fxn)(CLIContext *, char**, int);
int (*cmd_fxn)(CLIContext *, char **, int);
char *cmd_help;
} CmdEntry;

#include <string.h>

/*
* Maximum number of arguments that the parser will handle.
* Maximum number of arguments that the parser will handle.
* Includes command name.
*/
#define MAX_ARGV 8

#define MAX_ARGC 8
/*
* Delimiter used for parsing arguments.
* Add any other delimeters to this string.
*/
#define DELIMETER " "

static int help(CLIContext *cxt, char **argv, int argc);

/**
* Declaration of commands. Syntax is as follows:
* {"NAME_OF_COMMAND", command_function, "HELP_STRING"}
* The command function follows the signature of "main", but with a
* The command function follows the signature of "main", but with a
* Context Parameter, ex:
* int command_function(CLIContext *ctx, char** argv, int argc)
* A return value of zero indicates success, anything else indicates failure.
*/

const CmdEntry COMMANDS[] = {
{"help", help, "Prints help for this commandline"},
{"help", help,
"Prints help for this commandline.\r\n"
"supply the name of a command after \"help\" for help with that command"},
// Add more entries here.
{NULL, NULL, NULL}
};


{NULL, NULL, NULL}};

/**
* Handles a command, as given by the null terminated string "cmd"
Expand All @@ -45,30 +48,61 @@ const CmdEntry COMMANDS[] = {
* @return 0 on successful handling, or another value on failure.
*/
int handle_command(CLIContext *ctx, char *cmd) {
char *arguments[MAX_ARGV];
const CmdEntry *entry;
int argc;
char *arguments[MAX_ARGC], *saveptr;
// The parser interprets a space as a delimeter between arguments.
(void)arguments;
ctx->cli_write("This will work\r\n", 16);
cli_printf(ctx, "Hello there");
// Init strtok_r.
arguments[0] = strtok_r(cmd, DELIMETER, &saveptr);
// Parse the rest of the arguments.
for (argc = 1; argc < MAX_ARGC; argc++) {
arguments[argc] = strtok_r(NULL, DELIMETER, &saveptr);
if (arguments[argc] == NULL) {
// Out of args to parse, break.
break;
}
}
// Now, find the command to run.
entry = COMMANDS;
while (entry->cmd_name != NULL) {
if (strcmp(entry->cmd_name, arguments[0]) == 0) {
return entry->cmd_fxn(ctx, arguments, argc);
}
entry++;
}
// If we exit the loop, we don't recognize this command.
cli_printf(ctx, "Warning: unknown command. Try \"help\". \r\n");
return 0;
}


/**
* Help function. Prints avaliable commandline targets.
* @param argv: list of all string arguments given (first will be "help")
* @param argc: length of the argv array.
*/
static int help(CLIContext *ctx, char **argv, int argc) {
CmdEntry *entry;
const CmdEntry *entry = COMMANDS;
if (argc == 1) {
cli_printf(ctx, "Test\n");
entry = (CmdEntry*)&COMMANDS[0];
cli_printf(ctx, "Avaliable Commands:\r\n");
while (entry->cmd_name != NULL) {
// Write command name and newline
cli_printf(ctx, "%s\n", entry->cmd_name);
cli_printf(ctx, "%s\r\n", entry->cmd_name);
entry++;
}
return 0;
} else if (argc == 2) {
while (entry->cmd_name != NULL) {
if (strcmp(entry->cmd_name, argv[1]) == 0) {
// Print help for this command.
cli_printf(ctx, "%s: %s\r\n", entry->cmd_name, entry->cmd_help);
return 0;
}
}
// If we make it here, the command name was unknown.
cli_printf(ctx, "Unknown command: %s\r\n", argv[1]);
return 255;
}
return 0;
}
// If neither of the above conditions are triggered, print error.
cli_printf(ctx, "Unsupported number of arguments\r\n");
return 255;
}
2 changes: 1 addition & 1 deletion sd_logger.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -508,5 +508,5 @@ Program.global.heartbeat = Task.create("&heartBeatFxn", task0Params);
var task1Params = new Task.Params();
task1Params.instance.name = "uartconsole";
// Must be larger to hold CLI history buffer.
task1Params.stackSize = 1024;
task1Params.stackSize = 2048;
Program.global.uartconsole = Task.create("&uart_task_entry", task1Params);

0 comments on commit 8fc0b5b

Please sign in to comment.