aboutsummaryrefslogtreecommitdiffstats
path: root/fileop.c
blob: 6b7d82350335236ae3207c3908560b61014dab6f (plain)
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
#include "abcprintd.h"
#include "hostfile.h"
#include "abcfile.h"
#include "trace.h"
#include "print.h"

const char *fileop_path = "abcdir";

#define BUF_SIZE 512

static inline struct fileop_file *getfile(struct abcprint *me, uint16_t ix)
{
    return &me->filemap[ix];
}

/* In the future this may be used to allocate filemap storage */
#define getfile_alloc(me,ix) getfile(me,ix)

static inline bool file_open(const struct fileop_file *ff)
{
    return ff && ff->hf;
}

static inline bool file_binary(const struct fileop_file *ff)
{
    return ff->binary;
}

static inline enum host_file_mode file_mode(const struct fileop_file *ff)
{
    return ff->hf->mode;
}

static void trace_data(const void *data, size_t len, const char *pfx)
{
    size_t i;
    const uint8_t *dp = data;

    if (!tracing(TRACE_PR))
        return;

    fprintf(tracef, "PR:  %-5s: ", pfx);

    for (i = 0; i < 16; i++) {
        if (i >= len)
            fprintf(tracef, "  ");
        else
            fprintf(tracef, "%02x", dp[i]);

        putc(i == 8 ? '-' : ' ', tracef);
    }

    fprintf(tracef, "%c  [", (len > 16) ? '+' : ' ');

    for (i = 0; i < 16; i++) {
        char c;

        c = (i >= len) ? ' ' : dp[i];
        if (c < 32 || c > 126)
            c = '.';

        putc(c, tracef);
    }
    putc(']', tracef);
    if (len > 16)
        fprintf(tracef, "+ (%lu bytes)", (unsigned long)len);
    putc('\n', tracef);
}

static void pr_send(struct abcprint *me, const void *buf, size_t len)
{
    const uint8_t *p = buf;

    trace_data(buf, len, "SEND");
    while (len) {
	size_t sent = me->sd.func(me->sd.pvt, p, len);
	if (sent == (size_t)-1)
	    sent = 0;
	len -= sent;
	p += sent;
    }
}

static void send_reply(struct abcprint *me, int status)
{
    unsigned char reply[4];

    reply[0] = 0xff;
    reply[1] = me->cmd[0];
    reply[2] = me->cmd[1];
    reply[3] = status;

    pr_send(me, reply, 4);
}

/* Returns the status code, use send_reply(me, do_close(ix)) if reply desired */
static int do_close(struct abcprint *me)
{
    if (file_open(me->ff)) {
        close_file(&me->ff->hf);
        return 0;
    } else {
        return 128 + 45;        /* "Fel logiskt filnummer" */
    }
}

static void do_closeall(struct abcprint *me, bool reply)
{
    size_t ix;

    for (ix = 0; ix <= 65535; ix++) {
	struct fileop_file *ff = getfile(me, ix);
	if (file_open(ff))
	    close_file(&ff->hf);
    }

    if (reply)
        send_reply(me, 0);
}

static void do_open(struct abcprint *me, char *name)
{
    int err;
    char path_buf[64];
    int openflags;
    enum host_file_mode mode;
    struct fileop_file *ff;
    struct host_file *hf;
    uint8_t cmd0 = me->cmd[0];

    if (!fileop_path) {
        send_reply(me, 128 + 42);   /* Skivan ej klar */
        return;
    }

    do_close(me);

    unmangle_filename(path_buf, name);

    if (!path_buf[0]) {
        /* Empty filename (readdir) */

        mode = ((cmd0 & 3) == 0) ? HF_DIRECTORY : HF_FAIL;
        openflags = 0;
    } else {
        /* Actual filename */

        mode = HF_BINARY;
        mode |= (cmd0 & 2) ? 0 : HF_RETRY;
        openflags = (cmd0 & 2) ? (O_RDWR | O_TRUNC | O_CREAT) : O_RDWR;
    }

    hf = open_host_file(mode, fileop_path, path_buf, openflags);
    ff = getfile_alloc(me, me->ix);
    ff->hf = hf;
    ff->binary = cmd0 & 1;

    switch (errno) {
#if 0                           /* Enable this? */
    case EACCES:
        err = 128 + 39;
        break;
    case EROFS:
        err = 128 + 43;
        break;
    case EIO:
    case ENOTDIR:
        err = 128 + 48;
        break;
#endif
    default:
	err = 128;		/* Hittar ej filen (default error) */
        break;
    }

    send_reply(me, hf ? 0 : err);
}

