]> 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 d7bcafd60bdf7e602be46c40a4109b0b984f020a..651404f83eb073236fb45f704baa5c1aeaaea0dd 100644 (file)
@@ -8,43 +8,13 @@
  * attach channels to devices
  */
 
-#include "common.h"
-#include <string.h>
-#include <stdbool.h>
+#include "cmd_attach.h"
 
 #include "command.h"
 #include "z180-serv.h"
 #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 * 0
-               printf_P(PSTR("rc=%u FR_"), rc);
-               my_puts_P(rc_names[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,9 +28,9 @@ void printerror(int rc, uint8_t unit, char *fn)
  *
  */
 
-command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+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;
@@ -79,9 +49,6 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
                return CMD_RET_SUCCESS;
        }
 
-       /* reset getopt() */
-       optind = 0;
-
        int opt;
        while ((opt = getopt(argc, argv, PSTR("darwo:"))) != -1) {
                switch (opt) {
@@ -136,8 +103,7 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
                        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) {
@@ -154,7 +120,7 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
 
        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;
 }