[go: up one dir, main page]

File: error.c

package info (click to toggle)
cook 2.19-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,316 kB
  • ctags: 3,969
  • sloc: ansic: 50,331; sh: 13,190; makefile: 4,542; yacc: 3,174; perl: 224; awk: 154
file content (454 lines) | stat: -rw-r--r-- 8,428 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 *	cook - file construction tool
 *	Copyright (C) 1993, 1994, 1995, 1997, 1998, 1999, 2001 Peter Miller;
 *	All rights reserved.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to report errors
 */

#include <ac/ctype.h>
#include <ac/errno.h>
#include <ac/stddef.h>
#include <ac/stdio.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/stdarg.h>

#include <error.h>
#include <mprintf.h>
#include <progname.h>
#include <quit.h>
#include <star.h>


/*
 * NAME
 *	wrap - wrap s string over lines
 *
 * SYNOPSIS
 *	void wrap(char *);
 *
 * DESCRIPTION
 *	The wrap function is used to print error messages onto stderr
 *	wrapping ling lines.
 *
 * CAVEATS
 *	Line length is assumed to be 80 characters.
 */

#define PAGE_WIDTH 79

static void wrap _((char *));

static void
wrap(s)
	char	*s;
{
	static char escapes[] = "\rr\nn\ff\bb\tt";
	char	tmp[PAGE_WIDTH + 2];
	int	first_line;
	char	*tp;
	int	bomb_later;
	char	*progname;

	/*
	 * Flush stdout so that errors are in sync with the output.
	 * If you get an error doing this, whinge about it _after_ reporting
	 * the originating error.  Also, clear the error on stdout to
	 * avoid getting caught in an infinite loop.
	 */
	star_eoln();
	if (fflush(stdout) || ferror(stdout))
	{
		bomb_later = errno;
		clearerr(stdout);
	}
	else
		bomb_later = 0;

	progname = progname_get();
	first_line = 1;
	while (*s)
	{
		char	*ep;
		int	ocol;

		/*
		 * Work out how many characters fit on the line.
		 */
		if (first_line)
			ocol = strlen(progname) + 2;
		else
			ocol = 8;
		for (ep = s; *ep; ++ep)
		{
			int	cw;
			int	c;

			c = (unsigned char)*ep;
			if (isprint(c))
				cw = 1 + (c == '\\');
			else
				cw = (strchr(escapes, c) ? 2 : 4);
			if (ocol + cw > PAGE_WIDTH)
				break;
			ocol += cw;
		}

		/*
		 * see if there is a better place to break the line
		 */
		if (*ep && *ep != ' ')
		{
			char	*mp;
			char	*bp_space;
			char	*bp_slash;

			bp_space = 0;
			for (mp = ep; mp > s; --mp)
			{
				if (mp[-1] == ' ')
				{
					bp_space = mp;
					break;
				}
			}

			bp_slash = 0;
			for (mp = ep; mp > s; --mp)
			{
				if (strchr("\\/", mp[-1]))
				{
					bp_slash = mp;
					break;
				}
			}

			/*
			 * We could break it at the space, and only use
			 * the slash if there are no spaces on the line.
			 * This can lead to large amounts of wasted
			 * space, particularly for link commands.  So, if
			 * both breaks are possible, and the space break
			 * is before the slash break, and the space
			 * break is in the left half of the line, use
			 * the slash break.
			 */
			if
			(
				bp_space
			&&
				bp_slash
			&&
				bp_space < bp_slash
			&&
				bp_space < s + 30
			)
				bp_space = 0;

			/*
			 * use the break if available
			 */
			if (bp_space)
				ep = bp_space;
			else if (bp_slash)
				ep = bp_slash;
		}

		/*
		 * ignore trailing blanks
		 */
		while (ep > s && ep[-1] == ' ')
			ep--;

		/*
		 * print the line
		 */
		if (first_line)
			sprintf(tmp, "%s: ", progname);
		else
			strcpy(tmp, "\t");
		tp = tmp + strlen(tmp);
		while (s < ep)
		{
			int	c;

			c = (unsigned char)*s++;
			if (isprint(c))
			{
				if (c == '\\')
					*tp++ = '\\';
				*tp++ = c;
			}
			else
			{
				char	*esc;

				esc = strchr(escapes, c);
				if (esc)
				{
					*tp++ = '\\';
					*tp++ = esc[1];
				}
				else
				{
					sprintf(tp, "\\%3.3o", c);
					tp += strlen(tp);
				}
			}
		}
		*tp++ = '\n';
		*tp = 0;
		fputs(tmp, stderr);
		if (ferror(stderr))
			break;

		/*
		 * skip leading spaces for subsequent lines
		 */
		while (*s == ' ')
			s++;
		first_line = 0;
	}
	if (fflush(stderr) || ferror(stderr))
	{
		/* don't print why, there is no point! */
		quit(1);
	}
	if (bomb_later)
	{
		errno = bomb_later;
		nfatal_raw("standard output");
	}
}


