-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.c
1577 lines (1271 loc) · 36 KB
/
server.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
/*
* Copyright (c) 1993-1996 Julian Highfield. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Julian Highfield.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* server.c
*
* Handle message buffers to and from server.
* Handle messages to server.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#include <io.h>
#include <fcntl.h>
#endif
#ifdef __MWERKS__
#include <SIOUX.h>
#include "mac_input.h"
#else
#ifdef CURTERM
#include "curterm.h"
#else
#ifdef SUN
#include <termio.h>
#else
#include <sys/termios.h>
#include <sys/ioctl.h>
#define TCGETS TIOCGETA
#define TCSETS TIOCSETA
#define termio termios
#endif
#endif
#include <sys/types.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#endif
#include <time.h>
#include "t4debug.h"
#include "processor.h"
#include "server.h"
/* Signal handler. */
void handler (int);
#undef TRUE
#undef FALSE
#define TRUE 0x0001
#define FALSE 0x0000
int length;
int boottemp;
int bootlen;
int loop1;
int loop2;
int FromServerLen = 0;
int ToServerLen = 0;
u_char FromServerBuffer[(16*1024)+5];
u_char ToServerBuffer[514];
void dump_message (char *msg, u_char *buffer, int len);
void notimpl_packet (void);
void error_packet (void);
int DumpMessage;
#define MAX_FD 256
typedef struct _FileDesc_ {
FILE *fd;
int type;
} FileDesc;
FileDesc Files[MAX_FD];
char *MessageTags[129];
extern int32_t quit;
extern int32_t quitstatus;
extern int copy;
extern FILE *CopyIn;
extern int emudebug;
extern int msgdebug;
extern char *dbgtrigger;
extern void set_debug (void);
extern int usetvs;
extern FILE *InpFile, *OutFile;
extern int profiling;
extern uint64_t profile[10];
extern struct termios t_getk;
extern struct termios t_poll;
extern enum {INIT, GETK, POLL} t_state;
#ifdef NDEBUG
#define word(a) word_int(a)
#endif
static
void init_server (void)
{
int i;
char msg[16];
for (i = 0; i < MAX_FD; i++)
{
Files[i].fd = (FILE *)NULL;
Files[i].type = -1;
}
Files[0].fd = stdin;
Files[0].type = SP_TEXT;
Files[1].fd = stdout;
Files[1].type = SP_TEXT;
Files[2].fd = stderr;
Files[2].type = SP_TEXT;
DumpMessage = FALSE;
MessageTags[ SP_OPEN] = "SP_OPEN";
MessageTags[ SP_CLOSE] = "SP_CLOSE";
MessageTags[ SP_READ] = "SP_READ";
MessageTags[ SP_WRITE] = "SP_WRITE";
MessageTags[ SP_GETS] = "SP_GETS";
MessageTags[ SP_PUTS] = "SP_PUTS";
MessageTags[ SP_FLUSH] = "SP_FLUSH";
MessageTags[ SP_SEEK] = "SP_PEEK";
MessageTags[ SP_TELL] = "SP_TELL";
MessageTags[ SP_EOF] = "SP_EOF";
MessageTags[ SP_FERROR] = "SP_FERROR";
MessageTags[ SP_REMOVE] = "SP_REMOVE";
MessageTags[ SP_RENAME] = "SP_RENAME";
MessageTags[ SP_GETKEY] = "SP_GETKEY";
MessageTags[ SP_POLLKEY] = "SP_POLLKEY";
MessageTags[ SP_GETENV] = "SP_GETENV";
MessageTags[ SP_TIME] = "SP_TIME";
MessageTags[ SP_SYSTEM] = "SP_SYSTEM";
MessageTags[ SP_EXIT] = "SP_EXIT";
MessageTags[ SP_COMMANDLINE] = "SP_COMMANDLINE";
MessageTags[ SP_CORE] = "SP_CORE";
MessageTags[ SP_VERSION] = "SP_VERSION";
MessageTags[ SP_DOS] = "SP_DOS";
MessageTags[ SP_HELIOS] = "SP_HELIOS";
MessageTags[ SP_VMS] = "SP_VMS";
MessageTags[ SP_SUNOS] = "SP_SUNOS";
MessageTags[ SP_TDS] = "SP_TDS";
MessageTags[ SP_OTHER] = "SP_OTHER";
for (i = 0; i < 129; i++)
{
if (MessageTags[i] == NULL)
{
sprintf (msg, "MSG%X", i);
MessageTags[i] = strdup (msg);
}
}
}
static
unsigned int FromFile (FILE *fd)
{
unsigned int i;
i = 0;
while (Files[i].fd && i < MAX_FD)
i++;
if (MAX_FD == i)
{
printf ("-E-EMUSRV: Help - files[] table overflow!\n");
handler(-1);
}
Files[i].fd = fd;
return i;
}
static
FILE* ToFile (unsigned int i)
{
if (MAX_FD <= i || (FILE *)NULL == Files[i].fd)
{
printf ("-E-EMUSRV: Help - invalid file descriptor!\n");
handler(-1);
}
return Files[i].fd;
}
#ifdef EMUDEBUG
static
char *msgtag (u_char tag)
{
static char msg[16];
if (tag < 129)
return MessageTags[tag];
sprintf (msg, "MSG%d", tag);
return msg;
}
#endif
/* XXX support ALT construction on Link0 */
int server (void)
{
static int do_init = 1;
int activity;
uint32_t LinkWdesc;
int data;
/* M.Bruestle 15.2.2012
*
* No Wdesc register in Link hardware, use channel control word directly.
*
*/
if (do_init)
{
init_server();
do_init = 0;
}
PROFILE(profile[PRO_ISERVER]++);
activity = 0;
if (copy==TRUE)
{
/* Still copying boot file to link. */
boottemp = (16*1024) - FromServerLen;
EMUDBG2 ("-I-EMUSRV: BootTemp = %d.", boottemp);
bootlen = fread ((char *) &(FromServerBuffer[FromServerLen]), 1, boottemp, CopyIn);
FromServerLen = FromServerLen + bootlen;
EMUDBG2 (" Loaded %d bytes.\n", bootlen);
if ((bootlen < boottemp))
{
/* Met end of file. */
copy = FALSE;
}
activity++;
}
LinkWdesc = word (Link0Out);
#ifdef EMUDEBUG
if (msgdebug || emudebug)
{
if (ToServerLen + FromServerLen)
printf ("-I-EMUSRV: To server buffer %d; From server buffer %d.\n", ToServerLen, FromServerLen);
if (NotProcess_p != LinkWdesc)
printf ("-I-EMUSRV: linkWdesc = #%08X Link0OutLength = #%x.\n", LinkWdesc, Link0OutLength);
}
#endif
if ((!usetvs && ((LinkWdesc != NotProcess_p) && Link0OutLength)) ||
( usetvs && (Link0InLength == 0)))
{
/* No messages leaving server, so server can handle the next incoming message. */
/* Check Link0Out for a valid process to see if there is a message. */
/* Check the length register, because the channel control word may be written. */
/* Example: memory sizing in Minix demo. */
if ((LinkWdesc != NotProcess_p) && Link0OutLength)
{
activity++;
if (usetvs)
{
MSGDBG2 ("-I-EMUSRV: TVS output. Link0OutLength = #%X.\n", Link0OutLength);
ToServerLen = 0;
for (loop1=0; loop1<Link0OutLength; loop1++)
{
data = byte_int (Link0OutSource);
putc (data, OutFile);
Link0OutSource++;
ToServerBuffer[ToServerLen++] = data;
}
#ifdef EMUDEBUG
if (msgdebug)
{
DumpMessage = TRUE;
dump_message ("TVS Output.", ToServerBuffer, ToServerLen);
}
#endif
ToServerLen = 0;
}
else
{
/* Move message to ToServerBuffer. */
for (loop1=0; loop1<Link0OutLength; loop1++)
{
ToServerBuffer[ToServerLen] = byte_int (Link0OutSource);
ToServerLen++;
Link0OutSource++;
if (ToServerLen == 514)
{
printf ("-E-EMUSRV: Help - overflowing ToServerBuffer!\n");
handler (-1);
}
}
}
MSGDBG2 ("-I-EMUSRV: Satisfied comms request. Rescheduling process #%8X.\n", LinkWdesc);
/* Reset Link0Out. */
reset_channel (Link0Out);
/* Reschedule outputting process. */
schedule (LinkWdesc);
/* Check if incoming message is all here yet. */
if (!usetvs && (ToServerLen>=2))
{
length = ToServerBuffer[0] + (256 * ToServerBuffer[1]);
if (length<6)
{
printf ("-W-EMUSRV: Bad message packet to server - too short! (%d)\n", length);
}
else if (length>510)
{
printf ("-W-EMUSRV: Bad message packet to server - too long! (%d)\n", length);
}
else if ((length&0x01)!=0)
{
printf ("-W-EMUSRV: Bad message packet to server - odd length! (%d)\n", length);
}
else if ((length+2)==ToServerLen)
{
/* Full message has arrived. Handle it. */
message();
/* XXX Delete handled message. */
for (loop1=0; loop1<(length+2); loop1++)
{
ToServerBuffer[loop1] = ToServerBuffer[loop1+length+2];
}
ToServerLen = ToServerLen - (length + 2);
}
}
}
}
LinkWdesc = word (Link0In);
#ifdef EMUDEBUG
if (msgdebug || emudebug)
{
if (NotProcess_p != LinkWdesc)
printf ("-I-EMUSRV: linkWdesc = #%08X Link0InLength = #%x.\n", LinkWdesc, Link0InLength);
}
#endif
if (LinkWdesc != NotProcess_p)
{
/* Can the next byte of outgoing message be transferred? */
/* Check the length register too. See notes at Link0Out. */
if (Link0InLength)
{
activity++;
/* Move the requested amount of message. */
loop1 = 0;
MSGDBG2 ("-I-EMUSRV: FromServerLen = #%X.\n", FromServerLen);
#ifdef EMUDEBUG
dump_message ("Reply.", FromServerBuffer, FromServerLen);
#endif
if (usetvs)
{
MSGDBG2 ("-I-EMUSRV: TVS input. Link0InLength = #%X.\n", Link0InLength);
if (feof (InpFile))
{
MSGDBG ("-I-EMUSRV: EOF on TVS input.\n");
quit = TRUE;
}
else
{
FromServerLen = 0;
while (loop1 < Link0InLength)
{
if (feof (InpFile))
{
MSGDBG ("-I-EMUSRV: EOF during TVS input.\n");
/* Terminate here. */
quit = TRUE;
break;
}
data = getc (InpFile);
writebyte_int (Link0InDest, data);
Link0InDest++;
FromServerBuffer[FromServerLen++] = data;
loop1++;
}
}
loop1 = Link0InLength;
#ifdef EMUDEBUG
if (msgdebug)
{
DumpMessage = TRUE;
dump_message ("TVS Input.", FromServerBuffer, FromServerLen);
}
#endif
FromServerLen = 0;
}
else
{
while ((loop1 < Link0InLength) && (FromServerLen > 0))
{
writebyte_int (Link0InDest, FromServerBuffer[loop1]);
Link0InDest++;
FromServerLen--;
loop1++;
}
/* Moved loop bytes. Correct FromServerBuffer. */
for (loop2=0; loop2<FromServerLen; loop2++)
FromServerBuffer[loop2] = FromServerBuffer[loop2+loop1];
}
MSGDBG3 ("-I-EMUSRV: Link0InLength = #%X, loop = #%X.\n", Link0InLength, loop1);
/* If message request was fully satisfied, reset channel. */
Link0InLength = Link0InLength - loop1;
if (Link0InLength == 0)
{
MSGDBG2 ("-I-EMUSRV: Comms request satisfied. Reschedule process #%8X.\n", LinkWdesc);
/* Reset channel. */
reset_channel (Link0In);
/* Reschedule outputting process. */
schedule (LinkWdesc);
}
}
else
{
MSGDBG2 ("-I-EMUSRV: Link0In ALT test. LinkWdesc=#%8X.\n", LinkWdesc);
alt_channel (&Link[0].In);
}
}
return activity;
}
int printable (int ch)
{
return (31 < ch) && (ch < 127) ? ch : '.';
}
#ifdef EMUDEBUG
void dump_message (char *msg, u_char *buffer, int len)
{
int temp, i;
if (!msgdebug)
return;
if (FALSE == DumpMessage)
return;
if (msg)
printf ("-I-EMUSRV: %s\n", msg);
printf ("-I-EMUSRV: Message dump.\n");
for (temp = 0; temp < len; temp += 16)
{
for (i = 0; i < 16; i++)
{
if (temp + i < len)
printf ("%02X ", buffer[temp + i]);
else
printf (" ");
}
printf (" ");
for (i = 0; i < 16; i++)
{
if (temp + i < len)
printf ("%c", printable (buffer[temp + i]));
}
printf ("\n");
}
DumpMessage = FALSE;
}
#endif
void message(void)
{
u_char tag;
tag = ToServerBuffer[2];
#ifdef __MWERKS__
/* Update stdio stream. */
check_input();
#endif
MSGDBG4 ("-I-EMUSRV: Handling server command. Buffer = %d. Tag = #%02X (%s)\n", ToServerLen, tag, msgtag (tag));
/* XXX: 1024byte getblock/putblock support */
/* XXX: correctly respond to non-supported sp function request */
DumpMessage = TRUE;
#ifdef EMUDEBUG
dump_message (0, ToServerBuffer, ToServerLen);
#endif
switch (tag)
{
case SP_OPEN: sp_open();
break;
case SP_CLOSE: sp_close();
break;
case SP_READ: sp_read();
break;
case SP_WRITE: sp_write();
break;
case SP_GETS: sp_gets();
break;
case SP_PUTS: sp_puts();
break;
case SP_FLUSH: sp_flush();
break;
case SP_SEEK: sp_seek();
break;
case SP_TELL: sp_tell();
break;
case SP_EOF: sp_eof();
break;
case SP_FERROR: sp_ferror();
break;
case SP_REMOVE: sp_remove();
break;
case SP_RENAME: sp_rename();
break;
case SP_GETKEY: sp_getkey();
break;
case SP_POLLKEY: sp_pollkey();
break;
case SP_GETENV: sp_getenv();
break;
case SP_TIME: sp_time();
break;
case SP_SYSTEM: sp_system();
break;
case SP_EXIT: sp_exit();
break;
case SP_COMMANDLINE: sp_commandline();
break;
case SP_CORE: sp_core();
break;
case SP_VERSION: sp_version();
break;
default: /*
printf ("-E-EMUSRV: Error - bad server command %02X.\n", tag);
dump_message (0, ToServerBuffer, ToServerLen);
handler (-1);
*/
notimpl_packet ();
break;
}
DumpMessage = TRUE;
}
void notimpl_packet (void)
{
FromServerBuffer[0] = 6;
FromServerBuffer[1] = 0;
FromServerBuffer[2] = SP_NOT_IMPLEMENTED;
FromServerBuffer[3] = 0;
FromServerBuffer[4] = 0;
FromServerBuffer[5] = 0;
FromServerBuffer[6] = 0;
FromServerBuffer[7] = 0;
FromServerLen = 8;
}
void error_packet (void)
{
FromServerBuffer[0] = 6;
FromServerBuffer[1] = 0;
FromServerBuffer[2] = SP_ERROR;
FromServerBuffer[3] = 0;
FromServerBuffer[4] = 0;
FromServerBuffer[5] = 0;
FromServerBuffer[6] = 0;
FromServerBuffer[7] = 0;
FromServerLen = 8;
}
void sp_open (void)
{
char filename[256];
char string[10];
int namelen;
int type;
int mode;
FILE *fd;
unsigned int fdx;
namelen = ToServerBuffer[3]+(256*ToServerBuffer[4]);
if (ToServerLen<(namelen+7))
{
printf ("-W-EMUSRV: Bad message packet - SP_OPEN - too short.\n");
error_packet ();
return;
}
if (namelen <= 0)
{ printf ("-W-EMUSRV: Bad Packet.\n");
error_packet ();
return;
}
strncpy (filename, (char *) (ToServerBuffer+5), namelen);
filename[namelen] = '\0';
type = ToServerBuffer[5+namelen];
mode = ToServerBuffer[6+namelen];
switch (type)
{
case SP_BINARY: break;
case SP_TEXT: break;
default: error_packet ();
return;
}
switch (mode)
{
case SP_INPUT: strcpy (string, "r");
break;
case SP_OUTPUT: strcpy (string, "w");
break;
case SP_APPEND: strcpy (string, "a");
break;
case SP_EXISTING_UPDATE: strcpy (string, "r+");
break;
case SP_NEW_UPDATE: strcpy (string, "w+");
break;
case SP_APPEND_UPDATE: strcpy (string, "a+");
break;
default: error_packet ();
return;
}
fd = fopen (filename, string);
if (fd == NULL)
{
EMUDBG2 ("-W-EMUSRV: Failed to open %s!\n",filename);
error_packet ();
return;
}
#ifdef _MSC_VER
if (type == SP_BINARY)
_setmode( _fileno(fd), _O_BINARY);
#endif
FromServerBuffer[0] = 6;
FromServerBuffer[1] = 0;
FromServerBuffer[2] = SP_OK;
fdx = FromFile (fd);
Files[fdx].type = type;
FromServerBuffer[3] = ((unsigned int)fdx) & 0x000000ff;
FromServerBuffer[4] = (((unsigned int)fdx) & 0x0000ff00) >> 8;
FromServerBuffer[5] = (((unsigned int)fdx) & 0x00ff0000) >> 16;
FromServerBuffer[6] = (((unsigned int)fdx) & 0xff000000) >> 24;
FromServerBuffer[7] = 0;
FromServerLen = 8;
}
void sp_close (void)
{
FILE *fd;
unsigned int fdx;
fdx = ToServerBuffer[3] + (ToServerBuffer[4]<<8) + (ToServerBuffer[5]<<16) + (ToServerBuffer[6]<<24);
fd = ToFile (fdx);
if (fclose(fd) != 0)
{
error_packet ();
return;
}
Files[fdx].fd = (FILE *)NULL;
Files[fdx].type = -1;
FromServerBuffer[0] = 6;
FromServerBuffer[1] = 0;
FromServerBuffer[2] = SP_OK;
FromServerBuffer[3] = 0;
FromServerBuffer[4] = 0;
FromServerBuffer[5] = 0;
FromServerBuffer[6] = 0;
FromServerBuffer[7] = 0;
FromServerLen = 8;
}
void sp_read (void)
{
FILE *fd;
int count;
int length;
if (ToServerLen<9)
{
printf ("-W-EMUSRV: Bad message packet - SP_READ - too short.\n");
error_packet ();
return;
}
fd = ToFile (ToServerBuffer[3] + (ToServerBuffer[4]<<8) + (ToServerBuffer[5]<<16) + (ToServerBuffer[6]<<24));
count = ToServerBuffer[7] + (ToServerBuffer[8]<<8);
if (count > 16384)
{
printf ("-W-EMUSRV: SP_READ - Requested %d bytes - too many.\n", count);
handler (-1);
}
length = fread ((&FromServerBuffer[5]), 1, count, fd);
FromServerBuffer[2] = SP_OK;
FromServerBuffer[3] = (length & 0x000000ff);
FromServerBuffer[4] = (length & 0x0000ff00) >> 8;
length = length + 3;
if ((length & 0x01) == 1) length++;
FromServerBuffer[0] = (length & 0x000000ff);
FromServerBuffer[1] = (length & 0x0000ff00) >> 8;
FromServerLen = length + 2;
}
void sp_write (void)
{
FILE *fd;
int datalen;
int length;
int pos;
unsigned int fdx;
fdx = ToServerBuffer[3] + (ToServerBuffer[4]<<8) + (ToServerBuffer[5]<<16) + (ToServerBuffer[6]<<24);
fd = ToFile (fdx);
datalen = ToServerBuffer[7]+(ToServerBuffer[8]<<8);
if (ToServerLen<(datalen+9))
{
printf ("-W-EMUSRV: Bad message packet - SP_WRITE - too short.\n");
error_packet ();
return;
}
/* Massage output to avoid CRs. */
if (Files[fdx].type == SP_TEXT)
for (pos = 9; pos < (9 + datalen); pos++)
if (ToServerBuffer[pos] == 0x0d)
ToServerBuffer[pos] = 0x00;
#ifdef EMUDEBUG
if (dbgtrigger && (NULL != strstr ((char *) &ToServerBuffer[9], dbgtrigger)))
{
set_debug ();
dbgtrigger = NULL;
}
#endif
length = fwrite ((&ToServerBuffer[9]), 1, datalen, fd);
/* Must flush screen output. Strange standard. */
if (fd == stdout)
{
fflush (stdout);
/* fflush doesn't work with SIOUX without this. */
/*SetSIOUXBufferMode (SIOUXNoBuffering);
printf ("\0");
SetSIOUXBufferMode (SIOUXLineBuffering);*/
}
FromServerBuffer[0] = 6;
FromServerBuffer[1] = 0;
FromServerBuffer[2] = SP_OK;
FromServerBuffer[3] = (length & 0x000000ff);
FromServerBuffer[4] = (length & 0x0000ff00) >> 8;
FromServerBuffer[5] = 0;
FromServerBuffer[6] = 0;
FromServerBuffer[7] = 0;
FromServerLen = 8;
}
void sp_gets (void) /* XXX */
{
FILE *fd;
int count;
char *result;
int length;
int temp;
if (ToServerLen<9)
{
printf ("-W-EMUSRV: Bad message packet - SP_GETS - too short.\n");
error_packet ();
return;
}
fd = ToFile (ToServerBuffer[3] + (ToServerBuffer[4]<<8) + (ToServerBuffer[5]<<16) + (ToServerBuffer[6]<<24));
count = ToServerBuffer[7] + (ToServerBuffer[8]<<8);
if (count > 16384)
{
printf ("-W-EMUSRV: SP_GETS - Requested %d bytes - too many.\n", count);
handler (-1);
}
/* Read count chars, or until EOF, or until newline. */
result = fgets ((char *) &(FromServerBuffer[5]), (count+1), fd);
if (result==NULL)
{
error_packet ();
return;
}
length = strlen (result);
/* Do not return newline characters. */
for (temp=0; temp<length; temp++)
if (FromServerBuffer[5+temp]=='\n')
length = temp;
FromServerBuffer[2] = SP_OK;
FromServerBuffer[3] = (length & 0x000000ff);
FromServerBuffer[4] = (length & 0x0000ff00) >> 8;
length = length + 3;
if ((length & 0x01) == 1) length++;
FromServerBuffer[0] = (length & 0x000000ff);
FromServerBuffer[1] = (length & 0x0000ff00) >> 8;
FromServerLen = length + 2;
}
void sp_puts (void) /* XXX */
{
FILE *fd;
int datalen;
int length;
fd = ToFile (ToServerBuffer[3] + (ToServerBuffer[4]<<8) + (ToServerBuffer[5]<<16) + (ToServerBuffer[6]<<24));
datalen = ToServerBuffer[7]+(ToServerBuffer[8]<<8);
if (ToServerLen<(datalen+9))
{
printf ("-W-EMUSRV: Bad message packet - SP_PUTS - too short.\n");
error_packet ();
return;
}
length = fwrite ((&ToServerBuffer[9]), 1, datalen, fd);
/* Must flush stdout. */
if (fd == stdout)
{
fflush (stdout);
/* fflush doesn't work with SIOUX without this. */
/*SetSIOUXBufferMode (SIOUXNoBuffering);
printf ("\0");
SetSIOUXBufferMode (SIOUXLineBuffering);*/
}
if (length==EOF)
{
error_packet ();
return;
}
length = fputs ("\n", fd);
if (length==EOF)
{
error_packet ();
return;
}
FromServerBuffer[0] = 6;
FromServerBuffer[1] = 0;
FromServerBuffer[2] = SP_OK;
FromServerBuffer[3] = 0;
FromServerBuffer[4] = 0;
FromServerBuffer[5] = 0;
FromServerBuffer[6] = 0;
FromServerBuffer[7] = 0;
FromServerLen = 8;
}
void sp_flush (void) /* XXX */
{
FILE *fd;
fd = ToFile (ToServerBuffer[3] + (ToServerBuffer[4]<<8) + (ToServerBuffer[5]<<16) + (ToServerBuffer[6]<<24));
if (fflush (fd) != 0)
{
error_packet ();
return;
}
FromServerBuffer[0] = 6;
FromServerBuffer[1] = 0;
FromServerBuffer[2] = SP_OK;
FromServerBuffer[3] = 0;
FromServerBuffer[4] = 0;
FromServerBuffer[5] = 0;
FromServerBuffer[6] = 0;
FromServerBuffer[7] = 0;
FromServerLen = 8;
}
void sp_seek (void) /* XXX */
{
FILE *fd;
int32_t offset;
int origin;
fd = ToFile (ToServerBuffer[3] + (ToServerBuffer[4]<<8) + (ToServerBuffer[5]<<16) + (ToServerBuffer[6]<<24));
if (ToServerLen<15)
{
printf ("-W-EMUSRV: Bad message packet - SP_SEEK - too short.\n");
error_packet ();
return;
}
offset = ToServerBuffer[7] + (ToServerBuffer[8]<<8);
offset = offset + (ToServerBuffer[9]<<16) + (ToServerBuffer[10]<<24);
origin = ToServerBuffer[11] + (ToServerBuffer[12]<<8);
origin = origin + (ToServerBuffer[13]<<16) + (ToServerBuffer[14]<<24);