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