-
Notifications
You must be signed in to change notification settings - Fork 0
/
QL_driver.c
1176 lines (979 loc) · 23.2 KB
/
QL_driver.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
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
/*
* (c) UQLX - see COPYRIGHT
*/
/*#include "QLtypes.h"*/
#include "QL68000.h"
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <dirent.h>
#include "QL.h"
#include "xcodes.h"
#include "driver.h"
#include "QDOS.h"
#include "QInstAddr.h"
#include "unix.h"
#include "uqlx_cfg.h"
#include "qx_proto.h"
#define min(_a_,_b_)(_a_<_b_ ? _a_ : _b_)
#define max(_a_,_b_)(_a_>_b_ ? _a_ : _b_)
#ifdef __EMX__
#define strncasecmp strncmp
#endif
#ifdef strchr
#undef strchr
#endif
#define strchr(_str_,_c_) my_strchr(_str_,_c_)
extern void rts(void);
void DrvOpen(void);
void DrvIO(void);
void DrvClose(void);
int qerrno;
int prt_init(int,void *);
int prt_open(int, void**);
int prt_test(int, char*);
void prt_close(int, void *);
void prt_io(int, void *);
int num_drivers=0;
/*#define TEST*/
/*#define IOTEST*/
open_arg prt_opt_vals[]={0,1};
open_arg prt_tra_vals[]={0,1};
open_arg prt_cmdoptions[]={0}; /*simpler to check for arg presence ;-) */
open_arg prt_cmd[]={0};
struct PARENTRY prt_pars[]={{parse_option,"f",prt_opt_vals},
{parse_option,"t",prt_tra_vals},
{parse_nseparator,"_",prt_cmdoptions},
{parse_nseparator,"!",prt_cmd},
{NULL,NULL,NULL}};
struct NAME_PARS prt_name={ "PRT",4,&prt_pars};
#define BDEV
#ifdef BDEV
extern int boot_init(int,void *);
extern int boot_open(int, void**);
extern int boot_test(int, char*);
extern void boot_close(int, void *);
extern void boot_io(int, void *);
struct PARENTRY boot_pars[]={{NULL,NULL,NULL}};
struct NAME_PARS boot_name={"BOOT",0,&boot_pars};
#endif
#ifdef NEWSERIAL
extern int ser_init(int,void *);
extern int ser_open(int, void**);
extern int ser_test(int, char*);
extern void ser_close(int, void *);
extern void ser_io(int, void *);
open_arg ser_unit[]={1};
open_arg ser_ovals1[]={0,1,2,3,4};
open_arg ser_ovals2[]={-1,-1,0};
open_arg ser_ovals3[]={-1,-1,0,1};
open_arg ser_sv0[]={0}; /* Dummy '_' separator*/
open_arg ser_sv[]={9600};
struct PARENTRY ser_pars[]={{parse_value,NULL,ser_unit},
{parse_option,"OEMS",ser_ovals1},
{parse_option,"HI",ser_ovals2},
{parse_option,"RZC",ser_ovals3},
{parse_separator,"_",ser_sv0}, /* dummy */
{parse_separator,"b",ser_sv},
{NULL,NULL,NULL}};
struct NAME_PARS ser_name={"SER",6,&ser_pars};
#endif
#ifdef NEWPTY
extern int pty_init(int, void *);
extern int pty_open(int, void**);
extern int pty_test(int, char*);
extern void pty_close(int, void *);
extern void pty_io(int, void *);
open_arg pty_ovals1[]={0,1,2};
open_arg pty_ovals2[]={-1,-1,0,1,3};
open_arg pty_val[]={(open_arg)""};
struct PARENTRY pty_pars[]={{parse_option,"IK",pty_ovals1},
{parse_option,"RZCT",pty_ovals2},
{parse_nseparator,"_",pty_val},
{NULL,NULL,NULL}};
struct NAME_PARS pty_name={"PTY",3,&pty_pars};
#endif
#ifdef POPEN_DEV
extern int popen_init(int);
extern int popen_open(int, void**);
extern int popen_test(int, char*);
extern void popen_close(int, void *);
extern void popen_io(int, void *);
open_arg popen_val[]={(open_arg)""};
struct PARENTRY popen_pars[]={{parse_nseparator,"_",popen_val},
{NULL,NULL,NULL}};
struct NAME_PARS popen_name={"popen",1,&popen_pars};
#endif
#include "QLfiles.h"
#include "QFilesPriv.h"
#include "QVFS.h"
#ifdef IPDEV
#include "QLip.h"
open_arg ip_host[]={(open_arg)""};
open_arg ip_port[]={(open_arg)""};
struct PARENTRY ip_pars[]={
{parse_mseparator,"_:",ip_host}, /* dummy */
{parse_mseparator,":",ip_port},
{NULL,NULL,NULL}};
struct NAME_PARS tcp_name={"*TCP",2,&ip_pars};
struct NAME_PARS udp_name={"*UDP",2,&ip_pars};
struct NAME_PARS uxs_name={"*UXS",2,&ip_pars};
struct NAME_PARS uxd_name={"*UXD",2,&ip_pars};
struct NAME_PARS sck_name={"*SCK_",0, NULL};
#endif
#ifdef TEST
int bg_init(int, void *);
int bg_open(int,void **);
int bg_test(int,char *);
void bg_close(int, void *);
void bg_io(int, void *);
open_arg bg_val[]={133};
open_arg bg_ovals[]={-1,'a','b','c','d','e','f','g','h','i','j','k','l'};
open_arg bg_sv1[]={11};
open_arg bg_sv2[]={12};
open_arg bg_sv3[]={13};
open_arg bg_sv4[]={(open_arg)"string1"};
open_arg bg_sv5[]={(open_arg)"string2"};
struct PARENTRY bg_pars[]={{parse_value,NULL,bg_val},
{parse_option,"abcdefghijkl",bg_ovals},
{parse_separator,"_",bg_sv1},
{parse_separator,"/",bg_sv2},
{parse_separator,"x",bg_sv3},
{parse_nseparator,"--",bg_sv4},
{parse_nseparator,",",bg_sv5},
{NULL,NULL,NULL}};
struct NAME_PARS bg_name={"BG",4,&bg_pars};
#endif
#ifdef XSCREEN
#if 0
extern int scr_init(int, void *);
extern int scr_open(int, void**);
extern int scr_test(int, char*);
extern int con_test(int, char*);
extern void scr_close(int, void *);
extern void scr_io(int, void *);
#endif
open_arg scr_sv1[]={448};
open_arg scr_sv2[]={180};
open_arg scr_sv3[]={32};
open_arg scr_sv4[]={16};
open_arg scr_sv5[]={-1};
struct PARENTRY scr_pars[]={{parse_separator,"_",scr_sv1},
{parse_separator,"x",scr_sv2},
{parse_separator,"a",scr_sv3},
{parse_separator,"x",scr_sv4},
{parse_separator,"_",scr_sv5},
{NULL,NULL,NULL}};
struct NAME_PARS scr_name={"SCR",6,&scr_pars};
struct NAME_PARS con_name={"CON",6,&scr_pars};
#endif
#ifdef SOUND
#include "QL_sound.h"
extern int sound_init(int, void *);
extern int sound_open(int, void**);
extern int sound_test(int, char*);
extern void sound_close(int, void *);
extern void sound_io(int, void *);
open_arg sound_oread[]= {-1,'r'};
open_arg sound_sm[]= {-1,'m','s'};
open_arg sound_x[]= {0,'x'};
open_arg sound_subdev[]={-1,'1','2','3','4','5','6','7','8','9','s'};
open_arg sound_sv2[]={20001};
struct PARENTRY sound_pars[]={{parse_option,"r",sound_oread},
{parse_option,"ms",sound_sm},
{parse_option,"x",sound_x},
{parse_option,"123456789",sound_subdev},
{parse_separator,"_",sound_sv2},
{NULL,NULL,NULL}};
struct NAME_PARS sound_name={"SOUND",5,&sound_pars};
#endif
struct NAME_PARS qvf_name={"VFS_",0,NULL};
struct DRV Drivers[]={
{0,prt_init, prt_test, prt_open, prt_close, prt_io, &prt_name, 0},
#ifdef NEWSERIAL
{0,ser_init, ser_test, ser_open, ser_close, ser_io, &ser_name, 0},
#endif
#ifdef NEWPTY
{0,pty_init, pty_test, pty_open, pty_close, ser_io, &pty_name, 0},
#endif
#ifdef BDEV
{0,boot_init, boot_test, boot_open, boot_close, boot_io, &boot_name, 0},
#endif
#ifdef TEST
{0,bg_init,bg_test,bg_open,bg_close,bg_io,&bg_name,0},
#endif
#ifdef IPDEV
{0, ip_init, ip_test, ip_open, ip_close, ip_io, &tcp_name, 0},
{0,ip_init,ip_test,ip_open,ip_close,ip_io,&udp_name,0},
{0,ip_init,ip_test,ip_open,ip_close,ip_io,&uxs_name,0},
{0,ip_init,ip_test,ip_open,ip_close,ip_io,&uxd_name,0},
{0,ip_init,ip_test,ip_open,ip_close,ip_io,&sck_name,0},
#endif
#ifdef POPEN_DEV
{0,popen_init,popen_test,popen_open,popen_close,popen_io,&popen_name,0},
#endif
#if 0 /* XSCREEN */
{0,scr_init, scr_test, scr_open, scr_close, scr_io, &scr_name, 0x100},
{0,scr_init, con_test, scr_open, scr_close, scr_io, &con_name, 0x100},
#endif
#ifdef SOUND
{0,sound_init, sound_test, sound_open, sound_close, sound_io, &sound_name, 0x100},
#endif
/* QVFS should always be LAST in the list */
#ifdef QVFS
{0,qvf_init, qvf_test, qvf_open, qvf_close, qvf_io, &qvf_name, 0},
#endif
{0,NULL,NULL,NULL,NULL,NULL,NULL}};
#if 0
/* emulator opcodes */
#define DEV_IO_INSTR 0xaaaa
#define DEV_OPEN_INSTR 0xaaab
#define DEV_CLOSE_INSTR 0xaaac
#endif
w32 DEV_IO_ADDR, DEV_CLOSE_ADDR;
struct DRV *dget_drv()
{
struct DRV *p;
p=Drivers;
while(p->open!=NULL)
{
if (p->ref==aReg[3]+0x18)
return p;
p++;
}
return 0;
/*if (found=(*(p->open_test))(p-Drivers,name)) break;*/
}
static void InitDevDriver(struct DRV *driver, int indx)
{
w32 savedRegs[4];
w32 *p;
char *name=(driver->namep)->name;
BlockMoveData(aReg,savedRegs,4*sizeof(w32));
reg[1]=40+strlen(name);
if((strlen(name)&1)!=0) reg[1]++;
if (driver->slot != 0)
{
if(driver->slot <reg[1])
{
printf("requested driver size for driver %s too small: %d\n",*name,driver->slot);
goto ddier;
}
reg[1]=driver->slot;
}
reg[2]=0;
QLtrap(1,0x18,200000l); /* allocate memory for the driver linkage block */
if((*reg)==0)
{
driver->ref=aReg[0];
p=(w32*)(aReg[0]+(Ptr)theROM+4);
WL(p,DEV_IO_ADDR); /* io */
WL(p+1,(w32) ((Ptr)(p+3)-(Ptr)theROM)); /* open */
WL(p+2,DEV_CLOSE_ADDR); /* close */
WW(p+3,DEVO_CMD_CODE);
strcpy((Ptr)(p+6)+4,name); /* name for QPAC2 etc */
WW((Ptr)(p+3+3)+2,strlen(name));
WL((Ptr)(p+3)+2,0x264f4eba); /* so much code is needed to fool QPAC2 ...*/
WL((Ptr)(p+4)+2,0x2c566046);
WL((Ptr)(p+5)+2,0x6044604a);
if ((*(driver->init))(indx,p-1) <0)
goto ddier;
QLtrap(1,0x20,20000l); /* link directory device driver in IOSS */
}
ddier:
BlockMoveData(savedRegs,aReg,4*sizeof(w32));
}
void InitDrivers()
{
struct DRV *p=Drivers;
DEV_IO_ADDR=0x14020;
DEV_CLOSE_ADDR=0x14022;
/* DEV_OPEN_ADDR=0x14024;*/
WW(((uw16*)((Ptr)theROM+DEV_IO_ADDR)),DEVIO_CMD_CODE);
WW(((uw16*)((Ptr)theROM+DEV_CLOSE_ADDR)),DEVC_CMD_CODE);
/*WW(((uw16*)((Ptr)theROM+DEV_OPEN_ADDR)),DEV_OPEN_INSTR);*/
table[DEVO_CMD_CODE]=DrvOpen;
table[DEVIO_CMD_CODE]=DrvIO;
table[DEVC_CMD_CODE]=DrvClose;
while (p->open!=NULL)
{
InitDevDriver(p++,p-Drivers);
num_drivers++;
}
}
char *a0addr(Cond check)
{
char *f;
if(*aReg<131072 || *aReg>RTOP-130)
{
return nil;
}
f=(char *)((Ptr)theROM+((*aReg)&ADDR_MASK_E));
if(!check) return f;
if(DGET_ID(f)==DRV_ID) return f;
return nil;
}
void DrvIO(void)
{
char *f;
struct DRV *driver;
int ix;
if((long)((Ptr)gPC-(Ptr)theROM)-2 != DEV_IO_ADDR)
{
exception=4;
extraFlag=true;
nInst2=nInst;
nInst=0;
return;
}
f=a0addr(false);
if(f==nil)
{
*reg=QERR_NO; /* overflow */
rts();
return;
}
#if 0
if (DGET_ID(f)!=DRV_ID)
{
reg[0]= QERR_NO;
rts();
return;
}
#endif
driver=dget_drv(); /*&Drivers[DGET_DRV(f)];*/
ix=driver-Drivers;
if (driver==0)
{
printf("possible driver problem ??\n");
return;
}
(*(driver->io))(ix,DGET_PRIV(f));
rts();
}
void DrvOpen(void)
{
char * f=nil;
char *name;
struct DRV *p, *drv=Drivers;
void *priv;
int err,found=0;
#if 0 /* need a different test here */
if((long)((Ptr)gPC-(Ptr)theROM)-2 != 0x14002l)
{
exception=4;
extraFlag=true;
nInst2=nInst;
nInst=0;
return;
}
#endif
name=(char *)((Ptr)theROM+((*aReg)&ADDR_MASK_E));
//printf("DrvOpen: %s\n",name+2);
#if 0
if(*aReg>=RTOP || ((*aReg+RW(name))>=RTOP))
{
*reg=-18; /* overflow */
goto end;
}
#endif
/* get device */
p=dget_drv();
found=(*(p->open_test))(p-Drivers,name);
if (!found)
{
reg[0]=QERR_NF;
goto end;
}
if (found==-2)
{
reg[0]=qerrno;
goto end;
}
if (found==-1)
{
reg[0]=QERR_BN;
goto end;
}
err=(*(p->open))(p-Drivers,&priv);
if (err==0)
{
reg[1]=DRV_SIZE;
reg[2]=0;
QLvector(0xc0,20000l);
if ((uw16)reg[0]) goto end;
f=a0addr(false);
if (f)
{
DSET_ID(f,DRV_ID);
DSET_PRIV(f,priv);
}
}
if (err>=0)
reg[0]=0;
if (err<0)
reg[0]=err;
end:
rts();
}
void DrvClose(void)
{
char *f;
struct DRV *driver;
int ix;
w32 saved_regs[16];
if((long)((Ptr)gPC-(Ptr)theROM)-2 != DEV_CLOSE_ADDR)
{
exception=4;
extraFlag=true;
nInst2=nInst;
nInst=0;
return;
}
//save_regs(saved_regs);
f=a0addr(false);
if(f==nil)
{
*reg=QERR_NO; /* overflow */
rts();
return;
}
#if 0
if (DGET_ID(f)!=DRV_ID)
{
reg[0]= QERR_NO;
rts();
return;
}
#endif
driver=dget_drv(); /*&Drivers[DGET_DRV(f)];*/
ix=driver-Drivers;
if (driver==0)
{
printf("possible driver problem ??\n");
return;
}
(*(driver->close))(ix,DGET_PRIV(f));
QLvector(0xc2,20000l);
//restore_regs(saved_regs);
rts();
}
/* *********** generic helper routines *********** */
char *strchr_noalpha(char *str, char c)
{
char *res=(char*)0;
do
{
if (*str==c)
{
res=str;
break;
}
}while(*str++);
return res;
}
char *strchr_alpha(char *str, char c)
{
char *res=(char*)0;
c=c&0xdf;
do
{
if (((*str)&0xdf)==c)
{
res=str;
break;
}
}while(*str++);
return res;
}
char *my_strchr(char *str, char c)
{
if (isalpha(c))
return strchr_alpha(str, c);
else return strchr_noalpha(str,c);
}
static char rest_name[1025];
static char *ppname;
int parse_separator(char **name,int nlen,char *opts,open_arg *vals,open_arg *res)
{
#ifdef TEST
printf("parse_separator: \'%c\' : %s\n",*opts,*name);
#endif
if (**name!=*opts)
{
res->i=vals->i;
#ifdef TEST
printf("... use default value %d\n",res->i);
#endif
return 0;
}
(*name)++;
#ifdef TEST
printf("...found, fetch value\n");
#endif
return parse_value(name,nlen-1,NULL,vals,res);
}
int parse_nseparator(char **name,int nnlen,char *opts,open_arg *vals,open_arg *res)
{
char *nend;
int nlen;
#ifdef TEST
printf("parse name: %s rest name %s\n",opts,*name);
#endif
if (**name!=*opts)
{
noval:
res->s=vals->s;
#ifdef TEST
printf("... use default value %s\n",res->s);
#endif
return 0;
}
(*name)++;
nend=strchr(*name,opts[1]); /**/
if (!nend) nend=strchr(*name,0);
if (!nend || nend==*name) goto noval;
nlen=nend-*name;
memcpy(ppname,*name,nlen);
ppname[nlen]=0;
res->s=ppname;
ppname=&ppname[nlen+1];
*name=nend;
if (*name<strchr(*name,0))
(*name)++;
return 1;
}
#ifdef IPDEV
int parse_mseparator(char **name,int nnlen,char *opts,open_arg *vals,open_arg *res)
{
char *nend;
int nlen;
if (**name!=*opts)
{
noval:
res->s=vals->s;
return 0;
}
(*name)++;
nend=strchr(*name,opts[1]); /**/
if (!nend) nend=strchr(*name,0);
if (!nend || nend==*name) goto noval;
nlen=nend-*name;
memcpy(ppname,*name,nlen);
ppname[nlen]=0;
res->s=ppname;
ppname=&ppname[nlen+1];
*name=nend;
return 1;
}
#endif
int parse_value(char **name,int nlen,char *opts,open_arg *vals,open_arg *res)
{
char *p=*name;
int r;
#ifdef TEST
printf("parse_value: %s\n",*name);
#endif
r=strtol(*name,name,10);
if (*name==p)
{
res->i=vals->i;
#ifdef TEST
printf("... take default value %d\n",res->i);
#endif
return 0;
}
else {
res->i=r;
/**name=p;*/
#ifdef TEST
printf("... return %d\n",res->i);
#endif
return 1;
}
}
int parse_option(char **name,int nlen,char *opts,open_arg *vals,open_arg *res)
{
char *p;
#ifdef TEST
printf("parse_option: opt_string %s name %s\n",opts,*name);
#endif
if (**name)
p=strchr(opts,**name);
else p=NULL;
if (!p)
{
res->i=vals->i;
return 0;
}
(*name)++;
res->i=vals[p-opts+1].i;
#ifdef TEST
printf("... return optnr %d, value %d\n",p-opts+1,res->i);
#endif
return 1;
}
/* returns -1 bad name, 0 not found, >0 success*/
int decode_name(char *name, struct NAME_PARS *ndescr, open_arg *parblk)
{
int res;
open_arg rval;
struct PARENTRY *pars;
char *nend;
int j;
int i=ndescr->pcount;
ppname=rest_name;
#ifdef TEST
printf("decode_name: dev_name %s file name %s\n",name+2,ndescr->name);
#endif
if ( RW(name)<strlen(ndescr->name) || strncasecmp(name+2,ndescr->name,strlen(ndescr->name)) )
return 0;
nend=name;
if (RW(name)>1024) return -1;
name=rest_name;
j=RW(nend)-strlen(ndescr->name);
strncpy(name,nend+2+strlen(ndescr->name),j);
name[j]=0;
nend=&name[j];
pars=ndescr->pars;
while(i-- /*&& name<nend*/ && pars->func)
{
res=(*(pars->func))(&name,nend-name,pars->opt,pars->values,&rval);
switch (res)
{
case -1 : return -1;
case 0 :
case 1 : pars++; *parblk++=rval;
break;
default:
#ifdef TEST
printf("decode_name problem\n");
#endif
return -1;
}
}
if (name != nend) return -1;
else return 1;
}
static char buf[1024];
int ioskip(int (*io_read)(void *, void *,int), void *priv,int len)
{
int res=0,ss=0;
while(len>0)
{
res=(*io_read)(priv,buf,min(len,1024));
len-=1024;
if (res>0) ss+=res;
else break;
}
if (res<0) return -ss;
else return ss;
}
void ioread(int(*io_read)(), void *priv, uw32 addr, int *count, int lf)
{
int cnt,ocnt,startpos;
int c, fn, err,sz,e;
char *p;
Ptr i=0;
uw32 to,from;
cnt=*count;
from=addr;
to=from+cnt;
if (from<131072)
{
err=ioskip(io_read,priv,131072-from);
if (err<0)
{
cnt=-err;
e=QERR_NC;
goto errexit;
}
else from +=err;
}
if (to>=RTOP) to=RTOP;
ocnt=cnt=to-from;
e=0;
if (cnt<0) cnt=0;
if(cnt>0)
{
if (lf)
{
for(i=0,fn=cnt,p=(Ptr)theROM+from;fn>0;)
{
e=err=(*io_read)(priv,p,1);
if (err<0)
{
cnt=(long)((Ptr)p-(Ptr)theROM)-from;
goto errexit;
}
if (err==0 && *(p-1)!=10)
{
cnt=(long)((Ptr)p-(Ptr)theROM)-from;
e=QERR_EF; /* QERR_NC ???*/
goto errexit;
}
p+=e;
fn-=e;
if (*(p-1)==10) {i=p; break;}
}
if (i)
{cnt=(long)((Ptr)i-(Ptr)theROM)-from; e=0;}
else {cnt=ocnt; e=QERR_BF;}
}
else /* non LF part here */
{
e=err=(*io_read)(priv,(Ptr)theROM+from,cnt);
/*printf("ser_read resutl: %d\n",e);*/
if (e<=0)
{
if (e==0 && cnt>0) e=QERR_EF;
cnt=0;
goto errexit;
}
else
if (e<cnt) e=QERR_NC;
else e=0;
cnt=err;
}
}
errexit:
*count=cnt;
ChangedMemory(from,from+cnt);
reg[0]=e;
return;
}
/* io_handle, similar to SERIO vector */
void io_handle(int (*io_read)(), int (*io_write)(), int (*io_pend)(),
void *priv)
{
void *addr;
int err,res;
char c;
int count,rc_count;
w32 qaddr;
int op=(w8)reg[0];
reg[0]=0;
#ifdef IOTEST
printf("call io_handle \t\td0=%d\td1=%x\td2=%x\td3=%x\ta1=%x\n",op,reg[1],reg[2],reg[3],aReg[1]);
#endif
switch (op)
{
case 0:
*reg=(*io_pend)(priv);
break;
case 1:
res=(*io_read)(priv,&c,1);
if (res==1)
*((char*)reg+4+RBO)=c;
else *reg=res ? res: QERR_EOF;
break;
case 2: /* read line */
count=max(0,(uw16)reg[2]-(uw16)reg[1]);
rc_count=(uw16)reg[1];
qaddr=aReg[1];
prepChangeMem(aReg[1],aReg[1]+rc_count);
ioread(io_read,priv,qaddr,&count,true);
//(uw16)reg[1]=count+rc_count;
SETREG16(reg[1], count+rc_count);
aReg[1]=qaddr+count;
break;
case 3: /* fetch string */
qaddr=aReg[1];
count=max(0,(uw16)reg[2]-(uw16)reg[1]);
rc_count=(uw16)reg[1];
prepChangeMem(aReg[1],aReg[1]+rc_count);
ioread(io_read,priv,qaddr,&count,false);
reg[1]=count+rc_count;
aReg[1]=qaddr+count;
break;
case 5:
res=(*io_write)(priv,(Ptr)reg+4+RBO,1);
if (res<0) *reg=res;
break;
case 7: /* send string */
count=(uw16)reg[2];
res=(*io_write)(priv,(Ptr)theROM+aReg[1],count);
if (res<0)
{
count=0;
*reg=res;
} else count=res;
reg[1]=count;
aReg[1]+=count;
break;
case 0x48: /* read file into memory */
qaddr=aReg[1];
count=reg[2];
rc_count=reg[1];
prepChangeMem(aReg[1],aReg[1]+rc_count);
ioread(io_read,priv,qaddr,&count,false);
aReg[1]=qaddr+count;
break;
case 0x49:
count=reg[2];
res=(*io_write)(priv,(Ptr)theROM+aReg[1],count);
if (res<0)
{
count=0;
*reg=res;
} else count=res;
aReg[1]+=count;
break;
default:
*reg=QERR_BP;
break;
}
#ifdef IOTEST
printf("ret from io_handle \td0=%d\td1=%x\t\ta1=%x\n",reg[0],reg[1],aReg[1]);
#endif