File: error.c

package info (click to toggle)
sex 0.19.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 572 kB
  • ctags: 779
  • sloc: ansic: 6,273; sh: 179; makefile: 116
file content (93 lines) | stat: -rw-r--r-- 2,090 bytes parent folder | download | duplicates (2)
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
/*
 * File:	error.c
 * Purpose:	Implement error reporting functions.
 * Author:	Lars Wirzenius
 * Version:	"@(#)SeX:$Id: error.c,v 1.1 1996/01/05 13:16:05 liw Exp $"
 */


#include <stdarg.h>
#include <stdio.h>
#include "error.h"


/*
 * Variables:	error_list, error_count, error_start
 * Purpose:	List of error messages.
 * Note:	This shouldn't be dynamic, since that introduces more
 *		possibilities for errors.
 */

#define MAX_ERRORS	2
#define MAX_ERROR_LEN	200
static struct {
	char msg[MAX_ERROR_LEN];
	struct win *win;
} error_list[MAX_ERRORS];
static int error_count = 0;
static int error_start = 0;



/*
 * Function:	error
 * Purpose:	Add new error to queue of error messages.
 * Arguments:	win	the window for which the error occurred
 *			(NULL means relevant to all windows)
 *		other arguments are like in printf.
 * Return:	Nothing.
 * Note:	The output may not be more than MAX_ERROR_LEN-1 characters.
 */
void error(struct win *win, const char *fmt, ...) {
	va_list args;
	int i;
	static FILE *devnull = NULL;
	int n;

	if (error_count == MAX_ERRORS)
		error_start = (error_start + 1) % MAX_ERRORS;
	i = (error_start + error_count) % MAX_ERRORS;
	++error_count;
	
	if (devnull == NULL) {
		devnull = fopen("/dev/null", "w");
		if (devnull == NULL) {
			sprintf(error_list[i].msg, "couldn't open /dev/null");
			return;
		}
	}

	va_start(args, fmt);
	n = vfprintf(devnull, fmt, args);
	va_end(args);
	
	if (n >= MAX_ERROR_LEN)
		sprintf(error_list[i].msg, "error message too long");
	else {
		va_start(args, fmt);
		vsprintf(error_list[i].msg, fmt, args);
		error_list[i].win = win;
		va_end(args);
	}
}



/*
 * Function:	next_error
 * Purpose:	Return next error message and remove it from list.
 * Arguments:	win	the window for which messages should be reported
 * Return:	Pointer to string, NULL if none.
 * Note:	The string will be overwritten by a future call to error.
 */
char *next_error(struct win *win) {
	char *p;

	if (error_count == 0)
		return NULL;

	p = error_list[error_start].msg;
	error_start = (error_start + 1) % MAX_ERRORS;
	--error_count;
	return p;
}