1  /*\
2   *                          COPYRIGHT 1990
3   *                    DIGITAL EQUIPMENT CORPORATION
4   *                       MAYNARD, MASSACHUSETTS
5   *                        ALL RIGHTS RESERVED.
6   *
7   * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
8   * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
9   * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
10   * FOR ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED
11   * WARRANTY.
12   *
13   * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
14   * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
15   * ADDITION TO THAT SET FORTH ABOVE.
16   *
17   * Permission to use, copy, modify, and distribute this software and its
18   * documentation for any purpose and without fee is hereby granted, provided
19   * that the above copyright notice appear in all copies and that both that
20   * copyright notice and this permission notice appear in supporting
21   * documentation, and that the name of Digital Equipment Corporation not be
22   * used in advertising or publicity pertaining to distribution of the
23   * software without specific, written prior permission.
24  \*/
25
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
30#include 	"utils.h"
31#include	<ctype.h>
32#include	<stdlib.h>
33
34unsigned int debugFlags;
35
36
37/***====================================================================***/
38
39static FILE *errorFile = NULL;
40
41Boolean
42uSetErrorFile(const char *name)
43{
44    if ((errorFile != NULL) && (errorFile != stderr)) {
45        fprintf(errorFile, "switching to %s\n", name ? name : "stderr");
46        fclose(errorFile);
47    }
48    if (name != NullString)
49        errorFile = fopen(name, "w");
50    else
51        errorFile = stderr;
52    if (errorFile == NULL) {
53        errorFile = stderr;
54        return (False);
55    }
56    return (True);
57}
58
59void
60uInformation(const char *s, ...)
61{
62    va_list ap;
63
64    va_start(ap, s);
65    vfprintf(errorFile, s, ap);
66    fflush(errorFile);
67    va_end(ap);
68    return;
69}
70
71/***====================================================================***/
72
73void
74uAction(const char *s, ...)
75{
76    va_list ap;
77
78    va_start(ap, s);
79    fprintf(errorFile, "                  ");
80    vfprintf(errorFile, s, ap);
81    fflush(errorFile);
82    va_end(ap);
83    return;
84}
85
86/***====================================================================***/
87
88void
89uWarning(const char *s, ...)
90{
91    va_list ap;
92
93    va_start(ap, s);
94    fprintf(errorFile, "Warning:          ");
95    vfprintf(errorFile, s, ap);
96    fflush(errorFile);
97    va_end(ap);
98    return;
99}
100
101/***====================================================================***/
102
103void
104uError(const char *s, ...)
105{
106    va_list ap;
107
108    va_start(ap, s);
109    fprintf(errorFile, "Error:            ");
110    vfprintf(errorFile, s, ap);
111    fflush(errorFile);
112    va_end(ap);
113    return;
114}
115
116/***====================================================================***/
117
118void
119uFatalError(const char *s, ...)
120{
121    va_list ap;
122
123    va_start(ap, s);
124    fprintf(errorFile, "Fatal Error:      ");
125    vfprintf(errorFile, s, ap);
126    fprintf(errorFile, "                  Exiting\n");
127    fflush(errorFile);
128    va_end(ap);
129    exit(1);
130    /* NOTREACHED */
131}
132
133/***====================================================================***/
134
135void
136uInternalError(const char *s, ...)
137{
138    va_list ap;
139
140    va_start(ap, s);
141    fprintf(errorFile, "Internal error:   ");
142    vfprintf(errorFile, s, ap);
143    fflush(errorFile);
144    va_end(ap);
145    return;
146}
147
148/***====================================================================***/
149
150#ifndef HAVE_ASPRINTF
151int
152uAsprintf(char ** ret, const char *format, ...)
153{
154    char buf[256];
155    int len;
156    va_list ap;
157
158    va_start(ap, format);
159    len = vsnprintf(buf, sizeof(buf), format, ap);
160    va_end(ap);
161
162    if (len < 0)
163        return -1;
164
165    if (len < sizeof(buf))
166    {
167        *ret = strdup(buf);
168    }
169    else
170    {
171        *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */
172        if (*ret != NULL)
173        {
174            va_start(ap, format);
175            len = vsnprintf(*ret, len + 1, format, ap);
176            va_end(ap);
177            if (len < 0) {
178                free(*ret);
179                *ret = NULL;
180            }
181        }
182    }
183
184    if (*ret == NULL)
185        return -1;
186
187    return len;
188}
189#endif /* HAVE_ASPRINTF */
190