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