shell: pass around console object

tags/0.1.0
S.J.R. van Schaik 7 years ago
parent 3f7580ae39
commit c071903dd1
  1. 15
      include/console.h
  2. 4
      include/macros.h
  3. 20
      include/shell.h
  4. 49
      source/drivers/usart_console.c
  5. 15
      source/main.c
  6. 18
      source/shell/cmd.c
  7. 72
      source/shell/flash.c
  8. 13
      source/shell/ftl.c
  9. 103
      source/shell/mufs.c

@ -2,11 +2,12 @@
#include <stdio.h> #include <stdio.h>
struct usart_console; struct console {
FILE *fp;
};
struct usart_console *console_init(unsigned dev_id); struct console *console_init(unsigned dev_id);
FILE *console_to_fp(struct usart_console *console); int console_peek(char *c, struct console *console, int block);
int console_peek(char *c, struct usart_console *console, int block); int console_getc(char *c, struct console *console);
int console_getc(char *c, struct usart_console *console); int console_getline(struct console *console, char *buf, size_t n);
int console_getline(struct usart_console *console, char *buf, size_t n); ssize_t console_read(struct console *console, char *buf, size_t n);
ssize_t console_read(struct usart_console *console, char *buf, size_t n);

@ -7,6 +7,7 @@
#endif #endif
#include <limits.h> #include <limits.h>
#include <stdarg.h>
#define min(x, y) ((x < y) ? (x) : (y)) #define min(x, y) ((x < y) ? (x) : (y))
#define max(x, y) ((x > y) ? (x) : (y)) #define max(x, y) ((x > y) ? (x) : (y))
@ -16,6 +17,9 @@
#define MIB (1024 * KIB) #define MIB (1024 * KIB)
#define GIB (1024 * MIB) #define GIB (1024 * MIB)
#define container_of(ptr, type, member) \
(type *)((char *)ptr - offsetof(type, member))
/* Rounding */ /* Rounding */
#define IS_ROUND(x, k) (!((x) & ((k) - 1))) #define IS_ROUND(x, k) (!((x) & ((k) - 1)))
#define ROUND_DOWN(x, k) ((x) & ~((k) - 1)) #define ROUND_DOWN(x, k) ((x) & ~((k) - 1))

