xref: /netbsd-src/usr.bin/error/error.h (revision 08ac0fb3d495947ceae8c20d71d3c2f5ed65573e)
1 /*	$NetBSD: error.h,v 1.21 2023/08/26 15:18:27 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)error.h	8.1 (Berkeley) 6/6/93
32  */
33 
34 #include <stdbool.h>
35 
36 /*
37  * Descriptors for the various languages we know about.
38  * If you touch these, also touch lang_table
39  */
40 #define INUNKNOWN	0
41 #define INCPP		1
42 #define INCC		2
43 #define INAS		3
44 #define INLD		4
45 #define INLINT		5
46 #define INF77		6
47 #define INPI		7
48 #define INPC		8
49 #define INFRANZ		9
50 #define INLISP		10
51 #define INVAXIMA	11
52 #define INRATFOR	12
53 #define INLEX		13
54 #define INYACC		14
55 #define INAPL		15
56 #define INMAKE		16
57 #define INRI		17
58 #define INTROFF		18
59 #define INMOD2		19
60 
61 /*
62  * We analyze each line in the error message file, and
63  * attempt to categorize it by type, as well as language.
64  * Here are the type descriptors.
65  */
66 typedef int Errorclass;
67 
68 #define C_FIRST		  0	/* first error category */
69 #define C_UNKNOWN	0	/* must be zero */
70 #define C_IGNORE	1	/* ignore the message; used for pi */
71 #define C_SYNC		2	/* synchronization errors */
72 #define C_DISCARD	3	/* touches dangerous files, so discard */
73 #define C_NONSPEC	4	/* not specific to any file */
74 #define C_THISFILE	5	/* specific to this file, but at no line */
75 #define C_NULLED	6	/* refers to special func; so null */
76 #define C_TRUE		7	/* fits into true error format */
77 #define C_DUPL		8	/* sub class only; duplicated error message */
78 #define C_LAST		  9	/* last error category */
79 
80 #define SORTABLE(x)	(!(NOTSORTABLE(x)))
81 #define NOTSORTABLE(x)	(x <= C_NONSPEC)
82 
83 /*
84  * Resources to count and print out the error categories
85  */
86 extern const char *class_table[];
87 extern int class_count[];
88 
89 extern size_t filelevel;
90 
91 #define nunknown	class_count[C_UNKNOWN]
92 #define nignore		class_count[C_IGNORE]
93 #define nsyncerrors	class_count[C_SYNC]
94 #define ndiscard	class_count[C_DISCARD]
95 #define nnonspec	class_count[C_NONSPEC]
96 #define nthisfile	class_count[C_THISFILE]
97 #define nnulled		class_count[C_NULLED]
98 #define ntrue		class_count[C_TRUE]
99 #define ndupl		class_count[C_DUPL]
100 
101 /* places to put the error complaints */
102 
103 #define TOTHEFILE	1	/* touch the file */
104 #define TOSTDOUT	2	/* just print them out (ho-hum) */
105 
106 extern FILE *errorfile;	/* where error file comes from */
107 extern FILE *queryfile;	/* where the query responses from the user come from*/
108 
109 extern char *scriptname;
110 
111 extern const char *suffixlist;
112 
113 extern bool query;
114 extern bool terse;
115 int inquire(const char *, ...) __printflike(1, 2);	/* inquire for yes/no */
116 
117 /*
118  * codes for inquire() to return
119  */
120 #define Q_error	-1			/* an error occurred */
121 #define Q_NO	1			/* 'N' */
122 #define Q_no	2			/* 'n' */
123 #define Q_YES	3			/* 'Y' */
124 #define Q_yes	4			/* 'y' */
125 
126 /*
127  * Describes attributes about a language
128  */
129 struct lang_desc {
130 	const char *lang_name;
131 	const char *lang_incomment;	/* one of the following defines */
132 	const char *lang_outcomment;	/* one of the following defines */
133 };
134 extern struct lang_desc lang_table[];
135 
136 #define CINCOMMENT	"/*###"
137 #define COUTCOMMENT	"%%%*/\n"
138 #define FINCOMMENT	"C###"
139 #define FOUTCOMMENT	"%%%\n"
140 #define NEWLINE		"%%%\n"
141 #define PIINCOMMENT	"(*###"
142 #define PIOUTCOMMENT	"%%%*)\n"
143 #define LISPINCOMMENT	";###"
144 #define ASINCOMMENT	"####"
145 #define RIINCOMMENT	CINCOMMENT
146 #define RIOUTCOMMENT	COUTCOMMENT
147 #define TROFFINCOMMENT	".\\\"###"
148 #define TROFFOUTCOMMENT	NEWLINE
149 #define MOD2INCOMMENT	"(*###"
150 #define MOD2OUTCOMMENT	"%%%*)\n"
151 
152 /*
153  * Defines and resources for determing if a given line
154  * is to be discarded because it refers to a file not to
155  * be touched, or if the function reference is to a
156  * function the user doesn't want recorded.
157  */
158 
159 #define ERRORNAME	"/.errorrc"
160 extern int nignored;
161 extern char **names_ignored;
162 
163 /*
164  * Structure definition for a full error
165  */
166 typedef struct edesc Edesc;
167 typedef Edesc *Eptr;
168 
169 struct edesc {
170 	Eptr error_next;		/* linked together */
171 	int error_lgtext;		/* how many on the right hand side */
172 	char **error_text;		/* the right hand side proper */
173 	Errorclass error_e_class;	/* error category of this error */
174 	Errorclass error_s_class;	/* sub descriptor of error_e_class */
175 	int error_language;		/* the language for this error */
176 	int error_position;		/* oridinal position */
177 	int error_line;			/* discovered line number */
178 	int error_no;			/* sequence number on input */
179 };
180 
181 /*
182  * Resources for the true errors
183  */
184 extern int nerrors;
185 extern Eptr er_head;
186 
187 extern int cur_wordc;
188 extern char **cur_wordv;
189 
190 
191 /*
192  * Resources for each of the files mentioned
193  */
194 extern int nfiles;
195 extern Eptr **files;			/* array of pointers into errors */
196 extern bool *touchedfiles;		/* which files we touched */
197 
198 /*
199  * The language the compilation is in, as intuited from
200  * the flavor of error messages analyzed.
201  */
202 extern int language;
203 extern char *currentfilename;
204 extern char default_currentfilename[];
205 
206 /*
207  * Macros for initializing arrays of string constants.
208  * This is a fairly gross set of preprocessor hacks; the idea is
209  * that instead of
210  *     static char *foo[4] = { "alpha", "beta", "gamma", "delta" };
211  * you do
212  *     DECL_STRINGS_4(static, foo, "alpha", "beta", "gamma", "delta");
213  *
214  * and it comes out as
215  *     static char foo_0[] = "delta";
216  *     static char foo_1[] = "gamma";
217  *     static char foo_2[] = "beta";
218  *     static char foo_3[] = "alpha";
219  *     static char *foo[4] = { foo_3, foo_2, foo_1, foo_0 };
220  *
221  * none of which is const.
222  *
223  * Unfortunately, the string arrays this program slings around freely
224  * can't be all const (because it munges some of them) and can't be
225  * all non-const without something like this to initialize the ones
226  * that need to contain string constants. Unfortunately you can't
227  * mix (const char *const *) and (char **) in C, only in C++.
228  */
229 
230 #define DECL_STR(sym, num, str) static char sym##_##num[] = str
231 
232 #define DECL_S1(sym, s, ...) __VA_ARGS__ DECL_STR(sym, 0, s)
233 #define DECL_S2(sym, s, ...) DECL_STR(sym, 1, s); DECL_S1(sym, __VA_ARGS__)
234 #define DECL_S3(sym, s, ...) DECL_STR(sym, 2, s); DECL_S2(sym, __VA_ARGS__)
235 #define DECL_S4(sym, s, ...) DECL_STR(sym, 3, s); DECL_S3(sym, __VA_ARGS__)
236 #define DECL_S5(sym, s, ...) DECL_STR(sym, 4, s); DECL_S4(sym, __VA_ARGS__)
237 #define DECL_S6(sym, s, ...) DECL_STR(sym, 5, s); DECL_S5(sym, __VA_ARGS__)
238 
239 #define USE_S1(sym) sym##_0
240 #define USE_S2(sym) sym##_1, USE_S1(sym)
241 #define USE_S3(sym) sym##_2, USE_S2(sym)
242 #define USE_S4(sym) sym##_3, USE_S3(sym)
243 #define USE_S5(sym) sym##_4, USE_S4(sym)
244 #define USE_S6(sym) sym##_5, USE_S5(sym)
245 
246 #define DECL_STRS(num, class, sym, ...) \
247 	DECL_S##num(sym, __VA_ARGS__); \
248 	class char *sym[num] = { USE_S##num(sym) }
249 
250 #define DECL_STRINGS_1(class, sym, ...) DECL_STRS(1, class, sym, __VA_ARGS__)
251 #define DECL_STRINGS_2(class, sym, ...) DECL_STRS(2, class, sym, __VA_ARGS__)
252 #define DECL_STRINGS_3(class, sym, ...) DECL_STRS(3, class, sym, __VA_ARGS__)
253 #define DECL_STRINGS_4(class, sym, ...) DECL_STRS(4, class, sym, __VA_ARGS__)
254 #define DECL_STRINGS_5(class, sym, ...) DECL_STRS(5, class, sym, __VA_ARGS__)
255 #define DECL_STRINGS_6(class, sym, ...) DECL_STRS(6, class, sym, __VA_ARGS__)
256 
257 
258 /*
259  *	Functional forwards
260  */
261 void arrayify(int *, Eptr **, Eptr);
262 void *Calloc(size_t, size_t);
263 void clob_last(char *,  char);
264 Errorclass discardit(Eptr);
265 void eaterrors(int *, Eptr **);
266 void erroradd(int, char **, Errorclass, Errorclass);
267 void filenames(int, Eptr **);
268 void findfiles(int, Eptr *, int *, Eptr ***);
269 char firstchar(const char *);
270 void getignored(const char *);
271 char lastchar(const char *);
272 char next_lastchar(const char *);
273 void onintr(int);
274 bool persperdexplode(char *, char **, char **);
275 Errorclass pi(void);
276 int position(const char *, char);
277 void printerrors(bool, int, Eptr []);
278 const char *plural(int);
279 char *Strdup(const char *);
280 char *substitute(char *, char, char);
281 bool touchfiles(int, Eptr **, int *, char ***);
282 const char *verbform(int);
283 void wordvbuild(char *, int*, char ***);
284 bool wordv_eq(char **, int, char **);
285 void wordvprint(FILE *, int, char **);
286 char **wordvsplice(int, int, char **);
287