From 414caa77d5709bf3372d1f9245312781eee7961b Mon Sep 17 00:00:00 2001 From: Leo C Date: Fri, 31 Aug 2018 23:36:06 +0200 Subject: [PATCH] error handling: improved cmd_error() - print fatfs error strings --- avr/Tupfile | 2 +- avr/cmd_fat.c | 53 +++++++++++++++++++++++----------------------- avr/command.c | 19 ++++++++++++----- avr/strerror.c | 26 +++++++++++++++++++++++ include/cmd_fat.h | 2 ++ include/command.h | 2 +- include/strerror.h | 12 +++++++++++ 7 files changed, 83 insertions(+), 33 deletions(-) create mode 100644 avr/strerror.c create mode 100644 include/strerror.h diff --git a/avr/Tupfile b/avr/Tupfile index 5e1f305..b74b305 100644 --- a/avr/Tupfile +++ b/avr/Tupfile @@ -11,7 +11,7 @@ SRC += cmd_run.c cmd_boot.c cmd_misc.c SRC += cmd_date.c cmd_mem.c cmd_gpio.c cmd_attach.c SRC += cmd_loadihex.c cmd_loadcpm3.c cmd_sd.c cmd_fat.c SRC += env.c con-utils.c print-utils.c getopt-min.c eval_arg.c -SRC += timer.c serial.c i2c.c bcd.c pcf8583.c mmc.c +SRC += timer.c serial.c i2c.c bcd.c pcf8583.c mmc.c strerror.c SRC += background.c z180-serv.c z80-if.c gpio.c SRC += $(FATFS) diff --git a/avr/cmd_fat.c b/avr/cmd_fat.c index ab16fa5..7cc9e46 100644 --- a/avr/cmd_fat.c +++ b/avr/cmd_fat.c @@ -76,26 +76,26 @@ static bool check_abort(void) static const FLASH char * const FLASH rc_strings[] = { - FSTR("OK"), - FSTR("disk error"), - FSTR("internal error"), - FSTR("not ready"), - FSTR("no file"), - FSTR("no path"), - FSTR("invalid name"), - FSTR("denied"), - FSTR("exist"), - FSTR("invalid object"), - FSTR("write protected"), - FSTR("invalid drive"), - FSTR("not enabled"), - FSTR("no file system"), - FSTR("mkfs aborted"), - FSTR("timeout"), - FSTR("locked"), - FSTR("not enough core"), - FSTR("too many open files"), - FSTR("invalid parameter") + FSTR("Success"), + FSTR("Disk error"), + FSTR("Internal error"), + FSTR("Not ready"), + FSTR("No file"), + FSTR("No path"), + FSTR("Invalid name"), + FSTR("Denied"), + FSTR("Exist"), + FSTR("Invalid object"), + FSTR("Write protected"), + FSTR("Invalid drive"), + FSTR("Not enabled"), + FSTR("No file system"), + FSTR("Mkfs aborted"), + FSTR("Timeout"), + FSTR("Locked"), + FSTR("Not enough core"), + FSTR("Too many open files"), + FSTR("Invalid parameter") }; static const FLASH char * const FLASH rc_names[] = { @@ -135,7 +135,7 @@ void put_rc (FRESULT rc) } -const FLASH char * rctostr(FRESULT rc) +const FLASH char * fat_rctostr(FRESULT rc) { return rc < ARRAY_SIZE(rc_strings) ? rc_strings[rc] : PSTR(" Unknown Error"); } @@ -198,13 +198,13 @@ static void strip_trailing_slash(PATH_T *p) static PATH_T *path_setup(char *string) { if (strlen(string) > PATH_MAX) { - cmd_error(PSTR("'%s': name too long"), string); + cmd_error(0, 0, PSTR("'%s': Name too long"), string); return NULL; } PATH_T *p = (PATH_T *) malloc(sizeof *p); if (p == NULL) { - cmd_error(PSTR("'%s': Out of Memory"), string); + cmd_error(0, 0, PSTR("'%s': Out of Memory"), string); return NULL; } @@ -216,7 +216,7 @@ static PATH_T *path_setup(char *string) p->p_path[2] = '/'; len += 1; } else { - cmd_error(PSTR("'%s': Out of Memory"), string); + cmd_error(0, 0, PSTR("'%s': Out of Memory"), string); return NULL; } } @@ -255,8 +255,9 @@ command_ret_t do_pwd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc } free(buf); if (res != FR_OK) { - put_rc(res); - return CMD_RET_FAILURE; + cmd_error(CMD_RET_FAILURE, res, NULL); + //put_rc(res); + //return CMD_RET_FAILURE; } return CMD_RET_SUCCESS; } diff --git a/avr/command.c b/avr/command.c index b0d9c90..ff3a685 100644 --- a/avr/command.c +++ b/avr/command.c @@ -22,6 +22,7 @@ #include "env.h" #include "debug.h" #include "getopt-min.h" +#include "strerror.h" #define DEBUG_CMD 0 /* set to 1 to debug */ @@ -791,16 +792,24 @@ int cmd_process_error(cmd_tbl_t *cmdtp, int err) return 0; } - -void cmd_error(const char *fmt, ...) +void cmd_error(int status, int errnum, const char *fmt, ...) { va_list ap; va_start(ap, fmt); print_prefixed_name(cmd_invocation_ptr); - my_puts_P(PSTR(": ")); - vfprintf_P(stdout, fmt, ap); + if (fmt != NULL) { + my_puts_P(PSTR(": ")); + vfprintf_P(stdout, fmt, ap); + } va_end(ap); + + if (errnum != 0) + printf_P(PSTR(": %S"), my_strerror_P(errnum)); + putchar('\n'); _delay_ms(20); - //command_ret = CMD_RET_FAILURE; + + if (status != 0) { + longjmp(cmd_jbuf, 1); + } } diff --git a/avr/strerror.c b/avr/strerror.c new file mode 100644 index 0000000..d80a3bc --- /dev/null +++ b/avr/strerror.c @@ -0,0 +1,26 @@ +/* + * (C) Copyright 2018 Leo C. + * + * SPDX-License-Identifier: GPL-2.0 + */ + +#include "common.h" +#include "cmd_fat.h" + +static const FLASH char * const FLASH error_strings[] = { + FSTR("Unknown error") +}; + +const FLASH char * my_strerror_P(int errnum) +{ + if (errnum < 100) + return fat_rctostr(errnum); + + errnum -= 100; + if (errnum < 0) + errnum = 0; + if ((unsigned) errnum >= ARRAY_SIZE(error_strings)) + errnum = 0; + + return error_strings[errnum]; +} diff --git a/include/cmd_fat.h b/include/cmd_fat.h index ed96a52..222b14c 100644 --- a/include/cmd_fat.h +++ b/include/cmd_fat.h @@ -8,9 +8,11 @@ #define CMD_FAT_H #include "command.h" +#include "ff.h" extern cmd_tbl_t cmd_tbl_fat[]; +const FLASH char * fat_rctostr(FRESULT rc); command_ret_t do_fat(cmd_tbl_t *, uint_fast8_t, int, char * const []); void setup_fatfs(void); diff --git a/include/command.h b/include/command.h index d5eeb36..6109f9f 100644 --- a/include/command.h +++ b/include/command.h @@ -102,7 +102,7 @@ int cmd_process_error(cmd_tbl_t *cmdtp, int err); * * @fmt: */ -void cmd_error(const char *fmt, ...); +void cmd_error(int status, int errnum, const char *fmt, ...); /* * Monitor Command diff --git a/include/strerror.h b/include/strerror.h new file mode 100644 index 0000000..9cc0821 --- /dev/null +++ b/include/strerror.h @@ -0,0 +1,12 @@ +/* + * (C) Copyright 2014 Leo C. + * + * SPDX-License-Identifier: GPL-2.0 + */ + +#ifndef STRERROR_H +#define STRERROR_H + +const FLASH char * my_strerror_P(int errnum); + +#endif /* STRERROR_H */ -- 2.39.2