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