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