File: error.c

package info (click to toggle)
super 3.30.0-3%2Bsqueeze2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,088 kB
  • ctags: 755
  • sloc: ansic: 10,089; sh: 288; makefile: 201
file content (599 lines) | stat: -rw-r--r-- 16,258 bytes parent folder | download | duplicates (4)
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
static const char rcsid[] = "$Id: error.c,v 1.79 2007/03/07 00:23:27 will Exp $";

/*
 *	Copyright (c) 1993 by California Institute of Technology.
 *	Written by William Deich.  Not derived from licensed software.

 *    You may distribute under the terms of either the GNU General Public
 *    License or the Artistic License, as specified in the README file.
    
 */

/********************************************************************/
/*
 * If you don't have localsys.h, supply the following:
 * #define HAVE_STDARG_H   if you have <stdarg.h> (ie ANSI C variadic arg lists)
 * #define HAVE_SYSLOG_H   if you have <syslog.h> and syslog() function
 */

/*
 * If you don't like the following "priority" and "facility" values for
 * use with syslog(), supply the following:
 * #define SYSLOG_PRIORITY nnn	...priority for logging if syslog() use is
 *					enabled; default is LOG_ERR.
 * #define SYSLOG_FACILITY nnn	...syslog() facility code if syslog() use is
 *					enabled; default is LOG_USER.

 * We call Strerror(e) to return error messages for error code e.  This
 * routine is expected to simply be strerror(e) + wrapper code to ensure
 * that e is in the valid range.
 */
#include "localsys.h"

/********************************************************************/

/* Error -- print error message, then optionally die.

 * Usage:	Error(show_perror, die, format, args... );
 *	Print error message according to format & args.
 *	If show_perror != 0 && errno != 0, follow error message with perror("").
 *	(If show_perror !=0, but errno==0, follow error msg with "\n").
 *	If die != 0, exit with exit(die).

 * There are several external variables that a calling program can modify:
 *	error_prog: program name preceding msgs to stderr.  Default off.
 *	error_log_prog: program name preceding msgs to logfile.  Default off.
 *	error_stderr: controls whether messages go to stderr.  Default enabled.
 *	error_logfile: controls whether messages go to a logfile. Default off.
 *	error_command: controls whether msgs go to a popen'd command. Def off.
 *	error_syslog: controls whether messages go to [r]syslog.  Default: off.
 *	error_rlog_host: host to receive rsyslog() messages.  Def: ourselves.
 *	error_user: Controls username used when printing messages.
 *	error_tag: Controls use of error_{srcfile,line,nl}.  Default off.
 *	error_fflush: fflush(stdout) before printing error message.  Default on.
 *	error_srcfile, error_line, error_nl: error_srcfile is a file in
 *			which an error was found, starting at line error_line
 *			and continuing over error_nl lines.
 *	error_counter: incremented with every call to Error().
 * In detail:

 * If error_prog != NULL, then the message to stderr is preceded
 *	with "<error_prog>: ".

 * If error_stderr == 0, then the message is NOT directed to stderr.
 *	By default error_stderr == 1.

 * If error_logfile != NULL, then the message is also directed to
 *	that file, preceded with
 *		error_log_prog: user@hostname timestamp
 * 	If error_log_prog is NULL, it isn't printed.  We keep separate
 *	error_prog and error_log_prog so that the program name can be
 *	printed on stderr (where there might otherwise be confusion to the
 *	user about which program it is) but optionally not printed in the
 *	logfile, which is typically unique to the program, so that the
 *	program name is redundant.

 * If error_user != NULL, then the username used in messages is error_user,
 *	instead of the default name found by looking at the user's password
 *	entry.

 * If error_command != NULL and *error_command != '\0', then error_command
 *	is popen'd and the message is piped in.  If the popen fails, Error()
 *	is silent about the problem.  NOTE: the command is executed separately
 *	for each call to this routine.

 * If HAVE_SYSLOG_H is defined, and the caller sets error_syslog != 0,
 *	then the message is passed to [r]syslog(), at priority error_priority,
 *	using facility error_facility.  We compile with rsyslog, but if the
 *	remote host argument is NULL at the time we open the log, we use the
 *	syslog routines instead.
 *	The fmt string and the printf output must be less
 *	than MAXPRINT characters each. This is because syslog() accepts a
 *	printf-style variadic argument list, but it doesn't have a va_list
 *	version.  Therefore we print into a string and pass that onto syslog().
 *	As a side effect, you can't use syslog-specific "%m" in the fmt.
 *	If the error_prog string is non-null, then just before the first
 *	call to syslog, openlog is called with an ident string = error_prog.
 *	Note that this is done just once: you can't change error_prog
 *	between messages.

 * If error_tag is !0, or if the _first_ two characters of the output
 *	format are "%t", then the output is preceded with a message like
 *	the following:
 *  (a) error_srcfile == NULL:
 *	""				# error_line <= 0
 *	"line %d: "			# error_line > 0,  error_nl <= 1
 *	"lines %d..%d: "		# error_line > 0,  error_nl > 1
 *
 *  (b) error_srcfile == "-":
 *	"in <stdin>: "			# error_line <= 0
 *	"line %d in <stdin>: "		# error_line > 0,  error_nl <= 1
 *	"lines %d..%d in <stdin>: "	# error_line > 0,  error_nl > 1

 *  (c) error_srcfile == anothername:
 *	"in file `%s': "		# error_line <= 0
 *	"line %d in file `%s': "	# error_line > 0,  error_nl <= 1
 *	"lines %d..%d in file `%s': "	# error_line > 0, error_nl > 1

 * Notes:
 *	1. If error_prog and this line information is printed, then
 *	the program name is _not_ suffixed with ":" -- that way, the
 *	output looks something like:
 *		progxyz (lines 232..255 in file `.......'): errmsg
 *	2. In the event that the first two characters are "%t", they enable
 *	tagline printing but are not printed.

 * Return code is -1, so you can print error messages and return an error
 *	code with   return Error(...);
 */


