FSlibint.h revision 1bedbe3f
1/*
2 * Copyright 1990 Network Computing Devices;
3 * Portions Copyright 1987 by Digital Equipment Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation, and that the names of Network Computing
10 * Devices or Digital not be used in advertising or publicity pertaining
11 * to distribution of the software without specific, written prior
12 * permission. Network Computing Devices or Digital make no representations
13 * about the suitability of this software for any purpose.  It is provided
14 * "as is" without express or implied warranty.
15 *
16 * NETWORK COMPUTING DEVICES AND  DIGITAL DISCLAIM ALL WARRANTIES WITH
17 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES
19 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 * SOFTWARE.
24 */
25
26/*
27
28Copyright 1987, 1994, 1998  The Open Group
29
30Permission to use, copy, modify, distribute, and sell this software and its
31documentation for any purpose is hereby granted without fee, provided that
32the above copyright notice appear in all copies and that both that
33copyright notice and this permission notice appear in supporting
34documentation.
35
36The above copyright notice and this permission notice shall be included in
37all copies or substantial portions of the Software.
38
39THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
42OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
43AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
46Except as contained in this notice, the name of The Open Group shall not be
47used in advertising or otherwise to promote the sale, use or other dealings
48in this Software without prior written authorization from The Open Group.
49
50*/
51
52/*
53 * FSlib internal decls
54 */
55#include <X11/Xos.h>
56#include <stdio.h>
57#include <sys/types.h>
58
59#include	"FSlib.h"
60#include	"FSlibos.h"
61#include	<errno.h>
62#include	<stddef.h>
63
64typedef int (* FSIOErrorHandler)(FSServer *) _X_NORETURN;
65typedef int (* FSErrorHandler)(FSServer *, FSErrorEvent *);
66
67extern FSIOErrorHandler _FSIOErrorFunction;
68extern FSErrorHandler _FSErrorFunction;
69
70extern void _FSEatData ( FSServer *svr, unsigned long n );
71extern void _FSWaitForWritable ( FSServer *svr );
72extern void _FSWaitForReadable ( FSServer *svr );
73extern void _FSFlush ( FSServer *svr );
74extern void _FSRead ( FSServer *svr, char *data, long size );
75extern void _FSReadEvents ( FSServer *svr );
76extern void _FSReadPad ( FSServer *svr, char *data, long size );
77extern void _FSSend ( FSServer *svr, const char *data, long size );
78extern void _FSEnq ( FSServer *svr, fsEvent *event );
79extern void _FSFreeServerStructure ( FSServer *svr );
80extern int _FSError ( FSServer *svr, fsError *rep );
81extern int _FSReply ( FSServer *svr, fsReply *rep, int extra, int discard );
82extern XtransConnInfo _FSConnectServer ( char *server_name );
83extern void _FSDisconnectServer ( XtransConnInfo trans_conn );
84extern void _FSSendClientPrefix ( FSServer *svr, fsConnClientPrefix *client );
85extern int _FSEventsQueued ( FSServer *svr, int mode );
86extern unsigned long _FSSetLastRequestRead ( FSServer *svr,
87					     fsGenericReply *rep );
88extern int _FSUnknownWireEvent ( FSServer *svr, FSEvent *re, fsEvent *event );
89extern int _FSUnknownNativeEvent ( FSServer *svr, FSEvent *re,
90				   fsEvent *event );
91extern int _FSWireToEvent ( FSServer *svr, FSEvent *re, fsEvent *event );
92extern int _FSDefaultIOError ( FSServer *svr ) _X_NORETURN;
93extern int _FSPrintDefaultError ( FSServer *svr, FSErrorEvent *event,
94				  FILE *fp );
95extern int _FSDefaultError ( FSServer *svr, FSErrorEvent *event );
96extern char * _FSAllocScratch ( FSServer *svr, unsigned long nbytes );
97extern void _FSFreeQ ( void );
98extern int _FSGetHostname ( char *buf, int maxlen );
99
100extern FSErrorHandler  FSSetErrorHandler ( FSErrorHandler handler );
101extern FSIOErrorHandler FSSetIOErrorHandler ( FSIOErrorHandler handler );
102
103extern _FSQEvent *_FSqfree;
104extern FSServer *_FSHeadOfServerList;
105
106#ifndef BUFSIZE
107#define BUFSIZE 2048		/* FS output buffer size. */
108#endif
109
110/*
111 * server flags
112 */
113#define	FSlibServerIOError	(1L << 0)
114#define	FSlibServerClosing	(1L << 1)
115
116/*   Need to start requests on 64 bit word boundries
117 *   on a CRAY computer so add a NoOp (127) if needed.
118 *   A character pointer on a CRAY computer will be non-zero
119 *   after shifting right 61 bits of it is not pointing to
120 *   a word boundary.
121 */
122
123#ifdef WORD64
124#define WORD64ALIGN if ((long)svr->bufptr >> 61) {\
125           svr->last_req = svr->bufptr;\
126           *(svr->bufptr)   = FS_Noop;\
127           *(svr->bufptr+1) =  0;\
128           *(svr->bufptr+2) =  0;\
129           *(svr->bufptr+3) =  1;\
130             svr->request += 1;\
131             svr->bufptr += 4;\
132         }
133#else				/* else does not require alignment on 64-bit
134				 * boundaries */
135#define WORD64ALIGN
136#endif				/* WORD64 */
137
138/*
139 * GetReq - Get the next avilable FS request packet in the buffer and
140 * return it.
141 *
142 * "name" is the name of the request, e.g. InternAtom, OpenFont, etc.
143 * "req" is the name of the request pointer.
144 *
145 */
146
147#if !defined(UNIXCPP) || defined(ANSICPP)
148#define GetReq(name, req) \
149        WORD64ALIGN\
150	if ((svr->bufptr + SIZEOF(fs##name##Req)) > svr->bufmax)\
151		_FSFlush(svr);\
152	req = (fs##name##Req *)(svr->last_req = svr->bufptr);\
153	req->reqType = FS_##name;\
154	req->length = (SIZEOF(fs##name##Req))>>2;\
155	svr->bufptr += SIZEOF(fs##name##Req);\
156	svr->request++
157
158#else				/* non-ANSI C uses empty comment instead of
159				 * "##" for token concatenation */
160#define GetReq(name, req) \
161        WORD64ALIGN\
162	if ((svr->bufptr + SIZEOF(fs/**/name/**/Req)) > svr->bufmax)\
163		_FSFlush(svr);\
164	req = (fs/**/name/**/Req *)(svr->last_req = svr->bufptr);\
165	req->reqType = FS_/**/name;\
166	req->length = (SIZEOF(fs/**/name/**/Req))>>2;\
167	svr->bufptr += SIZEOF(fs/**/name/**/Req);\
168	svr->request++
169#endif
170
171/* GetReqExtra is the same as GetReq, but allocates "n" additional
172   bytes after the request. "n" must be a multiple of 4!  */
173
174#if !defined(UNIXCPP) || defined(ANSICPP)
175#define GetReqExtra(name, n, req) \
176        WORD64ALIGN\
177	if ((svr->bufptr + SIZEOF(fs##name##Req) + n) > svr->bufmax)\
178		_FSFlush(svr);\
179	req = (fs##name##Req *)(svr->last_req = svr->bufptr);\
180	req->reqType = FS_##name;\
181	req->length = (SIZEOF(fs##name##Req) + n)>>2;\
182	svr->bufptr += SIZEOF(fs##name##Req) + n;\
183	svr->request++
184#else
185#define GetReqExtra(name, n, req) \
186        WORD64ALIGN\
187	if ((svr->bufptr + SIZEOF(fs/**/name/**/Req) + n) > svr->bufmax)\
188		_FSFlush(svr);\
189	req = (fs/**/name/**/Req *)(svr->last_req = svr->bufptr);\
190	req->reqType = FS_/**/name;\
191	req->length = (SIZEOF(fs/**/name/**/Req) + n)>>2;\
192	svr->bufptr += SIZEOF(fs/**/name/**/Req) + n;\
193	svr->request++
194#endif
195
196
197/*
198 * GetResReq is for those requests that have a resource ID
199 * (Window, Pixmap, GContext, etc.) as their single argument.
200 * "rid" is the name of the resource.
201 */
202
203#if !defined(UNIXCPP) || defined(ANSICPP)
204#define GetResReq(name, rid, req) \
205        WORD64ALIGN\
206	if ((svr->bufptr + SIZEOF(fsResourceReq)) > svr->bufmax)\
207	    _FSFlush(svr);\
208	req = (fsResourceReq *) (svr->last_req = svr->bufptr);\
209	req->reqType = FS_##name;\
210	req->length = 2;\
211	req->id = (rid);\
212	svr->bufptr += SIZEOF(fsResourceReq);\
213	svr->request++
214#else
215#define GetResReq(name, rid, req) \
216        WORD64ALIGN\
217	if ((svr->bufptr + SIZEOF(fsResourceReq)) > svr->bufmax)\
218	    _FSFlush(svr);\
219	req = (fsResourceReq *) (svr->last_req = svr->bufptr);\
220	req->reqType = FS_/**/name;\
221	req->length = 2;\
222	req->id = (rid);\
223	svr->bufptr += SIZEOF(fsResourceReq);\
224	svr->request++
225#endif
226
227/*
228 * GetEmptyReq is for those requests that have no arguments
229 * at all.
230 */
231
232#if !defined(UNIXCPP) || defined(ANSICPP)
233#define GetEmptyReq(name, req) \
234        WORD64ALIGN\
235	if ((svr->bufptr + SIZEOF(fsReq)) > svr->bufmax)\
236	    _FSFlush(svr);\
237	req = (fsReq *) (svr->last_req = svr->bufptr);\
238	req->reqType = FS_##name;\
239	req->length = 1;\
240	svr->bufptr += SIZEOF(fsReq);\
241	svr->request++
242#else
243#define GetEmptyReq(name, req) \
244        WORD64ALIGN\
245	if ((svr->bufptr + SIZEOF(fsReq)) > svr->bufmax)\
246	    _FSFlush(svr);\
247	req = (fsReq *) (svr->last_req = svr->bufptr);\
248	req->reqType = FS_/**/name;\
249	req->length = 1;\
250	svr->bufptr += SIZEOF(fsReq);\
251	svr->request++
252#endif
253
254#define	SyncHandle()	\
255	if (svr->synchandler) (*svr->synchandler)(svr)
256
257/*
258 * Data - Place data in the buffer and pad the end to provide
259 * 32 bit word alignment.  Transmit if the buffer fills.
260 *
261 * "svr" is a pointer to a Display.
262 * "data" is a pinter to a data buffer.
263 * "len" is the length of the data buffer.
264 * we can presume buffer less than 2^16 bytes, so bcopy can be used safely.
265 */
266
267#ifdef DataRoutineIsProcedure
268extern void Data();
269
270#else
271#define Data(svr, data, len) \
272	if (svr->bufptr + (len) <= svr->bufmax) {\
273		memmove(svr->bufptr, data, len);\
274		svr->bufptr += ((len) + 3) & ~3;\
275	} else\
276		_FSSend(svr, data, len)
277#endif				/* DataRoutineIsProcedure */
278
279
280/* Allocate bytes from the buffer.  No padding is done, so if
281 * the length is not a multiple of 4, the caller must be
282 * careful to leave the buffer aligned after sending the
283 * current request.
284 *
285 * "type" is the type of the pointer being assigned to.
286 * "ptr" is the pointer being assigned to.
287 * "n" is the number of bytes to allocate.
288 *
289 * Example:
290 *    xTextElt *elt;
291 *    BufAlloc (xTextElt *, elt, nbytes)
292 */
293
294#define BufAlloc(type, ptr, n) \
295    if (svr->bufptr + (n) > svr->bufmax) \
296        _FSFlush (svr); \
297    ptr = (type) svr->bufptr; \
298    svr->bufptr += (n);
299
300/*
301 * provide emulation routines for smaller architectures
302 */
303#ifndef WORD64
304#define Data16(dpy, data, len) Data((dpy), (char *)(data), (len))
305#define Data32(dpy, data, len) Data((dpy), (char *)(data), (len))
306#define _FSRead16Pad(dpy, data, len) _FSReadPad((dpy), (char *)(data), (len))
307#define _FSRead16(dpy, data, len) _FSRead((dpy), (char *)(data), (len))
308#define _FSRead32(dpy, data, len) _FSRead((dpy), (char *)(data), (len))
309#endif /* not WORD64 */
310
311#define PackData16(dpy,data,len) Data16 (dpy, data, len)
312#define PackData32(dpy,data,len) Data32 (dpy, data, len)
313
314#define min(a,b) (((a) < (b)) ? (a) : (b))
315#define max(a,b) (((a) > (b)) ? (a) : (b))
316
317#ifdef MUSTCOPY
318/* a little bit of magic */
319#define OneDataCard32(svr,dstaddr,srcvar) \
320  { svr->bufptr -= 4; Data32 (svr, (char *) &(srcvar), 4); }
321
322#define STARTITERATE(tpvar,type,start,endcond,decr) \
323  { register char *cpvar; \
324  for (cpvar = (char *) start; endcond; cpvar = NEXTPTR(cpvar,type), decr) { \
325    type dummy; memmove ((char *) &dummy, cpvar, SIZEOF(type)); \
326    tpvar = (type *) cpvar;
327#define ENDITERATE }}
328
329#else
330
331/* srcvar must be a variable for large architecture version */
332#define OneDataCard32(svr,dstaddr,srcvar) \
333  { *(unsigned long *)(dstaddr) = (srcvar); }
334
335#define STARTITERATE(tpvar,type,start,endcond,decr) \
336  for (tpvar = (type *) start; endcond; tpvar++, decr) {
337#define ENDITERATE }
338#endif				/* MUSTCOPY - used machines whose C structs
339				 * don't line up with proto */
340
341
342#if !defined(UNIXCPP) || defined(ANSICPP)
343#define FSCat(x,y) x##_##y
344#else
345#define FSCat(x,y) x/**/_/**/y
346#endif
347
348/* copy XCharInfo parts of a protocol reply into a FSXCharInfo */
349
350#define FSUnpack_XCharInfo(packet, structure) \
351    (structure)->left = FSCat(packet,left); \
352    (structure)->right = FSCat(packet,right); \
353    (structure)->width = FSCat(packet,width); \
354    (structure)->ascent = FSCat(packet,ascent); \
355    (structure)->descent = FSCat(packet,descent); \
356    (structure)->attributes = FSCat(packet,attributes)
357
358
359/* copy XFontInfoHeader parts of a protocol reply into a FSXFontInfoHeader */
360
361#define FSUnpack_XFontInfoHeader(packet, structure, serverversion) \
362    (structure)->flags = (packet)->font_header_flags; \
363    (structure)->draw_direction = (packet)->font_header_draw_direction; \
364 \
365    if (serverversion > 1) { \
366	(structure)->char_range.min_char.high = (packet)->font_hdr_char_range_min_char_high; \
367	(structure)->char_range.min_char.low = (packet)->font_hdr_char_range_min_char_low; \
368	(structure)->char_range.max_char.high = (packet)->font_hdr_char_range_max_char_high; \
369	(structure)->char_range.max_char.low = (packet)->font_hdr_char_range_max_char_low; \
370	(structure)->default_char.high = (packet)->font_header_default_char_high; \
371	(structure)->default_char.low = (packet)->font_header_default_char_low; \
372    } else { \
373	(structure)->char_range.min_char.high = (packet)->font_hdr_char_range_min_char_low; \
374	(structure)->char_range.min_char.low = (packet)->font_hdr_char_range_min_char_high; \
375	(structure)->char_range.max_char.high = (packet)->font_hdr_char_range_max_char_low; \
376	(structure)->char_range.max_char.low = (packet)->font_hdr_char_range_max_char_high; \
377	(structure)->default_char.high = (packet)->font_header_default_char_low; \
378	(structure)->default_char.low = (packet)->font_header_default_char_high; \
379	} \
380 \
381    (structure)->font_ascent = (packet)->font_header_font_ascent; \
382    (structure)->font_descent = (packet)->font_header_font_descent; \
383 \
384    FSUnpack_XCharInfo((packet)->font_header_min_bounds, &(structure)->min_bounds); \
385    FSUnpack_XCharInfo((packet)->font_header_max_bounds, &(structure)->max_bounds)
386