xref: /original-bsd/usr.bin/error/pi.c (revision 14531783)
1*14531783Sdist /*
2*14531783Sdist  * Copyright (c) 1980 Regents of the University of California.
3*14531783Sdist  * All rights reserved.  The Berkeley software License Agreement
4*14531783Sdist  * specifies the terms and conditions for redistribution.
5*14531783Sdist  */
6*14531783Sdist 
7*14531783Sdist #ifndef lint
8*14531783Sdist static char sccsid[] = "@(#)pi.c	5.1 (Berkeley) 05/31/85";
9*14531783Sdist #endif not lint
10*14531783Sdist 
118f7b35b5Sroot #include <stdio.h>
128f7b35b5Sroot #include <ctype.h>
138f7b35b5Sroot #include "error.h"
148f7b35b5Sroot 
158f7b35b5Sroot extern	char	*currentfilename;
168f7b35b5Sroot static	char	*c_linenumber;
178f7b35b5Sroot static	char	*unk_hdr[] = {"In", "program", "???"};
188f7b35b5Sroot static	char	**c_header = &unk_hdr[0];
198f7b35b5Sroot 
208f7b35b5Sroot /*
218f7b35b5Sroot  *	Attempt to handle error messages produced by pi (and by pc)
228f7b35b5Sroot  *
238f7b35b5Sroot  *	problem #1:	There is no file name available when a file does not
248f7b35b5Sroot  *			use a #include; this will have to be given to error
258f7b35b5Sroot  *			in the command line.
268f7b35b5Sroot  *	problem #2:	pi doesn't always tell you what line number
278f7b35b5Sroot  *			a error refers to; for example during the tree
288f7b35b5Sroot  *			walk phase of code generation and error detection,
298f7b35b5Sroot  *			an error can refer to "variable foo in procedure bletch"
308f7b35b5Sroot  *			without giving a line number
318f7b35b5Sroot  *	problem #3:	line numbers, when available, are attached to
328f7b35b5Sroot  *			the source line, along with the source line itself
338f7b35b5Sroot  *			These line numbers must be extracted, and
348f7b35b5Sroot  *			the source line thrown away.
358f7b35b5Sroot  *	problem #4:	Some error messages produce more than one line number
368f7b35b5Sroot  *			on the same message.
378f7b35b5Sroot  *			There are only two (I think):
388f7b35b5Sroot  *				%s undefined on line%s
398f7b35b5Sroot  *				%s improperly used on line%s
408f7b35b5Sroot  *			here, the %s makes line plural or singular.
418f7b35b5Sroot  *
428f7b35b5Sroot  *	Here are the error strings used in pi version 1.2 that can refer
438f7b35b5Sroot  *	to a file name or line number:
448f7b35b5Sroot  *
458f7b35b5Sroot  *		Multiply defined label in case, lines %d and %d
468f7b35b5Sroot  *		Goto %s from line %d is into a structured statement
478f7b35b5Sroot  *		End matched %s on line %d
488f7b35b5Sroot  *		Inserted keyword end matching %s on line %d
498f7b35b5Sroot  *
508f7b35b5Sroot  *	Here are the general pi patterns recognized:
518f7b35b5Sroot  *	define piptr == -.*^-.*
528f7b35b5Sroot  *	define msg = .*
538f7b35b5Sroot  *	define digit = [0-9]
548f7b35b5Sroot  *	definename = .*
558f7b35b5Sroot  *	define date_format letter*3 letter*3 (digit | (digit digit))
568f7b35b5Sroot  *			(digit | (digit digit)):digit*2 digit*4
578f7b35b5Sroot  *
588f7b35b5Sroot  *	{e,E} (piptr) (msg)	Encounter an error during textual scan
598f7b35b5Sroot  *	E {digit}* - (msg)	Have an error message that refers to a new line
608f7b35b5Sroot  *	E - msg			Have an error message that refers to current
618f7b35b5Sroot  *					function, program or procedure
628f7b35b5Sroot  *	(date_format) (name):	When switch compilation files
638f7b35b5Sroot  *	... (msg)		When refer to the previous line
648f7b35b5Sroot  *	'In' ('procedure'|'function'|'program') (name):
658f7b35b5Sroot  *				pi is now complaining about 2nd pass errors.
668f7b35b5Sroot  *
678f7b35b5Sroot  *	Here is the output from a compilation
688f7b35b5Sroot  *
698f7b35b5Sroot  *
708f7b35b5Sroot  *	     2  	var	i:integer;
718f7b35b5Sroot  *	e --------------^--- Inserted ';'
728f7b35b5Sroot  *	E 2 - All variables must be declared in one var part
738f7b35b5Sroot  *	E 5 - Include filename must end in .i
748f7b35b5Sroot  *	Mon Apr 21 15:56 1980  test.h:
758f7b35b5Sroot  *	     2  begin
768f7b35b5Sroot  *	e ------^--- Inserted ';'
778f7b35b5Sroot  *	Mon Apr 21 16:06 1980  test.p:
788f7b35b5Sroot  *	E 2 - Function type must be specified
798f7b35b5Sroot  *	     6  procedure foo(var x:real);
808f7b35b5Sroot  *	e ------^--- Inserted ';'
818f7b35b5Sroot  *	In function bletch:
828f7b35b5Sroot  *	  E - No assignment to the function variable
838f7b35b5Sroot  *	  w - variable x is never used
848f7b35b5Sroot  *	E 6 - foo is already defined in this block
858f7b35b5Sroot  *	In procedure foo:
868f7b35b5Sroot  *	  w - variable x is neither used nor set
878f7b35b5Sroot  *	     9  	z : = 23;
888f7b35b5Sroot  *	E --------------^--- Undefined variable
898f7b35b5Sroot  *	    10  	y = [1];
908f7b35b5Sroot  *	e ----------------^--- Inserted ':'
918f7b35b5Sroot  *	    13  	z := 345.;
928f7b35b5Sroot  *	e -----------------------^--- Digits required after decimal point
938f7b35b5Sroot  *	E 10 - Constant set involved in non set context
948f7b35b5Sroot  *	E 11 - Type clash: real is incompatible with integer
958f7b35b5Sroot  *	   ... Type of expression clashed with type of variable in assignment
968f7b35b5Sroot  *	E 12 - Parameter type not identical to type of var parameter x of foo
978f7b35b5Sroot  *	In program mung:
988f7b35b5Sroot  *	  w - variable y is never used
998f7b35b5Sroot  *	  w - type foo is never used
1008f7b35b5Sroot  *	  w - function bletch is never used
1018f7b35b5Sroot  *	  E - z undefined on lines 9 13
1028f7b35b5Sroot  */
1038f7b35b5Sroot char *Months[] = {
1048f7b35b5Sroot 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
1058f7b35b5Sroot 	"Jul", "Aug", "Sep", "Oct","Nov", "Dec",
1068f7b35b5Sroot 	0
1078f7b35b5Sroot };
1088f7b35b5Sroot char *Days[] = {
1098f7b35b5Sroot 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0
1108f7b35b5Sroot };
1118f7b35b5Sroot char *Piroutines[] = {
1128f7b35b5Sroot 		"program", "function", "procedure", 0
1138f7b35b5Sroot };
1148f7b35b5Sroot 
1158f7b35b5Sroot 
1168f7b35b5Sroot static boolean	structured, multiple;
1178f7b35b5Sroot 
1188f7b35b5Sroot char *pi_Endmatched[] = {"End", "matched"};
1198f7b35b5Sroot char *pi_Inserted[] = {"Inserted", "keyword", "end", "matching"};
1208f7b35b5Sroot 
1218f7b35b5Sroot char *pi_multiple[] = {"Mutiply", "defined", "label", "in", "case,", "line"};
1228f7b35b5Sroot char *pi_structured[] = {"is", "into", "a", "structured", "statement"};
1238f7b35b5Sroot 
1248f7b35b5Sroot char *pi_und1[] = {"undefined", "on", "line"};
1258f7b35b5Sroot char *pi_und2[] = {"undefined", "on", "lines"};
1268f7b35b5Sroot char *pi_imp1[] = {"improperly", "used", "on", "line"};
1278f7b35b5Sroot char *pi_imp2[] = {"improperly", "used", "on", "lines"};
1288f7b35b5Sroot 
1298f7b35b5Sroot boolean alldigits(string)
1302357a0a9Srrh 	reg	char	*string;
1318f7b35b5Sroot {
1328f7b35b5Sroot 	for (; *string && isdigit(*string); string++)
1338f7b35b5Sroot 		continue;
1348f7b35b5Sroot 	return(*string == '\0');
1358f7b35b5Sroot }
1368f7b35b5Sroot boolean instringset(member, set)
1378f7b35b5Sroot 		char	*member;
1382357a0a9Srrh 	reg	char	**set;
1398f7b35b5Sroot {
1408f7b35b5Sroot 	for(; *set; set++){
1418f7b35b5Sroot 		if (strcmp(*set, member) == 0)
1428f7b35b5Sroot 			return(TRUE);
1438f7b35b5Sroot 	}
1448f7b35b5Sroot 	return(FALSE);
1458f7b35b5Sroot }
1468f7b35b5Sroot 
1478f7b35b5Sroot boolean isdateformat(wordc, wordv)
1488f7b35b5Sroot 	int	wordc;
1498f7b35b5Sroot 	char	**wordv;
1508f7b35b5Sroot {
1518f7b35b5Sroot 	return(
1528f7b35b5Sroot 	        (wordc == 5)
1538f7b35b5Sroot 	     && (instringset(wordv[0], Days))
1548f7b35b5Sroot 	     && (instringset(wordv[1], Months))
1558f7b35b5Sroot 	     && (alldigits(wordv[2]))
1568f7b35b5Sroot 	     && (alldigits(wordv[4])) );
1578f7b35b5Sroot }
1588f7b35b5Sroot 
1598f7b35b5Sroot boolean piptr(string)
1602357a0a9Srrh 	reg	char	*string;
1618f7b35b5Sroot {
1628f7b35b5Sroot 	if (*string != '-')
1638f7b35b5Sroot 		return(FALSE);
1648f7b35b5Sroot 	while (*string && *string == '-')
1658f7b35b5Sroot 		string++;
1668f7b35b5Sroot 	if (*string != '^')
1678f7b35b5Sroot 		return(FALSE);
1688f7b35b5Sroot 	string++;
1698f7b35b5Sroot 	while (*string && *string == '-')
1708f7b35b5Sroot 		string++;
1718f7b35b5Sroot 	return(*string == '\0');
1728f7b35b5Sroot }
1738f7b35b5Sroot 
1748f7b35b5Sroot extern	int	wordc;
1758f7b35b5Sroot extern	char	**wordv;
1768f7b35b5Sroot 
1778f7b35b5Sroot Errorclass pi()
1788f7b35b5Sroot {
1798f7b35b5Sroot 	char	**nwordv;
1808f7b35b5Sroot 
1817589d586Ssam 	if (wordc < 2)
1827589d586Ssam 		return (C_UNKNOWN);
1838f7b35b5Sroot 	if (   ( strlen(wordv[1]) == 1)
1848f7b35b5Sroot 	    && ( (wordv[1][0] == 'e') || (wordv[1][0] == 'E') )
1858f7b35b5Sroot 	    && ( piptr(wordv[2]) )
1868f7b35b5Sroot 	) {
1878f7b35b5Sroot 		boolean	longpiptr = 0;
1888f7b35b5Sroot 		/*
1898f7b35b5Sroot 		 *	We have recognized a first pass error of the form:
1908f7b35b5Sroot 		 *	letter ------^---- message
1918f7b35b5Sroot 		 *
1928f7b35b5Sroot 		 *	turn into an error message of the form:
1938f7b35b5Sroot 		 *
1948f7b35b5Sroot 		 *	file line 'pascal errortype' letter \n |---- message
1958f7b35b5Sroot 		 *	or of the form:
1968f7b35b5Sroot 		 *	file line letter |---- message
1978f7b35b5Sroot 		 *		when there are strlen("(*[pi]") or more
1988f7b35b5Sroot 		 *		preceding '-' on the error pointer.
1998f7b35b5Sroot 		 *
2008f7b35b5Sroot 		 *	Where the | is intended to be a down arrow, so that
2018f7b35b5Sroot 		 *	the pi error messages can be inserted above the
2028f7b35b5Sroot 		 *	line in error, instead of below.  (All of the other
2038f7b35b5Sroot 		 *	langauges put thier messages before the source line,
2048f7b35b5Sroot 		 *	instead of after it as does pi.)
2058f7b35b5Sroot 		 *
2068f7b35b5Sroot 		 *	where the pointer to the error has been truncated
2078f7b35b5Sroot 		 *	by 6 characters to account for the fact that
2088f7b35b5Sroot 		 *	the pointer points into a tab preceded input line.
2098f7b35b5Sroot 		 */
2108f7b35b5Sroot 		language = INPI;
2112357a0a9Srrh 		(void)substitute(wordv[2], '^', '|');
2128f7b35b5Sroot 		longpiptr = position(wordv[2],'|') > (6+8);
2138f7b35b5Sroot 		nwordv = wordvsplice(longpiptr ? 2 : 4, wordc, wordv+1);
2148f7b35b5Sroot 		nwordv[0] = strsave(currentfilename);
2158f7b35b5Sroot 		nwordv[1] = strsave(c_linenumber);
2168f7b35b5Sroot 		if (!longpiptr){
2178f7b35b5Sroot 			nwordv[2] = "pascal errortype";
2188f7b35b5Sroot 			nwordv[3] = wordv[1];
2198f7b35b5Sroot 			nwordv[4] = strsave("%%%\n");
2208f7b35b5Sroot 			if (strlen(nwordv[5]) > (8-2))	/* this is the pointer */
2218f7b35b5Sroot 				nwordv[5] += (8-2);	/* bump over 6 characters */
2228f7b35b5Sroot 		}
2238f7b35b5Sroot 		wordv = nwordv - 1;		/* convert to 1 based */
2248f7b35b5Sroot 		wordc += longpiptr ? 2 : 4;
2258f7b35b5Sroot 		return(C_TRUE);
2268f7b35b5Sroot 	}
2278f7b35b5Sroot 	if (   (wordc >= 4)
2288f7b35b5Sroot 	    && (strlen(wordv[1]) == 1)
2298f7b35b5Sroot 	    && ( (*wordv[1] == 'E') || (*wordv[1] == 'w') || (*wordv[1] == 'e') )
2308f7b35b5Sroot 	    && (alldigits(wordv[2]))
2318f7b35b5Sroot 	    && (strlen(wordv[3]) == 1)
2328f7b35b5Sroot 	    && (wordv[3][0] == '-')
2338f7b35b5Sroot 	){
2348f7b35b5Sroot 		/*
2358f7b35b5Sroot 		 *	Message of the form: letter linenumber - message
2368f7b35b5Sroot 		 *	Turn into form: filename linenumber letter - message
2378f7b35b5Sroot 		 */
2388f7b35b5Sroot 		language = INPI;
2398f7b35b5Sroot 		nwordv = wordvsplice(1, wordc, wordv + 1);
2408f7b35b5Sroot 		nwordv[0] = strsave(currentfilename);
2418f7b35b5Sroot 		nwordv[1] = wordv[2];
2428f7b35b5Sroot 		nwordv[2] = wordv[1];
2438f7b35b5Sroot 		c_linenumber = wordv[2];
2448f7b35b5Sroot 		wordc += 1;
2458f7b35b5Sroot 		wordv = nwordv - 1;
2468f7b35b5Sroot 		return(C_TRUE);
2478f7b35b5Sroot 	}
2488f7b35b5Sroot 	if (   (wordc >= 3)
2498f7b35b5Sroot 	    && (strlen(wordv[1]) == 1)
2508f7b35b5Sroot 	    && ( (*(wordv[1]) == 'E') || (*(wordv[1]) == 'w') || (*(wordv[1]) == 'e') )
2518f7b35b5Sroot 	    && (strlen(wordv[2]) == 1)
2528f7b35b5Sroot 	    && (wordv[2][0] == '-')
2538f7b35b5Sroot 	) {
2548f7b35b5Sroot 		/*
2558f7b35b5Sroot 		 *	Message of the form: letter - message
2568f7b35b5Sroot 		 *	This happens only when we are traversing the tree
2578f7b35b5Sroot 		 *	during the second pass of pi, and discover semantic
2588f7b35b5Sroot 		 *	errors.
2598f7b35b5Sroot 		 *
2608f7b35b5Sroot 		 *	We have already (presumably) saved the header message
2618f7b35b5Sroot 		 *	and can now construct a nulled error message for the
2628f7b35b5Sroot 		 *	current file.
2638f7b35b5Sroot 		 *
2648f7b35b5Sroot 		 *	Turns into a message of the form:
2658f7b35b5Sroot 		 *	filename (header) letter - message
2668f7b35b5Sroot 		 *
2678f7b35b5Sroot 		 *	First, see if it is a message referring to more than
2688f7b35b5Sroot 		 *	one line number.  Only of the form:
2698f7b35b5Sroot  		 *		%s undefined on line%s
2708f7b35b5Sroot  		 *		%s improperly used on line%s
2718f7b35b5Sroot 		 */
2728f7b35b5Sroot 		boolean undefined = 0;
2738f7b35b5Sroot 		int	wordindex;
2748f7b35b5Sroot 
2758f7b35b5Sroot 		language = INPI;
2768f7b35b5Sroot 		if (    (undefined = (wordvcmp(wordv+2, 3, pi_und1) == 0) )
2778f7b35b5Sroot 		     || (undefined = (wordvcmp(wordv+2, 3, pi_und2) == 0) )
2788f7b35b5Sroot 		     || (wordvcmp(wordv+2, 4, pi_imp1) == 0)
2798f7b35b5Sroot 		     || (wordvcmp(wordv+2, 4, pi_imp2) == 0)
2808f7b35b5Sroot 		){
2818f7b35b5Sroot 			for (wordindex = undefined ? 5 : 6; wordindex <= wordc;
2828f7b35b5Sroot 			    wordindex++){
2838f7b35b5Sroot 				nwordv = wordvsplice(2, undefined ? 2 : 3, wordv+1);
2848f7b35b5Sroot 				nwordv[0] = strsave(currentfilename);
2858f7b35b5Sroot 				nwordv[1] = wordv[wordindex];
2868f7b35b5Sroot 				if (wordindex != wordc)
2878f7b35b5Sroot 					erroradd(undefined ? 4 : 5, nwordv,
2888f7b35b5Sroot 						C_TRUE, C_UNKNOWN);
2898f7b35b5Sroot 			}
2908f7b35b5Sroot 			wordc = undefined ? 4 : 5;
2918f7b35b5Sroot 			wordv = nwordv - 1;
2928f7b35b5Sroot 			return(C_TRUE);
2938f7b35b5Sroot 		}
2948f7b35b5Sroot 
2958f7b35b5Sroot 		nwordv = wordvsplice(1+3, wordc, wordv+1);
2968f7b35b5Sroot 		nwordv[0] = strsave(currentfilename);
2978f7b35b5Sroot 		nwordv[1] = strsave(c_header[0]);
2988f7b35b5Sroot 		nwordv[2] = strsave(c_header[1]);
2998f7b35b5Sroot 		nwordv[3] = strsave(c_header[2]);
3008f7b35b5Sroot 		wordv = nwordv - 1;
3018f7b35b5Sroot 		wordc += 1 + 3;
3028f7b35b5Sroot 		return(C_THISFILE);
3038f7b35b5Sroot 	}
3048f7b35b5Sroot 	if (strcmp(wordv[1], "...") == 0){
3058f7b35b5Sroot 		/*
3068f7b35b5Sroot 		 *	have a continuation error message
3078f7b35b5Sroot 		 *	of the form: ... message
3088f7b35b5Sroot 		 *	Turn into form : filename linenumber message
3098f7b35b5Sroot 		 */
3108f7b35b5Sroot 		language = INPI;
3118f7b35b5Sroot 		nwordv = wordvsplice(1, wordc, wordv+1);
3128f7b35b5Sroot 		nwordv[0] = strsave(currentfilename);
3138f7b35b5Sroot 		nwordv[1] = strsave(c_linenumber);
3148f7b35b5Sroot 		wordv = nwordv - 1;
3158f7b35b5Sroot 		wordc += 1;
3168f7b35b5Sroot 		return(C_TRUE);
3178f7b35b5Sroot 	}
3188f7b35b5Sroot 	if(   (wordc == 6)
3198f7b35b5Sroot 	   && (lastchar(wordv[6]) == ':')
3208f7b35b5Sroot 	   && (isdateformat(5, wordv + 1))
3218f7b35b5Sroot 	){
3228f7b35b5Sroot 		/*
3238f7b35b5Sroot 		 *	Have message that tells us we have changed files
3248f7b35b5Sroot 		 */
3258f7b35b5Sroot 		language = INPI;
3268f7b35b5Sroot 		currentfilename = strsave(wordv[6]);
3278f7b35b5Sroot 		clob_last(currentfilename, '\0');
3288f7b35b5Sroot 		return(C_SYNC);
3298f7b35b5Sroot 	}
3308f7b35b5Sroot 	if(   (wordc == 3)
3318f7b35b5Sroot 	   && (strcmp(wordv[1], "In") == 0)
3328f7b35b5Sroot 	   && (lastchar(wordv[3]) == ':')
3338f7b35b5Sroot 	   && (instringset(wordv[2], Piroutines))
3348f7b35b5Sroot 	) {
3358f7b35b5Sroot 		language = INPI;
3368f7b35b5Sroot 		c_header = wordvsplice(0, wordc, wordv+1);
3378f7b35b5Sroot 		return(C_SYNC);
3388f7b35b5Sroot 	}
3398f7b35b5Sroot 	/*
3408f7b35b5Sroot 	 *	now, check for just the line number followed by the text
3418f7b35b5Sroot 	 */
3428f7b35b5Sroot 	if (alldigits(wordv[1])){
3438f7b35b5Sroot 		language = INPI;
3448f7b35b5Sroot 		c_linenumber = wordv[1];
3458f7b35b5Sroot 		return(C_IGNORE);
3468f7b35b5Sroot 	}
3478f7b35b5Sroot 	/*
3488f7b35b5Sroot 	 *	Attempt to match messages refering to a line number
3498f7b35b5Sroot 	 *
3508f7b35b5Sroot 	 *	Multiply defined label in case, lines %d and %d
3518f7b35b5Sroot 	 *	Goto %s from line %d is into a structured statement
3528f7b35b5Sroot 	 *	End matched %s on line %d
3538f7b35b5Sroot 	 *	Inserted keyword end matching %s on line %d
3548f7b35b5Sroot 	 */
3558f7b35b5Sroot 	multiple = structured = 0;
3568f7b35b5Sroot 	if (
3578f7b35b5Sroot 	       ( (wordc == 6) && (wordvcmp(wordv+1, 2, pi_Endmatched) == 0))
3588f7b35b5Sroot 	    || ( (wordc == 8) && (wordvcmp(wordv+1, 4, pi_Inserted) == 0))
3598f7b35b5Sroot 	    || ( multiple = ((wordc == 9) && (wordvcmp(wordv+1,6, pi_multiple) == 0) ) )
3608f7b35b5Sroot 	    || ( structured = ((wordc == 10) && (wordvcmp(wordv+6,5, pi_structured) == 0 ) ))
3618f7b35b5Sroot 	){
3628f7b35b5Sroot 		language = INPI;
3638f7b35b5Sroot 		nwordv = wordvsplice(2, wordc, wordv+1);
3648f7b35b5Sroot 		nwordv[0] = strsave(currentfilename);
3658f7b35b5Sroot 		nwordv[1] = structured ? wordv [5] : wordv[wordc];
3668f7b35b5Sroot 		wordc += 2;
3678f7b35b5Sroot 		wordv = nwordv - 1;
3688f7b35b5Sroot 		if (!multiple)
3698f7b35b5Sroot 			return(C_TRUE);
3708f7b35b5Sroot 		erroradd(wordc, nwordv, C_TRUE, C_UNKNOWN);
3718f7b35b5Sroot 		nwordv = wordvsplice(0, wordc, nwordv);
3728f7b35b5Sroot 		nwordv[1] = wordv[wordc - 2];
3738f7b35b5Sroot 		return(C_TRUE);
3748f7b35b5Sroot 	}
3758f7b35b5Sroot 	return(C_UNKNOWN);
3768f7b35b5Sroot }
377