utils.h revision 34345a63
1#ifndef UTILS_H
2#define	UTILS_H 1
3
4  /*\
5   *
6   *                          COPYRIGHT 1990
7   *                    DIGITAL EQUIPMENT CORPORATION
8   *                       MAYNARD, MASSACHUSETTS
9   *                        ALL RIGHTS RESERVED.
10   *
11   * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
12   * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
13   * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
14   * FOR ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED
15   * WARRANTY.
16   *
17   * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
18   * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
19   * ADDITION TO THAT SET FORTH ABOVE.
20   *
21   * Permission to use, copy, modify, and distribute this software and its
22   * documentation for any purpose and without fee is hereby granted, provided
23   * that the above copyright notice appear in all copies and that both that
24   * copyright notice and this permission notice appear in supporting
25   * documentation, and that the name of Digital Equipment Corporation not be
26   * used in advertising or publicity pertaining to distribution of the
27   * software without specific, written prior permission.
28   \*/
29
30/***====================================================================***/
31
32#include 	<stdio.h>
33#include	<X11/Xos.h>
34#include	<X11/Xfuncproto.h>
35#include	<X11/Xfuncs.h>
36
37#include <stddef.h>
38#include "config.h"
39
40#ifndef NUL
41#define	NUL	'\0'
42#endif
43
44/***====================================================================***/
45
46#ifndef OPAQUE_DEFINED
47typedef void *Opaque;
48#endif
49#ifndef NullOpaque
50#define	NullOpaque	((Opaque)NULL)
51#endif
52
53#ifndef BOOLEAN_DEFINED
54typedef char Boolean;
55#endif
56
57#ifndef True
58#define	True	((Boolean)1)
59#define	False	((Boolean)0)
60#endif /* ndef True */
61#define	booleanText(b)	((b)?"True":"False")
62
63#ifndef COMPARISON_DEFINED
64typedef int Comparison;
65
66#define	Greater		((Comparison)1)
67#define	Equal		((Comparison)0)
68#define	Less		((Comparison)-1)
69#define	CannotCompare	((Comparison)-37)
70#define	comparisonText(c)	((c)?((c)<0?"Less":"Greater"):"Equal")
71#endif
72
73/***====================================================================***/
74
75extern Opaque uAlloc(unsigned   /* size */
76    );
77extern Opaque uCalloc(unsigned /* n */ ,
78                      unsigned  /* size */
79    );
80extern Opaque uRealloc(Opaque /* old */ ,
81                       unsigned /* newSize */
82    );
83extern Opaque uRecalloc(Opaque /* old */ ,
84                        unsigned /* nOld */ ,
85                        unsigned /* nNew */ ,
86                        unsigned        /* newSize */
87    );
88extern void uFree(Opaque        /* ptr */
89    );
90
91#define	uTypedAlloc(t)		((t *)uAlloc((unsigned)sizeof(t)))
92#define	uTypedCalloc(n,t)	((t *)uCalloc((unsigned)n,(unsigned)sizeof(t)))
93#define	uTypedRealloc(pO,n,t)	((t *)uRealloc((Opaque)pO,((unsigned)n)*sizeof(t)))
94#define	uTypedRecalloc(pO,o,n,t) ((t *)uRecalloc((Opaque)pO,((unsigned)o),((unsigned)n),sizeof(t)))
95#if (defined mdHasAlloca) && (mdHasAlloca)
96#define	uTmpAlloc(n)	((Opaque)alloca((unsigned)n))
97#define	uTmpFree(p)
98#else
99#define	uTmpAlloc(n)	uAlloc(n)
100#define	uTmpFree(p)	uFree(p)
101#endif
102
103/***====================================================================***/
104
105extern Boolean uSetErrorFile(char *     /* name */
106    );
107
108#define INFO6 			uInformation
109#define INFO5 			uInformation
110#define INFO4 			uInformation
111#define INFO3 			uInformation
112#define INFO2 			uInformation
113#define INFO1 			uInformation
114#define INFO 			uInformation
115
116extern void
117uInformation(const char * /* s */ , ...
118    )
119#if defined(__GNUC__) && \
120    ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 6)))
121    __attribute__ ((format(printf, 1, 2)))
122#endif
123    ;
124
125#define ACTION6			uAction
126#define ACTION5			uAction
127#define ACTION4			uAction
128#define ACTION3			uAction
129#define ACTION2			uAction
130#define ACTION1			uAction
131#define ACTION			uAction
132
133     extern void uAction(const char * /* s  */ , ...
134    )
135#if defined(__GNUC__) && \
136    ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 6)))
137    __attribute__ ((format(printf, 1, 2)))
138#endif
139    ;
140
141#define WARN6			uWarning
142#define WARN5			uWarning
143#define WARN4			uWarning
144#define WARN3			uWarning
145#define WARN2			uWarning
146#define WARN1			uWarning
147#define WARN			uWarning
148
149     extern void uWarning(const char * /* s  */ , ...
150    )
151#if defined(__GNUC__) && \
152    ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 6)))
153    __attribute__ ((format(printf, 1, 2)))
154#endif
155    ;
156
157#define ERROR6			uError
158#define ERROR5			uError
159#define ERROR4			uError
160#define ERROR3			uError
161#define ERROR2			uError
162#define ERROR1			uError
163#define ERROR			uError
164
165     extern void uError(const char * /* s  */ , ...
166    )
167#if defined(__GNUC__) && \
168    ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 6)))
169    __attribute__ ((format(printf, 1, 2)))
170#endif
171    ;
172
173#define FATAL6			uFatalError
174#define FATAL5			uFatalError
175#define FATAL4			uFatalError
176#define FATAL3			uFatalError
177#define FATAL2			uFatalError
178#define FATAL1			uFatalError
179#define FATAL			uFatalError
180
181     extern void uFatalError(const char * /* s  */ , ...
182    )
183#if defined(__GNUC__) && \
184    ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 6)))
185    __attribute__ ((format(printf, 1, 2)))
186#endif
187    ;
188
189/* WSGO stands for "Weird Stuff Going On" */
190#define WSGO6			uInternalError
191#define WSGO5			uInternalError
192#define WSGO4			uInternalError
193#define WSGO3			uInternalError
194#define WSGO2			uInternalError
195#define WSGO1			uInternalError
196#define WSGO			uInternalError
197
198     extern void uInternalError(const char * /* s  */ , ...
199    )
200#if defined(__GNUC__) && \
201    ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 6)))
202    __attribute__ ((format(printf, 1, 2)))
203#endif
204    ;
205
206     extern void uSetPreErrorMessage(char *     /* msg */
207    );
208
209     extern void uSetPostErrorMessage(char *    /* msg */
210    );
211
212     extern void uSetErrorPrefix(char * /* void */
213    );
214
215     extern void uFinishUp(void);
216
217
218/***====================================================================***/
219
220#define	NullString	((char *)NULL)
221
222#define	uStringText(s)		((s)==NullString?"<NullString>":(s))
223#define	uStringEqual(s1,s2)	(uStringCompare(s1,s2)==Equal)
224#define	uStringPrefix(p,s)	(strncmp(p,s,strlen(p))==0)
225#define	uStringCompare(s1,s2)	(((s1)==NullString||(s2)==NullString)?\
226                                 (s1)!=(s2):strcmp(s1,s2))
227#define	uStrCaseEqual(s1,s2)	(uStrCaseCmp(s1,s2)==0)
228#ifdef HAVE_STRCASECMP
229#define	uStrCaseCmp(s1,s2)	(strcasecmp(s1,s2))
230#define	uStrCasePrefix(p,s)	(strncasecmp(p,s,strlen(p))==0)
231#else
232     extern int uStrCaseCmp(const char * /* s1 */ ,
233                            const char *        /* s2 */
234    );
235     extern int uStrCasePrefix(const char * /* p */ ,
236                               char *   /* str */
237    );
238#endif
239#ifdef HAVE_STRDUP
240#define	uStringDup(s1)		((s1) ? strdup(s1) : NULL)
241#else
242     extern char *uStringDup(const char *       /* s1 */
243    );
244#endif
245
246/***====================================================================***/
247
248#ifdef	ASSERTIONS_ON
249#define	uASSERT(where,why) \
250	{if (!(why)) uFatalError("assertion botched in %s ( why )\n",where);}
251#else
252#define	uASSERT(where,why)
253#endif
254
255/***====================================================================***/
256
257#ifndef DEBUG_VAR
258#define	DEBUG_VAR	debugFlags
259#endif
260
261extern
262     unsigned int DEBUG_VAR;
263
264     extern void uDebug(char * /* s  */ , ...
265    )
266#if defined(__GNUC__) && \
267    ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 6)))
268    __attribute__ ((format(printf, 1, 2)))
269#endif
270    ;
271
272     extern void uDebugNOI(     /* no indent */
273                              char * /* s  */ , ...
274    )
275#if defined(__GNUC__) && \
276    ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 6)))
277    __attribute__ ((format(printf, 1, 2)))
278#endif
279    ;
280
281     extern Boolean uSetDebugFile(char *name);
282
283     extern FILE *uDebugFile;
284     extern int uDebugIndentLevel;
285     extern int uDebugIndentSize;
286#define	uDebugIndent(l)		(uDebugIndentLevel+=(l))
287#define	uDebugOutdent(l)	(uDebugIndentLevel-=(l))
288#ifdef DEBUG_ON
289#define	uDEBUG(f,s)		{ if (DEBUG_VAR&(f)) uDebug(s);}
290#define	uDEBUG1(f,s,a)		{ if (DEBUG_VAR&(f)) uDebug(s,a);}
291#define	uDEBUG2(f,s,a,b)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b);}
292#define	uDEBUG3(f,s,a,b,c)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b,c);}
293#define	uDEBUG4(f,s,a,b,c,d)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b,c,d);}
294#define	uDEBUG5(f,s,a,b,c,d,e)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b,c,d,e);}
295#define	uDEBUG_NOI(f,s)		{ if (DEBUG_VAR&(f)) uDebug(s);}
296#define	uDEBUG_NOI1(f,s,a)	{ if (DEBUG_VAR&(f)) uDebugNOI(s,a);}
297#define	uDEBUG_NOI2(f,s,a,b)	{ if (DEBUG_VAR&(f)) uDebugNOI(s,a,b);}
298#define	uDEBUG_NOI3(f,s,a,b,c)	{ if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c);}
299#define	uDEBUG_NOI4(f,s,a,b,c,d) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c,d);}
300#define	uDEBUG_NOI5(f,s,a,b,c,d,e) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c,d,e);}
301#else
302#define	uDEBUG(f,s)
303#define	uDEBUG1(f,s,a)
304#define	uDEBUG2(f,s,a,b)
305#define	uDEBUG3(f,s,a,b,c)
306#define	uDEBUG4(f,s,a,b,c,d)
307#define	uDEBUG5(f,s,a,b,c,d,e)
308#define	uDEBUG_NOI(f,s)
309#define	uDEBUG_NOI1(f,s,a)
310#define	uDEBUG_NOI2(f,s,a,b)
311#define	uDEBUG_NOI3(f,s,a,b,c)
312#define	uDEBUG_NOI4(f,s,a,b,c,d)
313#define	uDEBUG_NOI5(f,s,a,b,c,d,e)
314#endif
315
316     extern Boolean uSetEntryFile(char *name);
317     extern void uEntry(int /* l */ ,
318                        char * /* s  */ , ...
319    )
320#if defined(__GNUC__) && \
321    ((__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 6)))
322    __attribute__ ((format(printf, 2, 3)))
323#endif
324    ;
325
326     extern void uExit(int l, char *rtVal);
327#ifdef ENTRY_TRACKING_ON
328#define	ENTRY_BIT	0x10
329#define	LOW_ENTRY_BIT	0x1000
330#define	ENTER	(DEBUG_VAR&ENTRY_BIT)
331#define	FLAG(fLag)	(DEBUG_VAR&(fLag))
332
333     extern int uEntryLevel;
334
335#define	uENTRY(s)			{ if (ENTER) uEntry(1,s);}
336#define	uENTRY1(s,a)			{ if (ENTER) uEntry(1,s,a);}
337#define	uENTRY2(s,a,b)			{ if (ENTER) uEntry(1,s,a,b);}
338#define	uENTRY3(s,a,b,c)		{ if (ENTER) uEntry(1,s,a,b,c);}
339#define	uENTRY4(s,a,b,c,d)		{ if (ENTER) uEntry(1,s,a,b,c,d);}
340#define	uENTRY5(s,a,b,c,d,e)		{ if (ENTER) uEntry(1,s,a,b,c,d,e);}
341#define	uENTRY6(s,a,b,c,d,e,f)		{ if (ENTER) uEntry(1,s,a,b,c,d,e,f);}
342#define	uENTRY7(s,a,b,c,d,e,f,g)	{ if (ENTER) uEntry(1,s,a,b,c,d,e,f,g);}
343#define	uRETURN(v)			{ if (ENTER) uEntryLevel--; return(v); }
344#define	uVOIDRETURN			{ if (ENTER) uEntryLevel--; return; }
345
346#define	uFLAG_ENTRY(w,s)		{ if (FLAG(w)) uEntry(0,s);}
347#define	uFLAG_ENTRY1(w,s,a)		{ if (FLAG(w)) uEntry(0,s,a);}
348#define	uFLAG_ENTRY2(w,s,a,b)		{ if (FLAG(w)) uEntry(0,s,a,b);}
349#define	uFLAG_ENTRY3(w,s,a,b,c)		{ if (FLAG(w)) uEntry(0,s,a,b,c);}
350#define	uFLAG_ENTRY4(w,s,a,b,c,d)	{ if (FLAG(w)) uEntry(0,s,a,b,c,d);}
351#define	uFLAG_ENTRY5(w,s,a,b,c,d,e)	{ if (FLAG(w)) uEntry(0,s,a,b,c,d,e);}
352#define	uFLAG_ENTRY6(w,s,a,b,c,d,e,f)	{ if (FLAG(w)) uEntry(0,s,a,b,c,d,e,f);}
353#define	uFLAG_ENTRY7(w,s,a,b,c,d,e,f,g)	{ if(FLAG(w))uEntry(0,s,a,b,c,d,e,f,g);}
354#define	uFLAG_RETURN(v)			{ return(v);}
355#define	uFLAG_VOIDRETURN		{ return; }
356#else
357#define	uENTRY(s)
358#define	uENTRY1(s,a)
359#define	uENTRY2(s,a1,a2)
360#define	uENTRY3(s,a1,a2,a3)
361#define	uENTRY4(s,a1,a2,a3,a4)
362#define	uENTRY5(s,a1,a2,a3,a4,a5)
363#define	uENTRY6(s,a1,a2,a3,a4,a5,a6)
364#define	uENTRY7(s,a1,a2,a3,a4,a5,a6,a7)
365#define	uRETURN(v)	{ return(v); }
366#define	uVOIDRETURN	{ return; }
367
368#define	uFLAG_ENTRY(f,s)
369#define	uFLAG_ENTRY1(f,s,a)
370#define	uFLAG_ENTRY2(f,s,a,b)
371#define	uFLAG_ENTRY3(f,s,a,b,c)
372#define	uFLAG_ENTRY4(f,s,a,b,c,d)
373#define	uFLAG_ENTRY5(f,s,a,b,c,d,e)
374#define	uFLAG_ENTRY6(f,s,a,b,c,d,e,g)
375#define	uFLAG_ENTRY7(f,s,a,b,c,d,e,g,h)
376#define	uFLAG_RETURN(v)			{ return(v);}
377#define	uFLAG_VOIDRETURN		{ return; }
378#endif
379
380
381#endif /* UTILS_H */
382