1%{ 2/*- 3 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua> 4 * at Electronni Visti IA, Kiev, Ukraine. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 *
| 1%{ 2/*- 3 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua> 4 * at Electronni Visti IA, Kiev, Ukraine. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 *
|
104 105 fp = fopen(out_file, "w"); 106 if(!fp) 107 err(EX_UNAVAILABLE, "can't open destination file %s", 108 out_file); 109 110 strcpy(__collate_version, COLLATE_VERSION); 111 fwrite(__collate_version, sizeof(__collate_version), 1, fp); 112 fwrite(__collate_substitute_table, sizeof(__collate_substitute_table), 1, fp); 113 fwrite(__collate_char_pri_table, sizeof(__collate_char_pri_table), 1, fp); 114 fwrite(__collate_chain_pri_table, sizeof(__collate_chain_pri_table), 1, fp); 115 if (fflush(fp)) 116 err(EX_UNAVAILABLE, "IO error writting to destination file %s", 117 out_file); 118 fclose(fp); 119#ifdef COLLATE_DEBUG 120 if (debug) 121 collate_print_tables(); 122#endif 123 exit(EX_OK); 124} 125; 126order_list : item 127 | order_list ';' item 128; 129item : CHAR { 130 if (__collate_char_pri_table[$1].prim) 131 yyerror("Char 0x%02x duplicated", $1); 132 __collate_char_pri_table[$1].prim = prim_pri++; 133} 134 | CHAIN { 135 if (chain_index >= TABLE_SIZE - 1) 136 yyerror("__collate_chain_pri_table overflow"); 137 strcpy(__collate_chain_pri_table[chain_index].str, $1); 138 __collate_chain_pri_table[chain_index++].prim = prim_pri++; 139} 140 | CHAR RANGE CHAR { 141 u_int i; 142 143 if ($3 <= $1) 144 yyerror("Illegal range 0x%02x -- 0x%02x", $1, $3); 145 146 for (i = $1; i <= $3; i++) { 147 if (__collate_char_pri_table[(u_char)i].prim) 148 yyerror("Char 0x%02x duplicated", (u_char)i); 149 __collate_char_pri_table[(u_char)i].prim = prim_pri++; 150 } 151} 152 | '{' prim_order_list '}' { 153 prim_pri++; 154} 155 | '(' sec_order_list ')' { 156 prim_pri++; 157 sec_pri = 1; 158} 159; 160prim_order_list : prim_sub_item 161 | prim_order_list ',' prim_sub_item 162; 163sec_order_list : sec_sub_item 164 | sec_order_list ',' sec_sub_item 165; 166prim_sub_item : CHAR { 167 if (__collate_char_pri_table[$1].prim) 168 yyerror("Char 0x%02x duplicated", $1); 169 __collate_char_pri_table[$1].prim = prim_pri; 170} 171 | CHAR RANGE CHAR { 172 u_int i; 173 174 if ($3 <= $1) 175 yyerror("Illegal range 0x%02x -- 0x%02x", 176 $1, $3); 177 178 for (i = $1; i <= $3; i++) { 179 if (__collate_char_pri_table[(u_char)i].prim) 180 yyerror("Char 0x%02x duplicated", (u_char)i); 181 __collate_char_pri_table[(u_char)i].prim = prim_pri; 182 } 183} 184 | CHAIN { 185 if (chain_index >= TABLE_SIZE - 1) 186 yyerror("__collate_chain_pri_table overflow"); 187 strcpy(__collate_chain_pri_table[chain_index].str, $1); 188 __collate_chain_pri_table[chain_index++].prim = prim_pri; 189} 190; 191sec_sub_item : CHAR { 192 if (__collate_char_pri_table[$1].prim) 193 yyerror("Char 0x%02x duplicated", $1); 194 __collate_char_pri_table[$1].prim = prim_pri; 195 __collate_char_pri_table[$1].sec = sec_pri++; 196} 197 | CHAR RANGE CHAR { 198 u_int i; 199 200 if ($3 <= $1) 201 yyerror("Illegal range 0x%02x -- 0x%02x", 202 $1, $3); 203 204 for (i = $1; i <= $3; i++) { 205 if (__collate_char_pri_table[(u_char)i].prim) 206 yyerror("Char 0x%02x duplicated", (u_char)i); 207 __collate_char_pri_table[(u_char)i].prim = prim_pri; 208 __collate_char_pri_table[(u_char)i].sec = sec_pri++; 209 } 210} 211 | CHAIN { 212 if (chain_index >= TABLE_SIZE - 1) 213 yyerror("__collate_chain_pri_table overflow"); 214 strcpy(__collate_chain_pri_table[chain_index].str, $1); 215 __collate_chain_pri_table[chain_index].prim = prim_pri; 216 __collate_chain_pri_table[chain_index++].sec = sec_pri++; 217} 218; 219%% 220int 221main(ac, av) 222 char **av; 223{ 224 int ch; 225 226#ifdef COLLATE_DEBUG 227 while((ch = getopt(ac, av, ":do:I:")) != EOF) { 228#else 229 while((ch = getopt(ac, av, ":o:I:")) != EOF) { 230#endif 231 switch (ch) 232 { 233#ifdef COLLATE_DEBUG 234 case 'd': 235 debug++; 236 break; 237#endif 238 case 'o': 239 out_file = optarg; 240 break; 241 242 case 'I': 243 strcpy(map_name, optarg); 244 break; 245 246 default: 247 usage(); 248 } 249 } 250 ac -= optind; 251 av += optind; 252 if(ac > 0) { 253 if((yyin = fopen(*av, "r")) == 0) 254 err(EX_UNAVAILABLE, "can't open source file %s", *av); 255 } 256 for(ch = 0; ch <= UCHAR_MAX; ch++) 257 __collate_substitute_table[ch][0] = ch; 258 yyparse(); 259 return 0; 260} 261 262static void 263usage() 264{ 265 fprintf(stderr, "usage: colldef [-o out_file] [-I map_dir] [filename]\n"); 266 exit(EX_USAGE); 267} 268 269void yyerror(char *fmt, ...) 270{ 271 va_list ap; 272 char msg[128]; 273 274 va_start(ap, fmt); 275 vsprintf(msg, fmt, ap); 276 va_end(ap); 277 errx(EX_UNAVAILABLE, "%s near line %d", msg, line_no); 278} 279 280#ifdef COLLATE_DEBUG 281void 282collate_print_tables() 283{ 284 int i; 285 struct __collate_st_chain_pri *p2; 286 287 printf("Substitute table:\n"); 288 for (i = 0; i < UCHAR_MAX + 1; i++) 289 if (i != *__collate_substitute_table[i]) 290 printf("\t'%c' --> \"%s\"\n", i, 291 __collate_substitute_table[i]); 292 printf("Chain priority table:\n"); 293 for (p2 = __collate_chain_pri_table; p2->str[0]; p2++) 294 printf("\t\"%s\" : %d %d\n\n", p2->str, p2->prim, p2->sec); 295 printf("Char priority table:\n"); 296 for (i = 0; i < UCHAR_MAX + 1; i++) 297 printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim, 298 __collate_char_pri_table[i].sec); 299} 300#endif
| 110 111 fp = fopen(out_file, "w"); 112 if(!fp) 113 err(EX_UNAVAILABLE, "can't open destination file %s", 114 out_file); 115 116 strcpy(__collate_version, COLLATE_VERSION); 117 fwrite(__collate_version, sizeof(__collate_version), 1, fp); 118 fwrite(__collate_substitute_table, sizeof(__collate_substitute_table), 1, fp); 119 fwrite(__collate_char_pri_table, sizeof(__collate_char_pri_table), 1, fp); 120 fwrite(__collate_chain_pri_table, sizeof(__collate_chain_pri_table), 1, fp); 121 if (fflush(fp)) 122 err(EX_UNAVAILABLE, "IO error writting to destination file %s", 123 out_file); 124 fclose(fp); 125#ifdef COLLATE_DEBUG 126 if (debug) 127 collate_print_tables(); 128#endif 129 exit(EX_OK); 130} 131; 132order_list : item 133 | order_list ';' item 134; 135item : CHAR { 136 if (__collate_char_pri_table[$1].prim) 137 yyerror("Char 0x%02x duplicated", $1); 138 __collate_char_pri_table[$1].prim = prim_pri++; 139} 140 | CHAIN { 141 if (chain_index >= TABLE_SIZE - 1) 142 yyerror("__collate_chain_pri_table overflow"); 143 strcpy(__collate_chain_pri_table[chain_index].str, $1); 144 __collate_chain_pri_table[chain_index++].prim = prim_pri++; 145} 146 | CHAR RANGE CHAR { 147 u_int i; 148 149 if ($3 <= $1) 150 yyerror("Illegal range 0x%02x -- 0x%02x", $1, $3); 151 152 for (i = $1; i <= $3; i++) { 153 if (__collate_char_pri_table[(u_char)i].prim) 154 yyerror("Char 0x%02x duplicated", (u_char)i); 155 __collate_char_pri_table[(u_char)i].prim = prim_pri++; 156 } 157} 158 | '{' prim_order_list '}' { 159 prim_pri++; 160} 161 | '(' sec_order_list ')' { 162 prim_pri++; 163 sec_pri = 1; 164} 165; 166prim_order_list : prim_sub_item 167 | prim_order_list ',' prim_sub_item 168; 169sec_order_list : sec_sub_item 170 | sec_order_list ',' sec_sub_item 171; 172prim_sub_item : CHAR { 173 if (__collate_char_pri_table[$1].prim) 174 yyerror("Char 0x%02x duplicated", $1); 175 __collate_char_pri_table[$1].prim = prim_pri; 176} 177 | CHAR RANGE CHAR { 178 u_int i; 179 180 if ($3 <= $1) 181 yyerror("Illegal range 0x%02x -- 0x%02x", 182 $1, $3); 183 184 for (i = $1; i <= $3; i++) { 185 if (__collate_char_pri_table[(u_char)i].prim) 186 yyerror("Char 0x%02x duplicated", (u_char)i); 187 __collate_char_pri_table[(u_char)i].prim = prim_pri; 188 } 189} 190 | CHAIN { 191 if (chain_index >= TABLE_SIZE - 1) 192 yyerror("__collate_chain_pri_table overflow"); 193 strcpy(__collate_chain_pri_table[chain_index].str, $1); 194 __collate_chain_pri_table[chain_index++].prim = prim_pri; 195} 196; 197sec_sub_item : CHAR { 198 if (__collate_char_pri_table[$1].prim) 199 yyerror("Char 0x%02x duplicated", $1); 200 __collate_char_pri_table[$1].prim = prim_pri; 201 __collate_char_pri_table[$1].sec = sec_pri++; 202} 203 | CHAR RANGE CHAR { 204 u_int i; 205 206 if ($3 <= $1) 207 yyerror("Illegal range 0x%02x -- 0x%02x", 208 $1, $3); 209 210 for (i = $1; i <= $3; i++) { 211 if (__collate_char_pri_table[(u_char)i].prim) 212 yyerror("Char 0x%02x duplicated", (u_char)i); 213 __collate_char_pri_table[(u_char)i].prim = prim_pri; 214 __collate_char_pri_table[(u_char)i].sec = sec_pri++; 215 } 216} 217 | CHAIN { 218 if (chain_index >= TABLE_SIZE - 1) 219 yyerror("__collate_chain_pri_table overflow"); 220 strcpy(__collate_chain_pri_table[chain_index].str, $1); 221 __collate_chain_pri_table[chain_index].prim = prim_pri; 222 __collate_chain_pri_table[chain_index++].sec = sec_pri++; 223} 224; 225%% 226int 227main(ac, av) 228 char **av; 229{ 230 int ch; 231 232#ifdef COLLATE_DEBUG 233 while((ch = getopt(ac, av, ":do:I:")) != EOF) { 234#else 235 while((ch = getopt(ac, av, ":o:I:")) != EOF) { 236#endif 237 switch (ch) 238 { 239#ifdef COLLATE_DEBUG 240 case 'd': 241 debug++; 242 break; 243#endif 244 case 'o': 245 out_file = optarg; 246 break; 247 248 case 'I': 249 strcpy(map_name, optarg); 250 break; 251 252 default: 253 usage(); 254 } 255 } 256 ac -= optind; 257 av += optind; 258 if(ac > 0) { 259 if((yyin = fopen(*av, "r")) == 0) 260 err(EX_UNAVAILABLE, "can't open source file %s", *av); 261 } 262 for(ch = 0; ch <= UCHAR_MAX; ch++) 263 __collate_substitute_table[ch][0] = ch; 264 yyparse(); 265 return 0; 266} 267 268static void 269usage() 270{ 271 fprintf(stderr, "usage: colldef [-o out_file] [-I map_dir] [filename]\n"); 272 exit(EX_USAGE); 273} 274 275void yyerror(char *fmt, ...) 276{ 277 va_list ap; 278 char msg[128]; 279 280 va_start(ap, fmt); 281 vsprintf(msg, fmt, ap); 282 va_end(ap); 283 errx(EX_UNAVAILABLE, "%s near line %d", msg, line_no); 284} 285 286#ifdef COLLATE_DEBUG 287void 288collate_print_tables() 289{ 290 int i; 291 struct __collate_st_chain_pri *p2; 292 293 printf("Substitute table:\n"); 294 for (i = 0; i < UCHAR_MAX + 1; i++) 295 if (i != *__collate_substitute_table[i]) 296 printf("\t'%c' --> \"%s\"\n", i, 297 __collate_substitute_table[i]); 298 printf("Chain priority table:\n"); 299 for (p2 = __collate_chain_pri_table; p2->str[0]; p2++) 300 printf("\t\"%s\" : %d %d\n\n", p2->str, p2->prim, p2->sec); 301 printf("Char priority table:\n"); 302 for (i = 0; i < UCHAR_MAX + 1; i++) 303 printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim, 304 __collate_char_pri_table[i].sec); 305} 306#endif
|