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