forked from opensourcecobol/opensourcecobol4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.c
363 lines (322 loc) · 8.84 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
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
/*
* Copyright (C) 2003-2009 Keisuke Nishida
* Copyright (C) 2007-2009 Roger While
* Copyright (C) 2021-2022 TOKYO SYSTEM HOUSE Co., Ltd.
*
* 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 3, 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 software; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "cobj.h"
#include "tree.h"
#define max_names 5
static char *errnamebuff = NULL;
static char *errmsgbuff = NULL;
static void
print_error (char *file, int line, const char *prefix, const char *fmt, va_list ap)
{
int flag_too_many_names = 0;
static struct cb_label *last_section = NULL;
static struct cb_label *last_paragraph = NULL;
int cnt = 0;
const char *pfmt, *pstr;
void *param;
void *allname[max_names];
void *p_bfree[max_names];
char msgword[COB_MINI_BUFF];
file = file ? file : cb_source_file;
line = line ? line : cb_source_line;
/* print the paragraph or section name */
if (current_section != last_section || current_paragraph != last_paragraph) {
if (current_paragraph &&
strcmp ((const char *)(current_paragraph->name), "MAIN PARAGRAPH")) {
cb_get_jisword_buff ((const char *)current_paragraph->name, msgword, sizeof (msgword));
fprintf (stderr, _("%s: In paragraph '%s':\n"), file, msgword);
} else if (current_section &&
strcmp ((const char *)(current_section->name), "MAIN SECTION")) {
cb_get_jisword_buff ((const char *)current_section->name, msgword, sizeof (msgword));
fprintf (stderr, _("%s: In section '%s':\n"), file, msgword);
}
last_section = current_section;
last_paragraph = current_paragraph;
}
/* print error */
fprintf (stderr, "%s:%d: %s", file, line, prefix);
if (!errmsgbuff) {
errmsgbuff = cobc_malloc (COB_NORMAL_BUFF);
}
/* decode arguments */
memset (p_bfree, 0, sizeof (p_bfree));
pfmt = fmt;
while ((pstr = strchr (pfmt, '%')) != NULL) {
while (isalnum (*(pstr+=1)) != 0) {
if (isalpha (*pstr) != 0) {
param = va_arg (ap, void *);
switch (*pstr) {
case 's':
allname[cnt] = p_bfree[cnt] = cb_get_jisword (param);
cnt++;
break;
case 'c':
case 'd':
default:
allname[cnt] = param;
cnt++;
break;
}
}
}
if (cnt > max_names) {
flag_too_many_names = 1;
break;
}
pfmt = pstr;
}
if (flag_too_many_names) {
fputs (_("Internal error: Too many params in message. output suppressed.\n"),
stderr);
} else {
/* error output */
switch (cnt) {
case 0:
snprintf (errmsgbuff, COB_NORMAL_BUFF, fmt);
break;
case 1:
snprintf (errmsgbuff, COB_NORMAL_BUFF, fmt, allname[0]);
break;
case 2:
snprintf (errmsgbuff, COB_NORMAL_BUFF, fmt, allname[0], allname[1]);
break;
case 3:
snprintf (errmsgbuff, COB_NORMAL_BUFF, fmt, allname[0], allname[1], allname[2]);
break;
case 4:
snprintf (errmsgbuff, COB_NORMAL_BUFF, fmt, allname[0], allname[1], allname[2], allname[3]);
break;
case 5:
default:
/* upper limit of cnt should be checked above. */
snprintf (errmsgbuff, COB_NORMAL_BUFF, fmt, allname[0], allname[1], allname[2], allname[3], allname[4]);
break;
}
fprintf (stderr, "%s\n", errmsgbuff);
}
for (cnt = 0; cnt < max_names; cnt++) {
if (p_bfree[cnt]) {
free (p_bfree[cnt]);
}
}
}
char *
check_filler_name (char *name)
{
if (!memcmp (name, "WORK$", 5)) {
name = (char *)"FILLER";
}
return name;
}
void
cb_warning (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
print_error (NULL, 0, "Warning: ", fmt, ap);
va_end (ap);
warningcount++;
}
void
cb_error (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
print_error (NULL, 0, "Error: ", fmt, ap);
va_end (ap);
errorcount++;
}
void
cb_warning_x (cb_tree x, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
print_error ((char *)(x->source_file), x->source_line, "Warning: ", fmt, ap);
va_end (ap);
warningcount++;
}
void
cb_error_x (cb_tree x, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
print_error ((char *)(x->source_file), x->source_line, "Error: ", fmt, ap);
va_end (ap);
errorcount++;
}
int
cb_verify (const enum cb_support tag, const char *feature)
{
switch (tag) {
case CB_OK:
return 1;
case CB_WARNING:
return 1;
case CB_ARCHAIC:
if (cb_warn_archaic) {
cb_warning (_("%s is archaic in %s"), feature, cb_config_name);
}
return 1;
case CB_OBSOLETE:
if (cb_warn_obsolete) {
cb_warning (_("%s is obsolete in %s"), feature, cb_config_name);
}
return 1;
case CB_SKIP:
return 0;
case CB_IGNORE:
cb_warning (_("%s ignored"), feature);
return 0;
case CB_ERROR:
return 0;
case CB_UNCONFORMABLE:
cb_error (_("%s does not conform to %s"), feature, cb_config_name);
return 0;
}
return 0;
}
void
redefinition_error (cb_tree x)
{
struct cb_word *w;
w = CB_REFERENCE (x)->word;
cb_error_x (x, _("Redefinition of '%s'"), w->name);
cb_error_x (CB_VALUE (w->items), _("'%s' previously defined here"), w->name);
}
void
redefinition_warning (cb_tree x, cb_tree y)
{
struct cb_word *w;
w = CB_REFERENCE (x)->word;
cb_warning_x (x, _("Redefinition of '%s'"), w->name);
if (y) {
cb_warning_x (y, _("'%s' previously defined here"), w->name);
} else {
cb_warning_x (CB_VALUE (w->items), _("'%s' previously defined here"), w->name);
}
}
#define jisword_cat(w, buf, len) { \
size_t n = strnlen ((buf), (len)); \
cb_get_jisword_buff ((w), &((buf)[n]), (len) + 1 - n); \
}
#define jisword_quote(w, buf, len) { \
strncpy ((buf), "'", (len)); \
cb_get_jisword_buff ((w), &((buf)[1]), (len)); \
strncat ((buf), "'", (len)); \
}
void
undefined_error (cb_tree x)
{
struct cb_reference *r;
cb_tree c;
if (!errnamebuff) {
errnamebuff = cobc_malloc (COB_NORMAL_BUFF);
}
r = CB_REFERENCE (x);
jisword_quote (CB_NAME (x), errnamebuff, COB_NORMAL_MAX);
for (c = r->chain; c; c = CB_REFERENCE (c)->chain) {
strncat (errnamebuff, " in '", COB_NORMAL_MAX);
jisword_cat (CB_NAME (c), errnamebuff, COB_NORMAL_MAX);
strncat (errnamebuff, "'", COB_NORMAL_MAX);
}
cb_error_x (x, _("%s undefined"), errnamebuff);
}
void
ambiguous_error (cb_tree x)
{
struct cb_word *w;
struct cb_field *p;
struct cb_label *l2;
cb_tree l;
cb_tree y;
w = CB_REFERENCE (x)->word;
if (w->error == 0) {
if (!errnamebuff) {
errnamebuff = cobc_malloc (COB_NORMAL_BUFF);
}
/* display error on the first time */
jisword_quote (CB_NAME (x), errnamebuff, COB_NORMAL_MAX);
for (l = CB_REFERENCE (x)->chain; l; l = CB_REFERENCE (l)->chain) {
strncat (errnamebuff, " in '", COB_NORMAL_MAX);
jisword_cat (CB_NAME (l), errnamebuff, COB_NORMAL_MAX);
strncat (errnamebuff, "'", COB_NORMAL_MAX);
}
cb_error_x (x, _("%s ambiguous; need qualification"), errnamebuff);
w->error = 1;
/* display all fields with the same name */
for (l = w->items; l; l = CB_CHAIN (l)) {
y = CB_VALUE (l);
jisword_quote (w->name, errnamebuff, COB_NORMAL_MAX);
switch (CB_TREE_TAG (y)) {
case CB_TAG_FIELD:
for (p = CB_FIELD (y)->parent; p; p = p->parent) {
strncat (errnamebuff, " in '", COB_NORMAL_MAX);
jisword_cat (check_filler_name((char *)p->name),
errnamebuff, COB_NORMAL_MAX);
strncat (errnamebuff, "'", COB_NORMAL_MAX);
}
break;
case CB_TAG_LABEL:
l2 = CB_LABEL (y);
if (l2->section) {
strncat (errnamebuff, " in '", COB_NORMAL_MAX);
jisword_cat ((const char *)(l2->section->name),
errnamebuff, COB_NORMAL_MAX);
strncat (errnamebuff, "'", COB_NORMAL_MAX);
}
break;
default:
break;
}
strcat (errnamebuff, _(" defined here"));
cb_error_x (y, errnamebuff);
}
}
}
void
group_error (cb_tree x, const char *clause)
{
cb_error_x (x, _("Group item '%s' cannot have %s clause"), check_filler_name (cb_name (x)), clause);
}
void
level_redundant_error (cb_tree x, const char *clause)
{
cb_error_x (x, _("Level %02d item '%s' cannot have %s clause"),
cb_field (x)->level, check_filler_name (cb_name (x)), clause);
}
void
level_require_error (cb_tree x, const char *clause)
{
cb_error_x (x, _("Level %02d item '%s' requires %s clause"),
cb_field (x)->level, check_filler_name (cb_name (x)), clause);
}
void
level_except_error (cb_tree x, const char *clause)
{
cb_error_x (x, _("Level %02d item '%s' cannot have other than %s clause"),
cb_field (x)->level, check_filler_name (cb_name (x)), clause);
}