shell: return exit codes

This commit is contained in:
S.J.R. van Schaik 2017-07-27 11:54:31 +02:00
parent a7504ad99c
commit 2942340b21
12 changed files with 169 additions and 118 deletions

View file

@ -5,7 +5,7 @@ struct console;
struct cmd { struct cmd {
const char *cmd; const char *cmd;
const char *desc; const char *desc;
void (* exec)(struct console *con, size_t argc, const char **argv); int (* exec)(struct console *con, size_t argc, const char **argv);
}; };
struct shell { struct shell {
@ -18,14 +18,14 @@ struct shell {
size_t count_args(const char *line); size_t count_args(const char *line);
char **parse_args(const char *line, size_t *argc); char **parse_args(const char *line, size_t *argc);
void cmd_exec(struct cmd *cmds, struct console *con, size_t argc, int cmd_exec(struct cmd *cmds, struct console *con, size_t argc,
const char **argv); const char **argv);
void cmd_parse(struct cmd *cmds, struct console *con, const char *line); int cmd_parse(struct cmd *cmds, struct console *con, const char *line);
int shell_init(struct shell *shell, struct cmd *cmds, int shell_init(struct shell *shell, struct cmd *cmds,
struct console *con, const char *prompt); struct console *con, const char *prompt);
int shell_parse(struct shell *shell); int shell_parse(struct shell *shell);
void do_flash_cmd(struct console *con, size_t argc, const char **argv); int do_flash_cmd(struct console *con, size_t argc, const char **argv);
void do_ftl_cmd(struct console *con, size_t argc, const char **argv); int do_ftl_cmd(struct console *con, size_t argc, const char **argv);
void do_mufs_cmd(struct console *con, size_t argc, const char **argv); int do_mufs_cmd(struct console *con, size_t argc, const char **argv);

View file

@ -1,3 +1,3 @@
#pragma once #pragma once
void shell_echo(struct console *con, size_t argc, const char **argv); int shell_echo(struct console *con, size_t argc, const char **argv);

View file

@ -1,14 +1,14 @@
#pragma once #pragma once
void shell_mount(struct console *con, size_t argc, const char **argv); int shell_mount(struct console *con, size_t argc, const char **argv);
void shell_unmount(struct console *con, size_t argc, const char **argv); int shell_unmount(struct console *con, size_t argc, const char **argv);
void shell_format(struct console *con, size_t argc, const char **argv); int shell_format(struct console *con, size_t argc, const char **argv);
void shell_mkdir(struct console *con, size_t argc, const char **argv); int shell_mkdir(struct console *con, size_t argc, const char **argv);
void shell_rmdir(struct console *con, size_t argc, const char **argv); int shell_rmdir(struct console *con, size_t argc, const char **argv);
void shell_stat(struct console *con, size_t argc, const char **argv); int shell_stat(struct console *con, size_t argc, const char **argv);
void shell_cat(struct console *con, size_t argc, const char **argv); int shell_cat(struct console *con, size_t argc, const char **argv);
void shell_write(struct console *con, size_t argc, const char **argv); int shell_write(struct console *con, size_t argc, const char **argv);
void shell_append(struct console *con, size_t argc, const char **argv); int shell_append(struct console *con, size_t argc, const char **argv);
void shell_mv(struct console *con, size_t argc, const char **argv); int shell_mv(struct console *con, size_t argc, const char **argv);
void shell_rm(struct console *con, size_t argc, const char **argv); int shell_rm(struct console *con, size_t argc, const char **argv);
void shell_ls(struct console *con, size_t argc, const char **argv); int shell_ls(struct console *con, size_t argc, const char **argv);

View file

@ -2,6 +2,6 @@
struct console; struct console;
void shell_date(struct console *con, size_t argc, const char **argv); int shell_date(struct console *con, size_t argc, const char **argv);
void shell_time(struct console *con, size_t argc, const char **argv); int shell_time(struct console *con, size_t argc, const char **argv);
void shell_set_date(struct console *con, size_t argc, const char **argv); int shell_set_date(struct console *con, size_t argc, const char **argv);

View file

@ -1,3 +1,3 @@
#pragma once #pragma once
void shell_version(struct console *con, size_t argc, const char **argv); int shell_version(struct console *con, size_t argc, const char **argv);

View file

@ -5,32 +5,36 @@
#include <console.h> #include <console.h>
#include <shell.h> #include <shell.h>
void cmd_exec(struct cmd *cmds, struct console *con, size_t argc, int cmd_exec(struct cmd *cmds, struct console *con, size_t argc,
const char **argv) const char **argv)
{ {
struct cmd *cmd; struct cmd *cmd;
if (argc < 1) if (argc < 1)
return; return -1;
for (cmd = cmds; cmd->cmd; ++cmd) { for (cmd = cmds; cmd->cmd; ++cmd) {
if (strcmp(cmd->cmd, argv[0]) == 0) { if (strcmp(cmd->cmd, argv[0]) == 0) {
cmd->exec(con, argc - 1, argv + 1); return cmd->exec(con, argc - 1, argv + 1);
break;
} }
} }
return -1;
} }
void cmd_parse(struct cmd *cmds, struct console *con, const char *line) int cmd_parse(struct cmd *cmds, struct console *con, const char *line)
{ {
char **argv; char **argv;
size_t argc; size_t argc;
int ret;
if (!(argv = parse_args(line, &argc))) if (!(argv = parse_args(line, &argc)))
return; return -1;
cmd_exec(cmds, con, argc, (const char **)argv); ret = cmd_exec(cmds, con, argc, (const char **)argv);
free(argv); free(argv);
return ret;
} }
int shell_init(struct shell *shell, struct cmd *cmds, int shell_init(struct shell *shell, struct cmd *cmds,

View file

@ -4,12 +4,14 @@
#include <shell/echo.h> #include <shell/echo.h>
void shell_echo(struct console *con, size_t argc, const char **argv) int shell_echo(struct console *con, size_t argc, const char **argv)
{ {
if (argc < 1) if (argc < 1)
return; return -1;
fprintf(con->fp, "%s\n", argv[0]); fprintf(con->fp, "%s\n", argv[0]);
return 0;
} }

View file

@ -59,7 +59,7 @@ static void print_hex_ascii(FILE *fp, uint32_t offset, const char *buf, size_t l
} }
} }
static void do_flash_probe(struct console *con, size_t argc, const char **argv) static int do_flash_probe(struct console *con, size_t argc, const char **argv)
{ {
(void)argv; (void)argv;
(void)argc; (void)argc;
@ -71,25 +71,29 @@ static void do_flash_probe(struct console *con, size_t argc, const char **argv)
if (!(flash = flash_probe())) { if (!(flash = flash_probe())) {
fprintf(con->fp, "error: unable to probe the flash device.\n"); fprintf(con->fp, "error: unable to probe the flash device.\n");
return; return -1;
} }
return 0;
} }
static void do_flash_release(struct console *con, size_t argc, const char **argv) static int do_flash_release(struct console *con, size_t argc, const char **argv)
{ {
(void)argv; (void)argv;
(void)argc; (void)argc;
if (!flash) { if (!flash) {
fprintf(con->fp, "no flash device currently active.\n"); fprintf(con->fp, "no flash device currently active.\n");
return; return -1;
} }
flash_release(flash); flash_release(flash);
flash = NULL; flash = NULL;
return 0;
} }
static void do_flash_info(struct console *con, size_t argc, const char **argv) static int do_flash_info(struct console *con, size_t argc, const char **argv)
{ {
size_t size, capacity; size_t size, capacity;
@ -98,7 +102,7 @@ static void do_flash_info(struct console *con, size_t argc, const char **argv)
if (!flash) { if (!flash) {
fprintf(con->fp, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return -1;
} }
size = flash_get_size(flash); size = flash_get_size(flash);
@ -106,39 +110,41 @@ static void do_flash_info(struct console *con, size_t argc, const char **argv)
fprintf(con->fp, " size: %" PRSZu "/%" PRSZu " bytes (%" PRSZu "%% usage)\n", fprintf(con->fp, " size: %" PRSZu "/%" PRSZu " bytes (%" PRSZu "%% usage)\n",
size, capacity, 100 * size / capacity); size, capacity, 100 * size / capacity);
return 0;
} }
static void do_flash_read(struct console *con, size_t argc, const char **argv) static int do_flash_read(struct console *con, size_t argc, const char **argv)
{ {
char buf[256]; char buf[256];
size_t addr, len, nbytes; size_t addr, len, nbytes;
if (argc < 2) { if (argc < 2) {
fprintf(con->fp, "usage: flash read <addr> <len>\n"); fprintf(con->fp, "usage: flash read <addr> <len>\n");
return; return -1;
} }
if (!flash) { if (!flash) {
fprintf(con->fp, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return -1;
} }
addr = strtoull(argv[0], NULL, 16); addr = strtoull(argv[0], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { if (errno == EINVAL || errno == ERANGE) {
fprintf(con->fp, "error: expected hexadecimal value for addr\n"); fprintf(con->fp, "error: expected hexadecimal value for addr\n");
return; return -1;
} }
len = strtoull(argv[1], NULL, 16); len = strtoull(argv[1], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { if (errno == EINVAL || errno == ERANGE) {
fprintf(con->fp, "error: expected hexadecimal value for len\n"); fprintf(con->fp, "error: expected hexadecimal value for len\n");
return; return -1;
} }
if (!len) if (!len)
return; return 0;
while (len) { while (len) {
nbytes = min(len, 256); nbytes = min(len, 256);
@ -148,39 +154,41 @@ static void do_flash_read(struct console *con, size_t argc, const char **argv)
addr += nbytes; addr += nbytes;
len -= nbytes; len -= nbytes;
} }
return 0;
} }
static void do_flash_write(struct console *con, size_t argc, const char **argv) static int do_flash_write(struct console *con, size_t argc, const char **argv)
{ {
char buf[256]; char buf[256];
size_t addr, len, nbytes; size_t addr, len, nbytes;
if (argc < 2) { if (argc < 2) {
fprintf(con->fp, "usage: flash write <addr> <len>\n"); fprintf(con->fp, "usage: flash write <addr> <len>\n");
return; return -1;
} }
if (!flash) { if (!flash) {
fprintf(con->fp, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return -1;
} }
addr = strtoull(argv[0], NULL, 16); addr = strtoull(argv[0], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { if (errno == EINVAL || errno == ERANGE) {
fprintf(con->fp, "error: expected hexadecimal value for addr\n"); fprintf(con->fp, "error: expected hexadecimal value for addr\n");
return; return -1;
} }
len = strtoull(argv[1], NULL, 16); len = strtoull(argv[1], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { if (errno == EINVAL || errno == ERANGE) {
fprintf(con->fp, "error: expected hexadecimal value for len\n"); fprintf(con->fp, "error: expected hexadecimal value for len\n");
return; return -1;
} }
if (!len) if (!len)
return; return 0;
while (len) { while (len) {
nbytes = min(len, 256); nbytes = min(len, 256);
@ -195,40 +203,44 @@ static void do_flash_write(struct console *con, size_t argc, const char **argv)
} }
flash_sync(flash); flash_sync(flash);
return 0;
} }
static void do_flash_erase(struct console *con, size_t argc, const char **argv) static int do_flash_erase(struct console *con, size_t argc, const char **argv)
{ {
size_t addr, len; size_t addr, len;
if (argc < 2) { if (argc < 2) {
fprintf(con->fp, "usage: flash erase <addr> <len>\n"); fprintf(con->fp, "usage: flash erase <addr> <len>\n");
return; return -1;
} }
if (!flash) { if (!flash) {
fprintf(con->fp, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return -1;
} }
addr = strtoull(argv[0], NULL, 16); addr = strtoull(argv[0], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { if (errno == EINVAL || errno == ERANGE) {
fprintf(con->fp, "error: expected hexadecimal value for addr\n"); fprintf(con->fp, "error: expected hexadecimal value for addr\n");
return; return -1;
} }
len = strtoull(argv[1], NULL, 16); len = strtoull(argv[1], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { if (errno == EINVAL || errno == ERANGE) {
fprintf(con->fp, "error: expected hexadecimal value for len\n"); fprintf(con->fp, "error: expected hexadecimal value for len\n");
return; return -1;
} }
if (!len) if (!len)
return; return 0;
flash_erase(flash, addr, len); flash_erase(flash, addr, len);
return 0;
} }
static struct cmd flash_cmds[] = { static struct cmd flash_cmds[] = {
@ -241,12 +253,12 @@ static struct cmd flash_cmds[] = {
{ NULL, NULL, NULL }, { NULL, NULL, NULL },
}; };
void do_flash_cmd(struct console *con, size_t argc, const char **argv) int do_flash_cmd(struct console *con, size_t argc, const char **argv)
{ {
if (argc < 1) { if (argc < 1) {
fprintf(con->fp, "usage: flash <command>\n"); fprintf(con->fp, "usage: flash <command>\n");
return; return -1;
} }
cmd_exec(flash_cmds, con, argc, argv); return cmd_exec(flash_cmds, con, argc, argv);
} }

View file

@ -15,7 +15,7 @@
extern struct flash_dev *flash; extern struct flash_dev *flash;
static void do_ftl_probe(struct console *con, size_t argc, const char **argv) static int do_ftl_probe(struct console *con, size_t argc, const char **argv)
{ {
(void)argv; (void)argv;
(void)argc; (void)argc;
@ -27,13 +27,15 @@ static void do_ftl_probe(struct console *con, size_t argc, const char **argv)
if (!(flash = flash_probe())) { if (!(flash = flash_probe())) {
fprintf(con->fp, "error: unable to probe the flash device.\n"); fprintf(con->fp, "error: unable to probe the flash device.\n");
return; return -1;
} }
if (!(flash = ftl_mount(flash))) { if (!(flash = ftl_mount(flash))) {
fprintf(con->fp, "error: unable to mount the flash translation layer.\n"); fprintf(con->fp, "error: unable to mount the flash translation layer.\n");
return; return -1;
} }
return 0;
} }
static struct cmd ftl_cmds[] = { static struct cmd ftl_cmds[] = {
@ -41,12 +43,12 @@ static struct cmd ftl_cmds[] = {
{ NULL, NULL, NULL }, { NULL, NULL, NULL },
}; };
void do_ftl_cmd(struct console *con, size_t argc, const char **argv) int do_ftl_cmd(struct console *con, size_t argc, const char **argv)
{ {
if (argc < 1) { if (argc < 1) {
fprintf(con->fp, "usage: flash <command>\n"); fprintf(con->fp, "usage: flash <command>\n");
return; return -1;
} }
cmd_exec(ftl_cmds, con, argc, argv); return cmd_exec(ftl_cmds, con, argc, argv);
} }

View file

@ -20,14 +20,14 @@
extern struct flash_dev *flash; extern struct flash_dev *flash;
struct mufs *mufs = NULL; struct mufs *mufs = NULL;
void shell_mount(struct console *con, size_t argc, const char **argv) int shell_mount(struct console *con, size_t argc, const char **argv)
{ {
(void)argc; (void)argc;
(void)argv; (void)argv;
if (!flash) { if (!flash) {
fprintf(con->fp, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return -1;
} }
if (mufs) { if (mufs) {
@ -37,20 +37,22 @@ void shell_mount(struct console *con, size_t argc, const char **argv)
if (!(mufs = mufs_mount(flash))) { if (!(mufs = mufs_mount(flash))) {
fprintf(con->fp, "error: unable to mount the filesystem.\n"); fprintf(con->fp, "error: unable to mount the filesystem.\n");
return; return -1;
} }
flash = NULL; flash = NULL;
return 0;
} }
void shell_unmount(struct console *con, size_t argc, const char **argv) int shell_unmount(struct console *con, size_t argc, const char **argv)
{ {
(void)argc; (void)argc;
(void)argv; (void)argv;
if (!mufs) { if (!mufs) {
fprintf(con->fp, "no mufs filesystem currently active\n"); fprintf(con->fp, "no mufs filesystem currently active\n");
return; return -1;
} }
if (flash) if (flash)
@ -61,106 +63,116 @@ void shell_unmount(struct console *con, size_t argc, const char **argv)
mufs_unmount(mufs); mufs_unmount(mufs);
mufs = NULL; mufs = NULL;
return 0;
} }
void shell_format(struct console *con, size_t argc, const char **argv) int shell_format(struct console *con, size_t argc, const char **argv)
{ {
(void)argc; (void)argc;
(void)argv; (void)argv;
if (!flash) { if (!flash) {
fprintf(con->fp, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return -1;
} }
if (mufs_format(flash) < 0) { if (mufs_format(flash) < 0) {
fprintf(con->fp, "error: unable to format the flash device.\n"); fprintf(con->fp, "error: unable to format the flash device.\n");
return; return -1;
} }
return 0;
} }
void shell_mkdir(struct console *con, size_t argc, const char **argv) int shell_mkdir(struct console *con, size_t argc, const char **argv)
{ {
if (argc < 1) { if (argc < 1) {
fprintf(con->fp, "usage: mufs mkdir <path>\n"); fprintf(con->fp, "usage: mufs mkdir <path>\n");
return; return -1;
} }
if (!mufs) { if (!mufs) {
fprintf(con->fp, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return -1;
} }
if (mufs_mkdir(mufs, argv[0]) < 0) { if (mufs_mkdir(mufs, argv[0]) < 0) {
fprintf(con->fp, "error: unable to create the directory\n"); fprintf(con->fp, "error: unable to create the directory\n");
return; return -1;
} }
return 0;
} }
void shell_rmdir(struct console *con, size_t argc, const char **argv) int shell_rmdir(struct console *con, size_t argc, const char **argv)
{ {
if (argc < 1) { if (argc < 1) {
fprintf(con->fp, "usage: mufs rmdir <path>\n"); fprintf(con->fp, "usage: mufs rmdir <path>\n");
return; return -1;
} }
if (!mufs) { if (!mufs) {
fprintf(con->fp, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return -1;
} }
if (mufs_rmdir(mufs, argv[0]) < 0) { if (mufs_rmdir(mufs, argv[0]) < 0) {
fprintf(con->fp, "error: unable to remove the directory\n"); fprintf(con->fp, "error: unable to remove the directory\n");
return; return -1;
} }
return 0;
} }
void shell_stat(struct console *con, size_t argc, const char **argv) int shell_stat(struct console *con, size_t argc, const char **argv)
{ {
struct mufs_stat stat; struct mufs_stat stat;
if (argc < 1) { if (argc < 1) {
fprintf(con->fp, "usage: mufs stat <path>\n"); fprintf(con->fp, "usage: mufs stat <path>\n");
return; return -1;
} }
if (!mufs) { if (!mufs) {
fprintf(con->fp, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return -1;
} }
if (mufs_stat(mufs, argv[0], &stat) < 0) { if (mufs_stat(mufs, argv[0], &stat) < 0) {
fprintf(con->fp, "error: unable to stat the file\n"); fprintf(con->fp, "error: unable to stat the file\n");
return; return -1;
} }
switch (stat.type) { switch (stat.type) {
case MUFS_DIR: printf(" type: directory\n"); break; case MUFS_DIR: printf(" type: directory\n"); break;
case MUFS_FILE: printf(" type: file\n"); break; case MUFS_FILE: printf(" type: file\n"); break;
default: return; default: return -1;
} }
printf(" file size: %" PRIu32 " bytes\n", stat.file_size); printf(" file size: %" PRIu32 " bytes\n", stat.file_size);
return 0;
} }
void shell_cat(struct console *con, size_t argc, const char **argv) int shell_cat(struct console *con, size_t argc, const char **argv)
{ {
char data[256]; char data[256];
struct mufs_file *file; struct mufs_file *file;
if (argc < 1) { if (argc < 1) {
fprintf(con->fp, "usage: mufs cat <path>\n"); fprintf(con->fp, "usage: mufs cat <path>\n");
return; return -1;
} }
if (!mufs) { if (!mufs) {
fprintf(con->fp, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return -1;
} }
if (!(file = mufs_open(mufs, argv[0], MUFS_READ))) { if (!(file = mufs_open(mufs, argv[0], MUFS_READ))) {
fprintf(con->fp, "error: unable to open the file\n"); fprintf(con->fp, "error: unable to open the file\n");
return; return -1;
} }
while (mufs_read(file, data, sizeof data) != 0) { while (mufs_read(file, data, sizeof data) != 0) {
@ -168,9 +180,11 @@ void shell_cat(struct console *con, size_t argc, const char **argv)
} }
mufs_close(file); mufs_close(file);
return 0;
} }
void shell_write(struct console *con, size_t argc, const char **argv) int shell_write(struct console *con, size_t argc, const char **argv)
{ {
char data[256]; char data[256];
struct mufs_file *file; struct mufs_file *file;
@ -178,17 +192,17 @@ void shell_write(struct console *con, size_t argc, const char **argv)
if (argc < 1) { if (argc < 1) {
fprintf(con->fp, "usage: mufs write <path>\n"); fprintf(con->fp, "usage: mufs write <path>\n");
return; return -1;
} }
if (!mufs) { if (!mufs) {
fprintf(con->fp, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return -1;
} }
if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE))) { if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE))) {
fprintf(con->fp, "error: unable to open the file\n"); fprintf(con->fp, "error: unable to open the file\n");
return; return -1;
} }
fprintf(con->fp, "> "); fprintf(con->fp, "> ");
@ -199,9 +213,11 @@ void shell_write(struct console *con, size_t argc, const char **argv)
} }
mufs_close(file); mufs_close(file);
return 0;
} }
void shell_append(struct console *con, size_t argc, const char **argv) int shell_append(struct console *con, size_t argc, const char **argv)
{ {
struct mufs_file *file; struct mufs_file *file;
char data[256]; char data[256];
@ -209,17 +225,17 @@ void shell_append(struct console *con, size_t argc, const char **argv)
if (argc < 2) { if (argc < 2) {
fprintf(con->fp, "usage: mufs append <path> <line>\n"); fprintf(con->fp, "usage: mufs append <path> <line>\n");
return; return -1;
} }
if (!mufs) { if (!mufs) {
fprintf(con->fp, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return -1;
} }
if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE | MUFS_APPEND))) { if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE | MUFS_APPEND))) {
fprintf(con->fp, "error: unable to open the file\n"); fprintf(con->fp, "error: unable to open the file\n");
return; return -1;
} }
n = strlen(argv[1]); n = strlen(argv[1]);
@ -230,45 +246,51 @@ void shell_append(struct console *con, size_t argc, const char **argv)
mufs_write(file, data, n + 1); mufs_write(file, data, n + 1);
mufs_close(file); mufs_close(file);
return 0;
} }
void shell_mv(struct console *con, size_t argc, const char **argv) int shell_mv(struct console *con, size_t argc, const char **argv)
{ {
if (argc < 2) { if (argc < 2) {
fprintf(con->fp, "usage: mufs mv <old> <new>\n"); fprintf(con->fp, "usage: mufs mv <old> <new>\n");
return; return -1;
} }
if (!mufs) { if (!mufs) {
fprintf(con->fp, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return -1;
} }
if (mufs_rename(mufs, argv[0], argv[1]) < 0) { if (mufs_rename(mufs, argv[0], argv[1]) < 0) {
fprintf(con->fp, "error: unable to move the file\n"); fprintf(con->fp, "error: unable to move the file\n");
return; return -1;
} }
return 0;
} }
void shell_rm(struct console *con, size_t argc, const char **argv) int shell_rm(struct console *con, size_t argc, const char **argv)
{ {
if (argc < 1) { if (argc < 1) {
fprintf(con->fp, "usage: mufs rm <path>\n"); fprintf(con->fp, "usage: mufs rm <path>\n");
return; return -1;
} }
if (!mufs) { if (!mufs) {
fprintf(con->fp, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return -1;
} }
if (mufs_unlink(mufs, argv[0]) < 0) { if (mufs_unlink(mufs, argv[0]) < 0) {
fprintf(con->fp, "error: unable to remove the file\n"); fprintf(con->fp, "error: unable to remove the file\n");
return; return -1;
} }
return 0;
} }
void shell_ls(struct console *con, size_t argc, const char **argv) int shell_ls(struct console *con, size_t argc, const char **argv)
{ {
struct mufs_dirent ent; struct mufs_dirent ent;
struct mufs_dir *dir; struct mufs_dir *dir;
@ -280,12 +302,12 @@ void shell_ls(struct console *con, size_t argc, const char **argv)
if (!mufs) { if (!mufs) {
fprintf(con->fp, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return -1;
} }
if (!(dir = mufs_opendir(mufs, path))) { if (!(dir = mufs_opendir(mufs, path))) {
fprintf(con->fp, "error: unable to open the directory\n"); fprintf(con->fp, "error: unable to open the directory\n");
return; return -1;
} }
while (mufs_readdir(dir, &ent) == 0) { while (mufs_readdir(dir, &ent) == 0) {
@ -293,6 +315,7 @@ void shell_ls(struct console *con, size_t argc, const char **argv)
} }
mufs_closedir(dir); mufs_closedir(dir);
return 0;
} }
static struct cmd mufs_cmds[] = { static struct cmd mufs_cmds[] = {
@ -311,7 +334,7 @@ static struct cmd mufs_cmds[] = {
{ NULL, NULL, NULL }, { NULL, NULL, NULL },
}; };
void do_mufs_cmd(struct console *con, size_t argc, const char **argv) int do_mufs_cmd(struct console *con, size_t argc, const char **argv)
{ {
cmd_exec(mufs_cmds, con, argc, argv); return cmd_exec(mufs_cmds, con, argc, argv);
} }

View file

@ -6,7 +6,7 @@
#include <shell/rtc.h> #include <shell/rtc.h>
void shell_date(struct console *con, size_t argc, const char **argv) int shell_date(struct console *con, size_t argc, const char **argv)
{ {
struct tm now; struct tm now;
@ -18,9 +18,11 @@ void shell_date(struct console *con, size_t argc, const char **argv)
fprintf(con->fp, "%02d:%02d:%02d %02d-%02d-%d\n", fprintf(con->fp, "%02d:%02d:%02d %02d-%02d-%d\n",
now.tm_hour, now.tm_min, now.tm_sec, now.tm_hour, now.tm_min, now.tm_sec,
now.tm_mday, now.tm_mon + 1, now.tm_year + 1900); now.tm_mday, now.tm_mon + 1, now.tm_year + 1900);
return 0;
} }
void shell_time(struct console *con, size_t argc, const char **argv) int shell_time(struct console *con, size_t argc, const char **argv)
{ {
struct tm now; struct tm now;
@ -30,22 +32,24 @@ void shell_time(struct console *con, size_t argc, const char **argv)
rtc_get_time(&now); rtc_get_time(&now);
fprintf(con->fp, "%ld\n", mktime(&now)); fprintf(con->fp, "%ld\n", mktime(&now));
return 0;
} }
void shell_set_date(struct console *con, size_t argc, const char **argv) int shell_set_date(struct console *con, size_t argc, const char **argv)
{ {
struct tm now; struct tm now;
if (argc < 2) { if (argc < 2) {
fprintf(con->fp, "usage: set-date <format> <date>\n"); fprintf(con->fp, "usage: set-date <format> <date>\n");
return; return -1;
} }
rtc_get_time(&now); rtc_get_time(&now);
if (!strptime(argv[1], argv[0], &now)) { if (!strptime(argv[1], argv[0], &now)) {
fprintf(con->fp, "error\n"); fprintf(con->fp, "error\n");
return; return -1;
} }
rtc_init(&now); rtc_init(&now);
@ -53,4 +57,6 @@ void shell_set_date(struct console *con, size_t argc, const char **argv)
fprintf(con->fp, "%02d:%02d:%02d %02d-%02d-%d\n", fprintf(con->fp, "%02d:%02d:%02d %02d-%02d-%d\n",
now.tm_hour, now.tm_min, now.tm_sec, now.tm_hour, now.tm_min, now.tm_sec,
now.tm_mday, now.tm_mon + 1, now.tm_year + 1900); now.tm_mday, now.tm_mon + 1, now.tm_year + 1900);
return 0;
} }

View file

@ -4,12 +4,14 @@
#include <shell/version.h> #include <shell/version.h>
void shell_version(struct console *con, size_t argc, const char **argv) int shell_version(struct console *con, size_t argc, const char **argv)
{ {
(void)argv; (void)argv;
if (argc < 1) if (argc < 1)
return; return -1;
fprintf(con->fp, "hello,%s\n", TBM_VERSION); fprintf(con->fp, "hello,%s\n", TBM_VERSION);
return 0;
} }