FILE *error_logfile = NULL;
char *error_prog = NULL;
char *error_log_prog = NULL;
char *error_command = NULL;
char *error_user = NULL;

int error_counter = 0;

int error_stderr = 1;

int error_syslog = 0;
char *error_rlog_host = NULL;

int error_line = -1;
int error_nl = -1;
char *error_srcfile = NULL;
int error_tag = 0;
int error_fflush = 1;

#ifdef HAVE_SYSLOG_H

/* Default error priority */
#ifndef SYSLOG_PRIORITY
#define SYSLOG_PRIORITY LOG_ERR
#endif

/* Default error facility */
#ifndef SYSLOG_FACILITY
#ifdef LOG_USER
#define SYSLOG_FACILITY LOG_USER
#else
#define SYSLOG_FACILITY 0
#endif
#endif

void ropenlog P__(( char *ident, int logopt, int facility, char *host ));
void rsyslog P__(( unsigned int level, char *fmt, ... ));

int error_priority = SYSLOG_PRIORITY;
int error_facility = SYSLOG_FACILITY;
int openlog_done = 0;

static int using_rsyslog = 0;

void
OpenLog(prog, opt, fac)
char *prog;
int opt;
int fac;
{
    if (error_rlog_host != NULL) {
	ropenlog(prog, opt, fac, error_rlog_host);
	using_rsyslog = 1;
    } else {
	openlog(prog, opt, fac);
	using_rsyslog = 0;
    }
}

void
SysLog(pri, buf)
int pri;
char *buf;
{
    if (using_rsyslog) {
	rsyslog(pri, "%s", buf);
    } else {
	syslog(pri, "%s", buf);
    }
}

#endif

static int uid = -1;
static char user[128] = "";
static char hostname[1024] = "";

extern char *Strerror();

