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