4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
36 * Arrayify a list of rules
39 arrayify(int *e_length
, Eptr
**e_array
, Eptr header
)
46 for (errorp
= header
, listlength
= 0;
47 errorp
; errorp
= errorp
->error_next
, listlength
++)
49 array
= Calloc(listlength
+1, sizeof (Eptr
));
50 for (listindex
= 0, errorp
= header
;
51 listindex
< listlength
;
52 listindex
++, errorp
= errorp
->error_next
) {
53 array
[listindex
] = errorp
;
54 errorp
->error_position
= listindex
;
56 array
[listindex
] = (Eptr
)0;
57 *e_length
= listlength
;
63 error(char *format
, ...)
67 va_start(args
, format
);
68 (void) fprintf(stderr
, "Error: ");
69 (void) vfprintf(stderr
, format
, args
);
70 (void) fprintf(stderr
, "\n");
71 (void) fflush(stdout
);
72 (void) fflush(stderr
);
78 Calloc(int nelements
, int size
)
81 if ((back
= calloc(nelements
, size
)) == NULL
) {
82 error("Ran out of memory.\n");
89 strsave(char *instring
)
92 (void) strcpy(outstring
= Calloc(1, strlen(instring
) + 1),
97 * find the position of a given character in a string
101 position(char *string
, char ch
)
105 for (i
= 1; *string
; string
++, i
++) {
112 * clobber the first occurance of ch in string by the new character
115 substitute(char *string
, char chold
, char chnew
)
131 lastchar(char *string
)
137 length
= strlen(string
);
139 return (string
[length
-1]);
145 firstchar(char *string
)
154 next_lastchar(char *string
)
160 length
= strlen(string
);
162 return (string
[length
- 2]);
168 clob_last(char *string
, char newstuff
)
172 length
= strlen(string
);
174 string
[length
- 1] = newstuff
;
178 * parse a string that is the result of a format %s(%d)
179 * return TRUE if this is of the proper format
182 persperdexplode(char *string
, char **r_perd
, char **r_pers
)
188 length
= strlen(string
);
189 if ((length
>= 4) && (string
[length
- 1] == ')')) {
190 for (cp
= &string
[length
- 2];
191 (isdigit(*cp
)) && (*cp
!= '('); --cp
)
194 string
[length
- 1] = '\0'; /* clobber the ) */
195 *r_perd
= strsave(cp
+1);
196 string
[length
- 1] = ')';
197 *cp
= '\0'; /* clobber the ( */
198 *r_pers
= strsave(string
);
206 static char cincomment
[] = CINCOMMENT
;
207 static char coutcomment
[] = COUTCOMMENT
;
208 static char fincomment
[] = FINCOMMENT
;
209 static char foutcomment
[] = FOUTCOMMENT
;
210 static char newline
[] = NEWLINE
;
211 static char piincomment
[] = PIINCOMMENT
;
212 static char pioutcomment
[] = PIOUTCOMMENT
;
213 static char lispincomment
[] = LISPINCOMMENT
;
214 static char riincomment
[] = RIINCOMMENT
;
215 static char rioutcomment
[] = RIOUTCOMMENT
;
216 static char troffincomment
[] = TROFFINCOMMENT
;
217 static char troffoutcomment
[] = TROFFOUTCOMMENT
;
218 static char mod2incomment
[] = MOD2INCOMMENT
;
219 static char mod2outcomment
[] = MOD2OUTCOMMENT
;
221 struct lang_desc lang_table
[] = {
222 /* INUNKNOWN 0 */ "unknown", cincomment
, coutcomment
,
223 /* INCPP 1 */ "cpp", cincomment
, coutcomment
,
224 /* INCC 2 */ "cc", cincomment
, coutcomment
,
225 /* INAS 3 */ "as", ASINCOMMENT
, newline
,
226 /* INLD 4 */ "ld", cincomment
, coutcomment
,
227 /* INLINT 5 */ "lint", cincomment
, coutcomment
,
228 /* INF77 6 */ "f77", fincomment
, foutcomment
,
229 /* INPI 7 */ "pi", piincomment
, pioutcomment
,
230 /* INPC 8 */ "pc", piincomment
, pioutcomment
,
231 /* INFRANZ 9 */ "franz", lispincomment
, newline
,
232 /* INLISP 10 */ "lisp", lispincomment
, newline
,
233 /* INVAXIMA 11 */ "vaxima", lispincomment
, newline
,
234 /* INRATFOR 12 */ "ratfor", fincomment
, foutcomment
,
235 /* INLEX 13 */ "lex", cincomment
, coutcomment
,
236 /* INYACC 14 */ "yacc", cincomment
, coutcomment
,
237 /* INAPL 15 */ "apl", ".lm", newline
,
238 /* INMAKE 16 */ "make", ASINCOMMENT
, newline
,
239 /* INRI 17 */ "ri", riincomment
, rioutcomment
,
240 /* INTROFF 18 */ "troff", troffincomment
, troffoutcomment
,
241 /* INMOD2 19 */ "mod2", mod2incomment
, mod2outcomment
,
242 /* INSUNF77 20 */ "Sunf77", fincomment
, foutcomment
,
247 printerrors(boolean look_at_subclass
, int errorc
, Eptr errorv
[])
252 for (errorp
= errorv
[i
= 0]; i
< errorc
; errorp
= errorv
[++i
]) {
253 if (errorp
->error_e_class
== C_IGNORE
)
255 if (look_at_subclass
&& errorp
->error_s_class
== C_DUPL
)
257 (void) printf("Error %d, (%s error) [%s], text = \"",
259 class_table
[errorp
->error_e_class
],
260 lang_table
[errorp
->error_language
].lang_name
);
261 wordvprint(stdout
, errorp
->error_lgtext
, errorp
->error_text
);
262 (void) printf("\"\n");
267 wordvprint(FILE *fyle
, int wordc
, char *wordv
[])
272 for (i
= 0; i
< wordc
; i
++)
274 (void) fprintf(fyle
, "%s%s", sep
, wordv
[i
]);
280 * Given a string, parse it into a number of words, and build
281 * a wordc wordv combination pointing into it.
284 wordvbuild(char *string
, int *r_wordc
, char ***r_wordv
)
292 saltedbuffer
= strsave(string
);
293 for (wordcount
= 0, cp
= saltedbuffer
; *cp
; wordcount
++) {
294 while (*cp
&& isspace(*cp
))
298 while (*cp
&& !isspace(*cp
))
301 wordv
= Calloc(wordcount
+ 1, sizeof (char *));
302 for (cp
= saltedbuffer
, wordindex
= 0; wordcount
;
303 wordindex
++, --wordcount
) {
304 while (*cp
&& isspace(*cp
))
308 wordv
[wordindex
] = cp
;
309 while (*cp
&& !isspace(*cp
))
314 error("Initial miscount of the number of words in a line\n");
315 wordv
[wordindex
] = NULL
;
317 for (wordcount
= 0; wordcount
< wordindex
; wordcount
++)
318 (void) printf("Word %d = \"%s\"\n", wordcount
,
322 *r_wordc
= wordindex
;
326 * Compare two 0 based wordvectors
329 wordvcmp(char **wordv1
, int wordc
, char **wordv2
)
334 for (i
= 0; i
< wordc
; i
++) {
335 if (wordv1
[i
] == 0 || wordv2
[i
] == 0)
337 if (back
= strcmp(wordv1
[i
], wordv2
[i
])) {
341 return (0); /* they are equal */
345 * splice a 0 basedword vector onto the tail of a
346 * new wordv, allowing the first emptyhead slots to be empty
349 wordvsplice(int emptyhead
, int wordc
, char **wordv
)
352 int nwordc
= emptyhead
+ wordc
;
355 nwordv
= Calloc(nwordc
, sizeof (char *));
356 for (i
= 0; i
< emptyhead
; i
++)
358 for (i
= emptyhead
; i
< nwordc
; i
++) {
359 nwordv
[i
] = wordv
[i
-emptyhead
];
365 * plural'ize and verb forms
367 static char *S
= "s";
373 return (n
> 1 ? S
: N
);
379 return (n
> 1 ? N
: S
);