#!/usr/bin/env tclsh # script to change empty_string_p and string equal into "x eq/ne y" # Gustaf Neumann, Dez 2005 array set opt {-reset 0 -change 1} array set opt $argv if {$opt(-reset)} { foreach file [exec find -L . -type f -name *-original] { regexp {^(.*)-original} $file _ new file delete $new file rename $file $new } } if {$opt(-change)} { set totalchanges 0 set files 0 foreach file [exec find -L . -type f -name *tcl] { set F [open $file]; set c [read $F]; close $F set newFile "" set changes 0 incr changes [regsub -all {if +(\[[^\[\]]+\]) +\{} $c "if {\\1} \{" c] incr changes [regsub -all {if +(! *\[[^\[\]]+\]) +\{} $c "if {\\1} \{" c] incr changes [regsub -all {if +(! *\[[^\[]+\[[^\]]*\][^\]]*\]) +\{} $c "if {\\1} \{" c] incr changes [regsub -all {\[expr *(! *\[[^\[\]]+\]) *\]} $c "\[expr {\\1}\] " c] incr changes [regsub -all {\[ *expr +"([^"]+)\" *\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +([a-zA-Z0-9\$!][^\[\]]+) *\\\]} $c "\[expr {\\1}\\\]" c] incr changes [regsub -all {\[ *expr +([a-zA-Z0-9\$!][^\[\]]+) *\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +(\$[^\[\]]+ +\[[^\[\]]+ *\]) *\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +(\[[^\[\]]+ *\][^\[\]]+ *)\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +(\[[^\[\]]+ *\] +(\|\||&&) +\[[^\[\]]+ *\] *)\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +(\[[^\[\]]+ *\] +[^\[\]]+ +\[[^\[\]]+ *\] *[^\[\]]*)\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {! *\[ *empty_string_p +(\$[a-zA-Z0-9_\(\):\$]+) *\]} $c {\1 ne ""} c] incr changes [regsub -all {\[ *empty_string_p +(\$[a-zA-Z0-9_\(\):\$]+) *\]} $c {\1 eq ""} c] # quote string literals in string compare and equal incr changes [regsub -all {\[ *string +eq +} $c \ "\[string equal " c] incr changes [regsub -all {\[ *string +(equal|compare) +(\$[a-zA-Z0-9_\(\):\$]+|\[[^\[\]]+\]) +([a-zA-Z0-9/%+:.-][^ ]*) *\]} $c \ {[string \1 \2 "\3"]} c] incr changes [regsub -all {\[ *string +(equal|compare|match) +([a-zA-Z0-9/%+:.-][^ ]*) +(\$[a-zA-Z0-9_\(\):\$]+|\[[^\[\]]+\]) *\]} $c \ {[string \1 "\2" \3]} c] # string equal $var "string", "string" $var, $var $var incr changes [regsub -all {if +\{ *\[string +equal +(\$[a-zA-Z0-9_\(\):\$]+) +("[^\"]*"|{}|\[[^\[\]]+\]) *\] *\}} $c \ {if {\1 eq \2}} c] incr changes [regsub -all {if +\{ *\[string +equal +("[^\"]*"|{}|\[[^\[\]]+\]) +(\$[a-zA-Z0-9_\(\):\$]+) *\] *\}} $c \ {if {\1 eq \2}} c] incr changes [regsub -all {if +\{ *\[string +equal +(\$[a-zA-Z0-9_\(\):\$]+) +(\$[a-zA-Z0-9_\(\):\$]+) *\] *\}} $c \ {if {\1 eq \2}} c] incr changes [regsub -all {if +\{ *\[string +equal +("[^\"]*"|{}|\[[^\[\]]+\]) +("[^\"]*"|{}|\[[^\[\]]+\]) *\] *\}} $c \ {if {\1 eq \2}} c] # !string equal $var "string", "string" $var, $var $var (we must (well...) be already in an expression) incr changes [regsub -all {! *\[ *string +equal +(\$[a-zA-Z0-9_\(\):\$]+) +("[^\"]*"|{}|\[[^\[\]]+\]) *\] *} $c \ "\\1 ne \\2 " c] incr changes [regsub -all {! *\[ *string +equal +("[^\"]*"|{}|\[[^\[\]]+\]) +(\$[a-zA-Z0-9_\(\):\$]+) *\] *} $c \ "\\1 ne \\2 " c] incr changes [regsub -all {! *\[ *string +equal +(\$[a-zA-Z0-9_\(\):\$]+) +(\$[a-zA-Z0-9_\(\):\$]+) *\] *} $c \ "\\1 ne \\2 " c] incr changes [regsub -all {! *\[ *string +equal +("[^\"]*"|{}|\[[^\[\]]+\]) +("[^\"]*"|{}|\[[^\[\]]+\]) *\] *} $c \ "\\1 ne \\2 " c] # string equal within expression incr changes [regsub -all {\[ *string +equal +(\$[a-zA-Z0-9_\(\):\$]+) +("[^\"]*"|{}|\[[^\[\]]+\]) *\] +([&|])} $c \ {\1 eq \2 \3} c] incr changes [regsub -all {\[ *string +equal +("[^\"]*"|{}|\[[^\[\]]+\]) +(\$[a-zA-Z0-9_\(\):\$]+) *\] +([&|])} $c \ {\1 eq \2 \3} c] incr changes [regsub -all {\[ *string +equal +(\$[a-zA-Z0-9_\(\):\$]+) +(\$[a-zA-Z0-9_\(\):\$]+) *\] +([&|])} $c \ {\1 eq \2 \3} c] incr changes [regsub -all {([&|]) *\[ *string +equal +(\$[a-zA-Z0-9_\(\):\$]+) +("[^\"]*"|{}|\[[^\[\]]+\]) *\]} $c \ {\1 \2 eq \3} c] incr changes [regsub -all {([&|]) *\[ *string +equal +("[^\"]*"|{}|\[[^\[\]]+\]) +(\$[a-zA-Z0-9_\(\):\$]+) *\]} $c \ {\1 \2 eq \3} c] incr changes [regsub -all {([&|]) *\[ *string +equal +(\$[a-zA-Z0-9_\(\):\$]+) +(\$[a-zA-Z0-9_\(\):\$]+) *\]} $c \ {\1 \2 eq \3} c] # replace string length == 0 incr changes [regsub -all {\[ *string +length +(\[[^\[\]]+\]) *\] *== *0} $c {\1 eq ""} c] incr changes [regsub -all {\[ *string +length +(\$[a-zA-Z0-9_\(\):\$]+) *\] *== *0} $c {\1 eq ""} c] incr changes [regsub -all {\[ *string +length +(\$[a-zA-Z0-9_\(\):\$]+) *\] *> *0} $c {\1 ne ""} c] # replace string match without wildcards; seems to be used everywhere within expressions incr changes [regsub -all {! *\[ *string +match +("[^*?\[\]\$]*") +(\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) *\]} $c \ "\\1 ne \\2" c] incr changes [regsub -all {\[ *string +match +("[^*?\[\]\$]*") +(\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) *\]} $c \ "\\1 eq \\2" c] # !string compare $var "string", "string" $var, $var $var incr changes [regsub -all {! *\[ *string +compare +(\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) (\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) *\] *} $c \ "\\1 eq \\2 " c] incr changes [regsub -all {\[ *string +compare +(\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) (\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) *\] *== *0} $c \ "\\1 eq \\2 " c] incr changes [regsub -all {\[ *string +compare +(\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) (\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) *\] *!= *0} $c \ "\\1 ne \\2 " c] incr changes [regsub -all {if +\{ *\[ *string +compare +(\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) (\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) *\]} $c \ "if \{\\1 ne \\2 " c] incr changes [regsub -all {([&|]) *\[ *string +compare +(\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) (\$[a-zA-Z0-9_\(\):\$]+|"[^\"]*"|{}|\[[^\[\]]+\]) *\]} $c \ "\\1 \\2 ne \\3 " c] # insert spaces before and after comparisons regsub -all {([\"\]])(==|!=)([\"\[])} $c "\\1 \\2 \\3" c # == with literal comparison incr changes [regsub -all {(\$[a-zA-Z0-9_\(\):\$]+|\[[^\[\]]+\]) * == * ("[a-zA-Z_:+*.,/' \\\}\{-]*")} $c "\\1 eq \\2" c] incr changes [regsub -all {(\$[a-zA-Z0-9_\(\):\$]+|\[[^\[\]]+\]) * != * ("[a-zA-Z_:+*.,/' \\\}\{-]*")} $c "\\1 ne \\2" c] # insert curly brackets around expressions (maybe introduced by the above replacements) incr changes [regsub -all {if +(\$[a-zA-Z0-9_\(\):\$]+) +eq ""} $c \ {if {\1 eq ""}} c] incr changes [regsub -all {(if|expr|while) +(\$[a-zA-Z0-9_\(\):\$]+) +eq ""} $c \ {\1 {\2 eq ""}} c] incr changes [regsub -all {(if|expr|while) +(\$[a-zA-Z0-9_\(\):\$]+) +ne ""} $c \ {\1 {\2 ne ""}} c] incr changes [regsub -all {\mif !+(\$[a-zA-Z0-9_\(\):\$]+) +\{} $c \ "if {!\\1} \{" c] incr changes [regsub -all {\mif +(\$[a-zA-Z0-9_\(\):\$]+) +\{} $c \ "if {\\1} \{" c] # try this again on the simplified expressions incr changes [regsub -all {\[ *expr +"([^"]+)\" *\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +([a-zA-Z0-9\$!][^\[\]]+) *\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +(\$[^\[\]]+ +\[[^\[\]]+ *\]) *\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +(\[[^\[\]]+ *\][^\[\]]+ *)\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +(\[[^\[\]]+ *\] +(\|\||&&) +\[[^\[\]]+ *\] *)\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +(\[[^\[\]]+ *\] +[^\[\]]+ +\[[^\[\]]+ *\] *[^\[\]]*)\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {\[ *expr +(\[[^\[\]]+ *\] +[^\[\]]+ +\[[^\[\]]+\[[^\[\]]+\][^\[\]]*\] *[^\[\]]*)\]} $c "\[expr {\\1}\]" c] incr changes [regsub -all {! *\[ *empty_string_p +(\$[a-zA-Z0-9_\(\):\$]+) *\]} $c {\1 ne ""} c] incr changes [regsub -all {\[ *empty_string_p +(\$[a-zA-Z0-9_\(\):\$]+) *\]} $c {\1 eq ""} c] incr changes [regsub -all {{"" (eq|ne) (\$[a-zA-Z0-9_\(\):\$]+) *}} $c {{\2 \1 ""}} c] if {$changes > 0} { puts "... updating $file ($changes changes)" set F [open /tmp/XXX w]; puts -nonewline $F $c; close $F file rename $file $file-original set F [open $file w]; puts -nonewline $F $c; close $F incr totalchanges $changes incr files } } puts "$totalchanges changes in $files files" }