From 5044f565d2699e20e221ba1750062241d8babe97 Mon Sep 17 00:00:00 2001 From: b Date: Sat, 10 Sep 2022 16:12:13 +0000 Subject: [PATCH] Replacement functionality now works. --- configure.pl | 108 +++++++++++++++++++++++++++++++++++++----- settings_template.txt | 1 - 2 files changed, 96 insertions(+), 13 deletions(-) diff --git a/configure.pl b/configure.pl index ce2ac6d..1caa813 100755 --- a/configure.pl +++ b/configure.pl @@ -1,11 +1,10 @@ #!/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 @@ -36,6 +35,8 @@ use constant ESCAPE => '_ESCAPE'; 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 @@ -48,14 +49,72 @@ foreach my $arg (@ARGV) { %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 = )) { + $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";} @@ -68,6 +127,31 @@ sub unescape { 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) { @@ -238,7 +322,7 @@ sub parse_pattern { while ($to_parse ne '') { if ($to_parse =~ /^\\(.)(.*)$/) { - $parsed .= unescape($1); + $parsed .= unescape1ch($1); $to_parse = $2; } elsif ($to_parse =~ /^\$([A-Za-z0-9_\-\.]+)(.*)$/) { @@ -260,7 +344,7 @@ sub parse_pattern { return $parsed; } elsif ($name eq ESCAPE) { - return '+'; #TODO + return escape(@parameter_list); } elsif ($name eq URL_ENCODE) { return urlencode(@parameter_list); @@ -296,7 +380,7 @@ sub parse_value { while ($to_parse ne '') { # escape if ($to_parse =~ /^\\(.)(.*)$/) { - $parsed .= unescape($1); + $parsed .= unescape1ch($1); $to_parse = $2; } elsif ($to_parse =~ /^\$([A-Za-z0-9_\-\.]+)(.*)$/) { diff --git a/settings_template.txt b/settings_template.txt index 7ea3146..e69de29 100644 --- a/settings_template.txt +++ b/settings_template.txt @@ -1 +0,0 @@ -p0 = $_SPRINTF(%02x %02x,44,22) -- 2.30.2