1/*	$NetBSD: pi.c,v 1.17 2009/08/13 06:59:37 dholland 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
35static char sccsid[] = "@(#)pi.c	8.1 (Berkeley) 6/6/93";
36#endif
37__RCSID("$NetBSD: pi.c,v 1.17 2009/08/13 06:59:37 dholland Exp $");
38#endif /* not lint */
39
40#include <stdio.h>
41#include <ctype.h>
42#include <string.h>
43#include <stdlib.h>
44#include "error.h"
45
46#if 0 /* not const-correct */
47static char *unk_hdr[] = {"In", "program", "???"};
48#else
49DECL_STRINGS_3(static, unk_hdr, "In", "program", "???");
50#endif
51
52static char *c_linenumber;
53static char **c_header = &unk_hdr[0];
54
55static boolean alldigits(const char *);
56static boolean isdateformat(int, char **);
57static boolean instringset(const char *, const char **);
58static boolean piptr(const char *);
59
60
61/*
62 * Attempt to handle error messages produced by pi (and by pc)
63 *
64 *	problem #1:	There is no file name available when a file does not
65 *			use a #include; this will have to be given to error
66 *			in the command line.
67 *	problem #2:	pi doesn't always tell you what line number
68 *			a error refers to; for example during the tree
69 *			walk phase of code generation and error detection,
70 *			an error can refer to "variable foo in procedure bletch"
71 *			without giving a line number
72 *	problem #3:	line numbers, when available, are attached to
73 *			the source line, along with the source line itself
74 *			These line numbers must be extracted, and
75 *			the source line thrown away.
76 *	problem #4:	Some error messages produce more than one line number
77 *			on the same message.
78 *			There are only two (I think):
79 *				%s undefined on line%s
80 *				%s improperly used on line%s
81 *			here, the %s makes line plural or singular.
82 *
83 *	Here are the error strings used in pi version 1.2 that can refer
84 *	to a file name or line number:
85 *
86 *		Multiply defined label in case, lines %d and %d
87 *		Goto %s from line %d is into a structured statement
88 *		End matched %s on line %d
89 *		Inserted keyword end matching %s on line %d
90 *
91 *	Here are the general pi patterns recognized:
92 *	define piptr == -.*^-.*
93 *	define msg = .*
94 *	define digit = [0-9]
95 *	definename = .*
96 *	define date_format letter*3 letter*3 (digit | (digit digit))
97 *			(digit | (digit digit)):digit*2 digit*4
98 *
99 *	{e,E} (piptr) (msg)	Encounter an error during textual scan
100 *	E {digit}* - (msg)	Have an error message that refers to a new line
101 *	E - msg			Have an error message that refers to current
102 *					function, program or procedure
103 *	(date_format) (name):	When switch compilation files
104 *	... (msg)		When refer to the previous line
105 *	'In' ('procedure'|'function'|'program') (name):
106 *				pi is now complaining about 2nd pass errors.
107 *
108 *	Here is the output from a compilation
109 *
110 *
111 *	     2  	var	i:integer;
112 *	e --------------^--- Inserted ';'
113 *	E 2 - All variables must be declared in one var part
114 *	E 5 - Include filename must end in .i
115 *	Mon Apr 21 15:56 1980  test.h:
116 *	     2  begin
117 *	e ------^--- Inserted ';'
118 *	Mon Apr 21 16:06 1980  test.p:
119 *	E 2 - Function type must be specified
120 *	     6  procedure foo(var x:real);
121 *	e ------^--- Inserted ';'
122 *	In function bletch:
123 *	  E - No assignment to the function variable
124 *	  w - variable x is never used
125 *	E 6 - foo is already defined in this block
126 *	In procedure foo:
127 *	  w - variable x is neither used nor set
128 *	     9  	z : = 23;
129 *	E --------------^--- Undefined variable
130 *	    10  	y = [1];
131 *	e ----------------^--- Inserted ':'
132 *	    13  	z := 345.;
133 *	e -----------------------^--- Digits required after decimal point
134 *	E 10 - Constant set involved in non set context
135 *	E 11 - Type clash: real is incompatible with integer
136 *	   ... Type of expression clashed with type of variable in assignment
137 *	E 12 - Parameter type not identical to type of var parameter x of foo
138 *	In program mung:
139 *	  w - variable y is never used
140 *	  w - type foo is never used
141 *	  w - function bletch is never used
142 *	  E - z undefined on lines 9 13
143 */
144static const char *Months[] = {
145	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
146	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
147	0
148};
149static const char *Days[] = {
150	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 0
151};
152static const char *Piroutines[] = {
153	"program", "function", "procedure", 0
154};
155
156
157static boolean structured, multiple;
158
159#if 0 /* not const-correct */
160static char *pi_Endmatched[] = {"End", "matched"};
161static char *pi_Inserted[] = {"Inserted", "keyword", "end", "matching"};
162
163static char *pi_multiple[] = {"Mutiply", "defined", "label", "in", "case,", "line"};
164static char *pi_structured[] = {"is", "into", "a", "structured", "statement"};
165
166static char *pi_und1[] = {"undefined", "on", "line"};
167static char *pi_und2[] = {"undefined", "on", "lines"};
168static char *pi_imp1[] = {"improperly", "used", "on", "line"};
169static char *pi_imp2[] = {"improperly", "used", "on", "lines"};
170
171#else
172DECL_STRINGS_2(static, pi_Endmatched, "End", "matched");
173DECL_STRINGS_4(static, pi_Inserted, "Inserted", "keyword", "end", "matching");
174
175DECL_STRINGS_6(static, pi_multiple,
176	       "Mutiply", "defined", "label", "in", "case,", "line");
177DECL_STRINGS_5(static, pi_structured,
178	       "is", "into", "a", "structured", "statement");
179
180DECL_STRINGS_3(static, pi_und1, "undefined", "on", "line");
181DECL_STRINGS_3(static, pi_und2, "undefined", "on", "lines");
182DECL_STRINGS_4(static, pi_imp1, "improperly", "used", "on", "line");
183DECL_STRINGS_4(static, pi_imp2, "improperly", "used", "on", "lines");
184
185#endif
186
187static boolean
188alldigits(const char *string)
189{
190	for (; *string && isdigit((unsigned char)*string); string++)
191		continue;
192	return (*string == '\0');
193}
194
195static boolean
196instringset(const char *member, const char **set)
197{
198	for (; *set; set++) {
199		if (strcmp(*set, member) == 0)
200			return true;
201	}
202	return false;
203}
204
205static boolean
206isdateformat(int wordc, char **wordv)
207{
208	return (
209	        (wordc == 5)
210	     && (instringset(wordv[0], Days))
211	     && (instringset(wordv[1], Months))
212	     && (alldigits(wordv[2]))
213	     && (alldigits(wordv[4])));
214}
215
216static boolean
217piptr(const char *string)
218{
219	if (*string != '-')
220		return false;
221	while (*string && *string == '-')
222		string++;
223	if (*string != '^')
224		return false;
225	string++;
226	while (*string && *string == '-')
227		string++;
228	return (*string == '\0');
229}
230
231Errorclass
232pi(void)
233{
234	char **nwordv;
235
236	nwordv = NULL;
237	if (cur_wordc < 2)
238		return (C_UNKNOWN);
239	if (strlen(cur_wordv[1]) == 1
240	    && ( cur_wordv[1][0] == 'e' || cur_wordv[1][0] == 'E')
241	    && piptr(cur_wordv[2])
242	) {
243		boolean longpiptr = 0;
244
245		/*
246		 *	We have recognized a first pass error of the form:
247		 *	letter ------^---- message
248		 *
249		 *	turn into an error message of the form:
250		 *
251		 *	file line 'pascal errortype' letter \n |---- message
252		 *	or of the form:
253		 *	file line letter |---- message
254		 *		when there are strlen("(*[pi]") or more
255		 *		preceding '-' on the error pointer.
256		 *
257		 *	Where the | is intended to be a down arrow, so that
258		 *	the pi error messages can be inserted above the
259		 *	line in error, instead of below.  (All of the other
260		 *	languages put their messages before the source line,
261		 *	instead of after it as does pi.)
262		 *
263		 *	where the pointer to the error has been truncated
264		 *	by 6 characters to account for the fact that
265		 *	the pointer points into a tab preceded input line.
266		 */
267		language = INPI;
268		(void)substitute(cur_wordv[2], '^', '|');
269		longpiptr = position(cur_wordv[2],'|') > (6+8);
270		nwordv = wordvsplice(longpiptr ? 2 : 4, cur_wordc, cur_wordv+1);
271		nwordv[0] = strdup(currentfilename);
272		nwordv[1] = strdup(c_linenumber);
273		if (!longpiptr) {
274			nwordv[2] = Strdup("pascal errortype"); /* XXX leaked */
275			nwordv[3] = cur_wordv[1];
276			nwordv[4] = strdup("%%%\n");
277			if (strlen(nwordv[5]) > (8-2))	/* this is the pointer */
278				nwordv[5] += (8-2);	/* bump over 6 characters */
279		}
280		cur_wordv = nwordv - 1;		/* convert to 1 based */
281		cur_wordc += longpiptr ? 2 : 4;
282		return (C_TRUE);
283	}
284	if (cur_wordc >= 4
285	    && strlen(cur_wordv[1]) == 1
286	    && (*cur_wordv[1] == 'E' || *cur_wordv[1] == 'w' || *cur_wordv[1] == 'e')
287	    && alldigits(cur_wordv[2])
288	    && strlen(cur_wordv[3]) == 1
289	    && cur_wordv[3][0] == '-'
290	) {
291		/*
292		 * Message of the form: letter linenumber - message
293		 * Turn into form: filename linenumber letter - message
294		 */
295		language = INPI;
296		nwordv = wordvsplice(1, cur_wordc, cur_wordv + 1);
297		nwordv[0] = strdup(currentfilename);
298		nwordv[1] = cur_wordv[2];
299		nwordv[2] = cur_wordv[1];
300		c_linenumber = cur_wordv[2];
301		cur_wordc += 1;
302		cur_wordv = nwordv - 1;
303		return (C_TRUE);
304	}
305	if (cur_wordc >= 3
306	    && strlen(cur_wordv[1]) == 1
307	    && (*cur_wordv[1] == 'E' || *cur_wordv[1] == 'w' || *cur_wordv[1] == 'e')
308	    && strlen(cur_wordv[2]) == 1
309	    && cur_wordv[2][0] == '-'
310	) {
311		/*
312		 * Message of the form: letter - message
313		 *
314		 * This happens only when we are traversing the tree
315		 * during the second pass of pi, and discover semantic
316		 * errors.
317		 *
318		 * We have already (presumably) saved the header message
319		 * and can now construct a nulled error message for the
320		 * current file.
321		 *
322		 * Turns into a message of the form:
323		 *      filename (header) letter - message
324		 *
325		 * First, see if it is a message referring to more than
326		 * one line number.  Only of the form:
327 		 *      %s undefined on line%s
328 		 *      %s improperly used on line%s
329		 */
330		boolean undefined = 0;
331		int wordindex;
332
333		language = INPI;
334		if ((undefined = (wordvcmp(cur_wordv+2, 3, pi_und1) == 0))
335		     || (undefined = (wordvcmp(cur_wordv+2, 3, pi_und2) == 0))
336		     || wordvcmp(cur_wordv+2, 4, pi_imp1) == 0
337		     || wordvcmp(cur_wordv+2, 4, pi_imp2) == 0
338		) {
339			for (wordindex = undefined ? 5 : 6;
340			     wordindex <= cur_wordc;
341			     wordindex++) {
342				if (nwordv) {
343					free(nwordv[0]);
344					free(nwordv);
345				}
346				nwordv = wordvsplice(2, undefined ? 2 : 3, cur_wordv+1);
347				nwordv[0] = strdup(currentfilename);
348				nwordv[1] = cur_wordv[wordindex];
349				if (wordindex != cur_wordc)
350					erroradd(undefined ? 4 : 5, nwordv,
351						C_TRUE, C_UNKNOWN);
352			}
353			cur_wordc = undefined ? 4 : 5;
354			cur_wordv = nwordv - 1;
355			return (C_TRUE);
356		}
357
358		nwordv = wordvsplice(1+3, cur_wordc, cur_wordv+1);
359		nwordv[0] = strdup(currentfilename);
360		nwordv[1] = strdup(c_header[0]);
361		nwordv[2] = strdup(c_header[1]);
362		nwordv[3] = strdup(c_header[2]);
363		cur_wordv = nwordv - 1;
364		cur_wordc += 1 + 3;
365		return (C_THISFILE);
366	}
367	if (strcmp(cur_wordv[1], "...") == 0 && c_linenumber &&
368	    currentfilename != default_currentfilename) {
369		/*
370		 * have a continuation error message
371		 * of the form: ... message
372		 * Turn into form : filename linenumber message
373		 */
374		language = INPI;
375		nwordv = wordvsplice(1, cur_wordc, cur_wordv+1);
376		nwordv[0] = strdup(currentfilename);
377		nwordv[1] = strdup(c_linenumber);
378		cur_wordv = nwordv - 1;
379		cur_wordc += 1;
380		return (C_TRUE);
381	}
382	if (cur_wordc == 6
383	   && lastchar(cur_wordv[6]) == ':'
384	   && isdateformat(5, cur_wordv + 1)
385	) {
386		/*
387		 * Have message that tells us we have changed files
388		 */
389		language = INPI;
390		currentfilename = strdup(cur_wordv[6]);
391		clob_last(currentfilename, '\0');
392		return (C_SYNC);
393	}
394	if (cur_wordc == 3
395	   && strcmp(cur_wordv[1], "In") == 0
396	   && lastchar(cur_wordv[3]) == ':'
397	   && instringset(cur_wordv[2], Piroutines)
398	) {
399		language = INPI;
400		c_header = wordvsplice(0, cur_wordc, cur_wordv+1);
401		return (C_SYNC);
402	}
403
404	/*
405	 * now, check for just the line number followed by the text
406	 */
407	if (alldigits(cur_wordv[1])) {
408		language = INPI;
409		c_linenumber = cur_wordv[1];
410		return (C_IGNORE);
411	}
412
413	/*
414	 * Attempt to match messages refering to a line number
415	 *
416	 * Multiply defined label in case, lines %d and %d
417	 * Goto %s from line %d is into a structured statement
418	 * End matched %s on line %d
419	 * Inserted keyword end matching %s on line %d
420	 */
421	multiple = structured = 0;
422	if (
423	       (cur_wordc == 6 && wordvcmp(cur_wordv+1, 2, pi_Endmatched) == 0)
424	    || (cur_wordc == 8 && wordvcmp(cur_wordv+1, 4, pi_Inserted) == 0)
425	    || (multiple = (cur_wordc == 9 && wordvcmp(cur_wordv+1,6, pi_multiple) == 0))
426	    || (structured = (cur_wordc == 10 && wordvcmp(cur_wordv+6,5, pi_structured) == 0))
427	) {
428		language = INPI;
429		nwordv = wordvsplice(2, cur_wordc, cur_wordv+1);
430		nwordv[0] = strdup(currentfilename);
431		nwordv[1] = structured ? cur_wordv [5] : cur_wordv[cur_wordc];
432		cur_wordc += 2;
433		cur_wordv = nwordv - 1;
434		if (!multiple)
435			return (C_TRUE);
436		erroradd(cur_wordc, nwordv, C_TRUE, C_UNKNOWN);
437		nwordv = wordvsplice(0, cur_wordc, nwordv);
438		nwordv[1] = cur_wordv[cur_wordc - 2];
439		return (C_TRUE);
440	}
441	return (C_UNKNOWN);
442}
443