1 /* $NetBSD: compile_et.c,v 1.3 2014/04/24 13:45:34 pettai 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 = 1;
63 #endif
64
65 char *filename;
66 char hfn[128];
67 char cfn[128];
68
69 struct error_code *codes = NULL;
70
71 static int
generate_c(void)72 generate_c(void)
73 {
74 int n;
75 struct error_code *ec;
76
77 FILE *c_file = fopen(cfn, "w");
78 if(c_file == NULL)
79 return 1;
80
81 fprintf(c_file, "/* Generated from %s */\n", basename(filename));
82 if(id_str)
83 fprintf(c_file, "/* %s */\n", id_str);
84 fprintf(c_file, "\n");
85 fprintf(c_file, "#include <stddef.h>\n");
86 fprintf(c_file, "#include <krb5/com_err.h>\n");
87 fprintf(c_file, "#include \"%s\"\n", hfn);
88 fprintf(c_file, "\n");
89 fprintf(c_file, "#define N_(x) (x)\n");
90 fprintf(c_file, "\n");
91
92 fprintf(c_file, "static const char *%s_error_strings[] = {\n", name);
93
94 for(ec = codes, n = 0; ec; ec = ec->next, n++) {
95 while(n < ec->number) {
96 fprintf(c_file, "\t/* %03d */ \"Reserved %s error (%d)\",\n",
97 n, name, n);
98 n++;
99
100 }
101 fprintf(c_file, "\t/* %03d */ N_(\"%s\"),\n",
102 ec->number, ec->string);
103 }
104
105 fprintf(c_file, "\tNULL\n");
106 fprintf(c_file, "};\n");
107 fprintf(c_file, "\n");
108 fprintf(c_file, "#define num_errors %d\n", number);
109 fprintf(c_file, "\n");
110 fprintf(c_file,
111 "void initialize_%s_error_table_r(struct et_list **list)\n",
112 name);
113 fprintf(c_file, "{\n");
114 fprintf(c_file,
115 " initialize_error_table_r(list, %s_error_strings, "
116 "num_errors, ERROR_TABLE_BASE_%s);\n", name, name);
117 fprintf(c_file, "}\n");
118 fprintf(c_file, "\n");
119 fprintf(c_file, "void initialize_%s_error_table(void)\n", name);
120 fprintf(c_file, "{\n");
121 fprintf(c_file,
122 " init_error_table(%s_error_strings, ERROR_TABLE_BASE_%s, "
123 "num_errors);\n", name, name);
124 fprintf(c_file, "}\n");
125
126 fclose(c_file);
127 return 0;
128 }
129
130 static int
generate_h(void)131 generate_h(void)
132 {
133 struct error_code *ec;
134 char fn[128];
135 FILE *h_file = fopen(hfn, "w");
136 char *p;
137
138 if(h_file == NULL)
139 return 1;
140
141 snprintf(fn, sizeof(fn), "__%s__", hfn);
142 for(p = fn; *p; p++)
143 if(!isalnum((unsigned char)*p))
144 *p = '_';
145
146 fprintf(h_file, "/* Generated from %s */\n", basename(filename));
147 if(id_str)
148 fprintf(h_file, "/* %s */\n", id_str);
149 fprintf(h_file, "\n");
150 fprintf(h_file, "#ifndef %s\n", fn);
151 fprintf(h_file, "#define %s\n", fn);
152 fprintf(h_file, "\n");
153 fprintf(h_file, "struct et_list;\n");
154 fprintf(h_file, "\n");
155 fprintf(h_file,
156 "void initialize_%s_error_table_r(struct et_list **);\n",
157 name);
158 fprintf(h_file, "\n");
159 fprintf(h_file, "void initialize_%s_error_table(void);\n", name);
160 fprintf(h_file, "#define init_%s_err_tbl initialize_%s_error_table\n",
161 name, name);
162 fprintf(h_file, "\n");
163 fprintf(h_file, "typedef enum %s_error_number{\n", name);
164
165 for(ec = codes; ec; ec = ec->next) {
166 fprintf(h_file, "\t%s = %ld%s\n", ec->name, base_id + ec->number,
167 (ec->next != NULL) ? "," : "");
168 }
169
170 fprintf(h_file, "} %s_error_number;\n", name);
171 fprintf(h_file, "\n");
172 fprintf(h_file, "#define ERROR_TABLE_BASE_%s %ld\n", name, base_id);
173 fprintf(h_file, "\n");
174 fprintf(h_file, "#define COM_ERR_BINDDOMAIN_%s \"heim_com_err%ld\"\n", name, base_id);
175 fprintf(h_file, "\n");
176 fprintf(h_file, "#endif /* %s */\n", fn);
177
178
179 fclose(h_file);
180 return 0;
181 }
182
183 static int
generate(void)184 generate(void)
185 {
186 return generate_c() || generate_h();
187 }
188
189 int version_flag;
190 int help_flag;
191 struct getargs args[] = {
192 { "version", 0, arg_flag, &version_flag },
193 { "help", 0, arg_flag, &help_flag }
194 };
195 int num_args = sizeof(args) / sizeof(args[0]);
196
197 static void
usage(int code)198 usage(int code)
199 {
200 arg_printusage(args, num_args, NULL, "error-table");
201 exit(code);
202 }
203
204 int
main(int argc,char ** argv)205 main(int argc, char **argv)
206 {
207 char *p;
208 int optidx = 0;
209
210 setprogname(argv[0]);
211 if(getarg(args, num_args, argc, argv, &optidx))
212 usage(1);
213 if(help_flag)
214 usage(0);
215 if(version_flag) {
216 print_version(NULL);
217 exit(0);
218 }
219
220 if(optidx == argc)
221 usage(1);
222 filename = argv[optidx];
223 yyin = fopen(filename, "r");
224 if(yyin == NULL)
225 err(1, "%s", filename);
226
227
228 p = strrchr(filename, rk_PATH_DELIM);
229 if(p)
230 p++;
231 else
232 p = filename;
233 strlcpy(Basename, p, sizeof(Basename));
234
235 Basename[strcspn(Basename, ".")] = '\0';
236
237 snprintf(hfn, sizeof(hfn), "%s.h", Basename);
238 snprintf(cfn, sizeof(cfn), "%s.c", Basename);
239
240 yyparse();
241 if(numerror)
242 return 1;
243
244 return generate();
245 }
246