]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
error handling (WIP)
authorLeo C <leo@lenti.loc>
Mon, 27 Aug 2018 20:01:03 +0000 (22:01 +0200)
committerLeo C <erbl259-lmu@yahoo.de>
Tue, 28 Aug 2018 10:23:50 +0000 (12:23 +0200)
avr/cmd_fat.c
avr/command.c
include/command.h

index 2d64251fbb2f2275ecf08b60ed770695b2ff4899..ab16fa581c20ebc805ba0a34a3bd2d3106c9776f 100644 (file)
@@ -75,6 +75,29 @@ 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")
+               };
+
 static const FLASH char * const FLASH rc_names[] = {
                        FSTR("OK"),
                        FSTR("DISK_ERR"),
@@ -112,16 +135,9 @@ void put_rc (FRESULT rc)
 }
 
 
-void err(const char *fmt, ...)
+const FLASH char * rctostr(FRESULT rc)
 {
-       va_list ap;
-       va_start(ap, fmt);
-       printf_P(PSTR("fat %s: "), cmdname);
-       vfprintf_P(stdout, fmt, ap);
-       va_end(ap);
-       printf_P(PSTR("\n"));
-       _delay_ms(20);
-       command_ret = CMD_RET_FAILURE;
+       return  rc < ARRAY_SIZE(rc_strings) ? rc_strings[rc] : PSTR(" Unknown Error");
 }
 
 static void swirl(void)
@@ -182,13 +198,13 @@ static void strip_trailing_slash(PATH_T *p)
 static PATH_T *path_setup(char *string)
 {
        if (strlen(string) > PATH_MAX) {
-               err(PSTR("'%s': name too long"), string);
+               cmd_error(PSTR("'%s': name too long"), string);
                return NULL;
        }
 
        PATH_T *p = (PATH_T *) malloc(sizeof *p);
        if (p == NULL) {
-               err(PSTR("'%s': Out of Memory"), string);
+               cmd_error(PSTR("'%s': Out of Memory"), string);
                return NULL;
        }
 
@@ -200,7 +216,7 @@ static PATH_T *path_setup(char *string)
                        p->p_path[2] = '/';
                        len += 1;
                } else {
-                       err(PSTR("'%s': Out of Memory"), string);
+                       cmd_error(PSTR("'%s': Out of Memory"), string);
                        return NULL;
                }
        }
@@ -255,8 +271,6 @@ command_ret_t do_cd(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc,
        char *arg;
        FRESULT res = FR_OK;
 
-       cmdname = argv[0];
-
        if (argc < 2) {
                arg = getenv_str(PSTR(ENV_HOME));
                if (arg == NULL) {
index ff527575f33dc67c7b203c3e907ceb53d5a7fedb..b0d9c90219010a18d161fb52b045d9f14481715e 100644 (file)
@@ -100,7 +100,7 @@ static cmd_tbl_t *get_cmd_tbl_parent(cmd_tbl_t  *child)
        return NULL;
 }
 
-static int print_name_prefix(cmd_tbl_t *p)
+static int print_nameprefix(cmd_tbl_t *p)
 {
        cmd_tbl_t *top = get_cmd_tbl_parent(p);
 
@@ -112,13 +112,22 @@ static int print_name_prefix(cmd_tbl_t *p)
        return width;
 }
 
+static int print_prefixed_name(cmd_tbl_t *p)
+{
+       int len;
+
+       len = print_nameprefix(p);
+       len += strlen_P(p->name);
+       my_puts_P(p->name);
+
+       return len;
+}
+
 static void print_usage_line(cmd_tbl_t *p, int width)
 {
-       width -= strlen_P(p->name);
-       width -= print_name_prefix(p);
+       width -= print_prefixed_name(p);
        if (width < 0)
                width = 0;
-       my_puts_P(p->name);
        print_blanks(width);
        my_puts_P(PSTR(" - "));
        puts_P(p->usage);
@@ -414,8 +423,7 @@ command_ret_t cmd_usage(cmd_tbl_t *cmdtp)
        print_usage_line(cmdtp, 0);
 #ifdef CONFIG_SYS_LONGHELP
        my_puts_P(PSTR("Usage:\n"));
-       print_name_prefix(cmdtp);
-       my_puts_P(cmdtp->name);
+       print_prefixed_name(cmdtp);
        my_puts_P(PSTR(" "));
 
        if (cmdtp->help && *cmdtp->help != '\0')
@@ -686,6 +694,7 @@ int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np, int *c
 #endif /* CONFIG_AUTO_COMPLETE */
 
 
+static cmd_tbl_t *cmd_invocation_ptr;
 
 /**
  * Call a command function. This should be the only route in U-Boot to call
@@ -760,6 +769,7 @@ command_ret_t cmd_process(uint_fast8_t flag, int argc, char * const argv[],
 
        /* If OK so far, then do the command */
        if (!rc) {
+               cmd_invocation_ptr = cmdtp;
                rc = cmd_call(cmdtp, flag, argc, argv);
                *repeatable &= (cmdtp->flags & CTBL_RPT) != 0;
        }
@@ -780,3 +790,17 @@ int cmd_process_error(cmd_tbl_t *cmdtp, int err)
 
        return 0;
 }
+
+
+void cmd_error(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);
+       va_end(ap);
+       putchar('\n');
+       _delay_ms(20);
+       //command_ret = CMD_RET_FAILURE;
+}
index 93c9a2defb543b99378bcf37c6c82119f70247ce..d5eeb36b2f9230c9281562f00e4d9bb77a817066 100644 (file)
@@ -97,6 +97,13 @@ extern int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np,
  */
 int cmd_process_error(cmd_tbl_t *cmdtp, int err);
 
+/**
+ * cmd_error() - print error message
+ *
+ * @fmt:
+ */
+void cmd_error(const char *fmt, ...);
+
 /*
  * Monitor Command
  *