-
Notifications
You must be signed in to change notification settings - Fork 9
/
syncdiff_cfg_parser.y
466 lines (370 loc) · 10.1 KB
/
syncdiff_cfg_parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
%{ # start of the code section
###########################################################################
# Copyright (C) 2014 John 'Warthog9' Hawley #
# #
# This file is originally part of SyncDiff(erent). #
# #
# This library is free software; you can redistribute it and/or #
# modify it under the terms of the GNU Lesser General Public #
# License as published by the Free Software Foundation; either #
# version 2.1 of the License, or (at your option) any later version. #
# #
# This library is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# Lesser General Public License for more details. #
# #
# You should have received a copy of the GNU Lesser General Public #
# License along with this library; if not, write to the: #
# Free Software Foundation, Inc. #
# 51 Franklin Street #
# Fifth Floor #
# Boston, MA 02110-1301 #
# USA #
# #
# Or, see <http://www.gnu.org/licenses/>. #
###########################################################################
use Data::Dumper;
use Sys::Hostname;
use File::FnMatch qw(:fnmatch); # import everything
my $autonum = 0;
my %groups;
my %prefixes;
my $curgroup = undef;
my $curprefix = undef;
my $ignore_uid = 0;
my $ignore_gid = 0;
my $ignore_mod = 0;
sub get_groups {
return %groups;
}
sub get_prefixes {
return %prefixes;
}
sub get_ignores {
return ($ignore_uid, $ignore_gid, $ignore_mod);
}
sub new_group {
my ($name) = @_;
# print "function: new_group()\n";
#
# print "\tnew_group - name: ". $name ."\n";
if( $name eq "" ){
$name = "group_". $autonum;
$autonum++;
}
$curgroup = $name;
$groups{$name} = {};
} # end new_group
sub add_host {
my ($hostname) = @_;
my $local_hostname = hostname;
# print "function: add_host()\n";
#
# print "\tadd_host: ". $hostname ."\n";
$hostname = lc($hostname);
$local_hostname = lc($local_hostname);
if( $hostname eq $local_hostname){
#print "\t~~ Found ourselves\n";
return;
}
if(
$groups{$curgroup}->{'host'} == undef
||
$groups{$curgroup}->{'host'} eq ""
){
my @temparray = ();
$groups{$curgroup}->{'host'} = \@temparray;
}
# print Dumper \@_;
my @temparray = ( $hostname, );
# print Dumper \@temparray;
# print Dumper $groups{$curgroup}->{'host'};
push( @{ $groups{$curgroup}->{'host'} }, $hostname);
} # end add_host
sub add_patt {
my($flat, $pattern) = @_;
# print "function: add_patt()\n";
# print "\tadd_patt - pattern: ". $pattern ."\n";
if(
$groups{$curgroup}->{'patterns'} == undef
||
$groups{$curgroup}->{'patterns'} eq ""
){
my @temparray = ();
$groups{$curgroup}->{'patterns'} = \@temparray;
}
push( @{ $groups{$curgroup}->{'patterns'} }, $pattern);
} # end add_patt
sub set_key {
my( $key ) = @_;
# print "function: set_key()\n";
if( $groups{$curgroup}->{'key'} ne "" ){
print "*** Multiple keys found for group '". $curgroup ."' - last one wins! You are warned. ***\n";
}
if(
$key =~ /^\//
&&
-e $key
){
local $/;
open(FILE, $key) or die "Can't read file '$key' [$!]\n";
$key = <FILE>;
close (FILE);
}
print "Key is: $key\n";
if( length($key) < 32 ){
print "*** WARNING ***\n";
print "\tKey for group:". $curgroup ." is less than 32 charaters. Security is at risk\n";
print "***************\n";
}
$groups{$curgroup}->{'key'} = $key;
} # end set_key()
sub set_auto {
my ( $auto_resolve ) = @_;
# print "function: set_auto()\n";
$auto_resolve = lc( $auto_resolve );
if( $auto_resolve eq "none" ){
$groups{$curgroup}->{'auto_resolve'} = "none";
return;
}
if( $auto_resolve eq "first" ){
$groups{$curgroup}->{'auto_resolve'} = "first";
return;
}
if( $auto_resolve eq "younger" ){
$groups{$curgroup}->{'auto_resolve'} = "younger";
return;
}
if( $auto_resolve eq "older" ){
$groups{$curgroup}->{'auto_resolve'} = "older";
return;
}
if( $auto_resolve eq "bigger" ){
$groups{$curgroup}->{'auto_resolve'} = "bigger";
return;
}
if( $auto_resolve eq "smaller" ){
$groups{$curgroup}->{'auto_resolve'} = "smaller";
return;
}
if( $auto_resolve eq "copy" ){
$groups{$curgroup}->{'auto_resolve'} = "copy";
return;
}
print "*** WARNING ***\n";
print "\tUnknown auto resolution mechanism: ". $auto_resolve ."\n";
print "\tIgnoring option\n";
print "***************\n";
} # end set_auto()
sub set_bak_dir {
my ($back_dir) = @_;
# print "function: set_bak_dir()\n";
$groups{$curgroup}->{'back_dir'} = $back_dir;
} # end set_bak_dir();
sub set_bak_gen {
my ($backup_generations) = @_;
# print "function: set_bak_gen()\n";
if( $backup_generations =~ /[^0-9]+/ ){
print "*** WARNING ***\n";
print "\tUnknown number of Backup Generations: ". $backup_generations ."\n";
print "\tIgnoring option\n";
print "***************\n";
return;
}
$groups{$curgroup}->{'backup_generations'} = $backup_generations;
} # end set_back_gen()
sub check_group {
# print "function: check_group()\n";
if( length( $groups{$curgroup}->{'key'} ) <= 0 ){
die("Config error: groups must have a key.\n");
}
} # end check_group()
sub new_action {
print "function: new_action()\n";
} # end new_action()
sub add_action_pattern {
print "function: add_action_pattern()\n";
} # end add_action_pattern
sub add_action_exec {
print "function: add_action_exec()\n";
} # end add_action_exec()
sub set_action_logfile {
print "function: set_action_logfile()\n";
} # end set_action_logfile()
sub set_action_dolocal {
print "function: set_action_dolocal()\n";
} # end set_action_dolocal
sub new_prefix {
my( $pname ) = @_;
# print "function: new_prefix: $pname\n";
$curprefix = $pname;
$prefixes{$pname} = "";
} # end new_prefix
sub new_prefix_entry {
my( $pattern, $path ) = @_;
# print "function: new_prefix_entry()\n";
if( $path !~ /^\// ){
print "\t Prefix Path: '". $path ."' is not an absolute path.\n";
}
my $hostname = hostname;
if(
fnmatch( $pattern, $hostname )
&&
(
$prefixes{$curprefix} eq ""
||
$prefixes{$curprefix} eq undef
)
){
$prefixes{$curprefix}=$path;
}
} # end new_prefix_entry()
sub new_ignore {
my( $propname ) = @_;
# print "function: new_ignore()\n";
if( $propname eq "uid" ){
$ignore_uid = 1;
} elsif( $propname eq "gid" ){
$ignore_gid = 1;
} elsif( $propname eq "mod" ){
$ignore_mod = 1;
} else {
print "\tInvalid ignore option: '". $propname ."' - IGNORING\n";
}
} # end new_ignore()
sub on_cygwin_lowercase {
my( $string ) = @_;
# print "function: on_cygwin_lowercase()\n";
lc( $string );
# print "on_cygwin_loewrcase: ". $string ."\n";
return $string;
} # end on_cygwin_lowercase()
sub disable_cygwin_lowercase_hack {
print "function: disable_cygwin_lowercase_hack()\n";
}
%} # end of the code section
%expect 2
%union {
char *txt;
}
%token TK_BLOCK_BEGIN TK_BLOCK_END TK_STEND TK_AT TK_AUTO
%token TK_IGNORE TK_GROUP TK_HOST TK_EXCL TK_INCL TK_COMP TK_KEY
%token TK_ACTION TK_PATTERN TK_EXEC TK_DOLOCAL TK_LOGFILE TK_NOCYGLOWER
%token TK_PREFIX TK_ON TK_COLON TK_POPEN TK_PCLOSE
%token TK_BAK_DIR TK_BAK_GEN
%token <txt> TK_STRING
%%
config:
/* empty */
| block config
;
block:
block_header block_body
| TK_PREFIX TK_STRING
{ new_prefix($_[2]); }
TK_BLOCK_BEGIN prefix_list TK_BLOCK_END
{ }
| TK_IGNORE ignore_list TK_STEND
| TK_NOCYGLOWER TK_STEND
{ disable_cygwin_lowercase_hack(); }
;
ignore_list:
/* empty */
| TK_STRING ignore_list
{ new_ignore($_[1]); }
;
prefix_list:
/* empty */
| prefix_list TK_ON TK_STRING TK_COLON TK_STRING TK_STEND
{ new_prefix_entry($_[3], on_cygwin_lowercase($_[5])); }
;
block_header:
TK_GROUP
{ new_group(0); }
| TK_GROUP TK_STRING
{ new_group($_[2]); }
;
block_body:
TK_BLOCK_BEGIN stmts TK_BLOCK_END
{ check_group(); }
;
stmts:
/* empty */
| stmt TK_STEND stmts
| action stmts
;
stmt:
TK_HOST host_list
| TK_EXCL excl_list
| TK_INCL incl_list
| TK_COMP comp_list
| TK_KEY TK_STRING
{ set_key($_[2]); }
| TK_AUTO TK_STRING
{ set_auto($_[2]); }
| TK_BAK_DIR TK_STRING
{ set_bak_dir($_[2]); }
| TK_BAK_GEN TK_STRING
{ set_bak_gen($_[2]); }
;
host_list:
/* empty */
| host_list TK_STRING
{ add_host($_[2], $_[2], 0); }
| host_list TK_STRING TK_AT TK_STRING
{ add_host($_[2], $_[4], 0); }
| host_list TK_POPEN host_list_slaves TK_PCLOSE host_list
;
host_list_slaves:
/* empty */
| host_list_slaves TK_STRING
{ add_host($_[2], $_[2], 1); }
| host_list_slaves TK_STRING TK_AT TK_STRING
{ add_host($_[2], $_[4], 1); }
;
excl_list:
/* empty */
| excl_list TK_STRING
{ add_patt(0, on_cygwin_lowercase($_[2])); }
;
incl_list:
/* empty */
| incl_list TK_STRING
{ add_patt(1, on_cygwin_lowercase($_[2])); }
;
comp_list:
/* empty */
| incl_list TK_STRING
{ add_patt(2, on_cygwin_lowercase($_[2])); }
;
action:
TK_ACTION
{ new_action(); }
TK_BLOCK_BEGIN action_stmts TK_BLOCK_END
;
action_stmts:
/* empty */
| action_stmt TK_STEND action_stmts
;
action_stmt:
TK_PATTERN action_pattern_list
| TK_EXEC action_exec_list
| TK_LOGFILE TK_STRING
{ set_action_logfile($_[2]); }
| TK_DOLOCAL
{ set_action_dolocal(); }
;
action_pattern_list:
/* empty */
| action_pattern_list TK_STRING
{ add_action_pattern(on_cygwin_lowercase($_[2])); }
;
action_exec_list:
/* empty */
| action_exec_list TK_STRING
{ add_action_exec($_[2]); }
;
%%