static void do_read_block(struct abcprint *me, uint16_t len)
{
    struct fileop_file *ff = me->ff;
    struct host_file *hf;
    int err;
    int dlen;

    if (!file_open(ff)) {
        send_reply(me, 128 + 45);
        return;
    }
    hf = ff->hf;

    if (file_mode(ff) == HF_DIRECTORY) {
        send_reply(me, 128 + 37);   /* Felaktigt recordformat */
        return;
    }

    clearerr(hf->f);
    dlen = fread(me->data + 2, 1, len, hf->f);
    if (dlen == 0) {
        if (ferror(hf->f)) {
            switch (errno) {
            case EBADF:
                err = 128 + 44; /* Logisk fil ej öppen */
                break;
            case EIO:
                err = 128 + 35; /* Checksummafel vid läsning */
                break;
            default:
                err = 128 + 48; /* Fel i biblioteket */
                break;
            }
        } else {
            /* EOF - definitely not the default return! */
	    err = 128 + 34;	/* Slut på filen (är det rätt?) */
        }
        send_reply(me, err);
        return;
    }

    send_reply(me, 0);

    me->data[0] = len;
    me->data[1] = len >> 8;
    pr_send(me, me->data, len + 2);
}

/* Common routine for all commands which need seek */
static int seeker(struct abcprint *me, uint64_t pos)
{
    struct fileop_file *ff = me->ff;
    struct host_file *hf;

    if (!file_open(ff))
        return 128 + 45;         /* Fel logiskt filnummer */

    hf = ff->hf;
    if (file_mode(ff) == HF_DIRECTORY)
        return 128 + 37;         /* Felaktigt recordformat */

    if (fseeko(hf->f, pos, SEEK_SET) == -1)
        return 128 + 38;         /* Recordnummer utanför filen */

    return 0;
}

static void do_seek(struct abcprint *me, uint64_t pos)
{
    send_reply(me, seeker(me, pos));
}

static void do_pread(struct abcprint *me, uint16_t blk)
{
    int err;

    err = seeker(me, me->blksize * (long)blk);
    if (err)
        send_reply(me, err);
    else
        do_read_block(me, me->blksize);
}

