Deleted Added
full compact
parse.y (43940) parse.y (43943)
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 *
28 * $Id: parse.y,v 1.11 1998/12/06 22:58:17 archie Exp $
28 * $Id: parse.y,v 1.12 1999/02/12 20:39:05 ache Exp $
29 */
30
31#include <err.h>
32#include <stdarg.h>
33#include <stdio.h>
34#include <string.h>
35#include <stdlib.h>
36#include <unistd.h>
37#include <sysexits.h>
38#include "collate.h"
39
40extern int line_no;
41extern FILE *yyin;
42void yyerror(char *fmt, ...);
43int yyparse(void);
44int yylex(void);
45static void usage __P((void));
46
47char map_name[FILENAME_MAX] = ".";
48
49char __collate_version[STR_LEN];
50u_char charmap_table[UCHAR_MAX + 1][STR_LEN];
51u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
52struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
53struct __collate_st_chain_pri __collate_chain_pri_table[TABLE_SIZE];
54int chain_index;
55int prim_pri = 1, sec_pri = 1;
56#ifdef COLLATE_DEBUG
57int debug;
58#endif
59
60char *out_file = "LC_COLLATE";
61%}
62%union {
63 u_char ch;
64 u_char str[STR_LEN];
65}
66%token SUBSTITUTE WITH ORDER RANGE
67%token <str> STRING
68%token <str> CHAIN
69%token <str> DEFN
70%token <ch> CHAR
71%%
72collate : statment_list
73;
74statment_list : statment
75 | statment_list '\n' statment
76;
77statment :
78 | charmap
79 | substitute
80 | order
81;
82charmap : DEFN CHAR {
83 strcpy(charmap_table[$2], $1);
84}
85;
86substitute : SUBSTITUTE STRING WITH STRING {
87 u_char ch = $2[0];
88
29 */
30
31#include <err.h>
32#include <stdarg.h>
33#include <stdio.h>
34#include <string.h>
35#include <stdlib.h>
36#include <unistd.h>
37#include <sysexits.h>
38#include "collate.h"
39
40extern int line_no;
41extern FILE *yyin;
42void yyerror(char *fmt, ...);
43int yyparse(void);
44int yylex(void);
45static void usage __P((void));
46
47char map_name[FILENAME_MAX] = ".";
48
49char __collate_version[STR_LEN];
50u_char charmap_table[UCHAR_MAX + 1][STR_LEN];
51u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
52struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
53struct __collate_st_chain_pri __collate_chain_pri_table[TABLE_SIZE];
54int chain_index;
55int prim_pri = 1, sec_pri = 1;
56#ifdef COLLATE_DEBUG
57int debug;
58#endif
59
60char *out_file = "LC_COLLATE";
61%}
62%union {
63 u_char ch;
64 u_char str[STR_LEN];
65}
66%token SUBSTITUTE WITH ORDER RANGE
67%token <str> STRING
68%token <str> CHAIN
69%token <str> DEFN
70%token <ch> CHAR
71%%
72collate : statment_list
73;
74statment_list : statment
75 | statment_list '\n' statment
76;
77statment :
78 | charmap
79 | substitute
80 | order
81;
82charmap : DEFN CHAR {
83 strcpy(charmap_table[$2], $1);
84}
85;
86substitute : SUBSTITUTE STRING WITH STRING {
87 u_char ch = $2[0];
88
89 if (strlen($2) > 1)
90 yyerror("Only characters can be substituted, not strings");
91 if (ch == '\0')
92 yyerror("NUL character can't be substituted");
89 if (strchr($4, ch) != NULL)
90 yyerror("Char 0x%02x substitution is recursive", ch);
91 strcpy(__collate_substitute_table[ch], $4);
92}
93;
94order : ORDER order_list {
95 FILE *fp;
93 if (strchr($4, ch) != NULL)
94 yyerror("Char 0x%02x substitution is recursive", ch);
95 strcpy(__collate_substitute_table[ch], $4);
96}
97;
98order : ORDER order_list {
99 FILE *fp;
96 int ch;
100 int ch, substed, ordered;
97
101
98 for (ch = 0; ch < UCHAR_MAX + 1; ch++)
99 if ( !__collate_char_pri_table[ch].prim
100 && __collate_substitute_table[ch][0] == ch
101 && __collate_substitute_table[ch][1] == '\0'
102 )
102 for (ch = 0; ch < UCHAR_MAX + 1; ch++) {
103 substed = (__collate_substitute_table[ch][0] != ch);
104 ordered = !!__collate_char_pri_table[ch].prim;
105 if (!ordered && !substed)
103 yyerror("Char 0x%02x not found", ch);
106 yyerror("Char 0x%02x not found", ch);
107 if (substed && ordered)
108 yyerror("Char 0x%02x can't be ordered since substituted", ch);
109 }
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