#define MAXPRINT 1300

#define StrLCat(s1, s2, maxlen)  strncat(s1, s2, ((maxlen) - strlen(s1) - 1))

char *taglines();

#ifdef HAVE_STDARG_H
/* VARARGS3 */
int
Error(
    int show_perror,	/* If errno != 0, follow msg with perror("") */
    int die,		/* If !0, exit with exit(die) */
    char *fmt,		/* Print rest of args with fprintf(stderr, fmt, ...) */
    ... )
{
    va_list ap;
    int error;
    FILE *error_cmd = NULL;
    char *tag;
    int pctt;

    if (error_fflush)
	fflush(stdout);

    error = errno;

    error_counter++;

    /* Figure out line tagging */
    pctt = strncmp(fmt, "%t", 2) == 0;
    tag = ( pctt || error_tag ) ? taglines(3) : NULL;
    if (pctt)
	fmt += 2;

    /* Program name */
    if (error_stderr && error_prog)
	(void) fprintf(stderr, "%s%s ", error_prog, (tag && *tag) ? "" : ":");

    if (error_command && *error_command)
	error_cmd = popen(error_command, "w");

    if (error_log_prog) {
	if (error_logfile)
	    (void) fprintf(error_logfile, "%s%s ", error_log_prog,
						(tag && *tag) ? "" : ":");
	if (error_cmd)
	    (void) fprintf(error_cmd, "%s%s ", error_log_prog,
						(tag && *tag) ? "" : ":");
    }

    if (error_logfile || error_syslog || error_command) {
	if (getuid() != uid || *user == '\0') {
	    struct passwd *pw;
	    int e = errno;
	    pw = getpwuid((uid=getuid()));
	    if (pw) (void) strcpy(user, pw->pw_name);
	    errno = e;
	}
    }

    if (error_logfile || error_cmd) {
	/* user@hostname & timestamp */
	char *s;
	time_t tptr;
	if (*hostname == '\0')
	    (void) gethostname(hostname, sizeof(hostname));
	(void) time(&tptr);
	s = ctime(&tptr);
	s[strlen(s) - 1] = '\0';
	if (error_logfile)
	    (void) fprintf(error_logfile, "%s@%s %s\t",
			    error_user ? error_user : user,
			    hostname, s);
	if (error_cmd)
	    (void) fprintf(error_cmd, "%s@%s %s\t",
			    error_user ? error_user : user,
			    hostname, s);
    }

    if (error_stderr) {
	if (tag)
	    (void) fputs(tag, stderr);
	/* User's msg */
	va_start(ap, fmt);
	(void) vfprintf(stderr, fmt, ap);
	va_end(ap);
    }

    if (error_logfile) {
	if (tag)
	    (void) fputs(tag, error_logfile);
	/* User's msg */
	va_start(ap, fmt);
	(void) vfprintf(error_logfile, fmt, ap);
	va_end(ap);
    }

    if (error_cmd) {
	if (tag)
	    (void) fputs(tag, error_cmd);
	/* User's msg */
	va_start(ap, fmt);
	(void) vfprintf(error_cmd, fmt, ap);
	va_end(ap);
    }

    if (show_perror) {
	if (error) {
	    errno = error;
	    if (error_stderr)
		perror("");
	    if (error_logfile) {
		(void) fprintf(error_logfile, "%s\n", Strerror(error));
	    }
	    if (error_cmd) {
		(void) fprintf(error_cmd, "%s\n", Strerror(error));
	    }
	} else {
	    if (error_stderr)
		(void) fputc('\n', stderr);
	    if (error_logfile)
		(void) fputc('\n', error_logfile);
	    if (error_cmd)
		(void) fputc('\n', error_cmd);
	}
    }

#ifdef HAVE_SYSLOG_H
    if (error_syslog) {
	char newfmt[MAXPRINT], buf[MAXPRINT];
	if (!openlog_done) {
	    OpenLog(error_prog ? error_prog : "", 0, error_facility);
	    openlog_done = 1;
	}
	sprintf(newfmt, "(%s) ", error_user ? error_user : user);
	StrLCat(newfmt, fmt, sizeof(newfmt));
	if (tag)
	    StrLCat(newfmt, tag, sizeof(newfmt));
	va_start(ap, fmt);
	(void) vsprintf(buf, newfmt, ap);
	va_end(ap);
	SysLog(error_priority, buf);
    }
#endif

    if (die)
	(void) exit(die);

    if (error_cmd)
	pclose(error_cmd);

    return -1;

}
#else

