utils.c revision 6930ead5
1 2 /*\ 3 * 4 * COPYRIGHT 1990 5 * DIGITAL EQUIPMENT CORPORATION 6 * MAYNARD, MASSACHUSETTS 7 * ALL RIGHTS RESERVED. 8 * 9 * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND 10 * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION. 11 * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE 12 * FOR ANY PURPOSE. IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED 13 * WARRANTY. 14 * 15 * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT 16 * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN 17 * ADDITION TO THAT SET FORTH ABOVE. 18 * 19 * Permission to use, copy, modify, and distribute this software and its 20 * documentation for any purpose and without fee is hereby granted, provided 21 * that the above copyright notice appear in all copies and that both that 22 * copyright notice and this permission notice appear in supporting 23 * documentation, and that the name of Digital Equipment Corporation not be 24 * used in advertising or publicity pertaining to distribution of the 25 * software without specific, written prior permission. 26 \*/ 27 28#include "utils.h" 29#include <ctype.h> 30#include <stdlib.h> 31#include <stdarg.h> 32 33 34/***====================================================================***/ 35 36#ifndef HAVE_RECALLOCARRAY 37void * 38uRecalloc(void *old, size_t nOld, size_t nNew, size_t itemSize) 39{ 40 char *rtrn; 41 42 if (old == NULL) 43 rtrn = calloc(nNew, itemSize); 44 else 45 { 46 rtrn = reallocarray(old, nNew, itemSize); 47 if ((rtrn) && (nNew > nOld)) 48 { 49 bzero(&rtrn[nOld * itemSize], (nNew - nOld) * itemSize); 50 } 51 } 52 return (void *) rtrn; 53} 54#endif 55 56 57/***====================================================================***/ 58/*** DEBUG FUNCTIONS ***/ 59/***====================================================================***/ 60 61#ifdef DEBUG 62static FILE *uDebugFile = NULL; 63int uDebugIndentLevel = 0; 64static const int uDebugIndentSize = 4; 65 66Boolean 67uSetDebugFile(const char *name) 68{ 69 if ((uDebugFile != NULL) && (uDebugFile != stderr)) 70 { 71 fprintf(uDebugFile, "switching to %s\n", name ? name : "stderr"); 72 fclose(uDebugFile); 73 } 74 if (name != NullString) 75 uDebugFile = fopen(name, "w"); 76 else 77 uDebugFile = stderr; 78 if (uDebugFile == NULL) 79 { 80 uDebugFile = stderr; 81 return (False); 82 } 83 return (True); 84} 85 86void 87uDebug(const char *s, ...) 88{ 89 va_list args; 90 91 for (int i = (uDebugIndentLevel * uDebugIndentSize); i > 0; i--) 92 { 93 putc(' ', uDebugFile); 94 } 95 va_start(args, s); 96 vfprintf(uDebugFile, s, args); 97 va_end(args); 98 fflush(uDebugFile); 99} 100#endif 101 102/***====================================================================***/ 103 104static FILE *errorFile = NULL; 105static int outCount = 0; 106static const char *preMsg = NULL; 107static const char *postMsg = NULL; 108static const char *prefix = NULL; 109 110Boolean 111uSetErrorFile(const char *name) 112{ 113 if ((errorFile != NULL) && (errorFile != stderr)) 114 { 115 fprintf(errorFile, "switching to %s\n", name ? name : "stderr"); 116 fclose(errorFile); 117 } 118 if (name != NullString) 119 errorFile = fopen(name, "w"); 120 else 121 errorFile = stderr; 122 if (errorFile == NULL) 123 { 124 errorFile = stderr; 125 return (False); 126 } 127 return (True); 128} 129 130void 131uInformation(const char *s, ...) 132{ 133 va_list args; 134 135 va_start(args, s); 136 vfprintf(errorFile, s, args); 137 va_end(args); 138 fflush(errorFile); 139} 140 141/***====================================================================***/ 142 143void 144uAction(const char *s, ...) 145{ 146 va_list args; 147 148 if (prefix != NULL) 149 fprintf(errorFile, "%s", prefix); 150 fprintf(errorFile, " "); 151 va_start(args, s); 152 vfprintf(errorFile, s, args); 153 va_end(args); 154 fflush(errorFile); 155} 156 157/***====================================================================***/ 158 159void 160uWarning(const char *s, ...) 161{ 162 va_list args; 163 164 if ((outCount == 0) && (preMsg != NULL)) 165 fprintf(errorFile, "%s\n", preMsg); 166 if (prefix != NULL) 167 fprintf(errorFile, "%s", prefix); 168 fprintf(errorFile, "Warning: "); 169 va_start(args, s); 170 vfprintf(errorFile, s, args); 171 va_end(args); 172 fflush(errorFile); 173 outCount++; 174} 175 176/***====================================================================***/ 177 178void 179uError(const char *s, ...) 180{ 181 va_list args; 182 183 if ((outCount == 0) && (preMsg != NULL)) 184 fprintf(errorFile, "%s\n", preMsg); 185 if (prefix != NULL) 186 fprintf(errorFile, "%s", prefix); 187 fprintf(errorFile, "Error: "); 188 va_start(args, s); 189 vfprintf(errorFile, s, args); 190 va_end(args); 191 fflush(errorFile); 192 outCount++; 193} 194 195/***====================================================================***/ 196 197void 198uFatalError(const char *s, ...) 199{ 200 va_list args; 201 202 if ((outCount == 0) && (preMsg != NULL)) 203 fprintf(errorFile, "%s\n", preMsg); 204 if (prefix != NULL) 205 fprintf(errorFile, "%s", prefix); 206 fprintf(errorFile, "Fatal Error: "); 207 va_start(args, s); 208 vfprintf(errorFile, s, args); 209 va_end(args); 210 fprintf(errorFile, " Exiting\n"); 211 fflush(errorFile); 212 outCount++; 213 exit(1); 214 /* NOTREACHED */ 215} 216 217/***====================================================================***/ 218 219void 220uInternalError(const char *s, ...) 221{ 222 va_list args; 223 224 if ((outCount == 0) && (preMsg != NULL)) 225 fprintf(errorFile, "%s\n", preMsg); 226 if (prefix != NULL) 227 fprintf(errorFile, "%s", prefix); 228 fprintf(errorFile, "Internal error: "); 229 va_start(args, s); 230 vfprintf(errorFile, s, args); 231 va_end(args); 232 fflush(errorFile); 233 outCount++; 234} 235 236void 237uSetPreErrorMessage(const char *msg) 238{ 239 outCount = 0; 240 preMsg = msg; 241 return; 242} 243 244void 245uSetPostErrorMessage(const char *msg) 246{ 247 postMsg = msg; 248 return; 249} 250 251void 252uSetErrorPrefix(const char *pre) 253{ 254 prefix = pre; 255 return; 256} 257 258void 259uFinishUp(void) 260{ 261 if ((outCount > 0) && (postMsg != NULL)) 262 fprintf(errorFile, "%s\n", postMsg); 263 return; 264} 265 266/***====================================================================***/ 267 268#ifndef HAVE_STRDUP 269char * 270uStringDup(const char *str) 271{ 272 char *rtrn; 273 274 if (str == NULL) 275 return NULL; 276 rtrn = malloc(strlen(str) + 1); 277 strcpy(rtrn, str); 278 return rtrn; 279} 280#endif 281 282#ifndef HAVE_STRCASECMP 283int 284uStrCaseCmp(const char *str1, const char *str2) 285{ 286 char buf1[512], buf2[512]; 287 char c, *s; 288 int n; 289 290 for (n = 0, s = buf1; (c = *str1++); n++) 291 { 292 if (isupper(c)) 293 c = tolower(c); 294 if (n > 510) 295 break; 296 *s++ = c; 297 } 298 *s = '\0'; 299 for (n = 0, s = buf2; (c = *str2++); n++) 300 { 301 if (isupper(c)) 302 c = tolower(c); 303 if (n > 510) 304 break; 305 *s++ = c; 306 } 307 *s = '\0'; 308 return (strcmp(buf1, buf2)); 309} 310 311int 312uStrCasePrefix(const char *my_prefix, const char *str) 313{ 314 char c1; 315 char c2; 316 while (((c1 = *my_prefix) != '\0') && ((c2 = *str) != '\0')) 317 { 318 if (isupper(c1)) 319 c1 = tolower(c1); 320 if (isupper(c2)) 321 c2 = tolower(c2); 322 if (c1 != c2) 323 return 0; 324 my_prefix++; 325 str++; 326 } 327 if (c1 != '\0') 328 return 0; 329 return 1; 330} 331 332#endif 333