shell: reimplement all commands to use the new shell system

tags/0.1.0
S.J.R. van Schaik 8 years ago
parent fa16f60b06
commit cfa38c9d39
  1. 4
      include/shell.h
  2. 14
      source/main.c
  3. 182
      source/shell/flash.c
  4. 30
      source/shell/ftl.c
  5. 218
      source/shell/mufs.c

@ -23,3 +23,7 @@ void cmd_parse(struct cmd *cmds, FILE *out, const char *line);
int shell_init(struct shell *shell, struct cmd *cmds, int shell_init(struct shell *shell, struct cmd *cmds,
struct usart_console *con, const char *prompt); struct usart_console *con, const char *prompt);
int shell_parse(struct shell *shell); int shell_parse(struct shell *shell);
void do_flash_cmd(FILE *out, const char **argv, size_t argc);
void do_ftl_cmd(FILE *out, const char **argv, size_t argc);
void do_mufs_cmd(FILE *out, const char **argv, size_t argc);

@ -20,11 +20,19 @@ static void do_echo(FILE *out, const char **argv, size_t argc)
fprintf(out, "%s\n", argv[0]); fprintf(out, "%s\n", argv[0]);
} }
struct cmd cmds[] = { struct cmd user_cmds[] = {
{ "echo", "", do_echo }, { "echo", "", do_echo },
{ NULL, NULL, NULL }, { NULL, NULL, NULL },
}; };
struct cmd admin_cmds[] = {
{ "echo", "", do_echo },
{ "flash", "", do_flash_cmd },
{ "ftl", "", do_ftl_cmd },
{ "mufs", "", do_mufs_cmd },
{ NULL, NULL, NULL },
};
int main(void) int main(void)
{ {
FILE *fp; FILE *fp;
@ -41,8 +49,8 @@ int main(void)
fp = console_to_fp(admin_con); fp = console_to_fp(admin_con);
fprintf(fp, "TBM-dev (built on " __DATE__ ")\n"); fprintf(fp, "TBM-dev (built on " __DATE__ ")\n");
shell_init(&user_shell, cmds, user_con, "tbm $"); shell_init(&user_shell, user_cmds, user_con, "tbm $");
shell_init(&admin_shell, cmds, admin_con, "tbm #"); shell_init(&admin_shell, admin_cmds, admin_con, "tbm #");
while (1) { while (1) {
shell_parse(&user_shell); shell_parse(&user_shell);

@ -11,23 +11,6 @@
#include <macros.h> #include <macros.h>
#include <shell.h> #include <shell.h>
static void do_flash_probe(const char *s);
static void do_flash_release(const char *s);
static void do_flash_info(const char *s);
static void do_flash_read(const char *s);
static void do_flash_write(const char *s);
static void do_flash_erase(const char *s);
static struct cmd flash_cmds[] = {
{ "probe", do_flash_probe },
{ "release", do_flash_release },
{ "info", do_flash_info },
{ "read", do_flash_read },
{ "write", do_flash_write },
{ "erase", do_flash_erase },
{ NULL, NULL },
};
struct flash_dev *flash = NULL; struct flash_dev *flash = NULL;
/* TODO: implement a parser for hex arrays that is more user-friendly. /* TODO: implement a parser for hex arrays that is more user-friendly.
@ -54,7 +37,7 @@ static void parse_hex(FILE *fp, char *buf, size_t len)
} }
} }
static void print_hex_ascii(uint32_t offset, const char *buf, size_t len) static void print_hex_ascii(FILE *out, uint32_t offset, const char *buf, size_t len)
{ {
size_t n, i; size_t n, i;
uint8_t c; uint8_t c;
@ -62,96 +45,103 @@ static void print_hex_ascii(uint32_t offset, const char *buf, size_t len)
for (; len; buf += n, len -= n) { for (; len; buf += n, len -= n) {
n = min(len, 16); n = min(len, 16);
printf("%08" PRIx32 ": ", offset); fprintf(out, "%08" PRIx32 ": ", offset);
for (i = 0; i < 16; ++i) { for (i = 0; i < 16; ++i) {
c = (i < n) ? buf[i] : 0; c = (i < n) ? buf[i] : 0;
printf("%02x", c); fprintf(out, "%02x", c);
} }
printf(" "); fprintf(out, " ");
for (i = 0; i < 16; ++i) { for (i = 0; i < 16; ++i) {
c = (i < n) ? buf[i] : 0; c = (i < n) ? buf[i] : 0;
printf("%c", isalnum(c) ? c : '.'); fprintf(out, "%c", isalnum(c) ? c : '.');
} }
printf("\n"); fprintf(out, "\n");
offset += n; offset += n;
} }
} }
static void do_flash_probe(const char *s) static void do_flash_probe(FILE *out, const char **argv, size_t argc)
{ {
(void)s; (void)argv;
(void)argc;
do_flash_release(NULL); if (flash) {
flash_release(flash);
flash = NULL;
}
if (!(flash = flash_probe())) { if (!(flash = flash_probe())) {
fprintf(stderr, "error: unable to probe the flash device.\n"); fprintf(out, "error: unable to probe the flash device.\n");
return; return;
} }
} }
static void do_flash_release(const char *s) static void do_flash_release(FILE *out, const char **argv, size_t argc)
{ {
(void)s; (void)argv;
(void)argc;
if (!flash) if (!flash) {
fprintf(out, "no flash device currently active.\n");
return; return;
}
flash_release(flash); flash_release(flash);
flash = NULL; flash = NULL;
} }
static void do_flash_info(const char *s) static void do_flash_info(FILE *out, const char **argv, size_t argc)
{ {
size_t size, capacity; size_t size, capacity;
(void)s; (void)argv;
(void)argc;
if (!flash) { if (!flash) {
fprintf(stderr, "error: no flash device probed.\n"); fprintf(out, "error: no flash device probed.\n");
return; return;
} }
size = flash_get_size(flash); size = flash_get_size(flash);
capacity = flash_get_capacity(flash); capacity = flash_get_capacity(flash);
printf(" size: %" PRSZu "/%" PRSZu " bytes (%" PRSZu "%% usage)\n", fprintf(out, " size: %" PRSZu "/%" PRSZu " bytes (%" PRSZu "%% usage)\n",
size, capacity, 100 * size / capacity); size, capacity, 100 * size / capacity);
} }
static void do_flash_read(const char *s) static void do_flash_read(FILE *out, const char **argv, size_t argc)
{ {
char buf[256], *end = NULL; char buf[256];
size_t addr, len, nbytes; size_t addr, len, nbytes;
if (!flash) { if (argc < 2) {
fprintf(stderr, "error: no flash device probed.\n"); fprintf(out, "usage: flash read <addr> <len>\n");
return; return;
} }
if (strncmp(s, "0x", 2) == 0) if (!flash) {
s += 2; fprintf(out, "error: no flash device probed.\n");
addr = strtoull(s, &end, 16);
s = end;
if (errno == EINVAL || errno == ERANGE)
return; return;
}
s += strspn(s, " \n"); addr = strtoull(argv[0], NULL, 16);
if (strncmp(s, "0x", 2) == 0) if (errno == EINVAL || errno == ERANGE) {
s += 2; fprintf(out, "error: expected hexadecimal value for addr\n");
return;
}
len = strtoull(s, &end, 16); len = strtoull(argv[1], NULL, 16);
s = end;
if (errno == EINVAL || errno == ERANGE) if (errno == EINVAL || errno == ERANGE) {
fprintf(out, "error: expected hexadecimal value for len\n");
return; return;
}
if (!len) if (!len)
return; return;
@ -159,51 +149,50 @@ static void do_flash_read(const char *s)
while (len) { while (len) {
nbytes = min(len, 256); nbytes = min(len, 256);
flash_read(flash, addr, buf, nbytes); flash_read(flash, addr, buf, nbytes);
print_hex_ascii(addr, buf, nbytes); print_hex_ascii(out, addr, buf, nbytes);
addr += nbytes; addr += nbytes;
len -= nbytes; len -= nbytes;
} }
} }
static void do_flash_write(const char *s) static void do_flash_write(FILE *out, const char **argv, size_t argc)
{ {
char buf[256], *end = NULL; char buf[256];
size_t addr, len, nbytes; size_t addr, len, nbytes;
if (!flash) { if (argc < 2) {
fprintf(stderr, "error: no flash device probed.\n"); fprintf(out, "usage: flash write <addr> <len>\n");
return; return;
} }
if (strncmp(s, "0x", 2) == 0) if (!flash) {
s += 2; fprintf(out, "error: no flash device probed.\n");
addr = strtoull(s, &end, 16);
s = end;
if (errno == EINVAL || errno == ERANGE)
return; return;
}
s += strspn(s, " \n"); addr = strtoull(argv[0], NULL, 16);
if (strncmp(s, "0x", 2) == 0) if (errno == EINVAL || errno == ERANGE) {
s += 2; fprintf(out, "error: expected hexadecimal value for addr\n");
return;
}
len = strtoull(s, &end, 16); len = strtoull(argv[1], NULL, 16);
s = end;
if (errno == EINVAL || errno == ERANGE) if (errno == EINVAL || errno == ERANGE) {
fprintf(out, "error: expected hexadecimal value for len\n");
return; return;
}
if (!len) if (!len)
return; return;
while (len) { while (len) {
nbytes = min(len, 256); nbytes = min(len, 256);
parse_hex(stdin, buf, nbytes); parse_hex(out, buf, nbytes);
printf("\n"); printf("\n");
print_hex_ascii(addr, buf, nbytes); print_hex_ascii(out, addr, buf, nbytes);
flash_write(flash, addr, buf, nbytes); flash_write(flash, addr, buf, nbytes);
addr += nbytes; addr += nbytes;
@ -211,35 +200,33 @@ static void do_flash_write(const char *s)
} }
} }
static void do_flash_erase(const char *s) static void do_flash_erase(FILE *out, const char **argv, size_t argc)
{ {
char *end = NULL;
size_t addr, len; size_t addr, len;
if (!flash) { if (argc < 2) {
fprintf(stderr, "error: no flash device probed.\n"); fprintf(out, "usage: flash erase <addr> <len>\n");
return; return;
} }
if (strncmp(s, "0x", 2) == 0) if (!flash) {
s += 2; fprintf(out, "error: no flash device probed.\n");
addr = strtoull(s, &end, 16);
s = end;
if (errno == EINVAL || errno == ERANGE)
return; return;
}
s += strspn(s, " \n"); addr = strtoull(argv[0], NULL, 16);
if (strncmp(s, "0x", 2) == 0) if (errno == EINVAL || errno == ERANGE) {
s += 2; fprintf(out, "error: expected hexadecimal value for addr\n");
return;
}
len = strtoull(s, &end, 16); len = strtoull(argv[1], NULL, 16);
s = end;
if (errno == EINVAL || errno == ERANGE) if (errno == EINVAL || errno == ERANGE) {
fprintf(out, "error: expected hexadecimal value for len\n");
return; return;
}
if (!len) if (!len)
return; return;
@ -247,7 +234,22 @@ static void do_flash_erase(const char *s)
flash_erase(flash, addr, len); flash_erase(flash, addr, len);
} }
void do_flash_cmd(const char *line) static struct cmd flash_cmds[] = {
{ "probe", NULL, do_flash_probe },
{ "release", NULL, do_flash_release },
{ "info", NULL, do_flash_info },
{ "read", NULL, do_flash_read },
{ "write", NULL, do_flash_write },
{ "erase", NULL, do_flash_erase },
{ NULL, NULL, NULL },
};
void do_flash_cmd(FILE *out, const char **argv, size_t argc)
{ {
cmd_exec(flash_cmds, line); if (argc < 1) {
fprintf(out, "usage: flash <command>\n");
return;
}
cmd_exec(flash_cmds, out, argv, argc);
} }

@ -12,18 +12,12 @@
#include <macros.h> #include <macros.h>
#include <shell.h> #include <shell.h>
static void do_ftl_probe(const char *s);
static struct cmd ftl_cmds[] = {
{ "probe", do_ftl_probe },
{ NULL, NULL },
};
extern struct flash_dev *flash; extern struct flash_dev *flash;
static void do_ftl_probe(const char *s) static void do_ftl_probe(FILE *out, const char **argv, size_t argc)
{ {
(void)s; (void)argv;
(void)argc;
if (flash) { if (flash) {
flash_release(flash); flash_release(flash);
@ -31,17 +25,27 @@ static void do_ftl_probe(const char *s)
} }
if (!(flash = flash_probe())) { if (!(flash = flash_probe())) {
fprintf(stderr, "error: unable to probe the flash device.\n"); fprintf(out, "error: unable to probe the flash device.\n");
return; return;
} }
if (!(flash = ftl_mount(flash))) { if (!(flash = ftl_mount(flash))) {
fprintf(stderr, "error: unable to mount the flash translation layer.\n"); fprintf(out, "error: unable to mount the flash translation layer.\n");
return; return;
} }
} }
void do_ftl_cmd(const char *line) static struct cmd ftl_cmds[] = {
{ "probe", NULL, do_ftl_probe },
{ NULL, NULL, NULL },
};
void do_ftl_cmd(FILE *out, const char **argv, size_t argc)
{ {
cmd_exec(ftl_cmds, line); if (argc < 1) {
fprintf(out, "usage: flash <command>\n");
return;
}
cmd_exec(ftl_cmds, out, argv, argc);
} }

@ -14,61 +14,41 @@
#include <fs/mufs.h> #include <fs/mufs.h>
static void do_mufs_mount(const char *s);
static void do_mufs_unmount(const char *s);
static void do_mufs_format(const char *s);
static void do_mufs_mkdir(const char *s);
static void do_mufs_rmdir(const char *s);
static void do_mufs_stat(const char *stat);
static void do_mufs_cat(const char *s);
static void do_mufs_append(const char *s);
static void do_mufs_mv(const char *s);
static void do_mufs_rm(const char *s);
static void do_mufs_ls(const char *s);
static struct cmd mufs_cmds[] = {
{ "mount", do_mufs_mount },
{ "unmount", do_mufs_unmount },
{ "format", do_mufs_format },
{ "mkdir", do_mufs_mkdir },
{ "rmdir", do_mufs_rmdir },
{ "ls", do_mufs_ls },
{ "stat", do_mufs_stat },
{ "cat", do_mufs_cat },
{ "append", do_mufs_append },
{ "mv", do_mufs_mv, },
{ "rm", do_mufs_rm, },
{ NULL, NULL },
};
extern struct flash_dev *flash; extern struct flash_dev *flash;
struct mufs *mufs = NULL; struct mufs *mufs = NULL;
static void do_mufs_mount(const char *s) static void do_mufs_mount(FILE *out, const char **argv, size_t argc)
{ {
(void)s; (void)argc;
(void)argv;
if (!flash) { if (!flash) {
fprintf(stderr, "error: no flash device probed.\n"); fprintf(out, "error: no flash device probed.\n");
return; return;
} }
do_mufs_unmount(NULL); if (mufs) {
mufs_unmount(mufs);
mufs = NULL;
}
if (!(mufs = mufs_mount(flash))) { if (!(mufs = mufs_mount(flash))) {
fprintf(stderr, "error: unable to mount the filesystem.\n"); fprintf(out, "error: unable to mount the filesystem.\n");
return; return;
} }
flash = NULL; flash = NULL;
} }
static void do_mufs_unmount(const char *s) static void do_mufs_unmount(FILE *out, const char **argv, size_t argc)
{ {
(void)s; (void)argc;
(void)argv;
if (!mufs) if (!mufs) {
fprintf(out, "no mufs filesystem currently active\n");
return; return;
}
if (flash) if (flash)
flash_release(flash); flash_release(flash);
@ -80,58 +60,74 @@ static void do_mufs_unmount(const char *s)
mufs = NULL; mufs = NULL;
} }
static void do_mufs_format(const char *s) static void do_mufs_format(FILE *out, const char **argv, size_t argc)
{ {
(void)s; (void)argc;
(void)argv;
if (!flash) { if (!flash) {
fprintf(stderr, "error: no flash device probed.\n"); fprintf(out, "error: no flash device probed.\n");
return; return;
} }
if (mufs_format(flash) < 0) { if (mufs_format(flash) < 0) {
fprintf(stderr, "error: unable to format the flash device.\n"); fprintf(out, "error: unable to format the flash device.\n");
return; return;
} }
} }
static void do_mufs_mkdir(const char *s) static void do_mufs_mkdir(FILE *out, const char **argv, size_t argc)
{ {
if (argc < 1) {
fprintf(out, "usage: mufs mkdir <path>\n");
return;
}
if (!mufs) { if (!mufs) {
fprintf(stderr, "error: no file system mounted.\n"); fprintf(out, "error: no file system mounted.\n");
return; return;
} }
if (mufs_mkdir(mufs, s) < 0) { if (mufs_mkdir(mufs, argv[0]) < 0) {
fprintf(stderr, "error: unable to create the directory\n"); fprintf(out, "error: unable to create the directory\n");
return; return;
} }
} }
static void do_mufs_rmdir(const char *s) static void do_mufs_rmdir(FILE *out, const char **argv, size_t argc)
{ {
if (argc < 1) {
fprintf(out, "usage: mufs rmdir <path>\n");
return;
}
if (!mufs) { if (!mufs) {
fprintf(stderr, "error: no file system mounted.\n"); fprintf(out, "error: no file system mounted.\n");
return; return;
} }
if (mufs_rmdir(mufs, s) < 0) { if (mufs_rmdir(mufs, argv[0]) < 0) {
fprintf(stderr, "error: unable to remove the directory\n"); fprintf(out, "error: unable to remove the directory\n");
return; return;
} }
} }
static void do_mufs_stat(const char *s) static void do_mufs_stat(FILE *out, const char **argv, size_t argc)
{ {
struct mufs_stat stat; struct mufs_stat stat;
if (argc < 1) {
fprintf(out, "usage: mufs stat <path>\n");
return;
}
if (!mufs) { if (!mufs) {
fprintf(stderr, "error: no file system mounted.\n"); fprintf(out, "error: no file system mounted.\n");
return; return;
} }
if (mufs_stat(mufs, s, &stat) < 0) { if (mufs_stat(mufs, argv[0], &stat) < 0) {
fprintf(stderr, "error: unable to stat the file\n"); fprintf(out, "error: unable to stat the file\n");
return; return;
} }
@ -144,18 +140,23 @@ static void do_mufs_stat(const char *s)
printf(" file size: %" PRIu32 " bytes\n", stat.file_size); printf(" file size: %" PRIu32 " bytes\n", stat.file_size);
} }
static void do_mufs_cat(const char *s) static void do_mufs_cat(FILE *out, const char **argv, size_t argc)
{ {
char data[256]; char data[256];
struct mufs_file *file; struct mufs_file *file;
if (argc < 1) {
fprintf(out, "usage: mufs cat <path>\n");
return;
}
if (!mufs) { if (!mufs) {
fprintf(stderr, "error: no file system mounted.\n"); fprintf(out, "error: no file system mounted.\n");
return; return;
} }
if (!(file = mufs_open(mufs, s, MUFS_READ))) { if (!(file = mufs_open(mufs, argv[0], MUFS_READ))) {
fprintf(stderr, "error: unable to open the file\n"); fprintf(out, "error: unable to open the file\n");
return; return;
} }
@ -166,105 +167,116 @@ static void do_mufs_cat(const char *s)
mufs_close(file); mufs_close(file);
} }
static void do_mufs_append(const char *s) static void do_mufs_append(FILE *out, const char **argv, size_t argc)
{ {
struct mufs_file *file; struct mufs_file *file;
char *path, *line; char data[256];
size_t n;
if (!mufs) { if (argc < 2) {
fprintf(stderr, "error: no file system mounted.\n"); fprintf(out, "usage: mufs append <path> <line>\n");
return; return;
} }
if (!(path = malloc(strlen(s) + 2))) if (!mufs) {
fprintf(out, "error: no file system mounted.\n");
return; return;
}
strcpy(path, s); if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE | MUFS_APPEND))) {
fprintf(out, "error: unable to open the file\n");
if (!(line = strchr(path, ' '))) { return;
fprintf(stderr, "usage: append <path> <line>\n");
goto err_free_path;
} }
*line++ = '\0'; n = strlen(argv[1]);
if (!(file = mufs_open(mufs, path, MUFS_WRITE | MUFS_APPEND))) { memcpy(data, argv[1], n);
fprintf(stderr, "error: unable to open the file\n"); data[n] = '\n';
goto err_free_path; data[n + 1] = '\0';
}
strcpy(line + strlen(line), "\n"); mufs_write(file, data, n + 1);
mufs_write(file, line, strlen(line));
mufs_close(file); mufs_close(file);
err_free_path:
free(path);
} }
static void do_mufs_mv(const char *s) static void do_mufs_mv(FILE *out, const char **argv, size_t argc)
{ {
char *old, *new; if (argc < 2) {
fprintf(out, "usage: mufs mv <old> <new>\n");
return;
}
if (!mufs) { if (!mufs) {
fprintf(stderr, "error: no file system mounted.\n"); fprintf(out, "error: no file system mounted.\n");
return; return;
} }
if (!(old = strdup(s))) if (mufs_rename(mufs, argv[0], argv[1]) < 0) {
fprintf(out, "error: unable to move the file\n");
return; return;
if (!(new = strchr(old, ' '))) {
fprintf(stderr, "usage: mv <old> <new>\n");
goto err_free_old;
} }
*new++ = '\0';
if (mufs_rename(mufs, old, new) < 0) {
fprintf(stderr, "error: unable to move the file\n");
goto err_free_old;
} }
err_free_old: static void do_mufs_rm(FILE *out, const char **argv, size_t argc)
free(old); {
if (argc < 1) {
fprintf(out, "usage: mufs rm <path>\n");
return;
} }
static void do_mufs_rm(const char *s)
{
if (!mufs) { if (!mufs) {
fprintf(stderr, "error: no file system mounted.\n"); fprintf(out, "error: no file system mounted.\n");
return; return;
} }
if (mufs_unlink(mufs, s) < 0) { if (mufs_unlink(mufs, argv[0]) < 0) {
fprintf(stderr, "error: unable to remove the file\n"); fprintf(out, "error: unable to remove the file\n");
return; return;
} }
} }
static void do_mufs_ls(const char *s) static void do_mufs_ls(FILE *out, const char **argv, size_t argc)
{ {
struct mufs_dirent ent; struct mufs_dirent ent;
struct mufs_dir *dir; struct mufs_dir *dir;
const char *path = "";
if (argc >= 1) {
path = argv[0];
}
if (!mufs) { if (!mufs) {
fprintf(stderr, "error: no file system mounted.\n"); fprintf(out, "error: no file system mounted.\n");
return; return;
} }
if (!(dir = mufs_opendir(mufs, s))) { if (!(dir = mufs_opendir(mufs, path))) {
fprintf(stderr, "error: unable to open the directory\n"); fprintf(out, "error: unable to open the directory\n");
return; return;
} }
while (mufs_readdir(dir, &ent) == 0) { while (mufs_readdir(dir, &ent) == 0) {
printf("%s\n", ent.path); fprintf(out, "%s\n", ent.path);
} }
mufs_closedir(dir); mufs_closedir(dir);
} }
void do_mufs_cmd(const char *line) static struct cmd mufs_cmds[] = {
{ "mount", NULL, do_mufs_mount },
{ "umount", NULL, do_mufs_unmount },
{ "format", NULL, do_mufs_format },
{ "mkdir", NULL, do_mufs_mkdir },
{ "rmdir", NULL, do_mufs_rmdir },
{ "ls", NULL, do_mufs_ls },
{ "stat", NULL, do_mufs_stat },
{ "cat", NULL, do_mufs_cat },
{ "append", NULL, do_mufs_append },
{ "mv", NULL, do_mufs_mv, },
{ "rm", NULL, do_mufs_rm, },
{ NULL, NULL, NULL },
};
void do_mufs_cmd(FILE *out, const char **argv, size_t argc)
{ {
cmd_exec(mufs_cmds, line); cmd_exec(mufs_cmds, out, argv, argc);
} }

Loading…
Cancel
Save