@ -1,29 +1,31 @@
#pragma once #pragma once
struct console;
struct cmd { struct cmd {
const char *cmd; const char *cmd;
const char *desc; 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 { struct shell {
char line[128]; char line[128];
struct cmd *cmds; struct cmd *cmds;
const char *prompt; const char *prompt;
struct usart_console *con; struct console *con;
FILE *fp;
}; };
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, FILE *out, const char **argv, size_t argc); void cmd_exec(struct cmd *cmds, struct console *con, const char **argv,
void cmd_parse(struct cmd *cmds, FILE *out, const char *line); size_t argc);
void 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 usart_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(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(FILE *out, const char **argv, size_t argc); void do_ftl_cmd(struct console *con, const char **argv, size_t argc);
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);

@ -14,12 +14,13 @@
#include <sys/types.h> #include <sys/types.h>
#include <console.h> #include <console.h>
#include <macros.h>
#define RECV_BUF_LEN 128 #define RECV_BUF_LEN 128
struct usart_console { struct usart_console {
struct console console;
char recv_buf[RECV_BUF_LEN]; char recv_buf[RECV_BUF_LEN];
FILE *fp;
volatile size_t cur; volatile size_t cur;
volatile size_t next; volatile size_t next;
unsigned irq_no; unsigned irq_no;
@ -72,8 +73,11 @@ static char usart_getc(struct usart_console *console, int block)
return c; 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); while (block && console->cur == console->next);
if (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; 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) if (console->cur == console->next)
return -1; return -1;
@ -97,14 +104,16 @@ int console_getc(char *c, struct usart_console *console)
return 0; 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 *p = buf + strlen(buf);
char c; char c;
*p = '\0'; *p = '\0';
while (console_getc(&c, console) == 0) { while (console_getc(&c, console_) == 0) {
switch (c) { switch (c) {
case '\r': case '\r':
usart_send_blocking(console->dev, '\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; 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 *p = buf;
char c; char c;
*buf = '\0'; *buf = '\0';
while (console_peek(&c, console, 1) == 0) { while (console_peek(&c, console_, 1) == 0) {
switch (c) { switch (c) {
case '\004': case '\004':
if (buf < p) if (buf < p)
goto out; goto out;
console_getc(&c, console); console_getc(&c, console_);
return 0; return 0;
case 10: case 10:
case 127: case 127:
@ -161,7 +172,7 @@ ssize_t console_read(struct usart_console *console, char *buf, size_t n)
--p; --p;
} }
console_getc(&c, console); console_getc(&c, console_);
break; break;
case '\r': case '\r':
@ -170,7 +181,7 @@ ssize_t console_read(struct usart_console *console, char *buf, size_t n)
*p++ = '\n'; *p++ = '\n';
usart_send_blocking(console->dev, '\r'); usart_send_blocking(console->dev, '\r');
console_getc(&c, console); console_getc(&c, console_);
goto out; goto out;
default: default:
if (((size_t)p - (size_t)buf) >= n) 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; *p++ = c;
usart_send_blocking(console->dev, c); usart_send_blocking(console->dev, c);
console_getc(&c, console); console_getc(&c, console_);
break; break;
} }
} }
@ -280,10 +291,10 @@ static int usart_init(struct usart_console *console)
return 0; return 0;
} }
struct usart_console *console_init(unsigned dev_id) struct console *console_init(unsigned dev_id)
{ {
cookie_io_functions_t fops = { cookie_io_functions_t fops = {
.read = console_read, .read = usart_read,
.write = usart_write, .write = usart_write,
.seek = NULL, .seek = NULL,
.close = NULL, .close = NULL,
@ -300,17 +311,11 @@ struct usart_console *console_init(unsigned dev_id)
console->cur = 0; console->cur = 0;
console->next = 0; console->next = 0;
//usart[dev_id] = console;
usart_init(console); usart_init(console);
console->fp = fopencookie(console, "r+w", fops); console->console.fp = fopencookie(console, "r+w", fops);
setvbuf(console->fp, NULL, _IONBF, 0); setvbuf(console->console.fp, NULL, _IONBF, 0);
return console; return &console->console;
}
FILE *console_to_fp(struct usart_console *console)
{
return console->fp;
} }

@ -9,15 +9,15 @@
#include <libopencm3/stm32/usart.h> #include <libopencm3/stm32/usart.h>
struct usart_console *user_con, *admin_con; struct console *user_con, *admin_con;
struct shell user_shell, admin_shell; 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) if (argc < 1)
return; return;
fprintf(out, "%s\n", argv[0]); fprintf(con->fp, "%s\n", argv[0]);
} }
struct cmd user_cmds[] = { struct cmd user_cmds[] = {
@ -35,19 +35,14 @@ struct cmd admin_cmds[] = {
int main(void) int main(void)
{ {
FILE *fp;
rcc_init(); rcc_init();
gpio_init(); gpio_init();
user_con = console_init(0); user_con = console_init(0);
admin_con = console_init(1); admin_con = console_init(1);
fp = console_to_fp(user_con); fprintf(user_con->fp, "TBM-dev (built on " __DATE__ ")\n");
fprintf(fp, "TBM-dev (built on " __DATE__ ")\n"); fprintf(admin_con->fp, "TBM-dev (built on " __DATE__ ")\n");
fp = console_to_fp(admin_con);
fprintf(fp, "TBM-dev (built on " __DATE__ ")\n");
shell_init(&user_shell, user_cmds, user_con, "tbm $"); shell_init(&user_shell, user_cmds, user_con, "tbm $");
shell_init(&admin_shell, admin_cmds, admin_con, "tbm #"); shell_init(&admin_shell, admin_cmds, admin_con, "tbm #");

@ -5,7 +5,8 @@
#include <console.h> #include <console.h>
#include <shell.h> #include <shell.h>
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; 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) { for (cmd = cmds; cmd->cmd; ++cmd) {
if (strcmp(cmd->cmd, argv[0]) == 0) { if (strcmp(cmd->cmd, argv[0]) == 0) {
cmd->exec(out, argv + 1, argc - 1); cmd->exec(con, argv + 1, argc - 1);
break; 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; char **argv;
size_t argc; size_t argc;
@ -28,12 +29,12 @@ void cmd_parse(struct cmd *cmds, FILE *out, const char *line)
if (!(argv = parse_args(line, &argc))) if (!(argv = parse_args(line, &argc)))
return; return;
cmd_exec(cmds, out, (const char **)argv, argc); cmd_exec(cmds, con, (const char **)argv, argc);
free(argv); free(argv);
} }
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 console *con, const char *prompt)
{ {
if (!shell || !con) if (!shell || !con)
return -1; return -1;
@ -41,10 +42,9 @@ int shell_init(struct shell *shell, struct cmd *cmds,
memset(shell->line, '\0', sizeof shell->line); memset(shell->line, '\0', sizeof shell->line);
shell->cmds = cmds; shell->cmds = cmds;
shell->con = con; shell->con = con;
shell->fp = console_to_fp(con);
shell->prompt = prompt; shell->prompt = prompt;
fprintf(shell->fp, "%s ", prompt ? prompt : "#"); fprintf(shell->con->fp, "%s ", prompt ? prompt : "#");
return 0; return 0;
} }
@ -60,8 +60,8 @@ int shell_parse(struct shell *shell)
0) 0)
return ret; return ret;
cmd_parse(shell->cmds, shell->fp, shell->line); cmd_parse(shell->cmds, shell->con, shell->line);
fprintf(shell->fp, "%s ", shell->prompt ? shell->prompt : "#"); fprintf(shell->con->fp, "%s ", shell->prompt ? shell->prompt : "#");
*shell->line = '\0'; *shell->line = '\0';
return 0; return 0;

@ -7,6 +7,7 @@
#include <errno.h> #include <errno.h>
#include <console.h>
#include <flash.h> #include <flash.h>
#include <macros.h> #include <macros.h>
#include <shell.h> #include <shell.h>
@ -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; size_t n, i;
uint8_t c; 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) { for (; len; buf += n, len -= n) {
n = min(len, 16); n = min(len, 16);
fprintf(out, "%08" PRIx32 ": ", offset); fprintf(fp, "%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;
fprintf(out, "%02x", c); fprintf(fp, "%02x", c);
} }
fprintf(out, " "); fprintf(fp, " ");
for (i = 0; i < 16; ++i) { for (i = 0; i < 16; ++i) {
c = (i < n) ? buf[i] : 0; 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; 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)argv;
(void)argc; (void)argc;
@ -69,18 +70,18 @@ static void do_flash_probe(FILE *out, const char **argv, size_t argc)
} }
if (!(flash = flash_probe())) { 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; 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)argv;
(void)argc; (void)argc;
if (!flash) { if (!flash) {
fprintf(out, "no flash device currently active.\n"); fprintf(con->fp, "no flash device currently active.\n");
return; return;
} }
@ -88,7 +89,7 @@ static void do_flash_release(FILE *out, const char **argv, size_t argc)
flash = NULL; 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; size_t size, capacity;
@ -96,43 +97,43 @@ static void do_flash_info(FILE *out, const char **argv, size_t argc)
(void)argc; (void)argc;
if (!flash) { if (!flash) {
fprintf(out, "error: no flash device probed.\n"); fprintf(con->fp, "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);
fprintf(out, " 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);
} }
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]; char buf[256];
size_t addr, len, nbytes; size_t addr, len, nbytes;
if (argc < 2) { if (argc < 2) {
fprintf(out, "usage: flash read <addr> <len>\n"); fprintf(con->fp, "usage: flash read <addr> <len>\n");
return; return;
} }
if (!flash) { if (!flash) {
fprintf(out, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return;
} }
addr = strtoull(argv[0], NULL, 16); addr = strtoull(argv[0], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { 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; return;
} }
len = strtoull(argv[1], NULL, 16); len = strtoull(argv[1], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { 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; return;
} }
@ -142,39 +143,39 @@ static void do_flash_read(FILE *out, const char **argv, size_t argc)
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(out, addr, buf, nbytes); print_hex_ascii(con->fp, addr, buf, nbytes);
addr += nbytes; addr += nbytes;
len -= 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]; char buf[256];
size_t addr, len, nbytes; size_t addr, len, nbytes;
if (argc < 2) { if (argc < 2) {
fprintf(out, "usage: flash write <addr> <len>\n"); fprintf(con->fp, "usage: flash write <addr> <len>\n");
return; return;
} }
if (!flash) { if (!flash) {
fprintf(out, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return;
} }
addr = strtoull(argv[0], NULL, 16); addr = strtoull(argv[0], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { 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; return;
} }
len = strtoull(argv[1], NULL, 16); len = strtoull(argv[1], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { 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; return;
} }
@ -183,41 +184,44 @@ static void do_flash_write(FILE *out, const char **argv, size_t argc)
while (len) { while (len) {
nbytes = min(len, 256); nbytes = min(len, 256);
parse_hex(out, buf, nbytes); parse_hex(con->fp, buf, nbytes);
printf("\n"); printf("\n");
print_hex_ascii(out, addr, buf, nbytes); print_hex_ascii(con->fp, addr, buf, nbytes);
flash_write(flash, addr, buf, nbytes); flash_write(flash, addr, buf, nbytes);
addr += nbytes; addr += nbytes;
len -= 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; size_t addr, len;
if (argc < 2) { if (argc < 2) {
fprintf(out, "usage: flash erase <addr> <len>\n"); fprintf(con->fp, "usage: flash erase <addr> <len>\n");
return; return;
} }
if (!flash) { if (!flash) {
fprintf(out, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return;
} }
addr = strtoull(argv[0], NULL, 16); addr = strtoull(argv[0], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { 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; return;
} }
len = strtoull(argv[1], NULL, 16); len = strtoull(argv[1], NULL, 16);
if (errno == EINVAL || errno == ERANGE) { 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; return;
} }
@ -237,12 +241,12 @@ static struct cmd flash_cmds[] = {
{ NULL, NULL, NULL }, { 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) { if (argc < 1) {
fprintf(out, "usage: flash <command>\n"); fprintf(con->fp, "usage: flash <command>\n");
return; return;
} }
cmd_exec(flash_cmds, out, argv, argc); cmd_exec(flash_cmds, con, argv, argc);
} }

@ -7,6 +7,7 @@
#include <errno.h> #include <errno.h>
#include <console.h>
#include <flash.h> #include <flash.h>
#include <ftl.h> #include <ftl.h>
#include <macros.h> #include <macros.h>
@ -14,7 +15,7 @@
extern struct flash_dev *flash; 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)argv;
(void)argc; (void)argc;
@ -25,12 +26,12 @@ static void do_ftl_probe(FILE *out, const char **argv, size_t argc)
} }
if (!(flash = flash_probe())) { 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; return;
} }
if (!(flash = ftl_mount(flash))) { 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; return;
} }
} }
@ -40,12 +41,12 @@ static struct cmd ftl_cmds[] = {
{ NULL, NULL, NULL }, { 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) { if (argc < 1) {
fprintf(out, "usage: flash <command>\n"); fprintf(con->fp, "usage: flash <command>\n");
return; return;
} }
cmd_exec(ftl_cmds, out, argv, argc); cmd_exec(ftl_cmds, con, argv, argc);
} }

