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