]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/com_err/compile_et.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / com_err / compile_et.c
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 /* $FreeBSD$ */
34
35 #undef ROKEN_RENAME
36 #include "compile_et.h"
37 #include <getarg.h>
38
39 #if 0
40 RCSID("$Id: compile_et.c 15426 2005-06-16 19:21:42Z lha $");
41 #endif
42
43 #include <err.h>
44 #include "parse.h"
45
46 int numerror;
47 extern FILE *yyin;
48
49 extern void yyparse(void);
50
51 long base_id;
52 int number;
53 char *prefix;
54 char *id_str;
55
56 char name[128];
57 char Basename[128];
58
59 #ifdef YYDEBUG
60 extern int yydebug = 1;
61 #endif
62
63 char *filename;
64 char hfn[128];
65 char cfn[128];
66
67 struct error_code *codes = NULL;
68
69 static int
70 generate_c(void)
71 {
72     int n;
73     struct error_code *ec;
74
75     FILE *c_file = fopen(cfn, "w");
76     if(c_file == NULL)
77         return 1;
78
79     fprintf(c_file, "/* Generated from %s */\n", filename);
80     if(id_str) 
81         fprintf(c_file, "/* %s */\n", id_str);
82     fprintf(c_file, "\n");
83     fprintf(c_file, "#include <stddef.h>\n");
84     fprintf(c_file, "#include <com_err.h>\n");
85     fprintf(c_file, "#include \"%s\"\n", hfn);
86     fprintf(c_file, "\n");
87
88     fprintf(c_file, "static const char *%s_error_strings[] = {\n", name);
89
90     for(ec = codes, n = 0; ec; ec = ec->next, n++) {
91         while(n < ec->number) {
92             fprintf(c_file, "\t/* %03d */ \"Reserved %s error (%d)\",\n",
93                     n, name, n);
94             n++;
95             
96         }
97         fprintf(c_file, "\t/* %03d */ \"%s\",\n", ec->number, ec->string);
98     }
99
100     fprintf(c_file, "\tNULL\n");
101     fprintf(c_file, "};\n");
102     fprintf(c_file, "\n");
103     fprintf(c_file, "#define num_errors %d\n", number);
104     fprintf(c_file, "\n");
105     fprintf(c_file, 
106             "void initialize_%s_error_table_r(struct et_list **list)\n", 
107             name);
108     fprintf(c_file, "{\n");
109     fprintf(c_file, 
110             "    initialize_error_table_r(list, %s_error_strings, "
111             "num_errors, ERROR_TABLE_BASE_%s);\n", name, name);
112     fprintf(c_file, "}\n");
113     fprintf(c_file, "\n");
114     fprintf(c_file, "void initialize_%s_error_table(void)\n", name);
115     fprintf(c_file, "{\n");
116     fprintf(c_file,
117             "    init_error_table(%s_error_strings, ERROR_TABLE_BASE_%s, "
118             "num_errors);\n", name, name);
119     fprintf(c_file, "}\n");
120
121     fclose(c_file);
122     return 0;
123 }
124
125 static int
126 generate_h(void)
127 {
128     struct error_code *ec;
129     char fn[128];
130     FILE *h_file = fopen(hfn, "w");
131     char *p;
132
133     if(h_file == NULL)
134         return 1;
135
136     snprintf(fn, sizeof(fn), "__%s__", hfn);
137     for(p = fn; *p; p++)
138         if(!isalnum((unsigned char)*p))
139             *p = '_';
140     
141     fprintf(h_file, "/* Generated from %s */\n", filename);
142     if(id_str) 
143         fprintf(h_file, "/* %s */\n", id_str);
144     fprintf(h_file, "\n");
145     fprintf(h_file, "#ifndef %s\n", fn);
146     fprintf(h_file, "#define %s\n", fn);
147     fprintf(h_file, "\n");
148     fprintf(h_file, "struct et_list;\n");
149     fprintf(h_file, "\n");
150     fprintf(h_file, 
151             "void initialize_%s_error_table_r(struct et_list **);\n",
152             name);
153     fprintf(h_file, "\n");
154     fprintf(h_file, "void initialize_%s_error_table(void);\n", name);
155     fprintf(h_file, "#define init_%s_err_tbl initialize_%s_error_table\n", 
156             name, name);
157     fprintf(h_file, "\n");
158     fprintf(h_file, "typedef enum %s_error_number{\n", name);
159
160     for(ec = codes; ec; ec = ec->next) {
161         fprintf(h_file, "\t%s = %ld%s\n", ec->name, base_id + ec->number, 
162                 (ec->next != NULL) ? "," : "");
163     }
164
165     fprintf(h_file, "} %s_error_number;\n", name);
166     fprintf(h_file, "\n");
167     fprintf(h_file, "#define ERROR_TABLE_BASE_%s %ld\n", name, base_id);
168     fprintf(h_file, "\n");
169     fprintf(h_file, "#endif /* %s */\n", fn);
170
171
172     fclose(h_file);
173     return 0;
174 }
175
176 static int
177 generate(void)
178 {
179     return generate_c() || generate_h();
180 }
181
182 int help_flag;
183 struct getargs args[] = {
184     { "help", 0, arg_flag, &help_flag }
185 };
186 int num_args = sizeof(args) / sizeof(args[0]);
187
188 static void
189 usage(int code)
190 {
191     arg_printusage(args, num_args, NULL, "error-table");
192     exit(code);
193 }
194
195 int
196 main(int argc, char **argv)
197 {
198     char *p;
199     int optidx = 0;
200
201     setprogname(argv[0]);
202     if(getarg(args, num_args, argc, argv, &optidx))
203         usage(1);
204     if(help_flag)
205         usage(0);
206
207     if(optidx == argc) 
208         usage(1);
209     filename = argv[optidx];
210     yyin = fopen(filename, "r");
211     if(yyin == NULL)
212         err(1, "%s", filename);
213         
214     
215     p = strrchr(filename, '/');
216     if(p)
217         p++;
218     else
219         p = filename;
220     strlcpy(Basename, p, sizeof(Basename));
221     
222     Basename[strcspn(Basename, ".")] = '\0';
223     
224     snprintf(hfn, sizeof(hfn), "%s.h", Basename);
225     snprintf(cfn, sizeof(cfn), "%s.c", Basename);
226     
227     yyparse();
228     if(numerror)
229         return 1;
230
231     return generate();
232 }