]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_attach.c
2 * (C) Copyright 2016 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
8 * attach channels to devices
11 #include "cmd_attach.h"
14 #include "z180-serv.h"
15 #include "getopt-min.h"
18 static const FLASH
char * const FLASH rc_messages
[] = {
20 FSTR("Unknown error"),
21 FSTR("Disk number out of range 0..7"),
22 FSTR("Disk allready attached"),
23 FSTR("Disk not attached"),
24 FSTR("File not found"),
25 FSTR("Not enough memory"),
26 FSTR("Error opening file"),
27 FSTR("File allready attached to other drive"),
31 void printerror(int rc
, uint8_t unit
, char *fn
)
33 if (rc
< 0 || (unsigned) rc
>= ARRAY_SIZE(rc_messages
))
37 printf_P(PSTR("Attachment of '%s' to dsk%d failed: "), fn
, unit
);
38 my_puts_P(rc_messages
[rc
]);
39 my_puts_P(PSTR("!\n"));
41 printf_P(PSTR("Attachment of '%s' to dsk%d failed: %S!\n"),
42 fn
, unit
, rc_messages
[rc
]);
47 * attach [[options] [unit [diskfile]]]
52 * attach -o reattach unit
53 * attach -o reattach unit diskfile
55 * attach unit diskfile
59 command_ret_t
do_attach(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
62 char *filename
= NULL
;
64 bool detach_all
= false;
65 drv_opt_t options
= 0;
68 (void) cmdtp
; (void) flag
;
71 if (argv
[0][0] == 'd') {
72 /* we are called as 'detach' */
74 } else if (argc
== 1) {
77 return CMD_RET_SUCCESS
;
84 while ((opt
= getopt(argc
, argv
, PSTR("darwo:"))) != -1) {
93 options
|= DRV_OPT_RO
;
96 options
&= ~DRV_OPT_RO
;
100 static const FLASH
char delim
[] = {", "};
101 char *p
= strtok_P(optarg
, delim
);
103 if (!strcmp_P(p
, PSTR("ro")))
104 options
|= DRV_OPT_RO
;
105 else if (!strcmp_P(p
, PSTR("rw")))
106 options
&= ~DRV_OPT_RO
;
107 else if (!strcmp_P(p
, PSTR("debug")))
108 options
|= DRV_OPT_DEBUG
;
109 else if (!strcmp_P(p
, PSTR("nodebug")))
110 options
&= ~DRV_OPT_DEBUG
;
111 else if (!strcmp_P(p
, PSTR("reattach")))
112 options
|= DRV_OPT_REATTATCH
;
114 return CMD_RET_USAGE
;
116 p
= strtok_P(NULL
, delim
);
121 return CMD_RET_USAGE
;
125 /* remaining arguments */
127 if ( !( (argc
== 0 && detach
&& detach_all
) ||
128 (argc
== 1 && detach
) ||
129 (argc
== 1 && (options
& DRV_OPT_REATTATCH
)) ||
131 return CMD_RET_USAGE
;
133 if (argc
> 0 && ((strlen(argv
[optind
]) != 4) ||
134 strncmp_P(argv
[optind
], PSTR("dsk"), 3) ||
135 (unit
= argv
[optind
][3] - '0') >= CONFIG_CPM_MAX_DRIVE
)) {
137 printf_P(PSTR("Unknown device: '%s'\n"), argv
[optind
]);
138 return CMD_RET_FAILURE
;
143 for (uint8_t i
= 0; i
< CONFIG_CPM_MAX_DRIVE
; i
++)
147 return CMD_RET_SUCCESS
;
151 filename
= argv
[++optind
];
153 res
= drv_attach(unit
, filename
, options
);
155 printerror(res
, unit
, filename
);
157 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;