forked from samstyle/mctrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
820 lines (764 loc) · 18.2 KB
/
main.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define TYPE_UNKNOWN 0
#define TYPE_SCL 1
#define TYPE_TRD 2
#define TYPE_TAP 3
#ifdef _WIN32
#define SLASH '\\'
#endif
#ifdef __linux
#define SLASH '/'
#endif
// structures
typedef struct {
char name[10];
char ext[3];
} xName;
struct {
unsigned isBasic:1;
unsigned headLess:1;
unsigned asHobeta:1;
unsigned mono:1;
int aStart;
} opt;
#pragma pack(push,1)
typedef struct {
char type;
char name[10];
unsigned char dll;
unsigned char dlh;
unsigned char asl;
unsigned char ash;
unsigned char pll;
unsigned char plh;
} tHead;
typedef struct {
char name[8];
char ext;
unsigned char stl;
unsigned char sth;
unsigned char lnl;
unsigned char lnh;
unsigned char slen;
unsigned char sec;
unsigned char trk;
} trdFile;
#pragma pack(pop)
// common
int readfile(char* path, char* buf, int maxlen) {
FILE *infile = fopen(path,"rb");
if (!infile) {
printf("Can't open file '%s'\n",path);
return -1; // failed to open file
}
fseek(infile, 0, SEEK_END);
int iflen = ftell(infile);
rewind(infile);
if (iflen <= maxlen) {
fread(buf, iflen, 1, infile);
} else {
printf("File '%s' is too long (max is 0x%X bytes)\n", path, maxlen);
iflen = -2;
}
fclose(infile);
return iflen;
}
int savefile(char* path, char* buf, int len) {
FILE *file = fopen(path, "wb");
if (!file) {
printf("Can't open file '%s' for writing\n",path);
return 0;
}
if (buf && (len > 0)) fwrite(buf, len, 1, file);
fclose(file);
return 1;
}
int testExt(char* path, char* ext) {
char* ptr = strrchr(path, '.');
if (!ptr) return 0;
char buf[255];
strcpy(buf, ptr + 1);
ptr = buf;
while (*ptr) {
*ptr = tolower(*ptr);
ptr++;
}
if (strcmp(ext, buf)) return 0;
return 1;
}
int getFileType(char* path) {
FILE* file = fopen(path, "rb");
char buf[9];
int res = TYPE_UNKNOWN;
if (testExt(path, "tap")) {
res = TYPE_TAP;
} else if (testExt(path, "scl") && file) {
fread(buf, 9, 1, file);
if ((strncmp(buf, "SINCLAIR", 8) == 0) && ((unsigned)buf[8] < 129))
res = TYPE_SCL;
} else if (testExt(path, "trd") && file) {
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
fseek(file, 0x8e7, SEEK_SET);
int type = fgetc(file);
if ((size > 0x8ff) && (size < 0xa0001) && (type == 0x10))
res = TYPE_TRD;
}
if (file) fclose(file);
return res;
}
xName cutName(char* path) {
char fnam[strlen(path) + 1];
char* ptr = strrchr(path, SLASH); // last slash
if (ptr) {
strcpy(fnam, ptr + 1);
} else {
strcpy(fnam, path);
}
ptr = strrchr(fnam, '.');
xName nm;
memset(nm.name, 0x20, 10);
memset(nm.ext, 0x20, 3);
if (ptr) {
*ptr = 0x00;
memcpy(nm.ext, ptr + 1, (strlen(ptr + 1) > 3) ? 3 : strlen(ptr + 1));
}
memcpy(nm.name, fnam, (strlen(fnam) > 10) ? 10 : strlen(fnam));
return nm;
}
int stdAccumulate(unsigned char* ptr, int size, int res) {
while (size > 0) {
res += *ptr;
ptr++;
size--;
}
return res;
}
int fgeti(FILE* file) {
int res = fgetc(file) & 0xff;
res |= (fgetc(file) << 8);
res |= (fgetc(file) << 16);
res |= (fgetc(file) << 24);
return res;
}
void fputi(FILE* file, int val) {
fputc(val & 0xff, file);
fputc((val >> 8) & 0xff, file);
fputc((val >> 16) & 0xff, file);
fputc((val >> 24) & 0xff, file);
}
// disk common
void makeName(char* name, char* buf) {
memset(buf, 0x20, 9);
char* ptr = strrchr(name, '.');
if (ptr) {
memcpy(buf, name, ((ptr - name) > 8) ? 8 : (ptr - name));
ptr++;
if (*ptr) buf[8] = *ptr;
} else {
memcpy(buf, name, (strlen(name) > 8) ? 8 : strlen(name));
}
}
int getSum(char* data, int len) {
unsigned int crc = 0;
while (len--) {
crc += (unsigned char)*data;
data++;
}
return crc;
}
trdFile makeDSC(char* path, int len) {
trdFile hd;
xName nm = cutName(path);
memcpy(hd.name, nm.name, 8);
hd.lnl = len & 0xff;
hd.lnh = (len >> 8) & 0xff;
if (opt.isBasic) {
hd.ext = 'B';
hd.stl = hd.lnl;
hd.sth = hd.lnh;
len += 4; // 80,AA,<AS.line>
} else {
hd.ext = nm.ext[0];
hd.stl = nm.ext[1];
hd.sth = nm.ext[2];
}
hd.slen = ((len >> 8) & 0xff) + ((len & 0xff) ? 1 : 0);
return hd;
}
trdFile prepareFile(char* path, char* buf) {
memset(buf, 0x00, 0x10000);
trdFile hd;
int ilen;
memset(hd.name, 0x00, sizeof(trdFile));
if (opt.asHobeta) {
ilen = readfile(path, buf, 0xff11); // +17 bytes header
if (ilen > 0) {
if (!buf[13])
buf[13] = buf[14];
memcpy((char*)&hd, buf, 15);
memcpy(buf, buf+17, ilen-17);
}
} else {
ilen = readfile(path, buf, 0xff00);
if (ilen > 0) {
if (opt.isBasic) {
buf[ilen] = 0x80;
buf[ilen+1] = 0xaa;
buf[ilen+2] = opt.aStart & 0xff;
buf[ilen+3] = (opt.aStart >> 8) & 0xff;
}
hd = makeDSC(path, ilen);
}
}
return hd;
}
int saveoutput(char* oname, char* buf, trdFile hd) {
int len;
if ((hd.lnh + (hd.lnl ? 1 : 0)) == hd.slen) {
len = hd.lnl | (hd.lnh << 8);
} else {
len = hd.slen << 8;
}
int res;
if (opt.asHobeta) {
char hbuf[0x10010];
memcpy(hbuf, (char*)&hd, 13);
hbuf[13] = 0x00;
hbuf[14] = hd.slen;
int crc = ((105 + 257 * stdAccumulate((unsigned char*)hbuf, 15, 0)) & 0xffff);
hbuf[15] = crc & 0xff;
hbuf[16] = ((crc & 0xff00) >> 8);
memcpy(hbuf + 17, buf, len);
res = savefile(oname, hbuf, len + 17);
} else {
res = savefile(oname, buf, len);
}
return res;
}
// tap
void tapAddBlock(FILE* file, int len, char* buf, int type) {
int crc = type & 0xff;
for (int i = 0; i < len; i++) {
crc ^= buf[i];
}
fputc((len + 2) & 0xff, file);
fputc(((len + 2) >> 8) & 0xff, file);
fputc(type, file);
fwrite(buf, len, 1, file);
fputc(crc, file);
}
void tapCreate(char* path) {
savefile(path, NULL, 0);
}
void tapList(char* path) {
FILE* file = fopen(path, "rb");
if (!file) {
printf("Can't open image '%s'\n",path);
return;
}
int len;
tHead hd;
int blk = 0;
printf("Name\t\tType\tStart\tSize\n");
printf("------------------------------------\n");
while (1) {
len = fgetc(file);
len |= (fgetc(file) << 8);
if (feof(file)) break;
int type = fgetc(file);
if (type == 0x00) {
fread((char*)&hd, sizeof(tHead), 1, file);
printf("%.10s\t%s\t%i\t%i\n",hd.name, hd.type ? "Bytes" : "Prog", hd.asl | (hd.ash << 8), hd.dll | (hd.dlh << 8));
fgetc(file); // skip crc
} else {
fseek(file, len - 1, SEEK_CUR);
printf("blk-%.3i\t\t\t\t%i\n",blk,len - 2);
}
blk++;
}
fclose(file);
}
void tapPush(char* iname, char* fname) {
char buf[0x10000];
int flen = readfile(fname, buf, 0xfffd);
if (flen < 0) return;
FILE* file = fopen(iname, "ab");
if (!file) {
printf("Can't open '%s' for append\n", iname);
return;
}
if (!opt.headLess) {
tHead hd;
memset(hd.name, 0x20, 10);
xName nm = cutName(fname);
memcpy(hd.name, nm.name, 10);
hd.dll = flen & 0xff;
hd.dlh = (flen >> 8) & 0xff;
if (opt.isBasic) {
hd.type = 0x00;
hd.asl = opt.aStart & 0xff;
hd.ash = (opt.aStart >> 8) & 0xff;
hd.pll = hd.dll;
hd.plh = hd.dlh;
} else {
hd.type = 0x03;
hd.asl = 0;
hd.ash = 0;
hd.pll = 0;
hd.plh = 0x80;
}
tapAddBlock(file, sizeof(tHead), (char*)&hd, 0x00);
}
tapAddBlock(file, flen, buf, 0xff); // data
fclose(file);
}
void tapPop(char* img, char* fname, char* oname) {
printf("Not developed yet\n");
}
void tapRename(char* ipath, char* oname, char* nname) {
printf("Not developed yet\n");
}
// scl
void sclCreate(char* path) {
char data[] = {'S', 'I', 'N', 'C', 'L', 'A', 'I', 'R', 0x00, 0x55, 0x02, 0x00, 0x00}; // last 4 is CRC
savefile(path, data, 13);
}
void sclList(char* path) {
FILE* file = fopen(path, "rb");
if (!file) {
printf("Can't open image '%s'\n",path);
return;
}
trdFile hd;
printf("Name\t\tExt\tStart\tSize\tSLen\n---------------------------\n");
fseek(file, 8, SEEK_SET);
int cnt = fgetc(file);
unsigned short start, len;
while (cnt > 0) {
fread((char*)&hd, 14, 1, file);
start = hd.stl | (hd.sth << 8);
len = hd.lnl | (hd.lnh << 8);
printf("%.8s\t%c\t%i\t%i\t%i\n",hd.name, hd.ext, start, len, hd.slen);
cnt--;
}
fclose (file);
}
void sclPush(char* ipath, char* fpath) {
char buf[0x10000];
trdFile hd = prepareFile(fpath, buf);
if (hd.name[0] == 0) return;
FILE* file = fopen(ipath, "rb");
if (!file) {
printf("Can't open image '%s'\n", ipath);
return;
}
char bufH[9]; // head
fread(bufH, 9, 1, file);
int cnt = bufH[8];
int clen = cnt * 14;
char bufC[clen]; // catalog
fread(bufC, clen, 1, file);
int dsiz = 0;
for (int i = 0; i < cnt; i++) {
dsiz += (unsigned)bufC[14 * i + 13];
}
dsiz <<= 8;
char bufD[dsiz]; // data
fread(bufD, dsiz, 1, file);
fclose(file);
if ((unsigned)bufH[8] > 127) {
printf("Too much files in image\n");
return;
}
bufH[8]++;
// trdFile hd = makeDSC(fpath, ilen);
file = fopen(ipath, "wb");
if (!file) {
printf("Can't open image '%s' for writing\n",ipath);
return;
}
fwrite(bufH, 9, 1, file); // header
fwrite(bufC, clen, 1, file); // old catalog
fwrite((char*)&hd, 14, 1, file); // new DSC
fwrite(bufD, dsiz, 1, file); // old data
fwrite(buf, hd.slen << 8, 1, file); // new data
int crc = getSum(bufH, 9);
crc += getSum(bufC, clen);
crc += getSum((char*)&hd, 14);
crc += getSum(bufD, dsiz);
crc += getSum(buf, hd.slen << 8);
fputi(file, crc);
fclose(file);
}
void sclPop(char* ipath, char* fname, char* oname) {
FILE* file = fopen(ipath, "rb");
if (!file) {
printf("Can't open file '%s'\n",ipath);
return;
}
char nbuf[9];
char buf[0x10000];
makeName(fname, nbuf);
// printf("searching name '%.9s'\n",nbuf);
fseek(file, 8, SEEK_SET);
trdFile hd;
int cnt = fgetc(file);
int pos = 9 + cnt * 14; // data pos
int find = 0;
while ((cnt > 0) && (find == 0)) {
fread((char*)&hd, 14, 1, file);
if (memcmp(hd.name, nbuf, 9) == 0) {
fseek(file, pos, SEEK_SET);
fread(buf, hd.slen << 8, 1, file);
find = 1;
} else {
pos += (hd.slen << 8);
cnt--;
}
}
fclose(file);
if (!find) {
printf("File '%s' not found in image\n",fname);
} else {
saveoutput(oname, buf, hd);
}
}
int sclSeekFile(FILE* file, char* name, trdFile* hd) {
fseek(file, 8, SEEK_SET);
int cnt = fgetc(file);
int find = 0;
int pos = 9;
while ((cnt > 0) && !find) {
fread((char*)hd, 14, 1, file);
if (memcmp((char*)hd, name, 9) == 0) {
find = 1;
} else {
pos += 14;
cnt--;
}
}
return find ? pos : -1;
}
void sclRename(char* ipath, char* oname, char* nname) {
FILE* file = fopen(ipath, "r+");
if (!file) {
printf("Can't open file '%s'\n",ipath);
return;
}
char nbuf[9];
makeName(oname, nbuf);
trdFile hd;
int pos = sclSeekFile(file, nbuf, &hd);
if (pos < 0) {
printf("File '%s' not found in image\n",oname);
} else {
int oldSum = getSum(nbuf, 9);
makeName(nname, nbuf);
int dif = getSum(nbuf, 9) - oldSum; // diff to new CRC
fseek(file, pos, SEEK_SET); // read old CRC
fwrite(nbuf, 9, 1, file);
fseek(file, -4, SEEK_END);
int crc = fgeti(file);
crc += dif; // change it
fseek(file, -4, SEEK_END); // write back
fputi(file, crc);
}
fclose(file);
}
// trd
void trdCreate(char* path) {
char buf[0xa0000];
memset(buf, 0x00, 0xa0000);
buf[0x8e2] = 0x01;
buf[0x8e3] = 0x16;
buf[0x8e5] = 0xf0;
buf[0x8e6] = 0x09;
buf[0x8e7] = 0x10;
FILE *file = fopen(path, "wb");
if (!file) {
printf("Can't write to file '%s'\n",path);
} else {
fwrite(buf, 0xa0000, 1, file);
fclose(file);
}
}
void trdList(char* path) {
FILE* file = fopen(path, "rb");
if (!file) {
printf("Can't open image '%s'\n", path);
return;
}
printf("Name\t\tExt\tStart\tSize\tSLen\tSec\tTrk\n---------------------------\n");
trdFile hd;
int cnt = 0;
int start,len;
while (cnt < 128) {
fread((char*)&hd, 16, 1, file);
if (hd.name[0] == 0x00) break;
if (hd.name[0] != 0x01) {
start = hd.stl | (hd.sth << 8);
len = hd.lnl | (hd.lnh << 8);
printf("%.8s\t%c\t%i\t%i\t%i\t%i\t%i\n", hd.name, hd.ext, start, len, hd.slen, hd.sec, hd.trk);
}
}
fclose(file);
}
void trdPush(char* ipath, char* fpath) {
char buf[0x10000];
trdFile hd = prepareFile(fpath, buf);
if (hd.name[0] == 0) return;
char img[0xa0000];
if (readfile(ipath, img, 0xa0000) < 0) {
printf("Can't read image '%s'\n", ipath);
return;
}
// trdFile hd = makeDSC(fpath, ilen);
int files = img[0x8e4]; // test files count
if (files > 127) {
printf("Too many files in image\n");
return;
}
int freesec = (img[0x8e5] & 0xff) | ((img[0x8e6] & 0xff) << 8);
if (freesec < hd.slen) { // test free sectors
printf("No room for file\n");
return;
}
int lastsec = img[0x8e1] & 0xff;
int lasttrk = img[0x8e2] & 0xff;
hd.sec = lastsec;
hd.trk = lasttrk;
files++;
img[0x8e4] = files;
freesec -= hd.slen;
img[0x8e5] = freesec & 0xff;
img[0x8e6] = ((freesec & 0xff00) >> 8);
char* ptr = img;
while (*ptr) // find 1st free descriptor
ptr += 16;
memcpy(ptr, (char*)&hd, 16); // make new descriptor
int secnum = ((hd.trk << 4) + hd.sec); // free sector num
int fpos = (secnum << 8); // position in image
secnum += hd.slen;
img[0x8e1] = secnum & 0x0f;
img[0x8e2] = ((secnum & 0xfff0) >> 4);
memcpy(img + fpos, buf, hd.slen << 8); // copy file data
savefile(ipath, img, 0xa0000);
}
int trdSeekFile(FILE* file, char* name, trdFile* hd) {
int find = 0;
int cnt = 0;
while ((cnt < 128) && !find) {
fread((char*)hd, 16, 1, file);
if (hd->name[0] == 0x00) break;
if (memcmp((char*)hd, name, 9) == 0) {
find = 1;
}
cnt++;
}
return find ? (cnt - 1) << 4 : -1;
}
void trdPop(char* ipath, char* fname, char* oname) {
FILE* file = fopen(ipath, "rb");
if (!file) {
printf("Can't open image '%s'\n",ipath);
return;
}
char nbuf[9];
char buf[0x10000];
makeName(fname, nbuf);
trdFile hd;
if (trdSeekFile(file, nbuf, &hd) < 0) {
printf("Can't find file '%s' in image\n",fname);
} else {
fseek(file, ((hd.trk << 4) | (hd.sec & 0x0f)) << 8, SEEK_SET);
fread(buf, hd.slen << 8, 1, file);
saveoutput(oname, buf, hd);
}
fclose(file);
}
void trdRename(char* ipath, char* oname, char* nname) {
FILE* file = fopen(ipath, "r+");
if (!file) {
printf("Can't open image '%s'\n",ipath);
return;
}
char nbuf[9];
makeName(oname, nbuf);
trdFile hd;
int pos = trdSeekFile(file, nbuf, &hd);
if (pos < 0) {
printf("Can't find file '%s' in image\n",oname);
} else {
makeName(nname, nbuf);
fseek(file, pos, SEEK_SET);
fwrite(nbuf, 9, 1, file);
}
fclose(file);
}
// commands
void create(char* path) {
if (testExt(path, "tap")) {
tapCreate(path);
} else if (testExt(path, "scl")) {
sclCreate(path);
} else if (testExt(path, "trd")) {
trdCreate(path);
} else {
printf("Unknown type, use TAP SCL TRD please\n");
}
}
void list(char* path) {
int mode = getFileType(path);
switch (mode) {
case TYPE_TAP:
tapList(path);
break;
case TYPE_SCL:
sclList(path);
break;
case TYPE_TRD:
trdList(path);
break;
default:
printf("Unknown image type\n");
break;
}
}
void push(char* ipath, char* fpath) {
int type = getFileType(ipath);
switch(type) {
case TYPE_TAP:
tapPush(ipath, fpath);
break;
case TYPE_SCL:
sclPush(ipath, fpath);
break;
case TYPE_TRD:
trdPush(ipath, fpath);
break;
default:
printf("Unknown image type\n");
break;
}
}
void pop(char* ipath, char* fname, char* oname) {
if (!oname) {
if (opt.asHobeta) {
char fpath[1024];
memset(fpath, 0, 1024);
char* pDot = strrchr(fname,'.');
if (pDot) {
memcpy(fpath, fname, pDot - fname + 1);
strcat(fpath,"$");
strcat(fpath,pDot + 1);
oname = fpath;
} else {
oname = fname;
}
} else {
oname = fname;
}
}
int type = getFileType(ipath);
switch(type) {
case TYPE_TAP:
tapPop(ipath, fname, oname);
break;
case TYPE_SCL:
sclPop(ipath, fname, oname);
break;
case TYPE_TRD:
trdPop(ipath, fname, oname);
break;
default:
printf("Unknown image type\n");
break;
}
}
void renam(char* ipath, char* oname, char* nname) {
int type = getFileType(ipath);
switch(type) {
case TYPE_TAP:
tapRename(ipath, oname, nname);
break;
case TYPE_SCL:
sclRename(ipath, oname, nname);
break;
case TYPE_TRD:
trdRename(ipath, oname, nname);
break;
default:
printf("Unknown image type\n");
break;
}
}
void help() {
printf("::: Usage :::\n");
printf("mctrd [-h][-z][-b][-n][-a num] command name1 [name2] [name3]\n");
printf("::: Keys :::\n");
printf("%*s %s\n",-32,"-a | --autostart NUM","set autostart line number for basic file");
printf("%*s %s\n",-32,"-b | --basic","add file to archive as basic");
printf("%*s %s\n",-32,"-h | --help","show this page");
printf("%*s %s\n",-32,"-n | --no-head","push file to TAP without header");
printf("%*s %s\n",-32,"-z | --hobeta","pop/push file as hobeta (SCL, TRD only)");
printf("::: Commands :::\n");
printf("%*s %s\n",-32,"list <image>","show image catalog");
printf("%*s %s\n",-32,"new <image>","create new image (TAP, TRD, SCL: depends on extension)");
printf("%*s %s\n",-32,"add <file> <image>","put file into image");
printf("%*s %s\n",-32,"pop <file> <image> [<outname>]","extract file from image");
printf("%*s %s\n",-32,"rename <image> <oname> <nname>","rename file inside image. oname = old name, nname = new name");
}
int main(int ac,char* av[]) {
char* com = NULL;
char* fname1 = NULL;
char* fname2 = NULL;
char* fname3 = NULL;
opt.isBasic = 0;
opt.headLess = 0;
opt.asHobeta = 0;
opt.mono = 0;
opt.aStart = 0;
int i = 1;
char* parg;
while (i < ac) {
parg = av[i++];
if ((strcmp(parg,"-a") == 0) || (strcmp(parg,"--autostart") == 0)) {
if (i < ac) {
opt.aStart = atoi(av[i++]);
} else {
printf("Invalid argument count\n");
return 1;
}
} else if ((strcmp(parg,"-b") && strcmp(parg,"--basic")) == 0) {
opt.isBasic = 1;
} else if ((strcmp(parg,"-h") && strcmp(parg,"--help")) == 0) {
help();
return 0;
} else if ((strcmp(parg,"-n") && strcmp(parg,"--no-head")) == 0) {
opt.headLess = 1;
} else if ((strcmp(parg,"-z") && strcmp(parg,"--hobeta")) == 0) {
opt.asHobeta = 1;
} else if ((strcmp(parg,"-m") && strcmp(parg,"--mono")) == 0) {
opt.mono = 1;
} else {
if (!com) com = parg;
else if (!fname1) fname1 = parg;
else if (!fname2) fname2 = parg;
else if (!fname3) fname3 = parg;
}
}
if (!fname1) help();
else if (strcmp(com,"list") == 0) list(fname1);
else if (strcmp(com,"new") == 0) create(fname1);
else if (!fname2) help();
else if (strcmp(com,"pop") == 0) pop(fname2, fname1, fname3);
else if (strcmp(com,"add") == 0) push(fname2, fname1);
else if (!fname3) help();
else if (strcmp(com,"rename") == 0) renam(fname1, fname2, fname3);
else help();
return 0;
}