static void double_jeopardy _((void));

static void
double_jeopardy()
{
	char	buffer[200];

	sprintf
	(
		buffer,
		"while attempting to construct an error message: %s (fatal)",
		strerror(errno)
	);
	wrap(buffer);
	quit(1);
}


static char *copy_a_string _((char *));

static char *
copy_a_string(s)
	char		*s;
{
	char		*cp;

	errno = 0;
	cp = malloc(strlen(s) + 1);
	if (!cp)
	{
		if (!errno)
			errno = ENOMEM;
		double_jeopardy();
	}
	strcpy(cp, s);
	return cp;
}


/*
 * NAME
 *	error - place a message on the error stream
 *
 * SYNOPSIS
 *	void error(char *s, ...);
 *
 * DESCRIPTION
 *	Error places a message on the error output stream.
 *	The first argument is a printf-like format string,
 *	optionally followed by other arguments.
 *	The message will be prefixed by the program name and a colon,
 *	and will be terminated with a newline, automatically.
 *
 * CAVEAT
 *	Things like "error(filename)" blow up if the filename
 *	contains a '%' character.
 */

void
#if defined(__STDC__) && __STDC__
error_raw(char *s, ...)
#else
error_raw(s sva_last)
	char		*s;
	sva_last_decl
#endif
{
	va_list		ap;
	char		*msg;

	sva_init(ap, s);
	msg = vmprintf(s, ap);
	va_end(ap);
	if (!msg)
		double_jeopardy();
	wrap(msg);
}


/*
 * NAME
 *	nerror - place a system fault message on the error stream
 *
 * SYNOPSIS
 *	void nerror(char *s, ...);
 *
 * DESCRIPTION
 *	Nerror places a message on the error output stream.
 *	The first argument is a printf-like format string,
 *	optionally followed by other arguments.
 *	The message will be prefixed by the program name and a colon,
 *	and will be terminated with a text description of the error
 *	indicated by the 'errno' global variable, automatically.
 *
 * CAVEAT
 *	Things like "nerror(filename)" blow up if the filename
 *	contains a '%' character.
 */

/*VARARGS1*/
void
#if defined(__STDC__) && __STDC__
nerror_raw(char *s, ...)
#else
nerror_raw(s sva_last)
	char		*s;
	sva_last_decl
#endif
{
	va_list		ap;
	int		n;
	char		*msg;

	n = errno;
	sva_init(ap, s);
	msg = vmprintf(s, ap);
	va_end(ap);
	if (!msg)
		double_jeopardy();
	msg = copy_a_string(msg);
	error_raw("%s: %s", msg, strerror(n));
	free(msg);
}


/*
 * NAME
 *	nfatal - place a system fault message on the error stream and exit
 *
 * SYNOPSIS
 *	void nfatal(char *s, ...);
 *
 * DESCRIPTION
 *	Nfatal places a message on the error output stream and exits.
 *	The first argument is a printf-like format string,
 *	optionally followed by other arguments.
 *	The message will be prefixed by the program name and a colon,
 *	and will be terminated with a text description of the error
 *	indicated by the 'errno' global variable, automatically.
 *
 * CAVEAT
 *	Things like "nfatal(filename)" blow up if the filename
 *	contains a '%' character.
 *
 *	This function does NOT return.
 */

/*VARARGS1*/
void
#if defined(__STDC__) && __STDC__
nfatal_raw(char *s, ...)
#else
nfatal_raw(s sva_last)
	char		*s;
	sva_last_decl
#endif
{
	va_list		ap;
	int		n;
	char		*msg;

	n = errno;
	sva_init(ap, s);
	msg = vmprintf(s, ap);
	va_end(ap);
	if (!msg)
		double_jeopardy();
	msg = copy_a_string(msg);
	error_raw("%s: %s", msg, strerror(n));
	free(msg);
	quit(1);
}


/*
 * NAME
 *	fatal - place a message on the error stream and exit
 *
 * SYNOPSIS
 *	void fatal(char *s, ...);
 *
 * DESCRIPTION
 *	Fatal places a message on the error output stream and exits.
 *	The first argument is a printf-like format string,
 *	optionally followed by other arguments.
 *	The message will be prefixed by the program name and a colon,
 *	and will be terminated with a newline, automatically.
 *
 * CAVEAT
 *	Things like "error(filename)" blow up if the filename
 *	contains a '%' character.
 *
 *	This function does NOT return.
 */

void
#if defined(__STDC__) && __STDC__
fatal_raw(char *s, ...)
#else
fatal_raw(s sva_last)
	char		*s;
	sva_last_decl
#endif
{
	va_list		ap;
	char		*msg;

	sva_init(ap, s);
	msg = vmprintf(s, ap);
	va_end(ap);
	if (!msg)
		double_jeopardy();
	wrap(msg);
	quit(1);
}