]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cmd_attach.c
Increase max. # of command line args to 20 and max. # of environment variables to 30
[z180-stamp.git] / avr / cmd_attach.c
CommitLineData
cb4fb1ed
L
1/*
2 * (C) Copyright 2016 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7/*
8 * attach chanels to devices
9 */
10
11#include "common.h"
12#include <string.h>
13#include <stdbool.h>
14
15#include "command.h"
16#include "z180-serv.h"
17#include "getopt-min.h"
18//#include "print-utils.h"
19//#include "con-utils.h"
20//#include "timer.h"
21//#include "z80-if.h"
22//#include "debug.h"
23
24
25static const FLASH char * const FLASH rc_messages[] = {
26 FSTR("OK"),
27 FSTR("Unknown error"),
28 FSTR("Disk number out of range 0..7"),
29 FSTR("Disk allready attached"),
30 FSTR("Disk not attached"),
31 FSTR("File not found"),
32 FSTR("Not enough memory"),
33 FSTR("Error opening file"),
34 FSTR("File allready attached to other drive"),
35 };
36
37static
38void printerror(int rc, uint8_t unit, char *fn)
39{
40 if (rc < 0 || (unsigned) rc >= ARRAY_SIZE(rc_messages))
41 rc = 1;
42
43#if GCC_BUG_61443 * 0
44 printf_P(PSTR("rc=%u FR_"), rc);
45 my_puts_P(rc_names[rc]);
46 my_puts_P(PSTR("\n"));
47#else
48 printf_P(PSTR("Attachment of '%s' to dsk%d failed: %S!\n"),
49 fn, unit, rc_messages[rc]);
50#endif
51}
52
53/*
54 * attach [[options] [unit [diskfile]]]
55 *
56 * detach unit
57 * attach -d unit
58 *
59 * attach -o reattach unit
60 * attach -o reattach unit diskfile
61 *
62 * attach unit diskfile
63 *
64 */
65
66command_ret_t do_attach(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
67{
68 uint8_t unit;
69 char *filename = NULL;
70 bool detach = false;
71 drv_opt_t options = 0;
72 int res;
73
74 (void) cmdtp; (void) flag;
75
76
77 if (argv[0][0] == 'd')
78 /* we are called as 'detach' */
79 detach = true;
80
81 if (argc == 1) {
82 /* no arguments */
83 drv_list();
84 return CMD_RET_SUCCESS;
85 }
86
87 /* reset getopt() */
88 optind = 1;
89
90 int opt;
91 while ((opt = getopt(argc, argv, PSTR("drwo:"))) != -1) {
92 switch (opt) {
93 case 'd':
94 detach = true;
95 break;
96 case 'r':
97 options |= DRV_OPT_RO;
98 break;
99 case 'w':
100 options &= ~DRV_OPT_RO;
101 break;
102 case 'o':
103 {
104 static const FLASH char delim[] = {", "};
105 char *p = strtok_P(optarg, delim);
106 while (p != NULL) {
107 if (!strcmp_P(p, PSTR("ro")))
108 options |= DRV_OPT_RO;
109 else if (!strcmp_P(p, PSTR("rw")))
110 options &= ~DRV_OPT_RO;
111 else if (!strcmp_P(p, PSTR("debug")))
112 options |= DRV_OPT_DEBUG;
113 else if (!strcmp_P(p, PSTR("nodebug")))
114 options &= ~DRV_OPT_DEBUG;
115 else if (!strcmp_P(p, PSTR("reattach")))
116 options |= DRV_OPT_REATTATCH;
117 else
118 return CMD_RET_USAGE;
119
120 p = strtok_P(NULL, delim);
121 }
122 }
123 break;
124 default: /* '?' */
125 return CMD_RET_USAGE;
126 }
127 }
128
129 /* remaining arguments */
130 argc -= optind;
131 if ( !((detach && argc == 1) ||
132 ((options & DRV_OPT_REATTATCH) && argc == 1) ||
133 argc == 2) )
134 return CMD_RET_USAGE;
135
136 if ((strlen(argv[optind]) != 4) ||
137 strncmp_P(argv[optind], PSTR("dsk"), 3) ||
138 (unit = argv[optind][3] - '0') >= CONFIG_CPM_MAX_DRIVE) {
139
140 printf_P(PSTR("Unknown device: '%s'\n"), argv[optind]);
141 return CMD_RET_FAILURE;
142 }
143
144 if (detach) {
145 res = drv_detach(unit);
146 return CMD_RET_SUCCESS;
147 }
148
149 if (argc == 2)
150 filename = argv[++optind];
151
152 res = drv_attach(unit, filename, options);
153 if (res)
154 printerror(res, unit, filename);
155
156 return res ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
157}