1f46a6179Smrg 2f46a6179Smrg /*\ 3f46a6179Smrg * 434345a63Smrg * COPYRIGHT 1990 534345a63Smrg * DIGITAL EQUIPMENT CORPORATION 634345a63Smrg * MAYNARD, MASSACHUSETTS 734345a63Smrg * ALL RIGHTS RESERVED. 8f46a6179Smrg * 9f46a6179Smrg * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND 10f46a6179Smrg * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION. 11bfe6082cSmrg * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE 12bfe6082cSmrg * FOR ANY PURPOSE. IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED 13f46a6179Smrg * WARRANTY. 14f46a6179Smrg * 15f46a6179Smrg * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT 16f46a6179Smrg * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN 17f46a6179Smrg * ADDITION TO THAT SET FORTH ABOVE. 18f46a6179Smrg * 19f46a6179Smrg * Permission to use, copy, modify, and distribute this software and its 20f46a6179Smrg * documentation for any purpose and without fee is hereby granted, provided 21f46a6179Smrg * that the above copyright notice appear in all copies and that both that 22f46a6179Smrg * copyright notice and this permission notice appear in supporting 23f46a6179Smrg * documentation, and that the name of Digital Equipment Corporation not be 24bfe6082cSmrg * used in advertising or publicity pertaining to distribution of the 25f46a6179Smrg * software without specific, written prior permission. 2634345a63Smrg \*/ 27f46a6179Smrg 28f46a6179Smrg#include "utils.h" 29f46a6179Smrg#include <ctype.h> 30f46a6179Smrg#include <stdlib.h> 31f46a6179Smrg#include <stdarg.h> 32f46a6179Smrg 33f46a6179Smrg 34f46a6179Smrg/***====================================================================***/ 35f46a6179Smrg 366930ead5Smrg#ifndef HAVE_RECALLOCARRAY 376930ead5Smrgvoid * 386930ead5SmrguRecalloc(void *old, size_t nOld, size_t nNew, size_t itemSize) 39f46a6179Smrg{ 4034345a63Smrg char *rtrn; 4134345a63Smrg 4234345a63Smrg if (old == NULL) 436930ead5Smrg rtrn = calloc(nNew, itemSize); 4434345a63Smrg else 4534345a63Smrg { 466930ead5Smrg rtrn = reallocarray(old, nNew, itemSize); 4734345a63Smrg if ((rtrn) && (nNew > nOld)) 4834345a63Smrg { 4934345a63Smrg bzero(&rtrn[nOld * itemSize], (nNew - nOld) * itemSize); 5034345a63Smrg } 51f46a6179Smrg } 526930ead5Smrg return (void *) rtrn; 53f46a6179Smrg} 546930ead5Smrg#endif 55f46a6179Smrg 56f46a6179Smrg 57f46a6179Smrg/***====================================================================***/ 586930ead5Smrg/*** DEBUG FUNCTIONS ***/ 59f46a6179Smrg/***====================================================================***/ 60f46a6179Smrg 616930ead5Smrg#ifdef DEBUG 626930ead5Smrgstatic FILE *uDebugFile = NULL; 6334345a63Smrgint uDebugIndentLevel = 0; 646930ead5Smrgstatic const int uDebugIndentSize = 4; 65f46a6179Smrg 66f46a6179SmrgBoolean 676930ead5SmrguSetDebugFile(const char *name) 68f46a6179Smrg{ 6934345a63Smrg if ((uDebugFile != NULL) && (uDebugFile != stderr)) 7034345a63Smrg { 7134345a63Smrg fprintf(uDebugFile, "switching to %s\n", name ? name : "stderr"); 7234345a63Smrg fclose(uDebugFile); 73f46a6179Smrg } 7434345a63Smrg if (name != NullString) 7534345a63Smrg uDebugFile = fopen(name, "w"); 7634345a63Smrg else 7734345a63Smrg uDebugFile = stderr; 7834345a63Smrg if (uDebugFile == NULL) 7934345a63Smrg { 8034345a63Smrg uDebugFile = stderr; 8134345a63Smrg return (False); 82f46a6179Smrg } 8334345a63Smrg return (True); 84f46a6179Smrg} 85f46a6179Smrg 86f46a6179Smrgvoid 876930ead5SmrguDebug(const char *s, ...) 88f46a6179Smrg{ 8934345a63Smrg va_list args; 90f46a6179Smrg 916930ead5Smrg for (int i = (uDebugIndentLevel * uDebugIndentSize); i > 0; i--) 9234345a63Smrg { 9334345a63Smrg putc(' ', uDebugFile); 94f46a6179Smrg } 95f46a6179Smrg va_start(args, s); 9634345a63Smrg vfprintf(uDebugFile, s, args); 97f46a6179Smrg va_end(args); 98f46a6179Smrg fflush(uDebugFile); 99f46a6179Smrg} 1006930ead5Smrg#endif 101f46a6179Smrg 102f46a6179Smrg/***====================================================================***/ 103f46a6179Smrg 10434345a63Smrgstatic FILE *errorFile = NULL; 10534345a63Smrgstatic int outCount = 0; 1066930ead5Smrgstatic const char *preMsg = NULL; 1076930ead5Smrgstatic const char *postMsg = NULL; 1086930ead5Smrgstatic const char *prefix = NULL; 109f46a6179Smrg 110f46a6179SmrgBoolean 1116930ead5SmrguSetErrorFile(const char *name) 112f46a6179Smrg{ 11334345a63Smrg if ((errorFile != NULL) && (errorFile != stderr)) 11434345a63Smrg { 11534345a63Smrg fprintf(errorFile, "switching to %s\n", name ? name : "stderr"); 11634345a63Smrg fclose(errorFile); 117f46a6179Smrg } 11834345a63Smrg if (name != NullString) 11934345a63Smrg errorFile = fopen(name, "w"); 12034345a63Smrg else 12134345a63Smrg errorFile = stderr; 12234345a63Smrg if (errorFile == NULL) 12334345a63Smrg { 12434345a63Smrg errorFile = stderr; 12534345a63Smrg return (False); 126f46a6179Smrg } 12734345a63Smrg return (True); 128f46a6179Smrg} 129f46a6179Smrg 130f46a6179Smrgvoid 131f46a6179SmrguInformation(const char *s, ...) 132f46a6179Smrg{ 13334345a63Smrg va_list args; 134f46a6179Smrg 135f46a6179Smrg va_start(args, s); 13634345a63Smrg vfprintf(errorFile, s, args); 137f46a6179Smrg va_end(args); 138f46a6179Smrg fflush(errorFile); 139f46a6179Smrg} 140f46a6179Smrg 141f46a6179Smrg/***====================================================================***/ 142f46a6179Smrg 143f46a6179Smrgvoid 144f46a6179SmrguAction(const char *s, ...) 145f46a6179Smrg{ 14634345a63Smrg va_list args; 147f46a6179Smrg 14834345a63Smrg if (prefix != NULL) 14934345a63Smrg fprintf(errorFile, "%s", prefix); 15034345a63Smrg fprintf(errorFile, " "); 151f46a6179Smrg va_start(args, s); 15234345a63Smrg vfprintf(errorFile, s, args); 153f46a6179Smrg va_end(args); 154f46a6179Smrg fflush(errorFile); 155f46a6179Smrg} 156f46a6179Smrg 157f46a6179Smrg/***====================================================================***/ 158f46a6179Smrg 159f46a6179Smrgvoid 160f46a6179SmrguWarning(const char *s, ...) 161f46a6179Smrg{ 16234345a63Smrg va_list args; 163f46a6179Smrg 16434345a63Smrg if ((outCount == 0) && (preMsg != NULL)) 16534345a63Smrg fprintf(errorFile, "%s\n", preMsg); 16634345a63Smrg if (prefix != NULL) 16734345a63Smrg fprintf(errorFile, "%s", prefix); 16834345a63Smrg fprintf(errorFile, "Warning: "); 169f46a6179Smrg va_start(args, s); 17034345a63Smrg vfprintf(errorFile, s, args); 171f46a6179Smrg va_end(args); 172f46a6179Smrg fflush(errorFile); 173f46a6179Smrg outCount++; 174f46a6179Smrg} 175f46a6179Smrg 176f46a6179Smrg/***====================================================================***/ 177f46a6179Smrg 178f46a6179Smrgvoid 179f46a6179SmrguError(const char *s, ...) 180f46a6179Smrg{ 18134345a63Smrg va_list args; 182f46a6179Smrg 18334345a63Smrg if ((outCount == 0) && (preMsg != NULL)) 18434345a63Smrg fprintf(errorFile, "%s\n", preMsg); 18534345a63Smrg if (prefix != NULL) 18634345a63Smrg fprintf(errorFile, "%s", prefix); 18734345a63Smrg fprintf(errorFile, "Error: "); 188f46a6179Smrg va_start(args, s); 18934345a63Smrg vfprintf(errorFile, s, args); 190f46a6179Smrg va_end(args); 191f46a6179Smrg fflush(errorFile); 192f46a6179Smrg outCount++; 193f46a6179Smrg} 194f46a6179Smrg 195f46a6179Smrg/***====================================================================***/ 196f46a6179Smrg 197f46a6179Smrgvoid 198f46a6179SmrguFatalError(const char *s, ...) 199f46a6179Smrg{ 20034345a63Smrg va_list args; 201f46a6179Smrg 20234345a63Smrg if ((outCount == 0) && (preMsg != NULL)) 20334345a63Smrg fprintf(errorFile, "%s\n", preMsg); 20434345a63Smrg if (prefix != NULL) 20534345a63Smrg fprintf(errorFile, "%s", prefix); 20634345a63Smrg fprintf(errorFile, "Fatal Error: "); 207f46a6179Smrg va_start(args, s); 20834345a63Smrg vfprintf(errorFile, s, args); 209f46a6179Smrg va_end(args); 21034345a63Smrg fprintf(errorFile, " Exiting\n"); 211f46a6179Smrg fflush(errorFile); 212f46a6179Smrg outCount++; 213f46a6179Smrg exit(1); 214f46a6179Smrg /* NOTREACHED */ 215f46a6179Smrg} 216f46a6179Smrg 217f46a6179Smrg/***====================================================================***/ 218f46a6179Smrg 219f46a6179Smrgvoid 220f46a6179SmrguInternalError(const char *s, ...) 221f46a6179Smrg{ 22234345a63Smrg va_list args; 223f46a6179Smrg 22434345a63Smrg if ((outCount == 0) && (preMsg != NULL)) 22534345a63Smrg fprintf(errorFile, "%s\n", preMsg); 22634345a63Smrg if (prefix != NULL) 22734345a63Smrg fprintf(errorFile, "%s", prefix); 22834345a63Smrg fprintf(errorFile, "Internal error: "); 229f46a6179Smrg va_start(args, s); 23034345a63Smrg vfprintf(errorFile, s, args); 231f46a6179Smrg va_end(args); 232f46a6179Smrg fflush(errorFile); 233f46a6179Smrg outCount++; 234f46a6179Smrg} 235f46a6179Smrg 236f46a6179Smrgvoid 2376930ead5SmrguSetPreErrorMessage(const char *msg) 238f46a6179Smrg{ 23934345a63Smrg outCount = 0; 24034345a63Smrg preMsg = msg; 241f46a6179Smrg return; 242f46a6179Smrg} 243f46a6179Smrg 244f46a6179Smrgvoid 2456930ead5SmrguSetPostErrorMessage(const char *msg) 246f46a6179Smrg{ 24734345a63Smrg postMsg = msg; 248f46a6179Smrg return; 249f46a6179Smrg} 250f46a6179Smrg 251f46a6179Smrgvoid 2526930ead5SmrguSetErrorPrefix(const char *pre) 253f46a6179Smrg{ 25434345a63Smrg prefix = pre; 255f46a6179Smrg return; 256f46a6179Smrg} 257f46a6179Smrg 258f46a6179Smrgvoid 259f46a6179SmrguFinishUp(void) 260f46a6179Smrg{ 26134345a63Smrg if ((outCount > 0) && (postMsg != NULL)) 26234345a63Smrg fprintf(errorFile, "%s\n", postMsg); 263f46a6179Smrg return; 264f46a6179Smrg} 265f46a6179Smrg 266f46a6179Smrg/***====================================================================***/ 267f46a6179Smrg 268f46a6179Smrg#ifndef HAVE_STRDUP 269f46a6179Smrgchar * 270f46a6179SmrguStringDup(const char *str) 271f46a6179Smrg{ 27234345a63Smrg char *rtrn; 273f46a6179Smrg 27434345a63Smrg if (str == NULL) 27534345a63Smrg return NULL; 2766930ead5Smrg rtrn = malloc(strlen(str) + 1); 27734345a63Smrg strcpy(rtrn, str); 278f46a6179Smrg return rtrn; 279f46a6179Smrg} 280f46a6179Smrg#endif 281f46a6179Smrg 282f46a6179Smrg#ifndef HAVE_STRCASECMP 283f46a6179Smrgint 284f46a6179SmrguStrCaseCmp(const char *str1, const char *str2) 285f46a6179Smrg{ 28634345a63Smrg char buf1[512], buf2[512]; 287f46a6179Smrg char c, *s; 2886930ead5Smrg int n; 289f46a6179Smrg 29034345a63Smrg for (n = 0, s = buf1; (c = *str1++); n++) 29134345a63Smrg { 29234345a63Smrg if (isupper(c)) 29334345a63Smrg c = tolower(c); 29434345a63Smrg if (n > 510) 29534345a63Smrg break; 29634345a63Smrg *s++ = c; 297f46a6179Smrg } 298f46a6179Smrg *s = '\0'; 29934345a63Smrg for (n = 0, s = buf2; (c = *str2++); n++) 30034345a63Smrg { 30134345a63Smrg if (isupper(c)) 30234345a63Smrg c = tolower(c); 30334345a63Smrg if (n > 510) 30434345a63Smrg break; 30534345a63Smrg *s++ = c; 306f46a6179Smrg } 307f46a6179Smrg *s = '\0'; 308f46a6179Smrg return (strcmp(buf1, buf2)); 309f46a6179Smrg} 310f46a6179Smrg 311f46a6179Smrgint 3126930ead5SmrguStrCasePrefix(const char *my_prefix, const char *str) 313f46a6179Smrg{ 314f46a6179Smrg char c1; 315f46a6179Smrg char c2; 31634345a63Smrg while (((c1 = *my_prefix) != '\0') && ((c2 = *str) != '\0')) 31734345a63Smrg { 31834345a63Smrg if (isupper(c1)) 31934345a63Smrg c1 = tolower(c1); 32034345a63Smrg if (isupper(c2)) 32134345a63Smrg c2 = tolower(c2); 32234345a63Smrg if (c1 != c2) 32334345a63Smrg return 0; 32434345a63Smrg my_prefix++; 32534345a63Smrg str++; 326f46a6179Smrg } 32734345a63Smrg if (c1 != '\0') 32834345a63Smrg return 0; 329f46a6179Smrg return 1; 330f46a6179Smrg} 331f46a6179Smrg 332f46a6179Smrg#endif 333