utils.h revision b091287f
1f46a6179Smrg#ifndef UTILS_H 2f46a6179Smrg#define UTILS_H 1 3f46a6179Smrg 4f46a6179Smrg /*\ 5f46a6179Smrg * 634345a63Smrg * COPYRIGHT 1990 734345a63Smrg * DIGITAL EQUIPMENT CORPORATION 834345a63Smrg * MAYNARD, MASSACHUSETTS 934345a63Smrg * ALL RIGHTS RESERVED. 10f46a6179Smrg * 11f46a6179Smrg * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND 12f46a6179Smrg * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION. 13f46a6179Smrg * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE 14f46a6179Smrg * FOR ANY PURPOSE. IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED 15f46a6179Smrg * WARRANTY. 16f46a6179Smrg * 17f46a6179Smrg * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT 18f46a6179Smrg * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN 19f46a6179Smrg * ADDITION TO THAT SET FORTH ABOVE. 20f46a6179Smrg * 21f46a6179Smrg * Permission to use, copy, modify, and distribute this software and its 22f46a6179Smrg * documentation for any purpose and without fee is hereby granted, provided 23f46a6179Smrg * that the above copyright notice appear in all copies and that both that 24f46a6179Smrg * copyright notice and this permission notice appear in supporting 25f46a6179Smrg * documentation, and that the name of Digital Equipment Corporation not be 26f46a6179Smrg * used in advertising or publicity pertaining to distribution of the 27f46a6179Smrg * software without specific, written prior permission. 2834345a63Smrg \*/ 29f46a6179Smrg 30f46a6179Smrg/***====================================================================***/ 31f46a6179Smrg 32f46a6179Smrg#include <stdio.h> 33f46a6179Smrg#include <X11/Xos.h> 34f46a6179Smrg#include <X11/Xfuncproto.h> 35f46a6179Smrg#include <X11/Xfuncs.h> 36f46a6179Smrg 37f46a6179Smrg#include <stddef.h> 3878038a65Smrg#ifdef HAVE_CONFIG_H 3934345a63Smrg#include "config.h" 4078038a65Smrg#endif 41f46a6179Smrg 42f46a6179Smrg#ifndef NUL 43f46a6179Smrg#define NUL '\0' 44f46a6179Smrg#endif 45f46a6179Smrg 46f46a6179Smrg/***====================================================================***/ 47f46a6179Smrg 48f46a6179Smrg#ifndef OPAQUE_DEFINED 4934345a63Smrgtypedef void *Opaque; 50f46a6179Smrg#endif 51f46a6179Smrg#ifndef NullOpaque 52f46a6179Smrg#define NullOpaque ((Opaque)NULL) 53f46a6179Smrg#endif 54f46a6179Smrg 55f46a6179Smrg#ifndef BOOLEAN_DEFINED 5634345a63Smrgtypedef char Boolean; 57f46a6179Smrg#endif 58f46a6179Smrg 59f46a6179Smrg#ifndef True 60f46a6179Smrg#define True ((Boolean)1) 61f46a6179Smrg#define False ((Boolean)0) 62f46a6179Smrg#endif /* ndef True */ 63f46a6179Smrg#define booleanText(b) ((b)?"True":"False") 64f46a6179Smrg 65f46a6179Smrg#ifndef COMPARISON_DEFINED 6634345a63Smrgtypedef int Comparison; 67f46a6179Smrg 68f46a6179Smrg#define Greater ((Comparison)1) 69f46a6179Smrg#define Equal ((Comparison)0) 70f46a6179Smrg#define Less ((Comparison)-1) 71f46a6179Smrg#define CannotCompare ((Comparison)-37) 72f46a6179Smrg#define comparisonText(c) ((c)?((c)<0?"Less":"Greater"):"Equal") 73f46a6179Smrg#endif 74f46a6179Smrg 75f46a6179Smrg/***====================================================================***/ 76f46a6179Smrg 7734345a63Smrgextern Opaque uAlloc(unsigned /* size */ 7834345a63Smrg ); 7934345a63Smrgextern Opaque uCalloc(unsigned /* n */ , 8034345a63Smrg unsigned /* size */ 8134345a63Smrg ); 8234345a63Smrgextern Opaque uRealloc(Opaque /* old */ , 8334345a63Smrg unsigned /* newSize */ 8434345a63Smrg ); 8534345a63Smrgextern Opaque uRecalloc(Opaque /* old */ , 8634345a63Smrg unsigned /* nOld */ , 8734345a63Smrg unsigned /* nNew */ , 8834345a63Smrg unsigned /* newSize */ 8934345a63Smrg ); 9034345a63Smrgextern void uFree(Opaque /* ptr */ 9134345a63Smrg ); 92f46a6179Smrg 93f46a6179Smrg#define uTypedAlloc(t) ((t *)uAlloc((unsigned)sizeof(t))) 94f46a6179Smrg#define uTypedCalloc(n,t) ((t *)uCalloc((unsigned)n,(unsigned)sizeof(t))) 95f46a6179Smrg#define uTypedRealloc(pO,n,t) ((t *)uRealloc((Opaque)pO,((unsigned)n)*sizeof(t))) 96f46a6179Smrg#define uTypedRecalloc(pO,o,n,t) ((t *)uRecalloc((Opaque)pO,((unsigned)o),((unsigned)n),sizeof(t))) 97f46a6179Smrg#if (defined mdHasAlloca) && (mdHasAlloca) 98f46a6179Smrg#define uTmpAlloc(n) ((Opaque)alloca((unsigned)n)) 99f46a6179Smrg#define uTmpFree(p) 100f46a6179Smrg#else 101f46a6179Smrg#define uTmpAlloc(n) uAlloc(n) 102f46a6179Smrg#define uTmpFree(p) uFree(p) 103f46a6179Smrg#endif 104f46a6179Smrg 105f46a6179Smrg/***====================================================================***/ 106f46a6179Smrg 10734345a63Smrgextern Boolean uSetErrorFile(char * /* name */ 10834345a63Smrg ); 109f46a6179Smrg 110f46a6179Smrg#define INFO6 uInformation 111f46a6179Smrg#define INFO5 uInformation 112f46a6179Smrg#define INFO4 uInformation 113f46a6179Smrg#define INFO3 uInformation 114f46a6179Smrg#define INFO2 uInformation 115f46a6179Smrg#define INFO1 uInformation 116f46a6179Smrg#define INFO uInformation 117f46a6179Smrg 11834345a63Smrgextern void 11934345a63SmrguInformation(const char * /* s */ , ... 120e9cdd019Smrg ) _X_ATTRIBUTE_PRINTF(1, 2); 121f46a6179Smrg 122f46a6179Smrg#define ACTION6 uAction 123f46a6179Smrg#define ACTION5 uAction 124f46a6179Smrg#define ACTION4 uAction 125f46a6179Smrg#define ACTION3 uAction 126f46a6179Smrg#define ACTION2 uAction 127f46a6179Smrg#define ACTION1 uAction 128f46a6179Smrg#define ACTION uAction 129f46a6179Smrg 13034345a63Smrg extern void uAction(const char * /* s */ , ... 131e9cdd019Smrg ) _X_ATTRIBUTE_PRINTF(1, 2); 132f46a6179Smrg 133f46a6179Smrg#define WARN6 uWarning 134f46a6179Smrg#define WARN5 uWarning 135f46a6179Smrg#define WARN4 uWarning 136f46a6179Smrg#define WARN3 uWarning 137f46a6179Smrg#define WARN2 uWarning 138f46a6179Smrg#define WARN1 uWarning 139f46a6179Smrg#define WARN uWarning 140f46a6179Smrg 14134345a63Smrg extern void uWarning(const char * /* s */ , ... 142e9cdd019Smrg ) _X_ATTRIBUTE_PRINTF(1, 2); 143f46a6179Smrg 144f46a6179Smrg#define ERROR6 uError 145f46a6179Smrg#define ERROR5 uError 146f46a6179Smrg#define ERROR4 uError 147f46a6179Smrg#define ERROR3 uError 148f46a6179Smrg#define ERROR2 uError 149f46a6179Smrg#define ERROR1 uError 150f46a6179Smrg#define ERROR uError 151f46a6179Smrg 15234345a63Smrg extern void uError(const char * /* s */ , ... 153e9cdd019Smrg ) _X_ATTRIBUTE_PRINTF(1, 2); 154f46a6179Smrg 155f46a6179Smrg#define FATAL6 uFatalError 156f46a6179Smrg#define FATAL5 uFatalError 157f46a6179Smrg#define FATAL4 uFatalError 158f46a6179Smrg#define FATAL3 uFatalError 159f46a6179Smrg#define FATAL2 uFatalError 160f46a6179Smrg#define FATAL1 uFatalError 161f46a6179Smrg#define FATAL uFatalError 162f46a6179Smrg 16334345a63Smrg extern void uFatalError(const char * /* s */ , ... 164b091287fSmrg ) _X_ATTRIBUTE_PRINTF(1, 2) _X_NORETURN; 165f46a6179Smrg 166f46a6179Smrg/* WSGO stands for "Weird Stuff Going On" */ 167f46a6179Smrg#define WSGO6 uInternalError 168f46a6179Smrg#define WSGO5 uInternalError 169f46a6179Smrg#define WSGO4 uInternalError 170f46a6179Smrg#define WSGO3 uInternalError 171f46a6179Smrg#define WSGO2 uInternalError 172f46a6179Smrg#define WSGO1 uInternalError 173f46a6179Smrg#define WSGO uInternalError 174f46a6179Smrg 17534345a63Smrg extern void uInternalError(const char * /* s */ , ... 176e9cdd019Smrg ) _X_ATTRIBUTE_PRINTF(1, 2); 177f46a6179Smrg 17834345a63Smrg extern void uSetPreErrorMessage(char * /* msg */ 17934345a63Smrg ); 180f46a6179Smrg 18134345a63Smrg extern void uSetPostErrorMessage(char * /* msg */ 18234345a63Smrg ); 183f46a6179Smrg 18434345a63Smrg extern void uSetErrorPrefix(char * /* void */ 18534345a63Smrg ); 186f46a6179Smrg 18734345a63Smrg extern void uFinishUp(void); 188f46a6179Smrg 189f46a6179Smrg 190f46a6179Smrg/***====================================================================***/ 191f46a6179Smrg 192f46a6179Smrg#define NullString ((char *)NULL) 193f46a6179Smrg 194f46a6179Smrg#define uStringText(s) ((s)==NullString?"<NullString>":(s)) 195f46a6179Smrg#define uStringEqual(s1,s2) (uStringCompare(s1,s2)==Equal) 196f46a6179Smrg#define uStringPrefix(p,s) (strncmp(p,s,strlen(p))==0) 197f46a6179Smrg#define uStringCompare(s1,s2) (((s1)==NullString||(s2)==NullString)?\ 198f46a6179Smrg (s1)!=(s2):strcmp(s1,s2)) 199f46a6179Smrg#define uStrCaseEqual(s1,s2) (uStrCaseCmp(s1,s2)==0) 200f46a6179Smrg#ifdef HAVE_STRCASECMP 201b091287fSmrg#include <strings.h> 202f46a6179Smrg#define uStrCaseCmp(s1,s2) (strcasecmp(s1,s2)) 203f46a6179Smrg#define uStrCasePrefix(p,s) (strncasecmp(p,s,strlen(p))==0) 204f46a6179Smrg#else 20534345a63Smrg extern int uStrCaseCmp(const char * /* s1 */ , 20634345a63Smrg const char * /* s2 */ 20734345a63Smrg ); 20834345a63Smrg extern int uStrCasePrefix(const char * /* p */ , 20934345a63Smrg char * /* str */ 21034345a63Smrg ); 211f46a6179Smrg#endif 212f46a6179Smrg#ifdef HAVE_STRDUP 213b091287fSmrg#include <string.h> 21434345a63Smrg#define uStringDup(s1) ((s1) ? strdup(s1) : NULL) 215f46a6179Smrg#else 21634345a63Smrg extern char *uStringDup(const char * /* s1 */ 21734345a63Smrg ); 218f46a6179Smrg#endif 219f46a6179Smrg 220f46a6179Smrg/***====================================================================***/ 221f46a6179Smrg 222f46a6179Smrg#ifdef ASSERTIONS_ON 223f46a6179Smrg#define uASSERT(where,why) \ 224f46a6179Smrg {if (!(why)) uFatalError("assertion botched in %s ( why )\n",where);} 225f46a6179Smrg#else 226f46a6179Smrg#define uASSERT(where,why) 227f46a6179Smrg#endif 228f46a6179Smrg 229f46a6179Smrg/***====================================================================***/ 230f46a6179Smrg 231f46a6179Smrg#ifndef DEBUG_VAR 232f46a6179Smrg#define DEBUG_VAR debugFlags 233f46a6179Smrg#endif 234f46a6179Smrg 235f46a6179Smrgextern 23634345a63Smrg unsigned int DEBUG_VAR; 237f46a6179Smrg 23834345a63Smrg extern void uDebug(char * /* s */ , ... 239e9cdd019Smrg ) _X_ATTRIBUTE_PRINTF(1, 2); 240f46a6179Smrg 24134345a63Smrg extern void uDebugNOI( /* no indent */ 24234345a63Smrg char * /* s */ , ... 243e9cdd019Smrg ) _X_ATTRIBUTE_PRINTF(1, 2); 244f46a6179Smrg 24534345a63Smrg extern Boolean uSetDebugFile(char *name); 246f46a6179Smrg 24734345a63Smrg extern FILE *uDebugFile; 24834345a63Smrg extern int uDebugIndentLevel; 24934345a63Smrg extern int uDebugIndentSize; 250f46a6179Smrg#define uDebugIndent(l) (uDebugIndentLevel+=(l)) 251f46a6179Smrg#define uDebugOutdent(l) (uDebugIndentLevel-=(l)) 252f46a6179Smrg#ifdef DEBUG_ON 253f46a6179Smrg#define uDEBUG(f,s) { if (DEBUG_VAR&(f)) uDebug(s);} 254f46a6179Smrg#define uDEBUG1(f,s,a) { if (DEBUG_VAR&(f)) uDebug(s,a);} 255f46a6179Smrg#define uDEBUG2(f,s,a,b) { if (DEBUG_VAR&(f)) uDebug(s,a,b);} 256f46a6179Smrg#define uDEBUG3(f,s,a,b,c) { if (DEBUG_VAR&(f)) uDebug(s,a,b,c);} 257f46a6179Smrg#define uDEBUG4(f,s,a,b,c,d) { if (DEBUG_VAR&(f)) uDebug(s,a,b,c,d);} 258f46a6179Smrg#define uDEBUG5(f,s,a,b,c,d,e) { if (DEBUG_VAR&(f)) uDebug(s,a,b,c,d,e);} 259f46a6179Smrg#define uDEBUG_NOI(f,s) { if (DEBUG_VAR&(f)) uDebug(s);} 260f46a6179Smrg#define uDEBUG_NOI1(f,s,a) { if (DEBUG_VAR&(f)) uDebugNOI(s,a);} 261f46a6179Smrg#define uDEBUG_NOI2(f,s,a,b) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b);} 262f46a6179Smrg#define uDEBUG_NOI3(f,s,a,b,c) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c);} 263f46a6179Smrg#define uDEBUG_NOI4(f,s,a,b,c,d) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c,d);} 264f46a6179Smrg#define uDEBUG_NOI5(f,s,a,b,c,d,e) { if (DEBUG_VAR&(f)) uDebugNOI(s,a,b,c,d,e);} 265f46a6179Smrg#else 266f46a6179Smrg#define uDEBUG(f,s) 267f46a6179Smrg#define uDEBUG1(f,s,a) 268f46a6179Smrg#define uDEBUG2(f,s,a,b) 269f46a6179Smrg#define uDEBUG3(f,s,a,b,c) 270f46a6179Smrg#define uDEBUG4(f,s,a,b,c,d) 271f46a6179Smrg#define uDEBUG5(f,s,a,b,c,d,e) 272f46a6179Smrg#define uDEBUG_NOI(f,s) 273f46a6179Smrg#define uDEBUG_NOI1(f,s,a) 274f46a6179Smrg#define uDEBUG_NOI2(f,s,a,b) 275f46a6179Smrg#define uDEBUG_NOI3(f,s,a,b,c) 276f46a6179Smrg#define uDEBUG_NOI4(f,s,a,b,c,d) 277f46a6179Smrg#define uDEBUG_NOI5(f,s,a,b,c,d,e) 278f46a6179Smrg#endif 279f46a6179Smrg 28034345a63Smrg extern Boolean uSetEntryFile(char *name); 28134345a63Smrg extern void uEntry(int /* l */ , 28234345a63Smrg char * /* s */ , ... 283e9cdd019Smrg ) _X_ATTRIBUTE_PRINTF(2, 3); 284f46a6179Smrg 28534345a63Smrg extern void uExit(int l, char *rtVal); 286f46a6179Smrg#ifdef ENTRY_TRACKING_ON 287f46a6179Smrg#define ENTRY_BIT 0x10 288f46a6179Smrg#define LOW_ENTRY_BIT 0x1000 289f46a6179Smrg#define ENTER (DEBUG_VAR&ENTRY_BIT) 290f46a6179Smrg#define FLAG(fLag) (DEBUG_VAR&(fLag)) 291f46a6179Smrg 29234345a63Smrg extern int uEntryLevel; 293f46a6179Smrg 294f46a6179Smrg#define uENTRY(s) { if (ENTER) uEntry(1,s);} 295f46a6179Smrg#define uENTRY1(s,a) { if (ENTER) uEntry(1,s,a);} 296f46a6179Smrg#define uENTRY2(s,a,b) { if (ENTER) uEntry(1,s,a,b);} 297f46a6179Smrg#define uENTRY3(s,a,b,c) { if (ENTER) uEntry(1,s,a,b,c);} 298f46a6179Smrg#define uENTRY4(s,a,b,c,d) { if (ENTER) uEntry(1,s,a,b,c,d);} 299f46a6179Smrg#define uENTRY5(s,a,b,c,d,e) { if (ENTER) uEntry(1,s,a,b,c,d,e);} 300f46a6179Smrg#define uENTRY6(s,a,b,c,d,e,f) { if (ENTER) uEntry(1,s,a,b,c,d,e,f);} 301f46a6179Smrg#define uENTRY7(s,a,b,c,d,e,f,g) { if (ENTER) uEntry(1,s,a,b,c,d,e,f,g);} 302f46a6179Smrg#define uRETURN(v) { if (ENTER) uEntryLevel--; return(v); } 303f46a6179Smrg#define uVOIDRETURN { if (ENTER) uEntryLevel--; return; } 304f46a6179Smrg 305f46a6179Smrg#define uFLAG_ENTRY(w,s) { if (FLAG(w)) uEntry(0,s);} 306f46a6179Smrg#define uFLAG_ENTRY1(w,s,a) { if (FLAG(w)) uEntry(0,s,a);} 307f46a6179Smrg#define uFLAG_ENTRY2(w,s,a,b) { if (FLAG(w)) uEntry(0,s,a,b);} 308f46a6179Smrg#define uFLAG_ENTRY3(w,s,a,b,c) { if (FLAG(w)) uEntry(0,s,a,b,c);} 309f46a6179Smrg#define uFLAG_ENTRY4(w,s,a,b,c,d) { if (FLAG(w)) uEntry(0,s,a,b,c,d);} 310f46a6179Smrg#define uFLAG_ENTRY5(w,s,a,b,c,d,e) { if (FLAG(w)) uEntry(0,s,a,b,c,d,e);} 311f46a6179Smrg#define uFLAG_ENTRY6(w,s,a,b,c,d,e,f) { if (FLAG(w)) uEntry(0,s,a,b,c,d,e,f);} 312f46a6179Smrg#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);} 313f46a6179Smrg#define uFLAG_RETURN(v) { return(v);} 314f46a6179Smrg#define uFLAG_VOIDRETURN { return; } 315f46a6179Smrg#else 316f46a6179Smrg#define uENTRY(s) 317f46a6179Smrg#define uENTRY1(s,a) 318f46a6179Smrg#define uENTRY2(s,a1,a2) 319f46a6179Smrg#define uENTRY3(s,a1,a2,a3) 320f46a6179Smrg#define uENTRY4(s,a1,a2,a3,a4) 321f46a6179Smrg#define uENTRY5(s,a1,a2,a3,a4,a5) 322f46a6179Smrg#define uENTRY6(s,a1,a2,a3,a4,a5,a6) 323f46a6179Smrg#define uENTRY7(s,a1,a2,a3,a4,a5,a6,a7) 324f46a6179Smrg#define uRETURN(v) { return(v); } 325f46a6179Smrg#define uVOIDRETURN { return; } 326f46a6179Smrg 327f46a6179Smrg#define uFLAG_ENTRY(f,s) 328f46a6179Smrg#define uFLAG_ENTRY1(f,s,a) 329f46a6179Smrg#define uFLAG_ENTRY2(f,s,a,b) 330f46a6179Smrg#define uFLAG_ENTRY3(f,s,a,b,c) 331f46a6179Smrg#define uFLAG_ENTRY4(f,s,a,b,c,d) 332f46a6179Smrg#define uFLAG_ENTRY5(f,s,a,b,c,d,e) 333f46a6179Smrg#define uFLAG_ENTRY6(f,s,a,b,c,d,e,g) 334f46a6179Smrg#define uFLAG_ENTRY7(f,s,a,b,c,d,e,g,h) 335f46a6179Smrg#define uFLAG_RETURN(v) { return(v);} 336f46a6179Smrg#define uFLAG_VOIDRETURN { return; } 33734345a63Smrg#endif 338f46a6179Smrg 339f46a6179Smrg 340f46a6179Smrg#endif /* UTILS_H */ 341