static void do_input(struct abcprint *me)
{
    struct fileop_file *ff = me->ff;
    struct host_file *hf;
    int err;
    char data1[255 + 2];        /* Max number of bytes to return + 2 */
    char *p, *q, c;
    int dlen;
    struct dirent *de;
    struct stat st;

    if (!file_open(ff)) {
        send_reply(me, 128 + 45);
        return;
    }
    hf = ff->hf;

    if (file_mode(ff) != HF_DIRECTORY) {
        clearerr(hf->f);
        if (!fgets((char *)me->data, sizeof me->data, hf->f)) {
            if (ferror(hf->f)) {
                switch (errno) {
                case EBADF:
                    err = 128 + 44;     /* Logisk fil ej öppen */
                    break;
                case EIO:
                    err = 128 + 35;     /* Checksummafel vid läsning */
                    break;
                default:
                    err = 128 + 48;     /* Fel i biblioteket */
                    break;
                }
            } else {
                /* EOF */
                err = 128; /* Slut på filen (default error) */
            }
        } else {
            /* Strip CR and change LF -> CR LF */
            if (!file_binary(ff)) {
                for (p = (char *)me->data, q = data1 + 2; (c = *p); p++) {
                    if (q == &data1[sizeof data1])
                        break;
                    switch (c) {
                    case '\r':
                        break;
                    case '\n':
                        *q++ = '\r';
                        /* fall through */
                    default:
                        *q++ = c;
                        break;
                    }
                }
                dlen = q - (data1 + 2);
            } else {
                dlen = strnlen(data1 + 2, sizeof data1 - 2);
            }
            err = 0;
        }
    } else if (hf->d) {
        while ((de = readdir(hf->d))) {
            if (de->d_name[0] != '.' &&
                (dlen = mangle_for_readdir(data1 + 2, de->d_name))) {
                if (!stat_file(fileop_path, de->d_name, &st) &&
                    S_ISREG(st.st_mode))
                    break;
            }
        }
        if (de) {
            unsigned long blocks, pad;
            blocks = (st.st_size + me->blksize - 1) / me->blksize;
            pad = me->blksize * blocks - st.st_size;
            /* pad = unused bytes in the last block */
            dlen += sprintf(data1 + 2 + dlen, ",%lu,%lu\r\n", blocks, pad);
            err = 0;
        } else {
            err = 128;		/* End of file (default error) */
        }
    } else {
        err = 128 + 44;
    }

    send_reply(me, err);
    if (!err) {
        data1[0] = dlen;
        data1[1] = dlen >> 8;
        pr_send(me, data1, dlen + 2);
    }
}

static void do_print(struct abcprint *me, uint16_t len, bool eolcvt)
{
    struct fileop_file *ff = me->ff;
    struct host_file *hf;
    int err;

#ifdef __WIN32__
    /* ABC sends CR LF as line endings, which matches Windows */
    eolcvt = false;
#endif

    if (!file_open(ff)) {
        err = 128 + 45;
	goto fail;
    }
    hf = ff->hf;

    if (file_mode(ff) == HF_DIRECTORY) {
        err = 128 + 39;         /* Directories are readonly */
	goto fail;
    }

    hf = ff->hf;

    clearerr(hf->f);
    if (len) {
	if (eolcvt && !file_binary(ff)) {
	    int i;
	    for (i = 0; i < len - 1; i++) {
		char c = me->data[i];
		if (c == '\r')
		    continue;       /* CR LF -> LF */
		else
		    putc(c, hf->f);
	    }
	    /* Last char is always verbatim */
	    fputc(me->data[len - 1], hf->f);
	} else {
	    fwrite(me->data, 1, len, hf->f);
	}
	fflush(hf->f);
    }

    if (!ferror(hf->f)) {
	err = 0;
    } else {
	switch (errno) {
	case EACCES:
	    err = 128 + 39; /* Filen skrivskyddad */
	    break;
	case ENOSPC:
	case EFBIG:
	    err = 128 + 41; /* Skivan full */
	    break;
	case EROFS:
	    err = 128 + 43; /* Skivan skrivskyddad */
	    break;
	case EBADF:
	    err = 128 + 44; /* Logisk fil ej öppen */
	    break;
	case EIO:
	    err = 128 + 36; /* Checksummafel vid skrivning */
	    break;
	default:
	    err = 128 + 48; /* Fel i biblioteket */
	    break;
	}
    }

fail:
    send_reply(me, err);
}

static void do_pwrite(struct abcprint *me, uint16_t blk)
{
    int err;

    err = seeker(me, me->blksize * (long)blk);
    if (err)
        send_reply(me, err);
    else
        do_print(me, me->blksize, false);
}

static void do_rename(struct abcprint *me, const char *files)
{
    char old_name[64], new_name[64];
    int err;

    unmangle_filename(old_name, files);
    unmangle_filename(new_name, files + 11);

    if (!rename(old_name, new_name)) {
        err = 0;
    } else {
        switch (errno) {
        case EACCES:
            err = 128 + 39;     /* Filen skrivskyddad */
            break;
        case EROFS:
            err = 128 + 43;     /* Skivan skrivskyddad */
            break;
        case ENOENT:
            err = 128 + 21;     /* Hittar ej filen */
            break;
        case ENOSPC:
            err = 128 + 41;     /* Skivan full */
            break;
        case EISDIR:
            err = 128 + 40;     /* Filen raderingsskyddad */
            break;
        case EIO:
            err = 128 + 36;     /* Checksummafel vid skrivning */
            break;
        default:
            err = 128 + 48;     /* Fel i biblioteket */
            break;
        }
    }

    send_reply(me, err);
}

