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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
|
/*
* Copyright 1990 Network Computing Devices;
* Portions Copyright 1987 by Digital Equipment Corporation
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation, and that the names of Network Computing
* Devices or Digital not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior
* permission. Network Computing Devices or Digital make no representations
* about the suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
*
* NETWORK COMPUTING DEVICES AND DIGITAL DISCLAIM ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
* OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/*
Copyright 1987, 1994, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
*/
/*
* FSlibInt.c - Internal support routines for the C subroutine
* interface library (FSlib).
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include "FSlibint.h"
#include <X11/Xos.h>
static void _EatData32 ( FSServer *svr, unsigned long n );
static const char * _SysErrorMsg ( int n );
/* check for both EAGAIN and EWOULDBLOCK, because some supposedly POSIX
* systems are broken and return EWOULDBLOCK when they should return EAGAIN
*/
#ifdef WIN32
#define ETEST() (WSAGetLastError() == WSAEWOULDBLOCK)
#else
#if defined(EAGAIN) && defined(EWOULDBLOCK)
#define ETEST() (errno == EAGAIN || errno == EWOULDBLOCK)
#else
#ifdef EAGAIN
#define ETEST() (errno == EAGAIN)
#else
#define ETEST() (errno == EWOULDBLOCK)
#endif
#endif
#endif
#ifdef WIN32
#define ECHECK(err) (WSAGetLastError() == err)
#define ESET(val) WSASetLastError(val)
#else
#ifdef ISC
#define ECHECK(err) ((errno == err) || ETEST())
#else
#define ECHECK(err) (errno == err)
#endif
#define ESET(val) errno = val
#endif
/*
* The following routines are internal routines used by FSlib for protocol
* packet transmission and reception.
*
* FSIOError(FSServer *) will be called if any sort of system call error occurs.
* This is assumed to be a fatal condition, i.e., FSIOError should not return.
*
* FSError(FSServer *, FSErrorEvent *) will be called whenever an FS_Error event is
* received. This is not assumed to be a fatal condition, i.e., it is
* acceptable for this procedure to return. However, FSError should NOT
* perform any operations (directly or indirectly) on the DISPLAY.
*
* Routines declared with a return type of 'Status' return 0 on failure,
* and non 0 on success. Routines with no declared return type don't
* return anything. Whenever possible routines that create objects return
* the object they have created.
*/
_FSQEvent *_FSqfree = NULL; /* NULL _FSQEvent. */
static int padlength[4] = {0, 3, 2, 1};
/*
* lookup table for adding padding bytes to data that is read from or written
* to the FS socket.
*/
static fsReq _dummy_request = {
0, 0, 0
};
/*
* _FSFlush - Flush the FS request buffer. If the buffer is empty, no
* action is taken. This routine correctly handles incremental writes.
* This routine may have to be reworked if int < long.
*/
void
_FSFlush(register FSServer *svr)
{
register long size,
todo;
register int write_stat;
register char *bufindex;
size = todo = svr->bufptr - svr->buffer;
bufindex = svr->bufptr = svr->buffer;
/*
* While write has not written the entire buffer, keep looping until the
* entire buffer is written. bufindex will be incremented and size
* decremented as buffer is written out.
*/
while (size) {
ESET(0);
write_stat = _FSTransWrite(svr->trans_conn, bufindex, (int) todo);
if (write_stat >= 0) {
size -= write_stat;
todo = size;
bufindex += write_stat;
} else if (ETEST()) {
_FSWaitForWritable(svr);
#ifdef SUNSYSV
} else if (ECHECK(0)) {
_FSWaitForWritable(svr);
#endif
#ifdef EMSGSIZE
} else if (ECHECK(EMSGSIZE)) {
if (todo > 1)
todo >>= 1;
else
_FSWaitForWritable(svr);
#endif
} else {
/* Write failed! */
/* errno set by write system call. */
(*_FSIOErrorFunction) (svr);
}
}
svr->last_req = (char *) &_dummy_request;
}
int
_FSEventsQueued(
register FSServer *svr,
int mode)
{
register BytesReadable_t len;
BytesReadable_t pend;
char buf[BUFSIZE];
register fsReply *rep;
if (mode == QueuedAfterFlush) {
_FSFlush(svr);
if (svr->qlen)
return (svr->qlen);
}
if (_FSTransBytesReadable(svr->trans_conn, &pend) < 0)
(*_FSIOErrorFunction) (svr);
if ((len = pend) < SIZEOF(fsReply))
return (svr->qlen); /* _FSFlush can enqueue events */
else if (len > BUFSIZE)
len = BUFSIZE;
len /= SIZEOF(fsReply);
pend = len * SIZEOF(fsReply);
_FSRead(svr, buf, (long) pend);
/* no space between comma and type or else macro will die */
STARTITERATE(rep, fsReply, buf, (len > 0), len--) {
if (rep->generic.type == FS_Error)
_FSError(svr, (fsError *) rep);
else /* must be an event packet */
_FSEnq(svr, (fsEvent *) rep);
}
ENDITERATE
return (svr->qlen);
}
/* _FSReadEvents - Flush the output queue,
* then read as many events as possible (but at least 1) and enqueue them
*/
void
_FSReadEvents(register FSServer *svr)
{
char buf[BUFSIZE];
BytesReadable_t pend_not_register; /* because can't "&" a register
* variable */
register BytesReadable_t pend;
register fsEvent *ev;
Bool not_yet_flushed = True;
do {
/* find out how much data can be read */
if (_FSTransBytesReadable(svr->trans_conn, &pend_not_register) < 0)
(*_FSIOErrorFunction) (svr);
pend = pend_not_register;
/*
* must read at least one fsEvent; if none is pending, then we'll just
* flush and block waiting for it
*/
if (pend < SIZEOF(fsEvent)) {
pend = SIZEOF(fsEvent);
/* don't flush until we block the first time */
if (not_yet_flushed) {
int qlen = svr->qlen;
_FSFlush(svr);
if (qlen != svr->qlen)
return;
not_yet_flushed = False;
}
}
/* but we won't read more than the max buffer size */
if (pend > BUFSIZE)
pend = BUFSIZE;
/* round down to an integral number of XReps */
pend = (pend / SIZEOF(fsEvent)) * SIZEOF(fsEvent);
_FSRead(svr, buf, (long)pend);
/* no space between comma and type or else macro will die */
STARTITERATE(ev, fsEvent, buf, (pend > 0),
pend -= SIZEOF(fsEvent)) {
if (ev->type == FS_Error)
_FSError(svr, (fsError *) ev);
else /* it's an event packet; enqueue it */
_FSEnq(svr, ev);
}
ENDITERATE
} while (svr->head == NULL);
}
/*
* _FSRead - Read bytes from the socket taking into account incomplete
* reads. This routine may have to be reworked if int < long.
*/
void
_FSRead(
register FSServer *svr,
register char *data,
register long size)
{
register long bytes_read;
#if defined(SVR4) && defined(i386)
int num_failed_reads = 0;
#endif
if (size == 0)
return;
ESET(0);
/*
* For SVR4 with a unix-domain connection, ETEST() after selecting
* readable means the server has died. To do this here, we look for
* two consecutive reads returning ETEST().
*/
while ((bytes_read = _FSTransRead(svr->trans_conn, data, (int) size))
!= size) {
if (bytes_read > 0) {
size -= bytes_read;
data += bytes_read;
#if defined(SVR4) && defined(i386)
num_failed_reads = 0;
#endif
}
else if (ETEST()) {
_FSWaitForReadable(svr);
#if defined(SVR4) && defined(i386)
num_failed_reads++;
if (num_failed_reads > 1) {
ESET(EPIPE);
(*_FSIOErrorFunction) (svr);
}
#endif
ESET(0);
}
#ifdef SUNSYSV
else if (ECHECK(0)) {
_FSWaitForReadable(svr);
}
#endif
else if (bytes_read == 0) {
/* Read failed because of end of file! */
ESET(EPIPE);
(*_FSIOErrorFunction) (svr);
} else { /* bytes_read is less than 0; presumably -1 */
/* If it's a system call interrupt, it's not an error. */
if (!ECHECK(EINTR))
(*_FSIOErrorFunction) (svr);
#if defined(SVR4) && defined(i386)
else
num_failed_reads = 0;
#endif
}
}
}
#ifdef WORD64
/*
* XXX This is a *really* stupid way of doing this....
*/
#define PACKBUFFERSIZE 4096
/*
* _FSRead32 - Read bytes from the socket unpacking each 32 bits
* into a long (64 bits on a CRAY computer).
*
*/
static void
_doFSRead32(
register FSServer *svr,
register long *data,
register long size,
register char *packbuffer)
{
long *lpack,
*lp;
long mask32 = 0x00000000ffffffff;
long maskw,
nwords,
i,
bits;
_FSReadPad(svr, packbuffer, size);
lp = data;
lpack = (long *) packbuffer;
nwords = size >> 2;
bits = 32;
for (i = 0; i < nwords; i++) {
maskw = mask32 << bits;
*lp++ = (*lpack & maskw) >> bits;
bits = bits ^ 32;
if (bits) {
lpack++;
}
}
}
void
_FSRead32(
FSServer *svr,
long *data,
long len)
{
char packbuffer[PACKBUFFERSIZE];
unsigned nwords = (PACKBUFFERSIZE >> 2); /* bytes to CARD32 */
for (; len > nwords; len -= nwords, data += nwords) {
_doFSRead32(svr, data, nwords, packbuffer);
}
_doFSRead32(svr, data, len, packbuffer);
}
/*
* _FSRead16 - Read bytes from the socket unpacking each 16 bits
* into a long (64 bits on a CRAY computer).
*
*/
static void
_doFSRead16(
register FSServer *svr,
register short *data,
register long size,
char *packbuffer)
{
long *lpack,
*lp;
long mask16 = 0x000000000000ffff;
long maskw,
nwords,
i,
bits;
_FSRead(svr, packbuffer, size); /* don't do a padded read... */
lp = (long *) data;
lpack = (long *) packbuffer;
nwords = size >> 1; /* number of 16 bit words to be unpacked */
bits = 48;
for (i = 0; i < nwords; i++) {
maskw = mask16 << bits;
*lp++ = (*lpack & maskw) >> bits;
bits -= 16;
if (bits < 0) {
lpack++;
bits = 48;
}
}
}
void
_FSRead16(
FSServer *svr,
short *data,
long len)
{
char packbuffer[PACKBUFFERSIZE];
unsigned nwords = (PACKBUFFERSIZE >> 1); /* bytes to CARD16 */
for (; len > nwords; len -= nwords, data += nwords) {
_doFSRead16(svr, data, nwords, packbuffer);
}
_doFSRead16(svr, data, len, packbuffer);
}
void
_FSRead16Pad(
FSServer *svr,
short *data,
long size)
{
int slop = (size & 3);
short slopbuf[3];
_FSRead16(svr, data, size);
if (slop > 0) {
_FSRead16(svr, slopbuf, 4 - slop);
}
}
#endif /* WORD64 */
/*
* _FSReadPad - Read bytes from the socket taking into account incomplete
* reads. If the number of bytes is not 0 mod 32, read additional pad
* bytes. This routine may have to be reworked if int < long.
*/
void
_FSReadPad(
register FSServer *svr,
register char *data,
register long size)
{
register long bytes_read;
struct iovec iov[2];
char pad[3];
if (size == 0)
return;
iov[0].iov_len = (int) size;
iov[0].iov_base = data;
/*
* The following hack is used to provide 32 bit long-word aligned padding.
* The [1] vector is of length 0, 1, 2, or 3, whatever is needed.
*/
iov[1].iov_len = padlength[size & 3];
iov[1].iov_base = pad;
size += iov[1].iov_len;
ESET(0);
while ((bytes_read = _FSTransReadv(svr->trans_conn, iov, 2)) != size) {
if (bytes_read > 0) {
size -= bytes_read;
if (iov[0].iov_len < bytes_read) {
int pad_bytes_read = bytes_read - iov[0].iov_len;
iov[1].iov_len -= pad_bytes_read;
iov[1].iov_base =
(char *)iov[1].iov_base + pad_bytes_read;
iov[0].iov_len = 0;
} else {
iov[0].iov_len -= bytes_read;
iov[0].iov_base = (char *)iov[0].iov_base + bytes_read;
}
}
else if (ETEST()) {
_FSWaitForReadable(svr);
ESET(0);
}
#ifdef SUNSYSV
else if (ECHECK(0)) {
_FSWaitForReadable(svr);
}
#endif
else if (bytes_read == 0) {
/* Read failed because of end of file! */
ESET(EPIPE);
(*_FSIOErrorFunction) (svr);
} else { /* bytes_read is less than 0; presumably -1 */
/* If it's a system call interrupt, it's not an error. */
if (!ECHECK(EINTR))
(*_FSIOErrorFunction) (svr);
}
}
}
/*
* _FSSend - Flush the buffer and send the client data. 32 bit word aligned
* transmission is used, if size is not 0 mod 4, extra bytes are transmitted.
* This routine may have to be reworked if int < long;
*/
void
_FSSend(
register FSServer *svr,
const char *data,
register long size)
{
struct iovec iov[3];
static char pad[3] = {0, 0, 0};
long skip = 0;
long svrbufsize = (svr->bufptr - svr->buffer);
long padsize = padlength[size & 3];
long total = svrbufsize + size + padsize;
long todo = total;
/*
* There are 3 pieces that may need to be written out:
*
* o whatever is in the display buffer o the data passed in by the user o
* any padding needed to 32bit align the whole mess
*
* This loop looks at all 3 pieces each time through. It uses skip to figure
* out whether or not a given piece is needed.
*/
while (total) {
long before = skip; /* amount of whole thing written */
long remain = todo; /* amount to try this time, <= total */
int i = 0;
long len;
/*
* You could be very general here and have "in" and "out" iovecs and
* write a loop without using a macro, but what the heck. This
* translates to:
*
* how much of this piece is new? if more new then we are trying this
* time, clamp if nothing new then bump down amount already written,
* for next piece else put new stuff in iovec, will need all of next
* piece
*
* Note that todo had better be at least 1 or else we'll end up writing 0
* iovecs.
*/
#define InsertIOV(pointer, length) \
len = (length) - before; \
if (len > remain) \
len = remain; \
if (len <= 0) { \
before = (-len); \
} else { \
iov[i].iov_len = len; \
iov[i].iov_base = (pointer) + before; \
i++; \
remain -= len; \
before = 0; \
}
InsertIOV(svr->buffer, svrbufsize)
InsertIOV((char *)data, size)
InsertIOV(pad, padsize)
ESET(0);
if ((len = _FSTransWritev(svr->trans_conn, iov, i)) >= 0) {
skip += len;
total -= len;
todo = total;
} else if (ETEST()) {
_FSWaitForWritable(svr);
#ifdef SUNSYSV
} else if (ECHECK(0)) {
_FSWaitForWritable(svr);
#endif
#ifdef EMSGSIZE
} else if (ECHECK(EMSGSIZE)) {
if (todo > 1)
todo >>= 1;
else
_FSWaitForWritable(svr);
#endif
} else {
(*_FSIOErrorFunction) (svr);
}
}
svr->bufptr = svr->buffer;
svr->last_req = (char *) &_dummy_request;
}
#ifdef undef
/*
* _FSAllocID - normal resource ID allocation routine. A client
* can roll his own and instatantiate it if he wants, but must
* follow the rules.
*/
FSID
_FSAllocID(register FSServer *svr)
{
return (svr->resource_base + (svr->resource_id++ << svr->resource_shift));
}
#endif
/*
* The hard part about this is that we only get 16 bits from a reply. Well,
* then, we have three values that will march along, with the following
* invariant:
* svr->last_request_read <= rep->sequenceNumber <= svr->request
* The right choice for rep->sequenceNumber is the largest that
* still meets these constraints.
*/
unsigned long
_FSSetLastRequestRead(
register FSServer *svr,
register fsGenericReply *rep)
{
register unsigned long newseq,
lastseq;
newseq = (svr->last_request_read & ~((unsigned long) 0xffff)) |
rep->sequenceNumber;
lastseq = svr->last_request_read;
while (newseq < lastseq) {
newseq += 0x10000;
if (newseq > svr->request) {
(void) fprintf(stderr,
"FSlib: sequence lost (0x%lx > 0x%lx) in reply type 0x%x!\n",
newseq, svr->request,
(unsigned int) rep->type);
newseq -= 0x10000;
break;
}
}
svr->last_request_read = newseq;
return (newseq);
}
/*
* _FSReply - Wait for a reply packet and copy its contents into the
* specified rep. Mean while we must handle error and event packets that
* we may encounter.
*/
Status
_FSReply(
register FSServer *svr,
register fsReply *rep,
int extra, /* number of 32-bit words expected after the
* reply */
Bool discard)/* should I discard data followind "extra"
* words? */
{
/*
* Pull out the serial number now, so that (currently illegal) requests
* generated by an error handler don't confuse us.
*/
unsigned long cur_request = svr->request;
long rem_length;
_FSFlush(svr);
while (1) {
_FSRead(svr, (char *) rep, (long) SIZEOF(fsReply));
switch ((int) rep->generic.type) {
case FS_Reply:
/*
* Reply received. Fast update for synchronous replies, but deal
* with multiple outstanding replies.
*/
if (rep->generic.sequenceNumber == (cur_request & 0xffff))
svr->last_request_read = cur_request;
else
(void) _FSSetLastRequestRead(svr, &rep->generic);
rem_length = rep->generic.length - (SIZEOF(fsReply) >> 2);
if (rem_length < 0) rem_length = 0;
if (extra == 0) {
if (discard && rem_length)
/* unexpectedly long reply! */
_EatData32(svr, rem_length);
return (1);
}
if (extra == rem_length) {
/*
* Read the extra data into storage immediately following the
* GenericReply structure.
*/
_FSRead(svr, (char *) NEXTPTR(rep, fsReply), ((long) extra) << 2);
return (1);
}
if (extra < rem_length) {
/* Actual reply is longer than "extra" */
_FSRead(svr, (char *) NEXTPTR(rep, fsReply), ((long) extra) << 2);
if (discard)
_EatData32(svr, rem_length - extra);
return (1);
}
/*
* if we get here, then extra > rem_length -- meaning we
* read a reply that's shorter than we expected. This is an
* error, but we still need to figure out how to handle it...
*/
_FSRead(svr, (char *) NEXTPTR(rep, fsReply), rem_length << 2);
(*_FSIOErrorFunction) (svr);
return (0);
case FS_Error:
{
register _FSExtension *ext;
register Bool ret = False;
int ret_code;
fsError err;
unsigned long serial;
long err_data;
/* copy in the part we already read off the wire */
memcpy(&err, rep, SIZEOF(fsReply));
/* read the rest of the error */
_FSRead(svr, (char *) &err + SIZEOF(fsReply),
(long) (SIZEOF(fsError) - SIZEOF(fsReply)));
serial = _FSSetLastRequestRead(svr, (fsGenericReply *) rep);
if (serial == cur_request)
/* do not die on certain failures */
switch ((int) err.request) {
/* suck in any extra error info */
case FSBadResolution:
case FSBadLength:
case FSBadIDChoice:
case FSBadRange:
case FSBadFont:
case FSBadFormat:
_FSRead(svr, (char *) &err_data, 4);
break;
case FSBadAccessContext:
_FSRead(svr, (char *) &err_data, 4);
return 0;
case FSBadAlloc:
return (0);
/*
* we better see if there is an extension who may want
* to suppress the error.
*/
default:
ext = svr->ext_procs;
while (ext) {
if (ext->error != NULL)
ret = (*ext->error)
(svr, &err, &ext->codes, &ret_code);
ext = ext->next;
}
if (ret)
return (ret_code);
break;
}
_FSError(svr, &err);
if (serial == cur_request)
return (0);
}
break;
default:
_FSEnq(svr, (fsEvent *) rep);
break;
}
}
}
/* Read and discard "n" 8-bit bytes of data */
void
_FSEatData(
FSServer *svr,
register unsigned long n)
{
#define SCRATCHSIZE 2048
char buf[SCRATCHSIZE];
while (n > 0) {
register long bytes_read = (n > SCRATCHSIZE) ? SCRATCHSIZE : n;
_FSRead(svr, buf, bytes_read);
n -= bytes_read;
}
#undef SCRATCHSIZE
}
/* Read and discard "n" 32-bit words. */
static void
_EatData32(
FSServer *svr,
unsigned long n)
{
_FSEatData(svr, n << 2);
}
/*
* _FSEnq - Place event packets on the display's queue.
* note that no squishing of move events in V11, since there
* is pointer motion hints....
*/
void
_FSEnq(
register FSServer *svr,
register fsEvent *event)
{
register _FSQEvent *qelt;
/*NOSTRICT*/
if ((qelt = _FSqfree) != NULL) {
/* If _FSqfree is non-NULL do this, else malloc a new one. */
_FSqfree = qelt->next;
} else if ((qelt = FSmalloc(sizeof(_FSQEvent))) == NULL) {
/* Malloc call failed! */
ESET(ENOMEM);
(*_FSIOErrorFunction) (svr);
}
qelt->next = NULL;
/* go call through display to find proper event reformatter */
if ((*svr->event_vec[event->type & 0177]) (svr, &qelt->event, event)) {
if (svr->tail)
svr->tail->next = qelt;
else
svr->head = qelt;
svr->tail = qelt;
svr->qlen++;
} else {
/* ignored, or stashed away for many-to-one compression */
qelt->next = _FSqfree;
_FSqfree = qelt;
}
}
/*
* EventToWire in seperate file in that often not needed.
*/
/*ARGSUSED*/
Bool
_FSUnknownWireEvent(
register FSServer *svr, /* pointer to display structure */
register FSEvent *re, /* pointer to where event should be
* reformatted */
register fsEvent *event) /* wire protocol event */
{
#ifdef notdef
(void) fprintf(stderr,
"FSlib: unhandled wire event! event number = %d, display = %x\n.",
event->type, svr);
#endif
return (False);
}
/*ARGSUSED*/
Status
_FSUnknownNativeEvent(
register FSServer *svr, /* pointer to display structure */
register FSEvent *re, /* pointer to where event should be
* reformatted */
register fsEvent *event) /* wire protocol event */
{
#ifdef notdef
(void) fprintf(stderr,
"FSlib: unhandled native event! event number = %d, display = %x\n.",
re->type, svr);
#endif
return (0);
}
/*
* reformat a wire event into an FSEvent structure of the right type.
*/
Bool
_FSWireToEvent(
register FSServer *svr, /* pointer to display structure */
register FSEvent *re, /* pointer to where event should be
* reformatted */
register fsEvent *event) /* wire protocol event */
{
re->type = event->type & 0x7f;
((FSAnyEvent *) re)->serial = _FSSetLastRequestRead(svr,
(fsGenericReply *) event);
((FSAnyEvent *) re)->send_event = ((event->type & 0x80) != 0);
((FSAnyEvent *) re)->server = svr;
/*
* Ignore the leading bit of the event type since it is set when a client
* sends an event rather than the server.
*/
switch (event->type & 0177) {
default:
return (_FSUnknownWireEvent(svr, re, event));
}
}
static const char *
_SysErrorMsg(int n)
{
char *s = strerror(n);
return (s ? s : "no such error");
}
#ifdef __SUNPRO_C
/* prevent "Function has no return statement" error for _FSDefaultIOError */
#pragma does_not_return(exit)
#endif
/*
* _FSDefaultIOError - Default fatal system error reporting routine. Called
* when an X internal system error is encountered.
*/
int
_FSDefaultIOError(FSServer *svr)
{
(void) fprintf(stderr,
"FSIO: fatal IO error %d (%s) on font server \"%s\"\r\n",
#ifdef WIN32
WSAGetLastError(), strerror(WSAGetLastError()),
#else
errno, _SysErrorMsg(errno),
#endif
FSServerString(svr) ? FSServerString(svr) : "");
(void) fprintf(stderr,
" after %lu requests (%lu known processed) with %d events remaining.\r\n",
FSNextRequest(svr) - 1, FSLastKnownRequestProcessed(svr),
FSQLength(svr));
if (ECHECK(EPIPE)) {
(void) fprintf(stderr,
" The connection was probably broken by a server shutdown.\r\n");
}
exit(1);
/* NOTREACHED */
}
/*
* _FSError - Default non-fatal error reporting routine. Called when an
* FS_Error packet is encountered in the input stream.
*/
int
_FSError(
FSServer *svr,
fsError *rep)
{
FSErrorEvent event;
/*
* FS_Error packet encountered! We need to unpack the error before giving
* it to the user.
*/
event.server = svr;
event.type = FS_Error;
event.serial = _FSSetLastRequestRead(svr, (fsGenericReply *) rep);
event.error_code = rep->request;
event.request_code = rep->major_opcode;
event.minor_code = rep->minor_opcode;
if (_FSErrorFunction != NULL) {
return ((*_FSErrorFunction) (svr, &event));
}
exit(1);
/* NOTREACHED */
}
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral" // We know better
#endif
int
_FSPrintDefaultError(
FSServer *svr,
FSErrorEvent *event,
FILE *fp)
{
char buffer[BUFSIZ];
char mesg[BUFSIZ];
char number[32];
const char *mtype = "FSlibMessage";
register _FSExtension *ext = (_FSExtension *) NULL;
(void) FSGetErrorText(svr, event->error_code, buffer, BUFSIZ);
(void) FSGetErrorDatabaseText(svr, mtype, "FSError", "FS Error", mesg,
BUFSIZ);
(void) fprintf(fp, "%s: %s\n ", mesg, buffer);
(void) FSGetErrorDatabaseText(svr, mtype, "MajorCode",
"Request Major code %d", mesg, BUFSIZ);
(void) fprintf(fp, mesg, event->request_code);
if (event->request_code < 128) {
snprintf(number, sizeof(number), "%d", event->request_code);
(void) FSGetErrorDatabaseText(svr, "FSRequest", number, "", buffer,
BUFSIZ);
} else {
for (ext = svr->ext_procs;
ext && (ext->codes.major_opcode != event->request_code);
ext = ext->next);
if (ext)
strcpy(buffer, ext->name);
else
buffer[0] = '\0';
}
(void) fprintf(fp, " (%s)\n ", buffer);
(void) FSGetErrorDatabaseText(svr, mtype, "MinorCode",
"Request Minor code %d", mesg, BUFSIZ);
(void) fprintf(fp, mesg, event->minor_code);
if (ext) {
snprintf(mesg, sizeof(mesg), "%s.%d", ext->name, event->minor_code);
(void) FSGetErrorDatabaseText(svr, "FSRequest", mesg, "", buffer,
BUFSIZ);
(void) fprintf(fp, " (%s)", buffer);
}
fputs("\n ", fp);
(void) FSGetErrorDatabaseText(svr, mtype, "ResourceID", "ResourceID 0x%x",
mesg, BUFSIZ);
(void) fprintf(fp, mesg, event->resourceid);
fputs("\n ", fp);
(void) FSGetErrorDatabaseText(svr, mtype, "ErrorSerial", "Error Serial #%d",
mesg, BUFSIZ);
(void) fprintf(fp, mesg, event->serial);
fputs("\n ", fp);
(void) FSGetErrorDatabaseText(svr, mtype, "CurrentSerial",
"Current Serial #%d", mesg, BUFSIZ);
(void) fprintf(fp, mesg, svr->request);
fputs("\n", fp);
return 1;
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
int
_FSDefaultError(
FSServer *svr,
FSErrorEvent *event)
{
if (_FSPrintDefaultError(svr, event, stderr) == 0)
return 0;
exit(1);
/* NOTREACHED */
}
FSIOErrorHandler _FSIOErrorFunction = _FSDefaultIOError;
FSErrorHandler _FSErrorFunction = _FSDefaultError;
/*
* This routine can be used to (cheaply) get some memory within a single
* Xlib routine for scratch space. It is reallocated from the same place
* each time, unless the library needs a large scratch space.
*/
char *
_FSAllocScratch(
register FSServer *svr,
unsigned long nbytes)
{
if (nbytes > svr->scratch_length) {
if (svr->scratch_buffer != NULL)
FSfree(svr->scratch_buffer);
return (svr->scratch_length = nbytes,
svr->scratch_buffer = FSmalloc(nbytes));
}
return (svr->scratch_buffer);
}
int
FSFree(char *data)
{
FSfree(data);
return 1;
}
unsigned char *
FSMalloc(unsigned size)
{
return (unsigned char *) FSmalloc(size);
}
#ifdef DataRoutineIsProcedure
void
Data(
FSServer *svr,
char *data,
long len)
{
if (svr->bufptr + (len) <= svr->bufmax) {
memmove(svr->bufptr, data, len);
svr->bufptr += ((len) + 3) & ~3;
} else {
_FSSend(svr, data, len);
}
}
#endif /* DataRoutineIsProcedure */
#ifdef WORD64
/*
* XXX This is a *really* stupid way of doing this. It should just use
* svr->bufptr directly, taking into account where in the word it is.
*/
/*
* Data16 - Place 16 bit data in the buffer.
*
* "svr" is a pointer to a FSServer.
* "data" is a pointer to the data.
* "len" is the length in bytes of the data.
*/
static void
doData16(
register FSServer *svr,
short *data,
unsigned len,
char *packbuffer)
{
long *lp,
*lpack;
long i,
nwords,
bits;
long mask16 = 0x000000000000ffff;
lp = (long *) data;
lpack = (long *) packbuffer;
*lpack = 0;
/* nwords is the number of 16 bit values to be packed,
* the low order 16 bits of each word will be packed
* into 64 bit words
*/
nwords = len >> 1;
bits = 48;
for (i = 0; i < nwords; i++) {
*lpack ^= (*lp & mask16) << bits;
bits -= 16;
lp++;
if (bits < 0) {
lpack++;
*lpack = 0;
bits = 48;
}
}
Data(svr, packbuffer, len);
}
void
Data16(
FSServer *svr,
short *data,
unsigned len)
{
char packbuffer[PACKBUFFERSIZE];
unsigned nwords = (PACKBUFFERSIZE >> 1); /* bytes to CARD16 */
for (; len > nwords; len -= nwords, data += nwords) {
doData16(svr, data, nwords, packbuffer);
}
doData16(svr, data, len, packbuffer);
}
/*
* Data32 - Place 32 bit data in the buffer.
*
* "svr" is a pointer to a FSServer.
* "data" is a pointer to the data.
* "len" is the length in bytes of the data.
*/
static
doData32(
register FSServer *svr,
long *data,
unsigned len,
char *packbuffer)
{
long *lp,
*lpack;
long i,
bits,
nwords;
long mask32 = 0x00000000ffffffff;
lpack = (long *) packbuffer;
lp = data;
*lpack = 0;
/* nwords is the number of 32 bit values to be packed
* the low order 32 bits of each word will be packed
* into 64 bit words
*/
nwords = len >> 2;
bits = 32;
for (i = 0; i < nwords; i++) {
*lpack ^= (*lp & mask32) << bits;
bits = bits ^ 32;
lp++;
if (bits) {
lpack++;
*lpack = 0;
}
}
Data(svr, packbuffer, len);
}
void
Data32(
FSServer *svr,
short *data,
unsigned len)
{
char packbuffer[PACKBUFFERSIZE];
unsigned nwords = (PACKBUFFERSIZE >> 2); /* bytes to CARD32 */
for (; len > nwords; len -= nwords, data += nwords) {
doData32(svr, data, nwords, packbuffer);
}
doData32(svr, data, len, packbuffer);
}
#endif /* WORD64 */
/*
* _FSFreeQ - free the queue of events, called by XCloseServer when there are
* no more displays left on the display list
*/
void
_FSFreeQ(void)
{
register _FSQEvent *qelt = _FSqfree;
while (qelt) {
register _FSQEvent *qnext = qelt->next;
FSfree(qelt);
qelt = qnext;
}
_FSqfree = NULL;
return;
}
#ifdef _POSIX_SOURCE /* stupid makedepend [need if] */
#ifndef __QNX__ /* QNX's uname nodename entry is not same as tcpip hostname */
#define NEED_UTSNAME
#endif
#endif
#ifdef hpux
#define NEED_UTSNAME
#endif
#ifdef SVR4
#ifndef _SEQUENT_
#define NEED_UTSNAME
#endif
#endif
#ifdef NEED_UTSNAME
#include <sys/utsname.h>
#endif
/*
* _FSGetHostname - similar to gethostname but allows special processing.
*/
int
_FSGetHostname(
char *buf,
int maxlen)
{
int len;
#ifdef NEED_UTSNAME
/*
* same host name crock as in server and xinit.
*/
struct utsname name;
uname(&name);
len = strlen(name.nodename);
if (len >= maxlen)
len = maxlen - 1;
strncpy(buf, name.nodename, len);
buf[len] = '\0';
#else
buf[0] = '\0';
(void) gethostname(buf, maxlen);
buf[maxlen - 1] = '\0';
len = strlen(buf);
#endif /* NEED_UTSNAME */
return len;
}
#ifndef _FSANYSET
/*
* This is not always a macro.
*/
_FSANYSET(long *src)
{
int i;
for (i=0; i<MSKCNT; i++)
if (src[ i ])
return (1);
return (0);
}
#endif
|