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