-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherror-msg.c
125 lines (87 loc) · 2.55 KB
/
error-msg.c
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
/* Error message printing. */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "error-msg.h"
/* The name of the program running and of the source file the program
is reading from. The default values are just for debugging. */
static const char * program_name = "PROGRAM NAME UNDEFINED";
static const char * source_name = "SOURCE NAME UNDEFINED";
int * source_line = NULL;
#define DO_PRINT(file) \
va_start (a, format); \
vfprintf (file, format, a); \
fprintf (file, ".\n"); \
va_end (a)
/* Print a warning message with a line number. */
void PRINT_FORMAT (2, 3)
lwarning (unsigned line_number, const char * format, ...)
{
va_list a;
fprintf (stderr, "%s:%u: warning: ", source_name, line_number);
DO_PRINT (stderr);
}
/* Print an error message for a specified line and then exit */
NO_RETURN PRINT_FORMAT (1, 2)
line_error (const char * format, ... )
{
va_list a;
fprintf (stderr, "%s:%u: error: ", source_name, * source_line);
DO_PRINT (stderr);
// exit ok
exit (EXIT_FAILURE);
}
/* Print a warning message with a line number. */
void PRINT_FORMAT (1, 2)
line_warning (const char * format, ... )
{
va_list a;
fprintf (stderr, "%s:%u: warning: ", source_name, * source_line);
DO_PRINT (stderr);
}
/* Print a warning message with a line number. */
void PRINT_FORMAT (1, 2)
line_info (const char * format, ... )
{
va_list a;
fprintf (stdout, "%s:%u: ", source_name, * source_line);
DO_PRINT (stdout);
}
/* Print the specified error message and exit. */
NO_RETURN PRINT_FORMAT (1, 2)
error (const char * format, ... )
{
va_list a;
fprintf ( stderr, "%s: error: ", program_name );
DO_PRINT (stderr);
// exit ok
exit (EXIT_FAILURE);
}
/* Print a warning message. */
void PRINT_FORMAT (1, 2)
warning (const char * format, ... )
{
va_list a;
fprintf ( stderr, "%s: warning: ", program_name );
DO_PRINT (stderr);
}
/* Print an internal error message. This is for the case that
something that should never happen occurs, such as a `default:' in
a switch statement that is used to catch errors. */
NO_RETURN PRINT_FORMAT (3, 4)
bug (const char * file, unsigned line, const char * format, ... )
{
va_list a;
fprintf ( stderr, "%s:%s:%u: internal error: ",
program_name, file, line );
fprintf ( stderr, "%s:%s:%u: internal error: ",
program_name, file, line );
DO_PRINT (stderr);
// exit ok
exit (EXIT_FAILURE);
}
void
set_program_name (const char * name)
{
program_name = name;
}