Added few simple builtin commands to the shell.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15370 27425a3e-05d8-49a3-a47f-9c15f0e5edd8
This commit is contained in:
Giovanni Di Sirio 2022-01-17 18:30:18 +00:00
parent 8c8004d11f
commit 0d2a33ce22
3 changed files with 74 additions and 28 deletions

View File

@ -104,6 +104,8 @@ static const char *sbx1_argv[] = {
static const char *sbx1_envp[] = {
"PATH=/bin",
"PROMPT=sb1> ",
"HOME=/",
NULL
};

View File

@ -134,6 +134,7 @@ __crt0_exit:
.global __crt0_entry
__crt0_entry:
nop
/* Popping from the stack the information passed by the
loader, saving the stack position as end of heap.*/
pop {r7, r8, r9, r10}

View File

@ -27,6 +27,8 @@
#define SHELL_NEWLINE_STR "\r\n"
#define SHELL_WELCOME_STR "ChibiOS/SB shell"
static const char *prompt;
static void shell_write(const char *s) {
size_t n = strlen(s);
@ -44,6 +46,13 @@ static void shell_putc(char c) {
(void) write(STDOUT_FILENO, &c, 1);
}
static void shell_usage(const char *s) {
shell_error("usage: ");
shell_error(s);
shell_error(SHELL_NEWLINE_STR);
}
bool shell_getline(char *line, size_t size) {
char *p = line;
@ -119,12 +128,60 @@ static char *fetch_argument(char **pp) {
return ap;
}
static int cmd_exit(int argc, char *argv[]) {
static int cmd_env(int argc, char *argv[]) {
extern char **environ;
char **pp;
(void)argc;
(void)argv;
sbExit(0);
if (argc != 1) {
shell_usage("env");
return 1;
}
pp = environ;
while (*pp != NULL) {
shell_write(*pp++);
shell_write(SHELL_NEWLINE_STR);
}
return 0;
}
static int cmd_exit(int argc, char *argv[]) {
msg_t msg;
if (argc == 1) {
msg = 0;
}
else if (argc == 2) {
msg = atoi(argv[1]);
}
else {
shell_usage("exit [n]");
return 1;
}
sbExit(msg);
return 0;
}
static int cmd_path(int argc, char *argv[]) {
char *s;
(void)argv;
if (argc != 1) {
shell_usage("path");
return 1;
}
s = getenv("PATH");
if (s != NULL) {
shell_write(s);
shell_write(SHELL_NEWLINE_STR);
}
return 0;
}
@ -136,7 +193,9 @@ static int shell_execute(int argc, char *argv[]) {
const char *name;
int (*cmdf)(int argc, char *argv[]);
} builtins[] = {
{"env", cmd_env},
{"exit", cmd_exit},
{"path", cmd_path},
{NULL, NULL}
};
@ -155,45 +214,29 @@ static int shell_execute(int argc, char *argv[]) {
* Application entry point.
*/
int main(int argc, char *argv[], char *envp[]) {
#if 0
char *s;
int i = 1;
printf("argc: %d\r\n", argc);
printf("argv: ");
while ((s = *argv++) != NULL) {
printf("%s", s);
}
printf("\r\n");
printf("envp: ");
while ((s = *envp++) != NULL) {
printf("%s", s);
}
printf("\r\n");
while (i <= 10) {
printf("#1 Hello World (%u)!!\r\n", i++);
sbSleepMilliseconds(500);
}
return i;
#endif
char line[SHELL_MAX_LINE_LENGTH];
char *args[SHELL_MAX_ARGUMENTS + 1];
char *ap, *tokp;
int i;
asm volatile ("bkpt");
(void)argc;
(void)argv;
(void)envp;
prompt = getenv("PROMPT");
if (prompt == NULL) {
prompt = SHELL_PROMPT_STR;
}
/* Welcome.*/
shell_write(SHELL_WELCOME_STR SHELL_NEWLINE_STR SHELL_NEWLINE_STR);
shell_write(SHELL_NEWLINE_STR SHELL_WELCOME_STR SHELL_NEWLINE_STR);
while (true) {
/* Prompt.*/
shell_write(SHELL_PROMPT_STR);
shell_write(prompt);
/* Reading input line.*/
if (shell_getline(line, SHELL_MAX_LINE_LENGTH)) {