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
|
package XorpConfigParser;
use lib "/opt/vyatta/share/perl5/";
use strict;
my %data;
my %fields = (
_data => \%data
);
sub new {
my $that = shift;
my $class = ref ($that) || $that;
my $self = {
%fields,
};
bless $self, $class;
return $self;
}
sub copy_node {
my ($self, $from, $to, $name) = @_;
if (!defined($from) || !defined($to) || !defined($name)) {
return;
}
foreach my $node (@$from) {
my $stringNodeNameHere = $node->{'name'};
if ($stringNodeNameHere =~ /^$name.*/) {
foreach my $nodeCheck (@$to) {
my $stringCheck = $nodeCheck->{'name'};
if ($name eq $stringCheck) {
$nodeCheck->{'value'} = $node->{'value'};
$nodeCheck->{'children'} = $node->{'children'};
$nodeCheck->{'comment'} = $node->{'comment'};
return;
}
}
push(@$to, $node);
}
}
}
sub copy_multis {
my ($self, $nodes, $name) = @_;
if (!defined($nodes) || !defined($name)) {
return undef;
}
my @multis;
foreach my $node (@$nodes) {
my $stringNodeNameHere = $node->{'name'};
if ($stringNodeNameHere =~ /$name\s(\S+)/) {
my $stringNameHere = $1;
my %multi = (
'name' => $stringNameHere,
'comment' => $node->{'comment'},
'value' => $node->{'value'},
'children' => $node->{'children'}
);
push(@multis, \%multi);
}
}
return @multis;
}
sub comment_out_child {
my ($self, $children, $name, $comment) = @_;
if (!defined($children) || !defined($name)) {
return;
}
for (my $i = 0; $i < @$children; $i++) {
my $stringNodeNameHere = @$children[$i]->{'name'};
if ($name eq $stringNodeNameHere) {
@$children[$i]->{'comment_out'} = "1";
if (defined($comment)) {
@$children[$i]->{'comment_out'} = $comment;
}
}
}
}
sub create_node {
my ($self, $path) = @_;
my $hash = \%data;
foreach my $segment (@$path) {
my $children = $hash->{'children'};
if (!defined($children)) {
my @new_children;
$hash->{'children'} = \@new_children;
$children = \@new_children;
}
my $child_found = 0;
foreach my $child (@$children) {
if ($child->{'name'} eq $segment) {
$child_found = 1;
$hash = $child;
last;
}
}
if ($child_found == 0) {
my %new_hash = (
'name' => $segment
);
push(@$children, \%new_hash);
$hash = \%new_hash;
}
}
return $hash;
}
sub delete_child {
my ($self, $children, $name) = @_;
if (!defined($children) || !defined($name)) {
return;
}
for (my $i = 0; $i < @$children; $i++) {
my $stringNodeNameHere = @$children[$i]->{'name'};
if ($name eq $stringNodeNameHere) {
@$children[$i] = undef;
}
}
}
sub find_child {
my ($self, $children, $name) = @_;
if (!defined($children) || !defined($name)) {
return undef;
}
foreach my $child (@$children) {
my $stringNodeNameHere = $child->{'name'};
if ($name eq $stringNodeNameHere) {
return $child;
}
}
return undef;
}
sub get_node {
my ($self, $path) = @_;
my $hash = $self->{_data};
foreach my $segment (@$path) {
my $children = $hash->{'children'};
if (!defined($children)) {
return undef;
}
my $child_found = 0;
foreach my $child (@$children) {
if ($child->{'name'} eq $segment) {
$child_found = 1;
$hash = $child;
last;
}
}
if ($child_found == 0) {
return undef;
}
}
return $hash;
}
sub push_comment {
my ($self, $path, $comment) = @_;
my $hash = \%data;
foreach my $segment (@$path) {
my $children = $hash->{'children'};
if (!defined($children)) {
my @children;
$hash->{'children'} = \@children;
$children = \@children;
}
my $child_found = 0;
foreach my $child (@$children) {
if ($child->{'name'} eq $segment) {
$child_found = 1;
$hash = $child;
last;
}
}
if ($child_found == 0) {
my %new_hash = (
'name' => $segment
);
push(@$children, \%new_hash);
$hash = \%new_hash;
}
}
my %new_comment = (
'comment' => $comment
);
my $childrenPush = $hash->{'children'};
if (!defined($childrenPush)) {
my @new_children;
$hash->{'children'} = \@new_children;
$childrenPush = \@new_children;
}
push(@$childrenPush, \%new_comment);
}
sub set_value {
my ($self, $path, $value) = @_;
my $hash = $self->create_node($path);
if (defined($hash)) {
$hash->{'value'} = $value;
}
}
sub output {
my ($self, $depth, $hash) = @_;
if (!defined($hash)) {
$hash = $self->{_data};
}
if ($hash->{'comment'} ne '') {
print '/*' . $hash->{'comment'} . "*/\n";
}
my $children = $hash->{'children'};
foreach my $child (@$children) {
if (defined($child)) {
if (defined($child->{'comment_out'})) {
print "\n";
if ($child->{'comment_out'} ne "1") {
print "/* --- $child->{'comment_out'} --- */\n";
}
print "/* --- CONFIGURATION COMMENTED OUT DURING MIGRATION BELOW ---\n";
}
print " " x $depth;
if ($child->{'value'} ne '') {
print "$child->{'name'}: $child->{'value'}";
print "\n";
} else {
my $print_brackets = 0;
my $children = $child->{'children'};
if (defined($children) && @$children > 0) {
$print_brackets = 1;
} elsif ($child->{'name'} ne '' && !($child->{'name'} =~ /\s/)) {
$print_brackets = 1;
}
if ($child->{'name'} ne '') {
print "$child->{'name'}";
if ($print_brackets) {
print " {";
}
print "\n";
}
$self->output($depth+1, $child);
if ($print_brackets) {
print " " x $depth;
print "}\n";
}
}
if (defined($child->{'comment_out'})) {
print " --- CONFIGURATION COMMENTED OUT DURING MIGRATION ABOVE --- */\n\n";
}
}
}
}
sub parse {
my ($self, $file) = @_;
open(INPUT, "< $file") or die "Error! Unable to open file \"$file\". $!";
my $contents = "";
while (<INPUT>) {$contents .= $_}
close INPUT;
my @array_contents = split('', $contents);
# print scalar(@array_contents) . "\n";
my $length_contents = @array_contents;
my $colon = 0;
my $colon_quote = 0;
my $name = '';
my $value = undef;
my @path;
my %tree;
for (my $i = 0; $i < $length_contents;) {
my $c = $array_contents[$i];
my $cNext = $array_contents[$i+1];
if ($c eq '/' && $cNext eq '*') {
my $comment_text = '';
my $comment_end = index($contents, '*/', $i+2);
if ($comment_end == -1) {
$comment_text = substr($contents, $i+2);
} else {
$comment_text = substr($contents, $i+2, $comment_end - $i - 2);
$i = $comment_end + 2;
}
# print 'Comment is: "' . $comment_text . "\"\n";
$self->push_comment(\@path, $comment_text);
} elsif ($colon == 0 && ($c eq '{' || $c eq ':' || $c eq "\n")) {
$name =~ s/^\s+|\s$//g;
if (length($name) > 0) {
push(@path, $name);
# print "Path is: \"@path\" Name is: \"$name\"\n";
$self->set_value(\@path, $value);
$name = '';
if ($c eq "\n") {
pop(@path);
}
if ($c eq ':') {
$colon = 1;
}
}
$i++;
} elsif ($c eq '}') {
pop(@path);
$name = '';
$i++;
} elsif ($c eq ';') {
$i++;
} elsif ($colon == 1) {
my $value_end = 0;
if ($c eq '"') {
$value .= $c;
if ($colon_quote == 1) {
$value_end = 1;
} else {
$colon_quote = 1;
}
} elsif ($c eq '\\' && $cNext eq '"') {
$value .= '\\"';
$i++;
} else {
if ((length($value) > 0) || (!($c =~ /\s/))) {
$value .= $c;
}
}
if ($colon_quote == 0 && ($cNext eq '}' || $cNext eq ';' || $cNext =~ /\s/)) {
$value_end = 1;
}
$i++;
if ($value_end == 1) {
if (length($value) > 0) {
# print "Path is: \"@path\" Value is: $value\n";
$self->set_value(\@path, $value);
$value = undef;
}
pop(@path);
$colon_quote = 0;
$colon = 0;
}
} else {
$name .= $c;
$i++;
}
}
}
|