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