Path: blob/main/crypto/heimdal/lib/com_err/compile_et.c
34870 views
/*1* Copyright (c) 1998-2002 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#undef ROKEN_RENAME3435#include "config.h"3637#include "compile_et.h"38#include <getarg.h>3940#include <roken.h>41#include <err.h>42#include "parse.h"4344int numerror;45extern FILE *yyin;4647extern void yyparse(void);4849long base_id;50int number;51char *prefix;52char *id_str;5354char name[128];55char Basename[128];5657#ifdef YYDEBUG58extern int yydebug = 1;59#endif6061char *filename;62char hfn[128];63char cfn[128];6465struct error_code *codes = NULL;6667static int68generate_c(void)69{70int n;71struct error_code *ec;7273FILE *c_file = fopen(cfn, "w");74if(c_file == NULL)75return 1;7677fprintf(c_file, "/* Generated from %s */\n", filename);78if(id_str)79fprintf(c_file, "/* %s */\n", id_str);80fprintf(c_file, "\n");81fprintf(c_file, "#include <stddef.h>\n");82fprintf(c_file, "#include <com_err.h>\n");83fprintf(c_file, "#include \"%s\"\n", hfn);84fprintf(c_file, "\n");85fprintf(c_file, "#define N_(x) (x)\n");86fprintf(c_file, "\n");8788fprintf(c_file, "static const char *%s_error_strings[] = {\n", name);8990for(ec = codes, n = 0; ec; ec = ec->next, n++) {91while(n < ec->number) {92fprintf(c_file, "\t/* %03d */ \"Reserved %s error (%d)\",\n",93n, name, n);94n++;9596}97fprintf(c_file, "\t/* %03d */ N_(\"%s\"),\n",98ec->number, ec->string);99}100101fprintf(c_file, "\tNULL\n");102fprintf(c_file, "};\n");103fprintf(c_file, "\n");104fprintf(c_file, "#define num_errors %d\n", number);105fprintf(c_file, "\n");106fprintf(c_file,107"void initialize_%s_error_table_r(struct et_list **list)\n",108name);109fprintf(c_file, "{\n");110fprintf(c_file,111" initialize_error_table_r(list, %s_error_strings, "112"num_errors, ERROR_TABLE_BASE_%s);\n", name, name);113fprintf(c_file, "}\n");114fprintf(c_file, "\n");115fprintf(c_file, "void initialize_%s_error_table(void)\n", name);116fprintf(c_file, "{\n");117fprintf(c_file,118" init_error_table(%s_error_strings, ERROR_TABLE_BASE_%s, "119"num_errors);\n", name, name);120fprintf(c_file, "}\n");121122fclose(c_file);123return 0;124}125126static int127generate_h(void)128{129struct error_code *ec;130char fn[128];131FILE *h_file = fopen(hfn, "w");132char *p;133134if(h_file == NULL)135return 1;136137snprintf(fn, sizeof(fn), "__%s__", hfn);138for(p = fn; *p; p++)139if(!isalnum((unsigned char)*p))140*p = '_';141142fprintf(h_file, "/* Generated from %s */\n", filename);143if(id_str)144fprintf(h_file, "/* %s */\n", id_str);145fprintf(h_file, "\n");146fprintf(h_file, "#ifndef %s\n", fn);147fprintf(h_file, "#define %s\n", fn);148fprintf(h_file, "\n");149fprintf(h_file, "struct et_list;\n");150fprintf(h_file, "\n");151fprintf(h_file,152"void initialize_%s_error_table_r(struct et_list **);\n",153name);154fprintf(h_file, "\n");155fprintf(h_file, "void initialize_%s_error_table(void);\n", name);156fprintf(h_file, "#define init_%s_err_tbl initialize_%s_error_table\n",157name, name);158fprintf(h_file, "\n");159fprintf(h_file, "typedef enum %s_error_number{\n", name);160161for(ec = codes; ec; ec = ec->next) {162fprintf(h_file, "\t%s = %ld%s\n", ec->name, base_id + ec->number,163(ec->next != NULL) ? "," : "");164}165166fprintf(h_file, "} %s_error_number;\n", name);167fprintf(h_file, "\n");168fprintf(h_file, "#define ERROR_TABLE_BASE_%s %ld\n", name, base_id);169fprintf(h_file, "\n");170fprintf(h_file, "#define COM_ERR_BINDDOMAIN_%s \"heim_com_err%ld\"\n", name, base_id);171fprintf(h_file, "\n");172fprintf(h_file, "#endif /* %s */\n", fn);173174175fclose(h_file);176return 0;177}178179static int180generate(void)181{182return generate_c() || generate_h();183}184185int version_flag;186int help_flag;187struct getargs args[] = {188{ "version", 0, arg_flag, &version_flag },189{ "help", 0, arg_flag, &help_flag }190};191int num_args = sizeof(args) / sizeof(args[0]);192193static void194usage(int code)195{196arg_printusage(args, num_args, NULL, "error-table");197exit(code);198}199200int201main(int argc, char **argv)202{203char *p;204int optidx = 0;205206setprogname(argv[0]);207if(getarg(args, num_args, argc, argv, &optidx))208usage(1);209if(help_flag)210usage(0);211if(version_flag) {212print_version(NULL);213exit(0);214}215216if(optidx == argc)217usage(1);218filename = argv[optidx];219yyin = fopen(filename, "r");220if(yyin == NULL)221err(1, "%s", filename);222223224p = strrchr(filename, rk_PATH_DELIM);225if(p)226p++;227else228p = filename;229strlcpy(Basename, p, sizeof(Basename));230231Basename[strcspn(Basename, ".")] = '\0';232233snprintf(hfn, sizeof(hfn), "%s.h", Basename);234snprintf(cfn, sizeof(cfn), "%s.c", Basename);235236yyparse();237if(numerror)238return 1;239240return generate();241}242243244