utils.h revision b3eb03f3
1#ifndef UTILS_H
2#define	UTILS_H 1
3
4  /*\
5   * $Xorg: utils.h,v 1.3 2000/08/17 19:54:51 cpqbld Exp $
6   *
7   *		              COPYRIGHT 1990
8   *		        DIGITAL EQUIPMENT CORPORATION
9   *		           MAYNARD, MASSACHUSETTS
10   *			    ALL RIGHTS RESERVED.
11   *
12   * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
13   * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
14   * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
15   * FOR ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED
16   * WARRANTY.
17   *
18   * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
19   * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
20   * ADDITION TO THAT SET FORTH ABOVE.
21   *
22   * Permission to use, copy, modify, and distribute this software and its
23   * documentation for any purpose and without fee is hereby granted, provided
24   * that the above copyright notice appear in all copies and that both that
25   * copyright notice and this permission notice appear in supporting
26   * documentation, and that the name of Digital Equipment Corporation not be
27   * used in advertising or publicity pertaining to distribution of the
28   * software without specific, written prior permission.
29  \*/
30/* $XFree86: xc/programs/xkbutils/utils.h,v 1.5 2001/07/25 15:05:26 dawes Exp $ */
31
32/***====================================================================***/
33
34#include 	<stdio.h>
35#include	<X11/Xos.h>
36#include	<X11/Xfuncproto.h>
37#include	<X11/Xfuncs.h>
38#include 	<stdarg.h>
39#include	<stddef.h>
40
41_XFUNCPROTOBEGIN
42
43
44#ifndef NUL
45#define	NUL	'\0'
46#endif
47
48/***====================================================================***/
49
50#ifndef OPAQUE_DEFINED
51typedef	void *Opaque;
52#endif
53#ifndef NullOpaque
54#define	NullOpaque	((Opaque)NULL)
55#endif
56
57#ifndef BOOLEAN_DEFINED
58typedef	char	Boolean;
59#endif
60
61#ifndef True
62#define	True	((Boolean)1)
63#define	False	((Boolean)0)
64#endif /* ndef True */
65#define	booleanText(b)	((b)?"True":"False")
66
67#ifndef COMPARISON_DEFINED
68typedef	int		Comparison;
69
70#define	Greater		((Comparison)1)
71#define	Equal		((Comparison)0)
72#define	Less		((Comparison)-1)
73#define	CannotCompare	((Comparison)-37)
74#define	comparisonText(c)	((c)?((c)<0?"Less":"Greater"):"Equal")
75#endif
76
77#ifdef notyet
78typedef union {
79	int		 i;
80	unsigned	 u;
81	void		*p;
82	void		*(*fp)();
83} Union;
84#endif
85
86/***====================================================================***/
87
88extern	Opaque	uAlloc(
89    unsigned	/* size */
90);
91extern	Opaque	uCalloc(
92    unsigned	/* n */,
93    unsigned	/* size */
94);
95extern	Opaque	uRealloc(
96    Opaque	/* old */,
97    unsigned	/* newSize */
98);
99extern	Opaque	uRecalloc(
100    Opaque 	/* old */,
101    unsigned	/* nOld */,
102    unsigned	/* nNew */,
103    unsigned 	/* newSize */
104);
105extern	void	uFree(
106    Opaque	/* ptr */
107);
108
109#define	uTypedAlloc(t)		((t *)uAlloc((unsigned)sizeof(t)))
110#define	uTypedCalloc(n,t)	((t *)uCalloc((unsigned)n,(unsigned)sizeof(t)))
111#define	uTypedRealloc(pO,n,t)	((t *)uRealloc((Opaque)pO,((unsigned)n)*sizeof(t)))
112#define	uTypedRecalloc(pO,o,n,t) ((t *)uRecalloc((Opaque)pO,((unsigned)o),((unsigned)n),sizeof(t)))
113#if (defined mdHasAlloca) && (mdHasAlloca)
114#define	uTmpAlloc(n)	((Opaque)alloca((unsigned)n))
115#define	uTmpFree(p)
116#else
117#define	uTmpAlloc(n)	uAlloc(n)
118#define	uTmpFree(p)	uFree(p)
119#endif
120
121/***====================================================================***/
122
123extern Boolean uSetErrorFile ( char *name );
124extern void uInformation ( char *s, ...);
125extern void uAction ( char *s, ... );
126extern void uWarning ( char *s, ... );
127extern void uError ( char *s, ... );
128extern void uFatalError(char *s,...);
129extern void uInternalError ( char *s, ... );
130
131/***====================================================================***/
132
133#define	NullString	((char *)NULL)
134
135#define	uStringText(s)		((s)==NullString?"<NullString>":(s))
136#define	uStringEqual(s1,s2)	(uStringCompare(s1,s2)==Equal)
137#define	uStringPrefix(p,s)	(strncmp(p,s,strlen(p))==0)
138#define	uStringCompare(s1,s2)	(strcmp(s1,s2))
139#define	uStrCaseEqual(s1,s2)	(uStrCaseCmp(s1,s2)==0)
140#ifdef HAVE_STRCASECMP
141#define	uStrCaseCmp(s1,s2)	(strcasecmp(s1,s2))
142#define	uStrCasePrefix(p,s)	(strncasecmp(p,s,strlen(p))==0)
143#else
144extern	int uStrCaseCmp(
145	char *	/* s1 */,
146	char *	/* s2 */
147);
148extern	int uStrCasePrefix(
149	char *	/* p */,
150	char *	/* str */
151);
152#endif
153#ifdef HAVE_STRDUP
154#define	uStringDup(s1)		(strdup(s1))
155#else
156extern	char *uStringDup(
157	char *	/* s1 */
158);
159#endif
160
161/***====================================================================***/
162
163#ifdef	ASSERTIONS_ON
164#define	uASSERT(where,why) \
165	{if (!(why)) uFatalError("assertion botched in %s ( why )\n",where);}
166#else
167#define	uASSERT(where,why)
168#endif
169
170/***====================================================================***/
171
172#ifndef DEBUG_VAR
173#define	DEBUG_VAR	debugFlags
174#endif
175
176extern
177unsigned	int	DEBUG_VAR;
178
179extern	void	uDebug( char *s, ... );
180extern	void	uDebugNOI( char *s, ... );	/* no indent */
181extern	Boolean	uSetDebugFile(
182    char *name
183);
184extern	FILE	*uDebugFile;
185extern	int	uDebugIndentLevel;
186extern	int	uDebugIndentSize;
187#define	uDebugIndent(l)		(uDebugIndentLevel+=(l))
188#define	uDebugOutdent(l)	(uDebugIndentLevel-=(l))
189#ifdef DEBUG_ON
190#define	uDEBUG(f,s)		{ if (DEBUG_VAR&(f)) uDebug(s);}
191#define	uDEBUG1(f,s,a)		{ if (DEBUG_VAR&(f)) uDebug(s,a);}
192#define	uDEBUG2(f,s,a,b)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b);}
193#define	uDEBUG3(f,s,a,b,c)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b,c);}
194#define	uDEBUG4(f,s,a,b,c,d)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b,c,d);}
195#define	uDEBUG5(f,s,a,b,c,d,e)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b,c,d,e);}
196#define	uDEBUG_NOI(f,s)		{ if (DEBUG_VAR&(f)) uDebug(s);}
197#define	uDEBUG_NOI1(f,s,a)	{ if (DEBUG_VAR&(f)) uDebugNOI(s,a);}
198#define	uDEBUG_NOI2(f,s,a,b)	{ if (DEBUG_VAR&(f)) uDebugNOI(s,a,b);}
199#define	uDEBUG_NOI3(f,s,a,b,c)	{ if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c);}
200#define	uDEBUG_NOI4(f,s,a,b,c,d) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c,d);}
201#define	uDEBUG_NOI5(f,s,a,b,c,d,e) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c,d,e);}
202#else
203#define	uDEBUG(f,s)
204#define	uDEBUG1(f,s,a)
205#define	uDEBUG2(f,s,a,b)
206#define	uDEBUG3(f,s,a,b,c)
207#define	uDEBUG4(f,s,a,b,c,d)
208#define	uDEBUG5(f,s,a,b,c,d,e)
209#define	uDEBUG_NOI(f,s)
210#define	uDEBUG_NOI1(f,s,a)
211#define	uDEBUG_NOI2(f,s,a,b)
212#define	uDEBUG_NOI3(f,s,a,b,c)
213#define	uDEBUG_NOI4(f,s,a,b,c,d)
214#define	uDEBUG_NOI5(f,s,a,b,c,d,e)
215#endif
216
217extern	Boolean	uSetEntryFile(
218    char *name
219);
220extern	void	uEntry(int l, char *s, ... );
221extern	void	uExit(
222    int l,char *rtVal
223);
224
225extern	int	uEntryLevel;
226#ifdef ENTRY_TRACKING_ON
227#define	ENTRY_BIT	0x10
228#define	LOW_ENTRY_BIT	0x1000
229#define	ENTER	(DEBUG_VAR&ENTRY_BIT)
230#define	FLAG(fLag)	(DEBUG_VAR&(fLag))
231
232#define	uENTRY(s)			{ if (ENTER) uEntry(1,s);}
233#define	uENTRY1(s,a)			{ if (ENTER) uEntry(1,s,a);}
234#define	uENTRY2(s,a,b)			{ if (ENTER) uEntry(1,s,a,b);}
235#define	uENTRY3(s,a,b,c)		{ if (ENTER) uEntry(1,s,a,b,c);}
236#define	uENTRY4(s,a,b,c,d)		{ if (ENTER) uEntry(1,s,a,b,c,d);}
237#define	uENTRY5(s,a,b,c,d,e)		{ if (ENTER) uEntry(1,s,a,b,c,d,e);}
238#define	uENTRY6(s,a,b,c,d,e,f)		{ if (ENTER) uEntry(1,s,a,b,c,d,e,f);}
239#define	uENTRY7(s,a,b,c,d,e,f,g)	{ if (ENTER) uEntry(1,s,a,b,c,d,e,f,g);}
240#define	uRETURN(v)			{ if (ENTER) uEntryLevel--; return(v); }
241#define	uVOIDRETURN			{ if (ENTER) uEntryLevel--; return; }
242
243#define	uFLAG_ENTRY(w,s)		{ if (FLAG(w)) uEntry(0,s);}
244#define	uFLAG_ENTRY1(w,s,a)		{ if (FLAG(w)) uEntry(0,s,a);}
245#define	uFLAG_ENTRY2(w,s,a,b)		{ if (FLAG(w)) uEntry(0,s,a,b);}
246#define	uFLAG_ENTRY3(w,s,a,b,c)		{ if (FLAG(w)) uEntry(0,s,a,b,c);}
247#define	uFLAG_ENTRY4(w,s,a,b,c,d)	{ if (FLAG(w)) uEntry(0,s,a,b,c,d);}
248#define	uFLAG_ENTRY5(w,s,a,b,c,d,e)	{ if (FLAG(w)) uEntry(0,s,a,b,c,d,e);}
249#define	uFLAG_ENTRY6(w,s,a,b,c,d,e,f)	{ if (FLAG(w)) uEntry(0,s,a,b,c,d,e,f);}
250#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);}
251#define	uFLAG_RETURN(v)			{ return(v);}
252#define	uFLAG_VOIDRETURN		{ return; }
253#else
254#define	uENTRY(s)
255#define	uENTRY1(s,a)
256#define	uENTRY2(s,a1,a2)
257#define	uENTRY3(s,a1,a2,a3)
258#define	uENTRY4(s,a1,a2,a3,a4)
259#define	uENTRY5(s,a1,a2,a3,a4,a5)
260#define	uENTRY6(s,a1,a2,a3,a4,a5,a6)
261#define	uENTRY7(s,a1,a2,a3,a4,a5,a6,a7)
262#define	uRETURN(v)	{ return(v); }
263#define	uVOIDRETURN	{ return; }
264
265#define	uFLAG_ENTRY(f,s)
266#define	uFLAG_ENTRY1(f,s,a)
267#define	uFLAG_ENTRY2(f,s,a,b)
268#define	uFLAG_ENTRY3(f,s,a,b,c)
269#define	uFLAG_ENTRY4(f,s,a,b,c,d)
270#define	uFLAG_ENTRY5(f,s,a,b,c,d,e)
271#define	uFLAG_ENTRY6(f,s,a,b,c,d,e,g)
272#define	uFLAG_ENTRY7(f,s,a,b,c,d,e,g,h)
273#define	uFLAG_RETURN(v)			{ return(v);}
274#define	uFLAG_VOIDRETURN		{ return; }
275#endif
276
277_XFUNCPROTOEND
278
279#endif /* UTILS_H */
280
281
282