1 /* $NetBSD: compile_et.c,v 1.3 2014/04/24 13:45:34 pettai Exp $ */
4 * Copyright (c) 1998-2002 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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
40 #include "compile_et.h"
62 extern int yydebug
= 1;
69 struct error_code
*codes
= NULL
;
75 struct error_code
*ec
;
77 FILE *c_file
= fopen(cfn
, "w");
81 fprintf(c_file
, "/* Generated from %s */\n", basename(filename
));
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");
92 fprintf(c_file
, "static const char *%s_error_strings[] = {\n", name
);
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",
101 fprintf(c_file
, "\t/* %03d */ N_(\"%s\"),\n",
102 ec
->number
, ec
->string
);
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");
111 "void initialize_%s_error_table_r(struct et_list **list)\n",
113 fprintf(c_file
, "{\n");
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");
122 " init_error_table(%s_error_strings, ERROR_TABLE_BASE_%s, "
123 "num_errors);\n", name
, name
);
124 fprintf(c_file
, "}\n");
133 struct error_code
*ec
;
135 FILE *h_file
= fopen(hfn
, "w");
141 snprintf(fn
, sizeof(fn
), "__%s__", hfn
);
143 if(!isalnum((unsigned char)*p
))
146 fprintf(h_file
, "/* Generated from %s */\n", basename(filename
));
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");
156 "void initialize_%s_error_table_r(struct et_list **);\n",
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",
162 fprintf(h_file
, "\n");
163 fprintf(h_file
, "typedef enum %s_error_number{\n", name
);
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
) ? "," : "");
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
);
186 return generate_c() || generate_h();
191 struct getargs args
[] = {
192 { "version", 0, arg_flag
, &version_flag
},
193 { "help", 0, arg_flag
, &help_flag
}
195 int num_args
= sizeof(args
) / sizeof(args
[0]);
200 arg_printusage(args
, num_args
, NULL
, "error-table");
205 main(int argc
, char **argv
)
210 setprogname(argv
[0]);
211 if(getarg(args
, num_args
, argc
, argv
, &optidx
))
222 filename
= argv
[optidx
];
223 yyin
= fopen(filename
, "r");
225 err(1, "%s", filename
);
228 p
= strrchr(filename
, rk_PATH_DELIM
);
233 strlcpy(Basename
, p
, sizeof(Basename
));
235 Basename
[strcspn(Basename
, ".")] = '\0';
237 snprintf(hfn
, sizeof(hfn
), "%s.h", Basename
);
238 snprintf(cfn
, sizeof(cfn
), "%s.c", Basename
);