]>
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"
19 * attach [[options] [unit [diskfile]]]
24 * attach -o reattach unit
25 * attach -o reattach unit diskfile
27 * attach unit diskfile
31 command_ret_t
do_attach(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
34 char *filename
= NULL
;
36 bool detach_all
= false;
37 drv_opt_t options
= 0;
40 (void) cmdtp
; (void) flag
;
43 if (argv
[0][0] == 'd') {
44 /* we are called as 'detach' */
46 } else if (argc
== 1) {
49 return CMD_RET_SUCCESS
;
56 while ((opt
= getopt(argc
, argv
, PSTR("darwo:"))) != -1) {
65 options
|= DRV_OPT_RO
;
68 options
&= ~DRV_OPT_RO
;
72 static const FLASH
char delim
[] = {", "};
73 char *p
= strtok_P(optarg
, delim
);
75 if (!strcmp_P(p
, PSTR("ro")))
76 options
|= DRV_OPT_RO
;
77 else if (!strcmp_P(p
, PSTR("rw")))
78 options
&= ~DRV_OPT_RO
;
79 else if (!strcmp_P(p
, PSTR("debug")))
80 options
|= DRV_OPT_DEBUG
;
81 else if (!strcmp_P(p
, PSTR("nodebug")))
82 options
&= ~DRV_OPT_DEBUG
;
83 else if (!strcmp_P(p
, PSTR("reattach")))
84 options
|= DRV_OPT_REATTATCH
;
88 p
= strtok_P(NULL
, delim
);
97 /* remaining arguments */
99 if ( !( (argc
== 0 && detach
&& detach_all
) ||
100 (argc
== 1 && detach
) ||
101 (argc
== 1 && (options
& DRV_OPT_REATTATCH
)) ||
103 return CMD_RET_USAGE
;
105 if (argc
> 0 && ((strlen(argv
[optind
]) != 4) ||
106 strncmp_P(argv
[optind
], PSTR("dsk"), 3) ||
107 (unit
= argv
[optind
][3] - '0') >= CONFIG_CPM_MAX_DRIVE
)) {
109 cmd_error(CMD_RET_FAILURE
, 0, PSTR("Invalid device: '%s'"), argv
[optind
]);
114 for (uint8_t i
= 0; i
< CONFIG_CPM_MAX_DRIVE
; i
++)
118 return CMD_RET_SUCCESS
;
122 filename
= argv
[++optind
];
124 res
= drv_attach(unit
, filename
, options
);
126 cmd_error(CMD_RET_FAILURE
, res
, PSTR("Attachment of '%s' to dsk%d failed"), filename
, unit
);
128 return CMD_RET_SUCCESS
;