-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbmdos.c
More file actions
290 lines (243 loc) · 8.08 KB
/
cbmdos.c
File metadata and controls
290 lines (243 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* cbmdos.c - Common CBM DOS routines.
*
* Written by
* Andreas Boose <viceteam@t-online.de>
*
* This file is part of VICE, the Versatile Commodore Emulator.
* See README for copyright notice.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*
*/
#include "vice.h"
#include <stdio.h>
#include <string.h>
#include "cbmdos.h"
#include "lib.h"
typedef struct cbmdos_errortext_s {
unsigned int nr;
const char *text;
} cbmdos_errortext_t;
static const cbmdos_errortext_t cbmdos_error_messages[] =
{
{ 0, " OK" },
{ 1, "FILES SCRATCHED" },
{ 2, "SELECTED PARTITION" }, /* 1581 */
{ 3, "UNIMPLEMENTED" },
{ 20, "READ ERROR" },
{ 21, "READ ERROR" },
{ 22, "READ ERROR" },
{ 23, "READ ERROR" },
{ 24, "READ ERROR" },
{ 25, "WRITE ERROR" },
{ 26, "WRITE PROTECT ON" },
{ 27, "READ ERROR" },
{ 28, "WRITE ERROR" },
{ 29, "DISK ID MISMATCH" },
{ 30, "SYNTAX ERROR" },
{ 31, "SYNTAX ERROR" },
{ 32, "SYNTAX ERROR" },
{ 33, "SYNTAX ERROR" },
{ 34, "SYNTAX ERROR" },
{ 39, "SYNTAX ERROR" },
{ 50, "RECORD NOT RESENT" },
{ 51, "OVERFLOW IN RECORD" },
{ 52, "FILE TOO LARGE" }, /* 1581 */
{ 60, "WRITE FILE OPEN" },
{ 61, "FILE NOT OPEN" },
{ 62, "FILE NOT FOUND" },
{ 63, "FILE EXISTS" },
{ 64, "FILE TYPE MISMATCH" },
{ 65, "NO BLOCK" },
{ 66, "ILLEGAL TRACK OR SECTOR" },
{ 67, "ILLEGAL SYSTEM T OR S" },
{ 70, "NO CHANNEL" },
{ 72, "DISK FULL" },
{ 73, "VIRTUAL DRIVE EMULATION V2.2" }, /* The program version */
{ 74, "DRIVE NOT READY" },
{ 77, "SELECTED PARTITION ILLEGAL" }, /* 1581 */
{ 80, "DIRECTORY NOT EMPTY" },
{ 81, "PERMISSION DENIED" },
{ 255, NULL }
};
static const char *cbmdos_ft[] = {
"DEL", "SEQ", "PRG", "USR", "REL", "CBM"
};
const char *cbmdos_errortext(unsigned int code)
{
unsigned int count = 0;
while (cbmdos_error_messages[count].nr != 255
&& cbmdos_error_messages[count].nr != code)
count++;
if (cbmdos_error_messages[count].nr != 255)
return cbmdos_error_messages[count].text;
return "UNKNOWN ERROR NUMBER";
}
const char *cbmdos_filetype_get(unsigned int filetype)
{
if (filetype > 5)
return NULL;
return cbmdos_ft[filetype];
}
unsigned int cbmdos_parse_wildcard_check(const char *name, unsigned int len)
{
unsigned int index;
for (index = 0; index < len; index++) {
if (name[index] == '*' || name[index] == '?')
return 1;
}
return 0;
}
unsigned int cbmdos_parse_wildcard_compare(const BYTE *name1, const BYTE *name2)
{
unsigned int index;
for (index = 0; index < CBMDOS_SLOT_NAME_LENGTH; index++) {
if (name1[index] == '*')
return 1;
if (name1[index] != '?' && name1[index] != name2[index])
return 0;
if (name1[index] == 0xa0)
return 1;
}
return 1;
}
BYTE *cbmdos_dir_slot_create(const char *name, unsigned int len)
{
BYTE *slot;
if (len > CBMDOS_SLOT_NAME_LENGTH)
len = CBMDOS_SLOT_NAME_LENGTH;
slot = lib_malloc(CBMDOS_SLOT_NAME_LENGTH);
memset(slot, 0xa0, CBMDOS_SLOT_NAME_LENGTH);
memcpy(slot, name, (size_t)len);
return slot;
}
/* Parse command `parsecmd', type and read/write mode from the given string
`cmd' with `cmdlength. '@' on write must be checked elsewhere. */
unsigned int cbmdos_command_parse(cbmdos_cmd_parse_t *cmd_parse)
{
const BYTE *p;
char *parsecmd, *c;
int cmdlen;
cmd_parse->parsecmd = NULL;
cmd_parse->readmode = (cmd_parse->secondary == 1)
? CBMDOS_FAM_WRITE : CBMDOS_FAM_READ;
if (cmd_parse->cmd == NULL || cmd_parse->cmdlength == 0)
return CBMDOS_IPE_NO_NAME;
p = memchr(cmd_parse->cmd, ':', cmd_parse->cmdlength);
if (p) {
p++;
} else { /* no colon found */
if (cmd_parse->cmd[0] != '$') {
p = cmd_parse->cmd;
} else {
/* Directory listings are special - see $da55 in the 1541 for reference*/
if (cmd_parse->cmdlength > 1) {
if (cmd_parse->cmdlength == 2 &&
(cmd_parse->cmd[1] == '0' || cmd_parse->cmd[1] == '1')) {
/* A single 0/1 digit is a drive number, ignore it */
p = cmd_parse->cmd + cmd_parse->cmdlength; /* set to null byte */
} else {
/* no colon: everything after $ is the pattern */
p = cmd_parse->cmd + 1;
}
} else {
/* Just a single $, set pointer to null byte */
p = cmd_parse->cmd + cmd_parse->cmdlength;
}
}
}
#if 0
if (cmd_parse->cmd[0] == '@' && p == cmd_parse->cmd)
p++;
#endif
cmdlen = cmd_parse->cmdlength - (p - cmd_parse->cmd);
cmd_parse->parselength = 0;
/* Temporary hack. */
cmd_parse->parsecmd = lib_calloc(1, cmdlen + 2);
parsecmd = cmd_parse->parsecmd;
while (*p != ',' && cmdlen-- > 0) {
(cmd_parse->parselength)++;
*(parsecmd++) = *(p++);
}
cmd_parse->filetype = 0;
/*
* Change modes ?
*/
while (cmdlen > 0) {
cmdlen--;
p++;
if (cmdlen == 0)
return CBMDOS_IPE_INVAL;
switch (*p) {
case 'S':
cmd_parse->filetype = CBMDOS_FT_SEQ;
break;
case 'P':
cmd_parse->filetype = CBMDOS_FT_PRG;
break;
case 'U':
cmd_parse->filetype = CBMDOS_FT_USR;
break;
case 'L': /* L,(#record length) max 254 */
if (p[1] == ',') {
cmd_parse->recordlength = p[2]; /* Changing RL causes error */
/* Don't allow REL file record lengths less than 2 or
greater than 254. The 1541/71/81 lets you create a
REL file of record length 0, but it locks up the CPU
on the drive - nice. */
if (cmd_parse->recordlength < 2 || cmd_parse->recordlength > 254)
return CBMDOS_IPE_OVERFLOW;
/* skip the REL length */
p+=3;
cmdlen-=3;
}
cmd_parse->filetype = CBMDOS_FT_REL;
break;
case 'R':
cmd_parse->readmode = CBMDOS_FAM_READ;
break;
case 'W':
cmd_parse->readmode = CBMDOS_FAM_WRITE;
break;
case 'A':
cmd_parse->readmode = CBMDOS_FAM_APPEND;
break;
default:
if (cmd_parse->readmode != CBMDOS_FAM_READ
&& cmd_parse->readmode != CBMDOS_FAM_WRITE)
return CBMDOS_IPE_INVAL;
}
c = (char *)memchr(p, ',', cmdlen);
if (c) {
cmdlen -= (c - (const char *)p);
p = (BYTE *)c;
} else {
cmdlen = 0;
}
}
/* Override read mode if secondary is 0 or 1. */
if (cmd_parse->secondary == 0)
cmd_parse->readmode = CBMDOS_FAM_READ;
if (cmd_parse->secondary == 1)
cmd_parse->readmode = CBMDOS_FAM_WRITE;
/* Set filetype according secondary address, if it was not specified
and if we are in write mode */
if (cmd_parse->filetype == 0 && cmd_parse->readmode == CBMDOS_FAM_WRITE)
cmd_parse->filetype = (cmd_parse->secondary < 2)
? CBMDOS_FT_PRG : CBMDOS_FT_SEQ;
return CBMDOS_IPE_OK;
}