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. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Kungliga Tekniska 20 * H�gskolan and its contributors. 21 * 22 * 4. Neither the name of the Institute nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #undef ROKEN_RENAME 40 #include "compile_et.h" 41 #include <getarg.h> 42 43 #if 0 44 RCSID("$Id: compile_et.c,v 1.12 1999/04/01 09:13:52 joda Exp $"); 45 #endif 46 47 #include <err.h> 48 #include "parse.h" 49 50 int numerror; 51 extern FILE *yyin; 52 53 extern void yyparse(void); 54 55 long base; 56 int number; 57 char *prefix; 58 char *id_str; 59 60 char name[128]; 61 char Basename[128]; 62 63 #ifdef YYDEBUG 64 extern int yydebug = 1; 65 #endif 66 67 char *filename; 68 char hfn[128]; 69 char cfn[128]; 70 71 struct error_code *codes = NULL; 72 73 static int 74 generate_c(void) 75 { 76 int n; 77 struct error_code *ec; 78 79 FILE *c_file = fopen(cfn, "w"); 80 if(c_file == NULL) 81 return 1; 82 83 fprintf(c_file, "/* Generated from %s */\n", filename); 84 if(id_str) 85 fprintf(c_file, "/* %s */\n", id_str); 86 fprintf(c_file, "\n"); 87 fprintf(c_file, "#include <stddef.h>\n"); 88 fprintf(c_file, "#include <com_err.h>\n"); 89 fprintf(c_file, "#include \"%s\"\n", hfn); 90 fprintf(c_file, "\n"); 91 92 fprintf(c_file, "static const char *text[] = {\n"); 93 94 for(ec = codes, n = 0; ec; ec = ec->next, n++) { 95 while(n < ec->number) { 96 fprintf(c_file, "\t/* %03d */ \"Reserved %s error (%d)\",\n", 97 n, name, n); 98 n++; 99 100 } 101 fprintf(c_file, "\t/* %03d */ \"%s\",\n", ec->number, ec->string); 102 } 103 104 fprintf(c_file, "\tNULL\n"); 105 fprintf(c_file, "};\n"); 106 fprintf(c_file, "\n"); 107 fprintf(c_file, 108 "void initialize_%s_error_table_r(struct et_list **list)\n", 109 name); 110 fprintf(c_file, "{\n"); 111 fprintf(c_file, 112 " initialize_error_table_r(list, text, " 113 "%s_num_errors, ERROR_TABLE_BASE_%s);\n", name, name); 114 fprintf(c_file, "}\n"); 115 fprintf(c_file, "\n"); 116 fprintf(c_file, "void initialize_%s_error_table(void)\n", name); 117 fprintf(c_file, "{\n"); 118 fprintf(c_file, 119 " init_error_table(text, ERROR_TABLE_BASE_%s, " 120 "%s_num_errors);\n", name, name); 121 fprintf(c_file, "}\n"); 122 123 fclose(c_file); 124 return 0; 125 } 126 127 static int 128 generate_h(void) 129 { 130 struct error_code *ec; 131 char fn[128]; 132 FILE *h_file = fopen(hfn, "w"); 133 char *p; 134 135 if(h_file == NULL) 136 return 1; 137 138 snprintf(fn, sizeof(fn), "__%s__", hfn); 139 for(p = fn; *p; p++) 140 if(!isalnum((unsigned char)*p)) 141 *p = '_'; 142 143 fprintf(h_file, "/* Generated from %s */\n", filename); 144 if(id_str) 145 fprintf(h_file, "/* %s */\n", id_str); 146 fprintf(h_file, "\n"); 147 fprintf(h_file, "#ifndef %s\n", fn); 148 fprintf(h_file, "#define %s\n", fn); 149 fprintf(h_file, "\n"); 150 fprintf(h_file, "#include <com_right.h>\n"); 151 fprintf(h_file, "\n"); 152 fprintf(h_file, 153 "void initialize_%s_error_table_r(struct et_list **);\n", 154 name); 155 fprintf(h_file, "\n"); 156 fprintf(h_file, "void initialize_%s_error_table(void);\n", name); 157 fprintf(h_file, "#define init_%s_err_tbl initialize_%s_error_table\n", 158 name, name); 159 fprintf(h_file, "\n"); 160 fprintf(h_file, "typedef enum %s_error_number{\n", name); 161 fprintf(h_file, "\tERROR_TABLE_BASE_%s = %ld,\n", name, base); 162 fprintf(h_file, "\t%s_err_base = %ld,\n", name, base); 163 164 for(ec = codes; ec; ec = ec->next) { 165 fprintf(h_file, "\t%s = %ld,\n", ec->name, base + ec->number); 166 } 167 168 fprintf(h_file, "\t%s_num_errors = %d\n", name, number); 169 fprintf(h_file, "} %s_error_number;\n", name); 170 fprintf(h_file, "\n"); 171 fprintf(h_file, "#endif /* %s */\n", fn); 172 173 174 fclose(h_file); 175 return 0; 176 } 177 178 static int 179 generate(void) 180 { 181 return generate_c() || generate_h(); 182 } 183 184 int help_flag; 185 struct getargs args[] = { 186 { "help", 0, arg_flag, &help_flag } 187 }; 188 int num_args = sizeof(args) / sizeof(args[0]); 189 190 static void 191 usage(int code) 192 { 193 arg_printusage(args, num_args, NULL, "error-table"); 194 exit(code); 195 } 196 197 int 198 main(int argc, char **argv) 199 { 200 char *p; 201 int optind = 0; 202 203 if(getarg(args, num_args, argc, argv, &optind)) 204 usage(1); 205 if(help_flag) 206 usage(0); 207 208 if(optind == argc) 209 usage(1); 210 filename = argv[optind]; 211 yyin = fopen(filename, "r"); 212 if(yyin == NULL) 213 err(1, "%s", filename); 214 215 216 p = strrchr(filename, '/'); 217 if(p) 218 p++; 219 else 220 p = filename; 221 strncpy(Basename, p, sizeof(Basename)); 222 Basename[sizeof(Basename) - 1] = '\0'; 223 224 Basename[strcspn(Basename, ".")] = '\0'; 225 226 snprintf(hfn, sizeof(hfn), "%s.h", Basename); 227 snprintf(cfn, sizeof(cfn), "%s.c", Basename); 228 229 yyparse(); 230 if(numerror) 231 return 1; 232 233 return generate(); 234 } 235