static void do_delete(struct abcprint *me, const char *file)
{
    char path_buf[64];
    int err;

    unmangle_filename(path_buf, file);

    if (!remove(path_buf)) {
        err = 0;
    } else {
        switch (errno) {
        case EROFS:
            err = 128 + 43;     /* Skivan skrivskyddad */
            break;
        case ENOENT:
            err = 128 + 21;     /* Hittar ej filen */
            break;
        case ENOSPC:
            err = 128 + 41;     /* Skivan full */
            break;
        case EISDIR:
        case EPERM:
        case EACCES:
            err = 128 + 40;     /* Filen raderingsskyddad */
            break;
        case EIO:
            err = 128 + 36;     /* Checksummafel vid skrivning */
            break;
        default:
            err = 128 + 48;     /* Fel i biblioteket */
            break;
        }
    }

    send_reply(me, err);
}

/* Revert the state machine to its initial state (no command in progress) */
static void fileop_goto_init_state(struct abcprint *me)
{
    memset(&me->cmd, 0, sizeof me->cmd);
    memset(&me->argbuf, 0, sizeof me->argbuf);
    me->bytep = me->cmd;
    me->byte_count = 4;
    me->fstate = st_op;
}

/* Initialize or terminate the fileop session */
void fileop_reset(struct abcprint *me)
{
    me->blksize = 253;
    fileop_goto_init_state(me);
}

static inline uint64_t get_qword(const argbuf *v)
{
#ifdef WORDS_LITTLEENDIAN
    return v->q;
#else
    return v->b[0] +
        ((uint64_t) v->b[1] << 8) +
        ((uint64_t) v->b[2] << 16) +
        ((uint64_t) v->b[3] << 24) +
        ((uint64_t) v->b[4] << 32) +
        ((uint64_t) v->b[5] << 40) +
        ((uint64_t) v->b[6] << 48) +
        ((uint64_t) v->b[7] << 56);
#endif
}

