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