]>
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
16 #include "z180-serv.h"
17 #include "getopt-min.h"
20 static const FLASH
char * const FLASH rc_messages
[] = {
22 FSTR("Unknown error"),
23 FSTR("Disk number out of range 0..7"),
24 FSTR("Disk allready attached"),
25 FSTR("Disk not attached"),
26 FSTR("File not found"),
27 FSTR("Not enough memory"),
28 FSTR("Error opening file"),
29 FSTR("File allready attached to other drive"),
33 void printerror(int rc
, uint8_t unit
, char *fn
)
35 if (rc
< 0 || (unsigned) rc
>= ARRAY_SIZE(rc_messages
))
39 printf_P(PSTR("rc=%u FR_"), rc
);
40 my_puts_P(rc_names
[rc
]);
41 my_puts_P(PSTR("\n"));
43 printf_P(PSTR("Attachment of '%s' to dsk%d failed: %S!\n"),
44 fn
, unit
, rc_messages
[rc
]);
49 * attach [[options] [unit [diskfile]]]
54 * attach -o reattach unit
55 * attach -o reattach unit diskfile
57 * attach unit diskfile
61 command_ret_t
do_attach(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
64 char *filename
= NULL
;
66 bool detach_all
= false;
67 drv_opt_t options
= 0;
70 (void) cmdtp
; (void) flag
;
73 if (argv
[0][0] == 'd') {
74 /* we are called as 'detach' */
76 } else if (argc
== 1) {
79 return CMD_RET_SUCCESS
;
86 while ((opt
= getopt(argc
, argv
, PSTR("darwo:"))) != -1) {
95 options
|= DRV_OPT_RO
;
98 options
&= ~DRV_OPT_RO
;
102 static const FLASH
char delim
[] = {", "};
103 char *p
= strtok_P(optarg
, delim
);
105 if (!strcmp_P(p
, PSTR("ro")))
106 options
|= DRV_OPT_RO
;
107 else if (!strcmp_P(p
, PSTR("rw")))
108 options
&= ~DRV_OPT_RO
;
109 else if (!strcmp_P(p
, PSTR("debug")))
110 options
|= DRV_OPT_DEBUG
;
111 else if (!strcmp_P(p
, PSTR("nodebug")))
112 options
&= ~DRV_OPT_DEBUG
;
113 else if (!strcmp_P(p
, PSTR("reattach")))
114 options
|= DRV_OPT_REATTATCH
;
116 return CMD_RET_USAGE
;
118 p
= strtok_P(NULL
, delim
);
123 return CMD_RET_USAGE
;
127 /* remaining arguments */
129 if ( !( (argc
== 0 && detach
&& detach_all
) ||
130 (argc
== 1 && detach
) ||
131 (argc
== 1 && (options
& DRV_OPT_REATTATCH
)) ||
133 return CMD_RET_USAGE
;
135 if (argc
> 0 && ((strlen(argv
[optind
]) != 4) ||
136 strncmp_P(argv
[optind
], PSTR("dsk"), 3) ||
137 (unit
= argv
[optind
][3] - '0') >= CONFIG_CPM_MAX_DRIVE
)) {
139 printf_P(PSTR("Unknown device: '%s'\n"), argv
[optind
]);
140 return CMD_RET_FAILURE
;
145 for (uint8_t i
= 0; i
< CONFIG_CPM_MAX_DRIVE
; i
++)
149 return CMD_RET_SUCCESS
;
153 filename
= argv
[++optind
];
155 res
= drv_attach(unit
, filename
, options
);
157 printerror(res
, unit
, filename
);
159 return res
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;