From 312dea1d673365656cf98d282fb2bce64289a11d Mon Sep 17 00:00:00 2001 From: Thomas Albers Date: Thu, 10 Aug 2023 10:37:52 +0200 Subject: Make param global --- cmd.c | 37 +++++++++++++++---------------------- zup.c | 38 +++++++++++++++++--------------------- zup.h | 10 +++++----- 3 files changed, 37 insertions(+), 48 deletions(-) diff --git a/cmd.c b/cmd.c index aa8beff..42a184d 100644 --- a/cmd.c +++ b/cmd.c @@ -23,8 +23,7 @@ enum args_type { A_OPTIONAL = 16 }; -typedef int (*fptr)(int, const struct command *, const struct param *, - int, char **); +typedef int (*fptr)(int, const struct command *, int, char **); struct command { char alias; @@ -100,8 +99,7 @@ syntax_error: } static int -cmd_boot(int fd, const struct command *cmd, const struct param *param, - int argc, char **args) +cmd_boot(int fd, const struct command *cmd, int argc, char **args) { int err; uint8_t bank; @@ -115,8 +113,7 @@ cmd_boot(int fd, const struct command *cmd, const struct param *param, } static int -cmd_read(int fd, const struct command *cmd, const struct param *param, - int argc, char **args) +cmd_read(int fd, const struct command *cmd, int argc, char **args) { int err = 0; uint8_t buf[MAX_PACKET_SIZE]; @@ -135,7 +132,7 @@ cmd_read(int fd, const struct command *cmd, const struct param *param, return err; } - if (param->human_readable) { + if (param.human_readable) { hexdump(address, length, buf); } else if (pathname) { FILE *fp; @@ -154,8 +151,7 @@ cmd_read(int fd, const struct command *cmd, const struct param *param, } static int -cmd_write(int fd, const struct command *cmd, const struct param *param, - int argc, char **args) +cmd_write(int fd, const struct command *cmd, int argc, char **args) { int err; char line[512]; @@ -172,7 +168,7 @@ cmd_write(int fd, const struct command *cmd, const struct param *param, if (err < 0) return -1; - if (param->human_readable) { + if (param.human_readable) { do { // TODO: Handle fgets error fgets(line, LEN(line), stdin); @@ -220,14 +216,13 @@ cmd_write(int fd, const struct command *cmd, const struct param *param, } static int -cmd_echo(int fd, const struct command *cmd, const struct param *param, - int argc, char **args) +cmd_echo(int fd, const struct command *cmd, int argc, char **args) { int err; char line[512]; char buf[MAX_PACKET_SIZE]; - if (!param->human_readable) { + if (!param.human_readable) { fputs("Echo is a interactive command only", stderr); return -1; } @@ -271,15 +266,13 @@ cmd_echo(int fd, const struct command *cmd, const struct param *param, } static int -repl_io_write(int fd, const struct command *cmd, const struct param *param, - int argc, char **args) +repl_io_write(int fd, const struct command *cmd, int argc, char **args) { return 0; } static int -repl_io_read(int fd, const struct command *cmd, const struct param *param, - int argc, char **args) +repl_io_read(int fd, const struct command *cmd, int argc, char **args) { return 0; } @@ -356,7 +349,7 @@ const struct command cmd_table[] = { }; int -run_line(int fd, struct param *param, char *line, const char *sep) +run_line(int fd, char *line, const char *sep) { int err; int argc = 1; @@ -385,7 +378,7 @@ run_line(int fd, struct param *param, char *line, const char *sep) } if ((p = cmd_table[cmdi].fptr) - && (err = p(fd, &cmd_table[cmdi], param, argc - 1, &args[1]))) { + && (err = p(fd, &cmd_table[cmdi], argc - 1, &args[1]))) { print_error(err); return err; } @@ -395,7 +388,7 @@ run_line(int fd, struct param *param, char *line, const char *sep) // TODO: Handle signals (CTRL-C) void -repl(int fd, struct param *param) +repl(int fd) { int quit = 0; @@ -411,7 +404,7 @@ repl(int fd, struct param *param) break; add_history(line); - quit = run_line(fd, param, line, " "); + quit = run_line(fd, line, " "); free(line); #else size_t len = 0; @@ -425,7 +418,7 @@ repl(int fd, struct param *param) if (line[len - 1] == '\n') line[len - 1] = '\0'; - quit = run_line(fd, param, line, " "); + quit = run_line(fd, line, " "); #endif } } diff --git a/zup.c b/zup.c index 8dffb17..844a693 100644 --- a/zup.c +++ b/zup.c @@ -95,7 +95,7 @@ parse_baud(const char *str) static int -parse_options(int argc, char *const argv[], struct param *param) +parse_options(int argc, char *const argv[]) { const char *sopts = "b:p:hHVRrv"; const struct option lopts[] = { @@ -116,30 +116,30 @@ parse_options(int argc, char *const argv[], struct param *param) while ((c = getopt_long(argc, argv, sopts, lopts, &i)) != -1) { switch (c) { case 'b': - if (!(param->baud = parse_baud(optarg))) { + if (!(param.baud = parse_baud(optarg))) { fprintf(stderr, "Error: Invalid baud rate '%s'\n", optarg); exit(EXIT_FAILURE); } break; case 'v': - param->verbose = 1; + param.verbose = 1; break; case 'p': - strncpy(param->port, optarg, LEN(param->port)); + strncpy(param.port, optarg, LEN(param.port)); break; case 'R': - param->repl = 1; + param.repl = 1; break; case 'H': - param->human_readable = 1; + param.human_readable = 1; break; case 'r': - param->human_readable = 0; + param.human_readable = 0; break; case 'V': @@ -210,7 +210,13 @@ hexdump(size_t start_address, size_t len, const uint8_t *buf) } } -int verbose = 0; +struct param param = { + .port = "/dev/ttyS1", + .baud = B9600, + .verbose = 0, + .human_readable = 0, + .repl = 0 +}; // TODO: Support stdin/stdout @@ -220,29 +226,19 @@ main(int argc, char *argv[]) int suc = EXIT_SUCCESS; // Init defaults - struct param param = { - .port = "/dev/ttyS1", - .baud = B9600, - .verbose = 0, - .human_readable = 0, - .repl = 0 - - }; - - int ind = parse_options(argc, argv, ¶m); - verbose = param.verbose; + int ind = parse_options(argc, argv); int fd = open_tty(param.port, param.baud); if (ind == argc || param.repl) { - repl(fd, ¶m); + repl(fd); } else { argc -= ind; argv = &argv[ind]; int err = 0; for (int i = 0; i < argc && !err; ++i) - err = run_line(fd, ¶m, argv[i], ":"); + err = run_line(fd, argv[i], ":"); if (err) suc = EXIT_FAILURE; diff --git a/zup.h b/zup.h index 7631e93..463f121 100644 --- a/zup.h +++ b/zup.h @@ -71,14 +71,14 @@ min(int x, int y) return x > y ? y : x; } -extern int verbose; +extern struct param param; static inline void msg(int level, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - if (verbose >= level) { + if (param.verbose >= level) { fprintf(stderr, "DEBUG [%d]: ", level); vfprintf(stderr, fmt, ap); } @@ -105,13 +105,13 @@ int open_tty(const char *port, int baud); int -run_commands(int fd, const struct param *param, int ncmd, char **cmds); +run_commands(int fd, int ncmd, char **cmds); int -run_line(int fd, struct param *param, char *line, const char *sep); +run_line(int fd, char *line, const char *sep); void -repl(int fd, struct param *param); +repl(int fd); void print_error(int error); -- cgit v1.2.3