Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 1998-2002 Kungliga Tekniska Högskolan
3 : : * (Royal Institute of Technology, Stockholm, Sweden).
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions
8 : : * are met:
9 : : *
10 : : * 1. Redistributions of source code must retain the above copyright
11 : : * notice, this list of conditions and the following disclaimer.
12 : : *
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.
16 : : *
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.
20 : : *
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.
32 : : */
33 : :
34 : : #undef ROKEN_RENAME
35 : :
36 : : #include "config.h"
37 : :
38 : : #include "compile_et.h"
39 : : #include <getarg.h>
40 : :
41 : : #include <roken.h>
42 : : #include <err.h>
43 : : #include "parse.h"
44 : :
45 : : int numerror;
46 : : extern FILE *yyin;
47 : :
48 : : extern void yyparse(void);
49 : :
50 : : long base_id;
51 : : int number;
52 : : char *prefix;
53 : : char *id_str;
54 : :
55 : : char name[128];
56 : : char Basename[128];
57 : :
58 : : #ifdef YYDEBUG
59 : : extern int yydebug = 1;
60 : : #endif
61 : :
62 : : char *filename;
63 : : char hfn[128];
64 : : char cfn[128];
65 : :
66 : : struct error_code *codes = NULL;
67 : :
68 : : static int
69 : 10 : generate_c(void)
70 : : {
71 : : int n;
72 : : struct error_code *ec;
73 : :
74 : 10 : FILE *c_file = fopen(cfn, "w");
75 [ - + ]: 10 : if(c_file == NULL)
76 : 0 : return 1;
77 : :
78 : 10 : fprintf(c_file, "/* Generated from %s */\n", filename);
79 [ + + ]: 10 : if(id_str)
80 : 9 : fprintf(c_file, "/* %s */\n", id_str);
81 : 10 : fprintf(c_file, "\n");
82 : 10 : fprintf(c_file, "#include <stddef.h>\n");
83 : 10 : fprintf(c_file, "#include <com_err.h>\n");
84 : 10 : fprintf(c_file, "#include \"%s\"\n", hfn);
85 : 10 : fprintf(c_file, "\n");
86 : 10 : fprintf(c_file, "#define N_(x) (x)\n");
87 : 10 : fprintf(c_file, "\n");
88 : :
89 : 10 : fprintf(c_file, "static const char *%s_error_strings[] = {\n", name);
90 : :
91 [ + + ]: 421 : for(ec = codes, n = 0; ec; ec = ec->next, n++) {
92 [ + + ]: 813 : while(n < ec->number) {
93 : 402 : fprintf(c_file, "\t/* %03d */ \"Reserved %s error (%d)\",\n",
94 : : n, name, n);
95 : 402 : n++;
96 : :
97 : : }
98 : 411 : fprintf(c_file, "\t/* %03d */ N_(\"%s\"),\n",
99 : : ec->number, ec->string);
100 : : }
101 : :
102 : 10 : fprintf(c_file, "\tNULL\n");
103 : 10 : fprintf(c_file, "};\n");
104 : 10 : fprintf(c_file, "\n");
105 : 10 : fprintf(c_file, "#define num_errors %d\n", number);
106 : 10 : fprintf(c_file, "\n");
107 : 10 : fprintf(c_file,
108 : : "void initialize_%s_error_table_r(struct et_list **list)\n",
109 : : name);
110 : 10 : fprintf(c_file, "{\n");
111 : 10 : fprintf(c_file,
112 : : " initialize_error_table_r(list, %s_error_strings, "
113 : : "num_errors, ERROR_TABLE_BASE_%s);\n", name, name);
114 : 10 : fprintf(c_file, "}\n");
115 : 10 : fprintf(c_file, "\n");
116 : 10 : fprintf(c_file, "void initialize_%s_error_table(void)\n", name);
117 : 10 : fprintf(c_file, "{\n");
118 : 10 : fprintf(c_file,
119 : : " init_error_table(%s_error_strings, ERROR_TABLE_BASE_%s, "
120 : : "num_errors);\n", name, name);
121 : 10 : fprintf(c_file, "}\n");
122 : :
123 : 10 : fclose(c_file);
124 : 10 : return 0;
125 : : }
126 : :
127 : : static int
128 : 10 : generate_h(void)
129 : : {
130 : : struct error_code *ec;
131 : : char fn[128];
132 : 10 : FILE *h_file = fopen(hfn, "w");
133 : : char *p;
134 : :
135 [ - + ]: 10 : if(h_file == NULL)
136 : 0 : return 1;
137 : :
138 : 10 : snprintf(fn, sizeof(fn), "__%s__", hfn);
139 [ + + ]: 150 : for(p = fn; *p; p++)
140 [ + + ]: 140 : if(!isalnum((unsigned char)*p))
141 : 60 : *p = '_';
142 : :
143 : 10 : fprintf(h_file, "/* Generated from %s */\n", filename);
144 [ + + ]: 10 : if(id_str)
145 : 9 : fprintf(h_file, "/* %s */\n", id_str);
146 : 10 : fprintf(h_file, "\n");
147 : 10 : fprintf(h_file, "#ifndef %s\n", fn);
148 : 10 : fprintf(h_file, "#define %s\n", fn);
149 : 10 : fprintf(h_file, "\n");
150 : 10 : fprintf(h_file, "struct et_list;\n");
151 : 10 : fprintf(h_file, "\n");
152 : 10 : fprintf(h_file,
153 : : "void initialize_%s_error_table_r(struct et_list **);\n",
154 : : name);
155 : 10 : fprintf(h_file, "\n");
156 : 10 : fprintf(h_file, "void initialize_%s_error_table(void);\n", name);
157 : 10 : fprintf(h_file, "#define init_%s_err_tbl initialize_%s_error_table\n",
158 : : name, name);
159 : 10 : fprintf(h_file, "\n");
160 : 10 : fprintf(h_file, "typedef enum %s_error_number{\n", name);
161 : :
162 [ + + ]: 421 : for(ec = codes; ec; ec = ec->next) {
163 [ + + ]: 411 : fprintf(h_file, "\t%s = %ld%s\n", ec->name, base_id + ec->number,
164 : 411 : (ec->next != NULL) ? "," : "");
165 : : }
166 : :
167 : 10 : fprintf(h_file, "} %s_error_number;\n", name);
168 : 10 : fprintf(h_file, "\n");
169 : 10 : fprintf(h_file, "#define ERROR_TABLE_BASE_%s %ld\n", name, base_id);
170 : 10 : fprintf(h_file, "\n");
171 : 10 : fprintf(h_file, "#define COM_ERR_BINDDOMAIN_%s \"heim_com_err%ld\"\n", name, base_id);
172 : 10 : fprintf(h_file, "\n");
173 : 10 : fprintf(h_file, "#endif /* %s */\n", fn);
174 : :
175 : :
176 : 10 : fclose(h_file);
177 : 10 : return 0;
178 : : }
179 : :
180 : : static int
181 : 10 : generate(void)
182 : : {
183 [ + - ][ - + ]: 10 : return generate_c() || generate_h();
184 : : }
185 : :
186 : : int version_flag;
187 : : int help_flag;
188 : : struct getargs args[] = {
189 : : { "version", 0, arg_flag, &version_flag },
190 : : { "help", 0, arg_flag, &help_flag }
191 : : };
192 : : int num_args = sizeof(args) / sizeof(args[0]);
193 : :
194 : : static void
195 : 0 : usage(int code)
196 : : {
197 : 0 : arg_printusage(args, num_args, NULL, "error-table");
198 : 0 : exit(code);
199 : : }
200 : :
201 : : int
202 : 10 : main(int argc, char **argv)
203 : : {
204 : : char *p;
205 : 10 : int optidx = 0;
206 : :
207 : 10 : setprogname(argv[0]);
208 [ - + ]: 10 : if(getarg(args, num_args, argc, argv, &optidx))
209 : 0 : usage(1);
210 [ - + ]: 10 : if(help_flag)
211 : 0 : usage(0);
212 [ - + ]: 10 : if(version_flag) {
213 : 0 : print_version(NULL);
214 : 0 : exit(0);
215 : : }
216 : :
217 [ - + ]: 10 : if(optidx == argc)
218 : 0 : usage(1);
219 : 10 : filename = argv[optidx];
220 : 10 : yyin = fopen(filename, "r");
221 [ - + ]: 10 : if(yyin == NULL)
222 : 0 : err(1, "%s", filename);
223 : :
224 : :
225 : 10 : p = strrchr(filename, rk_PATH_DELIM);
226 [ + - ]: 10 : if(p)
227 : 10 : p++;
228 : : else
229 : 0 : p = filename;
230 : 10 : strlcpy(Basename, p, sizeof(Basename));
231 : :
232 : 10 : Basename[strcspn(Basename, ".")] = '\0';
233 : :
234 : 10 : snprintf(hfn, sizeof(hfn), "%s.h", Basename);
235 : 10 : snprintf(cfn, sizeof(cfn), "%s.c", Basename);
236 : :
237 : 10 : yyparse();
238 [ - + ]: 10 : if(numerror)
239 : 0 : return 1;
240 : :
241 : 10 : return generate();
242 : : }
|