bool file_op(struct abcprint *me, unsigned char c)
{
    uint64_t arg;

    *me->bytep++ = c;
    if (--me->byte_count)
        return true;            /* More to do... */

    /* Otherwise, we have a full deck of *something* */
    me->ix = (me->cmd[3] << 8) + me->cmd[2];
    me->ff = getfile(me, me->ix);
    arg = get_qword(&me->argbuf);
    me->bytep = me->argbuf.b;

    switch (me->fstate) {
    case st_op:
        switch (me->cmd[0]) {
        case 0xA0:             /* OPEN TEXT */
        case 0xA1:             /* OPEN BINARY */
        case 0xA2:             /* PREPARE TEXT */
        case 0xA3:             /* PREPARE BINARY */
            me->byte_count = 11;
            me->fstate = st_open;
            break;

        case 0xA4:             /* INPUT */
            do_input(me);
            break;

        case 0xA5:             /* READ BLOCK */
            me->byte_count = 2;
            me->fstate = st_read;
            break;

        case 0xA6:             /* PRINT */
        case 0xB9:             /* PUT */
            me->byte_count = 2;
            me->fstate = st_print;
            break;

        case 0xA7:             /* CLOSE */
            send_reply(me, do_close(me));
            break;

        case 0xA8:             /* CLOSEALL */
            do_closeall(me, true);
            break;

        case 0xA9:             /* INIT */
            me->blksize = 253;
            do_closeall(me, false);
            break;

        case 0xAE:             /* SET BLOCK SIZE */
        case 0xAF:             /* INITSZ */
            me->byte_count = 2;
            me->fstate = st_blksize;
            break;

        case 0xAA:             /* RENAME */
            me->byte_count = 22;
            me->fstate = st_rename;
            break;

        case 0xAB:             /* DELETE */
            me->byte_count = 11;
            me->fstate = st_delete;
            break;

        case 0xAC:             /* PREAD */
            me->byte_count = 2;
            me->fstate = st_pread;
            break;

        case 0xAD:             /* PWRITE */
            me->byte_count = 2;
            me->fstate = st_pwrite;
            break;

        case 0xB0:             /* SEEK0 == REWIND */
            do_seek(me, 0);
            break;

        case 0xB1:             /* SEEK1 */
        case 0xB2:             /* SEEK2 */
        case 0xB3:             /* SEEK3 */
        case 0xB4:             /* SEEK4 */
        case 0xB5:             /* SEEK5 */
        case 0xB6:             /* SEEK6 */
        case 0xB7:             /* SEEK7 */
        case 0xB8:             /* SEEK8 */
            me->byte_count = me->cmd[0] - 0xb0;
            me->fstate = st_seek;
            break;

        default:
            /* Unknown command */
            send_reply(me, 128 + 11);
            break;
        }
        if (tracing(TRACE_PR)) {
            static const char *const cmdnames[0x20] = {
                "OPEN A", "OPEN B", "PREP A", "PREP B",
                "INPUT", "GET", "PRINT", "CLOSE",
                "CALL", "CALLNR", "RENAME", "DELETE",
                "PREAD", "PWRITE", "BLKSIZ", "INITSZ",
                "REWIND", "SEEK1", "SEEK2", "SEEK3",
                "SEEK4", "SEEK5", "SEEK6", "SEEK7",
                "SEEK8", "PUT", NULL, NULL,
                NULL, NULL, NULL, NULL
            };
            int cnum = me->cmd[0] - 0xa0;
            const char *cmdname = (cnum >= 0x20) ? NULL : cmdnames[cnum];

            fprintf(tracef, "PR:  CMD  : %-6s %02x %02x %04x <need %u bytes>\n",
                    cmdname ? cmdname : "???", me->cmd[0], me->cmd[1],
		    me->ix, me->byte_count);
        }
        break;

    case st_open:
        trace_data(me->argbuf.b, 11, "OPEN");
        do_open(me, me->argbuf.c);
        break;

    case st_read:
        trace_data(me->argbuf.b, 2, "READ");
        do_read_block(me, arg);
        break;

    case st_print:
        trace_data(me->argbuf.b, 2, "WRTE");
        me->bytep = me->data;
        me->byte_count = arg;
        me->fstate = st_print2;
        break;

    case st_print2:
        trace_data(me->data, me->datalen, "DATA");
        do_print(me, me->datalen, me->cmd[0] == 0xA6);
        break;

    case st_pwrite:
        trace_data(me->argbuf.b, 2, "PWRT");
        me->bytep = me->data;
        me->byte_count = 253;
        me->fstate = st_pwrite2;
        break;

    case st_pwrite2:
        trace_data(me->data, me->datalen, "DATA");
        do_pwrite(me, arg);
        break;

    case st_pread:
        trace_data(me->argbuf.b, 2, "PRED");
        do_pread(me, arg);
        break;

    case st_seek:
        trace_data(me->argbuf.b, me->cmd[0] - 0xb0, "SEEK");
        do_seek(me, arg);
        break;

    case st_rename:
        trace_data(me->argbuf.b, 11, "REN1");
        trace_data(me->argbuf.b + 11, 11, "REN2");
        do_rename(me, me->argbuf.c);
        break;

    case st_delete:
        trace_data(me->argbuf.b, 11, "DEL ");
        do_delete(me, me->argbuf.c);
        break;

    case st_blksize:
        trace_data(me->argbuf.b, 2, "SIZE");
        me->blksize = arg;
        if (me->cmd[0] == 0xAF)
            do_closeall(me, false);
        break;
    }

    me->datalen = me->byte_count;

    if (me->byte_count) {
        return true;
    } else {
	fileop_goto_init_state(me);
        return false;
    }
}