#!/usr/bin/perl
# configure.pl
-#
-# This script is called from the makefile. It reads the settings file and
-# inserts the information in the source files.
-#
-# Copyright (C) 2015-2017, 2022 Balthasar Szczepański
+
+# The new BOTM configuration tool
+
+# Copyright (C) 2022 Balthasar Szczepański
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
use constant URL_ENCODE => '_URL_ENCODE';
use constant PATH => '_PATH';
use constant SPRINTF => '_SPRINTF';
+use constant DEBUG => '_DEBUG';
+use constant DEBUG_ALL => '_DEBUG_ALL';
# TODO more special functions
%cfg = parse_file($arg, 0, %cfg);
}
+my %replace_line;
+my %replace_keyword;
+my $debug = '';
+my $debug_all = '';
+my $match_line;
+my $match_keyword;
+
foreach my $key (keys %cfg) {
- print $key.': >'.$cfg{$key}."<\n";
+ if ($debug_all ne ''){
+ $debug_all .= "\n";
+ }
+ $debug_all .= "$key: $cfg{$key}";
+ unless((substr($key,0,1) eq '_') or ($key =~ /^[0-9]+$/)){
+ if ($debug ne ''){
+ $debug .= "\n";
+ }
+ $debug .= "$key: $cfg{$key}";
+ $match_line = parse_pattern(REPLACE_LINE(), "($key)", 0, %cfg);
+ $match_keyword = parse_pattern(REPLACE_KEYWORD(), "($key)", 0, %cfg);
+ $replace_line{$match_line} = $cfg{$key};
+ $replace_keyword{$match_keyword} = $cfg{$key};
+ }
+}
+$match_line = parse_pattern(REPLACE_LINE(), '('.DEBUG().')', 0, %cfg);
+$match_keyword = parse_pattern(REPLACE_KEYWORD(), '('.DEBUG().')', 0, %cfg);
+$replace_line{$match_line} = $debug;
+$replace_keyword{$match_keyword} = $debug;
+$match_line = parse_pattern(REPLACE_LINE(), '('.DEBUG_ALL().')', 0, %cfg);
+$match_keyword = parse_pattern(REPLACE_KEYWORD(), '('.DEBUG_ALL().')', 0, %cfg);
+$replace_line{$match_line} = $debug_all;
+$replace_keyword{$match_keyword} = $debug_all;
+my $status;
+LINE: while (defined(my $line = <STDIN>)) {
+ $line =~ s/[\r\n]//g;
+ foreach my $key (keys %replace_line) {
+ if (index($line, $key) >= 0) {
+ print "$replace_line{$key}\n";
+ next LINE;
+ }
+ }
+ foreach my $key (keys %replace_keyword) {
+ do {
+ ($status, $line) = replace($line, $key, $replace_keyword{$key});
+ } while ($status);
+ }
+ print "$line\n";
}
-# TODO: OK we have settings parsing done.
-# Now parse the actual files, do the replacements
+# replace WITHOUT REGEXP
+sub replace {
+ (my $text, my $to_replace, my $replacement) = @_;
+
+ my $ind = index($text, $to_replace);
+ if ($ind <0) {
+ return (0, $text);
+ }
+ my $len = length($to_replace);
+
+ my $before = substr($text, 0, $ind);
+ my $after = substr($text, $ind+$len);
+
+ return (1, $before.$replacement.$after);
+}
-sub unescape {
+
+sub unescape1ch {
(my $to_escape) = @_;
if ($to_escape eq 'a') { return "\a";}
else { return $to_escape;}
}
+sub escape {
+ (my $text, my $match) = @_;
+ unless (defined $match) {
+ $match = '[\\\\\\\'\\\"]';
+ }
+
+ my $outcome = '';
+ foreach my $ch (split('', $text)) {
+ if ($ch =~ $match) {
+ if ($ch eq "\a") { $outcome .= '\\a';}
+ elsif ($ch eq "\b") { $outcome .= '\\b';}
+ elsif ($ch eq "\e") { $outcome .= '\\e';}
+ elsif ($ch eq "\f") { $outcome .= '\\f';}
+ elsif ($ch eq "\n") { $outcome .= '\\n';}
+ elsif ($ch eq "\r") { $outcome .= '\\r';}
+ elsif ($ch eq "\t") { $outcome .= '\\t';}
+ else { $outcome .= '\\'.$ch;}
+ }
+ else {
+ $outcome .= $ch;
+ }
+ }
+ return $outcome;
+}
+
sub urlencode {
(my $text, my $match) = @_;
unless (defined $match) {
while ($to_parse ne '') {
if ($to_parse =~ /^\\(.)(.*)$/) {
- $parsed .= unescape($1);
+ $parsed .= unescape1ch($1);
$to_parse = $2;
}
elsif ($to_parse =~ /^\$([A-Za-z0-9_\-\.]+)(.*)$/) {
return $parsed;
}
elsif ($name eq ESCAPE) {
- return '+'; #TODO
+ return escape(@parameter_list);
}
elsif ($name eq URL_ENCODE) {
return urlencode(@parameter_list);
while ($to_parse ne '') {
# escape
if ($to_parse =~ /^\\(.)(.*)$/) {
- $parsed .= unescape($1);
+ $parsed .= unescape1ch($1);
$to_parse = $2;
}
elsif ($to_parse =~ /^\$([A-Za-z0-9_\-\.]+)(.*)$/) {