LCOV - code coverage report
Current view: top level - source4/heimdal/lib/com_err - compile_et.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 91 104 87.5 %
Date: 2021-09-23 10:06:22 Functions: 4 5 80.0 %

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

Generated by: LCOV version 1.13