-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.c
177 lines (164 loc) · 3.42 KB
/
error.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
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
/*
* errors, messages, and recovery
*/
#include "ccc.h"
#define DEF_ERRMSG
#include "error.h"
int error;
/*
* print a message for an error code and emit an error location
* continue
*/
void
err(error_t errcode)
{
int i;
i = errcode;
if (i > ER_WTF) i = ER_WTF;
printf("file: %s line: %d error code %d %s\n",
filename, lineno, errcode, errmsg[i]);
error = errcode;
}
/*
* some errors are too nasty to fix
*/
void
fatal(error_t errcode)
{
err(errcode);
printf("too severe to recover\n");
exit(-errcode);
}
/*
* throw an error message and discard tokens until we see the token we specify
*/
void
recover(error_t errcode, token_t skipto)
{
err(errcode);
while ((cur.type != skipto) && (cur.type != E_O_F)) {
gettoken();
}
}
/*
* the next token must be 'check'. if it isn't, gripe about it and skip
* until we find 'skipto'
*/
void
need(token_t check, token_t skipto, error_t errcode)
{
if (cur.type == check) {
gettoken();
return;
}
recover(errcode, skipto);
}
#ifdef notdef
/*
* a variant sprintf with support for formats:
* %[<precision>]<format>
* where precision is [-][0][0-9]*
* if -, right pad
* if leading 0, zero pad
* formats:
* b - binary
* d - decimal
* x - hex
* s - string
*/
void
sprintf(char *d, char *fmt) {
char **ap;
char c;
char zpad;
char width;
char base;
char i;
char rpad;
int v;
char b[8];
char *s;
ap = &fmt;
ap++;
while ((c = *fmt++)) {
if (c != '%') {
*d++ = c;
continue;
}
c = *fmt++;
zpad = 0;
width = 0;
base = 0;
rpad = 0;
if (c == '-') {
rpad++;
c = *fmt++;
}
if (c == '0') {
zpad++;
c = *fmt++;
}
while (c >= '0' && c <= '9') {
width = width * 10 + c - '0';
c = *fmt++;
}
for (i = 0; i < sizeof(b); i++) {
b[i] = zpad ? '0' : ' ';
}
if (c == 'd') base = 10;
else if (c == 'x') base = 16;
else if (c == 'b') base = 2;
if ((c == 'd') || (c == 'x') || (c == 'b')) {
v = *((int *)ap);
ap++;
i--;
while (v) {
c = v % base;
v /= base;
if ((base == 16) && (c > 9)) {
c += 'a' - 10;
} else {
c += '0';
}
b[i--] = c;
}
if (rpad) {
i = sizeof(b) - (i + 1);
} else {
i = sizeof(b) - width;
}
while (width--) {
if (i > sizeof(b)) {
*d++ = ' ';
} else {
*d++ = b[i++];
}
}
continue;
}
if (c == 's') {
s = *ap;
ap++;
i = strlen(s);
if (!rpad) {
i = width - i;
while (i--) {
*d++ = ' ';
width--;
}
}
while (width--) {
if (*s) {
*d++ = *s++;
} else {
*d++ = ' ';
}
}
continue;
}
}
}
#endif
/*
* vim: tabstop=4 shiftwidth=4 expandtab:
*/