xref: /csrg-svn/usr.bin/error/input.c (revision 2798)
1*2798Swnj static	char *sccsid = "@(#)errorinput.c	1.2 (Berkeley) 1/14/81";
21456Sroot #include <stdio.h>
31456Sroot #include <ctype.h>
41456Sroot #include "error.h"
51456Sroot 
61456Sroot int	wordc;		/* how long the current error message is */
71456Sroot char	**wordv;	/* the actual error message */
81456Sroot 
91456Sroot int	nerrors;
101456Sroot int	language;
111456Sroot 
121456Sroot Errorclass	onelong();
131456Sroot Errorclass	cpp();
14*2798Swnj Errorclass	pccccom();	/* Portable C Compiler C Compiler */
15*2798Swnj Errorclass	richieccom();	/* Richie Compiler for 11 */
161456Sroot Errorclass	lint0();
171456Sroot Errorclass	lint1();
181456Sroot Errorclass	lint2();
191456Sroot Errorclass	lint3();
201456Sroot Errorclass	make();
211456Sroot Errorclass	f77();
221456Sroot Errorclass	pi();
231456Sroot Errorclass	ri();
241456Sroot /*
251456Sroot  *	Eat all of the lines in the input file, attempting to categorize
261456Sroot  *	them by their various flavors
271456Sroot  */
281456Sroot static	char	inbuffer[BUFSIZ];
291456Sroot 
301456Sroot eaterrors(r_errorc, r_errorv)
311456Sroot 	int	*r_errorc;
321456Sroot 	struct	error_desc	***r_errorv;
331456Sroot {
341456Sroot 	extern	boolean	piflag;
351456Sroot 	Errorclass	errorclass = C_SYNC;
361456Sroot 
371456Sroot     for (;;){
381456Sroot 	if (fgets(inbuffer, BUFSIZ, errorfile) == NULL)
391456Sroot 		break;
401456Sroot 	wordvbuild(inbuffer, &wordc, &wordv);
411456Sroot 	/*
421456Sroot 	 *	for convience, convert wordv to be 1 based, instead
431456Sroot 	 *	of 0 based.
441456Sroot 	 */
451456Sroot 	wordv -= 1;
461456Sroot 	if ( 0
471456Sroot #ifndef ERNIE
481456Sroot 	   || (piflag && ( (errorclass = pi() ) != C_UNKNOWN))
491456Sroot #endif
501456Sroot 	   || (( errorclass = onelong() ) != C_UNKNOWN)
511456Sroot 	   || (( errorclass = cpp() ) != C_UNKNOWN)
52*2798Swnj 	   || (( errorclass = pccccom() ) != C_UNKNOWN)
53*2798Swnj 	   || (( errorclass = richieccom() ) != C_UNKNOWN)
541456Sroot 	   || (( errorclass = lint0() ) != C_UNKNOWN)
551456Sroot 	   || (( errorclass = lint1() ) != C_UNKNOWN)
561456Sroot 	   || (( errorclass = lint2() ) != C_UNKNOWN)
571456Sroot 	   || (( errorclass = lint3() ) != C_UNKNOWN)
581456Sroot 	   || (( errorclass = make() ) != C_UNKNOWN)
591456Sroot 	   || (( errorclass = f77() ) != C_UNKNOWN)
601456Sroot #ifdef ERNIE
611456Sroot 	   || ((errorclass = pi() ) != C_UNKNOWN)
621456Sroot 	   || (( errorclass = ri() )!= C_UNKNOWN)
631456Sroot #endif
641456Sroot 	) ;
651456Sroot 	else
661456Sroot 		errorclass = catchall();
671456Sroot 	if (wordc)
681456Sroot 		erroradd(wordc, wordv+1, errorclass, C_UNKNOWN);
691456Sroot     }
701456Sroot #ifdef FULLDEBUG
711456Sroot     printf("%d errorentrys\n", nerrors);
721456Sroot #endif
731456Sroot     arrayify(r_errorc, r_errorv, er_head);
741456Sroot }
751456Sroot 
761456Sroot /*
771456Sroot  *	create a new error entry, given a zero based array and count
781456Sroot  */
791456Sroot erroradd(errorlength, errorv, errorclass, errorsubclass)
801456Sroot 	int		errorlength;
811456Sroot 	char		**errorv;
821456Sroot 	Errorclass	errorclass;
831456Sroot 	Errorclass	errorsubclass;
841456Sroot {
851456Sroot 	register	struct	error_desc	*newerror;
861456Sroot 	register	char	*cp;
871456Sroot 
881456Sroot 	if (errorclass == C_TRUE){
891456Sroot 		/* check canonicalization of the second argument*/
901456Sroot 		for(cp = errorv[1]; *cp && isdigit(*cp); cp++)
911456Sroot 			continue;
921456Sroot 		errorclass = (*cp == '\0') ? C_TRUE : C_NONSPEC;
931456Sroot #ifdef FULLDEBUG
941456Sroot 		if (errorclass != C_TRUE)
951456Sroot 			printf("The 2nd word, \"%s\" is not a number.\n",
961456Sroot 				errorv[1]);
971456Sroot #endif
981456Sroot 	}
991456Sroot 	if (errorlength > 0){
1001456Sroot 		newerror = (struct error_desc *)Calloc(1, sizeof(struct error_desc));
1011456Sroot 		newerror->error_language = language; /* language is global */
1021456Sroot 		newerror->error_text = errorv;
1031456Sroot 		newerror->error_lgtext = errorlength;
1041456Sroot 		if (errorclass == C_TRUE)
1051456Sroot 			newerror->error_line = atoi(errorv[1]);
1061456Sroot 		newerror->error_e_class = errorclass;
1071456Sroot 		newerror->error_s_class = errorsubclass;
1081456Sroot 		switch(newerror->error_e_class = discardit(newerror)){
1091456Sroot 			case C_SYNC:		nsyncerrors++; break;
1101456Sroot 			case C_DISCARD: 	ndiscard++; break;
1111456Sroot 			case C_NULLED:		nnulled++; break;
1121456Sroot 			case C_NONSPEC:		nnonspec++; break;
1131456Sroot 			case C_THISFILE: 	nthisfile++; break;
1141456Sroot 			case C_TRUE:		ntrue++; break;
1151456Sroot 			case C_UNKNOWN:		nunknown++; break;
1161456Sroot 			case C_IGNORE:		nignore++; break;
1171456Sroot 		}
1181456Sroot 		newerror->error_next = er_head;
1191456Sroot 		er_head = newerror;
1201456Sroot 		newerror->error_no = nerrors++;
1211456Sroot 	}	/* length > 0 */
1221456Sroot }
1231456Sroot 
1241456Sroot Errorclass onelong()
1251456Sroot {
1261456Sroot 	char	**nwordv;
1271456Sroot 	if ( (wordc == 1) && (language != INLD) ){
1281456Sroot 		/*
1291456Sroot 		 *	We have either:
1301456Sroot 		 *	a)	file name from cc
1311456Sroot 		 *	b)	Assembler telling world that it is complaining
1321456Sroot 		 *	c)	Noise from make ("Stop.")
1331456Sroot 		 *	c)	Random noise
1341456Sroot 		 */
1351456Sroot 		wordc = 0;
1361456Sroot 		if (strcmp(wordv[2], "Stop.") == 0){
1371456Sroot 			language = INMAKE; return(C_SYNC);
1381456Sroot 		}
1391456Sroot 		if (strcmp(wordv[1], "Assembler:") == 0){
1401456Sroot 			/* assembler always alerts us to what happened*/
1411456Sroot 			language = INAS; return(C_SYNC);
1421456Sroot 		} else
1431456Sroot 		if (strcmp(wordv[1], "Undefined:") == 0){
1441456Sroot 			/* loader complains about unknown symbols*/
1451456Sroot 			language = INLD; return(C_SYNC);
1461456Sroot 		}
1471456Sroot 		if (lastchar(wordv[1]) == ':'){
1481456Sroot 			/* cc tells us what file we are in */
1491456Sroot 			currentfilename = wordv[1];
1501456Sroot 			substitute(currentfilename, ':', '\0');
1511456Sroot 			language = INCC; return(C_SYNC);
1521456Sroot 		}
1531456Sroot 	} else
1541456Sroot 	if ( (wordc == 1) && (language == INLD) ){
1551456Sroot 		nwordv = (char **)Calloc(4, sizeof(char *));
1561456Sroot 		nwordv[0] = "ld:";
1571456Sroot 		nwordv[1] = wordv[1];
1581456Sroot 		nwordv[2] = "is";
1591456Sroot 		nwordv[3] = "undefined.";
1601456Sroot 		wordc = 4;
1611456Sroot 		wordv = nwordv - 1;
1621456Sroot 		return(C_NONSPEC);
1631456Sroot 	} else
1641456Sroot 	if (wordc == 1){
1651456Sroot 		return(C_SYNC);
1661456Sroot 	}
1671456Sroot 	return(C_UNKNOWN);
1681456Sroot }	/* end of one long */
1691456Sroot 
1701456Sroot Errorclass	cpp()
1711456Sroot {
1721456Sroot 	/*
1731456Sroot 	 *	Now attempt a cpp error message match
1741456Sroot 	 *	Examples:
1751456Sroot 	 *		./morse.h: 23: undefined control
1761456Sroot 	 *		morsesend.c: 229: MAGNIBBL: argument mismatch
1771456Sroot 	 *		morsesend.c: 237: MAGNIBBL: argument mismatch
1781456Sroot 	 *		test1.c: 6: undefined control
1791456Sroot 	 */
1801456Sroot 	if (   (language != INLD)		/* loader errors have almost same fmt*/
1811456Sroot 	    && (lastchar(wordv[1]) == ':')
1821456Sroot 	    && (isdigit(firstchar(wordv[2])))
1831456Sroot 	    && (lastchar(wordv[2]) == ':') ){
1841456Sroot 		language = INCPP;
1851456Sroot 		clob_last(wordv[1], '\0');
1861456Sroot 		clob_last(wordv[2], '\0');
1871456Sroot 		return(C_TRUE);
1881456Sroot 	}
1891456Sroot 	return(C_UNKNOWN);
1901456Sroot }	/*end of cpp*/
1911456Sroot 
192*2798Swnj Errorclass pccccom()
1931456Sroot {
1941456Sroot 	/*
1951456Sroot 	 *	Now attempt a ccom error message match:
1961456Sroot 	 *	Examples:
1971456Sroot 	 *	  "morsesend.c", line 237: operands of & have incompatible types
1981456Sroot 	 *	  "test.c", line 7: warning: old-fashioned initialization: use =
1991456Sroot 	 *	  "subdir.d/foo2.h", line 1: illegal initialization
2001456Sroot 	 */
2011456Sroot 	if (   (firstchar(wordv[1]) == '"')
2021456Sroot 	    && (lastchar(wordv[1]) == ',')
2031456Sroot 	    && (next_lastchar(wordv[1]) == '"')
2041456Sroot 	    && (strcmp(wordv[2],"line") == 0)
2051456Sroot 	    && (isdigit(firstchar(wordv[3])))
2061456Sroot 	    && (lastchar(wordv[3]) == ':') ){
2071456Sroot 		clob_last(wordv[1], '\0');	/* drop last , */
2081456Sroot 		clob_last(wordv[1], '\0');	/* drop last " */
2091456Sroot 		wordv[1]++;			/* drop first " */
2101456Sroot 		clob_last(wordv[3], '\0');	/* drop : on line number */
2111456Sroot 		wordv[2] = wordv[1];	/* overwrite "line" */
2121456Sroot 		wordv++;		/*compensate*/
2131456Sroot 		if (language == INAS){
2141456Sroot 			if (strcmp(currentfilename, "???") != 0)
2151456Sroot 				wordv[1] = currentfilename;
2161456Sroot 			return(C_NULLED);
2171456Sroot 		} else {
2181456Sroot 			currentfilename = wordv[1];
2191456Sroot 			language = INCC;
2201456Sroot 			return(C_TRUE);
2211456Sroot 		}
2221456Sroot 	}
2231456Sroot 	return(C_UNKNOWN);
2241456Sroot }	/* end of ccom */
225*2798Swnj /*
226*2798Swnj  *	Do the error message from the Richie C Compiler for the PDP11,
227*2798Swnj  *	which has this source:
228*2798Swnj  *
229*2798Swnj  *	if (filename[0])
230*2798Swnj  *		fprintf(stderr, "%s:", filename);
231*2798Swnj  *	fprintf(stderr, "%d: ", line);
232*2798Swnj  *
233*2798Swnj  */
234*2798Swnj Errorclass richieccom()
235*2798Swnj {
236*2798Swnj 	register	char	*cp;
237*2798Swnj 	register	char	**nwordv;
238*2798Swnj 			char	*file;
239*2798Swnj 	if (lastchar(wordv[1]) == ':'){
240*2798Swnj 		cp = wordv[1] + strlen(wordv[1]) - 1;
241*2798Swnj 		while (isdigit(*--cp))
242*2798Swnj 			continue;
243*2798Swnj 		if (*cp == ':'){
244*2798Swnj 			clob_last(wordv[1], '\0');	/* last : */
245*2798Swnj 			*cp = '\0';			/* first : */
246*2798Swnj 			file = wordv[1];
247*2798Swnj 			nwordv = wordvsplice(1, wordc, wordv+1);
248*2798Swnj 			nwordv[0] = file;
249*2798Swnj 			nwordv[1] = cp + 1;
250*2798Swnj 			wordc += 1;
251*2798Swnj 			wordv = nwordv - 1;
252*2798Swnj 			language = INCC;
253*2798Swnj 			currentfilename = wordv[1];
254*2798Swnj 			return(C_TRUE);
255*2798Swnj 		}
256*2798Swnj 	}
257*2798Swnj 	return(C_UNKNOWN);
258*2798Swnj }
2591456Sroot 
2601456Sroot Errorclass lint0()
2611456Sroot {
2621456Sroot 	register	char	*cp;
2631456Sroot 	register	char	**nwordv;
2641456Sroot 			char	*line, *file;
2651456Sroot 	/*
2661456Sroot 	 *	Attempt a match for the new lint style normal compiler
2671456Sroot 	 *	error messages, of the form
2681456Sroot 	 *
2691456Sroot 	 *	printf("%s(%d): %s\n", filename, linenumber, message);
2701456Sroot 	 */
2711456Sroot 	if (wordc >= 2){
2721456Sroot 		if (   (lastchar(wordv[1]) == ':')
2731456Sroot 		    && (next_lastchar(wordv[1]) == ')')
2741456Sroot 		) {
2751456Sroot 			clob_last(wordv[1], '\0'); /* colon */
2761456Sroot 			if (persperdexplode(wordv[1], &line, &file)){
2771456Sroot 				nwordv = wordvsplice(1, wordc, wordv+1);
2781456Sroot 				nwordv[0] = file;	/* file name */
2791456Sroot 				nwordv[1] = line;	/* line number */
2801456Sroot 				wordc += 1;
2811456Sroot 				wordv = nwordv - 1;
2821456Sroot 				language = INLINT;
2831456Sroot 				return(C_TRUE);
2841456Sroot 			}
2851456Sroot 			wordv[1][strlen(wordv[1])] = ':';
2861456Sroot 		}
2871456Sroot 	}
2881456Sroot 	return (C_UNKNOWN);
2891456Sroot }
2901456Sroot 
2911456Sroot Errorclass lint1()
2921456Sroot {
2931456Sroot 	char	*line1, *line2;
2941456Sroot 	char	*file1, *file2;
2951456Sroot 	char	**nwordv1, **nwordv2;
2961456Sroot 
2971456Sroot 	/*
2981456Sroot 	 *	Now, attempt a match for the various errors that lint
2991456Sroot 	 *	can complain about.
3001456Sroot 	 *
3011456Sroot 	 *	Look first for type 1 lint errors
3021456Sroot 	 */
3031456Sroot 	if (strcmp(wordv[wordc-1], "::") == 0){
3041456Sroot 	 /*
3051456Sroot   	  * %.7s, arg. %d used inconsistently %s(%d) :: %s(%d)
3061456Sroot   	  * %.7s value used inconsistently %s(%d) :: %s(%d)
3071456Sroot   	  * %.7s multiply declared %s(%d) :: %s(%d)
3081456Sroot   	  * %.7s value declared inconsistently %s(%d) :: %s(%d)
3091456Sroot   	  * %.7s function value type must be declared before use %s(%d) :: %s(%d)
3101456Sroot 	  */
3111456Sroot 		language = INLINT;
3121456Sroot 		if (    (persperdexplode(wordv[wordc], &line2, &file2))
3131456Sroot 		     && (persperdexplode(wordv[wordc-2], &line1, &file1)) ){
3141456Sroot 			nwordv1 = wordvsplice(2, wordc, wordv+1);
3151456Sroot 			nwordv2 = wordvsplice(2, wordc, wordv+1);
3161456Sroot 			nwordv1[0] = file1; nwordv1[1] = line1;
3171456Sroot 			erroradd(wordc+2, nwordv1, C_TRUE, C_DUPL); /* takes 0 based*/
3181456Sroot 			nwordv2[0] = file2; nwordv2[1] = line2;
3191456Sroot 			wordc = wordc + 2;
3201456Sroot 			wordv = nwordv2 - 1;	/* 1 based */
3211456Sroot 			return(C_TRUE);
3221456Sroot 		}
3231456Sroot 	}
3241456Sroot 	return(C_UNKNOWN);
3251456Sroot } /* end of lint 1*/
3261456Sroot 
3271456Sroot Errorclass lint2()
3281456Sroot {
3291456Sroot 	char	*file;
3301456Sroot 	char	*line;
3311456Sroot 	char	**nwordv;
3321456Sroot 	/*
3331456Sroot 	 *	Look for type 2 lint errors
3341456Sroot 	 *
3351456Sroot 	 *	%.7s used( %s(%d) ), but not defined
3361456Sroot 	 *	%.7s defined( %s(%d) ), but never used
3371456Sroot 	 *	%.7s declared( %s(%d) ), but never used or defined
3381456Sroot 	 *
3391456Sroot 	 *	bufp defined( "./metric.h"(10) ), but never used
3401456Sroot 	 */
3411456Sroot 	if (   (lastchar(wordv[2]) == '(' /* ')' */ )
3421456Sroot 	    && (strcmp(wordv[4], "),") == 0) ){
3431456Sroot 		language = INLINT;
3441456Sroot 		if (persperdexplode(wordv[3], &line, &file)){
3451456Sroot 			nwordv = wordvsplice(2, wordc, wordv+1);
3461456Sroot 			nwordv[0] = file; nwordv[1] = line;
3471456Sroot 			wordc = wordc + 2;
3481456Sroot 			wordv = nwordv - 1;	/* 1 based */
3491456Sroot 			return(C_TRUE);
3501456Sroot 		}
3511456Sroot 	}
3521456Sroot 	return(C_UNKNOWN);
3531456Sroot } /* end of lint 2*/
3541456Sroot 
3551456Sroot char	*Lint31[4] = {"returns", "value", "which", "is"};
3561456Sroot char	*Lint32[6] = {"value", "is", "used,", "but", "none", "returned"};
3571456Sroot Errorclass lint3()
3581456Sroot {
3591456Sroot 	if (   (wordvcmp(wordv+2, 4, Lint31) == 0)
3601456Sroot 	    || (wordvcmp(wordv+2, 6, Lint32) == 0) ){
3611456Sroot 		language = INLINT;
3621456Sroot 		return(C_NONSPEC);
3631456Sroot 	}
3641456Sroot 	return(C_UNKNOWN);
3651456Sroot }
3661456Sroot 
3671456Sroot /*
3681456Sroot  *	Special word vectors for use by F77 recognition
3691456Sroot  */
3701456Sroot char	*F77_fatal[3] = {"Compiler", "error", "line"};
3711456Sroot char	*F77_error[3] = {"Error", "on", "line"};
3721456Sroot char	*F77_warning[3] = {"Warning", "on", "line"};
3731456Sroot f77()
3741456Sroot {
3751456Sroot 	char	**nwordv;
3761456Sroot 	/*
3771456Sroot 	 *	look for f77 errors:
3781456Sroot 	 *	Error messages from /usr/src/cmd/f77/error.c, with
3791456Sroot 	 *	these printf formats:
3801456Sroot 	 *
3811456Sroot 	 *		Compiler error line %d of %s: %s
3821456Sroot 	 *		Error on line %d of %s: %s
3831456Sroot 	 *		Warning on line %d of %s: %s
3841456Sroot 	 */
3851456Sroot 	if (wordc < 6)
3861456Sroot 		return(C_UNKNOWN);
3871456Sroot 	if (	(lastchar(wordv[6]) == ':')
3881456Sroot 	    &&(
3891456Sroot 	       (wordvcmp(wordv+1, 3, F77_fatal) == 0)
3901456Sroot 	    || (wordvcmp(wordv+1, 3, F77_error) == 0)
3911456Sroot 	    || (wordvcmp(wordv+1, 3, F77_warning) == 0) )
3921456Sroot 	){
3931456Sroot 		language = INF77;
3941456Sroot 		nwordv = wordvsplice(2, wordc, wordv+1);
3951456Sroot 		nwordv[0] = wordv[6];
3961456Sroot 		clob_last(nwordv[0],'\0');
3971456Sroot 		nwordv[1] = wordv[4];
3981456Sroot 		wordc += 2;
3991456Sroot 		wordv = nwordv - 1;	/* 1 based */
4001456Sroot 		return(C_TRUE);
4011456Sroot 	}
4021456Sroot 	return(C_UNKNOWN);
4031456Sroot } /* end of f77 */
4041456Sroot 
4051456Sroot char	*Make_Croak[3] = {"***", "Error", "code"};
4061456Sroot char	*Make_NotRemade[5] = {"not", "remade", "because", "of", "errors"};
4071456Sroot Errorclass make()
4081456Sroot {
4091456Sroot 	if (wordvcmp(wordv+1, 3, Make_Croak) == 0){
4101456Sroot 		language = INMAKE;
4111456Sroot 		return(C_SYNC);
4121456Sroot 	}
4131456Sroot 	if  (wordvcmp(wordv+2, 5, Make_NotRemade) == 0){
4141456Sroot 		language = INMAKE;
4151456Sroot 		return(C_SYNC);
4161456Sroot 	}
4171456Sroot 	return(C_UNKNOWN);
4181456Sroot }
4191456Sroot Errorclass ri()
4201456Sroot {
4211456Sroot 	char	**nwordv;
4221456Sroot /*
4231456Sroot  *	Match an error message produced by ri; here is the
4241456Sroot  *	procedure yanked from the distributed version of ri
4251456Sroot  *	April 24, 1980.
4261456Sroot  *
4271456Sroot  *	serror(str, x1, x2, x3)
4281456Sroot  *		char str[];
4291456Sroot  *		char *x1, *x2, *x3;
4301456Sroot  *	{
4311456Sroot  *		extern int yylineno;
4321456Sroot  *
4331456Sroot  *		putc('"', stdout);
4341456Sroot  *		fputs(srcfile, stdout);
4351456Sroot  *		putc('"', stdout);
4361456Sroot  *		fprintf(stdout, " %d: ", yylineno);
4371456Sroot  *		fprintf(stdout, str, x1, x2, x3);
4381456Sroot  *		fprintf(stdout, "\n");
4391456Sroot  *		synerrs++;
4401456Sroot  *	}
4411456Sroot  */
4421456Sroot 	if (  (firstchar(wordv[1]) == '"')
4431456Sroot 	    &&(lastchar(wordv[1]) == '"')
4441456Sroot 	    &&(lastchar(wordv[2]) == ':')
4451456Sroot 	    &&(isdigit(firstchar(wordv[2]))) ){
4461456Sroot 		clob_last(wordv[1], '\0');	/* drop the last " */
4471456Sroot 		wordv[1]++;	/* skip over the first " */
4481456Sroot 		clob_last(wordv[2], '\0');
4491456Sroot 		language = INRI;
4501456Sroot 		return(C_TRUE);
4511456Sroot 	}
4521456Sroot 	return(C_UNKNOWN);
4531456Sroot }
4541456Sroot 
4551456Sroot Errorclass catchall()
4561456Sroot {
4571456Sroot 	/*
4581456Sroot 	 *	Catches random things.
4591456Sroot 	 */
4601456Sroot 	language = INUNKNOWN;
4611456Sroot 	return(C_NONSPEC);
4621456Sroot } /* end of catch all*/
463