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 unsigned long _FSSetLastRequestRead ( FSServer *svr,
86					     fsGenericReply *rep );
87extern int _FSUnknownWireEvent ( FSServer *svr, FSEvent *re, fsEvent *event );
88extern int _FSUnknownNativeEvent ( FSServer *svr, FSEvent *re,
89				   fsEvent *event );
90extern int _FSDefaultIOError ( FSServer *svr ) _X_NORETURN;
91extern int _FSPrintDefaultError ( FSServer *svr, FSErrorEvent *event,
92				  FILE *fp );
93extern int _FSDefaultError ( FSServer *svr, FSErrorEvent *event );
94extern void _FSFreeQ ( void );
95
96extern FSErrorHandler  FSSetErrorHandler ( FSErrorHandler handler );
97extern FSIOErrorHandler FSSetIOErrorHandler ( FSIOErrorHandler handler );
98
99extern _FSQEvent *_FSqfree;
100extern FSServer *_FSHeadOfServerList;
101
102#ifndef BUFSIZE
103#define BUFSIZE 2048		/* FS output buffer size. */
104#endif
105
106/*
107 * server flags
108 */
109#define	FSlibServerIOError	(1L << 0)
110#define	FSlibServerClosing	(1L << 1)
111
112
113/* FSMaxRequestBytes - returns FSMaxRequestSize converted to bytes */
114#define FSMaxRequestBytes(svr)	((svr)->max_request_size << 2)
115
116/*
117 * GetReq - Get the next available FS request packet in the buffer and
118 * return it.
119 *
120 * "name" is the name of the request, e.g. InternAtom, OpenFont, etc.
121 * "req" is the name of the request pointer.
122 *
123 */
124
125#define GetReq(name, req) \
126	if ((svr->bufptr + SIZEOF(fs##name##Req)) > svr->bufmax)\
127		_FSFlush(svr);\
128	req = (fs##name##Req *)(svr->last_req = svr->bufptr);\
129	req->reqType = FS_##name;\
130	req->length = (SIZEOF(fs##name##Req))>>2;\
131	svr->bufptr += SIZEOF(fs##name##Req);\
132	svr->request++
133
134/* GetReqExtra is the same as GetReq, but allocates "n" additional
135   bytes after the request. "n" must be a multiple of 4!  */
136
137#define GetReqExtra(name, n, req) \
138	if ((svr->bufptr + SIZEOF(fs##name##Req) + n) > svr->bufmax)\
139		_FSFlush(svr);\
140	req = (fs##name##Req *)(svr->last_req = svr->bufptr);\
141	req->reqType = FS_##name;\
142	req->length = (SIZEOF(fs##name##Req) + n)>>2;\
143	svr->bufptr += SIZEOF(fs##name##Req) + n;\
144	svr->request++
145
146/*
147 * GetResReq is for those requests that have a resource ID
148 * (Window, Pixmap, GContext, etc.) as their single argument.
149 * "rid" is the name of the resource.
150 */
151
152#define GetResReq(name, rid, req) \
153	if ((svr->bufptr + SIZEOF(fsResourceReq)) > svr->bufmax)\
154	    _FSFlush(svr);\
155	req = (fsResourceReq *) (svr->last_req = svr->bufptr);\
156	req->reqType = FS_##name;\
157	req->length = 2;\
158	req->id = (CARD32) (rid);\
159	svr->bufptr += SIZEOF(fsResourceReq);\
160	svr->request++
161
162/*
163 * GetEmptyReq is for those requests that have no arguments
164 * at all.
165 */
166
167#define GetEmptyReq(name, req) \
168	if ((svr->bufptr + SIZEOF(fsReq)) > svr->bufmax)\
169	    _FSFlush(svr);\
170	req = (fsReq *) (svr->last_req = svr->bufptr);\
171	req->reqType = FS_##name;\
172	req->length = 1;\
173	svr->bufptr += SIZEOF(fsReq);\
174	svr->request++
175
176#define	SyncHandle()	\
177	if (svr->synchandler) (*svr->synchandler)(svr)
178
179/*
180 * Data - Place data in the buffer and pad the end to provide
181 * 32 bit word alignment.  Transmit if the buffer fills.
182 *
183 * "svr" is a pointer to a Display.
184 * "data" is a pinter to a data buffer.
185 * "len" is the length of the data buffer.
186 * we can presume buffer less than 2^16 bytes, so bcopy can be used safely.
187 */
188
189#ifdef DataRoutineIsProcedure
190extern void Data();
191
192#else
193#define Data(svr, data, len) \
194	if (svr->bufptr + (len) <= svr->bufmax) {\
195		memmove(svr->bufptr, data, len);\
196		svr->bufptr += ((len) + 3) & ~3;\
197	} else\
198		_FSSend(svr, data, len)
199#endif				/* DataRoutineIsProcedure */
200
201
202/* Allocate bytes from the buffer.  No padding is done, so if
203 * the length is not a multiple of 4, the caller must be
204 * careful to leave the buffer aligned after sending the
205 * current request.
206 *
207 * "type" is the type of the pointer being assigned to.
208 * "ptr" is the pointer being assigned to.
209 * "n" is the number of bytes to allocate.
210 *
211 * Example:
212 *    xTextElt *elt;
213 *    BufAlloc (xTextElt *, elt, nbytes)
214 */
215
216#define BufAlloc(type, ptr, n) \
217    if (svr->bufptr + (n) > svr->bufmax) \
218        _FSFlush (svr); \
219    ptr = (type) svr->bufptr; \
220    svr->bufptr += (n);
221
222/*
223 * provide emulation routines for smaller architectures
224 */
225#define Data16(dpy, data, len) Data((dpy), (char *)(data), (len))
226#define Data32(dpy, data, len) Data((dpy), (char *)(data), (len))
227#define _FSRead16Pad(dpy, data, len) _FSReadPad((dpy), (char *)(data), (len))
228#define _FSRead16(dpy, data, len) _FSRead((dpy), (char *)(data), (len))
229#define _FSRead32(dpy, data, len) _FSRead((dpy), (char *)(data), (len))
230
231#define PackData16(dpy,data,len) Data16 (dpy, data, len)
232#define PackData32(dpy,data,len) Data32 (dpy, data, len)
233
234#define min(a,b) (((a) < (b)) ? (a) : (b))
235#define max(a,b) (((a) > (b)) ? (a) : (b))
236
237/* srcvar must be a variable for large architecture version */
238#define OneDataCard32(svr,dstaddr,srcvar) \
239  { *(unsigned long *)(dstaddr) = (srcvar); }
240
241#define STARTITERATE(tpvar,type,start,endcond,decr) \
242  for (tpvar = (type *) start; endcond; tpvar++, decr) {
243#define ENDITERATE }
244
245
246#define FSCat(x,y) x##_##y
247
248/* copy XCharInfo parts of a protocol reply into a FSXCharInfo */
249
250#define FSUnpack_XCharInfo(packet, structure) \
251    (structure)->left = FSCat(packet,left); \
252    (structure)->right = FSCat(packet,right); \
253    (structure)->width = FSCat(packet,width); \
254    (structure)->ascent = FSCat(packet,ascent); \
255    (structure)->descent = FSCat(packet,descent); \
256    (structure)->attributes = FSCat(packet,attributes)
257
258
259/* copy XFontInfoHeader parts of a protocol reply into a FSXFontInfoHeader */
260
261#define FSUnpack_XFontInfoHeader(packet, structure, serverversion) \
262    (structure)->flags = (packet)->font_header_flags; \
263    (structure)->draw_direction = (packet)->font_header_draw_direction; \
264 \
265    if (serverversion > 1) { \
266	(structure)->char_range.min_char.high = (packet)->font_hdr_char_range_min_char_high; \
267	(structure)->char_range.min_char.low = (packet)->font_hdr_char_range_min_char_low; \
268	(structure)->char_range.max_char.high = (packet)->font_hdr_char_range_max_char_high; \
269	(structure)->char_range.max_char.low = (packet)->font_hdr_char_range_max_char_low; \
270	(structure)->default_char.high = (packet)->font_header_default_char_high; \
271	(structure)->default_char.low = (packet)->font_header_default_char_low; \
272    } else { \
273	(structure)->char_range.min_char.high = (packet)->font_hdr_char_range_min_char_low; \
274	(structure)->char_range.min_char.low = (packet)->font_hdr_char_range_min_char_high; \
275	(structure)->char_range.max_char.high = (packet)->font_hdr_char_range_max_char_low; \
276	(structure)->char_range.max_char.low = (packet)->font_hdr_char_range_max_char_high; \
277	(structure)->default_char.high = (packet)->font_header_default_char_low; \
278	(structure)->default_char.low = (packet)->font_header_default_char_high; \
279	} \
280 \
281    (structure)->font_ascent = (packet)->font_header_font_ascent; \
282    (structure)->font_descent = (packet)->font_header_font_descent; \
283 \
284    FSUnpack_XCharInfo((packet)->font_header_min_bounds, &(structure)->min_bounds); \
285    FSUnpack_XCharInfo((packet)->font_header_max_bounds, &(structure)->max_bounds)
286