diff --git a/include/console.h b/include/console.h index 7116d69..4677b24 100644 --- a/include/console.h +++ b/include/console.h @@ -2,11 +2,12 @@ #include -struct usart_console; +struct console { + FILE *fp; +}; -struct usart_console *console_init(unsigned dev_id); -FILE *console_to_fp(struct usart_console *console); -int console_peek(char *c, struct usart_console *console, int block); -int console_getc(char *c, struct usart_console *console); -int console_getline(struct usart_console *console, char *buf, size_t n); -ssize_t console_read(struct usart_console *console, char *buf, size_t n); +struct console *console_init(unsigned dev_id); +int console_peek(char *c, struct console *console, int block); +int console_getc(char *c, struct console *console); +int console_getline(struct console *console, char *buf, size_t n); +ssize_t console_read(struct console *console, char *buf, size_t n); diff --git a/include/macros.h b/include/macros.h index 4441dd9..a987cbf 100644 --- a/include/macros.h +++ b/include/macros.h @@ -7,6 +7,7 @@ #endif #include +#include #define min(x, y) ((x < y) ? (x) : (y)) #define max(x, y) ((x > y) ? (x) : (y)) @@ -16,6 +17,9 @@ #define MIB (1024 * KIB) #define GIB (1024 * MIB) +#define container_of(ptr, type, member) \ + (type *)((char *)ptr - offsetof(type, member)) + /* Rounding */ #define IS_ROUND(x, k) (!((x) & ((k) - 1))) #define ROUND_DOWN(x, k) ((x) & ~((k) - 1)) diff --git a/include/shell.h b/include/shell.h index 8c8245a..9b206bf 100644 --- a/include/shell.h +++ b/include/shell.h @@ -1,29 +1,31 @@ #pragma once +struct console; + struct cmd { const char *cmd; const char *desc; - void (* exec)(FILE *out, const char **argv, size_t argc); + void (* exec)(struct console *con, const char **argv, size_t argc); }; struct shell { char line[128]; struct cmd *cmds; const char *prompt; - struct usart_console *con; - FILE *fp; + struct console *con; }; size_t count_args(const char *line); char **parse_args(const char *line, size_t *argc); -void cmd_exec(struct cmd *cmds, FILE *out, const char **argv, size_t argc); -void cmd_parse(struct cmd *cmds, FILE *out, const char *line); +void cmd_exec(struct cmd *cmds, struct console *con, const char **argv, + size_t argc); +void cmd_parse(struct cmd *cmds, struct console *con, const char *line); int shell_init(struct shell *shell, struct cmd *cmds, - struct usart_console *con, const char *prompt); + struct console *con, const char *prompt); 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); +void do_flash_cmd(struct console *con, const char **argv, size_t argc); +void do_ftl_cmd(struct console *con, const char **argv, size_t argc); +void do_mufs_cmd(struct console *con, const char **argv, size_t argc); diff --git a/source/drivers/usart_console.c b/source/drivers/usart_console.c index 4071f70..3de711b 100644 --- a/source/drivers/usart_console.c +++ b/source/drivers/usart_console.c @@ -14,12 +14,13 @@ #include #include +#include #define RECV_BUF_LEN 128 struct usart_console { + struct console console; char recv_buf[RECV_BUF_LEN]; - FILE *fp; volatile size_t cur; volatile size_t next; unsigned irq_no; @@ -72,8 +73,11 @@ static char usart_getc(struct usart_console *console, int block) return c; } -int console_peek(char *c, struct usart_console *console, int block) +int console_peek(char *c, struct console *console_, int block) { + struct usart_console *console = container_of(console_, + struct usart_console, console); + while (block && console->cur == console->next); if (console->cur != console->next) { @@ -84,8 +88,11 @@ int console_peek(char *c, struct usart_console *console, int block) return -EAGAIN; } -int console_getc(char *c, struct usart_console *console) +int console_getc(char *c, struct console *console_) { + struct usart_console *console = container_of(console_, + struct usart_console, console); + if (console->cur == console->next) return -1; @@ -97,14 +104,16 @@ int console_getc(char *c, struct usart_console *console) return 0; } -int console_getline(struct usart_console *console, char *buf, size_t n) +int console_getline(struct console *console_, char *buf, size_t n) { + struct usart_console *console = container_of(console_, + struct usart_console, console); char *p = buf + strlen(buf); char c; *p = '\0'; - while (console_getc(&c, console) == 0) { + while (console_getc(&c, console_) == 0) { switch (c) { case '\r': usart_send_blocking(console->dev, '\r'); @@ -137,20 +146,22 @@ int console_getline(struct usart_console *console, char *buf, size_t n) return -EAGAIN; } -ssize_t console_read(struct usart_console *console, char *buf, size_t n) +ssize_t console_read(struct console *console_, char *buf, size_t n) { + struct usart_console *console = container_of(console_, + struct usart_console, console); char *p = buf; char c; *buf = '\0'; - while (console_peek(&c, console, 1) == 0) { + while (console_peek(&c, console_, 1) == 0) { switch (c) { case '\004': if (buf < p) goto out; - console_getc(&c, console); + console_getc(&c, console_); return 0; case 10: case 127: @@ -161,7 +172,7 @@ ssize_t console_read(struct usart_console *console, char *buf, size_t n) --p; } - console_getc(&c, console); + console_getc(&c, console_); break; case '\r': @@ -170,7 +181,7 @@ ssize_t console_read(struct usart_console *console, char *buf, size_t n) *p++ = '\n'; usart_send_blocking(console->dev, '\r'); - console_getc(&c, console); + console_getc(&c, console_); goto out; default: if (((size_t)p - (size_t)buf) >= n) @@ -178,7 +189,7 @@ ssize_t console_read(struct usart_console *console, char *buf, size_t n) *p++ = c; usart_send_blocking(console->dev, c); - console_getc(&c, console); + console_getc(&c, console_); break; } } @@ -280,10 +291,10 @@ static int usart_init(struct usart_console *console) return 0; } -struct usart_console *console_init(unsigned dev_id) +struct console *console_init(unsigned dev_id) { cookie_io_functions_t fops = { - .read = console_read, + .read = usart_read, .write = usart_write, .seek = NULL, .close = NULL, @@ -300,17 +311,11 @@ struct usart_console *console_init(unsigned dev_id) console->cur = 0; console->next = 0; - //usart[dev_id] = console; usart_init(console); - console->fp = fopencookie(console, "r+w", fops); - setvbuf(console->fp, NULL, _IONBF, 0); + console->console.fp = fopencookie(console, "r+w", fops); + setvbuf(console->console.fp, NULL, _IONBF, 0); - return console; -} - -FILE *console_to_fp(struct usart_console *console) -{ - return console->fp; + return &console->console; } diff --git a/source/main.c b/source/main.c index f651ca6..41c2d22 100644 --- a/source/main.c +++ b/source/main.c @@ -9,15 +9,15 @@ #include -struct usart_console *user_con, *admin_con; +struct console *user_con, *admin_con; struct shell user_shell, admin_shell; -static void do_echo(FILE *out, const char **argv, size_t argc) +static void do_echo(struct console *con, const char **argv, size_t argc) { if (argc < 1) return; - fprintf(out, "%s\n", argv[0]); + fprintf(con->fp, "%s\n", argv[0]); } struct cmd user_cmds[] = { @@ -35,19 +35,14 @@ struct cmd admin_cmds[] = { int main(void) { - FILE *fp; - rcc_init(); gpio_init(); user_con = console_init(0); admin_con = console_init(1); - fp = console_to_fp(user_con); - fprintf(fp, "TBM-dev (built on " __DATE__ ")\n"); - - fp = console_to_fp(admin_con); - fprintf(fp, "TBM-dev (built on " __DATE__ ")\n"); + fprintf(user_con->fp, "TBM-dev (built on " __DATE__ ")\n"); + fprintf(admin_con->fp, "TBM-dev (built on " __DATE__ ")\n"); shell_init(&user_shell, user_cmds, user_con, "tbm $"); shell_init(&admin_shell, admin_cmds, admin_con, "tbm #"); diff --git a/source/shell/cmd.c b/source/shell/cmd.c index 27d5760..f0234ad 100644 --- a/source/shell/cmd.c +++ b/source/shell/cmd.c @@ -5,7 +5,8 @@ #include #include -void cmd_exec(struct cmd *cmds, FILE *out, const char **argv, size_t argc) +void cmd_exec(struct cmd *cmds, struct console *con, const char **argv, + size_t argc) { struct cmd *cmd; @@ -14,13 +15,13 @@ void cmd_exec(struct cmd *cmds, FILE *out, const char **argv, size_t argc) for (cmd = cmds; cmd->cmd; ++cmd) { if (strcmp(cmd->cmd, argv[0]) == 0) { - cmd->exec(out, argv + 1, argc - 1); + cmd->exec(con, argv + 1, argc - 1); break; } } } -void cmd_parse(struct cmd *cmds, FILE *out, const char *line) +void cmd_parse(struct cmd *cmds, struct console *con, const char *line) { char **argv; size_t argc; @@ -28,12 +29,12 @@ void cmd_parse(struct cmd *cmds, FILE *out, const char *line) if (!(argv = parse_args(line, &argc))) return; - cmd_exec(cmds, out, (const char **)argv, argc); + cmd_exec(cmds, con, (const char **)argv, argc); free(argv); } int shell_init(struct shell *shell, struct cmd *cmds, - struct usart_console *con, const char *prompt) + struct console *con, const char *prompt) { if (!shell || !con) return -1; @@ -41,10 +42,9 @@ int shell_init(struct shell *shell, struct cmd *cmds, memset(shell->line, '\0', sizeof shell->line); shell->cmds = cmds; shell->con = con; - shell->fp = console_to_fp(con); shell->prompt = prompt; - fprintf(shell->fp, "%s ", prompt ? prompt : "#"); + fprintf(shell->con->fp, "%s ", prompt ? prompt : "#"); return 0; } @@ -60,8 +60,8 @@ int shell_parse(struct shell *shell) 0) return ret; - cmd_parse(shell->cmds, shell->fp, shell->line); - fprintf(shell->fp, "%s ", shell->prompt ? shell->prompt : "#"); + cmd_parse(shell->cmds, shell->con, shell->line); + fprintf(shell->con->fp, "%s ", shell->prompt ? shell->prompt : "#"); *shell->line = '\0'; return 0; diff --git a/source/shell/flash.c b/source/shell/flash.c index 0736e22..7e7ab86 100644 --- a/source/shell/flash.c +++ b/source/shell/flash.c @@ -7,6 +7,7 @@ #include +#include #include #include #include @@ -30,7 +31,7 @@ static void parse_hex(FILE *fp, char *buf, size_t len) } } -static void print_hex_ascii(FILE *out, uint32_t offset, const char *buf, size_t len) +static void print_hex_ascii(FILE *fp, uint32_t offset, const char *buf, size_t len) { size_t n, i; uint8_t c; @@ -38,27 +39,27 @@ static void print_hex_ascii(FILE *out, uint32_t offset, const char *buf, size_t for (; len; buf += n, len -= n) { n = min(len, 16); - fprintf(out, "%08" PRIx32 ": ", offset); + fprintf(fp, "%08" PRIx32 ": ", offset); for (i = 0; i < 16; ++i) { c = (i < n) ? buf[i] : 0; - fprintf(out, "%02x", c); + fprintf(fp, "%02x", c); } - fprintf(out, " "); + fprintf(fp, " "); for (i = 0; i < 16; ++i) { c = (i < n) ? buf[i] : 0; - fprintf(out, "%c", isalnum(c) ? c : '.'); + fprintf(fp, "%c", isalnum(c) ? c : '.'); } - fprintf(out, "\n"); + fprintf(fp, "\n"); offset += n; } } -static void do_flash_probe(FILE *out, const char **argv, size_t argc) +static void do_flash_probe(struct console *con, const char **argv, size_t argc) { (void)argv; (void)argc; @@ -69,18 +70,18 @@ static void do_flash_probe(FILE *out, const char **argv, size_t argc) } if (!(flash = flash_probe())) { - fprintf(out, "error: unable to probe the flash device.\n"); + fprintf(con->fp, "error: unable to probe the flash device.\n"); return; } } -static void do_flash_release(FILE *out, const char **argv, size_t argc) +static void do_flash_release(struct console *con, const char **argv, size_t argc) { (void)argv; (void)argc; if (!flash) { - fprintf(out, "no flash device currently active.\n"); + fprintf(con->fp, "no flash device currently active.\n"); return; } @@ -88,7 +89,7 @@ static void do_flash_release(FILE *out, const char **argv, size_t argc) flash = NULL; } -static void do_flash_info(FILE *out, const char **argv, size_t argc) +static void do_flash_info(struct console *con, const char **argv, size_t argc) { size_t size, capacity; @@ -96,43 +97,43 @@ static void do_flash_info(FILE *out, const char **argv, size_t argc) (void)argc; if (!flash) { - fprintf(out, "error: no flash device probed.\n"); + fprintf(con->fp, "error: no flash device probed.\n"); return; } size = flash_get_size(flash); capacity = flash_get_capacity(flash); - fprintf(out, " size: %" PRSZu "/%" PRSZu " bytes (%" PRSZu "%% usage)\n", + fprintf(con->fp, " size: %" PRSZu "/%" PRSZu " bytes (%" PRSZu "%% usage)\n", size, capacity, 100 * size / capacity); } -static void do_flash_read(FILE *out, const char **argv, size_t argc) +static void do_flash_read(struct console *con, const char **argv, size_t argc) { char buf[256]; size_t addr, len, nbytes; if (argc < 2) { - fprintf(out, "usage: flash read \n"); + fprintf(con->fp, "usage: flash read \n"); return; } if (!flash) { - fprintf(out, "error: no flash device probed.\n"); + fprintf(con->fp, "error: no flash device probed.\n"); return; } addr = strtoull(argv[0], NULL, 16); if (errno == EINVAL || errno == ERANGE) { - fprintf(out, "error: expected hexadecimal value for addr\n"); + fprintf(con->fp, "error: expected hexadecimal value for addr\n"); return; } len = strtoull(argv[1], NULL, 16); if (errno == EINVAL || errno == ERANGE) { - fprintf(out, "error: expected hexadecimal value for len\n"); + fprintf(con->fp, "error: expected hexadecimal value for len\n"); return; } @@ -142,39 +143,39 @@ static void do_flash_read(FILE *out, const char **argv, size_t argc) while (len) { nbytes = min(len, 256); flash_read(flash, addr, buf, nbytes); - print_hex_ascii(out, addr, buf, nbytes); + print_hex_ascii(con->fp, addr, buf, nbytes); addr += nbytes; len -= nbytes; } } -static void do_flash_write(FILE *out, const char **argv, size_t argc) +static void do_flash_write(struct console *con, const char **argv, size_t argc) { char buf[256]; size_t addr, len, nbytes; if (argc < 2) { - fprintf(out, "usage: flash write \n"); + fprintf(con->fp, "usage: flash write \n"); return; } if (!flash) { - fprintf(out, "error: no flash device probed.\n"); + fprintf(con->fp, "error: no flash device probed.\n"); return; } addr = strtoull(argv[0], NULL, 16); if (errno == EINVAL || errno == ERANGE) { - fprintf(out, "error: expected hexadecimal value for addr\n"); + fprintf(con->fp, "error: expected hexadecimal value for addr\n"); return; } len = strtoull(argv[1], NULL, 16); if (errno == EINVAL || errno == ERANGE) { - fprintf(out, "error: expected hexadecimal value for len\n"); + fprintf(con->fp, "error: expected hexadecimal value for len\n"); return; } @@ -183,41 +184,44 @@ static void do_flash_write(FILE *out, const char **argv, size_t argc) while (len) { nbytes = min(len, 256); - parse_hex(out, buf, nbytes); + parse_hex(con->fp, buf, nbytes); printf("\n"); - print_hex_ascii(out, addr, buf, nbytes); + print_hex_ascii(con->fp, addr, buf, nbytes); + flash_write(flash, addr, buf, nbytes); addr += nbytes; len -= nbytes; } + + flash_sync(flash); } -static void do_flash_erase(FILE *out, const char **argv, size_t argc) +static void do_flash_erase(struct console *con, const char **argv, size_t argc) { size_t addr, len; if (argc < 2) { - fprintf(out, "usage: flash erase \n"); + fprintf(con->fp, "usage: flash erase \n"); return; } if (!flash) { - fprintf(out, "error: no flash device probed.\n"); + fprintf(con->fp, "error: no flash device probed.\n"); return; } addr = strtoull(argv[0], NULL, 16); if (errno == EINVAL || errno == ERANGE) { - fprintf(out, "error: expected hexadecimal value for addr\n"); + fprintf(con->fp, "error: expected hexadecimal value for addr\n"); return; } len = strtoull(argv[1], NULL, 16); if (errno == EINVAL || errno == ERANGE) { - fprintf(out, "error: expected hexadecimal value for len\n"); + fprintf(con->fp, "error: expected hexadecimal value for len\n"); return; } @@ -237,12 +241,12 @@ static struct cmd flash_cmds[] = { { NULL, NULL, NULL }, }; -void do_flash_cmd(FILE *out, const char **argv, size_t argc) +void do_flash_cmd(struct console *con, const char **argv, size_t argc) { if (argc < 1) { - fprintf(out, "usage: flash \n"); + fprintf(con->fp, "usage: flash \n"); return; } - cmd_exec(flash_cmds, out, argv, argc); + cmd_exec(flash_cmds, con, argv, argc); } diff --git a/source/shell/ftl.c b/source/shell/ftl.c index ec382f7..d33d650 100644 --- a/source/shell/ftl.c +++ b/source/shell/ftl.c @@ -7,6 +7,7 @@ #include +#include #include #include #include @@ -14,7 +15,7 @@ extern struct flash_dev *flash; -static void do_ftl_probe(FILE *out, const char **argv, size_t argc) +static void do_ftl_probe(struct console *con, const char **argv, size_t argc) { (void)argv; (void)argc; @@ -25,12 +26,12 @@ static void do_ftl_probe(FILE *out, const char **argv, size_t argc) } if (!(flash = flash_probe())) { - fprintf(out, "error: unable to probe the flash device.\n"); + fprintf(con->fp, "error: unable to probe the flash device.\n"); return; } if (!(flash = ftl_mount(flash))) { - fprintf(out, "error: unable to mount the flash translation layer.\n"); + fprintf(con->fp, "error: unable to mount the flash translation layer.\n"); return; } } @@ -40,12 +41,12 @@ static struct cmd ftl_cmds[] = { { NULL, NULL, NULL }, }; -void do_ftl_cmd(FILE *out, const char **argv, size_t argc) +void do_ftl_cmd(struct console *con, const char **argv, size_t argc) { if (argc < 1) { - fprintf(out, "usage: flash \n"); + fprintf(con->fp, "usage: flash \n"); return; } - cmd_exec(ftl_cmds, out, argv, argc); + cmd_exec(ftl_cmds, con, argv, argc); } diff --git a/source/shell/mufs.c b/source/shell/mufs.c index b405afd..5e2bf22 100644 --- a/source/shell/mufs.c +++ b/source/shell/mufs.c @@ -7,6 +7,7 @@ #include +#include #include #include #include @@ -17,15 +18,13 @@ extern struct flash_dev *flash; struct mufs *mufs = NULL; -extern struct usart_console *admin_con; - -static void do_mufs_mount(FILE *out, const char **argv, size_t argc) +static void do_mufs_mount(struct console *con, const char **argv, size_t argc) { (void)argc; (void)argv; if (!flash) { - fprintf(out, "error: no flash device probed.\n"); + fprintf(con->fp, "error: no flash device probed.\n"); return; } @@ -35,20 +34,20 @@ static void do_mufs_mount(FILE *out, const char **argv, size_t argc) } if (!(mufs = mufs_mount(flash))) { - fprintf(out, "error: unable to mount the filesystem.\n"); + fprintf(con->fp, "error: unable to mount the filesystem.\n"); return; } flash = NULL; } -static void do_mufs_unmount(FILE *out, const char **argv, size_t argc) +static void do_mufs_unmount(struct console *con, const char **argv, size_t argc) { (void)argc; (void)argv; if (!mufs) { - fprintf(out, "no mufs filesystem currently active\n"); + fprintf(con->fp, "no mufs filesystem currently active\n"); return; } @@ -62,74 +61,74 @@ static void do_mufs_unmount(FILE *out, const char **argv, size_t argc) mufs = NULL; } -static void do_mufs_format(FILE *out, const char **argv, size_t argc) +static void do_mufs_format(struct console *con, const char **argv, size_t argc) { (void)argc; (void)argv; if (!flash) { - fprintf(out, "error: no flash device probed.\n"); + fprintf(con->fp, "error: no flash device probed.\n"); return; } if (mufs_format(flash) < 0) { - fprintf(out, "error: unable to format the flash device.\n"); + fprintf(con->fp, "error: unable to format the flash device.\n"); return; } } -static void do_mufs_mkdir(FILE *out, const char **argv, size_t argc) +static void do_mufs_mkdir(struct console *con, const char **argv, size_t argc) { if (argc < 1) { - fprintf(out, "usage: mufs mkdir \n"); + fprintf(con->fp, "usage: mufs mkdir \n"); return; } if (!mufs) { - fprintf(out, "error: no file system mounted.\n"); + fprintf(con->fp, "error: no file system mounted.\n"); return; } if (mufs_mkdir(mufs, argv[0]) < 0) { - fprintf(out, "error: unable to create the directory\n"); + fprintf(con->fp, "error: unable to create the directory\n"); return; } } -static void do_mufs_rmdir(FILE *out, const char **argv, size_t argc) +static void do_mufs_rmdir(struct console *con, const char **argv, size_t argc) { if (argc < 1) { - fprintf(out, "usage: mufs rmdir \n"); + fprintf(con->fp, "usage: mufs rmdir \n"); return; } if (!mufs) { - fprintf(out, "error: no file system mounted.\n"); + fprintf(con->fp, "error: no file system mounted.\n"); return; } if (mufs_rmdir(mufs, argv[0]) < 0) { - fprintf(out, "error: unable to remove the directory\n"); + fprintf(con->fp, "error: unable to remove the directory\n"); return; } } -static void do_mufs_stat(FILE *out, const char **argv, size_t argc) +static void do_mufs_stat(struct console *con, const char **argv, size_t argc) { struct mufs_stat stat; if (argc < 1) { - fprintf(out, "usage: mufs stat \n"); + fprintf(con->fp, "usage: mufs stat \n"); return; } if (!mufs) { - fprintf(out, "error: no file system mounted.\n"); + fprintf(con->fp, "error: no file system mounted.\n"); return; } if (mufs_stat(mufs, argv[0], &stat) < 0) { - fprintf(out, "error: unable to stat the file\n"); + fprintf(con->fp, "error: unable to stat the file\n"); return; } @@ -142,82 +141,82 @@ static void do_mufs_stat(FILE *out, const char **argv, size_t argc) printf(" file size: %" PRIu32 " bytes\n", stat.file_size); } -static void do_mufs_cat(FILE *out, const char **argv, size_t argc) +static void do_mufs_cat(struct console *con, const char **argv, size_t argc) { char data[256]; struct mufs_file *file; if (argc < 1) { - fprintf(out, "usage: mufs cat \n"); + fprintf(con->fp, "usage: mufs cat \n"); return; } if (!mufs) { - fprintf(out, "error: no file system mounted.\n"); + fprintf(con->fp, "error: no file system mounted.\n"); return; } if (!(file = mufs_open(mufs, argv[0], MUFS_READ))) { - fprintf(out, "error: unable to open the file\n"); + fprintf(con->fp, "error: unable to open the file\n"); return; } while (mufs_read(file, data, sizeof data) != 0) { - fwrite(data, sizeof *data, sizeof data, out); + fwrite(data, sizeof *data, sizeof data, con->fp); } mufs_close(file); } -static void do_mufs_write(FILE *out, const char **argv, size_t argc) +static void do_mufs_write(struct console *con, const char **argv, size_t argc) { char data[256]; struct mufs_file *file; ssize_t ret; if (argc < 1) { - fprintf(out, "usage: mufs write \n"); + fprintf(con->fp, "usage: mufs write \n"); return; } if (!mufs) { - fprintf(out, "error: no file system mounted.\n"); + fprintf(con->fp, "error: no file system mounted.\n"); return; } if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE))) { - fprintf(out, "error: unable to open the file\n"); + fprintf(con->fp, "error: unable to open the file\n"); return; } - fprintf(out, "> "); + fprintf(con->fp, "> "); - while ((ret = console_read(admin_con, data, sizeof data)) > 0) { + while ((ret = console_read(con, data, sizeof data)) > 0) { mufs_write(file, data, ret); - fprintf(out, "> "); + fprintf(con->fp, "> "); } mufs_close(file); } -static void do_mufs_append(FILE *out, const char **argv, size_t argc) +static void do_mufs_append(struct console *con, const char **argv, size_t argc) { struct mufs_file *file; char data[256]; size_t n; if (argc < 2) { - fprintf(out, "usage: mufs append \n"); + fprintf(con->fp, "usage: mufs append \n"); return; } if (!mufs) { - fprintf(out, "error: no file system mounted.\n"); + fprintf(con->fp, "error: no file system mounted.\n"); return; } if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE | MUFS_APPEND))) { - fprintf(out, "error: unable to open the file\n"); + fprintf(con->fp, "error: unable to open the file\n"); return; } @@ -231,43 +230,43 @@ static void do_mufs_append(FILE *out, const char **argv, size_t argc) mufs_close(file); } -static void do_mufs_mv(FILE *out, const char **argv, size_t argc) +static void do_mufs_mv(struct console *con, const char **argv, size_t argc) { if (argc < 2) { - fprintf(out, "usage: mufs mv \n"); + fprintf(con->fp, "usage: mufs mv \n"); return; } if (!mufs) { - fprintf(out, "error: no file system mounted.\n"); + fprintf(con->fp, "error: no file system mounted.\n"); return; } if (mufs_rename(mufs, argv[0], argv[1]) < 0) { - fprintf(out, "error: unable to move the file\n"); + fprintf(con->fp, "error: unable to move the file\n"); return; } } -static void do_mufs_rm(FILE *out, const char **argv, size_t argc) +static void do_mufs_rm(struct console *con, const char **argv, size_t argc) { if (argc < 1) { - fprintf(out, "usage: mufs rm \n"); + fprintf(con->fp, "usage: mufs rm \n"); return; } if (!mufs) { - fprintf(out, "error: no file system mounted.\n"); + fprintf(con->fp, "error: no file system mounted.\n"); return; } if (mufs_unlink(mufs, argv[0]) < 0) { - fprintf(out, "error: unable to remove the file\n"); + fprintf(con->fp, "error: unable to remove the file\n"); return; } } -static void do_mufs_ls(FILE *out, const char **argv, size_t argc) +static void do_mufs_ls(struct console *con, const char **argv, size_t argc) { struct mufs_dirent ent; struct mufs_dir *dir; @@ -278,17 +277,17 @@ static void do_mufs_ls(FILE *out, const char **argv, size_t argc) } if (!mufs) { - fprintf(out, "error: no file system mounted.\n"); + fprintf(con->fp, "error: no file system mounted.\n"); return; } if (!(dir = mufs_opendir(mufs, path))) { - fprintf(out, "error: unable to open the directory\n"); + fprintf(con->fp, "error: unable to open the directory\n"); return; } while (mufs_readdir(dir, &ent) == 0) { - fprintf(out, "%s\n", ent.path); + fprintf(con->fp, "%s\n", ent.path); } mufs_closedir(dir); @@ -310,7 +309,7 @@ static struct cmd mufs_cmds[] = { { NULL, NULL, NULL }, }; -void do_mufs_cmd(FILE *out, const char **argv, size_t argc) +void do_mufs_cmd(struct console *con, const char **argv, size_t argc) { - cmd_exec(mufs_cmds, out, argv, argc); + cmd_exec(mufs_cmds, con, argv, argc); }