2021-09-19 19:16:41 -04:00
|
|
|
/**
|
|
|
|
* Implementation of the command line interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
2021-09-25 17:26:23 -04:00
|
|
|
#include "log.h"
|
2021-09-19 19:16:41 -04:00
|
|
|
#include "types.h"
|
|
|
|
#include "syscalls.h"
|
|
|
|
#include "cli/cli.h"
|
|
|
|
#include "cli/dos_cmds.h"
|
|
|
|
|
|
|
|
#define MAX_COMMAND_SIZE 128
|
2021-09-25 17:26:23 -04:00
|
|
|
#define MAX_ARGC 32
|
2021-09-19 19:16:41 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Types
|
|
|
|
//
|
|
|
|
|
|
|
|
// Structure to hold a record about a command...
|
|
|
|
|
|
|
|
typedef struct s_cli_command {
|
|
|
|
char *name;
|
|
|
|
char *help;
|
|
|
|
cli_cmd_handler handler;
|
|
|
|
} t_cli_command, *p_cli_command;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Commands
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern const t_cli_command g_cli_commands[];
|
|
|
|
|
|
|
|
//
|
|
|
|
// List all the commands
|
|
|
|
//
|
2021-09-25 17:26:23 -04:00
|
|
|
int cmd_help(short channel, int argc, char * argv[]) {
|
2021-09-19 19:16:41 -04:00
|
|
|
p_cli_command command;
|
|
|
|
|
|
|
|
for (command = g_cli_commands; (command != 0) && (command->name != 0); command++) {
|
|
|
|
sys_chan_write(channel, command->help, strlen(command->help));
|
|
|
|
sys_chan_write(channel, "\n", 2);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// CLI variables
|
|
|
|
//
|
|
|
|
|
|
|
|
const t_cli_command g_cli_commands[] = {
|
2021-09-24 19:49:13 -04:00
|
|
|
{ "?", "? -- print this helpful message", cmd_help },
|
|
|
|
{ "HELP", "HELP -- print this helpful message", cmd_help },
|
2021-09-19 19:16:41 -04:00
|
|
|
{ "DIR", "DIR <path> -- print directory listing", cmd_dir },
|
2021-09-24 19:49:13 -04:00
|
|
|
{ "LOAD", "LOAD <path> -- load a file into memory", cmd_load },
|
2021-09-24 12:05:45 -04:00
|
|
|
{ "TYPE", "TYPE <path> -- print the contents of a text file", cmd_type },
|
2021-09-19 19:16:41 -04:00
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Attempt to execute a command
|
|
|
|
//
|
|
|
|
// Inputs:
|
|
|
|
// command = the upper case name of the command (first word of the command line)
|
|
|
|
// parameters = the string of parameters to be passed to the command
|
|
|
|
//
|
2021-09-25 17:26:23 -04:00
|
|
|
short cli_exec(short channel, char * command, int argc, char * argv[]) {
|
2021-09-19 19:16:41 -04:00
|
|
|
const char * cmd_not_found = "Command not found.\n";
|
2021-09-25 17:26:23 -04:00
|
|
|
p_cli_command commands = g_cli_commands;
|
2021-09-19 19:16:41 -04:00
|
|
|
|
2021-09-25 17:26:23 -04:00
|
|
|
log3(LOG_INFO, "cli_exec: '", argv[0], "'");
|
|
|
|
log_num(LOG_INFO, "argc = ", argc);
|
2021-09-19 19:16:41 -04:00
|
|
|
|
|
|
|
while ((commands != 0) && (commands->name != 0)) {
|
|
|
|
// Does the command match the name?
|
|
|
|
if (strcmp(commands->name, command) == 0) {
|
|
|
|
// Found it, execute the handler
|
2021-09-25 17:26:23 -04:00
|
|
|
return commands->handler(channel, argc, argv);
|
2021-09-19 19:16:41 -04:00
|
|
|
} else {
|
|
|
|
// No match, keep checking...
|
|
|
|
commands++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Built in command not found..
|
|
|
|
// TODO: search the current drive for an executable file
|
|
|
|
sys_chan_write(channel, cmd_not_found, strlen(cmd_not_found));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-09-25 17:26:23 -04:00
|
|
|
char * strtok_r(char * source, const char * delimiter, char ** saveptr) {
|
|
|
|
char * x = *saveptr;
|
|
|
|
char * y;
|
|
|
|
|
|
|
|
/* Skip over leading delimiters */
|
|
|
|
for (x = *saveptr; *x && (*x == delimiter[0]); x++) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we reached the end of the string, return NULL */
|
|
|
|
if (*x == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (y = x; *y && (*y != delimiter[0]); y++) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we reached the end of the string, return x */
|
|
|
|
if (*y == 0) {
|
|
|
|
*saveptr = y;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, make that position in the source string NULL, and return x */
|
|
|
|
*y = 0;
|
|
|
|
*saveptr = y + 1;
|
|
|
|
return x;
|
|
|
|
}
|
2021-09-19 19:16:41 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Enter the CLI's read-eval-print loop
|
|
|
|
//
|
|
|
|
short cli_repl(short channel) {
|
2021-09-25 17:26:23 -04:00
|
|
|
char command_line[MAX_COMMAND_SIZE];
|
|
|
|
char * arg;
|
|
|
|
char * token_save;
|
|
|
|
char * delim = " ";
|
|
|
|
int argc = 0;
|
|
|
|
char * argv[MAX_ARGC];
|
2021-09-19 19:16:41 -04:00
|
|
|
|
|
|
|
const char * welcome = "\n\nFoenix/MCP Command Line Utility... online.\nType \"HELP\" or \"?\" for help.\n\n";
|
|
|
|
sys_chan_write(channel, welcome, strlen(welcome));
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
sys_chan_write(channel, "\n> ", 3); // Print our prompt
|
|
|
|
sys_chan_readline(channel, command_line, MAX_COMMAND_SIZE); // Attempt to read line
|
|
|
|
sys_chan_write(channel, "\n", 1);
|
|
|
|
|
2021-09-25 17:26:23 -04:00
|
|
|
for (argc = 0, token_save = command_line; argc < MAX_ARGC; argc++) {
|
|
|
|
arg = strtok_r(command_line, delim, &token_save);
|
|
|
|
if (arg != 0) {
|
|
|
|
argv[argc] = arg;
|
|
|
|
} else {
|
|
|
|
break;
|
2021-09-19 19:16:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 17:26:23 -04:00
|
|
|
if (argc > 0) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < strlen(argv[0]); i++) {
|
|
|
|
argv[0][i] = toupper(argv[0][i]);
|
|
|
|
}
|
2021-09-19 19:16:41 -04:00
|
|
|
|
2021-09-25 17:26:23 -04:00
|
|
|
// Try to execute the command
|
|
|
|
cli_exec(channel, argv[0], argc, argv);
|
2021-09-19 19:16:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Initialize the CLI
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// 0 on success, negative number on error
|
|
|
|
//
|
|
|
|
short cli_init() {
|
|
|
|
return 0;
|
|
|
|
}
|