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