8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / avs / errgen / errgen.c
blobea7eb756b69314af156f8edb4fbe0510878711a3
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Read an errgen status resource file (*.err) from standard input and
28 * write an SPCS error code C header file (-c), Java resource file (-j),
29 * libspcs Java exception class file(-e), error text file (-m) or JNI
30 * exception trinket table to standard output. Lines starting with "#"
31 * are ignored.
33 * Use "errgen -h" to get usage info including module codes and example
34 * input and output.
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <libgen.h>
43 #include <limits.h>
44 #include <sys/param.h>
46 /* The public error info header file. */
48 #include <sys/unistat/spcs_s.h>
50 /* The private error info header file */
52 #include <sys/unistat/spcs_s_impl.h>
55 /* locals */
57 static enum {C_MODE, J_MODE, M_MODE, E_MODE, T_MODE, X_MODE} mode = E_MODE;
58 static char key[SPCS_S_MAXKEY];
59 static char text[SPCS_S_MAXTEXT];
60 static int mod_number;
62 static char help_path[PATH_MAX];
64 static int count = 1;
66 static char key[SPCS_S_MAXKEY];
67 static char text[SPCS_S_MAXTEXT];
68 static char modname[SPCS_S_MAXMODNAME];
71 * Display help info
74 static void
75 help()
77 char line[SPCS_S_MAXLINE];
78 FILE *h = fopen(help_path, "r");
80 if (h) {
81 while (! feof(h)) {
82 (void) fgets(line, SPCS_S_MAXLINE, h);
83 if (! feof(h))
84 (void) fputs(line, stderr);
86 } else {
87 perror(strcat("could not open: ", help_path));
88 exit(1);
93 * Put out a message with terse instructions and err out
96 static void
97 fatal(char *msg)
99 (void) fprintf(stderr, "%s\n\n", msg);
100 (void) fprintf(stderr, "use errgen -h for help\n");
101 exit(1);
105 * Put out the output file preamble
108 static void
109 do_preamble()
111 switch (mode) {
112 case M_MODE:
113 (void) fprintf(stdout,
114 "static\nchar *SPCS_MSG_%s[] = {\n", modname);
115 (void) fprintf(stdout, "\t\"\",\n");
116 break;
117 case T_MODE:
118 (void) fprintf(stdout,
119 "static\nchar *SPCS_TRNK_%s[] = {\n", modname);
120 (void) fprintf(stdout, "\t\"\",\n");
121 break;
126 * Put out the output file trailer
129 static void
130 do_trailer()
132 switch (mode) {
133 case M_MODE:
134 (void) fprintf(stdout, "};\n");
135 (void) fprintf(stdout,
136 "#define\tSPCS_MSGLEN_%s %d\t/* total codes */\n",
137 modname, count-1);
138 break;
139 case T_MODE:
140 (void) fprintf(stdout, "};\n");
141 (void) fprintf(stdout,
142 "#define\tSPCS_TRNKLEN_%s %d\t/* total codes */\n",
143 modname, count-1);
144 break;
149 * Process a single input line
152 static void
153 do_line()
155 spcs_s_udata_t c;
156 int fc = 0;
157 int len = 0;
158 char ptext[SPCS_S_MAXTEXT];
159 char keystring[SPCS_S_MAXKEY+SPCS_S_MAXPRE];
160 char *p = text;
161 int tlen;
162 char *pt = ptext;
163 char havebytestream = 0;
165 c.i = 0;
166 (void) sprintf(keystring, "%s_E%s", modname, key);
167 while (*p) {
168 if (*p == '%') {
169 if (*(p + 1) != 's') {
170 (void) fprintf(stderr,
171 "ERRGEN: Error in .err file\n");
172 (void) fprintf(stderr,
173 "%c is an illegal format spec after %%",
174 *p);
175 (void) fprintf(stderr,
176 " at line: %d pos: %d\n", count,
177 /* LINTED possible ptrdiff_t overflow */
178 (int)(p - text));
179 fatal("");
181 len = sprintf(pt, "{%d}", fc);
182 pt += len;
183 p++;
184 fc += 1;
185 if (fc > SPCS_S_MAXSUPP) {
186 (void) fprintf(stderr,
187 "ERRGEN: Error in .err file\n");
188 (void) fprintf(stderr,
189 "SPCS_S_MAXSUPP exceeeded\n");
190 fatal("Too many %%s specifiers");
192 } else
193 *pt++ = *p;
194 p++;
197 /* look for a bytestream indicator */
199 tlen = strlen(text);
201 if ((tlen > 2) && (text[tlen - 1] == '@') && (text[tlen - 2] == '@')) {
202 if (fc)
203 fatal("ERRGEN: cannot have %%s and @@ ending too");
205 /* bump the item count and set the bytestream flag */
206 fc += 1;
207 havebytestream = 1;
210 *pt = 0;
212 switch (mode) {
213 case C_MODE:
214 c.f.bytestream = havebytestream;
215 c.f.sup_count = fc;
216 c.f.module = mod_number;
217 c.f.code = count;
218 (void) fprintf(stdout, "#define\t%s 0x%x /* %s */\n",
219 keystring, c.i, text);
220 break;
221 case J_MODE:
222 (void) fprintf(stdout, "`%s` = %s\n", keystring, ptext);
223 break;
224 case X_MODE:
225 (void) fprintf(stdout,
226 "#define\tT_%s \"`%s`\"\n", keystring, keystring);
227 break;
228 case T_MODE:
229 (void) fprintf(stdout, "\t\"`%s`\",\n", keystring);
230 break;
231 case M_MODE:
232 (void) fprintf(stdout, "\t\"%s\",\n", text);
233 break;
234 case E_MODE:
235 (void) fprintf(stdout, " /**\n * %s\n **/\n",
236 text);
237 (void) fprintf(stdout, " public static final String %s",
238 keystring);
239 (void) fprintf(stdout, " = `%s`;\n\n", keystring);
240 break;
245 main(int argc, char **argv)
247 int i;
248 int searching = 1;
249 char searchname[SPCS_S_MAXMODNAME];
250 char line[SPCS_S_MAXLINE];
251 char tline[SPCS_S_MAXLINE];
252 char *p, *p2;
254 (void) strcpy(help_path, dirname(argv[0]));
255 (void) strcat(help_path, "/errgen.help");
256 if ((argc == 1) || ((argc == 2) && (strcmp(argv[1], "-h") == 0))) {
257 help();
258 exit(0);
261 if (argc != 3)
262 fatal("Bad number of arguments");
264 p = argv[2];
265 p2 = modname;
267 while (*p)
268 *p2++ = toupper(*p++);
269 *p2 = 0;
271 switch (argv[1][1]) {
272 case 'c':
273 mode = C_MODE;
274 break;
275 case 'j':
276 mode = J_MODE;
277 break;
278 case 'e':
279 mode = E_MODE;
280 break;
281 case 'm':
282 mode = M_MODE;
283 break;
284 case 't':
285 mode = T_MODE;
286 break;
287 case 'x':
288 mode = X_MODE;
289 break;
290 default:
291 fatal("Unknown option switch");
294 if (strcmp(modname, "DSW") == 0) {
295 (void) strcpy(searchname, "II");
296 } else if (strcmp(modname, "RDC") == 0) {
297 (void) strcpy(searchname, "SNDR");
298 } else if (strcmp(modname, "SDCTL") == 0) {
299 (void) strcpy(searchname, "NSCTL");
300 } else {
301 (void) strcpy(searchname, modname);
304 i = 0;
305 do {
306 if (strcmp(module_names[i++], searchname) == 0) {
307 searching = 0;
308 mod_number = i - 1;
309 break;
311 } while (module_names[i]);
313 if (searching) {
314 if (i != SPCS_M_MAX)
315 (void) fprintf(stderr,
316 "NULL in module_names before SPCS_M_MAX\n");
317 fatal("Undefined module name");
320 do_preamble();
322 while (!feof(stdin)) {
323 (void) fgets(line, SPCS_S_MAXLINE, stdin);
324 if (feof(stdin)) {
325 if (count == 0)
326 fatal("errgen file empty");
328 do_trailer();
329 exit(0);
331 line[strlen(line)-1] = 0;
332 if ((strlen(line) != 0) && (line[0] != '#')) {
333 (void) strcpy(tline, line);
334 p = strchr(tline, ' ');
335 if (p == NULL) {
336 (void) fprintf(stderr,
337 "blank separator missing at line: %d\n",
338 count);
339 fatal("");
341 *p = 0;
342 if (strlen(p) > SPCS_S_MAXKEY) {
343 (void) fprintf(stderr,
344 "message label too long at line: %d\n", count);
345 fatal("");
347 (void) strcpy(key, tline);
348 if (strlen(key) == 0) {
349 (void) fprintf(stderr,
350 "leading blank at line: %d\n", count);
351 fatal("");
353 p++;
354 if (*p != '=') {
355 (void) fprintf(stderr,
356 "= separator missing at line: %d\n", count);
357 fatal("");
359 p++;
360 if (*p != ' ') {
361 (void) fprintf(stderr,
362 "blank separator missing at line: %d\n", count);
363 fatal("");
365 p++;
366 if (! *p) {
367 (void) fprintf(stderr,
368 "msg text missing at line:%d\n", count);
369 fatal("");
371 (void) strcpy(text, p);
373 do_line();
374 count++;
378 return (0);