@ -7,6 +7,7 @@
#include <errno.h> #include <errno.h>
#include <console.h>
#include <flash.h> #include <flash.h>
#include <ftl.h> #include <ftl.h>
#include <macros.h> #include <macros.h>
@ -17,15 +18,13 @@
extern struct flash_dev *flash; extern struct flash_dev *flash;
struct mufs *mufs = NULL; struct mufs *mufs = NULL;
extern struct usart_console *admin_con; static void do_mufs_mount(struct console *con, const char **argv, size_t argc)
static void do_mufs_mount(FILE *out, const char **argv, size_t argc)
{ {
(void)argc; (void)argc;
(void)argv; (void)argv;
if (!flash) { if (!flash) {
fprintf(out, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return;
} }
@ -35,20 +34,20 @@ static void do_mufs_mount(FILE *out, const char **argv, size_t argc)
} }
if (!(mufs = mufs_mount(flash))) { 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; return;
} }
flash = NULL; 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)argc;
(void)argv; (void)argv;
if (!mufs) { if (!mufs) {
fprintf(out, "no mufs filesystem currently active\n"); fprintf(con->fp, "no mufs filesystem currently active\n");
return; return;
} }
@ -62,74 +61,74 @@ static void do_mufs_unmount(FILE *out, const char **argv, size_t argc)
mufs = NULL; 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)argc;
(void)argv; (void)argv;
if (!flash) { if (!flash) {
fprintf(out, "error: no flash device probed.\n"); fprintf(con->fp, "error: no flash device probed.\n");
return; return;
} }
if (mufs_format(flash) < 0) { 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; 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) { if (argc < 1) {
fprintf(out, "usage: mufs mkdir <path>\n"); fprintf(con->fp, "usage: mufs mkdir <path>\n");
return; return;
} }
if (!mufs) { if (!mufs) {
fprintf(out, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return;
} }
if (mufs_mkdir(mufs, argv[0]) < 0) { 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; 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) { if (argc < 1) {
fprintf(out, "usage: mufs rmdir <path>\n"); fprintf(con->fp, "usage: mufs rmdir <path>\n");
return; return;
} }
if (!mufs) { if (!mufs) {
fprintf(out, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return;
} }
if (mufs_rmdir(mufs, argv[0]) < 0) { 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; 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; struct mufs_stat stat;
if (argc < 1) { if (argc < 1) {
fprintf(out, "usage: mufs stat <path>\n"); fprintf(con->fp, "usage: mufs stat <path>\n");
return; return;
} }
if (!mufs) { if (!mufs) {
fprintf(out, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return;
} }
if (mufs_stat(mufs, argv[0], &stat) < 0) { 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; 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); 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]; char data[256];
struct mufs_file *file; struct mufs_file *file;
if (argc < 1) { if (argc < 1) {
fprintf(out, "usage: mufs cat <path>\n"); fprintf(con->fp, "usage: mufs cat <path>\n");
return; return;
} }
if (!mufs) { if (!mufs) {
fprintf(out, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return;
} }
if (!(file = mufs_open(mufs, argv[0], MUFS_READ))) { 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; return;
} }
while (mufs_read(file, data, sizeof data) != 0) { 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); 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]; char data[256];
struct mufs_file *file; struct mufs_file *file;
ssize_t ret; ssize_t ret;
if (argc < 1) { if (argc < 1) {
fprintf(out, "usage: mufs write <path>\n"); fprintf(con->fp, "usage: mufs write <path>\n");
return; return;
} }
if (!mufs) { if (!mufs) {
fprintf(out, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return;
} }
if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE))) { 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; 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); mufs_write(file, data, ret);
fprintf(out, "> "); fprintf(con->fp, "> ");
} }
mufs_close(file); 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; struct mufs_file *file;
char data[256]; char data[256];
size_t n; size_t n;
if (argc < 2) { if (argc < 2) {
fprintf(out, "usage: mufs append <path> <line>\n"); fprintf(con->fp, "usage: mufs append <path> <line>\n");
return; return;
} }
if (!mufs) { if (!mufs) {
fprintf(out, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return;
} }
if (!(file = mufs_open(mufs, argv[0], MUFS_WRITE | MUFS_APPEND))) { 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; return;
} }
@ -231,43 +230,43 @@ static void do_mufs_append(FILE *out, const char **argv, size_t argc)
mufs_close(file); 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) { if (argc < 2) {
fprintf(out, "usage: mufs mv <old> <new>\n"); fprintf(con->fp, "usage: mufs mv <old> <new>\n");
return; return;
} }
if (!mufs) { if (!mufs) {
fprintf(out, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return;
} }
if (mufs_rename(mufs, argv[0], argv[1]) < 0) { 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; 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) { if (argc < 1) {
fprintf(out, "usage: mufs rm <path>\n"); fprintf(con->fp, "usage: mufs rm <path>\n");
return; return;
} }
if (!mufs) { if (!mufs) {
fprintf(out, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return;
} }
if (mufs_unlink(mufs, argv[0]) < 0) { 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; 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_dirent ent;
struct mufs_dir *dir; struct mufs_dir *dir;
@ -278,17 +277,17 @@ static void do_mufs_ls(FILE *out, const char **argv, size_t argc)
} }
if (!mufs) { if (!mufs) {
fprintf(out, "error: no file system mounted.\n"); fprintf(con->fp, "error: no file system mounted.\n");
return; return;
} }
if (!(dir = mufs_opendir(mufs, path))) { 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; return;
} }
while (mufs_readdir(dir, &ent) == 0) { while (mufs_readdir(dir, &ent) == 0) {
fprintf(out, "%s\n", ent.path); fprintf(con->fp, "%s\n", ent.path);
} }
mufs_closedir(dir); mufs_closedir(dir);
@ -310,7 +309,7 @@ static struct cmd mufs_cmds[] = {
{ NULL, NULL, NULL }, { 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);
} }

Loading…
Cancel
Save