Annotation of src/usr.bin/compile_et/compile_et.c, Revision 1.12
1.12 ! jaredy 1: /* $OpenBSD: compile_et.c,v 1.11 2003/07/14 16:14:43 mho Exp $ */
1.1 downsj 2: /*
1.5 hin 3: * Copyright (c) 1998, 1999 Kungliga Tekniska Högskolan
4: * (Royal Institute of Technology, Stockholm, Sweden).
5: * 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: *
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: *
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer in the
16: * documentation and/or other materials provided with the distribution.
17: *
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by Kungliga Tekniska
21: * Högskolan and its contributors.
22: *
23: * 4. Neither the name of the Institute nor the names of its contributors
24: * may be used to endorse or promote products derived from this software
25: * without specific prior written permission.
1.1 downsj 26: *
1.5 hin 27: * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
28: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30: * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
31: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37: * SUCH DAMAGE.
1.1 downsj 38: */
39:
1.5 hin 40: #undef ROKEN_RENAME
41: #include "compile_et.h"
42:
43: /* RCSID("$KTH: compile_et.c,v 1.12 1999/04/01 09:13:52 joda Exp $"); */
44:
45: #include <err.h>
46: #include "error_table.h"
47:
48: int numerror;
49: extern FILE *yyin;
50:
51: extern void yyparse(void);
1.1 downsj 52:
1.5 hin 53: long base;
54: int number;
55: char *prefix;
56: char *id_str;
1.1 downsj 57:
1.5 hin 58: char name[128];
59: char Basename[128];
1.1 downsj 60:
1.5 hin 61: #ifdef YYDEBUG
62: extern int yydebug = 1;
1.1 downsj 63: #endif
64:
1.5 hin 65: char *filename;
66: char hfn[128];
67: char cfn[128];
68:
69: struct error_code *codes = NULL;
70:
71: static int
72: generate_c(void)
73: {
1.7 aaron 74: int n;
75: struct error_code *ec;
76: FILE *c_file = fopen(cfn, "w");
77:
78: if (c_file == NULL)
79: return 1;
80:
81: fprintf(c_file, "/* Generated from %s */\n", filename);
82: if (id_str)
1.8 aaron 83: fprintf(c_file, "/* %s */\n", id_str);
1.7 aaron 84:
85: fprintf(c_file, "\n");
86: fprintf(c_file, "#include <stddef.h>\n");
87: fprintf(c_file, "#include <com_err.h>\n");
88: fprintf(c_file, "#include \"%s\"\n", hfn);
89: fprintf(c_file, "\n");
90:
91: fprintf(c_file, "static const char *text[] = {\n");
92:
93: for (ec = codes, n = 0; ec; ec = ec->next, n++) {
94: while (n < ec->number) {
95: fprintf(c_file,
96: "\t/* %03d */ \"Reserved %s error (%d)\",\n",
97: n, name, n);
98: n++;
99: }
100: fprintf(c_file,
101: "\t/* %03d */ \"%s\",\n", ec->number, ec->string);
1.5 hin 102: }
1.1 downsj 103:
1.7 aaron 104: fprintf(c_file, "\tNULL\n");
105: fprintf(c_file, "};\n");
106: fprintf(c_file, "\n");
107: fprintf(c_file,
108: "void initialize_%s_error_table_r(struct et_list **list)\n", name);
109: fprintf(c_file, "{\n");
110: fprintf(c_file,
1.5 hin 111: " initialize_error_table_r(list, text, "
112: "%s_num_errors, ERROR_TABLE_BASE_%s);\n", name, name);
1.7 aaron 113: fprintf(c_file, "}\n");
114: fprintf(c_file, "\n");
115: fprintf(c_file, "void initialize_%s_error_table(void)\n", name);
116: fprintf(c_file, "{\n");
117: fprintf(c_file,
1.5 hin 118: " init_error_table(text, ERROR_TABLE_BASE_%s, "
119: "%s_num_errors);\n", name, name);
1.7 aaron 120: fprintf(c_file, "}\n");
1.1 downsj 121:
1.7 aaron 122: fclose(c_file);
123: return 0;
1.1 downsj 124: }
125:
1.5 hin 126: static int
127: generate_h(void)
128: {
1.7 aaron 129: struct error_code *ec;
130: char fn[128], *p;
131: FILE *h_file = fopen(hfn, "w");
132:
133: if (h_file == NULL)
134: return 1;
135:
136: snprintf(fn, sizeof(fn), "__%s__", hfn);
137: for (p = fn; *p; p++) {
138: if(!isalnum((unsigned char)*p))
139: *p = '_';
140: }
141:
142: fprintf(h_file, "/* Generated from %s */\n", filename);
143: if (id_str)
144: fprintf(h_file, "/* %s */\n", id_str);
145:
146: fprintf(h_file, "\n");
147: fprintf(h_file, "#ifndef %s\n", fn);
148: fprintf(h_file, "#define %s\n", fn);
149: fprintf(h_file, "\n");
1.11 mho 150: fprintf(h_file, "#include <kerberosV/com_err.h>\n");
1.7 aaron 151: fprintf(h_file, "\n");
152: fprintf(h_file,
1.5 hin 153: "void initialize_%s_error_table_r(struct et_list **);\n",
154: name);
1.7 aaron 155: fprintf(h_file, "\n");
156: fprintf(h_file, "void initialize_%s_error_table(void);\n", name);
157: fprintf(h_file, "#define init_%s_err_tbl initialize_%s_error_table\n",
1.5 hin 158: name, name);
1.7 aaron 159: fprintf(h_file, "\n");
160: fprintf(h_file, "typedef enum %s_error_number{\n", name);
161: fprintf(h_file, "\tERROR_TABLE_BASE_%s = %ld,\n", name, base);
162: fprintf(h_file, "\t%s_err_base = %ld,\n", name, base);
163:
164: for (ec = codes; ec; ec = ec->next)
165: fprintf(h_file, "\t%s = %ld,\n", ec->name, base + ec->number);
166:
167: fprintf(h_file, "\t%s_num_errors = %d\n", name, number);
168: fprintf(h_file, "} %s_error_number;\n", name);
169: fprintf(h_file, "\n");
170: fprintf(h_file, "#endif /* %s */\n", fn);
1.5 hin 171:
1.7 aaron 172: fclose(h_file);
173: return 0;
1.1 downsj 174: }
175:
1.5 hin 176: static int
177: generate(void)
178: {
1.7 aaron 179: return generate_c() || generate_h();
1.1 downsj 180: }
181:
1.5 hin 182: static void
1.10 deraadt 183: usage(void)
1.5 hin 184: {
1.7 aaron 185: extern char *__progname;
186:
187: fprintf(stderr, "usage: %s file\n", __progname);
188: exit(1);
1.1 downsj 189: }
190:
1.5 hin 191: int
192: main(int argc, char **argv)
193: {
1.7 aaron 194: char *p;
1.5 hin 195:
1.7 aaron 196: if (argc != 2)
1.9 aaron 197: usage();
1.1 downsj 198:
1.7 aaron 199: filename = argv[1];
200: yyin = fopen(filename, "r");
201: if (yyin == NULL)
202: err(1, "%s", filename);
1.5 hin 203:
1.7 aaron 204: p = strrchr(filename, '/');
205: if (p)
206: p++;
207: else
208: p = filename;
209:
1.12 ! jaredy 210: strlcpy(Basename, p, sizeof(Basename));
1.7 aaron 211: Basename[strcspn(Basename, ".")] = '\0';
212:
213: snprintf(hfn, sizeof(hfn), "%s.h", Basename);
214: snprintf(cfn, sizeof(cfn), "%s.c", Basename);
215:
216: yyparse();
217: if (numerror)
218: return 1;
1.1 downsj 219:
1.7 aaron 220: return generate();
1.1 downsj 221: }