1 | %{ |
---|
2 | /* Copyright (C) 1988 Tim Shepard All rights reserved. */ |
---|
3 | |
---|
4 | #include <stdio.h> |
---|
5 | #include "synctree.h" |
---|
6 | |
---|
7 | extern rule rules[]; |
---|
8 | extern unsigned int lastrule; |
---|
9 | int lineno; |
---|
10 | char *yyinfilename; |
---|
11 | static char *srcpath; |
---|
12 | static char *dstpath; |
---|
13 | %} |
---|
14 | %union{ |
---|
15 | char *s; |
---|
16 | char c; |
---|
17 | char **svec; |
---|
18 | enum action_type action_type; |
---|
19 | unsigned int bits; |
---|
20 | bool_exp bool_exp; |
---|
21 | } |
---|
22 | %token MAP |
---|
23 | %token COPY LOCAL LINK IGNORE CHASE DELETE |
---|
24 | %token SET UNSET IF ELSE BEGIN_ END |
---|
25 | %token WHEN |
---|
26 | %token GE_END |
---|
27 | %token <c> ALPHANUM ACTIONOPT '/' '{' '}' ',' '?' '*' '[' ']' '!' |
---|
28 | %token <s> MAPDEST BOOLVAR CSH_CMD SH_CMD FILETYPES |
---|
29 | %type <s> pathge srcpathge dstpathge |
---|
30 | %type <s> ge ge1 ge2 chars |
---|
31 | %type <svec> sh_cmds csh_cmds mapdests mapdests1 |
---|
32 | %type <bits> filetypes aopts ifhack elsehack |
---|
33 | %type <action_type> action |
---|
34 | %type <bool_exp> boolexp boolexp1 boolexp0 |
---|
35 | %% |
---|
36 | |
---|
37 | %{ |
---|
38 | #include "lex.yy.c" |
---|
39 | %} |
---|
40 | |
---|
41 | rulefile: rules | |
---|
42 | rules: rules rule | rule |
---|
43 | rule: mrule | arule | wrule | srule | irule | brule |
---|
44 | crules: crules crule | crule |
---|
45 | crule: mrule | arule | wrule | irule | brule |
---|
46 | |
---|
47 | mrule: MAP srcpathge filetypes mapdests { newrule(); |
---|
48 | lstrule.type = R_MAP; |
---|
49 | lstrule.u.u_map.globexp = $2; |
---|
50 | lstrule.u.u_map.file_types = $3; |
---|
51 | lstrule.u.u_map.dests = $4; } |
---|
52 | | CHASE srcpathge filetypes aopts { newrule(); |
---|
53 | lstrule.type = R_CHASE; |
---|
54 | lstrule.u.u_chase.globexp = $2; |
---|
55 | /* filetypes and aopts ignored */ |
---|
56 | } |
---|
57 | arule: action dstpathge filetypes aopts { newrule(); |
---|
58 | lstrule.type = R_ACTION; |
---|
59 | lstrule.u.u_action.type = $1; |
---|
60 | lstrule.u.u_action.globexp = $2; |
---|
61 | lstrule.u.u_action.file_types = $3; |
---|
62 | lstrule.u.u_action.options = $4; } |
---|
63 | wrule: WHEN dstpathge filetypes csh_cmds { newrule(); |
---|
64 | lstrule.type = R_WHEN; |
---|
65 | lstrule.u.u_when.type = WHEN_CSH; |
---|
66 | lstrule.u.u_when.globexp = $2; |
---|
67 | lstrule.u.u_when.file_types = $3; |
---|
68 | lstrule.u.u_when.cmds = $4; } |
---|
69 | | WHEN dstpathge filetypes sh_cmds { newrule(); |
---|
70 | lstrule.type = R_WHEN; |
---|
71 | lstrule.u.u_when.type = WHEN_SH; |
---|
72 | lstrule.u.u_when.globexp = $2; |
---|
73 | lstrule.u.u_when.file_types = $3; |
---|
74 | lstrule.u.u_when.cmds = $4; } |
---|
75 | srule: SET BOOLVAR { setvar($2); sfree($2); } |
---|
76 | | UNSET BOOLVAR { unsetvar($2); sfree($2); } |
---|
77 | irule: IF ifhack boolexp crule { newrule(); |
---|
78 | lstrule.type = R_IF; |
---|
79 | lstrule.u.u_if.boolexp = $3; |
---|
80 | lstrule.u.u_if.first = $2 + 1; |
---|
81 | } |
---|
82 | | IF ifhack boolexp crule elsehack crule |
---|
83 | { newrule(); |
---|
84 | lstrule.type = R_IF_ELSE; |
---|
85 | lstrule.u.u_if.boolexp = $3; |
---|
86 | lstrule.u.u_if.first = $5; |
---|
87 | rules[$5].u.u_skip.first = $2 + 1; |
---|
88 | } |
---|
89 | elsehack: ELSE { newrule(); |
---|
90 | lstrule.type = R_SKIP; |
---|
91 | $$ = lastrule; |
---|
92 | } |
---|
93 | ifhack: { $$ = lastrule; } |
---|
94 | brule: BEGIN_ crules END { /* nothing to be done here! */ } |
---|
95 | |
---|
96 | filetypes: { $$ = TYPE_ALL; } |
---|
97 | | FILETYPES { char *s = $1; $$ = 0; |
---|
98 | while (*s != '\0') switch (*(s++)) { |
---|
99 | #define xxx(c1,c2,type) case c1: case c2: $$ |= type; break |
---|
100 | xxx('d','D',TYPE_D); |
---|
101 | xxx('c','C',TYPE_C); |
---|
102 | xxx('b','B',TYPE_B); |
---|
103 | xxx('l','L',TYPE_L); |
---|
104 | xxx('s','S',TYPE_S); |
---|
105 | xxx('r','R',TYPE_R); |
---|
106 | xxx('x','X',TYPE_X); |
---|
107 | #undef xxx |
---|
108 | default: |
---|
109 | /* *** give some sort of error message */ |
---|
110 | break; |
---|
111 | } |
---|
112 | sfree($1); |
---|
113 | } |
---|
114 | |
---|
115 | action: COPY { $$ = ACTION_COPY; } |
---|
116 | | LOCAL { $$ = ACTION_LOCAL; } |
---|
117 | | LINK { $$ = ACTION_LINK; } |
---|
118 | | DELETE { $$ = ACTION_DELETE; } |
---|
119 | | IGNORE { $$ = ACTION_IGNORE; } |
---|
120 | |
---|
121 | aopts: { $$ = 0; } |
---|
122 | | aopts ACTIONOPT { $$ = $1; set_option($$,$2); } |
---|
123 | |
---|
124 | |
---|
125 | csh_cmds: csh_cmds CSH_CMD { $$ = svecappend($1,$2); } |
---|
126 | | CSH_CMD { $$ = s2svec($1); } |
---|
127 | |
---|
128 | sh_cmds: sh_cmds SH_CMD { $$ = svecappend($1,$2); } |
---|
129 | | SH_CMD { $$ = s2svec($1); } |
---|
130 | |
---|
131 | |
---|
132 | mapdests: mapdests1 { $$ = $1; } |
---|
133 | | { $$ = (char **) malloc(sizeof(char *)); |
---|
134 | $$[0] = 0; } |
---|
135 | |
---|
136 | mapdests1: mapdests1 MAPDEST { $$ = svecappend($1,$2); } |
---|
137 | | MAPDEST { $$ = s2svec($1); } |
---|
138 | |
---|
139 | srcpathge: pathge { $$ = concat(append(scopy(srcpath),'/'),$1); } |
---|
140 | dstpathge: pathge { $$ = concat(append(scopy(dstpath),'/'),$1); } |
---|
141 | |
---|
142 | pathge: ge GE_END { $$ = $1; } |
---|
143 | |
---|
144 | ge: ALPHANUM ge { $$ = concat(c2s($1),$2); } |
---|
145 | | '?' ge { $$ = concat(c2s($1),$2); } |
---|
146 | | '*' ge { $$ = concat(c2s($1),$2); } |
---|
147 | | '{' ge1 '}' ge { $$ = concat(c2s($1),concat(append($2,$3),$4));} |
---|
148 | | '[' chars ']' ge { $$ = concat(c2s($1),concat(append($2,$3),$4));} |
---|
149 | | ALPHANUM { $$ = c2s($1); } |
---|
150 | | '?' { $$ = c2s($1); } |
---|
151 | | '*' { $$ = c2s($1); } |
---|
152 | | '{' ge1 '}' { $$ = append(concat(c2s($1),$2),$3); } |
---|
153 | | '[' chars ']' { $$ = append(concat(c2s($1),$2),$3); } |
---|
154 | |
---|
155 | ge1: ge2 ',' ge1 { $$ = concat(append($1,$2),$3); } |
---|
156 | | ge2 { $$ = $1; } |
---|
157 | |
---|
158 | ge2: ge { $$ = $1; } |
---|
159 | | { $$ = scopy(""); } |
---|
160 | |
---|
161 | chars: chars ALPHANUM { $$ = append($1,$2); } |
---|
162 | | ALPHANUM { $$ = c2s($1); } |
---|
163 | |
---|
164 | boolexp0: '!' BOOLVAR { $$ = bool_not(bool_var($2)); } |
---|
165 | | BOOLVAR { $$ = bool_var($1); } |
---|
166 | | '!' '(' boolexp ')' { $$ = bool_not($3); } |
---|
167 | | '(' boolexp ')' { $$ = $2; } |
---|
168 | |
---|
169 | boolexp1: boolexp1 '&' boolexp0 { $$ = bool_and($1,$3); } |
---|
170 | | boolexp0 { $$ = $1; } |
---|
171 | |
---|
172 | boolexp: boolexp '|' boolexp1 { $$ = bool_or($1,$3); } |
---|
173 | | boolexp1 { $$ = $1; } |
---|
174 | |
---|
175 | %% |
---|
176 | |
---|
177 | /* |
---|
178 | * String manipulation routines |
---|
179 | */ |
---|
180 | |
---|
181 | char *c2s(c) |
---|
182 | char c; |
---|
183 | { char *r; |
---|
184 | r = (char *) malloc(10); |
---|
185 | r[0] = c; |
---|
186 | r[1] = '\0'; |
---|
187 | return r; |
---|
188 | } |
---|
189 | |
---|
190 | char *scopy(s) |
---|
191 | char *s; |
---|
192 | { char *r; |
---|
193 | r = (char *) malloc(strlen(s)+1); |
---|
194 | strcpy(r,s); |
---|
195 | return r; |
---|
196 | } |
---|
197 | |
---|
198 | char *append(s,c) |
---|
199 | char *s; |
---|
200 | char c; |
---|
201 | { int len = strlen(s); |
---|
202 | s = (char *) realloc(s,len+2); |
---|
203 | s[len] = c; |
---|
204 | s[len+1] = '\0'; |
---|
205 | return s; |
---|
206 | } |
---|
207 | |
---|
208 | char *concat(s,s2) |
---|
209 | char *s; |
---|
210 | char *s2; |
---|
211 | { int len = strlen(s); |
---|
212 | int len2 = strlen(s2); |
---|
213 | s = (char *) realloc(s,len+len2+1); |
---|
214 | strcat(s,s2); |
---|
215 | free(s2); |
---|
216 | return s; |
---|
217 | } |
---|
218 | |
---|
219 | void sfree(s) |
---|
220 | char *s; |
---|
221 | { |
---|
222 | free(s); |
---|
223 | } |
---|
224 | |
---|
225 | /* |
---|
226 | * Svec manipulation routines |
---|
227 | */ |
---|
228 | |
---|
229 | char **s2svec(s) |
---|
230 | char *s; |
---|
231 | { char **r; |
---|
232 | r = (char **) malloc(2*sizeof(char *)); |
---|
233 | r[0] = s; |
---|
234 | r[1] = 0; |
---|
235 | return r; |
---|
236 | } |
---|
237 | |
---|
238 | char **svecappend(v,s) |
---|
239 | char **v; |
---|
240 | char *s; |
---|
241 | { int len = 0; |
---|
242 | while (v[len] != 0) len++; |
---|
243 | v = (char **) realloc(v,(len+2)*sizeof(char *)); |
---|
244 | v[len] = s; |
---|
245 | v[len+1] = 0; |
---|
246 | return v; |
---|
247 | } |
---|
248 | |
---|
249 | void svecfree(v) |
---|
250 | char **v; |
---|
251 | { int i; |
---|
252 | for (i=0; v[i] != 0; i++) |
---|
253 | free(v[i]); |
---|
254 | free(v); |
---|
255 | } |
---|
256 | |
---|
257 | /* |
---|
258 | * Readrules |
---|
259 | */ |
---|
260 | |
---|
261 | int readrules(rfile,src,dst) |
---|
262 | char *rfile; |
---|
263 | char *src; |
---|
264 | char *dst; |
---|
265 | { FILE *rulefile; |
---|
266 | int status; |
---|
267 | extern int verbosef, nflag; |
---|
268 | |
---|
269 | #ifdef YYDEBUG |
---|
270 | extern int yydebug; |
---|
271 | yydebug = (verbosef>3)? 1: 0; |
---|
272 | #endif YYDEBUG |
---|
273 | |
---|
274 | srcpath = src; dstpath = dst; |
---|
275 | |
---|
276 | if ((rulefile = fopen(rfile,"r")) == NULL) return 0; |
---|
277 | if (verbosef || nflag) printf("Parsing %s.\n",rfile); |
---|
278 | yysetin(rulefile); |
---|
279 | lineno = 0; |
---|
280 | yyinfilename = rfile; |
---|
281 | status = yyparse(); |
---|
282 | fclose(rulefile); |
---|
283 | return status; |
---|
284 | } |
---|
285 | |
---|
286 | yyerror(s) |
---|
287 | char *s; |
---|
288 | { |
---|
289 | printf("%s: %s near line %d\n", yyinfilename, s, lineno); |
---|
290 | } |
---|