utils.h revision b091287f
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 INFO6 uInformation 111#define INFO5 uInformation 112#define INFO4 uInformation 113#define INFO3 uInformation 114#define INFO2 uInformation 115#define INFO1 uInformation 116#define INFO uInformation 117 118extern void 119uInformation(const char * /* s */ , ... 120 ) _X_ATTRIBUTE_PRINTF(1, 2); 121 122#define ACTION6 uAction 123#define ACTION5 uAction 124#define ACTION4 uAction 125#define ACTION3 uAction 126#define ACTION2 uAction 127#define ACTION1 uAction 128#define ACTION uAction 129 130 extern void uAction(const char * /* s */ , ... 131 ) _X_ATTRIBUTE_PRINTF(1, 2); 132 133#define WARN6 uWarning 134#define WARN5 uWarning 135#define WARN4 uWarning 136#define WARN3 uWarning 137#define WARN2 uWarning 138#define WARN1 uWarning 139#define WARN uWarning 140 141 extern void uWarning(const char * /* s */ , ... 142 ) _X_ATTRIBUTE_PRINTF(1, 2); 143 144#define ERROR6 uError 145#define ERROR5 uError 146#define ERROR4 uError 147#define ERROR3 uError 148#define ERROR2 uError 149#define ERROR1 uError 150#define ERROR uError 151 152 extern void uError(const char * /* s */ , ... 153 ) _X_ATTRIBUTE_PRINTF(1, 2); 154 155#define FATAL6 uFatalError 156#define FATAL5 uFatalError 157#define FATAL4 uFatalError 158#define FATAL3 uFatalError 159#define FATAL2 uFatalError 160#define FATAL1 uFatalError 161#define FATAL uFatalError 162 163 extern void uFatalError(const char * /* s */ , ... 164 ) _X_ATTRIBUTE_PRINTF(1, 2) _X_NORETURN; 165 166/* WSGO stands for "Weird Stuff Going On" */ 167#define WSGO6 uInternalError 168#define WSGO5 uInternalError 169#define WSGO4 uInternalError 170#define WSGO3 uInternalError 171#define WSGO2 uInternalError 172#define WSGO1 uInternalError 173#define WSGO uInternalError 174 175 extern void uInternalError(const char * /* s */ , ... 176 ) _X_ATTRIBUTE_PRINTF(1, 2); 177 178 extern void uSetPreErrorMessage(char * /* msg */ 179 ); 180 181 extern void uSetPostErrorMessage(char * /* msg */ 182 ); 183 184 extern void uSetErrorPrefix(char * /* void */ 185 ); 186 187 extern void uFinishUp(void); 188 189 190/***====================================================================***/ 191 192#define NullString ((char *)NULL) 193 194#define uStringText(s) ((s)==NullString?"<NullString>":(s)) 195#define uStringEqual(s1,s2) (uStringCompare(s1,s2)==Equal) 196#define uStringPrefix(p,s) (strncmp(p,s,strlen(p))==0) 197#define uStringCompare(s1,s2) (((s1)==NullString||(s2)==NullString)?\ 198 (s1)!=(s2):strcmp(s1,s2)) 199#define uStrCaseEqual(s1,s2) (uStrCaseCmp(s1,s2)==0) 200#ifdef HAVE_STRCASECMP 201#include <strings.h> 202#define uStrCaseCmp(s1,s2) (strcasecmp(s1,s2)) 203#define uStrCasePrefix(p,s) (strncasecmp(p,s,strlen(p))==0) 204#else 205 extern int uStrCaseCmp(const char * /* s1 */ , 206 const char * /* s2 */ 207 ); 208 extern int uStrCasePrefix(const char * /* p */ , 209 char * /* str */ 210 ); 211#endif 212#ifdef HAVE_STRDUP 213#include <string.h> 214#define uStringDup(s1) ((s1) ? strdup(s1) : NULL) 215#else 216 extern char *uStringDup(const char * /* s1 */ 217 ); 218#endif 219 220/***====================================================================***/ 221 222#ifdef ASSERTIONS_ON 223#define uASSERT(where,why) \ 224 {if (!(why)) uFatalError("assertion botched in %s ( why )\n",where);} 225#else 226#define uASSERT(where,why) 227#endif 228 229/***====================================================================***/ 230 231#ifndef DEBUG_VAR 232#define DEBUG_VAR debugFlags 233#endif 234 235extern 236 unsigned int DEBUG_VAR; 237 238 extern void uDebug(char * /* s */ , ... 239 ) _X_ATTRIBUTE_PRINTF(1, 2); 240 241 extern void uDebugNOI( /* no indent */ 242 char * /* s */ , ... 243 ) _X_ATTRIBUTE_PRINTF(1, 2); 244 245 extern Boolean uSetDebugFile(char *name); 246 247 extern FILE *uDebugFile; 248 extern int uDebugIndentLevel; 249 extern int uDebugIndentSize; 250#define uDebugIndent(l) (uDebugIndentLevel+=(l)) 251#define uDebugOutdent(l) (uDebugIndentLevel-=(l)) 252#ifdef DEBUG_ON 253#define uDEBUG(f,s) { if (DEBUG_VAR&(f)) uDebug(s);} 254#define uDEBUG1(f,s,a) { if (DEBUG_VAR&(f)) uDebug(s,a);} 255#define uDEBUG2(f,s,a,b) { if (DEBUG_VAR&(f)) uDebug(s,a,b);} 256#define uDEBUG3(f,s,a,b,c) { if (DEBUG_VAR&(f)) uDebug(s,a,b,c);} 257#define uDEBUG4(f,s,a,b,c,d) { if (DEBUG_VAR&(f)) uDebug(s,a,b,c,d);} 258#define uDEBUG5(f,s,a,b,c,d,e) { if (DEBUG_VAR&(f)) uDebug(s,a,b,c,d,e);} 259#define uDEBUG_NOI(f,s) { if (DEBUG_VAR&(f)) uDebug(s);} 260#define uDEBUG_NOI1(f,s,a) { if (DEBUG_VAR&(f)) uDebugNOI(s,a);} 261#define uDEBUG_NOI2(f,s,a,b) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b);} 262#define uDEBUG_NOI3(f,s,a,b,c) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c);} 263#define uDEBUG_NOI4(f,s,a,b,c,d) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c,d);} 264#define uDEBUG_NOI5(f,s,a,b,c,d,e) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c,d,e);} 265#else 266#define uDEBUG(f,s) 267#define uDEBUG1(f,s,a) 268#define uDEBUG2(f,s,a,b) 269#define uDEBUG3(f,s,a,b,c) 270#define uDEBUG4(f,s,a,b,c,d) 271#define uDEBUG5(f,s,a,b,c,d,e) 272#define uDEBUG_NOI(f,s) 273#define uDEBUG_NOI1(f,s,a) 274#define uDEBUG_NOI2(f,s,a,b) 275#define uDEBUG_NOI3(f,s,a,b,c) 276#define uDEBUG_NOI4(f,s,a,b,c,d) 277#define uDEBUG_NOI5(f,s,a,b,c,d,e) 278#endif 279 280 extern Boolean uSetEntryFile(char *name); 281 extern void uEntry(int /* l */ , 282 char * /* s */ , ... 283 ) _X_ATTRIBUTE_PRINTF(2, 3); 284 285 extern void uExit(int l, char *rtVal); 286#ifdef ENTRY_TRACKING_ON 287#define ENTRY_BIT 0x10 288#define LOW_ENTRY_BIT 0x1000 289#define ENTER (DEBUG_VAR&ENTRY_BIT) 290#define FLAG(fLag) (DEBUG_VAR&(fLag)) 291 292 extern int uEntryLevel; 293 294#define uENTRY(s) { if (ENTER) uEntry(1,s);} 295#define uENTRY1(s,a) { if (ENTER) uEntry(1,s,a);} 296#define uENTRY2(s,a,b) { if (ENTER) uEntry(1,s,a,b);} 297#define uENTRY3(s,a,b,c) { if (ENTER) uEntry(1,s,a,b,c);} 298#define uENTRY4(s,a,b,c,d) { if (ENTER) uEntry(1,s,a,b,c,d);} 299#define uENTRY5(s,a,b,c,d,e) { if (ENTER) uEntry(1,s,a,b,c,d,e);} 300#define uENTRY6(s,a,b,c,d,e,f) { if (ENTER) uEntry(1,s,a,b,c,d,e,f);} 301#define uENTRY7(s,a,b,c,d,e,f,g) { if (ENTER) uEntry(1,s,a,b,c,d,e,f,g);} 302#define uRETURN(v) { if (ENTER) uEntryLevel--; return(v); } 303#define uVOIDRETURN { if (ENTER) uEntryLevel--; return; } 304 305#define uFLAG_ENTRY(w,s) { if (FLAG(w)) uEntry(0,s);} 306#define uFLAG_ENTRY1(w,s,a) { if (FLAG(w)) uEntry(0,s,a);} 307#define uFLAG_ENTRY2(w,s,a,b) { if (FLAG(w)) uEntry(0,s,a,b);} 308#define uFLAG_ENTRY3(w,s,a,b,c) { if (FLAG(w)) uEntry(0,s,a,b,c);} 309#define uFLAG_ENTRY4(w,s,a,b,c,d) { if (FLAG(w)) uEntry(0,s,a,b,c,d);} 310#define uFLAG_ENTRY5(w,s,a,b,c,d,e) { if (FLAG(w)) uEntry(0,s,a,b,c,d,e);} 311#define uFLAG_ENTRY6(w,s,a,b,c,d,e,f) { if (FLAG(w)) uEntry(0,s,a,b,c,d,e,f);} 312#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);} 313#define uFLAG_RETURN(v) { return(v);} 314#define uFLAG_VOIDRETURN { return; } 315#else 316#define uENTRY(s) 317#define uENTRY1(s,a) 318#define uENTRY2(s,a1,a2) 319#define uENTRY3(s,a1,a2,a3) 320#define uENTRY4(s,a1,a2,a3,a4) 321#define uENTRY5(s,a1,a2,a3,a4,a5) 322#define uENTRY6(s,a1,a2,a3,a4,a5,a6) 323#define uENTRY7(s,a1,a2,a3,a4,a5,a6,a7) 324#define uRETURN(v) { return(v); } 325#define uVOIDRETURN { return; } 326 327#define uFLAG_ENTRY(f,s) 328#define uFLAG_ENTRY1(f,s,a) 329#define uFLAG_ENTRY2(f,s,a,b) 330#define uFLAG_ENTRY3(f,s,a,b,c) 331#define uFLAG_ENTRY4(f,s,a,b,c,d) 332#define uFLAG_ENTRY5(f,s,a,b,c,d,e) 333#define uFLAG_ENTRY6(f,s,a,b,c,d,e,g) 334#define uFLAG_ENTRY7(f,s,a,b,c,d,e,g,h) 335#define uFLAG_RETURN(v) { return(v);} 336#define uFLAG_VOIDRETURN { return; } 337#endif 338 339 340#endif /* UTILS_H */ 341