shell: pass around console object

This commit is contained in:
S.J.R. van Schaik 2017-07-24 17:57:13 +02:00
parent 3f7580ae39
commit c071903dd1
9 changed files with 160 additions and 149 deletions

View file

@ -2,11 +2,12 @@
#include <stdio.h>
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);

View file

@ -7,6 +7,7 @@
#endif
#include <limits.h>
#include <stdarg.h>
#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))

View file

@ -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);