parse.y revision 102299
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 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/usr.bin/colldef/parse.y 102299 2002-08-23 04:18:26Z ache $"); 31 32#include <err.h> 33#include <stdarg.h> 34#include <stdio.h> 35#include <string.h> 36#include <stdlib.h> 37#include <unistd.h> 38#include <sysexits.h> 39#include "collate.h" 40#include "common.h" 41 42extern FILE *yyin; 43void yyerror(const char *fmt, ...) __printflike(1, 2); 44int yyparse(void); 45int yylex(void); 46static void usage(void); 47static void collate_print_tables(void); 48 49char map_name[FILENAME_MAX] = "."; 50char curr_chain[STR_LEN]; 51 52char __collate_version[STR_LEN]; 53u_char charmap_table[UCHAR_MAX + 1][CHARMAP_SYMBOL_LEN]; 54 55#undef __collate_substitute_table 56u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN]; 57#undef __collate_char_pri_table 58struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1]; 59#undef __collate_chain_pri_table 60struct __collate_st_chain_pri __collate_chain_pri_table[TABLE_SIZE]; 61 62int chain_index; 63int prim_pri = 1, sec_pri = 1; 64#ifdef COLLATE_DEBUG 65int debug; 66#endif 67 68const char *out_file = "LC_COLLATE"; 69%} 70%union { 71 u_char ch; 72 u_char str[BUFSIZE]; 73} 74%token SUBSTITUTE WITH ORDER RANGE 75%token <str> STRING 76%token <str> DEFN 77%token <ch> CHAR 78%% 79collate : statment_list 80; 81statment_list : statment 82 | statment_list '\n' statment 83; 84statment : 85 | charmap 86 | substitute 87 | order 88; 89charmap : DEFN CHAR { 90 if (strlen($1) + 1 > CHARMAP_SYMBOL_LEN) 91 yyerror("Charmap symbol name '%s' is too long", $1); 92 strcpy(charmap_table[$2], $1); 93} 94; 95substitute : SUBSTITUTE CHAR WITH STRING { 96 if ($2 == '\0') 97 yyerror("NUL character can't be substituted"); 98 if (strchr($4, $2) != NULL) 99 yyerror("Char 0x%02x substitution is recursive", $2); 100 if (strlen($4) + 1 > STR_LEN) 101 yyerror("Char 0x%02x substitution is too long", $2); 102 strcpy(__collate_substitute_table[$2], $4); 103} 104; 105order : ORDER order_list { 106 FILE *fp; 107 int ch, substed, ordered; 108 109 for (ch = 0; ch < UCHAR_MAX + 1; ch++) { 110 substed = (__collate_substitute_table[ch][0] != ch); 111 ordered = !!__collate_char_pri_table[ch].prim; 112 if (!ordered && !substed) 113 yyerror("Char 0x%02x not found", ch); 114 if (substed && ordered) 115 yyerror("Char 0x%02x can't be ordered since substituted", ch); 116 } 117 118 fp = fopen(out_file, "w"); 119 if(!fp) 120 err(EX_UNAVAILABLE, "can't open destination file %s", 121 out_file); 122 123 strcpy(__collate_version, COLLATE_VERSION); 124 fwrite(__collate_version, sizeof(__collate_version), 1, fp); 125 fwrite(__collate_substitute_table, sizeof(__collate_substitute_table), 1, fp); 126 fwrite(__collate_char_pri_table, sizeof(__collate_char_pri_table), 1, fp); 127 fwrite(__collate_chain_pri_table, sizeof(__collate_chain_pri_table), 1, fp); 128 if (fflush(fp)) 129 err(EX_UNAVAILABLE, "IO error writting to destination file %s", 130 out_file); 131 fclose(fp); 132#ifdef COLLATE_DEBUG 133 if (debug) 134 collate_print_tables(); 135#endif 136 exit(EX_OK); 137} 138; 139order_list : item 140 | order_list ';' item 141; 142chain : CHAR CHAR { 143 curr_chain[0] = $1; 144 curr_chain[1] = $2; 145 if (curr_chain[0] == '\0' || curr_chain[1] == '\0') 146 yyerror("\\0 can't be chained"); 147 curr_chain[2] = '\0'; 148} 149 | chain CHAR { 150 static char tb[2]; 151 152 tb[0] = $2; 153 if (tb[0] == '\0') 154 yyerror("\\0 can't be chained"); 155 if (strlen(curr_chain) + 2 > STR_LEN) 156 yyerror("Chain '%s' grows too long", curr_chain); 157 (void)strcat(curr_chain, tb); 158} 159; 160item : CHAR { 161 if (__collate_char_pri_table[$1].prim) 162 yyerror("Char 0x%02x duplicated", $1); 163 __collate_char_pri_table[$1].prim = prim_pri++; 164} 165 | chain { 166 if (chain_index >= TABLE_SIZE - 1) 167 yyerror("__collate_chain_pri_table overflow"); 168 (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain); 169 __collate_chain_pri_table[chain_index++].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", $1, $3); 176 177 for (i = $1; i <= $3; i++) { 178 if (__collate_char_pri_table[(u_char)i].prim) 179 yyerror("Char 0x%02x duplicated", (u_char)i); 180 __collate_char_pri_table[(u_char)i].prim = prim_pri++; 181 } 182} 183 | '{' prim_order_list '}' { 184 prim_pri++; 185} 186 | '(' sec_order_list ')' { 187 prim_pri++; 188 sec_pri = 1; 189} 190; 191prim_order_list : prim_sub_item 192 | prim_order_list ',' prim_sub_item 193; 194sec_order_list : sec_sub_item 195 | sec_order_list ',' sec_sub_item 196; 197prim_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} 202 | CHAR RANGE CHAR { 203 u_int i; 204 205 if ($3 <= $1) 206 yyerror("Illegal range 0x%02x -- 0x%02x", 207 $1, $3); 208 209 for (i = $1; i <= $3; i++) { 210 if (__collate_char_pri_table[(u_char)i].prim) 211 yyerror("Char 0x%02x duplicated", (u_char)i); 212 __collate_char_pri_table[(u_char)i].prim = prim_pri; 213 } 214} 215 | chain { 216 if (chain_index >= TABLE_SIZE - 1) 217 yyerror("__collate_chain_pri_table overflow"); 218 (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain); 219 __collate_chain_pri_table[chain_index++].prim = prim_pri; 220} 221; 222sec_sub_item : CHAR { 223 if (__collate_char_pri_table[$1].prim) 224 yyerror("Char 0x%02x duplicated", $1); 225 __collate_char_pri_table[$1].prim = prim_pri; 226 __collate_char_pri_table[$1].sec = sec_pri++; 227} 228 | CHAR RANGE CHAR { 229 u_int i; 230 231 if ($3 <= $1) 232 yyerror("Illegal range 0x%02x -- 0x%02x", 233 $1, $3); 234 235 for (i = $1; i <= $3; i++) { 236 if (__collate_char_pri_table[(u_char)i].prim) 237 yyerror("Char 0x%02x duplicated", (u_char)i); 238 __collate_char_pri_table[(u_char)i].prim = prim_pri; 239 __collate_char_pri_table[(u_char)i].sec = sec_pri++; 240 } 241} 242 | chain { 243 if (chain_index >= TABLE_SIZE - 1) 244 yyerror("__collate_chain_pri_table overflow"); 245 (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain); 246 __collate_chain_pri_table[chain_index].prim = prim_pri; 247 __collate_chain_pri_table[chain_index++].sec = sec_pri++; 248} 249; 250%% 251int 252main(int ac, char **av) 253{ 254 int ch; 255 256#ifdef COLLATE_DEBUG 257 while((ch = getopt(ac, av, ":do:I:")) != EOF) { 258#else 259 while((ch = getopt(ac, av, ":o:I:")) != EOF) { 260#endif 261 switch (ch) 262 { 263#ifdef COLLATE_DEBUG 264 case 'd': 265 debug++; 266 break; 267#endif 268 case 'o': 269 out_file = optarg; 270 break; 271 272 case 'I': 273 strlcpy(map_name, optarg, sizeof(map_name)); 274 break; 275 276 default: 277 usage(); 278 } 279 } 280 ac -= optind; 281 av += optind; 282 if(ac > 0) { 283 if((yyin = fopen(*av, "r")) == 0) 284 err(EX_UNAVAILABLE, "can't open source file %s", *av); 285 } 286 for(ch = 0; ch <= UCHAR_MAX; ch++) 287 __collate_substitute_table[ch][0] = ch; 288 yyparse(); 289 return 0; 290} 291 292static void 293usage(void) 294{ 295 fprintf(stderr, "usage: colldef [-o out_file] [-I map_dir] [filename]\n"); 296 exit(EX_USAGE); 297} 298 299void 300yyerror(const char *fmt, ...) 301{ 302 va_list ap; 303 char msg[128]; 304 305 va_start(ap, fmt); 306 vsnprintf(msg, sizeof(msg), fmt, ap); 307 va_end(ap); 308 errx(EX_UNAVAILABLE, "%s near line %d", msg, line_no); 309} 310 311#ifdef COLLATE_DEBUG 312static void 313collate_print_tables(void) 314{ 315 int i; 316 struct __collate_st_chain_pri *p2; 317 318 printf("Substitute table:\n"); 319 for (i = 0; i < UCHAR_MAX + 1; i++) 320 if (i != *__collate_substitute_table[i]) 321 printf("\t'%c' --> \"%s\"\n", i, 322 __collate_substitute_table[i]); 323 printf("Chain priority table:\n"); 324 for (p2 = __collate_chain_pri_table; p2->str[0]; p2++) 325 printf("\t\"%s\" : %d %d\n\n", p2->str, p2->prim, p2->sec); 326 printf("Char priority table:\n"); 327 for (i = 0; i < UCHAR_MAX + 1; i++) 328 printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim, 329 __collate_char_pri_table[i].sec); 330} 331#endif 332