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]
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 "#"
33 * Use "errgen -h" to get usage info including module codes and example
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>
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
];
66 static char key
[SPCS_S_MAXKEY
];
67 static char text
[SPCS_S_MAXTEXT
];
68 static char modname
[SPCS_S_MAXMODNAME
];
77 char line
[SPCS_S_MAXLINE
];
78 FILE *h
= fopen(help_path
, "r");
82 (void) fgets(line
, SPCS_S_MAXLINE
, h
);
84 (void) fputs(line
, stderr
);
87 perror(strcat("could not open: ", help_path
));
93 * Put out a message with terse instructions and err out
99 (void) fprintf(stderr
, "%s\n\n", msg
);
100 (void) fprintf(stderr
, "use errgen -h for help\n");
105 * Put out the output file preamble
113 (void) fprintf(stdout
,
114 "static\nchar *SPCS_MSG_%s[] = {\n", modname
);
115 (void) fprintf(stdout
, "\t\"\",\n");
118 (void) fprintf(stdout
,
119 "static\nchar *SPCS_TRNK_%s[] = {\n", modname
);
120 (void) fprintf(stdout
, "\t\"\",\n");
126 * Put out the output file trailer
134 (void) fprintf(stdout
, "};\n");
135 (void) fprintf(stdout
,
136 "#define\tSPCS_MSGLEN_%s %d\t/* total codes */\n",
140 (void) fprintf(stdout
, "};\n");
141 (void) fprintf(stdout
,
142 "#define\tSPCS_TRNKLEN_%s %d\t/* total codes */\n",
149 * Process a single input line
158 char ptext
[SPCS_S_MAXTEXT
];
159 char keystring
[SPCS_S_MAXKEY
+SPCS_S_MAXPRE
];
163 char havebytestream
= 0;
166 (void) sprintf(keystring
, "%s_E%s", modname
, key
);
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 %%",
175 (void) fprintf(stderr
,
176 " at line: %d pos: %d\n", count
,
177 /* LINTED possible ptrdiff_t overflow */
181 len
= sprintf(pt
, "{%d}", fc
);
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");
197 /* look for a bytestream indicator */
201 if ((tlen
> 2) && (text
[tlen
- 1] == '@') && (text
[tlen
- 2] == '@')) {
203 fatal("ERRGEN: cannot have %%s and @@ ending too");
205 /* bump the item count and set the bytestream flag */
214 c
.f
.bytestream
= havebytestream
;
216 c
.f
.module
= mod_number
;
218 (void) fprintf(stdout
, "#define\t%s 0x%x /* %s */\n",
219 keystring
, c
.i
, text
);
222 (void) fprintf(stdout
, "`%s` = %s\n", keystring
, ptext
);
225 (void) fprintf(stdout
,
226 "#define\tT_%s \"`%s`\"\n", keystring
, keystring
);
229 (void) fprintf(stdout
, "\t\"`%s`\",\n", keystring
);
232 (void) fprintf(stdout
, "\t\"%s\",\n", text
);
235 (void) fprintf(stdout
, " /**\n * %s\n **/\n",
237 (void) fprintf(stdout
, " public static final String %s",
239 (void) fprintf(stdout
, " = `%s`;\n\n", keystring
);
245 main(int argc
, char **argv
)
249 char searchname
[SPCS_S_MAXMODNAME
];
250 char line
[SPCS_S_MAXLINE
];
251 char tline
[SPCS_S_MAXLINE
];
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))) {
262 fatal("Bad number of arguments");
268 *p2
++ = toupper(*p
++);
271 switch (argv
[1][1]) {
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");
301 (void) strcpy(searchname
, modname
);
306 if (strcmp(module_names
[i
++], searchname
) == 0) {
311 } while (module_names
[i
]);
315 (void) fprintf(stderr
,
316 "NULL in module_names before SPCS_M_MAX\n");
317 fatal("Undefined module name");
322 while (!feof(stdin
)) {
323 (void) fgets(line
, SPCS_S_MAXLINE
, stdin
);
326 fatal("errgen file empty");
331 line
[strlen(line
)-1] = 0;
332 if ((strlen(line
) != 0) && (line
[0] != '#')) {
333 (void) strcpy(tline
, line
);
334 p
= strchr(tline
, ' ');
336 (void) fprintf(stderr
,
337 "blank separator missing at line: %d\n",
342 if (strlen(p
) > SPCS_S_MAXKEY
) {
343 (void) fprintf(stderr
,
344 "message label too long at line: %d\n", count
);
347 (void) strcpy(key
, tline
);
348 if (strlen(key
) == 0) {
349 (void) fprintf(stderr
,
350 "leading blank at line: %d\n", count
);
355 (void) fprintf(stderr
,
356 "= separator missing at line: %d\n", count
);
361 (void) fprintf(stderr
,
362 "blank separator missing at line: %d\n", count
);
367 (void) fprintf(stderr
,
368 "msg text missing at line:%d\n", count
);
371 (void) strcpy(text
, p
);