Home | History | Annotate | Line # | Download | only in error
subr.c revision 1.15
      1  1.15  dholland /*	$NetBSD: subr.c,v 1.15 2009/08/13 02:10:50 dholland Exp $	*/
      2   1.3       jtc 
      3   1.1       cgd /*
      4   1.3       jtc  * Copyright (c) 1980, 1993
      5   1.3       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.14       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.5     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.3       jtc #if 0
     35   1.3       jtc static char sccsid[] = "@(#)subr.c	8.1 (Berkeley) 6/6/93";
     36   1.3       jtc #endif
     37  1.15  dholland __RCSID("$NetBSD: subr.c,v 1.15 2009/08/13 02:10:50 dholland Exp $");
     38   1.1       cgd #endif /* not lint */
     39   1.1       cgd 
     40   1.5     lukem #include <ctype.h>
     41   1.5     lukem #include <err.h>
     42   1.1       cgd #include <stdio.h>
     43   1.1       cgd #include <stdlib.h>
     44   1.1       cgd #include <string.h>
     45   1.1       cgd #include "error.h"
     46  1.15  dholland 
     47   1.1       cgd /*
     48   1.1       cgd  *	Arrayify a list of rules
     49   1.1       cgd  */
     50   1.5     lukem void
     51  1.11       wiz arrayify(int *e_length, Eptr **e_array, Eptr header)
     52   1.1       cgd {
     53   1.5     lukem 	Eptr	errorp;
     54   1.5     lukem 	Eptr	*array;
     55   1.5     lukem 	int	listlength;
     56   1.5     lukem 	int	listindex;
     57   1.1       cgd 
     58   1.1       cgd 	for (errorp = header, listlength = 0;
     59   1.1       cgd 	     errorp; errorp = errorp->error_next, listlength++)
     60   1.1       cgd 		continue;
     61   1.1       cgd 	array = (Eptr*)Calloc(listlength+1, sizeof (Eptr));
     62   1.1       cgd 	for(listindex = 0, errorp = header;
     63   1.1       cgd 	    listindex < listlength;
     64   1.1       cgd 	    listindex++, errorp = errorp->error_next){
     65   1.1       cgd 		array[listindex] = errorp;
     66   1.1       cgd 		errorp->error_position = listindex;
     67   1.1       cgd 	}
     68   1.8  christos 	array[listindex] = NULL;
     69   1.1       cgd 	*e_length = listlength;
     70   1.1       cgd 	*e_array = array;
     71   1.1       cgd }
     72   1.1       cgd 
     73   1.5     lukem char *
     74  1.11       wiz Calloc(int nelements, int size)
     75   1.1       cgd {
     76   1.1       cgd 	char	*back;
     77   1.9       mjl 	if ( (back = (char *)calloc(nelements, size)) == NULL)
     78   1.5     lukem 		errx(1, "Ran out of memory.");
     79   1.1       cgd 	return(back);
     80   1.1       cgd }
     81   1.1       cgd 
     82   1.1       cgd /*
     83   1.1       cgd  *	find the position of a given character in a string
     84   1.1       cgd  *		(one based)
     85   1.1       cgd  */
     86   1.5     lukem int
     87  1.11       wiz position(char *string, char ch)
     88   1.1       cgd {
     89   1.5     lukem 	int	i;
     90   1.1       cgd 	if (string)
     91   1.1       cgd 	for (i=1; *string; string++, i++){
     92   1.1       cgd 		if (*string == ch)
     93   1.1       cgd 			return(i);
     94   1.1       cgd 	}
     95   1.1       cgd 	return(-1);
     96   1.1       cgd }
     97   1.5     lukem 
     98   1.1       cgd /*
     99   1.1       cgd  *	clobber the first occurance of ch in string by the new character
    100   1.1       cgd  */
    101   1.5     lukem char *
    102  1.11       wiz substitute(char *string, char chold, char chnew)
    103   1.1       cgd {
    104   1.5     lukem 	char	*cp = string;
    105   1.1       cgd 
    106   1.1       cgd 	if (cp)
    107   1.1       cgd 	while (*cp){
    108   1.1       cgd 		if (*cp == chold){
    109   1.1       cgd 			*cp = chnew;
    110   1.1       cgd 			break;
    111   1.1       cgd 		}
    112   1.1       cgd 		cp++;
    113   1.1       cgd 	}
    114   1.1       cgd 	return(string);
    115   1.1       cgd }
    116   1.1       cgd 
    117   1.5     lukem char
    118  1.11       wiz lastchar(char *string)
    119   1.1       cgd {
    120   1.1       cgd 	int	length;
    121   1.8  christos 	if (string == NULL) return('\0');
    122   1.1       cgd 	length = strlen(string);
    123   1.1       cgd 	if (length >= 1)
    124   1.1       cgd 		return(string[length-1]);
    125   1.1       cgd 	else
    126   1.1       cgd 		return('\0');
    127   1.1       cgd }
    128   1.1       cgd 
    129   1.5     lukem char
    130  1.11       wiz firstchar(char *string)
    131   1.1       cgd {
    132   1.1       cgd 	if (string)
    133   1.1       cgd 		return(string[0]);
    134   1.1       cgd 	else
    135   1.1       cgd 		return('\0');
    136   1.1       cgd }
    137   1.1       cgd 
    138   1.5     lukem char
    139  1.11       wiz next_lastchar(char *string)
    140   1.1       cgd {
    141   1.1       cgd 	int	length;
    142   1.8  christos 	if (string == NULL) return('\0');
    143   1.1       cgd 	length = strlen(string);
    144   1.1       cgd 	if (length >= 2)
    145   1.1       cgd 		return(string[length - 2]);
    146   1.1       cgd 	else
    147   1.1       cgd 		return('\0');
    148   1.1       cgd }
    149   1.1       cgd 
    150   1.5     lukem void
    151  1.11       wiz clob_last(char *string, char newstuff)
    152   1.1       cgd {
    153   1.1       cgd 	int	length = 0;
    154   1.1       cgd 	if (string)
    155   1.1       cgd 		length = strlen(string);
    156   1.1       cgd 	if (length >= 1)
    157   1.1       cgd 		string[length - 1] = newstuff;
    158   1.1       cgd }
    159   1.1       cgd 
    160   1.1       cgd /*
    161   1.1       cgd  *	parse a string that is the result of a format %s(%d)
    162   1.1       cgd  *	return TRUE if this is of the proper format
    163   1.1       cgd  */
    164   1.5     lukem boolean
    165  1.11       wiz persperdexplode(char *string, char **r_perd, char **r_pers)
    166   1.1       cgd {
    167   1.5     lukem 	char	*cp;
    168   1.5     lukem 	int	length = 0;
    169   1.1       cgd 
    170   1.1       cgd 	if (string)
    171   1.1       cgd 		length = strlen(string);
    172   1.1       cgd 	if (   (length >= 4)
    173   1.1       cgd 	    && (string[length - 1] == ')' ) ){
    174   1.1       cgd 		for (cp = &string[length - 2];
    175   1.6  christos 		     (isdigit((unsigned char)*cp)) && (*cp != '(');
    176   1.1       cgd 		     --cp)
    177   1.1       cgd 			continue;
    178   1.1       cgd 		if (*cp == '('){
    179   1.1       cgd 			string[length - 1] = '\0';	/* clobber the ) */
    180  1.13    itojun 			*r_perd = strdup(cp+1);
    181   1.1       cgd 			string[length - 1] = ')';
    182   1.1       cgd 			*cp = '\0';			/* clobber the ( */
    183  1.13    itojun 			*r_pers = strdup(string);
    184   1.1       cgd 			*cp = '(';
    185   1.1       cgd 			return(TRUE);
    186   1.1       cgd 		}
    187   1.1       cgd 	}
    188   1.1       cgd 	return(FALSE);
    189   1.1       cgd }
    190   1.5     lukem 
    191  1.15  dholland #if 0 /* unused */
    192   1.1       cgd /*
    193   1.1       cgd  *	parse a quoted string that is the result of a format \"%s\"(%d)
    194   1.1       cgd  *	return TRUE if this is of the proper format
    195   1.1       cgd  */
    196  1.15  dholland static boolean
    197  1.11       wiz qpersperdexplode(char *string, char **r_perd, char **r_pers)
    198   1.1       cgd {
    199   1.5     lukem 	char	*cp;
    200   1.5     lukem 	int	length = 0;
    201   1.1       cgd 
    202   1.1       cgd 	if (string)
    203   1.1       cgd 		length = strlen(string);
    204   1.1       cgd 	if (   (length >= 4)
    205   1.1       cgd 	    && (string[length - 1] == ')' ) ){
    206   1.1       cgd 		for (cp = &string[length - 2];
    207   1.6  christos 		     (isdigit((unsigned char)*cp)) && (*cp != '(');
    208   1.1       cgd 		     --cp)
    209   1.1       cgd 			continue;
    210   1.1       cgd 		if (*cp == '(' && *(cp - 1) == '"'){
    211   1.1       cgd 			string[length - 1] = '\0';
    212  1.13    itojun 			*r_perd = strdup(cp+1);
    213   1.1       cgd 			string[length - 1] = ')';
    214   1.1       cgd 			*(cp - 1) = '\0';		/* clobber the " */
    215  1.13    itojun 			*r_pers = strdup(string + 1);
    216   1.1       cgd 			*(cp - 1) = '"';
    217   1.1       cgd 			return(TRUE);
    218   1.1       cgd 		}
    219   1.1       cgd 	}
    220   1.1       cgd 	return(FALSE);
    221   1.1       cgd }
    222  1.15  dholland #endif /* 0 - unused */
    223   1.1       cgd 
    224   1.1       cgd static	char	cincomment[] = CINCOMMENT;
    225   1.1       cgd static	char	coutcomment[] = COUTCOMMENT;
    226   1.1       cgd static	char	fincomment[] = FINCOMMENT;
    227   1.1       cgd static	char	foutcomment[] = FOUTCOMMENT;
    228   1.1       cgd static	char	newline[] = NEWLINE;
    229   1.1       cgd static	char	piincomment[] = PIINCOMMENT;
    230   1.1       cgd static	char	pioutcomment[] = PIOUTCOMMENT;
    231   1.1       cgd static	char	lispincomment[] = LISPINCOMMENT;
    232   1.1       cgd static	char	riincomment[] = RIINCOMMENT;
    233   1.1       cgd static	char	rioutcomment[] = RIOUTCOMMENT;
    234   1.1       cgd static	char	troffincomment[] = TROFFINCOMMENT;
    235   1.1       cgd static	char	troffoutcomment[] = TROFFOUTCOMMENT;
    236   1.1       cgd static	char	mod2incomment[] = MOD2INCOMMENT;
    237   1.1       cgd static	char	mod2outcomment[] = MOD2OUTCOMMENT;
    238   1.1       cgd 
    239   1.1       cgd struct	lang_desc lang_table[] = {
    240   1.5     lukem 	{ /*INUNKNOWN	0*/	"unknown", cincomment,	coutcomment },
    241   1.5     lukem 	{ /*INCPP	1*/	"cpp",	cincomment,    coutcomment },
    242   1.5     lukem 	{ /*INCC	2*/	"cc",	cincomment,    coutcomment },
    243   1.5     lukem 	{ /*INAS	3*/	"as",	ASINCOMMENT,   newline },
    244   1.5     lukem 	{ /*INLD	4*/	"ld",	cincomment,    coutcomment },
    245   1.5     lukem 	{ /*INLINT	5*/	"lint",	cincomment,    coutcomment },
    246   1.5     lukem 	{ /*INF77	6*/	"f77",	fincomment,    foutcomment },
    247   1.5     lukem 	{ /*INPI	7*/	"pi",	piincomment,   pioutcomment },
    248   1.5     lukem 	{ /*INPC	8*/	"pc",	piincomment,   pioutcomment },
    249   1.5     lukem 	{ /*INFRANZ	9*/	"franz",lispincomment, newline },
    250   1.5     lukem 	{ /*INLISP	10*/	"lisp",	lispincomment, newline },
    251   1.5     lukem 	{ /*INVAXIMA	11*/	"vaxima",lispincomment,newline },
    252   1.5     lukem 	{ /*INRATFOR	12*/	"ratfor",fincomment,   foutcomment },
    253   1.5     lukem 	{ /*INLEX	13*/	"lex",	cincomment,    coutcomment },
    254   1.5     lukem 	{ /*INYACC	14*/	"yacc",	cincomment,    coutcomment },
    255   1.5     lukem 	{ /*INAPL	15*/	"apl",	".lm",	       newline },
    256   1.5     lukem 	{ /*INMAKE	16*/	"make",	ASINCOMMENT,   newline },
    257   1.5     lukem 	{ /*INRI	17*/	"ri",	riincomment,   rioutcomment },
    258   1.5     lukem 	{ /*INTROFF	18*/	"troff",troffincomment,troffoutcomment },
    259   1.5     lukem 	{ /*INMOD2	19*/	"mod2",	mod2incomment, mod2outcomment },
    260   1.5     lukem 	{			0,	0,	     0 }
    261   1.1       cgd };
    262   1.1       cgd 
    263   1.5     lukem void
    264  1.11       wiz printerrors(boolean look_at_subclass, int errorc, Eptr errorv[])
    265   1.1       cgd {
    266   1.5     lukem 	int	i;
    267   1.5     lukem 	Eptr	errorp;
    268   1.1       cgd 
    269   1.1       cgd 	for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]){
    270   1.1       cgd 		if (errorp->error_e_class == C_IGNORE)
    271   1.1       cgd 			continue;
    272   1.1       cgd 		if (look_at_subclass && errorp->error_s_class == C_DUPL)
    273   1.1       cgd 			continue;
    274   1.1       cgd 		printf("Error %d, (%s error) [%s], text = \"",
    275   1.1       cgd 			i,
    276   1.1       cgd 			class_table[errorp->error_e_class],
    277   1.1       cgd 			lang_table[errorp->error_language].lang_name);
    278   1.1       cgd 		wordvprint(stdout,errorp->error_lgtext,errorp->error_text);
    279   1.1       cgd 		printf("\"\n");
    280   1.1       cgd 	}
    281   1.1       cgd }
    282   1.1       cgd 
    283   1.5     lukem void
    284  1.11       wiz wordvprint(FILE *fyle, int wordc, char **wordv)
    285   1.1       cgd {
    286   1.1       cgd 	int	i;
    287   1.1       cgd 	char *sep = "";
    288   1.1       cgd 
    289   1.1       cgd 	for(i = 0; i < wordc; i++)
    290   1.1       cgd 		if (wordv[i]) {
    291   1.1       cgd 			fprintf(fyle, "%s%s",sep,wordv[i]);
    292   1.1       cgd 			sep = " ";
    293   1.1       cgd 		}
    294   1.1       cgd }
    295   1.1       cgd 
    296   1.1       cgd /*
    297   1.1       cgd  *	Given a string, parse it into a number of words, and build
    298   1.1       cgd  *	a wordc wordv combination pointing into it.
    299   1.1       cgd  */
    300   1.5     lukem void
    301  1.11       wiz wordvbuild(char *string, int *r_wordc, char ***r_wordv)
    302   1.1       cgd {
    303   1.5     lukem 	char 	*cp;
    304   1.5     lukem 	char	**wordv;
    305   1.5     lukem 	int	wordcount;
    306   1.5     lukem 	int	wordindex;
    307   1.1       cgd 
    308   1.4  christos 	for (wordcount = 0, cp = string; *cp; wordcount++){
    309   1.6  christos 		while (*cp  && isspace((unsigned char)*cp))
    310   1.1       cgd 			cp++;
    311   1.8  christos 		if (*cp == '\0')
    312   1.1       cgd 			break;
    313   1.7  sommerfe 		while (*cp && !isspace((unsigned char)*cp))
    314   1.1       cgd 			cp++;
    315   1.1       cgd 	}
    316   1.1       cgd 	wordv = (char **)Calloc(wordcount + 1, sizeof (char *));
    317   1.4  christos 	for (cp=string,wordindex=0; wordcount; wordindex++,--wordcount){
    318   1.6  christos 		while (*cp && isspace((unsigned char)*cp))
    319   1.1       cgd 			cp++;
    320   1.8  christos 		if (*cp == '\0')
    321   1.1       cgd 			break;
    322   1.1       cgd 		wordv[wordindex] = cp;
    323   1.7  sommerfe 		while(*cp && !isspace((unsigned char)*cp))
    324   1.1       cgd 			cp++;
    325   1.1       cgd 		*cp++ = '\0';
    326   1.1       cgd 	}
    327   1.1       cgd 	if (wordcount != 0)
    328   1.5     lukem 		errx(6, "Initial miscount of the number of words in a line");
    329   1.8  christos 	wordv[wordindex] = NULL;
    330   1.1       cgd #ifdef FULLDEBUG
    331   1.1       cgd 	for (wordcount = 0; wordcount < wordindex; wordcount++)
    332   1.1       cgd 		printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]);
    333   1.1       cgd 	printf("\n");
    334   1.1       cgd #endif
    335   1.1       cgd 	*r_wordc = wordindex;
    336   1.1       cgd 	*r_wordv = wordv;
    337   1.1       cgd }
    338   1.5     lukem 
    339   1.1       cgd /*
    340   1.1       cgd  *	Compare two 0 based wordvectors
    341   1.1       cgd  */
    342   1.5     lukem int
    343  1.11       wiz wordvcmp(char **wordv1, int wordc, char **wordv2)
    344   1.1       cgd {
    345   1.5     lukem 	int i;
    346   1.5     lukem 	int	back;
    347   1.5     lukem 
    348   1.1       cgd 	for (i = 0; i < wordc; i++){
    349   1.8  christos 		if (wordv1[i] == NULL || wordv2[i] == NULL)
    350   1.5     lukem 			return(-1);
    351  1.10  christos 		if ((back = strcmp(wordv1[i], wordv2[i])) != 0)
    352   1.1       cgd 			return(back);
    353   1.1       cgd 	}
    354   1.1       cgd 	return(0);	/* they are equal */
    355   1.1       cgd }
    356   1.1       cgd 
    357   1.1       cgd /*
    358   1.1       cgd  *	splice a 0 basedword vector onto the tail of a
    359   1.1       cgd  *	new wordv, allowing the first emptyhead slots to be empty
    360   1.1       cgd  */
    361   1.5     lukem char	**
    362  1.11       wiz wordvsplice(int emptyhead, int wordc, char **wordv)
    363   1.1       cgd {
    364   1.5     lukem 	char	**nwordv;
    365   1.5     lukem 	int	nwordc = emptyhead + wordc;
    366   1.5     lukem 	int	i;
    367   1.1       cgd 
    368   1.1       cgd 	nwordv = (char **)Calloc(nwordc, sizeof (char *));
    369   1.1       cgd 	for (i = 0; i < emptyhead; i++)
    370   1.8  christos 		nwordv[i] = NULL;
    371   1.1       cgd 	for(i = emptyhead; i < nwordc; i++){
    372   1.1       cgd 		nwordv[i] = wordv[i-emptyhead];
    373   1.1       cgd 	}
    374   1.1       cgd 	return(nwordv);
    375   1.1       cgd }
    376   1.5     lukem 
    377   1.1       cgd /*
    378   1.1       cgd  *	plural'ize and verb forms
    379   1.1       cgd  */
    380   1.1       cgd static	char	*S = "s";
    381   1.1       cgd static	char	*N = "";
    382   1.5     lukem 
    383   1.5     lukem char *
    384  1.11       wiz plural(int n)
    385   1.1       cgd {
    386   1.1       cgd 	return( n > 1 ? S : N);
    387   1.1       cgd }
    388   1.5     lukem 
    389   1.5     lukem char *
    390  1.11       wiz verbform(int n)
    391   1.1       cgd {
    392   1.1       cgd 	return( n > 1 ? N : S);
    393   1.1       cgd }
    394