1 /* $NetBSD: compile_et.c,v 1.5 2023/06/19 21:41:42 christos Exp $ */
2
3 /*
4 * Copyright (c) 1998-2002 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #undef ROKEN_RENAME
37
38 #include "config.h"
39
40 #include "compile_et.h"
41 #include <getarg.h>
42 #include <libgen.h>
43
44 #include <roken.h>
45 #include <err.h>
46 #include "parse.h"
47
48 int numerror;
49 extern FILE *yyin;
50
51 int yyparse(void);
52
53 long base_id;
54 int number;
55 char *prefix;
56 char *id_str;
57
58 char name[128];
59 char Basename[128];
60
61 #ifdef YYDEBUG
62 extern int yydebug;
63 int yydebug = 1;
64 #endif
65
66 char *filename;
67 char hfn[128];
68 char cfn[128];
69
70 struct error_code *codes = NULL;
71
72 static int
generate_c(void)73 generate_c(void)
74 {
75 int n;
76 struct error_code *ec;
77
78 FILE *c_file = fopen(cfn, "w");
79 if(c_file == NULL)
80 return 1;
81
82 fprintf(c_file, "/* Generated from %s */\n", basename(filename));
83 if(id_str)
84 fprintf(c_file, "/* %s */\n", id_str);
85 fprintf(c_file, "\n");
86 fprintf(c_file, "#include <stddef.h>\n");
87 fprintf(c_file, "#include <krb5/com_err.h>\n");
88 fprintf(c_file, "#include \"%s\"\n", hfn);
89 fprintf(c_file, "\n");
90 fprintf(c_file, "#define N_(x) (x)\n");
91 fprintf(c_file, "\n");
92
93 fprintf(c_file, "static const char *%s_error_strings[] = {\n", name);
94
95 for(ec = codes, n = 0; ec; ec = ec->next, n++) {
96 while(n < ec->number) {
97 fprintf(c_file, "\t/* %03d */ \"Reserved %s error (%d)\",\n",
98 n, name, n);
99 n++;
100
101 }
102 fprintf(c_file, "\t/* %03d */ N_(\"%s\"),\n",
103 ec->number, ec->string);
104 }
105
106 fprintf(c_file, "\tNULL\n");
107 fprintf(c_file, "};\n");
108 fprintf(c_file, "\n");
109 fprintf(c_file, "#define num_errors %d\n", number);
110 fprintf(c_file, "\n");
111 fprintf(c_file,
112 "void initialize_%s_error_table_r(struct et_list **list)\n",
113 name);
114 fprintf(c_file, "{\n");
115 fprintf(c_file,
116 " initialize_error_table_r(list, %s_error_strings, "
117 "num_errors, ERROR_TABLE_BASE_%s);\n", name, name);
118 fprintf(c_file, "}\n");
119 fprintf(c_file, "\n");
120 fprintf(c_file, "void initialize_%s_error_table(void)\n", name);
121 fprintf(c_file, "{\n");
122 fprintf(c_file,
123 " init_error_table(%s_error_strings, ERROR_TABLE_BASE_%s, "
124 "num_errors);\n", name, name);
125 fprintf(c_file, "}\n");
126
127 fclose(c_file);
128 return 0;
129 }
130
131 static int
generate_h(void)132 generate_h(void)
133 {
134 struct error_code *ec;
135 char fn[128];
136 FILE *h_file = fopen(hfn, "w");
137 char *p;
138
139 if(h_file == NULL)
140 return 1;
141
142 snprintf(fn, sizeof(fn), "__%s__", hfn);
143 for(p = fn; *p; p++)
144 if(!isalnum((unsigned char)*p))
145 *p = '_';
146
147 fprintf(h_file, "/* Generated from %s */\n", basename(filename));
148 if(id_str)
149 fprintf(h_file, "/* %s */\n", id_str);
150 fprintf(h_file, "\n");
151 fprintf(h_file, "#ifndef %s\n", fn);
152 fprintf(h_file, "#define %s\n", fn);
153 fprintf(h_file, "\n");
154 fprintf(h_file, "struct et_list;\n");
155 fprintf(h_file, "\n");
156 fprintf(h_file,
157 "void initialize_%s_error_table_r(struct et_list **);\n",
158 name);
159 fprintf(h_file, "\n");
160 fprintf(h_file, "void initialize_%s_error_table(void);\n", name);
161 fprintf(h_file, "#define init_%s_err_tbl initialize_%s_error_table\n",
162 name, name);
163 fprintf(h_file, "\n");
164 fprintf(h_file, "typedef enum %s_error_number{\n", name);
165
166 for(ec = codes; ec; ec = ec->next) {
167 fprintf(h_file, "\t%s = %ld%s\n", ec->name, base_id + ec->number,
168 (ec->next != NULL) ? "," : "");
169 }
170
171 fprintf(h_file, "} %s_error_number;\n", name);
172 fprintf(h_file, "\n");
173 fprintf(h_file, "#define ERROR_TABLE_BASE_%s %ld\n", name, base_id);
174 fprintf(h_file, "\n");
175 fprintf(h_file, "#define COM_ERR_BINDDOMAIN_%s \"heim_com_err%ld\"\n", name, base_id);
176 fprintf(h_file, "\n");
177 fprintf(h_file, "#endif /* %s */\n", fn);
178
179
180 fclose(h_file);
181 return 0;
182 }
183
184 static int
generate(void)185 generate(void)
186 {
187 return generate_c() || generate_h();
188 }
189
190 int version_flag;
191 int help_flag;
192 struct getargs args[] = {
193 { "version", 0, arg_flag, &version_flag, NULL, NULL },
194 { "help", 0, arg_flag, &help_flag, NULL, NULL }
195 };
196 int num_args = sizeof(args) / sizeof(args[0]);
197
198 static void
usage(int code)199 usage(int code)
200 {
201 arg_printusage(args, num_args, NULL, "error-table");
202 exit(code);
203 }
204
205 int
main(int argc,char ** argv)206 main(int argc, char **argv)
207 {
208 char *p;
209 int optidx = 0;
210
211 setprogname(argv[0]);
212 if(getarg(args, num_args, argc, argv, &optidx))
213 usage(1);
214 if(help_flag)
215 usage(0);
216 if(version_flag) {
217 print_version(NULL);
218 exit(0);
219 }
220
221 if(optidx == argc)
222 usage(1);
223 filename = argv[optidx];
224 yyin = fopen(filename, "r");
225 if(yyin == NULL)
226 err(1, "%s", filename);
227
228
229 p = strrchr(filename, rk_PATH_DELIM);
230 if(p)
231 p++;
232 else
233 p = filename;
234 strlcpy(Basename, p, sizeof(Basename));
235
236 Basename[strcspn(Basename, ".")] = '\0';
237
238 snprintf(hfn, sizeof(hfn), "%s.h", Basename);
239 snprintf(cfn, sizeof(cfn), "%s.c", Basename);
240
241 yyparse();
242 if(numerror)
243 return 1;
244
245 return generate();
246 }
247