37 |
$VERSION="0.18"; |
$VERSION="0.18"; |
38 |
|
|
39 |
$INDENT_SPACES = ' '; |
$INDENT_SPACES = ' '; |
|
#$TEMPLATES_DIRECTORY = 'templates'; #testing purpose |
|
|
|
|
|
#require $_ foreach (glob "$TEMPLATES_DIRECTORY/*_template.pm"); |
|
|
|
|
|
#my %templates; |
|
|
#basic templates |
|
|
#$templates{$_} = $base_templates{$_} foreach keys(%base_templates); |
|
|
|
|
|
#values_sections but '#' added |
|
|
#$templates{values_sections2} = $templates{values_sections}; |
|
|
#$templates{values_sections2}{comments} = [ [';'], ['#']]; |
|
|
#TODO : externalize the comments |
|
|
|
|
|
|
|
|
my %validation = (); |
|
|
# my %validation = ( |
|
|
# autologin => { |
|
|
# location => '/etc/sysconfig/autologin', |
|
|
# rules => q( |
|
|
# if ($atom->{type} eq 'KEY_VALUE') { |
|
|
# member($atom->{key}, qw(AUTOLOGIN USER EXEC)) and $atom->{valid} = 1; |
|
|
# } |
|
|
# ), |
|
|
# }, |
|
|
# 'php.ini' => { |
|
|
# location => '/etc/php4/php.ini', |
|
|
# rules => q( |
|
|
# if ($atom->{type} eq 'KEY_VALUE') { |
|
|
# my @valid_keys = qw(allow-url-fopen asp-tags auto-append-file auto-prepend-file disable-functions display-errors doc-root engine error-log error-reporting file-uploads html-errors docref-root docref-ext open-basedir gpc-order variables-order ignore-user-abort implicit-flush include-path log-errors magic-quotes-gpc magic-quotes-runtime magic-quotes-sybase max-execution-time memory-limit precision register-argc-argv post-max-size register-globals short-open-tag safe-mode track-errors track-vars upload-tmp-dir upload-max-filesize user-dir warn-plus-overloading safe-mode safe-mode-gid safe-mode-exec-dir safe-mode-include-dir host port enabled enable-dl extension-dir extension allow-persistent max-persistent max-links allow-persistent max-persistent max-links sesam-oml sesam-configfile sesam-messagecatalog allow-persistent max-persistent max-links allow-persistent max-persistent max-links min-server-severity min-client-severity login-timeout timeout hostname allow-persistent max-persistent max-links default-host default-user default-password blobinfile textasvarchar byteasvarchar charasvarchar nullformat scale browscap internal-encoding http-input http-output detect-order substitute-character encode_unicode decode_unicode_motorola decode_unicode_intel encode_jis decode_jis_motorola decode_jis_intel); |
|
|
# member($atom->{key}, @valid_keys) and $atom->{valid} = 1; |
|
|
# } elsif ($atom->{type} eq 'SECTION') { |
|
|
# $atom->{valid} = 1; |
|
|
# } |
|
|
# ), |
|
|
# }, |
|
|
# 'XF86Config-4' => { |
|
|
# location => '/etc/X11/XF86Config-4', #don't think it is used but I put the right thing anyway |
|
|
# rules => q( |
|
|
# if ($atom->{type} eq 'KEY_VALUE') { |
|
|
# my @valid_keys = qw(RgbPath FontPath NoTrapSignals DontZap DontZoom AllowMouseOpenFail Identifier VendorName ModelName BoardName Defaul |
|
|
# ColorDepth HorizSync VertRefresh ModeLine Driver Option Load Depth Modes ViewPort Screen InputDevice); |
|
|
# member($atom->{key}, @valid_keys) and $atom->{valid} = 1; |
|
|
# } elsif ($atom->{type} eq 'SECTION') { |
|
|
# $atom->{valid} = 1; |
|
|
# } |
|
|
# ), |
|
|
# }, |
|
|
# 'httpd.conf' => { |
|
|
# location => '/etc/httpd/conf/httpd.conf', #don't think it is used but I put the right thing anyway |
|
|
# rules => q( |
|
|
# if ($atom->{type} eq 'KEY_VALUE') { |
|
|
# my @valid_keys = qw(ServerType AddModule LoadModule Include); #todo : complete this list of keywords |
|
|
# member($atom->{key}, @valid_keys) and $atom->{valid} = 1; |
|
|
# } elsif ($atom->{type} eq 'SECTION') { |
|
|
# $atom->{valid} = 1; |
|
|
# } |
|
|
# ), |
|
|
# }, |
|
|
# 'sshd_config' => { |
|
|
# location => '/etc/ssh/sshd_config', #don't think it is used but I put the right thing anyway |
|
|
# rules => q( |
|
|
# if ($atom->{type} eq 'KEY_VALUE') { |
|
|
# my @valid_keys = qw(); #todo : complete this list of keywords |
|
|
# member($atom->{key}, @valid_keys) and $atom->{valid} = 1; |
|
|
# } elsif ($atom->{type} eq 'SECTION') { |
|
|
# $atom->{valid} = 1; |
|
|
# } |
|
|
# ), |
|
|
# }, |
|
|
# 'smb.conf' => { |
|
|
# location => '/etc/samba/smb.conf', #don't think it is used but I put the right thing anyway |
|
|
# rules => q( |
|
|
# if ($atom->{type} eq 'KEY_VALUE') { |
|
|
# my @valid_keys = qw(); #todo : complete this list of keywords |
|
|
# member($atom->{key}, @valid_keys) and $atom->{valid} = 1; |
|
|
# } elsif ($atom->{type} eq 'SECTION') { |
|
|
# $atom->{valid} = 1; |
|
|
# } |
|
|
# ), |
|
|
# }, |
|
|
# ); |
|
40 |
|
|
41 |
sub new { |
sub new { |
42 |
my ($pkg, $filename, $template_name, $validation_name, $template_filename, $safe_write) = @_; |
my ($pkg, $filename, $template_name, $validation_name, $template_filename, $safe_write) = @_; |
170 |
foreach my $atom (@out_atoms) { |
foreach my $atom (@out_atoms) { |
171 |
# print F "\n" x $$atom{line_feed}; |
# print F "\n" x $$atom{line_feed}; |
172 |
# $$atom{comments} and print F eval($templates{$template_name}{comment_output}) foreach split("\n", $$atom{comments}); |
# $$atom{comments} and print F eval($templates{$template_name}{comment_output}) foreach split("\n", $$atom{comments}); |
173 |
|
my $output_text = ''; |
174 |
|
my $output_indentation = $out->{current_indentation}; |
175 |
if (exists $templates{$template_name}->{output}{$atom->{type}}) { |
if (exists $templates{$template_name}->{output}{$atom->{type}}) { |
176 |
print F eval($templates{$template_name}->{output}{$atom->{type}}); |
eval($templates{$template_name}->{output}{$atom->{type}}); |
177 |
} else { |
} else { |
178 |
defined($atom->{type}) and print F '## WARNING - No valid $template->{output}{' . $atom->{type} . "}\n"; |
if (defined($atom->{type})) { |
179 |
|
print F '## WARNING - No valid $template->{output}{' . $atom->{type} . "}\n"; |
180 |
|
die '## WARNING - No valid $template->{output}{' . $atom->{type} . "}\n"; |
181 |
|
} |
182 |
} |
} |
183 |
|
print F handle_output($output_text, $output_indentation, $atom->{comments}); |
184 |
} |
} |
185 |
} |
} |
186 |
|
|
198 |
foreach my $in (@file) { |
foreach my $in (@file) { |
199 |
$out_atoms[$item] ||= {}; |
$out_atoms[$item] ||= {}; |
200 |
$out_atoms[$item]->{comments} ||= []; |
$out_atoms[$item]->{comments} ||= []; |
201 |
$in =~ /^(\s*)\n$/ and push(@{$out_atoms[$item]->{comments}}, { comment => $1, inline => 0 }), next; |
$in =~ /^\s*\n$/ and push(@{$out_atoms[$item]->{comments}}, { comment => '', inline => 0 }), next; |
202 |
# $in eq "\n" and $out_atoms[$item]->{line_feed}++, next; |
# $in eq "\n" and $out_atoms[$item]->{line_feed}++, next; |
203 |
chomp $in; |
chomp $in; |
204 |
#see if we are in a line previously commented |
#see if we are in a line previously commented |
273 |
$out->{atoms} = [@out_atoms]; |
$out->{atoms} = [@out_atoms]; |
274 |
} |
} |
275 |
|
|
276 |
sub handle_comment { |
sub handle_output { |
277 |
my ($string, $comments) = @_; |
my ($text, $indentation, $comments) = @_; |
278 |
my $ret = ''; |
my $ret = ''; |
279 |
my $flag = 1; |
my $flag = 1; |
280 |
|
$indentation = $INDENT_SPACES x $indentation; |
281 |
foreach my $comment (@$comments) { |
foreach my $comment (@$comments) { |
282 |
$comment->{inline} == -1 and $ret .= $comment->{comment} . " $string\n", $flag = 0; |
$comment->{inline} == -1 and $ret .= $indentation . $comment->{comment} . " $text\n", $flag = 0; |
283 |
$comment->{inline} == 0 and $ret .= $comment->{comment} . "\n"; |
$comment->{inline} == 0 and $ret .= $indentation . $comment->{comment} . "\n"; |
284 |
$comment->{inline} == 1 and $ret .= "$string " . $comment->{comment} . "\n", $flag = 0; |
$comment->{inline} == 1 and $ret .= $indentation . "$text " . $comment->{comment} . "\n", $flag = 0; |
285 |
} |
} |
286 |
$ret . ($flag ? $string . "\n" : ''); |
$flag && length($text) and $ret .= $indentation . $text . "\n"; |
287 |
|
$ret; |
288 |
} |
} |
289 |
|
|
290 |
sub debug { |
sub debug { |