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