2 * Copyright (c) 1998-2002 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
38 #include "compile_et.h"
67 struct error_code
*codes
= NULL
;
73 struct error_code
*ec
;
75 FILE *c_file
= fopen(cfn
, "w");
79 fprintf(c_file
, "/* Generated from %s */\n", filename
);
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 fprintf(c_file
, "#define N_(x) (x)\n");
88 fprintf(c_file
, "\n");
90 fprintf(c_file
, "static const char *const %s_error_strings[] = {\n", name
);
92 for(ec
= codes
, n
= 0; ec
; ec
= ec
->next
, n
++) {
93 while(n
< ec
->number
) {
94 fprintf(c_file
, "\t/* %03d */ \"Reserved %s error (%d)\",\n",
99 fprintf(c_file
, "\t/* %03d */ N_(\"%s\"),\n",
100 ec
->number
, ec
->string
);
103 fprintf(c_file
, "\tNULL\n");
104 fprintf(c_file
, "};\n");
105 fprintf(c_file
, "\n");
106 fprintf(c_file
, "#define num_errors %d\n", number
);
107 fprintf(c_file
, "\n");
109 "void initialize_%s_error_table_r(struct et_list **list)\n",
111 fprintf(c_file
, "{\n");
113 " initialize_error_table_r(list, %s_error_strings, "
114 "num_errors, ERROR_TABLE_BASE_%s);\n", name
, name
);
115 fprintf(c_file
, "}\n");
116 fprintf(c_file
, "\n");
117 fprintf(c_file
, "void initialize_%s_error_table(void)\n", name
);
118 fprintf(c_file
, "{\n");
120 " init_error_table(%s_error_strings, ERROR_TABLE_BASE_%s, "
121 "num_errors);\n", name
, name
);
122 fprintf(c_file
, "}\n");
131 struct error_code
*ec
;
133 FILE *h_file
= fopen(hfn
, "w");
139 snprintf(fn
, sizeof(fn
), "__%s__", hfn
);
141 if(!isalnum((unsigned char)*p
))
144 fprintf(h_file
, "/* Generated from %s */\n", filename
);
146 fprintf(h_file
, "/* %s */\n", id_str
);
147 fprintf(h_file
, "\n");
148 fprintf(h_file
, "#ifndef %s\n", fn
);
149 fprintf(h_file
, "#define %s\n", fn
);
150 fprintf(h_file
, "\n");
151 fprintf(h_file
, "struct et_list;\n");
152 fprintf(h_file
, "\n");
154 "void initialize_%s_error_table_r(struct et_list **);\n",
156 fprintf(h_file
, "\n");
157 fprintf(h_file
, "void initialize_%s_error_table(void);\n", name
);
158 fprintf(h_file
, "#define init_%s_err_tbl initialize_%s_error_table\n",
160 fprintf(h_file
, "\n");
161 fprintf(h_file
, "typedef enum %s_error_number{\n", name
);
163 for(ec
= codes
; ec
; ec
= ec
->next
) {
164 fprintf(h_file
, "\t%s = %ld%s\n", ec
->name
, base_id
+ ec
->number
,
165 (ec
->next
!= NULL
) ? "," : "");
168 fprintf(h_file
, "} %s_error_number;\n", name
);
169 fprintf(h_file
, "\n");
170 fprintf(h_file
, "#define ERROR_TABLE_BASE_%s %ld\n", name
, base_id
);
171 fprintf(h_file
, "\n");
172 fprintf(h_file
, "#define COM_ERR_BINDDOMAIN_%s \"heim_com_err%ld\"\n", name
, base_id
);
173 fprintf(h_file
, "\n");
174 fprintf(h_file
, "#endif /* %s */\n", fn
);
184 return generate_c() || generate_h();
189 struct getargs args
[] = {
190 { "version", 0, arg_flag
, &version_flag
, NULL
, NULL
},
191 { "help", 0, arg_flag
, &help_flag
, NULL
, NULL
}
193 int num_args
= sizeof(args
) / sizeof(args
[0]);
198 arg_printusage(args
, num_args
, NULL
, "error-table");
203 main(int argc
, char **argv
)
208 setprogname(argv
[0]);
209 if(getarg(args
, num_args
, argc
, argv
, &optidx
))
220 filename
= argv
[optidx
];
221 yyin
= fopen(filename
, "r");
223 err(1, "%s", filename
);
226 p
= strrchr(filename
, rk_PATH_DELIM
);
231 strlcpy(Basename
, p
, sizeof(Basename
));
233 Basename
[strcspn(Basename
, ".")] = '\0';
235 snprintf(hfn
, sizeof(hfn
), "%s.h", Basename
);
236 snprintf(cfn
, sizeof(cfn
), "%s.c", Basename
);