xref: /csrg-svn/usr.bin/error/subr.c (revision 1459)
1*1459Sroot static	char *sccsid = "@(#)subr.c	1.1 (Berkeley) 10/16/80";
2*1459Sroot #include <stdio.h>
3*1459Sroot #include <ctype.h>
4*1459Sroot #include "error.h"
5*1459Sroot /*
6*1459Sroot  *	go through and arrayify a list of rules
7*1459Sroot  */
8*1459Sroot arrayify(e_length, e_array, header)
9*1459Sroot 	int			*e_length;
10*1459Sroot 	struct	error_desc	***e_array;
11*1459Sroot 	struct	error_desc	*header;
12*1459Sroot {
13*1459Sroot 	register	struct	error_desc	*errorp;
14*1459Sroot 	register	struct	error_desc	**array;
15*1459Sroot 	register	int	listlength;
16*1459Sroot 	register	int	listindex;
17*1459Sroot 
18*1459Sroot 	for (errorp = header, listlength = 0;
19*1459Sroot 	     errorp; errorp = errorp->error_next, listlength++)
20*1459Sroot 		continue;
21*1459Sroot 	array = (struct error_desc **)Calloc(listlength+1,sizeof (struct error_desc*));
22*1459Sroot 	for(listindex = 0, errorp = header;
23*1459Sroot 	    listindex < listlength;
24*1459Sroot 	    listindex++, errorp = errorp->error_next){
25*1459Sroot 		array[listindex] = errorp;
26*1459Sroot 		errorp->error_position = listindex;
27*1459Sroot 	}
28*1459Sroot 	array[listindex] = (struct error_desc *)0;
29*1459Sroot 	*e_length = listlength;
30*1459Sroot 	*e_array = array;
31*1459Sroot }
32*1459Sroot 
33*1459Sroot /*VARARGS1*/
34*1459Sroot error(msg, a1, a2, a3)
35*1459Sroot 	char	*msg;
36*1459Sroot {
37*1459Sroot 	fprintf(stderr, "Error: ");
38*1459Sroot 	fprintf(stderr, msg, a1, a2, a3);
39*1459Sroot 	fprintf(stderr, "\n");
40*1459Sroot 	fflush(stdout);
41*1459Sroot 	fflush(stderr);
42*1459Sroot 	exit(6);
43*1459Sroot }
44*1459Sroot /*ARGSUSED*/
45*1459Sroot char *Calloc(nelements, size)
46*1459Sroot 	int	nelements;
47*1459Sroot 	int	size;
48*1459Sroot {
49*1459Sroot 	char	*back;
50*1459Sroot 	if ( (back = (char *)calloc(nelements, size)) == (char *)NULL){
51*1459Sroot 		error("Ran out of memory.\n");
52*1459Sroot 		exit(1);
53*1459Sroot 	}
54*1459Sroot 	return(back);
55*1459Sroot }
56*1459Sroot 
57*1459Sroot char *strsave(instring)
58*1459Sroot 	char	*instring;
59*1459Sroot {
60*1459Sroot 	char	*outstring;
61*1459Sroot 	strcpy(outstring = (char *)Calloc(1, strlen(instring) + 1), instring);
62*1459Sroot 	return(outstring);
63*1459Sroot }
64*1459Sroot /*
65*1459Sroot  *	find the position of a given character in a string
66*1459Sroot  *		(one based)
67*1459Sroot  */
68*1459Sroot int position(string, ch)
69*1459Sroot 	register	char	*string;
70*1459Sroot 	register	char	ch;
71*1459Sroot {
72*1459Sroot 	register	int	i;
73*1459Sroot 	for (i=1; *string; string++, i++){
74*1459Sroot 		if (*string == ch)
75*1459Sroot 			return(i);
76*1459Sroot 	}
77*1459Sroot 	return(-1);
78*1459Sroot }
79*1459Sroot /*
80*1459Sroot  *	clobber the first occurance of ch in string by the new character
81*1459Sroot  */
82*1459Sroot char *substitute(string, chold, chnew)
83*1459Sroot 	char	*string;
84*1459Sroot 	char	chold, chnew;
85*1459Sroot {
86*1459Sroot 	register	char	*cp = string;
87*1459Sroot 
88*1459Sroot 	while (*cp){
89*1459Sroot 		if (*cp == chold){
90*1459Sroot 			*cp = chnew;
91*1459Sroot 			break;
92*1459Sroot 		}
93*1459Sroot 		cp++;
94*1459Sroot 	}
95*1459Sroot 	return(string);
96*1459Sroot }
97*1459Sroot 
98*1459Sroot char lastchar(string)
99*1459Sroot 	char	*string;
100*1459Sroot {
101*1459Sroot 	int	length;
102*1459Sroot 	length = strlen(string);
103*1459Sroot 	if (length >= 1)
104*1459Sroot 		return(string[length-1]);
105*1459Sroot 	else
106*1459Sroot 		return('\0');
107*1459Sroot }
108*1459Sroot 
109*1459Sroot char firstchar(string)
110*1459Sroot 	char	*string;
111*1459Sroot {
112*1459Sroot 	return(string[0]);
113*1459Sroot }
114*1459Sroot 
115*1459Sroot char	next_lastchar(string)
116*1459Sroot 	char	*string;
117*1459Sroot {
118*1459Sroot 	int	length;
119*1459Sroot 	length = strlen(string);
120*1459Sroot 	if (length >= 2)
121*1459Sroot 		return(string[length - 2]);
122*1459Sroot 	else
123*1459Sroot 		return('\0');
124*1459Sroot }
125*1459Sroot 
126*1459Sroot clob_last(string, newstuff)
127*1459Sroot 	char	*string, newstuff;
128*1459Sroot {
129*1459Sroot 	int	length;
130*1459Sroot 	length = strlen(string);
131*1459Sroot 	if (length >= 1)
132*1459Sroot 		string[length - 1] = newstuff;
133*1459Sroot }
134*1459Sroot 
135*1459Sroot /*
136*1459Sroot  *	parse a string that is the result of a format %s(%d)
137*1459Sroot  *	return TRUE if this is of the proper format
138*1459Sroot  */
139*1459Sroot boolean persperdexplode(string, r_perd, r_pers)
140*1459Sroot 	char	*string;
141*1459Sroot 	char	**r_perd, **r_pers;
142*1459Sroot {
143*1459Sroot 	register	char	*cp;
144*1459Sroot 	int		length;
145*1459Sroot 
146*1459Sroot 	length = strlen(string);
147*1459Sroot 	if (   (length >= 4)
148*1459Sroot 	    && (string[length - 1] == ')' ) ){
149*1459Sroot 		for (cp = &string[length - 2];
150*1459Sroot 		     (isdigit(*cp)) && (*cp != '(');
151*1459Sroot 		     --cp)
152*1459Sroot 			continue;
153*1459Sroot 		if (*cp == '('){
154*1459Sroot 			string[length - 1] = '\0';	/* clobber the ) */
155*1459Sroot 			*r_perd = strsave(cp+1);
156*1459Sroot 			string[length - 1] = ')';
157*1459Sroot 			*cp = '\0';			/* clobber the ( */
158*1459Sroot 			*r_pers = strsave(string);
159*1459Sroot 			*cp = '(';
160*1459Sroot 			return(TRUE);
161*1459Sroot 		}
162*1459Sroot 	}
163*1459Sroot 	return(FALSE);
164*1459Sroot }
165*1459Sroot /*
166*1459Sroot  *	parse a quoted string that is the result of a format \"%s\"(%d)
167*1459Sroot  *	return TRUE if this is of the proper format
168*1459Sroot  */
169*1459Sroot boolean qpersperdexplode(string, r_perd, r_pers)
170*1459Sroot 	char	*string;
171*1459Sroot 	char	**r_perd, **r_pers;
172*1459Sroot {
173*1459Sroot 	register	char	*cp;
174*1459Sroot 	int		length;
175*1459Sroot 
176*1459Sroot 	length = strlen(string);
177*1459Sroot 	if (   (length >= 4)
178*1459Sroot 	    && (string[length - 1] == ')' ) ){
179*1459Sroot 		for (cp = &string[length - 2];
180*1459Sroot 		     (isdigit(*cp)) && (*cp != '(');
181*1459Sroot 		     --cp)
182*1459Sroot 			continue;
183*1459Sroot 		if (*cp == '(' && *(cp - 1) == '"'){
184*1459Sroot 			string[length - 1] = '\0';
185*1459Sroot 			*r_perd = strsave(cp+1);
186*1459Sroot 			string[length - 1] = ')';
187*1459Sroot 			*(cp - 1) = '\0';		/* clobber the " */
188*1459Sroot 			*r_pers = strsave(string + 1);
189*1459Sroot 			*(cp - 1) = '"';
190*1459Sroot 			return(TRUE);
191*1459Sroot 		}
192*1459Sroot 	}
193*1459Sroot 	return(FALSE);
194*1459Sroot }
195*1459Sroot 
196*1459Sroot static	char	cincomment[] = CINCOMMENT;
197*1459Sroot static	char	coutcomment[] = COUTCOMMENT;
198*1459Sroot static	char	fincomment[] = FINCOMMENT;
199*1459Sroot static	char	foutcomment[] = FOUTCOMMENT;
200*1459Sroot static	char	newline[] = NEWLINE;
201*1459Sroot static	char	piincomment[] = PIINCOMMENT;
202*1459Sroot static	char	pioutcomment[] = PIOUTCOMMENT;
203*1459Sroot static	char	lispincomment[] = LISPINCOMMENT;
204*1459Sroot static	char	riincomment[] = RIINCOMMENT;
205*1459Sroot static	char	rioutcomment[] = RIOUTCOMMENT;
206*1459Sroot 
207*1459Sroot struct	lang_desc lang_table[] = {
208*1459Sroot 	/*INUNKNOWN	0*/	"unknown", cincomment,	coutcomment,
209*1459Sroot 	/*INCPP		1*/	"cpp",	cincomment,    coutcomment,
210*1459Sroot 	/*INCC		2*/	"cc",	cincomment,    coutcomment,
211*1459Sroot 	/*INAS		3*/	"as",	ASINCOMMENT,   newline,
212*1459Sroot 	/*INLD		4*/	"ld",	cincomment,    coutcomment,
213*1459Sroot 	/*INLINT	5*/	"lint",	cincomment,    coutcomment,
214*1459Sroot 	/*INF77		6*/	"f77",	fincomment,    foutcomment,
215*1459Sroot 	/*INPI		7*/	"pi",	piincomment,   pioutcomment,
216*1459Sroot 	/*INPC		8*/	"pc",	piincomment,   pioutcomment,
217*1459Sroot 	/*INFRANZ	9*/	"franz",lispincomment, newline,
218*1459Sroot 	/*INLISP	10*/	"lisp",	lispincomment, newline,
219*1459Sroot 	/*INVAXIMA	11*/	"vaxima",lispincomment,newline,
220*1459Sroot 	/*INRATFOR	12*/	"ratfor",fincomment,   foutcomment,
221*1459Sroot 	/*INLEX		13*/	"lex",	cincomment,    coutcomment,
222*1459Sroot 	/*INYACC	14*/	"yacc",	cincomment,    coutcomment,
223*1459Sroot 	/*INAPL		15*/	"apl",	".lm",	       newline,
224*1459Sroot 	/*INMAKE	16*/	"make",	ASINCOMMENT,   newline,
225*1459Sroot 	/*INRI		17*/	"ri",	riincomment,   rioutcomment,
226*1459Sroot 				0,	0,	     0
227*1459Sroot };
228*1459Sroot 
229*1459Sroot printerrors(look_at_subclass, errorc, errorv)
230*1459Sroot 	boolean	look_at_subclass;
231*1459Sroot 	int	errorc;
232*1459Sroot 	struct	error_desc	*errorv[];
233*1459Sroot {
234*1459Sroot 	register	int	i;
235*1459Sroot 	register	struct	error_desc	*errorp;
236*1459Sroot 	for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]){
237*1459Sroot 		if (errorp->error_e_class == C_IGNORE)
238*1459Sroot 			continue;
239*1459Sroot 		if (look_at_subclass && errorp->error_s_class == C_DUPL)
240*1459Sroot 			continue;
241*1459Sroot 		printf("Error %d, (%s error) [%s], text = \"",
242*1459Sroot 			i,
243*1459Sroot 			class_table[errorp->error_e_class],
244*1459Sroot 			lang_table[errorp->error_language].lang_name);
245*1459Sroot 		wordvprint(stdout,errorp->error_lgtext,errorp->error_text);
246*1459Sroot 		printf("\"\n");
247*1459Sroot 	}
248*1459Sroot }
249*1459Sroot 
250*1459Sroot wordvprint(fyle, wordc, wordv)
251*1459Sroot 	FILE	*fyle;
252*1459Sroot 	int	wordc;
253*1459Sroot 	char	*wordv[];
254*1459Sroot {
255*1459Sroot 	int	i;
256*1459Sroot 	for(i = 0; i < wordc; i++){
257*1459Sroot 		fprintf(fyle, "%s",wordv[i]);
258*1459Sroot 		if (i != wordc - 1)
259*1459Sroot 			fprintf(fyle, " ");
260*1459Sroot 	}
261*1459Sroot }
262*1459Sroot 
263*1459Sroot /*
264*1459Sroot  *	Given a string, parse it into a number of words, and build
265*1459Sroot  *	a wordc wordv combination pointing into it.
266*1459Sroot  */
267*1459Sroot wordvbuild(string, r_wordc, r_wordv)
268*1459Sroot 	char	*string;
269*1459Sroot 	int	*r_wordc;
270*1459Sroot 	char	***r_wordv;
271*1459Sroot {
272*1459Sroot 	register	char 	*cp;
273*1459Sroot 			char	*saltedbuffer;
274*1459Sroot 			char	**wordv;
275*1459Sroot 			int	wordcount;
276*1459Sroot 			int	wordindex;
277*1459Sroot 
278*1459Sroot 	saltedbuffer = strsave(string);
279*1459Sroot 	for (wordcount = 0, cp = saltedbuffer; *cp; wordcount++){
280*1459Sroot 		while (*cp  && isspace(*cp))
281*1459Sroot 			cp++;
282*1459Sroot 		if (*cp == 0)
283*1459Sroot 			break;
284*1459Sroot 		while (!isspace(*cp))
285*1459Sroot 			cp++;
286*1459Sroot 	}
287*1459Sroot 	wordv = (char **)Calloc(wordcount + 1, sizeof (char *));
288*1459Sroot 	for (cp=saltedbuffer,wordindex=0; wordcount; wordindex++,--wordcount){
289*1459Sroot 		while (*cp && isspace(*cp))
290*1459Sroot 			cp++;
291*1459Sroot 		if (*cp == 0)
292*1459Sroot 			break;
293*1459Sroot 		wordv[wordindex] = cp;
294*1459Sroot 		while(!isspace(*cp))
295*1459Sroot 			cp++;
296*1459Sroot 		*cp++ = '\0';
297*1459Sroot 	}
298*1459Sroot 	if (wordcount != 0)
299*1459Sroot 		error("Initial miscount of the number of words in a line\n");
300*1459Sroot 	wordv[wordindex] = (char *)0;
301*1459Sroot #ifdef FULLDEBUG
302*1459Sroot 	for (wordcount = 0; wordcount < wordindex; wordcount++)
303*1459Sroot 		printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]);
304*1459Sroot 	printf("\n");
305*1459Sroot #endif
306*1459Sroot 	*r_wordc = wordindex;
307*1459Sroot 	*r_wordv = wordv;
308*1459Sroot }
309*1459Sroot /*
310*1459Sroot  *	Compare two 0 based wordvectors
311*1459Sroot  */
312*1459Sroot int wordvcmp(wordv1, wordc, wordv2)
313*1459Sroot 	char	**wordv1;
314*1459Sroot 	int	wordc;
315*1459Sroot 	char	**wordv2;
316*1459Sroot {
317*1459Sroot 	register	int i;
318*1459Sroot 			int	back;
319*1459Sroot 	for (i = 0; i < wordc; i++){
320*1459Sroot 		if (back = strcmp(wordv1[i], wordv2[i])){
321*1459Sroot 			return(back);
322*1459Sroot 		}
323*1459Sroot 	}
324*1459Sroot 	return(0);	/* they are equal */
325*1459Sroot }
326*1459Sroot 
327*1459Sroot /*
328*1459Sroot  *	splice a 0 basedword vector onto the tail of a
329*1459Sroot  *	new wordv, allowing the first emptyhead slots to be empty
330*1459Sroot  */
331*1459Sroot char	**wordvsplice(emptyhead, wordc, wordv)
332*1459Sroot 	int	emptyhead;
333*1459Sroot 	int	wordc;
334*1459Sroot 	char	**wordv;
335*1459Sroot {
336*1459Sroot 	register	char	**nwordv;
337*1459Sroot 	int	nwordc = emptyhead + wordc;
338*1459Sroot 	register	int	i;
339*1459Sroot 
340*1459Sroot 	nwordv = (char **)Calloc(nwordc, sizeof (char *));
341*1459Sroot 	for (i = 0; i < emptyhead; i++)
342*1459Sroot 		nwordv[i] = 0;
343*1459Sroot 	for(i = emptyhead; i < nwordc; i++){
344*1459Sroot 		nwordv[i] = wordv[i-emptyhead];
345*1459Sroot 	}
346*1459Sroot 	return(nwordv);
347*1459Sroot }
348