c4f2d992 |
1 | /* -*-c-*- |
2 | * |
03f996bd |
3 | * $Id: utils.c,v 1.2 1997/08/04 10:24:26 mdw Exp $ |
c4f2d992 |
4 | * |
5 | * Miscellaneous useful bits of code. |
6 | * |
7 | * (c) 1997 Mark Wooding |
8 | */ |
9 | |
03f996bd |
10 | /*----- Licensing notice --------------------------------------------------* |
c4f2d992 |
11 | * |
12 | * This file is part of `become' |
13 | * |
14 | * `Become' is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * `Become' is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
03f996bd |
25 | * along with `become'; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
c4f2d992 |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: utils.c,v $ |
03f996bd |
32 | * Revision 1.2 1997/08/04 10:24:26 mdw |
33 | * Sources placed under CVS control. |
34 | * |
35 | * Revision 1.1 1997/07/21 13:47:42 mdw |
c4f2d992 |
36 | * Initial revision |
37 | * |
38 | */ |
39 | |
40 | /*----- Header files ------------------------------------------------------*/ |
41 | |
42 | /* --- ANSI headers --- */ |
43 | |
03f996bd |
44 | #include <ctype.h> |
c4f2d992 |
45 | #include <stdarg.h> |
46 | #include <stdio.h> |
47 | #include <stdlib.h> |
03f996bd |
48 | #include <string.h> |
c4f2d992 |
49 | |
50 | /* --- Local headers --- */ |
51 | |
52 | #include "config.h" |
53 | #include "utils.h" |
54 | |
03f996bd |
55 | /*----- Program name handling ---------------------------------------------*/ |
c4f2d992 |
56 | |
03f996bd |
57 | /* --- Static data --- */ |
c4f2d992 |
58 | |
03f996bd |
59 | static const char *myname = 0; /* What's my name? */ |
c4f2d992 |
60 | |
61 | /* --- @quis@ --- * |
62 | * |
63 | * Arguments: --- |
64 | * |
65 | * Returns: Pointer to the program name. |
66 | * |
67 | * Use: Returns the program name. |
68 | */ |
69 | |
70 | const char *quis(void) |
71 | { |
72 | return (myname); |
73 | } |
74 | |
75 | /* --- @ego@ --- * |
76 | * |
77 | * Arguments: @const char *p@ = pointer to program name |
78 | * |
79 | * Returns: --- |
80 | * |
81 | * Use: Tells the utils library what the program's name is. |
82 | */ |
83 | |
84 | #ifndef PATHSEP |
85 | # if defined(__riscos) |
86 | # define PATHSEP '.' |
87 | # elif defined(__unix) || defined(unix) |
88 | # define PATHSEP '/' |
89 | # else |
90 | # define PATHSEP '\\' |
91 | # endif |
92 | #endif |
93 | |
94 | void ego(const char *p) |
95 | { |
96 | const char *q = p; |
97 | while (*q) { |
98 | if (*q++ == PATHSEP) |
99 | p = q; |
100 | } |
101 | myname = p; |
102 | } |
103 | |
104 | /*----- Error reporting ---------------------------------------------------*/ |
105 | |
106 | /* --- @moan@ --- * |
107 | * |
108 | * Arguments: @const char *f@ = a @printf@-style format string |
109 | * @...@ = other arguments |
110 | * |
111 | * Returns: --- |
112 | * |
113 | * Use: Reports an error. |
114 | */ |
115 | |
116 | void moan(const char *f, ...) |
117 | { |
118 | va_list ap; |
119 | va_start(ap, f); |
120 | fprintf(stderr, "%s: ", myname); |
121 | vfprintf(stderr, f, ap); |
122 | va_end(ap); |
123 | putc('\n', stderr); |
124 | } |
125 | |
126 | /* --- @die@ --- * |
127 | * |
128 | * Arguments: @const char *f@ = a @printf@-style format string |
129 | * @...@ = other arguments |
130 | * |
131 | * Returns: Never. |
132 | * |
133 | * Use: Reports an error and hari-kiris. Like @moan@ above, only |
134 | * more permanent. |
135 | */ |
136 | |
137 | void die(const char *f, ...) |
138 | { |
139 | va_list ap; |
140 | va_start(ap, f); |
141 | fprintf(stderr, "%s: ", myname); |
142 | vfprintf(stderr, f, ap); |
143 | va_end(ap); |
144 | putc('\n', stderr); |
145 | exit(EXIT_FAILURE); |
146 | } |
147 | |
03f996bd |
148 | /*----- Trace messages ----------------------------------------------------*/ |
149 | |
150 | #if defined(TRACING) || !defined(NDEBUG) |
151 | |
152 | /* --- Static data --- */ |
153 | |
154 | static FILE *tracefp = 0; /* Where does debugging go? */ |
155 | static unsigned int tracelvl = 0; /* How much tracing gets done? */ |
156 | |
157 | /* --- @trace@ --- * |
158 | * |
159 | * Arguments: @unsigned int lvl@ = trace level for output |
160 | * @const char *f@ = a @printf@-style format string |
161 | * @...@ = other arguments |
162 | * |
163 | * Returns: --- |
164 | * |
165 | * Use: Reports a message to the trace output. |
166 | */ |
167 | |
168 | void trace(unsigned int lvl, const char *f, ...) |
169 | { |
170 | va_list ap; |
171 | if ((lvl & tracing()) == 0) |
172 | return; |
173 | va_start(ap, f); |
174 | fprintf(tracefp, "*** %s: ", myname); |
175 | vfprintf(tracefp, f, ap); |
176 | va_end(ap); |
177 | putc('\n', tracefp); |
178 | } |
179 | |
180 | /* --- @traceblk@ --- * |
181 | * |
182 | * Arguments: @unsigned int lvl@ = trace level for output |
183 | * @const char *hdr@ = some header string to write |
184 | * @const void *blk@ = pointer to a block of memory to dump |
185 | * @size_t sz@ = size of the block of memory |
186 | * |
187 | * Returns: --- |
188 | * |
189 | * Use: Dumps the contents of a block to the trace output. |
190 | */ |
191 | |
192 | void traceblk(unsigned int lvl, const char *hdr, const void *blk, size_t sz) |
193 | { |
194 | const unsigned char *p = blk; |
195 | size_t i; |
196 | unsigned long o = 0; |
197 | size_t c; |
198 | |
199 | /* --- Skip if the trace level is too high --- */ |
200 | |
201 | if ((lvl & tracing()) == 0) |
202 | return; |
203 | |
204 | /* --- Now start work --- */ |
205 | |
206 | fprintf(tracefp, "*** %s: %s\n", myname, hdr); |
207 | |
208 | while (sz) { |
209 | fprintf(tracefp, "*** %s: %08lu : ", myname, o); |
210 | for (i = 0; i < 8; i++) { |
211 | if (i < sz) |
212 | fprintf(tracefp, "%02x ", p[i]); |
213 | else |
214 | fputs("** ", tracefp); |
215 | } |
216 | fputs(": ", tracefp); |
217 | for (i = 0; i < 8; i++) { |
218 | if (i < sz) |
219 | fputc(isprint(p[i]) ? p[i] : '.', tracefp); |
220 | else |
221 | fputc('*', tracefp); |
222 | } |
223 | fputc('\n', tracefp); |
224 | c = (sz >= 8) ? 8 : sz; |
225 | sz -= c, p += c, o += c; |
226 | } |
227 | } |
228 | |
229 | |
230 | /* --- @traceon@ --- * |
231 | * |
232 | * Arguments: @FILE *fp@ = a file to trace on |
233 | * @unsigned int lvl@ = trace level to set |
234 | * |
235 | * Returns: --- |
236 | * |
237 | * Use: Enables tracing to a file. |
238 | */ |
239 | |
240 | void traceon(FILE *fp, unsigned int lvl) |
241 | { |
242 | tracefp = fp; |
243 | if (!tracelvl) |
244 | tracelvl = lvl; |
245 | } |
246 | |
247 | /* --- @tracesetlvl@ --- * |
248 | * |
249 | * Arguments: @unsigned int lvl@ = trace level to set |
250 | * |
251 | * Returns: --- |
252 | * |
253 | * Use: Sets the tracing level. |
254 | */ |
255 | |
256 | void tracesetlvl(unsigned int lvl) { tracelvl = lvl; } |
257 | |
258 | /* --- @tracing@ --- * |
259 | * |
260 | * Arguments: --- |
261 | * |
262 | * Returns: Zero if not tracing, tracing level if tracing. |
263 | * |
264 | * Use: Informs the caller whether tracing is enabled. |
265 | */ |
266 | |
267 | unsigned int tracing(void) { return (tracefp ? tracelvl : 0u); } |
268 | |
269 | #endif |
270 | |
c4f2d992 |
271 | /*----- Memory management functions ---------------------------------------*/ |
272 | |
273 | /* --- @xmalloc@ --- * |
274 | * |
275 | * Arguments: @size_t sz@ = size of block to allocate |
276 | * |
277 | * Returns: Pointer to allocated block. |
278 | * |
279 | * Use: Allocates memory. If the memory isn't available, we don't |
280 | * hang aroung long enough for a normal function return. |
281 | */ |
282 | |
283 | void *xmalloc(size_t sz) |
284 | { |
285 | void *p = malloc(sz); |
286 | if (!p) |
287 | die("not enough memory"); |
288 | return (p); |
289 | } |
290 | |
291 | /* --- @xstrdup@ --- * |
292 | * |
293 | * Arguments: @const char *s@ = pointer to a string |
294 | * |
295 | * Returns: Pointer to a copy of the string. |
296 | * |
297 | * Use: Copies a string (like @strdup@ would, if it existed). |
298 | */ |
299 | |
300 | char *xstrdup(const char *s) |
301 | { |
302 | size_t sz = strlen(s) + 1; |
303 | char *p = xmalloc(sz); |
304 | memcpy(p, s, sz); |
305 | return (p); |
306 | } |
307 | |
308 | /* --- @xrealloc@ --- * |
309 | * |
310 | * Arguments: @void *p@ = pointer to a block of memory |
311 | * @size_t sz@ = new size desired for the block |
312 | * |
313 | * Returns: Pointer to the resized memory block (which is almost |
314 | * certainly not in the same place any more). |
315 | * |
316 | * Use: Resizes a memory block. |
317 | */ |
318 | |
319 | void *xrealloc(void *p, size_t sz) |
320 | { |
321 | p = realloc(p, sz); |
322 | if (!p) |
323 | die("not enough memory"); |
324 | return (p); |
325 | } |
326 | |
327 | /*----- That's all, folks -------------------------------------------------*/ |