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