/* VARARGS3 */
int
Error( va_alist )
va_dcl
{
    va_list ap;
    int die, show_perror;
    char *fmt, *orig_fmt;
    int error;
    char *tag;
    int pctt;
    FILE *error_cmd = NULL;

    if (error_fflush)
	fflush(stdout);

    error = errno;

    /* Figure out line tagging */
    va_start(ap);
    show_perror = va_arg(ap, int);
    die = va_arg(ap, int);
    fmt = va_arg(ap, char *);
    va_end(ap);
    pctt = strncmp(fmt, "%t", 2) == 0;
    tag = ( pctt || error_tag ) ? taglines(3) : NULL;
    if (pctt)
	fmt += 2;

    /* Program name */
    if (error_stderr && error_prog)
	(void) fprintf(stderr, "%s%s ", error_prog, (tag && *tag) ? "" : ":");

    if (error_command && *error_command)
	error_cmd = popen(error_command, "w");

    if (error_log_prog) {
	if (error_logfile)
	    (void) fprintf(error_logfile, "%s%s ", error_log_prog,
						(tag && *tag) ? "" : ":");
	if (error_cmd)
	    (void) fprintf(error_cmd, "%s%s ", error_log_prog,
						(tag && *tag) ? "" : ":");
    }

    if (error_logfile || error_syslog || error_command) {
	if (getuid() != uid || *user == '\0') {
	    struct passwd *pw;
	    int e = errno;
	    pw = getpwuid((uid=getuid()));
	    if (pw) (void) strcpy(user, pw->pw_name);
	    errno = e;
	}
    }

    if (error_logfile || error_cmd) {
	/* user@hostname & timestamp */
	char *s;
	time_t tptr;
	if (*hostname == '\0')
	    (void) gethostname(hostname, sizeof(hostname));
	(void) time(&tptr);
	s = ctime(&tptr);
	s[strlen(s) - 1] = '\0';
	if (error_logfile)
	    (void) fprintf(error_logfile, "%s@%s %s\t",
				error_user ? error_user : user,
				hostname, s);
	if (error_cmd)
	    (void) fprintf(error_cmd, "%s@%s %s\t",
				error_user ? error_user : user,
				hostname, s);
    }

    if (error_stderr) {
	if (tag)
	    (void) fputs(tag, stderr);
	/* User's msg */
	va_start(ap);
	show_perror = va_arg(ap, int);
	die = va_arg(ap, int);
	orig_fmt = va_arg(ap, char *);
	(void) vfprintf(stderr, fmt, ap);
	va_end(ap);
    }

    if (error_logfile) {
	if (tag)
	    (void) fputs(tag, error_logfile);
	/* User's msg */
	va_start(ap);
	show_perror = va_arg(ap, int);
	die = va_arg(ap, int);
	orig_fmt = va_arg(ap, char *);
	(void) vfprintf(error_logfile, fmt, ap);
	va_end(ap);
    }

    if (error_cmd) {
	if (tag)
	    (void) fputs(tag, error_cmd);
	/* User's msg */
	va_start(ap);
	show_perror = va_arg(ap, int);
	die = va_arg(ap, int);
	orig_fmt = va_arg(ap, char *);
	(void) vfprintf(error_cmd, fmt, ap);
	va_end(ap);
    }

    /* Figure out if we do show_perror */
    va_start(ap);
    show_perror = va_arg(ap, int);
    die = va_arg(ap, int);
    va_end(ap);

    if (show_perror) {
	if (error) {
	    errno = error;
	    if (error_stderr)
		perror("");
	    if (error_logfile)
		(void) fprintf(error_logfile, "%s\n", Strerror(error));
	    if (error_cmd)
		(void) fprintf(error_cmd, "%s\n", Strerror(error));
	} else {
	    if (error_stderr)
		(void) fputc('\n', stderr);
	    if (error_logfile)
		(void) fputc('\n', error_logfile);
	    if (error_cmd)
		(void) fputc('\n', error_cmd);
	}
    }

#ifdef HAVE_SYSLOG_H
    if (error_syslog) {
	char newfmt[MAXPRINT], buf[MAXPRINT];
	va_start(ap);
	show_perror = va_arg(ap, int);
	die = va_arg(ap, int);
	orig_fmt = va_arg(ap, char *);
	if (!openlog_done) {
	    OpenLog(error_prog ? error_prog : "", 0, error_facility);
	    openlog_done = 1;
	}
	sprintf(newfmt, "(%s) ", error_user ? error_user : user);
	StrLCat(newfmt, fmt, sizeof(newfmt));
	if (tag)
	    StrLCat(newfmt, tag, sizeof(newfmt));
	(void) vsprintf(buf, newfmt, ap);
	va_end(ap);
	SysLog(error_priority, buf);
    }
#endif

    if (die)
	(void) exit(die);

    if (error_cmd)
	pclose(error_cmd);

    return -1;

}
#endif

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* For tagging error text with a prefixing lines indicator. */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
char *
taglines(decorations)
int decorations;	/* 1 = enclose in parens; 2 = add a ": "; 3 = both */
{
    static char buf[MAXPRINT];
    int putparens = decorations & 01;
    int putcolon = decorations & 02;

    if (error_srcfile == NULL) {
	if (error_line <= 0)
	    return "";
	else if (error_nl <= 1)
	    (void) sprintf(buf, "%sline %d%s%s",
				putparens ? "(" : "",
				error_line,
				putparens ? ")" : "",
				putcolon ? ": " : "");
	else
	    (void) sprintf(buf, "%slines %d..%d%s%s",
				putparens ? "(" : "",
				error_line, error_line + error_nl - 1,
				putparens ? ")" : "",
				putcolon ? ": " : "");
    } else if (strcmp(error_srcfile, "-") == 0) {
	if (error_line <= 0)
	    (void) sprintf(buf, "%sin <stdin>%s%s",
				putparens ? "(" : "",
				putparens ? ")" : "",
				putcolon ? ": " : "");
	else if (error_nl <= 1)
	    (void) sprintf(buf, "%sline %d in <stdin>%s%s",
				putparens ? "(" : "",
				error_line,
				putparens ? ")" : "",
				putcolon ? ": " : "");
	else
	    (void) sprintf(buf, "%slines %d..%d in <stdin>%s%s",
				putparens ? "(" : "",
				error_line, error_line + error_nl - 1,
				putparens ? ")" : "",
				putcolon ? ": " : "");
    } else {
	if (error_line <= 0)
	    (void) sprintf(buf, "%sin file `%s'%s%s",
				putparens ? "(" : "",
				error_srcfile,
				putparens ? ")" : "",
				putcolon ? ": " : "");
	else if (error_nl <= 1)
	    (void) sprintf(buf, "%sline %d in file `%s'%s%s",
				putparens ? "(" : "",
				error_line, error_srcfile,
				putparens ? ")" : "",
				putcolon ? ": " : "");
	else
	    (void) sprintf(buf, "%slines %d..%d in file `%s'%s%s",
				putparens ? "(" : "",
				error_line, error_line + error_nl - 1,
				error_srcfile,
				putparens ? ")" : "",
				putcolon ? ": " : "");
    }
    return buf;
}