utils.h revision 370d0a5e
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#ifdef HAVE_CONFIG_H
39#include "config.h"
40#endif
41
42#ifndef NUL
43#define	NUL	'\0'
44#endif
45
46/***====================================================================***/
47
48#ifndef OPAQUE_DEFINED
49typedef void *Opaque;
50#endif
51#ifndef NullOpaque
52#define	NullOpaque	((Opaque)NULL)
53#endif
54
55#ifndef BOOLEAN_DEFINED
56typedef char Boolean;
57#endif
58
59#ifndef True
60#define	True	((Boolean)1)
61#define	False	((Boolean)0)
62#endif /* ndef True */
63#define	booleanText(b)	((b)?"True":"False")
64
65#ifndef COMPARISON_DEFINED
66typedef int Comparison;
67
68#define	Greater		((Comparison)1)
69#define	Equal		((Comparison)0)
70#define	Less		((Comparison)-1)
71#define	CannotCompare	((Comparison)-37)
72#define	comparisonText(c)	((c)?((c)<0?"Less":"Greater"):"Equal")
73#endif
74
75/***====================================================================***/
76
77extern Opaque uAlloc(unsigned   /* size */
78    );
79extern Opaque uCalloc(unsigned /* n */ ,
80                      unsigned  /* size */
81    );
82extern Opaque uRealloc(Opaque /* old */ ,
83                       unsigned /* newSize */
84    );
85extern Opaque uRecalloc(Opaque /* old */ ,
86                        unsigned /* nOld */ ,
87                        unsigned /* nNew */ ,
88                        unsigned        /* newSize */
89    );
90extern void uFree(Opaque        /* ptr */
91    );
92
93#define	uTypedAlloc(t)		((t *)uAlloc((unsigned)sizeof(t)))
94#define	uTypedCalloc(n,t)	((t *)uCalloc((unsigned)n,(unsigned)sizeof(t)))
95#define	uTypedRealloc(pO,n,t)	((t *)uRealloc((Opaque)pO,((unsigned)n)*sizeof(t)))
96#define	uTypedRecalloc(pO,o,n,t) ((t *)uRecalloc((Opaque)pO,((unsigned)o),((unsigned)n),sizeof(t)))
97#if (defined mdHasAlloca) && (mdHasAlloca)
98#define	uTmpAlloc(n)	((Opaque)alloca((unsigned)n))
99#define	uTmpFree(p)
100#else
101#define	uTmpAlloc(n)	uAlloc(n)
102#define	uTmpFree(p)	uFree(p)
103#endif
104
105/***====================================================================***/
106
107extern Boolean uSetErrorFile(char *     /* name */
108    );
109
110#define INFO 			uInformation
111
112extern void
113uInformation(const char * /* s */ , ...
114    ) _X_ATTRIBUTE_PRINTF(1, 2);
115
116#define ACTION			uAction
117
118     extern void uAction(const char * /* s  */ , ...
119    ) _X_ATTRIBUTE_PRINTF(1, 2);
120
121#define WARN			uWarning
122
123     extern void uWarning(const char * /* s  */ , ...
124    ) _X_ATTRIBUTE_PRINTF(1, 2);
125
126#define ERROR			uError
127
128     extern void uError(const char * /* s  */ , ...
129    ) _X_ATTRIBUTE_PRINTF(1, 2);
130
131#define FATAL			uFatalError
132
133     extern void uFatalError(const char * /* s  */ , ...
134    ) _X_ATTRIBUTE_PRINTF(1, 2) _X_NORETURN;
135
136/* WSGO stands for "Weird Stuff Going On" */
137#define WSGO			uInternalError
138
139     extern void uInternalError(const char * /* s  */ , ...
140    ) _X_ATTRIBUTE_PRINTF(1, 2);
141
142     extern void uSetPreErrorMessage(char *     /* msg */
143    );
144
145     extern void uSetPostErrorMessage(char *    /* msg */
146    );
147
148     extern void uSetErrorPrefix(char * /* void */
149    );
150
151     extern void uFinishUp(void);
152
153
154/***====================================================================***/
155
156#define	NullString	((char *)NULL)
157
158#define	uStringText(s)		((s)==NullString?"<NullString>":(s))
159#define	uStringEqual(s1,s2)	(uStringCompare(s1,s2)==Equal)
160#define	uStringPrefix(p,s)	(strncmp(p,s,strlen(p))==0)
161#define	uStringCompare(s1,s2)	(((s1)==NullString||(s2)==NullString)?\
162                                 (s1)!=(s2):strcmp(s1,s2))
163#define	uStrCaseEqual(s1,s2)	(uStrCaseCmp(s1,s2)==0)
164#ifdef HAVE_STRCASECMP
165#include <strings.h>
166#define	uStrCaseCmp(s1,s2)	(strcasecmp(s1,s2))
167#define	uStrCasePrefix(p,s)	(strncasecmp(p,s,strlen(p))==0)
168#else
169     extern int uStrCaseCmp(const char * /* s1 */ ,
170                            const char *        /* s2 */
171    );
172     extern int uStrCasePrefix(const char * /* p */ ,
173                               char *   /* str */
174    );
175#endif
176#ifdef HAVE_STRDUP
177#include <string.h>
178#define	uStringDup(s1)		((s1) ? strdup(s1) : NULL)
179#else
180     extern char *uStringDup(const char *       /* s1 */
181    );
182#endif
183
184/***====================================================================***/
185
186#ifndef DEBUG_VAR
187#define	DEBUG_VAR	debugFlags
188#endif
189
190extern
191     unsigned int DEBUG_VAR;
192
193     extern void uDebug(char * /* s  */ , ...
194    ) _X_ATTRIBUTE_PRINTF(1, 2);
195
196     extern void uDebugNOI(     /* no indent */
197                              char * /* s  */ , ...
198    ) _X_ATTRIBUTE_PRINTF(1, 2);
199
200     extern Boolean uSetDebugFile(char *name);
201
202     extern FILE *uDebugFile;
203     extern int uDebugIndentLevel;
204     extern int uDebugIndentSize;
205#define	uDebugIndent(l)		(uDebugIndentLevel+=(l))
206#define	uDebugOutdent(l)	(uDebugIndentLevel-=(l))
207#ifdef DEBUG
208#define	uDEBUG(f,s)		{ if (DEBUG_VAR&(f)) uDebug(s);}
209#define	uDEBUG1(f,s,a)		{ if (DEBUG_VAR&(f)) uDebug(s,a);}
210#define	uDEBUG2(f,s,a,b)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b);}
211#define	uDEBUG3(f,s,a,b,c)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b,c);}
212#define	uDEBUG4(f,s,a,b,c,d)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b,c,d);}
213#define	uDEBUG5(f,s,a,b,c,d,e)	{ if (DEBUG_VAR&(f)) uDebug(s,a,b,c,d,e);}
214#define	uDEBUG_NOI(f,s)		{ if (DEBUG_VAR&(f)) uDebug(s);}
215#define	uDEBUG_NOI1(f,s,a)	{ if (DEBUG_VAR&(f)) uDebugNOI(s,a);}
216#define	uDEBUG_NOI2(f,s,a,b)	{ if (DEBUG_VAR&(f)) uDebugNOI(s,a,b);}
217#define	uDEBUG_NOI3(f,s,a,b,c)	{ if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c);}
218#define	uDEBUG_NOI4(f,s,a,b,c,d) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c,d);}
219#define	uDEBUG_NOI5(f,s,a,b,c,d,e) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c,d,e);}
220#else
221#define	uDEBUG(f,s)
222#define	uDEBUG1(f,s,a)
223#define	uDEBUG2(f,s,a,b)
224#define	uDEBUG3(f,s,a,b,c)
225#define	uDEBUG4(f,s,a,b,c,d)
226#define	uDEBUG5(f,s,a,b,c,d,e)
227#define	uDEBUG_NOI(f,s)
228#define	uDEBUG_NOI1(f,s,a)
229#define	uDEBUG_NOI2(f,s,a,b)
230#define	uDEBUG_NOI3(f,s,a,b,c)
231#define	uDEBUG_NOI4(f,s,a,b,c,d)
232#define	uDEBUG_NOI5(f,s,a,b,c,d,e)
233#endif
234
235#endif /* UTILS_H */
236