-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc1541.c
More file actions
3147 lines (2660 loc) · 89.1 KB
/
Copy pathc1541.c
File metadata and controls
3147 lines (2660 loc) · 89.1 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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* c1541.c - Stand-alone disk image maintenance program.
*
* Written by
* Ettore Perazzoli <ettore@comm2000.it>
* Teemu Rantanen <tvr@cs.hut.fi>
* Jouko Valta <jopi@zombie.oulu.fi>
* Gerhard Wesp <gwesp@cosy.sbg.ac.at>
* Daniel Sladic <sladic@eecg.toronto.edu>
* Ricardo Ferreira <storm@esoterica.pt>
* Andreas Boose <viceteam@t-online.de>
*
* Patches by
* Olaf Seibert <rhialto@mbfys.kun.nl>
* Dirk Schnorpfeil <D.Schnorpfeil@web.de> (GEOS stuff)
*
* Zipcode implementation based on `zip2disk' by
* Paul David Doherty <h0142kdd@rz.hu-berlin.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 "diskimage.h"
#include "diskimage/fsimage.h"
#include "diskcontents.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include "archdep.h"
#include "cbmdos.h"
#include "cbmimage.h"
#include "charset.h"
#include "cmdline.h"
#include "diskimage.h"
#include "event.h"
#include "fileio.h"
#include "gcr.h"
#include "info.h"
#include "imagecontents.h"
#include "ioutil.h"
#include "lib.h"
#include "log.h"
#include "network.h"
#include "serial.h"
#include "snapshot.h"
#include "tape.h"
#include "types.h"
#include "util.h"
#include "vdrive-bam.h"
#include "vdrive-command.h"
#include "vdrive-dir.h"
#include "vdrive-iec.h"
#include "vdrive.h"
#include "zipcode.h"
#define GEOS /* DiSc */
#define MAXARG 256
#define MAXDRIVE 1
#define C1541_VERSION_MAJOR 4
#define C1541_VERSION_MINOR 0
const char machine_name[] = "C1541";
/* Global clock counter. */
CLOCK clk = 0L;
static vdrive_t *drives[4] = { NULL, NULL, NULL, NULL };
static unsigned int p00save[4] = { 0, 0, 0, 0 };
static int drive_number = 0;
static int interactive_mode = 0;
/* Local functions. */
static int attach_cmd(int nargs, char **args);
static int block_cmd(int nargs, char **args);
static int check_drive(int dev, int mode);
static int copy_cmd(int nargs, char **args);
static int delete_cmd(int nargs, char **args);
static int extract_cmd(int nargs, char **args);
static int format_cmd(int nargs, char **args);
static int help_cmd(int nargs, char **args);
static int info_cmd(int nargs, char **args);
static int list_cmd(int nargs, char **args);
static int name_cmd(int nargs, char **args);
static int p00save_cmd(int nargs, char **args);
static int quit_cmd(int nargs, char **args);
static int raw_cmd(int nargs, char **args); /* @ */
static int read_cmd(int nargs, char **args);
static int rename_cmd(int nargs, char **args);
static int show_cmd(int nargs, char **args);
static int tape_cmd(int nargs, char **args);
static int unit_cmd(int nargs, char **args);
static int unlynx_cmd(int nargs, char **args);
static int validate_cmd(int nargs, char **args);
static int write_cmd(int nargs, char **args);
static int zcreate_cmd(int nargs, char **args);
static int open_image(int dev, char *name, int create, int disktype);
#ifdef GEOS
int internal_read_geos_file(int unit, FILE* outf, char* src_name_ascii);
static int read_geos_cmd(int nargs, char **args);
static int fix_ts(int unit, unsigned int trk, unsigned int sec,
unsigned int next_trk, unsigned int next_sec,
unsigned int blk_offset);
static int internal_write_geos_file(int unit, FILE* f);
static int write_geos_cmd(int nargs, char **args);
#endif
int rom1541_loaded = 0;
int rom1541ii_loaded = 0;
int rom1571_loaded = 0;
int rom1581_loaded = 0;
int rom2031_loaded = 0;
int rom1001_loaded = 0;
int rom2040_loaded = 0;
BYTE *drive_rom1541;
BYTE *drive_rom1541ii;
BYTE *drive_rom1571;
BYTE *drive_rom1581;
BYTE *drive_rom2031;
BYTE *drive_rom1001;
BYTE *drive_rom2040;
/* ------------------------------------------------------------------------- */
/* dummy functions */
int cmdline_register_options(const cmdline_option_t *c)
{
return 0;
}
int network_connected(void)
{
return 0;
}
int network_get_mode(void)
{
return NETWORK_IDLE;
}
void network_event_record(unsigned int type, void *data, unsigned int size)
{
}
void event_record_in_list(event_list_state_t *list, unsigned int type,
void *data, unsigned int size)
{
}
void ui_error(const char *format, ...)
{
}
/* ------------------------------------------------------------------------- */
struct command {
const char *name;
const char *syntax;
const char *description;
unsigned int min_args, max_args;
int (*func) (int nargs, char **args);
};
typedef struct command command_t;
const command_t command_list[] = {
{ "@",
"@ [<command>]",
"Execute specified CBM DOS command and print the current status of the\n"
"drive. If no <command> is specified, just print the status.",
0, 1, raw_cmd },
{ "?",
"? [<command>]",
"Explain specified command. If no command is specified, list available\n" "ones.",
0, 1, help_cmd },
{ "attach",
"attach <diskimage> [<unit>]",
"Attach <diskimage> to <unit> (default unit is 8).",
1, 2,
attach_cmd },
{ "block",
"block <track> <sector> <disp> [<drive>]",
"Show specified disk block in hex form.",
3, 4, block_cmd },
{ "copy",
"copy <source1> [<source2> ... <sourceN>] <destination>",
"Copy `source1' ... `sourceN' into destination. If N > 1, `destination'\n"
"must be a simple drive specifier (@n:).",
2, MAXARG, copy_cmd },
{ "delete",
"delete <file1> [<file2> ... <fileN>]",
"Delete the specified files.",
1, MAXARG,
delete_cmd },
{ "dir",
"dir [<pattern>]",
"List files matching <pattern> (default is all files).",
0, 1,
list_cmd },
{ "exit",
"exit",
"Exit (same as `quit').",
0, 0, quit_cmd },
{ "extract",
"extract [<unit>]",
"Extract all the files to the file system.",
0, 1, extract_cmd },
{ "format",
"format <diskname,id> [<type> <imagename>] [<unit>]",
"If <unit> is specified, format the disk in unit <unit>.\n"
"If <type> and <imagename> are specified, create a new image named\n"
"<imagename>, attach it to unit 8 and format it. <type> is a disk image\n"
"type, and must be either `x64', `d64' (both VC1541/2031), `g64' (VC1541/2031,\n"
"but in GCR coding, `d67' (2040 DOS1), `d71' (VC1571), `d81' (VC1581), \n"
"`d80' (CBM8050) or `d82' (CBM8250). Otherwise, format the disk in \n"
"the current unit, if any.",
1, 4,
format_cmd },
#ifdef GEOS
{ "geosread",
"geosread <source> [<destination>]",
"Read GEOS <source> from the disk image and copy it as a Convert file into \n"
"<destination> in the file system. If <destination> is not specified, copy \n"
"it into a file with the same name as <source>.",
1, 2, read_geos_cmd },
{ "geoswrite",
"geoswrite <source>",
"Write GOES Convert file <source> from the file system on a disk image.",
1, 1, write_geos_cmd },
#endif
{ "help",
"help [<command>]",
"Explain specified command. If no command is specified, list available\n" "ones.",
0, 1, help_cmd },
{ "info",
"info [<unit>]",
"Display information about unit <unit> (if unspecified, use the current\n" "one).",
0, 1, info_cmd },
{ "list",
"list [<pattern>]",
"List files matching <pattern> (default is all files).",
0, 1,
list_cmd },
{ "name",
"name <diskname>[,<id>] <unit>",
"Change image name.",
1, 2, name_cmd },
{ "p00save",
"p00save <enable> [<unit>]",
"Save P00 files to the file system.",
1, 2, p00save_cmd },
{ "quit",
"quit",
"Exit (same as `exit').",
0, 0, quit_cmd },
{ "read",
"read <source> [<destination>]",
"Read <source> from the disk image and copy it into <destination> in\n"
"the file system. If <destination> is not specified, copy it into a\n"
"file with the same name as <source>.",
1, 2, read_cmd },
{ "rename",
"rename <oldname> <newname>",
"Rename <oldname> into <newname>. The files must be on the same drive.",
2, 2, rename_cmd },
{ "show",
"show [copying | warranty]",
"Show conditions for redistributing copies of C1541 (`copying') or the\n"
"various kinds of warranty you do not have with C1541 (`warranty').",
1, 1, show_cmd },
{ "tape",
"tape <t64name> [<file1> ... <fileN>]",
"Extract files from a T64 image into the current drive.",
1, MAXARG, tape_cmd },
{ "unit",
"unit <number>",
"Make unit <number> the current unit.",
1, 1, unit_cmd },
{ "unlynx",
"unlynx <lynxname> [<unit>]",
"Extract the specified Lynx image file into the specified unit (default\n"
"is the current unit).",
1, 2, unlynx_cmd },
{ "validate",
"validate [<unit>]",
"Validate the disk in unit <unit>. If <unit> is not specified, validate\n"
"the disk in the current unit.",
0, 1, validate_cmd },
{ "write",
"write <source> [<destination>]",
"Write <source> from the file system into <destination> on a disk image.", 1, 2, write_cmd },
{ "zcreate",
"zcreate <d64name> <zipname> [<label,id>]",
"Create a D64 disk image out of a set of four Zipcoded files named\n"
"`1!<zipname>', `2!<zipname>', `3!<zipname>' and `4!<zipname>'.",
2, 3, zcreate_cmd },
{ NULL, NULL, NULL, 0, 0, NULL }
};
/* ------------------------------------------------------------------------- */
#ifndef HAVE_READLINE
static char *read_line(const char *prompt)
{
static char line[1024];
line[sizeof(line)-1] = 0; /* Make sure there is a 0 at the end of the string */
fputs(prompt, stdout);
fflush(stdout);
return fgets(line, sizeof(line)-1, stdin);
}
#else
# include "editline.h"
static char *read_line(const char *prompt)
{
static char *line;
lib_free(line);
line = readline(prompt);
if (line != 0 && *line != 0)
add_history(line);
return line;
}
#endif
static int split_args(const char *line, int *nargs, char **args)
{
const char *s;
char *d;
char tmp[256];
int begin_of_arg, in_quote;
*nargs = 0;
in_quote = 0;
d = tmp;
begin_of_arg = 1;
for (s = line;; s++) {
switch (*s) {
case '"':
begin_of_arg = 0;
in_quote = !in_quote;
continue;
case '\\':
begin_of_arg = 0;
*(d++) = *(++s);
continue;
case ' ':
case '\t':
case '\n':
case '\r':
case 0:
if (*s == 0 && in_quote) {
fprintf(stderr, "Unbalanced quotes.\n");
return -1;
}
if (!in_quote && !begin_of_arg) {
if (*nargs == MAXARG) {
fprintf(stderr, "Too many arguments.\n");
return -1;
} else {
size_t len;
len = d - tmp;
if (args[*nargs] != NULL)
args[*nargs] = lib_realloc(args[*nargs], len + 1);
else
args[*nargs] = lib_malloc(len + 1);
memcpy(args[*nargs], tmp, len);
args[*nargs][len] = 0;
begin_of_arg = 1;
(*nargs)++;
d = tmp;
}
}
if (*s == 0)
return 0;
if (!(*s == ' ' && in_quote)) {
break;
}
default:
begin_of_arg = 0;
*(d++) = *s;
}
}
return 0;
}
static int arg_to_int(const char *arg, int *return_value)
{
char *tailptr;
*return_value = (int)strtol(arg, &tailptr, 10);
if (ioutil_errno(IOUTIL_ERRNO_ERANGE))
return -1;
/* Only whitespace is allowed after the last valid character. */
if (!util_check_null_string(tailptr)) {
while (isspace(*tailptr))
tailptr++;
if (*tailptr != 0)
return -1;
}
return 0;
}
static void print_error_message(int errval)
{
if (errval < 0) {
switch (errval) {
case FD_OK:
break;
case FD_NOTREADY:
fprintf(stderr, "Drive not ready.\n");
break;
case FD_CHANGED:
fprintf(stderr, "Image file has changed on disk.\n");
break;
case FD_NOTRD:
fprintf(stderr, "Cannot read file.\n");
break;
case FD_NOTWRT:
fprintf(stderr, "Cannot write file.\n");
break;
case FD_WRTERR:
fprintf(stderr, "Floppy write failed.\n");
break;
case FD_RDERR:
fprintf(stderr, "Floppy read failed.\n");
break;
case FD_INCOMP:
fprintf(stderr, "Incompatible DOS version.\n");
break;
case FD_BADIMAGE:
fprintf(stderr, "Invalid image.\n"); /* Disk or tape */
break;
case FD_BADNAME:
fprintf(stderr, "Invalid filename.\n");
break;
case FD_BADVAL:
fprintf(stderr, "Illegal value.\n");
break;
case FD_BADDEV:
fprintf(stderr, "Illegal device number.\n");
break;
case FD_BAD_TS:
fprintf(stderr, "Inaccessible Track or Sector.\n");
break;
default:
fprintf(stderr, "Unknown error.\n");
}
}
}
#define LOOKUP_NOTFOUND -1
#define LOOKUP_AMBIGUOUS -2
#define LOOKUP_SUCCESSFUL(n) ((n) >= 0)
static int lookup_command(const char *cmd)
{
size_t cmd_len;
int match;
int i;
match = LOOKUP_NOTFOUND;
cmd_len = strlen(cmd);
for (i = 0; command_list[i].name != NULL; i++) {
size_t len;
len = strlen(command_list[i].name);
if (len < cmd_len)
continue;
if (memcmp(command_list[i].name, cmd, cmd_len) == 0) {
if (match != -1)
return LOOKUP_AMBIGUOUS;
match = i;
if (len == cmd_len)
break; /* Exact match. */
}
}
return match;
}
static int lookup_and_execute_command(int nargs, char **args)
{
int match;
match = lookup_command(args[0]);
if (LOOKUP_SUCCESSFUL(match)) {
const command_t *cp;
cp = &command_list[match];
if (nargs - 1 < (int)(cp->min_args)
|| nargs - 1 > (int)(cp->max_args)) {
fprintf(stderr, "Wrong number of arguments.\n");
fprintf(stderr, "Syntax: %s\n", cp->syntax);
return -1;
} else {
int retval;
retval = command_list[match].func(nargs, args);
print_error_message(retval);
if (retval == FD_OK)
return 0;
else
return -1;
}
} else {
if (match == LOOKUP_AMBIGUOUS)
fprintf(stderr,
"Command `%s' is ambiguous. Try `help'.\n", args[0]);
else
fprintf(stderr,
"Command `%s' unrecognized. Try `help'.\n", args[0]);
return -1;
}
}
static char *extract_unit_from_file_name(const char *name,
unsigned int *unit_return)
{
if (name[0] == '@' && name[2] == ':'
&& (name[1] == '8' || name[1] == '9')) {
*unit_return = (unsigned int)(name[1] - '8');
return (char *)name + 3;
} else {
return NULL;
}
}
static int is_valid_cbm_file_name(const char *name)
{
/* Notice that ':' is the same on PETSCII and ASCII. */
return strchr(name, ':') == NULL;
}
/* ------------------------------------------------------------------------- */
/* A simple pager. */
/* It would be cool to have it in the monitor too. */
static int pager_x, pager_y, pager_num_cols, pager_num_lines;
static void pager_init(void)
{
if (ioutil_isatty(fileno(stdout))) {
pager_x = pager_y = 0;
pager_num_lines = archdep_num_text_lines();
pager_num_cols = archdep_num_text_columns();
} else {
pager_num_lines = pager_num_cols = -1;
}
}
static void pager_print(const char *text)
{
const char *p;
if (pager_num_lines < 0 || pager_num_cols < 0) {
fputs(text, stdout);
} else {
for (p = text; *p != 0; p++) {
if (*p != '\n') {
pager_x++;
if (pager_x > pager_num_cols) {
pager_y++;
pager_x = 0;
}
} else {
pager_x = 0;
pager_y++;
}
if (interactive_mode && (pager_y == pager_num_lines - 1)) {
char *s;
if (*p == '\n')
putchar(*p);
s = read_line("---Type <return> to continue, or q <return> to quit---");
if (s != NULL && toupper((int) *s) == 'Q')
break;
pager_x = pager_y = 0;
if (*p != '\n')
putchar(*p);
} else {
putchar(*p);
}
}
}
}
/* ------------------------------------------------------------------------- */
static int open_disk_image(vdrive_t *vdrive, const char *name,
unsigned int unit)
{
disk_image_t *image;
image = disk_image_create();
if (archdep_file_is_blockdev(name)) {
image->device = DISK_IMAGE_DEVICE_RAW;
serial_device_type_set(SERIAL_DEVICE_RAW, unit);
serial_realdevice_disable();
} else {
if (archdep_file_is_chardev(name)) {
image->device = DISK_IMAGE_DEVICE_REAL;
serial_device_type_set(SERIAL_DEVICE_REAL, unit);
serial_realdevice_enable();
} else {
image->device = DISK_IMAGE_DEVICE_FS;
serial_device_type_set(SERIAL_DEVICE_FS, unit);
serial_realdevice_disable();
}
}
disk_image_media_create(image);
image->gcr = gcr_create_image();
image->read_only = 0;
disk_image_name_set(image, lib_stralloc(name));
if (disk_image_open(image) < 0) {
disk_image_media_destroy(image);
disk_image_destroy(image);
fprintf(stderr, "Cannot open file `%s'.\n", name);
return -1;
}
vdrive_device_setup(vdrive, unit);
vdrive->image = image;
vdrive_attach_image(image, unit, vdrive);
return 0;
}
static void close_disk_image(vdrive_t *vdrive, int unit)
{
disk_image_t *image;
image = vdrive->image;
if (image != NULL) {
vdrive_detach_image(image, unit, vdrive);
gcr_destroy_image(image->gcr);
if (image->device == DISK_IMAGE_DEVICE_REAL)
serial_realdevice_disable();
disk_image_close(image);
disk_image_media_destroy(image);
disk_image_destroy(image);
vdrive->image = NULL;
}
}
/* Open image or create a new one. If the file exists, it must have valid
header. */
static int open_image(int dev, char *name, int create, int disktype)
{
if (dev < 0 || dev > MAXDRIVE)
return -1;
if (create) {
if (cbmimage_create_image(name, disktype) < 0) {
printf("Cannot create disk image.\n");
return -1;
}
}
if (open_disk_image(drives[dev], name, dev + 8) < 0) {
printf("Cannot open disk image.\n");
return -1;
}
return 0;
}
static int check_drive(int dev, int flags)
{
vdrive_t *vdrive;
dev &= 7;
if (dev < 0 || dev > 3)
return FD_BADDEV;
vdrive = drives[dev & 3];
if (flags != CHK_NUM && (vdrive == NULL || vdrive->image == NULL))
return FD_NOTREADY;
return FD_OK;
}
/* ------------------------------------------------------------------------- */
/* Here are the commands. */
/* Note: The double ASCII/PETSCII copies of file names we keep in some
functions are needed because we want to print the names of the files being
copied in ASCII and we don't trust `charset_petconvstring()' to be
reliable to get the original value back when we convert ASCII -> PETSCII
and then PETSCII -> ASCII again. */
static int attach_cmd(int nargs, char **args)
{
int dev = 0;
switch (nargs) {
case 2:
/* attach <image> */
dev = drive_number;
break;
case 3:
/* attach <image> <unit> */
if (arg_to_int(args[2], &dev) < 0)
return FD_BADDEV;
break;
}
if (check_drive(dev, CHK_NUM) < 0)
return FD_BADDEV;
open_disk_image(drives[dev & 3], args[1], (dev & 3) + 8);
return FD_OK;
}
static int block_cmd(int nargs, char **args)
{
int drive, disp;
int track, sector;
vdrive_t *vdrive;
BYTE *buf, str[20], sector_data[256];
int cnt;
/* block <track> <sector> <disp> [<drive>] show disk blocks in hex form */
if (arg_to_int(args[1], &track) < 0 || arg_to_int(args[2], §or) < 0)
return FD_BAD_TS;
if (arg_to_int(args[3], &disp) < 0)
return FD_BADVAL;
if (nargs == 5) {
if (arg_to_int(args[4], &drive) < 0)
return FD_BADDEV;
if (check_drive(drive, CHK_NUM) < 0)
return FD_BADDEV;
drive -= 8;
} else {
drive = drive_number;
}
if (check_drive(drive, CHK_RDY) < 0)
return FD_NOTREADY;
vdrive = drives[drive & 3];
if (disk_image_check_sector(vdrive->image, track, sector) < 0)
return FD_BAD_TS;
/* Read one block */
if (disk_image_read_sector(vdrive->image, sector_data, track, sector)
!= 0) {
fprintf(stderr, "Cannot read track %i sector %i.", track, sector);
return FD_RDERR;
}
buf = sector_data;
/* Show block */
printf("<%2d: %2d %2d>\n", drive, track, sector);
str[16] = 0;
for (; disp < 256;) {
printf("> %02X ", disp & 255);
for (cnt = 0; cnt < 16; cnt++, disp++) {
printf(" %02X", buf[disp & 255]);
str[cnt] = (buf[disp & 255] < ' ' ?
'.' : charset_p_toascii(buf[disp & 255], 0));
}
printf(" ;%s\n", str);
}
/* Find next sector for the file being traced. */
if (buf[0] && buf[1]) {
track = buf[0];
sector = buf[1];
} else {
if (disk_image_check_sector(vdrive->image, track, ++sector) < 0) {
sector = 0;
if ((unsigned int)(++track) > vdrive->image->tracks)
track = vdrive->Dir_Track;
}
}
return FD_OK;
}
static int copy_cmd(int nargs, char **args)
{
char *p;
char *dest_name_ascii, *dest_name_petscii;
unsigned int dest_unit, src_unit;
int i;
p = extract_unit_from_file_name(args[nargs - 1], &dest_unit);
if (p == NULL) {
if (nargs > 3) {
fprintf(stderr,
"The destination must be a drive if multiple sources are specified.\n");
return FD_OK; /* FIXME */
}
dest_name_ascii = lib_stralloc(args[nargs - 1]);
dest_name_petscii = lib_stralloc(dest_name_ascii);
charset_petconvstring((BYTE *)dest_name_petscii, 0);
dest_unit = drive_number;
} else {
if (*p != 0) {
if (nargs > 3) {
fprintf(stderr,
"The destination must be a drive if multiple sources are specified.\n");
return FD_OK; /* FIXME */
}
dest_name_ascii = lib_stralloc(p);
dest_name_petscii = lib_stralloc(dest_name_ascii);
charset_petconvstring((BYTE *)dest_name_petscii, 0);
} else {
dest_name_ascii = dest_name_petscii = NULL;
}
}
if (dest_name_ascii != NULL && !is_valid_cbm_file_name(dest_name_ascii)) {
fprintf(stderr,
"`%s' is not a valid CBM DOS file name.\n", dest_name_ascii);
return FD_OK; /* FIXME */
}
if (check_drive(dest_unit, CHK_RDY) < 0)
return FD_NOTREADY;
for (i = 1; i < nargs - 1; i++) {
char *src_name_ascii, *src_name_petscii;
p = extract_unit_from_file_name(args[i], &src_unit);
if (p == NULL) {
src_name_ascii = lib_stralloc(args[i]);
src_unit = drive_number;
} else {
if (check_drive(src_unit, CHK_RDY) < 0)
return FD_NOTREADY;
src_name_ascii = lib_stralloc(p);
}
if (!is_valid_cbm_file_name(src_name_ascii)) {
fprintf(stderr, "`%s' is not a valid CBM DOS file name: ignored.\n", src_name_ascii);
lib_free(src_name_ascii);
continue;
}
src_name_petscii = lib_stralloc(src_name_ascii);
charset_petconvstring((BYTE *)src_name_petscii, 0);
if (vdrive_iec_open(drives[src_unit], (BYTE *)src_name_petscii,
(int)strlen(src_name_petscii), 0, NULL)) {
fprintf(stderr, "Cannot read `%s'.\n", src_name_ascii);
if (dest_name_ascii != NULL) {
lib_free(dest_name_ascii);
lib_free(dest_name_petscii);
}
lib_free(src_name_ascii);
lib_free(src_name_petscii);
return FD_RDERR;
}
if (dest_name_ascii != NULL) {
if (vdrive_iec_open(drives[dest_unit], (BYTE *)dest_name_petscii,
(int)strlen(dest_name_petscii), 1, NULL)) {
fprintf(stderr, "Cannot write `%s'.\n", dest_name_petscii);
vdrive_iec_close(drives[src_unit], 0);
lib_free(dest_name_ascii);
lib_free(dest_name_petscii);
lib_free(src_name_ascii);
lib_free(src_name_petscii);
return FD_OK;
}
} else {
if (vdrive_iec_open(drives[dest_unit], (BYTE *)src_name_petscii,
(int)strlen(src_name_petscii), 1, NULL)) {
fprintf(stderr, "Cannot write `%s'.\n", src_name_petscii);
vdrive_iec_close(drives[src_unit], 0);
lib_free(src_name_ascii);
lib_free(src_name_petscii);
return FD_OK;
}
}
printf("Copying `%s'...\n", args[i]); /* FIXME */
{
BYTE c;
int status = 0;
do {
status = vdrive_iec_read(drives[src_unit], ((BYTE *)&c), 0);
if (vdrive_iec_write(drives[dest_unit], ((BYTE)(c)), 1)) {
fprintf(stderr, "No space on image ?\n");
break;
}
} while (status == SERIAL_OK);
}
vdrive_iec_close(drives[src_unit], 0);
vdrive_iec_close(drives[dest_unit], 1);
lib_free(src_name_ascii);
lib_free(src_name_petscii);
}
lib_free(dest_name_ascii);
lib_free(dest_name_petscii);
return FD_OK;
}
static int delete_cmd(int nargs, char **args)
{
int i = 1, status;
if (check_drive(drive_number, CHK_RDY) < 0)
return FD_NOTREADY;
for (i = 1; i < nargs; i++) {
unsigned int dnr;
char *p, *name;
char *command;
p = extract_unit_from_file_name(args[i], &dnr);
if (p == NULL) {
dnr = drive_number;
name = args[i];
} else {
name = p;
}
if (!is_valid_cbm_file_name(name)) {
fprintf(stderr,
"`%s' is not a valid CBM DOS file name: ignored.\n", name);
continue;
}