]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2016 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0 | |
5 | */ | |
6 | ||
7 | /* | |
8 | * attach channels to devices | |
9 | */ | |
10 | ||
11 | #include "cmd_attach.h" | |
12 | ||
13 | #include "command.h" | |
14 | #include "z180-serv.h" | |
15 | #include "getopt-min.h" | |
16 | ||
17 | ||
18 | /* | |
19 | * attach [[options] [unit [diskfile]]] | |
20 | * | |
21 | * detach unit | |
22 | * attach -d unit | |
23 | * | |
24 | * attach -o reattach unit | |
25 | * attach -o reattach unit diskfile | |
26 | * | |
27 | * attach unit diskfile | |
28 | * | |
29 | */ | |
30 | ||
31 | command_ret_t do_attach(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) | |
32 | { | |
33 | uint8_t unit = 0; | |
34 | char *filename = NULL; | |
35 | bool detach = false; | |
36 | bool detach_all = false; | |
37 | drv_opt_t options = 0; | |
38 | int res; | |
39 | ||
40 | (void) cmdtp; (void) flag; | |
41 | ||
42 | ||
43 | if (argv[0][0] == 'd') { | |
44 | /* we are called as 'detach' */ | |
45 | detach = true; | |
46 | } else if (argc == 1) { | |
47 | /* no arguments */ | |
48 | drv_list(); | |
49 | return CMD_RET_SUCCESS; | |
50 | } | |
51 | ||
52 | /* reset getopt() */ | |
53 | optind = 0; | |
54 | ||
55 | int opt; | |
56 | while ((opt = getopt(argc, argv, PSTR("darwo:"))) != -1) { | |
57 | switch (opt) { | |
58 | case 'd': | |
59 | detach = true; | |
60 | break; | |
61 | case 'a': | |
62 | detach_all = true; | |
63 | break; | |
64 | case 'r': | |
65 | options |= DRV_OPT_RO; | |
66 | break; | |
67 | case 'w': | |
68 | options &= ~DRV_OPT_RO; | |
69 | break; | |
70 | case 'o': | |
71 | { | |
72 | static const FLASH char delim[] = {", "}; | |
73 | char *p = strtok_P(optarg, delim); | |
74 | while (p != NULL) { | |
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; | |
85 | else | |
86 | return CMD_RET_USAGE; | |
87 | ||
88 | p = strtok_P(NULL, delim); | |
89 | } | |
90 | } | |
91 | break; | |
92 | default: /* '?' */ | |
93 | return CMD_RET_USAGE; | |
94 | } | |
95 | } | |
96 | ||
97 | /* remaining arguments */ | |
98 | argc -= optind; | |
99 | if ( !( (argc == 0 && detach && detach_all) || | |
100 | (argc == 1 && detach) || | |
101 | (argc == 1 && (options & DRV_OPT_REATTATCH)) || | |
102 | argc == 2) ) | |
103 | return CMD_RET_USAGE; | |
104 | ||
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)) { | |
108 | ||
109 | cmd_error(CMD_RET_FAILURE, 0, PSTR("Invalid device: '%s'"), argv[optind]); | |
110 | } | |
111 | ||
112 | if (detach) { | |
113 | if (detach_all) | |
114 | for (uint8_t i = 0; i < CONFIG_CPM_MAX_DRIVE; i++) | |
115 | drv_detach(i); | |
116 | else | |
117 | drv_detach(unit); | |
118 | return CMD_RET_SUCCESS; | |
119 | } | |
120 | ||
121 | if (argc == 2) | |
122 | filename = argv[++optind]; | |
123 | ||
124 | res = drv_attach(unit, filename, options); | |
125 | if (res) | |
126 | cmd_error(CMD_RET_FAILURE, res, PSTR("Attachment of '%s' to dsk%d failed"), filename, unit); | |
127 | ||
128 | return CMD_RET_SUCCESS; | |
129 | } |