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