Repository navigation
Expand file tree
/
Copy path1541fs.cpp
More file actions
executable file
·734 lines (599 loc) · 13.3 KB
/
Copy path1541fs.cpp
File metadata and controls
executable file
·734 lines (599 loc) · 13.3 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/*
* 1541fs.cpp - 1541 emulation in host file system
*
* Frodo (C) 1994-1997,2002 Christian Bauer
*
* Notes:
* ------
*
* - If the directory is opened (file name "$"), a temporary file
* with the structure of a 1541 directory file is created and
* opened. It can then be accessed in the same way as all other
* files.
*
* Incompatibilities:
* ------------------
*
* - No "raw" directory reading
* - No relative/sequential/user files
* - Only "I" and "UJ" commands implemented
*/
#include "sysdeps.h"
#include "1541fs.h"
#include "IEC.h"
#include "main.h"
#include "Prefs.h"
#ifdef __riscos__
#include "ROlib.h"
#endif
// Access modes
enum {
FMODE_READ, FMODE_WRITE, FMODE_APPEND
};
// File types
enum {
FTYPE_PRG, FTYPE_SEQ
};
// Prototypes
static bool match(char *p, char *n);
/*
* Constructor: Prepare emulation
*/
FSDrive::FSDrive(IEC *iec, char *path) : Drive(iec)
{
strcpy(orig_dir_path, path);
dir_path[0] = 0;
if (change_dir(orig_dir_path)) {
for (int i=0; i<16; i++)
file[i] = NULL;
Reset();
Ready = true;
}
}
/*
* Destructor
*/
FSDrive::~FSDrive()
{
if (Ready) {
close_all_channels();
Ready = false;
}
}
/*
* Change emulation directory
*/
bool FSDrive::change_dir(char *dirpath)
{
#ifndef __riscos__
DIR *dir;
if ((dir = opendir(dirpath)) != NULL) {
closedir(dir);
strcpy(dir_path, dirpath);
strncpy(dir_title, dir_path, 16);
return true;
} else
return false;
#else
int Info[4];
if ((ReadCatalogueInfo(dirpath,Info) & 2) != 0) // Directory or image file
{
strcpy(dir_path, dirpath);
strncpy(dir_title, dir_path, 16);
return true;
}
else
{
return false;
}
#endif
}
/*
* Open channel
*/
uint8 FSDrive::Open(int channel, char *filename)
{
set_error(ERR_OK);
// Channel 15: Execute file name as command
if (channel == 15) {
execute_command(filename);
return ST_OK;
}
// Close previous file if still open
if (file[channel]) {
fclose(file[channel]);
file[channel] = NULL;
}
if (filename[0] == '$')
return open_directory(channel, filename+1);
if (filename[0] == '#') {
set_error(ERR_NOCHANNEL);
return ST_OK;
}
return open_file(channel, filename);
}
/*
* Open file
*/
uint8 FSDrive::open_file(int channel, char *filename)
{
char plainname[NAMEBUF_LENGTH];
int filemode = FMODE_READ;
int filetype = FTYPE_PRG;
bool wildflag = false;
char *mode = "rb";
convert_filename(filename, plainname, &filemode, &filetype, &wildflag);
// Channel 0 is READ PRG, channel 1 is WRITE PRG
if (!channel) {
filemode = FMODE_READ;
filetype = FTYPE_PRG;
}
if (channel == 1) {
filemode = FMODE_WRITE;
filetype = FTYPE_PRG;
}
// Wildcards are only allowed on reading
if (wildflag) {
if (filemode != FMODE_READ) {
set_error(ERR_SYNTAX33);
return ST_OK;
}
find_first_file(plainname);
}
// Select fopen() mode according to file mode
switch (filemode) {
case FMODE_READ:
mode = "rb";
break;
case FMODE_WRITE:
mode = "wb";
break;
case FMODE_APPEND:
mode = "ab";
break;
}
// Open file
#ifndef __riscos__
if (chdir(dir_path))
set_error(ERR_NOTREADY);
else if ((file[channel] = fopen(plainname, mode)) != NULL) {
if (filemode == FMODE_READ) // Read and buffer first byte
read_char[channel] = fgetc(file[channel]);
} else
set_error(ERR_FILENOTFOUND);
chdir(AppDirPath);
#else
{
char fullname[NAMEBUF_LENGTH];
// On RISC OS make a full filename
sprintf(fullname,"%s.%s",dir_path,plainname);
if ((file[channel] = fopen(fullname, mode)) != NULL)
{
if (filemode == FMODE_READ)
{
read_char[channel] = fgetc(file[channel]);
}
}
else
{
set_error(ERR_FILENOTFOUND);
}
}
#endif
return ST_OK;
}
/*
* Analyze file name, get access mode and type
*/
void FSDrive::convert_filename(char *srcname, char *destname, int *filemode, int *filetype, bool *wildflag)
{
char *p, *q;
int i;
// Search for ':', p points to first character after ':'
if ((p = strchr(srcname, ':')) != NULL)
p++;
else
p = srcname;
// Convert char set of the remaining string -> destname
q = destname;
for (i=0; i<NAMEBUF_LENGTH && (*q++ = conv_from_64(*p++, true)); i++) ;
// Look for mode parameters seperated by ','
p = destname;
while ((p = strchr(p, ',')) != NULL) {
// Cut string after the first ','
*p++ = 0;
switch (*p) {
case 'p':
*filetype = FTYPE_PRG;
break;
case 's':
*filetype = FTYPE_SEQ;
break;
case 'r':
*filemode = FMODE_READ;
break;
case 'w':
*filemode = FMODE_WRITE;
break;
case 'a':
*filemode = FMODE_APPEND;
break;
}
}
// Search for wildcards
*wildflag = (strchr(destname, '?') != NULL) || (strchr(destname, '*') != NULL);
}
/*
* Find first file matching wildcard pattern and get its real name
*/
// Return true if name 'n' matches pattern 'p'
static bool match(char *p, char *n)
{
if (!*p) // Null pattern matches everything
return true;
do {
if (*p == '*') // Wildcard '*' matches all following characters
return true;
if ((*p != *n) && (*p != '?')) // Wildcard '?' matches single character
return false;
p++; n++;
} while (*p);
return !*n;
}
void FSDrive::find_first_file(char *name)
{
#ifndef __riscos__
DIR *dir;
struct dirent *de;
// Open directory for reading and skip '.' and '..'
if ((dir = opendir(dir_path)) == NULL)
return;
de = readdir(dir);
while (de && (0 == strcmp(".", de->d_name) || 0 == strcmp("..", de->d_name)))
de = readdir(dir);
while (de) {
// Match found? Then copy real file name
if (match(name, de->d_name)) {
strncpy(name, de->d_name, NAMEBUF_LENGTH);
closedir(dir);
return;
}
// Get next directory entry
de = readdir(dir);
}
closedir(dir);
#else
dir_env de;
char Buffer[NAMEBUF_LENGTH];
de.offset = 0; de.buffsize = NAMEBUF_LENGTH; de.match = name;
do
{
de.readno = 1;
if (ReadDirName(dir_path,Buffer,&de) != NULL) {de.offset = -1;}
else if (de.offset != -1)
{
if (match(name,Buffer))
{
strncpy(name, Buffer, NAMEBUF_LENGTH);
return;
}
}
}
while (de.offset != -1);
#endif
}
/*
* Open directory, create temporary file
*/
uint8 FSDrive::open_directory(int channel, char *filename)
{
char buf[] = "\001\004\001\001\0\0\022\042 \042 00 2A";
char str[NAMEBUF_LENGTH];
char pattern[NAMEBUF_LENGTH];
char *p, *q;
int i;
int filemode;
int filetype;
bool wildflag;
#ifndef __riscos__
DIR *dir;
struct dirent *de;
struct stat statbuf;
// Special treatment for "$0"
if (filename[0] == '0' && filename[1] == 0)
filename += 1;
// Convert filename ('$' already stripped), filemode/type are ignored
convert_filename(filename, pattern, &filemode, &filetype, &wildflag);
// Open directory for reading and skip '.' and '..'
if ((dir = opendir(dir_path)) == NULL) {
set_error(ERR_NOTREADY);
return ST_OK;
}
de = readdir(dir);
while (de && (0 == strcmp(".", de->d_name) || 0 == strcmp("..", de->d_name)))
de = readdir(dir);
// Create temporary file
if ((file[channel] = tmpfile()) == NULL) {
closedir(dir);
return ST_OK;
}
// Create directory title
p = &buf[8];
for (i=0; i<16 && dir_title[i]; i++)
*p++ = conv_to_64(dir_title[i], false);
fwrite(buf, 1, 32, file[channel]);
// Create and write one line for every directory entry
while (de) {
// Include only files matching the pattern
if (match(pattern, de->d_name)) {
// Get file statistics
chdir(dir_path);
stat(de->d_name, &statbuf);
chdir(AppDirPath);
// Clear line with spaces and terminate with null byte
memset(buf, ' ', 31);
buf[31] = 0;
p = buf;
*p++ = 0x01; // Dummy line link
*p++ = 0x01;
// Calculate size in blocks (254 bytes each)
i = (statbuf.st_size + 254) / 254;
*p++ = i & 0xff;
*p++ = (i >> 8) & 0xff;
p++;
if (i < 10) p++; // Less than 10: add one space
if (i < 100) p++; // Less than 100: add another space
// Convert and insert file name
strcpy(str, de->d_name);
*p++ = '\"';
q = p;
for (i=0; i<16 && str[i]; i++)
*q++ = conv_to_64(str[i], true);
*q++ = '\"';
p += 18;
// File type
if (S_ISDIR(statbuf.st_mode)) {
*p++ = 'D';
*p++ = 'I';
*p++ = 'R';
} else {
*p++ = 'P';
*p++ = 'R';
*p++ = 'G';
}
// Write line
fwrite(buf, 1, 32, file[channel]);
}
// Get next directory entry
de = readdir(dir);
}
#else
dir_full_info di;
dir_env de;
// Much of this is very similar to the original
if ((filename[0] == '0') && (filename[1] == 0)) {filename++;}
// Concatenate dir_path and pattern in buffer pattern ==> read subdirs!
strcpy(pattern,dir_path);
convert_filename(filename, pattern + strlen(pattern), &filemode, &filetype, &wildflag);
// We don't use tmpfile() -- problems involved!
DeleteFile(RO_TEMPFILE); // first delete it, if it exists
if ((file[channel] = fopen(RO_TEMPFILE,"wb+")) == NULL)
{
return(ST_OK);
}
de.offset = 0; de.buffsize = NAMEBUF_LENGTH; de.match = filename;
// Create directory title - copied from above
p = &buf[8];
for (i=0; i<16 && dir_title[i]; i++)
*p++ = conv_to_64(dir_title[i], false);
fwrite(buf, 1, 32, file[channel]);
do
{
de.readno = 1;
if (ReadDirNameInfo(pattern,&di,&de) != NULL) {de.offset = -1;}
else if (de.offset != -1) // don't have to check for match here
{
memset(buf,' ',31); buf[31] = 0; // most of this: see above
p = buf; *p++ = 0x01; *p++ = 0x01;
i = (di.length + 254) / 254; *p++ = i & 0xff; *p++ = (i>>8) & 0xff;
p++;
if (i < 10) {*p++ = ' ';}
if (i < 100) {*p++ = ' ';}
strcpy(str, di.name);
*p++ = '\"'; q = p;
for (i=0; (i<16 && str[i]); i++)
{
*q++ = conv_to_64(str[i], true);
}
*q++ = '\"'; p += 18;
if ((di.otype & 2) == 0)
{
*p++ = 'P'; *p++ = 'R'; *p++ = 'G';
}
else
{
*p++ = 'D'; *p++ = 'I'; *p++ = 'R';
}
fwrite(buf, 1, 32, file[channel]);
}
}
while (de.offset != -1);
#endif
// Final line
fwrite("\001\001\0\0BLOCKS FREE. \0\0", 1, 32, file[channel]);
// Rewind file for reading and read first byte
rewind(file[channel]);
read_char[channel] = fgetc(file[channel]);
#ifndef __riscos
// Close directory
closedir(dir);
#endif
return ST_OK;
}
/*
* Close channel
*/
uint8 FSDrive::Close(int channel)
{
if (channel == 15) {
close_all_channels();
return ST_OK;
}
if (file[channel]) {
fclose(file[channel]);
file[channel] = NULL;
}
return ST_OK;
}
/*
* Close all channels
*/
void FSDrive::close_all_channels(void)
{
for (int i=0; i<15; i++)
Close(i);
cmd_len = 0;
}
/*
* Read from channel
*/
uint8 FSDrive::Read(int channel, uint8 *byte)
{
int c;
// Channel 15: Error channel
if (channel == 15) {
*byte = *error_ptr++;
if (*byte != '\r')
return ST_OK;
else { // End of message
set_error(ERR_OK);
return ST_EOF;
}
}
if (!file[channel]) return ST_READ_TIMEOUT;
// Read one byte
*byte = read_char[channel];
c = fgetc(file[channel]);
if (c == EOF)
return ST_EOF;
else {
read_char[channel] = c;
return ST_OK;
}
}
/*
* Write to channel
*/
uint8 FSDrive::Write(int channel, uint8 byte, bool eoi)
{
// Channel 15: Collect chars and execute command on EOI
if (channel == 15) {
if (cmd_len >= 40)
return ST_TIMEOUT;
cmd_buffer[cmd_len++] = byte;
if (eoi) {
cmd_buffer[cmd_len] = 0;
cmd_len = 0;
execute_command(cmd_buffer);
}
return ST_OK;
}
if (!file[channel]) {
set_error(ERR_FILENOTOPEN);
return ST_TIMEOUT;
}
if (fputc(byte, file[channel]) == EOF) {
set_error(ERR_WRITEERROR);
return ST_TIMEOUT;
}
return ST_OK;
}
/*
* Execute command string
*/
void FSDrive::execute_command(char *command)
{
switch (command[0]) {
case 'I':
close_all_channels();
set_error(ERR_OK);
break;
case 'U':
if ((command[1] & 0x0f) == 0x0a) {
Reset();
} else
set_error(ERR_SYNTAX30);
break;
case 'G':
if (command[1] != ':')
set_error(ERR_SYNTAX30);
else
chdir_cmd(&command[2]);
break;
default:
set_error(ERR_SYNTAX30);
}
}
/*
* Execute 'G' command
*/
void FSDrive::chdir_cmd(char *dirpath)
{
char str[NAMEBUF_LENGTH];
char *p = str;
close_all_channels();
// G:. resets the directory path to its original setting
if (dirpath[0] == '.' && dirpath[1] == 0) {
change_dir(orig_dir_path);
} else {
// Convert directory name
for (int i=0; i<NAMEBUF_LENGTH && (*p++ = conv_from_64(*dirpath++, false)); i++) ;
if (!change_dir(str))
set_error(ERR_NOTREADY);
}
}
/*
* Reset drive
*/
void FSDrive::Reset(void)
{
close_all_channels();
cmd_len = 0;
set_error(ERR_STARTUP);
}
/*
* Conversion PETSCII->ASCII
*/
uint8 FSDrive::conv_from_64(uint8 c, bool map_slash)
{
if ((c >= 'A') && (c <= 'Z') || (c >= 'a') && (c <= 'z'))
return c ^ 0x20;
if ((c >= 0xc1) && (c <= 0xda))
return c ^ 0x80;
if ((c == '/') && map_slash && ThePrefs.MapSlash)
#ifdef __riscos__
return '.'; // directory separator is '.' in RO
if (c == '.') {return('_');} // convert dot to underscore
#else
return '\\';
#endif
return c;
}
/*
* Conversion ASCII->PETSCII
*/
uint8 FSDrive::conv_to_64(uint8 c, bool map_slash)
{
if ((c >= 'A') && (c <= 'Z') || (c >= 'a') && (c <= 'z'))
return c ^ 0x20;
#ifdef __riscos__
if ((c == '.') && map_slash && ThePrefs.MapSlash)
#else
if ((c == '\\') && map_slash && ThePrefs.MapSlash)
#endif
return '/';
#ifdef __riscos__
if (c == '_') {return('.');} // convert underscore to dot
#endif
return c;
}