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