xref: /netbsd-src/usr.bin/error/input.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: input.c,v 1.7 1998/11/06 23:10:08 christos 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)input.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: input.c,v 1.7 1998/11/06 23:10:08 christos Exp $");
42 #endif /* not lint */
43 
44 #include <stdio.h>
45 #include <ctype.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include "error.h"
49 
50 int	wordc;		/* how long the current error message is */
51 char	**wordv;	/* the actual error message */
52 
53 Errorclass	catchall __P((void));
54 Errorclass	cpp __P((void));
55 Errorclass	f77 __P((void));
56 Errorclass	lint0 __P((void));
57 Errorclass	lint1 __P((void));
58 Errorclass	lint2 __P((void));
59 Errorclass	lint3 __P((void));
60 Errorclass	make __P((void));
61 Errorclass	mod2 __P((void));
62 Errorclass	onelong __P((void));
63 Errorclass	pccccom __P((void));	/* Portable C Compiler C Compiler */
64 Errorclass	pi __P((void));
65 Errorclass	ri __P((void));
66 Errorclass	richieccom __P((void));	/* Richie Compiler for 11 */
67 Errorclass	troff __P((void));
68 
69 /*
70  *	Eat all of the lines in the input file, attempting to categorize
71  *	them by their various flavors
72  */
73 void
74 eaterrors(r_errorc, r_errorv)
75 	int	*r_errorc;
76 	Eptr	**r_errorv;
77 {
78 	Errorclass	errorclass = C_SYNC;
79 	char *line;
80 	char *inbuffer;
81 	size_t inbuflen;
82 
83     for (;;){
84 	if ((inbuffer = fgetln(errorfile, &inbuflen)) == NULL)
85 		break;
86 	line = Calloc(inbuflen + 1, sizeof(char));
87 	memcpy(line, inbuffer, inbuflen);
88 	line[inbuflen] = '\0';
89 	wordvbuild(line, &wordc, &wordv);
90 	/*
91 	 *	for convience, convert wordv to be 1 based, instead
92 	 *	of 0 based.
93 	 */
94 	wordv -= 1;
95 	if ( wordc > 0 &&
96 	   ((( errorclass = onelong() ) != C_UNKNOWN)
97 	   || (( errorclass = cpp() ) != C_UNKNOWN)
98 	   || (( errorclass = pccccom() ) != C_UNKNOWN)
99 	   || (( errorclass = richieccom() ) != C_UNKNOWN)
100 	   || (( errorclass = lint0() ) != C_UNKNOWN)
101 	   || (( errorclass = lint1() ) != C_UNKNOWN)
102 	   || (( errorclass = lint2() ) != C_UNKNOWN)
103 	   || (( errorclass = lint3() ) != C_UNKNOWN)
104 	   || (( errorclass = make() ) != C_UNKNOWN)
105 	   || (( errorclass = f77() ) != C_UNKNOWN)
106 	   || ((errorclass = pi() ) != C_UNKNOWN)
107 	   || (( errorclass = ri() )!= C_UNKNOWN)
108 	   || (( errorclass = mod2() )!= C_UNKNOWN)
109 	   || (( errorclass = troff() )!= C_UNKNOWN))
110 	) ;
111 	else
112 		errorclass = catchall();
113 	if (wordc)
114 		erroradd(wordc, wordv+1, errorclass, C_UNKNOWN);
115     }
116 #ifdef FULLDEBUG
117     printf("%d errorentrys\n", nerrors);
118 #endif
119     arrayify(r_errorc, r_errorv, er_head);
120 }
121 
122 /*
123  *	create a new error entry, given a zero based array and count
124  */
125 void
126 erroradd(errorlength, errorv, errorclass, errorsubclass)
127 	int		errorlength;
128 	char		**errorv;
129 	Errorclass	errorclass;
130 	Errorclass	errorsubclass;
131 {
132 	Eptr	newerror;
133 	char	*cp;
134 
135 	if (errorclass == C_TRUE){
136 		/* check canonicalization of the second argument*/
137 		for(cp = errorv[1]; *cp && isdigit((unsigned char)*cp); cp++)
138 			continue;
139 		errorclass = (*cp == '\0') ? C_TRUE : C_NONSPEC;
140 #ifdef FULLDEBUG
141 		if (errorclass != C_TRUE)
142 			printf("The 2nd word, \"%s\" is not a number.\n",
143 				errorv[1]);
144 #endif
145 	}
146 	if (errorlength > 0){
147 		newerror = (Eptr)Calloc(1, sizeof(Edesc));
148 		newerror->error_language = language; /* language is global */
149 		newerror->error_text = errorv;
150 		newerror->error_lgtext = errorlength;
151 		if (errorclass == C_TRUE)
152 			newerror->error_line = atoi(errorv[1]);
153 		newerror->error_e_class = errorclass;
154 		newerror->error_s_class = errorsubclass;
155 		switch(newerror->error_e_class = discardit(newerror)){
156 			case C_SYNC:		nsyncerrors++; break;
157 			case C_DISCARD: 	ndiscard++; break;
158 			case C_NULLED:		nnulled++; break;
159 			case C_NONSPEC:		nnonspec++; break;
160 			case C_THISFILE: 	nthisfile++; break;
161 			case C_TRUE:		ntrue++; break;
162 			case C_UNKNOWN:		nunknown++; break;
163 			case C_IGNORE:		nignore++; break;
164 		}
165 		newerror->error_next = er_head;
166 		er_head = newerror;
167 		newerror->error_no = nerrors++;
168 	}	/* length > 0 */
169 }
170 
171 Errorclass
172 onelong()
173 {
174 	char	**nwordv;
175 	if ( (wordc == 1) && (language != INLD) ){
176 		/*
177 		 *	We have either:
178 		 *	a)	file name from cc
179 		 *	b)	Assembler telling world that it is complaining
180 		 *	c)	Noise from make ("Stop.")
181 		 *	c)	Random noise
182 		 */
183 		wordc = 0;
184 		if (strcmp(wordv[1], "Stop.") == 0){
185 			language = INMAKE; return(C_SYNC);
186 		}
187 		if (strcmp(wordv[1], "Assembler:") == 0){
188 			/* assembler always alerts us to what happened*/
189 			language = INAS; return(C_SYNC);
190 		} else
191 		if (strcmp(wordv[1], "Undefined:") == 0){
192 			/* loader complains about unknown symbols*/
193 			language = INLD; return(C_SYNC);
194 		}
195 		if (lastchar(wordv[1]) == ':'){
196 			/* cc tells us what file we are in */
197 			currentfilename = wordv[1];
198 			(void)substitute(currentfilename, ':', '\0');
199 			language = INCC; return(C_SYNC);
200 		}
201 	} else
202 	if ( (wordc == 1) && (language == INLD) ){
203 		nwordv = (char **)Calloc(4, sizeof(char *));
204 		nwordv[0] = "ld:";
205 		nwordv[1] = wordv[1];
206 		nwordv[2] = "is";
207 		nwordv[3] = "undefined.";
208 		wordc = 4;
209 		wordv = nwordv - 1;
210 		return(C_NONSPEC);
211 	} else
212 	if (wordc == 1){
213 		return(C_SYNC);
214 	}
215 	return(C_UNKNOWN);
216 }	/* end of one long */
217 
218 Errorclass
219 cpp()
220 {
221 	/*
222 	 *	Now attempt a cpp error message match
223 	 *	Examples:
224 	 *		./morse.h: 23: undefined control
225 	 *		morsesend.c: 229: MAGNIBBL: argument mismatch
226 	 *		morsesend.c: 237: MAGNIBBL: argument mismatch
227 	 *		test1.c: 6: undefined control
228 	 */
229 	if (   (language != INLD)		/* loader errors have almost same fmt*/
230 	    && (lastchar(wordv[1]) == ':')
231 	    && (isdigit((unsigned char)firstchar(wordv[2])))
232 	    && (lastchar(wordv[2]) == ':') ){
233 		language = INCPP;
234 		clob_last(wordv[1], '\0');
235 		clob_last(wordv[2], '\0');
236 		return(C_TRUE);
237 	}
238 	return(C_UNKNOWN);
239 }	/*end of cpp*/
240 
241 Errorclass
242 pccccom()
243 {
244 	/*
245 	 *	Now attempt a ccom error message match:
246 	 *	Examples:
247 	 *	  "morsesend.c", line 237: operands of & have incompatible types
248 	 *	  "test.c", line 7: warning: old-fashioned initialization: use =
249 	 *	  "subdir.d/foo2.h", line 1: illegal initialization
250 	 */
251 	if (   (firstchar(wordv[1]) == '"')
252 	    && (lastchar(wordv[1]) == ',')
253 	    && (next_lastchar(wordv[1]) == '"')
254 	    && (strcmp(wordv[2],"line") == 0)
255 	    && (isdigit((unsigned char)firstchar(wordv[3])))
256 	    && (lastchar(wordv[3]) == ':') ){
257 		clob_last(wordv[1], '\0');	/* drop last , */
258 		clob_last(wordv[1], '\0');	/* drop last " */
259 		wordv[1]++;			/* drop first " */
260 		clob_last(wordv[3], '\0');	/* drop : on line number */
261 		wordv[2] = wordv[1];	/* overwrite "line" */
262 		wordv++;		/*compensate*/
263 		wordc--;
264 		currentfilename = wordv[1];
265 		language = INCC;
266 		return(C_TRUE);
267 	}
268 	return(C_UNKNOWN);
269 }	/* end of ccom */
270 /*
271  *	Do the error message from the Richie C Compiler for the PDP11,
272  *	which has this source:
273  *
274  *	if (filename[0])
275  *		fprintf(stderr, "%s:", filename);
276  *	fprintf(stderr, "%d: ", line);
277  *
278  */
279 
280 Errorclass
281 richieccom()
282 {
283 	char	*cp;
284 	char	**nwordv;
285 	char	*file;
286 
287 	if (lastchar(wordv[1]) == ':'){
288 		cp = wordv[1] + strlen(wordv[1]) - 1;
289 		while (isdigit((unsigned char)*--cp))
290 			continue;
291 		if (*cp == ':'){
292 			clob_last(wordv[1], '\0');	/* last : */
293 			*cp = '\0';			/* first : */
294 			file = wordv[1];
295 			nwordv = wordvsplice(1, wordc, wordv+1);
296 			nwordv[0] = file;
297 			nwordv[1] = cp + 1;
298 			wordc += 1;
299 			wordv = nwordv - 1;
300 			language = INCC;
301 			currentfilename = wordv[1];
302 			return(C_TRUE);
303 		}
304 	}
305 	return(C_UNKNOWN);
306 }
307 
308 Errorclass
309 lint0()
310 {
311 	char	**nwordv;
312 	char	*line, *file;
313 	/*
314 	 *	Attempt a match for the new lint style normal compiler
315 	 *	error messages, of the form
316 	 *
317 	 *	printf("%s(%d): %s\n", filename, linenumber, message);
318 	 */
319 	if (wordc >= 2){
320 		if (   (lastchar(wordv[1]) == ':')
321 		    && (next_lastchar(wordv[1]) == ')')
322 		) {
323 			clob_last(wordv[1], '\0'); /* colon */
324 			if (persperdexplode(wordv[1], &line, &file)){
325 				nwordv = wordvsplice(1, wordc, wordv+1);
326 				nwordv[0] = file;	/* file name */
327 				nwordv[1] = line;	/* line number */
328 				wordc += 1;
329 				wordv = nwordv - 1;
330 				language = INLINT;
331 				return(C_TRUE);
332 			}
333 			wordv[1][strlen(wordv[1])] = ':';
334 		}
335 	}
336 	return (C_UNKNOWN);
337 }
338 
339 Errorclass
340 lint1()
341 {
342 	char	*line1, *line2;
343 	char	*file1, *file2;
344 	char	**nwordv1, **nwordv2;
345 
346 	/*
347 	 *	Now, attempt a match for the various errors that lint
348 	 *	can complain about.
349 	 *
350 	 *	Look first for type 1 lint errors
351 	 */
352 	if (wordc > 1 && strcmp(wordv[wordc-1], "::") == 0){
353 	 /*
354   	  * %.7s, arg. %d used inconsistently %s(%d) :: %s(%d)
355   	  * %.7s value used inconsistently %s(%d) :: %s(%d)
356   	  * %.7s multiply declared %s(%d) :: %s(%d)
357   	  * %.7s value declared inconsistently %s(%d) :: %s(%d)
358   	  * %.7s function value type must be declared before use %s(%d) :: %s(%d)
359 	  */
360 		language = INLINT;
361 		if (wordc > 2
362 		     && (persperdexplode(wordv[wordc], &line2, &file2))
363 		     && (persperdexplode(wordv[wordc-2], &line1, &file1)) ){
364 			nwordv1 = wordvsplice(2, wordc, wordv+1);
365 			nwordv2 = wordvsplice(2, wordc, wordv+1);
366 			nwordv1[0] = file1; nwordv1[1] = line1;
367 			erroradd(wordc+2, nwordv1, C_TRUE, C_DUPL); /* takes 0 based*/
368 			nwordv2[0] = file2; nwordv2[1] = line2;
369 			wordc = wordc + 2;
370 			wordv = nwordv2 - 1;	/* 1 based */
371 			return(C_TRUE);
372 		}
373 	}
374 	return(C_UNKNOWN);
375 } /* end of lint 1*/
376 
377 Errorclass
378 lint2()
379 {
380 	char	*file;
381 	char	*line;
382 	char	**nwordv;
383 	/*
384 	 *	Look for type 2 lint errors
385 	 *
386 	 *	%.7s used( %s(%d) ), but not defined
387 	 *	%.7s defined( %s(%d) ), but never used
388 	 *	%.7s declared( %s(%d) ), but never used or defined
389 	 *
390 	 *	bufp defined( "./metric.h"(10) ), but never used
391 	 */
392 	if (   (lastchar(wordv[2]) == '(' /* ')' */ )
393 	    && (strcmp(wordv[4], "),") == 0) ){
394 		language = INLINT;
395 		if (persperdexplode(wordv[3], &line, &file)){
396 			nwordv = wordvsplice(2, wordc, wordv+1);
397 			nwordv[0] = file; nwordv[1] = line;
398 			wordc = wordc + 2;
399 			wordv = nwordv - 1;	/* 1 based */
400 			return(C_TRUE);
401 		}
402 	}
403 	return(C_UNKNOWN);
404 } /* end of lint 2*/
405 
406 char	*Lint31[4] = {"returns", "value", "which", "is"};
407 char	*Lint32[6] = {"value", "is", "used,", "but", "none", "returned"};
408 
409 Errorclass
410 lint3()
411 {
412 	if (   (wordvcmp(wordv+2, 4, Lint31) == 0)
413 	    || (wordvcmp(wordv+2, 6, Lint32) == 0) ){
414 		language = INLINT;
415 		return(C_NONSPEC);
416 	}
417 	return(C_UNKNOWN);
418 }
419 
420 /*
421  *	Special word vectors for use by F77 recognition
422  */
423 char	*F77_fatal[3] = {"Compiler", "error", "line"};
424 char	*F77_error[3] = {"Error", "on", "line"};
425 char	*F77_warning[3] = {"Warning", "on", "line"};
426 char    *F77_no_ass[3] = {"Error.","No","assembly."};
427 
428 Errorclass
429 f77()
430 {
431 	char	**nwordv;
432 	/*
433 	 *	look for f77 errors:
434 	 *	Error messages from /usr/src/cmd/f77/error.c, with
435 	 *	these printf formats:
436 	 *
437 	 *		Compiler error line %d of %s: %s
438 	 *		Error on line %d of %s: %s
439 	 *		Warning on line %d of %s: %s
440 	 *		Error.  No assembly.
441 	 */
442 	if (wordc == 3 && wordvcmp(wordv+1, 3, F77_no_ass) == 0) {
443 		wordc = 0;
444 		return(C_SYNC);
445 	}
446 	if (wordc < 6)
447 		return(C_UNKNOWN);
448 	if (	(lastchar(wordv[6]) == ':')
449 	    &&(
450 	       (wordvcmp(wordv+1, 3, F77_fatal) == 0)
451 	    || (wordvcmp(wordv+1, 3, F77_error) == 0)
452 	    || (wordvcmp(wordv+1, 3, F77_warning) == 0) )
453 	){
454 		language = INF77;
455 		nwordv = wordvsplice(2, wordc, wordv+1);
456 		nwordv[0] = wordv[6];
457 		clob_last(nwordv[0],'\0');
458 		nwordv[1] = wordv[4];
459 		wordc += 2;
460 		wordv = nwordv - 1;	/* 1 based */
461 		return(C_TRUE);
462 	}
463 	return(C_UNKNOWN);
464 } /* end of f77 */
465 
466 char	*Make_Croak[3] = {"***", "Error", "code"};
467 char	*Make_NotRemade[5] = {"not", "remade", "because", "of", "errors"};
468 
469 Errorclass
470 make()
471 {
472 	if (wordvcmp(wordv+1, 3, Make_Croak) == 0){
473 		language = INMAKE;
474 		return(C_SYNC);
475 	}
476 	if  (wordvcmp(wordv+2, 5, Make_NotRemade) == 0){
477 		language = INMAKE;
478 		return(C_SYNC);
479 	}
480 	return(C_UNKNOWN);
481 }
482 
483 Errorclass
484 ri()
485 {
486 /*
487  *	Match an error message produced by ri; here is the
488  *	procedure yanked from the distributed version of ri
489  *	April 24, 1980.
490  *
491  *	serror(str, x1, x2, x3)
492  *		char str[];
493  *		char *x1, *x2, *x3;
494  *	{
495  *		extern int yylineno;
496  *
497  *		putc('"', stdout);
498  *		fputs(srcfile, stdout);
499  *		putc('"', stdout);
500  *		fprintf(stdout, " %d: ", yylineno);
501  *		fprintf(stdout, str, x1, x2, x3);
502  *		fprintf(stdout, "\n");
503  *		synerrs++;
504  *	}
505  */
506 	if (  (firstchar(wordv[1]) == '"')
507 	    &&(lastchar(wordv[1]) == '"')
508 	    &&(lastchar(wordv[2]) == ':')
509 	    &&(isdigit((unsigned char)firstchar(wordv[2]))) ){
510 		clob_last(wordv[1], '\0');	/* drop the last " */
511 		wordv[1]++;	/* skip over the first " */
512 		clob_last(wordv[2], '\0');
513 		language = INRI;
514 		return(C_TRUE);
515 	}
516 	return(C_UNKNOWN);
517 }
518 
519 Errorclass
520 catchall()
521 {
522 	/*
523 	 *	Catches random things.
524 	 */
525 	language = INUNKNOWN;
526 	return(C_NONSPEC);
527 } /* end of catch all*/
528 
529 Errorclass
530 troff()
531 {
532 	/*
533 	 *	troff source error message, from eqn, bib, tbl...
534 	 *	Just like pcc ccom, except uses `'
535 	 */
536 	if (   (firstchar(wordv[1]) == '`')
537 	    && (lastchar(wordv[1]) == ',')
538 	    && (next_lastchar(wordv[1]) == '\'')
539 	    && (strcmp(wordv[2],"line") == 0)
540 	    && (isdigit((unsigned char)firstchar(wordv[3])))
541 	    && (lastchar(wordv[3]) == ':') ){
542 		clob_last(wordv[1], '\0');	/* drop last , */
543 		clob_last(wordv[1], '\0');	/* drop last " */
544 		wordv[1]++;			/* drop first " */
545 		clob_last(wordv[3], '\0');	/* drop : on line number */
546 		wordv[2] = wordv[1];	/* overwrite "line" */
547 		wordv++;		/*compensate*/
548 		currentfilename = wordv[1];
549 		language = INTROFF;
550 		return(C_TRUE);
551 	}
552 	return(C_UNKNOWN);
553 }
554 
555 Errorclass
556 mod2()
557 {
558 	/*
559 	 *	for decwrl modula2 compiler (powell)
560 	 */
561 	if (   (  (strcmp(wordv[1], "!!!") == 0)	/* early version */
562 	        ||(strcmp(wordv[1], "File") == 0))	/* later version */
563 	    && (lastchar(wordv[2]) == ',')	/* file name */
564 	    && (strcmp(wordv[3], "line") == 0)
565 	    && (isdigit((unsigned char)firstchar(wordv[4])))	/* line number */
566 	    && (lastchar(wordv[4]) == ':')	/* line number */
567 	){
568 		clob_last(wordv[2], '\0');	/* drop last , on file name */
569 		clob_last(wordv[4], '\0');	/* drop last : on line number */
570 		wordv[3] = wordv[2];		/* file name on top of "line" */
571 		wordv += 2;
572 		wordc -= 2;
573 		currentfilename = wordv[1];
574 		language = INMOD2;
575 		return(C_TRUE);
576 	}
577 	return(C_UNKNOWN);
578 }
579