]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cmd_attach.c
z80_toggle_reset(), z80_toggle_busreq()
[z180-stamp.git] / avr / cmd_attach.c
index e1a3b2c9052467403a6c0990c36eb146c357f461..09496875c3bf28f0b3ce034f419659c263b2d6db 100644 (file)
@@ -5,50 +5,15 @@
  */
 
 /*
- * attach chanels to devices
+ * 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"
-//#include "print-utils.h"
-//#include "con-utils.h"
-//#include "timer.h"
-//#include "z80-if.h"
-//#include "debug.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]]]
@@ -63,36 +28,39 @@ 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;
        drv_opt_t options = 0;
        int res;
 
        (void) cmdtp; (void) flag;
 
 
-       if (argv[0][0] == 'd')
+       if (argv[0][0] == 'd') {
                /* we are called as 'detach' */
                detach = true;
-
-       if (argc == 1) {
+       } else if (argc == 1) {
                /* no arguments */
                drv_list();
                return CMD_RET_SUCCESS;
        }
 
        /* reset getopt() */
-       optind = 1;
+       optind = 0;
 
        int opt;
-       while ((opt = getopt(argc, argv, PSTR("drwo:"))) != -1) {
+       while ((opt = getopt(argc, argv, PSTR("darwo:"))) != -1) {
                switch (opt) {
                case 'd':
                        detach = true;
                        break;
+               case 'a':
+                       detach_all = true;
+                       break;
                case 'r':
                        options |= DRV_OPT_RO;
                        break;
@@ -128,21 +96,25 @@ command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
 
        /* remaining arguments */
        argc -= optind;
-       if ( !((detach && argc == 1) ||
-                       ((options & DRV_OPT_REATTATCH) && argc == 1) ||
-                       argc == 2) )
+       if ( !( (argc == 0 && detach && detach_all) ||
+                       (argc == 1 && detach) ||
+                       (argc == 1 && (options & DRV_OPT_REATTATCH)) ||
+                        argc == 2) )
                return CMD_RET_USAGE;
 
-       if ((strlen(argv[optind]) != 4) ||
+       if (argc > 0 && ((strlen(argv[optind]) != 4) ||
                        strncmp_P(argv[optind], PSTR("dsk"), 3) ||
-                       (unit = argv[optind][3] - '0') >= CONFIG_CPM_MAX_DRIVE) {
+                       (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) {
-               res = drv_detach(unit);
+               if (detach_all)
+                       for (uint8_t i = 0; i < CONFIG_CPM_MAX_DRIVE; i++)
+                               drv_detach(i);
+               else
+                       drv_detach(unit);
                return CMD_RET_SUCCESS;
        }
 
@@ -151,7 +123,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;
 }