]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cmd_attach.c
reset optind before executing command
[z180-stamp.git] / avr / cmd_attach.c
index fcc4f4951e02ae07249f4e6104b3253d756c2dbf..651404f83eb073236fb45f704baa5c1aeaaea0dd 100644 (file)
 #include "getopt-min.h"
 
 
-static const FLASH char * const FLASH rc_messages[] = {
-                       FSTR("OK"),
-                       FSTR("Unknown error"),
-                       FSTR("Disk number out of range 0..7"),
-                       FSTR("Disk allready attached"),
-                       FSTR("Disk not attached"),
-                       FSTR("File not found"),
-                       FSTR("Not enough memory"),
-                       FSTR("Error opening file"),
-                       FSTR("File allready attached to other drive"),
-               };
-
-static
-void printerror(int rc, uint8_t unit, char *fn)
-{
-       if (rc < 0 || (unsigned) rc >= ARRAY_SIZE(rc_messages))
-               rc = 1;
-
-#if GCC_BUG_61443
-               printf_P(PSTR("Attachment of '%s' to dsk%d failed: "), fn, unit);
-               my_puts_P(rc_messages[rc]);
-               my_puts_P(PSTR("!\n"));
-#else
-               printf_P(PSTR("Attachment of '%s' to dsk%d failed: %S!\n"),
-                               fn, unit, rc_messages[rc]);
-#endif
-}
-
 /*
  * attach [[options] [unit [diskfile]]]
  *
@@ -58,7 +30,7 @@ void printerror(int rc, uint8_t unit, char *fn)
 
 command_ret_t do_attach(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
 {
-       uint8_t unit;
+       uint8_t unit = 0;
        char *filename = NULL;
        bool detach = false;
        bool detach_all = false;
@@ -77,9 +49,6 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * co
                return CMD_RET_SUCCESS;
        }
 
-       /* reset getopt() */
-       optind = 0;
-
        int opt;
        while ((opt = getopt(argc, argv, PSTR("darwo:"))) != -1) {
                switch (opt) {
@@ -134,8 +103,7 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * co
                        strncmp_P(argv[optind], PSTR("dsk"), 3) ||
                        (unit = argv[optind][3] - '0') >= CONFIG_CPM_MAX_DRIVE)) {
 
-               printf_P(PSTR("Unknown device: '%s'\n"), argv[optind]);
-               return CMD_RET_FAILURE;
+               cmd_error(CMD_RET_FAILURE, 0, PSTR("Invalid device: '%s'"), argv[optind]);
        }
 
        if (detach) {
@@ -152,7 +120,7 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * co
 
        res = drv_attach(unit, filename, options);
        if (res)
-               printerror(res, unit, filename);
+               cmd_error(CMD_RET_FAILURE, res, PSTR("Attachment of '%s' to dsk%d failed"),     filename, unit);
 
-       return res ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
+       return CMD_RET_SUCCESS;
 }