Home | History | Annotate | Line # | Download | only in error
subr.c revision 1.19
      1  1.19  dholland /*	$NetBSD: subr.c,v 1.19 2009/08/13 06:59:37 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.19  dholland __RCSID("$NetBSD: subr.c,v 1.19 2009/08/13 06:59:37 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.16  dholland  * 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.16  dholland 	Eptr errorp;
     54  1.16  dholland 	Eptr *array;
     55  1.16  dholland 	int listlength;
     56  1.16  dholland 	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.18  dholland 	array = Calloc(listlength+1, sizeof (Eptr));
     62  1.16  dholland 	for (listindex = 0, errorp = header;
     63   1.1       cgd 	    listindex < listlength;
     64  1.16  dholland 	    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.18  dholland void *
     74  1.18  dholland Calloc(size_t nelements, size_t size)
     75   1.1       cgd {
     76  1.18  dholland 	void *back;
     77  1.16  dholland 
     78  1.18  dholland 	back = calloc(nelements, size);
     79  1.18  dholland 	if (back == NULL)
     80   1.5     lukem 		errx(1, "Ran out of memory.");
     81  1.16  dholland 	return (back);
     82   1.1       cgd }
     83   1.1       cgd 
     84  1.19  dholland char *
     85  1.19  dholland Strdup(const char *s)
     86  1.19  dholland {
     87  1.19  dholland 	char *ret;
     88  1.19  dholland 
     89  1.19  dholland 	ret = strdup(s);
     90  1.19  dholland 	if (ret == NULL) {
     91  1.19  dholland 		errx(1, "Ran out of memory.");
     92  1.19  dholland 	}
     93  1.19  dholland 	return ret;
     94  1.19  dholland }
     95  1.19  dholland 
     96   1.1       cgd /*
     97  1.16  dholland  * find the position of a given character in a string
     98  1.16  dholland  * (one based)
     99   1.1       cgd  */
    100   1.5     lukem int
    101  1.17  dholland position(const char *string, char ch)
    102   1.1       cgd {
    103  1.16  dholland 	int i;
    104  1.16  dholland 
    105   1.1       cgd 	if (string)
    106  1.16  dholland 		for (i=1; *string; string++, i++) {
    107  1.16  dholland 			if (*string == ch)
    108  1.16  dholland 				return (i);
    109  1.16  dholland 		}
    110  1.16  dholland 	return (-1);
    111   1.1       cgd }
    112   1.5     lukem 
    113   1.1       cgd /*
    114  1.16  dholland  * clobber the first occurance of ch in string by the new character
    115   1.1       cgd  */
    116   1.5     lukem char *
    117  1.11       wiz substitute(char *string, char chold, char chnew)
    118   1.1       cgd {
    119  1.16  dholland 	char *cp = string;
    120   1.1       cgd 
    121   1.1       cgd 	if (cp)
    122  1.16  dholland 		while (*cp) {
    123  1.16  dholland 			if (*cp == chold) {
    124  1.16  dholland 				*cp = chnew;
    125  1.16  dholland 				break;
    126  1.16  dholland 			}
    127  1.16  dholland 			cp++;
    128   1.1       cgd 		}
    129  1.16  dholland 	return (string);
    130   1.1       cgd }
    131   1.1       cgd 
    132   1.5     lukem char
    133  1.17  dholland lastchar(const char *string)
    134   1.1       cgd {
    135  1.16  dholland 	int length;
    136  1.16  dholland 
    137  1.16  dholland 	if (string == NULL)
    138  1.16  dholland 		return ('\0');
    139   1.1       cgd 	length = strlen(string);
    140   1.1       cgd 	if (length >= 1)
    141  1.16  dholland 		return (string[length-1]);
    142   1.1       cgd 	else
    143  1.16  dholland 		return ('\0');
    144   1.1       cgd }
    145   1.1       cgd 
    146   1.5     lukem char
    147  1.17  dholland firstchar(const char *string)
    148   1.1       cgd {
    149   1.1       cgd 	if (string)
    150  1.16  dholland 		return (string[0]);
    151   1.1       cgd 	else
    152  1.16  dholland 		return ('\0');
    153   1.1       cgd }
    154   1.1       cgd 
    155   1.5     lukem char
    156  1.17  dholland next_lastchar(const char *string)
    157   1.1       cgd {
    158  1.16  dholland 	int length;
    159  1.16  dholland 
    160  1.16  dholland 	if (string == NULL)
    161  1.16  dholland 		return ('\0');
    162   1.1       cgd 	length = strlen(string);
    163   1.1       cgd 	if (length >= 2)
    164  1.16  dholland 		return (string[length - 2]);
    165   1.1       cgd 	else
    166  1.16  dholland 		return ('\0');
    167   1.1       cgd }
    168   1.1       cgd 
    169   1.5     lukem void
    170  1.11       wiz clob_last(char *string, char newstuff)
    171   1.1       cgd {
    172  1.16  dholland 	int length = 0;
    173  1.16  dholland 
    174   1.1       cgd 	if (string)
    175   1.1       cgd 		length = strlen(string);
    176   1.1       cgd 	if (length >= 1)
    177   1.1       cgd 		string[length - 1] = newstuff;
    178   1.1       cgd }
    179   1.1       cgd 
    180   1.1       cgd /*
    181  1.16  dholland  * parse a string that is the result of a format %s(%d)
    182  1.16  dholland  * return TRUE if this is of the proper format
    183   1.1       cgd  */
    184  1.18  dholland bool
    185  1.11       wiz persperdexplode(char *string, char **r_perd, char **r_pers)
    186   1.1       cgd {
    187  1.16  dholland 	char *cp;
    188  1.16  dholland 	int length = 0;
    189   1.1       cgd 
    190   1.1       cgd 	if (string)
    191   1.1       cgd 		length = strlen(string);
    192  1.18  dholland 	if (length >= 4 && string[length - 1] == ')') {
    193   1.1       cgd 		for (cp = &string[length - 2];
    194  1.18  dholland 		     isdigit((unsigned char)*cp) && *cp != '(';
    195   1.1       cgd 		     --cp)
    196   1.1       cgd 			continue;
    197  1.16  dholland 		if (*cp == '(') {
    198   1.1       cgd 			string[length - 1] = '\0';	/* clobber the ) */
    199  1.13    itojun 			*r_perd = strdup(cp+1);
    200   1.1       cgd 			string[length - 1] = ')';
    201   1.1       cgd 			*cp = '\0';			/* clobber the ( */
    202  1.13    itojun 			*r_pers = strdup(string);
    203   1.1       cgd 			*cp = '(';
    204  1.18  dholland 			return true;
    205  1.16  dholland 		}
    206   1.1       cgd 	}
    207  1.18  dholland 	return false;
    208   1.1       cgd }
    209   1.5     lukem 
    210  1.15  dholland #if 0 /* unused */
    211   1.1       cgd /*
    212  1.16  dholland  * parse a quoted string that is the result of a format \"%s\"(%d)
    213  1.16  dholland  * return TRUE if this is of the proper format
    214   1.1       cgd  */
    215  1.15  dholland static boolean
    216  1.11       wiz qpersperdexplode(char *string, char **r_perd, char **r_pers)
    217   1.1       cgd {
    218  1.16  dholland 	char *cp;
    219  1.16  dholland 	int length = 0;
    220   1.1       cgd 
    221   1.1       cgd 	if (string)
    222   1.1       cgd 		length = strlen(string);
    223  1.18  dholland 	if (length >= 4 && string[length - 1] == ')') {
    224   1.1       cgd 		for (cp = &string[length - 2];
    225  1.18  dholland 		     isdigit((unsigned char)*cp) && *cp != '(';
    226   1.1       cgd 		     --cp)
    227   1.1       cgd 			continue;
    228  1.16  dholland 		if (*cp == '(' && *(cp - 1) == '"') {
    229   1.1       cgd 			string[length - 1] = '\0';
    230  1.13    itojun 			*r_perd = strdup(cp+1);
    231   1.1       cgd 			string[length - 1] = ')';
    232   1.1       cgd 			*(cp - 1) = '\0';		/* clobber the " */
    233  1.13    itojun 			*r_pers = strdup(string + 1);
    234   1.1       cgd 			*(cp - 1) = '"';
    235  1.18  dholland 			return true;
    236  1.16  dholland 		}
    237   1.1       cgd 	}
    238  1.18  dholland 	return false;
    239   1.1       cgd }
    240  1.15  dholland #endif /* 0 - unused */
    241   1.1       cgd 
    242  1.16  dholland static char cincomment[] = CINCOMMENT;
    243  1.16  dholland static char coutcomment[] = COUTCOMMENT;
    244  1.16  dholland static char fincomment[] = FINCOMMENT;
    245  1.16  dholland static char foutcomment[] = FOUTCOMMENT;
    246  1.16  dholland static char newline[] = NEWLINE;
    247  1.16  dholland static char piincomment[] = PIINCOMMENT;
    248  1.16  dholland static char pioutcomment[] = PIOUTCOMMENT;
    249  1.16  dholland static char lispincomment[] = LISPINCOMMENT;
    250  1.16  dholland static char riincomment[] = RIINCOMMENT;
    251  1.16  dholland static char rioutcomment[] = RIOUTCOMMENT;
    252  1.16  dholland static char troffincomment[] = TROFFINCOMMENT;
    253  1.16  dholland static char troffoutcomment[] = TROFFOUTCOMMENT;
    254  1.16  dholland static char mod2incomment[] = MOD2INCOMMENT;
    255  1.16  dholland static char mod2outcomment[] = MOD2OUTCOMMENT;
    256  1.16  dholland 
    257  1.16  dholland struct lang_desc lang_table[] = {
    258  1.16  dholland 	{ /*INUNKNOWN	0*/	"unknown", cincomment,     coutcomment },
    259  1.16  dholland 	{ /*INCPP	1*/	"cpp",     cincomment,     coutcomment },
    260  1.16  dholland 	{ /*INCC	2*/	"cc",      cincomment,     coutcomment },
    261  1.16  dholland 	{ /*INAS	3*/	"as",      ASINCOMMENT,    newline },
    262  1.16  dholland 	{ /*INLD	4*/	"ld",      cincomment,     coutcomment },
    263  1.16  dholland 	{ /*INLINT	5*/	"lint",    cincomment,     coutcomment },
    264  1.16  dholland 	{ /*INF77	6*/	"f77",     fincomment,     foutcomment },
    265  1.16  dholland 	{ /*INPI	7*/	"pi",      piincomment,    pioutcomment },
    266  1.16  dholland 	{ /*INPC	8*/	"pc",      piincomment,    pioutcomment },
    267  1.16  dholland 	{ /*INFRANZ	9*/	"franz",   lispincomment,  newline },
    268  1.16  dholland 	{ /*INLISP	10*/	"lisp",    lispincomment,  newline },
    269  1.16  dholland 	{ /*INVAXIMA	11*/	"vaxima",  lispincomment,  newline },
    270  1.16  dholland 	{ /*INRATFOR	12*/	"ratfor",  fincomment,     foutcomment },
    271  1.16  dholland 	{ /*INLEX	13*/	"lex",     cincomment,     coutcomment },
    272  1.16  dholland 	{ /*INYACC	14*/	"yacc",    cincomment,     coutcomment },
    273  1.16  dholland 	{ /*INAPL	15*/	"apl",     ".lm",          newline },
    274  1.16  dholland 	{ /*INMAKE	16*/	"make",    ASINCOMMENT,    newline },
    275  1.16  dholland 	{ /*INRI	17*/	"ri",      riincomment,    rioutcomment },
    276  1.16  dholland 	{ /*INTROFF	18*/	"troff",   troffincomment, troffoutcomment },
    277  1.16  dholland 	{ /*INMOD2	19*/	"mod2",    mod2incomment,  mod2outcomment },
    278  1.16  dholland 	{			0,         0,              0 }
    279   1.1       cgd };
    280   1.1       cgd 
    281   1.5     lukem void
    282  1.18  dholland printerrors(bool look_at_subclass, int errorc, Eptr errorv[])
    283   1.1       cgd {
    284  1.16  dholland 	int i;
    285  1.16  dholland 	Eptr errorp;
    286   1.1       cgd 
    287  1.16  dholland 	for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]) {
    288   1.1       cgd 		if (errorp->error_e_class == C_IGNORE)
    289   1.1       cgd 			continue;
    290   1.1       cgd 		if (look_at_subclass && errorp->error_s_class == C_DUPL)
    291   1.1       cgd 			continue;
    292   1.1       cgd 		printf("Error %d, (%s error) [%s], text = \"",
    293   1.1       cgd 			i,
    294   1.1       cgd 			class_table[errorp->error_e_class],
    295   1.1       cgd 			lang_table[errorp->error_language].lang_name);
    296   1.1       cgd 		wordvprint(stdout,errorp->error_lgtext,errorp->error_text);
    297   1.1       cgd 		printf("\"\n");
    298   1.1       cgd 	}
    299   1.1       cgd }
    300   1.1       cgd 
    301   1.5     lukem void
    302  1.11       wiz wordvprint(FILE *fyle, int wordc, char **wordv)
    303   1.1       cgd {
    304  1.16  dholland 	int i;
    305  1.17  dholland 	const char *sep = "";
    306   1.1       cgd 
    307  1.16  dholland 	for (i = 0; i < wordc; i++)
    308   1.1       cgd 		if (wordv[i]) {
    309   1.1       cgd 			fprintf(fyle, "%s%s",sep,wordv[i]);
    310   1.1       cgd 			sep = " ";
    311   1.1       cgd 		}
    312   1.1       cgd }
    313   1.1       cgd 
    314   1.1       cgd /*
    315  1.16  dholland  * Given a string, parse it into a number of words, and build
    316  1.16  dholland  * a wordc wordv combination pointing into it.
    317   1.1       cgd  */
    318   1.5     lukem void
    319  1.11       wiz wordvbuild(char *string, int *r_wordc, char ***r_wordv)
    320   1.1       cgd {
    321  1.16  dholland 	char *cp;
    322  1.16  dholland 	char **wordv;
    323  1.16  dholland 	int wordcount;
    324  1.16  dholland 	int wordindex;
    325   1.1       cgd 
    326  1.16  dholland 	for (wordcount = 0, cp = string; *cp; wordcount++) {
    327  1.16  dholland 		while (*cp && isspace((unsigned char)*cp))
    328   1.1       cgd 			cp++;
    329   1.8  christos 		if (*cp == '\0')
    330   1.1       cgd 			break;
    331   1.7  sommerfe 		while (*cp && !isspace((unsigned char)*cp))
    332   1.1       cgd 			cp++;
    333   1.1       cgd 	}
    334  1.18  dholland 	wordv = Calloc(wordcount + 1, sizeof (char *));
    335  1.16  dholland 	for (cp=string, wordindex=0; wordcount; wordindex++, --wordcount) {
    336   1.6  christos 		while (*cp && isspace((unsigned char)*cp))
    337   1.1       cgd 			cp++;
    338   1.8  christos 		if (*cp == '\0')
    339   1.1       cgd 			break;
    340   1.1       cgd 		wordv[wordindex] = cp;
    341  1.16  dholland 		while (*cp && !isspace((unsigned char)*cp))
    342   1.1       cgd 			cp++;
    343   1.1       cgd 		*cp++ = '\0';
    344   1.1       cgd 	}
    345   1.1       cgd 	if (wordcount != 0)
    346   1.5     lukem 		errx(6, "Initial miscount of the number of words in a line");
    347   1.8  christos 	wordv[wordindex] = NULL;
    348   1.1       cgd #ifdef FULLDEBUG
    349   1.1       cgd 	for (wordcount = 0; wordcount < wordindex; wordcount++)
    350   1.1       cgd 		printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]);
    351   1.1       cgd 	printf("\n");
    352   1.1       cgd #endif
    353   1.1       cgd 	*r_wordc = wordindex;
    354   1.1       cgd 	*r_wordv = wordv;
    355   1.1       cgd }
    356   1.5     lukem 
    357   1.1       cgd /*
    358  1.16  dholland  * Compare two 0 based wordvectors
    359   1.1       cgd  */
    360   1.5     lukem int
    361  1.11       wiz wordvcmp(char **wordv1, int wordc, char **wordv2)
    362   1.1       cgd {
    363   1.5     lukem 	int i;
    364  1.16  dholland 	int back;
    365   1.5     lukem 
    366  1.16  dholland 	for (i = 0; i < wordc; i++) {
    367   1.8  christos 		if (wordv1[i] == NULL || wordv2[i] == NULL)
    368  1.16  dholland 			return (-1);
    369  1.10  christos 		if ((back = strcmp(wordv1[i], wordv2[i])) != 0)
    370  1.16  dholland 			return (back);
    371   1.1       cgd 	}
    372  1.16  dholland 	return (0);	/* they are equal */
    373   1.1       cgd }
    374  1.16  dholland 
    375   1.1       cgd /*
    376  1.16  dholland  * splice a 0 basedword vector onto the tail of a
    377  1.16  dholland  * new wordv, allowing the first emptyhead slots to be empty
    378   1.1       cgd  */
    379  1.16  dholland char **
    380  1.11       wiz wordvsplice(int emptyhead, int wordc, char **wordv)
    381   1.1       cgd {
    382  1.16  dholland 	char **nwordv;
    383  1.16  dholland 	int nwordc = emptyhead + wordc;
    384  1.16  dholland 	int i;
    385   1.1       cgd 
    386  1.18  dholland 	nwordv = Calloc(nwordc, sizeof (char *));
    387   1.1       cgd 	for (i = 0; i < emptyhead; i++)
    388   1.8  christos 		nwordv[i] = NULL;
    389  1.16  dholland 	for (i = emptyhead; i < nwordc; i++) {
    390   1.1       cgd 		nwordv[i] = wordv[i-emptyhead];
    391   1.1       cgd 	}
    392  1.16  dholland 	return (nwordv);
    393   1.1       cgd }
    394   1.5     lukem 
    395   1.1       cgd /*
    396  1.18  dholland  * plural and verb forms
    397   1.1       cgd  */
    398  1.17  dholland static const char *S = "s";
    399  1.17  dholland static const char *N = "";
    400   1.5     lukem 
    401  1.17  dholland const char *
    402  1.11       wiz plural(int n)
    403   1.1       cgd {
    404  1.16  dholland 	return (n > 1 ? S : N);
    405   1.1       cgd }
    406   1.5     lukem 
    407  1.17  dholland const char *
    408  1.11       wiz verbform(int n)
    409   1.1       cgd {
    410  1.16  dholland 	return (n > 1 ? N : S);
    411   1.1       cgd }
    412