utils.c revision 34345a63
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.
11f46a6179Smrg   * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
12f46a6179Smrg   * 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
24f46a6179Smrg   * 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
35f46a6179SmrgOpaque
36f46a6179SmrguAlloc(unsigned size)
37f46a6179Smrg{
3834345a63Smrg    return ((Opaque) malloc(size));
39f46a6179Smrg}
40f46a6179Smrg
41f46a6179Smrg/***====================================================================***/
42f46a6179Smrg
43f46a6179SmrgOpaque
4434345a63SmrguCalloc(unsigned n, unsigned size)
45f46a6179Smrg{
4634345a63Smrg    return ((Opaque) calloc(n, size));
47f46a6179Smrg}
48f46a6179Smrg
49f46a6179Smrg/***====================================================================***/
50f46a6179Smrg
51f46a6179SmrgOpaque
5234345a63SmrguRealloc(Opaque old, unsigned newSize)
53f46a6179Smrg{
5434345a63Smrg    if (old == NULL)
5534345a63Smrg        return ((Opaque) malloc(newSize));
5634345a63Smrg    else
5734345a63Smrg        return ((Opaque) realloc((char *) old, newSize));
58f46a6179Smrg}
59f46a6179Smrg
60f46a6179Smrg/***====================================================================***/
61f46a6179Smrg
62f46a6179SmrgOpaque
6334345a63SmrguRecalloc(Opaque old, unsigned nOld, unsigned nNew, unsigned itemSize)
64f46a6179Smrg{
6534345a63Smrg    char *rtrn;
6634345a63Smrg
6734345a63Smrg    if (old == NULL)
6834345a63Smrg        rtrn = (char *) calloc(nNew, itemSize);
6934345a63Smrg    else
7034345a63Smrg    {
7134345a63Smrg        rtrn = (char *) realloc((char *) old, nNew * itemSize);
7234345a63Smrg        if ((rtrn) && (nNew > nOld))
7334345a63Smrg        {
7434345a63Smrg            bzero(&rtrn[nOld * itemSize], (nNew - nOld) * itemSize);
7534345a63Smrg        }
76f46a6179Smrg    }
7734345a63Smrg    return (Opaque) rtrn;
78f46a6179Smrg}
79f46a6179Smrg
80f46a6179Smrg/***====================================================================***/
81f46a6179Smrg
82f46a6179Smrgvoid
83f46a6179SmrguFree(Opaque ptr)
84f46a6179Smrg{
8534345a63Smrg    if (ptr != (Opaque) NULL)
8634345a63Smrg        free((char *) ptr);
87f46a6179Smrg    return;
88f46a6179Smrg}
89f46a6179Smrg
90f46a6179Smrg/***====================================================================***/
91f46a6179Smrg/***                  FUNCTION ENTRY TRACKING                           ***/
92f46a6179Smrg/***====================================================================***/
93f46a6179Smrg
9434345a63Smrgstatic FILE *entryFile = NULL;
9534345a63Smrgint uEntryLevel;
96f46a6179Smrg
97f46a6179SmrgBoolean
98f46a6179SmrguSetEntryFile(char *name)
99f46a6179Smrg{
10034345a63Smrg    if ((entryFile != NULL) && (entryFile != stderr))
10134345a63Smrg    {
10234345a63Smrg        fprintf(entryFile, "switching to %s\n", name ? name : "stderr");
10334345a63Smrg        fclose(entryFile);
104f46a6179Smrg    }
10534345a63Smrg    if (name != NullString)
10634345a63Smrg        entryFile = fopen(name, "w");
10734345a63Smrg    else
10834345a63Smrg        entryFile = stderr;
10934345a63Smrg    if (entryFile == NULL)
11034345a63Smrg    {
11134345a63Smrg        entryFile = stderr;
11234345a63Smrg        return (False);
113f46a6179Smrg    }
11434345a63Smrg    return (True);
115f46a6179Smrg}
116f46a6179Smrg
117f46a6179Smrgvoid
11834345a63SmrguEntry(int l, char *s, ...)
119f46a6179Smrg{
12034345a63Smrg    int i;
12134345a63Smrg    va_list args;
122f46a6179Smrg
12334345a63Smrg    for (i = 0; i < uEntryLevel; i++)
12434345a63Smrg    {
12534345a63Smrg        putc(' ', entryFile);
126f46a6179Smrg    }
127f46a6179Smrg    va_start(args, s);
12834345a63Smrg    vfprintf(entryFile, s, args);
129f46a6179Smrg    va_end(args);
13034345a63Smrg    uEntryLevel += l;
131f46a6179Smrg}
132f46a6179Smrg
133f46a6179Smrgvoid
13434345a63SmrguExit(int l, char *rtVal)
135f46a6179Smrg{
13634345a63Smrg    int i;
13734345a63Smrg
13834345a63Smrg    uEntryLevel -= l;
13934345a63Smrg    if (uEntryLevel < 0)
14034345a63Smrg        uEntryLevel = 0;
14134345a63Smrg    for (i = 0; i < uEntryLevel; i++)
14234345a63Smrg    {
14334345a63Smrg        putc(' ', entryFile);
144f46a6179Smrg    }
14534345a63Smrg    fprintf(entryFile, "---> %p\n", rtVal);
146f46a6179Smrg    return;
147f46a6179Smrg}
148f46a6179Smrg
149f46a6179Smrg/***====================================================================***/
150f46a6179Smrg/***			PRINT FUNCTIONS					***/
151f46a6179Smrg/***====================================================================***/
152f46a6179Smrg
15334345a63SmrgFILE *uDebugFile = NULL;
15434345a63Smrgint uDebugIndentLevel = 0;
15534345a63Smrgint uDebugIndentSize = 4;
156f46a6179Smrg
157f46a6179SmrgBoolean
158f46a6179SmrguSetDebugFile(char *name)
159f46a6179Smrg{
16034345a63Smrg    if ((uDebugFile != NULL) && (uDebugFile != stderr))
16134345a63Smrg    {
16234345a63Smrg        fprintf(uDebugFile, "switching to %s\n", name ? name : "stderr");
16334345a63Smrg        fclose(uDebugFile);
164f46a6179Smrg    }
16534345a63Smrg    if (name != NullString)
16634345a63Smrg        uDebugFile = fopen(name, "w");
16734345a63Smrg    else
16834345a63Smrg        uDebugFile = stderr;
16934345a63Smrg    if (uDebugFile == NULL)
17034345a63Smrg    {
17134345a63Smrg        uDebugFile = stderr;
17234345a63Smrg        return (False);
173f46a6179Smrg    }
17434345a63Smrg    return (True);
175f46a6179Smrg}
176f46a6179Smrg
177f46a6179Smrgvoid
17834345a63SmrguDebug(char *s, ...)
179f46a6179Smrg{
18034345a63Smrg    int i;
18134345a63Smrg    va_list args;
182f46a6179Smrg
18334345a63Smrg    for (i = (uDebugIndentLevel * uDebugIndentSize); i > 0; i--)
18434345a63Smrg    {
18534345a63Smrg        putc(' ', uDebugFile);
186f46a6179Smrg    }
187f46a6179Smrg    va_start(args, s);
18834345a63Smrg    vfprintf(uDebugFile, s, args);
189f46a6179Smrg    va_end(args);
190f46a6179Smrg    fflush(uDebugFile);
191f46a6179Smrg}
192f46a6179Smrg
193f46a6179Smrgvoid
19434345a63SmrguDebugNOI(char *s, ...)
195f46a6179Smrg{
19634345a63Smrg    va_list args;
197f46a6179Smrg
198f46a6179Smrg    va_start(args, s);
19934345a63Smrg    vfprintf(uDebugFile, s, args);
200f46a6179Smrg    va_end(args);
201f46a6179Smrg    fflush(uDebugFile);
202f46a6179Smrg}
203f46a6179Smrg
204f46a6179Smrg/***====================================================================***/
205f46a6179Smrg
20634345a63Smrgstatic FILE *errorFile = NULL;
20734345a63Smrgstatic int outCount = 0;
20834345a63Smrgstatic char *preMsg = NULL;
20934345a63Smrgstatic char *postMsg = NULL;
21034345a63Smrgstatic char *prefix = NULL;
211f46a6179Smrg
212f46a6179SmrgBoolean
213f46a6179SmrguSetErrorFile(char *name)
214f46a6179Smrg{
21534345a63Smrg    if ((errorFile != NULL) && (errorFile != stderr))
21634345a63Smrg    {
21734345a63Smrg        fprintf(errorFile, "switching to %s\n", name ? name : "stderr");
21834345a63Smrg        fclose(errorFile);
219f46a6179Smrg    }
22034345a63Smrg    if (name != NullString)
22134345a63Smrg        errorFile = fopen(name, "w");
22234345a63Smrg    else
22334345a63Smrg        errorFile = stderr;
22434345a63Smrg    if (errorFile == NULL)
22534345a63Smrg    {
22634345a63Smrg        errorFile = stderr;
22734345a63Smrg        return (False);
228f46a6179Smrg    }
22934345a63Smrg    return (True);
230f46a6179Smrg}
231f46a6179Smrg
232f46a6179Smrgvoid
233f46a6179SmrguInformation(const char *s, ...)
234f46a6179Smrg{
23534345a63Smrg    va_list args;
236f46a6179Smrg
237f46a6179Smrg    va_start(args, s);
23834345a63Smrg    vfprintf(errorFile, s, args);
239f46a6179Smrg    va_end(args);
240f46a6179Smrg    fflush(errorFile);
241f46a6179Smrg}
242f46a6179Smrg
243f46a6179Smrg/***====================================================================***/
244f46a6179Smrg
245f46a6179Smrgvoid
246f46a6179SmrguAction(const char *s, ...)
247f46a6179Smrg{
24834345a63Smrg    va_list args;
249f46a6179Smrg
25034345a63Smrg    if (prefix != NULL)
25134345a63Smrg        fprintf(errorFile, "%s", prefix);
25234345a63Smrg    fprintf(errorFile, "                  ");
253f46a6179Smrg    va_start(args, s);
25434345a63Smrg    vfprintf(errorFile, s, args);
255f46a6179Smrg    va_end(args);
256f46a6179Smrg    fflush(errorFile);
257f46a6179Smrg}
258f46a6179Smrg
259f46a6179Smrg/***====================================================================***/
260f46a6179Smrg
261f46a6179Smrgvoid
262f46a6179SmrguWarning(const char *s, ...)
263f46a6179Smrg{
26434345a63Smrg    va_list args;
265f46a6179Smrg
26634345a63Smrg    if ((outCount == 0) && (preMsg != NULL))
26734345a63Smrg        fprintf(errorFile, "%s\n", preMsg);
26834345a63Smrg    if (prefix != NULL)
26934345a63Smrg        fprintf(errorFile, "%s", prefix);
27034345a63Smrg    fprintf(errorFile, "Warning:          ");
271f46a6179Smrg    va_start(args, s);
27234345a63Smrg    vfprintf(errorFile, s, args);
273f46a6179Smrg    va_end(args);
274f46a6179Smrg    fflush(errorFile);
275f46a6179Smrg    outCount++;
276f46a6179Smrg}
277f46a6179Smrg
278f46a6179Smrg/***====================================================================***/
279f46a6179Smrg
280f46a6179Smrgvoid
281f46a6179SmrguError(const char *s, ...)
282f46a6179Smrg{
28334345a63Smrg    va_list args;
284f46a6179Smrg
28534345a63Smrg    if ((outCount == 0) && (preMsg != NULL))
28634345a63Smrg        fprintf(errorFile, "%s\n", preMsg);
28734345a63Smrg    if (prefix != NULL)
28834345a63Smrg        fprintf(errorFile, "%s", prefix);
28934345a63Smrg    fprintf(errorFile, "Error:            ");
290f46a6179Smrg    va_start(args, s);
29134345a63Smrg    vfprintf(errorFile, s, args);
292f46a6179Smrg    va_end(args);
293f46a6179Smrg    fflush(errorFile);
294f46a6179Smrg    outCount++;
295f46a6179Smrg}
296f46a6179Smrg
297f46a6179Smrg/***====================================================================***/
298f46a6179Smrg
299f46a6179Smrgvoid
300f46a6179SmrguFatalError(const char *s, ...)
301f46a6179Smrg{
30234345a63Smrg    va_list args;
303f46a6179Smrg
30434345a63Smrg    if ((outCount == 0) && (preMsg != NULL))
30534345a63Smrg        fprintf(errorFile, "%s\n", preMsg);
30634345a63Smrg    if (prefix != NULL)
30734345a63Smrg        fprintf(errorFile, "%s", prefix);
30834345a63Smrg    fprintf(errorFile, "Fatal Error:      ");
309f46a6179Smrg    va_start(args, s);
31034345a63Smrg    vfprintf(errorFile, s, args);
311f46a6179Smrg    va_end(args);
31234345a63Smrg    fprintf(errorFile, "                  Exiting\n");
313f46a6179Smrg    fflush(errorFile);
314f46a6179Smrg    outCount++;
315f46a6179Smrg    exit(1);
316f46a6179Smrg    /* NOTREACHED */
317f46a6179Smrg}
318f46a6179Smrg
319f46a6179Smrg/***====================================================================***/
320f46a6179Smrg
321f46a6179Smrgvoid
322f46a6179SmrguInternalError(const char *s, ...)
323f46a6179Smrg{
32434345a63Smrg    va_list args;
325f46a6179Smrg
32634345a63Smrg    if ((outCount == 0) && (preMsg != NULL))
32734345a63Smrg        fprintf(errorFile, "%s\n", preMsg);
32834345a63Smrg    if (prefix != NULL)
32934345a63Smrg        fprintf(errorFile, "%s", prefix);
33034345a63Smrg    fprintf(errorFile, "Internal error:   ");
331f46a6179Smrg    va_start(args, s);
33234345a63Smrg    vfprintf(errorFile, s, args);
333f46a6179Smrg    va_end(args);
334f46a6179Smrg    fflush(errorFile);
335f46a6179Smrg    outCount++;
336f46a6179Smrg}
337f46a6179Smrg
338f46a6179Smrgvoid
339f46a6179SmrguSetPreErrorMessage(char *msg)
340f46a6179Smrg{
34134345a63Smrg    outCount = 0;
34234345a63Smrg    preMsg = msg;
343f46a6179Smrg    return;
344f46a6179Smrg}
345f46a6179Smrg
346f46a6179Smrgvoid
347f46a6179SmrguSetPostErrorMessage(char *msg)
348f46a6179Smrg{
34934345a63Smrg    postMsg = msg;
350f46a6179Smrg    return;
351f46a6179Smrg}
352f46a6179Smrg
353f46a6179Smrgvoid
354f46a6179SmrguSetErrorPrefix(char *pre)
355f46a6179Smrg{
35634345a63Smrg    prefix = pre;
357f46a6179Smrg    return;
358f46a6179Smrg}
359f46a6179Smrg
360f46a6179Smrgvoid
361f46a6179SmrguFinishUp(void)
362f46a6179Smrg{
36334345a63Smrg    if ((outCount > 0) && (postMsg != NULL))
36434345a63Smrg        fprintf(errorFile, "%s\n", postMsg);
365f46a6179Smrg    return;
366f46a6179Smrg}
367f46a6179Smrg
368f46a6179Smrg/***====================================================================***/
369f46a6179Smrg
370f46a6179Smrg#ifndef HAVE_STRDUP
371f46a6179Smrgchar *
372f46a6179SmrguStringDup(const char *str)
373f46a6179Smrg{
37434345a63Smrg    char *rtrn;
375f46a6179Smrg
37634345a63Smrg    if (str == NULL)
37734345a63Smrg        return NULL;
37834345a63Smrg    rtrn = (char *) uAlloc(strlen(str) + 1);
37934345a63Smrg    strcpy(rtrn, str);
380f46a6179Smrg    return rtrn;
381f46a6179Smrg}
382f46a6179Smrg#endif
383f46a6179Smrg
384f46a6179Smrg#ifndef HAVE_STRCASECMP
385f46a6179Smrgint
386f46a6179SmrguStrCaseCmp(const char *str1, const char *str2)
387f46a6179Smrg{
38834345a63Smrg    char buf1[512], buf2[512];
389f46a6179Smrg    char c, *s;
390f46a6179Smrg    register int n;
391f46a6179Smrg
39234345a63Smrg    for (n = 0, s = buf1; (c = *str1++); n++)
39334345a63Smrg    {
39434345a63Smrg        if (isupper(c))
39534345a63Smrg            c = tolower(c);
39634345a63Smrg        if (n > 510)
39734345a63Smrg            break;
39834345a63Smrg        *s++ = c;
399f46a6179Smrg    }
400f46a6179Smrg    *s = '\0';
40134345a63Smrg    for (n = 0, s = buf2; (c = *str2++); n++)
40234345a63Smrg    {
40334345a63Smrg        if (isupper(c))
40434345a63Smrg            c = tolower(c);
40534345a63Smrg        if (n > 510)
40634345a63Smrg            break;
40734345a63Smrg        *s++ = c;
408f46a6179Smrg    }
409f46a6179Smrg    *s = '\0';
410f46a6179Smrg    return (strcmp(buf1, buf2));
411f46a6179Smrg}
412f46a6179Smrg
413f46a6179Smrgint
414f46a6179SmrguStrCasePrefix(const char *my_prefix, char *str)
415f46a6179Smrg{
416f46a6179Smrg    char c1;
417f46a6179Smrg    char c2;
41834345a63Smrg    while (((c1 = *my_prefix) != '\0') && ((c2 = *str) != '\0'))
41934345a63Smrg    {
42034345a63Smrg        if (isupper(c1))
42134345a63Smrg            c1 = tolower(c1);
42234345a63Smrg        if (isupper(c2))
42334345a63Smrg            c2 = tolower(c2);
42434345a63Smrg        if (c1 != c2)
42534345a63Smrg            return 0;
42634345a63Smrg        my_prefix++;
42734345a63Smrg        str++;
428f46a6179Smrg    }
42934345a63Smrg    if (c1 != '\0')
43034345a63Smrg        return 0;
431f46a6179Smrg    return 1;
432f46a6179Smrg}
433f46a6179Smrg
434f46a6179Smrg#endif
435