xref: /csrg-svn/usr.bin/error/subr.c (revision 42683)
121638Sdist /*
221638Sdist  * Copyright (c) 1980 Regents of the University of California.
334664Sbostic  * All rights reserved.
434664Sbostic  *
5*42683Sbostic  * %sccs.include.redist.c%
621638Sdist  */
721638Sdist 
821638Sdist #ifndef lint
9*42683Sbostic static char sccsid[] = "@(#)subr.c	5.4 (Berkeley) 06/01/90";
1034664Sbostic #endif /* not lint */
1121638Sdist 
121459Sroot #include <stdio.h>
131459Sroot #include <ctype.h>
141459Sroot #include "error.h"
151459Sroot /*
165594Srrh  *	Arrayify a list of rules
171459Sroot  */
181459Sroot arrayify(e_length, e_array, header)
195594Srrh 	int	*e_length;
205594Srrh 	Eptr	**e_array;
215594Srrh 	Eptr	header;
221459Sroot {
235594Srrh 	reg	Eptr	errorp;
245594Srrh 	reg	Eptr	*array;
255594Srrh 	reg	int	listlength;
265594Srrh 	reg	int	listindex;
271459Sroot 
281459Sroot 	for (errorp = header, listlength = 0;
291459Sroot 	     errorp; errorp = errorp->error_next, listlength++)
301459Sroot 		continue;
315594Srrh 	array = (Eptr*)Calloc(listlength+1, sizeof (Eptr));
321459Sroot 	for(listindex = 0, errorp = header;
331459Sroot 	    listindex < listlength;
341459Sroot 	    listindex++, errorp = errorp->error_next){
351459Sroot 		array[listindex] = errorp;
361459Sroot 		errorp->error_position = listindex;
371459Sroot 	}
385594Srrh 	array[listindex] = (Eptr)0;
391459Sroot 	*e_length = listlength;
401459Sroot 	*e_array = array;
411459Sroot }
421459Sroot 
431459Sroot /*VARARGS1*/
441459Sroot error(msg, a1, a2, a3)
451459Sroot 	char	*msg;
461459Sroot {
471459Sroot 	fprintf(stderr, "Error: ");
481459Sroot 	fprintf(stderr, msg, a1, a2, a3);
491459Sroot 	fprintf(stderr, "\n");
501459Sroot 	fflush(stdout);
511459Sroot 	fflush(stderr);
521459Sroot 	exit(6);
531459Sroot }
541459Sroot /*ARGSUSED*/
551459Sroot char *Calloc(nelements, size)
561459Sroot 	int	nelements;
571459Sroot 	int	size;
581459Sroot {
591459Sroot 	char	*back;
601459Sroot 	if ( (back = (char *)calloc(nelements, size)) == (char *)NULL){
611459Sroot 		error("Ran out of memory.\n");
621459Sroot 		exit(1);
631459Sroot 	}
641459Sroot 	return(back);
651459Sroot }
661459Sroot 
671459Sroot char *strsave(instring)
681459Sroot 	char	*instring;
691459Sroot {
701459Sroot 	char	*outstring;
715594Srrh 	(void)strcpy(outstring = (char *)Calloc(1, strlen(instring) + 1),
725594Srrh 		instring);
731459Sroot 	return(outstring);
741459Sroot }
751459Sroot /*
761459Sroot  *	find the position of a given character in a string
771459Sroot  *		(one based)
781459Sroot  */
791459Sroot int position(string, ch)
805594Srrh 	reg	char	*string;
815594Srrh 	reg	char	ch;
821459Sroot {
835594Srrh 	reg	int	i;
8410830Ssam 	if (string)
851459Sroot 	for (i=1; *string; string++, i++){
861459Sroot 		if (*string == ch)
871459Sroot 			return(i);
881459Sroot 	}
891459Sroot 	return(-1);
901459Sroot }
911459Sroot /*
921459Sroot  *	clobber the first occurance of ch in string by the new character
931459Sroot  */
941459Sroot char *substitute(string, chold, chnew)
951459Sroot 	char	*string;
961459Sroot 	char	chold, chnew;
971459Sroot {
985594Srrh 	reg	char	*cp = string;
991459Sroot 
10010830Ssam 	if (cp)
1011459Sroot 	while (*cp){
1021459Sroot 		if (*cp == chold){
1031459Sroot 			*cp = chnew;
1041459Sroot 			break;
1051459Sroot 		}
1061459Sroot 		cp++;
1071459Sroot 	}
1081459Sroot 	return(string);
1091459Sroot }
1101459Sroot 
1111459Sroot char lastchar(string)
1121459Sroot 	char	*string;
1131459Sroot {
1141459Sroot 	int	length;
11510830Ssam 	if (string == 0) return('\0');
1161459Sroot 	length = strlen(string);
1171459Sroot 	if (length >= 1)
1181459Sroot 		return(string[length-1]);
1191459Sroot 	else
1201459Sroot 		return('\0');
1211459Sroot }
1221459Sroot 
1231459Sroot char firstchar(string)
1241459Sroot 	char	*string;
1251459Sroot {
12610830Ssam 	if (string)
12710830Ssam 		return(string[0]);
12810830Ssam 	else
12910830Ssam 		return('\0');
1301459Sroot }
1311459Sroot 
1321459Sroot char	next_lastchar(string)
1331459Sroot 	char	*string;
1341459Sroot {
1351459Sroot 	int	length;
13610830Ssam 	if (string == 0) return('\0');
1371459Sroot 	length = strlen(string);
1381459Sroot 	if (length >= 2)
1391459Sroot 		return(string[length - 2]);
1401459Sroot 	else
1411459Sroot 		return('\0');
1421459Sroot }
1431459Sroot 
1441459Sroot clob_last(string, newstuff)
1451459Sroot 	char	*string, newstuff;
1461459Sroot {
14710830Ssam 	int	length = 0;
14810830Ssam 	if (string)
14910830Ssam 		length = strlen(string);
1501459Sroot 	if (length >= 1)
1511459Sroot 		string[length - 1] = newstuff;
1521459Sroot }
1531459Sroot 
1541459Sroot /*
1551459Sroot  *	parse a string that is the result of a format %s(%d)
1561459Sroot  *	return TRUE if this is of the proper format
1571459Sroot  */
1581459Sroot boolean persperdexplode(string, r_perd, r_pers)
1591459Sroot 	char	*string;
1601459Sroot 	char	**r_perd, **r_pers;
1611459Sroot {
1625594Srrh 	reg	char	*cp;
16310830Ssam 		int	length = 0;
1641459Sroot 
16510830Ssam 	if (string)
16610830Ssam 		length = strlen(string);
1671459Sroot 	if (   (length >= 4)
1681459Sroot 	    && (string[length - 1] == ')' ) ){
1691459Sroot 		for (cp = &string[length - 2];
1701459Sroot 		     (isdigit(*cp)) && (*cp != '(');
1711459Sroot 		     --cp)
1721459Sroot 			continue;
1731459Sroot 		if (*cp == '('){
1741459Sroot 			string[length - 1] = '\0';	/* clobber the ) */
1751459Sroot 			*r_perd = strsave(cp+1);
1761459Sroot 			string[length - 1] = ')';
1771459Sroot 			*cp = '\0';			/* clobber the ( */
1781459Sroot 			*r_pers = strsave(string);
1791459Sroot 			*cp = '(';
1801459Sroot 			return(TRUE);
1811459Sroot 		}
1821459Sroot 	}
1831459Sroot 	return(FALSE);
1841459Sroot }
1851459Sroot /*
1861459Sroot  *	parse a quoted string that is the result of a format \"%s\"(%d)
1871459Sroot  *	return TRUE if this is of the proper format
1881459Sroot  */
1891459Sroot boolean qpersperdexplode(string, r_perd, r_pers)
1901459Sroot 	char	*string;
1911459Sroot 	char	**r_perd, **r_pers;
1921459Sroot {
1935594Srrh 	reg	char	*cp;
19410830Ssam 		int	length = 0;
1951459Sroot 
19610830Ssam 	if (string)
19710830Ssam 		length = strlen(string);
1981459Sroot 	if (   (length >= 4)
1991459Sroot 	    && (string[length - 1] == ')' ) ){
2001459Sroot 		for (cp = &string[length - 2];
2011459Sroot 		     (isdigit(*cp)) && (*cp != '(');
2021459Sroot 		     --cp)
2031459Sroot 			continue;
2041459Sroot 		if (*cp == '(' && *(cp - 1) == '"'){
2051459Sroot 			string[length - 1] = '\0';
2061459Sroot 			*r_perd = strsave(cp+1);
2071459Sroot 			string[length - 1] = ')';
2081459Sroot 			*(cp - 1) = '\0';		/* clobber the " */
2091459Sroot 			*r_pers = strsave(string + 1);
2101459Sroot 			*(cp - 1) = '"';
2111459Sroot 			return(TRUE);
2121459Sroot 		}
2131459Sroot 	}
2141459Sroot 	return(FALSE);
2151459Sroot }
2161459Sroot 
2171459Sroot static	char	cincomment[] = CINCOMMENT;
2181459Sroot static	char	coutcomment[] = COUTCOMMENT;
2191459Sroot static	char	fincomment[] = FINCOMMENT;
2201459Sroot static	char	foutcomment[] = FOUTCOMMENT;
2211459Sroot static	char	newline[] = NEWLINE;
2221459Sroot static	char	piincomment[] = PIINCOMMENT;
2231459Sroot static	char	pioutcomment[] = PIOUTCOMMENT;
2241459Sroot static	char	lispincomment[] = LISPINCOMMENT;
2251459Sroot static	char	riincomment[] = RIINCOMMENT;
2261459Sroot static	char	rioutcomment[] = RIOUTCOMMENT;
22713106Srrh static	char	troffincomment[] = TROFFINCOMMENT;
22813106Srrh static	char	troffoutcomment[] = TROFFOUTCOMMENT;
22917499Srrh static	char	mod2incomment[] = MOD2INCOMMENT;
23017499Srrh static	char	mod2outcomment[] = MOD2OUTCOMMENT;
2311459Sroot 
2321459Sroot struct	lang_desc lang_table[] = {
2331459Sroot 	/*INUNKNOWN	0*/	"unknown", cincomment,	coutcomment,
2341459Sroot 	/*INCPP		1*/	"cpp",	cincomment,    coutcomment,
2351459Sroot 	/*INCC		2*/	"cc",	cincomment,    coutcomment,
2361459Sroot 	/*INAS		3*/	"as",	ASINCOMMENT,   newline,
2371459Sroot 	/*INLD		4*/	"ld",	cincomment,    coutcomment,
2381459Sroot 	/*INLINT	5*/	"lint",	cincomment,    coutcomment,
2391459Sroot 	/*INF77		6*/	"f77",	fincomment,    foutcomment,
2401459Sroot 	/*INPI		7*/	"pi",	piincomment,   pioutcomment,
2411459Sroot 	/*INPC		8*/	"pc",	piincomment,   pioutcomment,
2421459Sroot 	/*INFRANZ	9*/	"franz",lispincomment, newline,
2431459Sroot 	/*INLISP	10*/	"lisp",	lispincomment, newline,
2441459Sroot 	/*INVAXIMA	11*/	"vaxima",lispincomment,newline,
2451459Sroot 	/*INRATFOR	12*/	"ratfor",fincomment,   foutcomment,
2461459Sroot 	/*INLEX		13*/	"lex",	cincomment,    coutcomment,
2471459Sroot 	/*INYACC	14*/	"yacc",	cincomment,    coutcomment,
2481459Sroot 	/*INAPL		15*/	"apl",	".lm",	       newline,
2491459Sroot 	/*INMAKE	16*/	"make",	ASINCOMMENT,   newline,
2501459Sroot 	/*INRI		17*/	"ri",	riincomment,   rioutcomment,
25113106Srrh 	/*INTROFF	18*/	"troff",troffincomment,troffoutcomment,
25217499Srrh 	/*INMOD2	19*/	"mod2",	mod2incomment, mod2outcomment,
2531459Sroot 				0,	0,	     0
2541459Sroot };
2551459Sroot 
2561459Sroot printerrors(look_at_subclass, errorc, errorv)
2571459Sroot 	boolean	look_at_subclass;
2581459Sroot 	int	errorc;
2595594Srrh 	Eptr	errorv[];
2601459Sroot {
2615594Srrh 	reg	int	i;
2625594Srrh 	reg	Eptr	errorp;
2635594Srrh 
2641459Sroot 	for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]){
2651459Sroot 		if (errorp->error_e_class == C_IGNORE)
2661459Sroot 			continue;
2671459Sroot 		if (look_at_subclass && errorp->error_s_class == C_DUPL)
2681459Sroot 			continue;
2691459Sroot 		printf("Error %d, (%s error) [%s], text = \"",
2701459Sroot 			i,
2711459Sroot 			class_table[errorp->error_e_class],
2721459Sroot 			lang_table[errorp->error_language].lang_name);
2731459Sroot 		wordvprint(stdout,errorp->error_lgtext,errorp->error_text);
2741459Sroot 		printf("\"\n");
2751459Sroot 	}
2761459Sroot }
2771459Sroot 
2781459Sroot wordvprint(fyle, wordc, wordv)
2791459Sroot 	FILE	*fyle;
2801459Sroot 	int	wordc;
2811459Sroot 	char	*wordv[];
2821459Sroot {
2831459Sroot 	int	i;
28413539Ssam 	char *sep = "";
28513539Ssam 
28613539Ssam 	for(i = 0; i < wordc; i++)
28713539Ssam 		if (wordv[i]) {
28813539Ssam 			fprintf(fyle, "%s%s",sep,wordv[i]);
28913539Ssam 			sep = " ";
29013539Ssam 		}
2911459Sroot }
2921459Sroot 
2931459Sroot /*
2941459Sroot  *	Given a string, parse it into a number of words, and build
2951459Sroot  *	a wordc wordv combination pointing into it.
2961459Sroot  */
2971459Sroot wordvbuild(string, r_wordc, r_wordv)
2981459Sroot 	char	*string;
2991459Sroot 	int	*r_wordc;
3001459Sroot 	char	***r_wordv;
3011459Sroot {
3025594Srrh 	reg	char 	*cp;
3035594Srrh 		char	*saltedbuffer;
3045594Srrh 		char	**wordv;
3055594Srrh 		int	wordcount;
3065594Srrh 		int	wordindex;
3071459Sroot 
3081459Sroot 	saltedbuffer = strsave(string);
3091459Sroot 	for (wordcount = 0, cp = saltedbuffer; *cp; wordcount++){
3101459Sroot 		while (*cp  && isspace(*cp))
3111459Sroot 			cp++;
3121459Sroot 		if (*cp == 0)
3131459Sroot 			break;
3141459Sroot 		while (!isspace(*cp))
3151459Sroot 			cp++;
3161459Sroot 	}
3171459Sroot 	wordv = (char **)Calloc(wordcount + 1, sizeof (char *));
3181459Sroot 	for (cp=saltedbuffer,wordindex=0; wordcount; wordindex++,--wordcount){
3191459Sroot 		while (*cp && isspace(*cp))
3201459Sroot 			cp++;
3211459Sroot 		if (*cp == 0)
3221459Sroot 			break;
3231459Sroot 		wordv[wordindex] = cp;
3241459Sroot 		while(!isspace(*cp))
3251459Sroot 			cp++;
3261459Sroot 		*cp++ = '\0';
3271459Sroot 	}
3281459Sroot 	if (wordcount != 0)
3291459Sroot 		error("Initial miscount of the number of words in a line\n");
3301459Sroot 	wordv[wordindex] = (char *)0;
3311459Sroot #ifdef FULLDEBUG
3321459Sroot 	for (wordcount = 0; wordcount < wordindex; wordcount++)
3331459Sroot 		printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]);
3341459Sroot 	printf("\n");
3351459Sroot #endif
3361459Sroot 	*r_wordc = wordindex;
3371459Sroot 	*r_wordv = wordv;
3381459Sroot }
3391459Sroot /*
3401459Sroot  *	Compare two 0 based wordvectors
3411459Sroot  */
3421459Sroot int wordvcmp(wordv1, wordc, wordv2)
3431459Sroot 	char	**wordv1;
3441459Sroot 	int	wordc;
3451459Sroot 	char	**wordv2;
3461459Sroot {
3475594Srrh 	reg	int i;
3485594Srrh 		int	back;
3491459Sroot 	for (i = 0; i < wordc; i++){
35010830Ssam 		if (wordv1[i] == 0 || wordv2[i] == 0)
35110830Ssam 				return(-1);
3521459Sroot 		if (back = strcmp(wordv1[i], wordv2[i])){
3531459Sroot 			return(back);
3541459Sroot 		}
3551459Sroot 	}
3561459Sroot 	return(0);	/* they are equal */
3571459Sroot }
3581459Sroot 
3591459Sroot /*
3601459Sroot  *	splice a 0 basedword vector onto the tail of a
3611459Sroot  *	new wordv, allowing the first emptyhead slots to be empty
3621459Sroot  */
3631459Sroot char	**wordvsplice(emptyhead, wordc, wordv)
3641459Sroot 	int	emptyhead;
3651459Sroot 	int	wordc;
3661459Sroot 	char	**wordv;
3671459Sroot {
3685594Srrh 	reg	char	**nwordv;
3695594Srrh 		int	nwordc = emptyhead + wordc;
3705594Srrh 	reg	int	i;
3711459Sroot 
3721459Sroot 	nwordv = (char **)Calloc(nwordc, sizeof (char *));
3731459Sroot 	for (i = 0; i < emptyhead; i++)
3741459Sroot 		nwordv[i] = 0;
3751459Sroot 	for(i = emptyhead; i < nwordc; i++){
3761459Sroot 		nwordv[i] = wordv[i-emptyhead];
3771459Sroot 	}
3781459Sroot 	return(nwordv);
3791459Sroot }
3805594Srrh /*
3815594Srrh  *	plural'ize and verb forms
3825594Srrh  */
3835594Srrh static	char	*S = "s";
3845594Srrh static	char	*N = "";
3855594Srrh char *plural(n)
3865594Srrh 	int	n;
3875594Srrh {
3885594Srrh 	return( n > 1 ? S : N);
3895594Srrh }
3905594Srrh char *verbform(n)
3915594Srrh 	int	n;
3925594Srrh {
3935594Srrh 	return( n > 1 ? N : S);
3945594Srrh }
3955594Srrh 
396