From 9fa24580c6ac3715e6b6d83b8947ba9ab058f19f Mon Sep 17 00:00:00 2001 From: Julian Noble Date: Fri, 21 Apr 2023 15:55:10 +1000 Subject: [PATCH] shellspy support for lib::scriptname.tcl lib::filter::scriptname.tcl etc for easier calling tcl scripts located in scriptlib --- src/punk86.vfs/lib/app-shellspy/shellspy.tcl | 77 +++++++++++++++++--- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/src/punk86.vfs/lib/app-shellspy/shellspy.tcl b/src/punk86.vfs/lib/app-shellspy/shellspy.tcl index c5ce98d4..d3700448 100644 --- a/src/punk86.vfs/lib/app-shellspy/shellspy.tcl +++ b/src/punk86.vfs/lib/app-shellspy/shellspy.tcl @@ -51,7 +51,7 @@ if {[file extension $arg1] in [list .tCl]} { #try the lowercase version (extension lowercased only) so that file doesn't have to be renamed to use alternate dispatch set scriptfile [file rootname $scriptfile][string tolower [file extension $scriptfile]] } - source [file normalize $arg1] + source [$scriptfile] #package require app-punk @@ -291,7 +291,10 @@ namespace eval shellspy { #set exitinfo [shellfilter::run [list pwsh -nologo -noprofile -c {*}$args] -debug 1] + set id_err [shellfilter::stack::add stderr ansiwrap -action sink -settings {-colour {red bold}}] set exitinfo [shellfilter::run [list pwsh -nologo -noprofile -c {*}$args] {*}$params] + shellfilter::stack::remove stderr $id_err + #Passing args in as a single element will tend to make powershell treat the args as a 'script block' # (powershell sees items in curly brackets {} as a scriptblock - when called from non-powershell, this tends to just echo back the contents) #set exitinfo [shellfilter::run [list pwsh -nologo -noprofile -c $args] {*}$params] @@ -308,11 +311,17 @@ namespace eval shellspy { dict set params -teehandle shellspy set params [dict merge $params [get_channel_config $::testconfig]] ;#working: 5 unbuffered, 6 linebuffered - set cmdlist [list pwsh -nologo -noprofile -c {*}$args] - set cmdlist [shellfilter::get_scriptrun_from_cmdlist_dquote_if_not $cmdlist] + #set cmdlist [list pwsh -nologo -noprofile -c {*}$args] + set cmdlist [list pwsh -nologo -c {*}$args] + #the big problem with using the 'script' command is that we get stderr/stdout mashed together. + + #set cmdlist [shellfilter::get_scriptrun_from_cmdlist_dquote_if_not $cmdlist] shellfilter::log::write $shellspy_status_log "do_in_powershell_terminal as script: '$cmdlist'" + set id_err [shellfilter::stack::add stderr ansiwrap -action sink -settings {-colour {red bold}}] set exitinfo [shellfilter::run $cmdlist {*}$params] + shellfilter::stack::remove stderr $id_err + if {[lindex $exitinfo 0] eq "exitcode"} { shellfilter::log::write $shellspy_status_log "do_in_powershell_terminal returning $exitinfo" } @@ -332,11 +341,14 @@ namespace eval shellspy { set params [dict merge $params [get_channel_config $::testconfig]] #set id_out [shellfilter::stack::add stdout tounix -action sink-locked -settings {}] + set id_err [shellfilter::stack::add stderr ansiwrap -action sink-locked -settings {-colour {red bold}}] #set exitinfo [shellfilter::run "cmd /c $args" -debug 1] set exitinfo [shellfilter::run [list cmd /c {*}$args] {*}$params] #set exitinfo [shellfilter::run [list cmd /c {*}$args] -debug 1] - + + shellfilter::stack::remove stderr $id_err + #shellfilter::stack::remove stdout $id_out @@ -439,8 +451,33 @@ namespace eval shellspy { set params [dict merge $params [get_channel_config $::testconfig]] + set exedir [file dirname [info nameofexecutable]] + set libroot [file join $exedir scriptlib] + if {[string match lib::* $scriptname]} { + set scriptname [string map [list "lib::" "" "::" "/"] $scriptname] + set scriptpath $libroot/$scriptname + } else { + set scriptpath $scriptname + } + if {![file exists $scriptpath]} { + set scriptpath [file rootname $scriptpath][string tolower [file extension $scriptpath]] + if {![file exists $scriptpath]} { + puts stderr "Failed to find script: '$scriptpath'" + error "bad scriptpath '$scriptpath'" + } + } + + + + set id_err [shellfilter::stack::add stderr ansiwrap -action sink-locked -settings {-colour {red bold}}] + + + #todo - use glob to check capitalisation of file tail (.TCL vs .tcl .Tcl etc) - set exitinfo [shellfilter::run [concat [auto_execok tclsh] $scriptname $args] {*}$params] + set exitinfo [shellfilter::run [concat [auto_execok tclsh] $scriptpath $args] {*}$params] + + shellfilter::stack::remove stderr $id_err + if {[lindex $exitinfo 0] eq "exitcode"} { shellfilter::log::write $shellspy_status_log "do_script_process returning $exitinfo" } @@ -450,7 +487,23 @@ namespace eval shellspy { variable shellspy_status_log shellfilter::log::write $shellspy_status_log "do_script got scriptname:'$scriptname' replwhen:$replwhen args:'$args'" - set script [string map [list %a% $args %s% $scriptname] { + set exedir [file dirname [info nameofexecutable]] + set libroot [file join $exedir scriptlib] + if {[string match lib::* $scriptname]} { + set scriptname [string map [list "lib::" "" "::" "/"] $scriptname] + set scriptpath $libroot/$scriptname + } else { + set scriptpath $scriptname + } + if {![file exists $scriptpath]} { + set scriptpath [file rootname $scriptpath][string tolower [file extension $scriptpath]] + if {![file exists $scriptpath]} { + puts stderr "Failed to find script: '$scriptpath'" + error "bad scriptpath '$scriptpath'" + } + } + + set script [string map [list %a% $args %s% $scriptpath] { set scriptname %s% set ::argv [list %a%] set ::argc [llength $::argv] @@ -479,8 +532,12 @@ source [file normalize $scriptname] set params [dict merge $params [get_channel_config $::testconfig]] + set id_err [shellfilter::stack::add stderr ansiwrap -action sink-locked -settings {-colour {red bold}}] set exitinfo [shellfilter::run $script {*}$params] + + shellfilter::stack::remove stderr $id_err + if {[lindex $exitinfo 0] eq "exitcode"} { shellfilter::log::write $shellspy_status_log "do_script returning $exitinfo" } @@ -638,7 +695,7 @@ source [file normalize $scriptname] ############################################################################################ #echo raw args to diverted stderr before running the argument analysis - puts -nonewline stderr "rawargs: $::argv\n" + puts -nonewline stderr "exe:[info nameofexecutable] script:[info script] shellspy-rawargs: $::argv\n" set i 1 foreach a $::argv { puts -nonewline stderr "arg$i: '$a'\n" @@ -646,7 +703,7 @@ source [file normalize $scriptname] } - puts stderr $::shellfilter::ansi::test + puts stderr "ansi-test:$::shellfilter::ansi::test" flush stderr set argdefinitions [list \ @@ -684,7 +741,7 @@ source [file normalize $scriptname] shellfilter::log::open shellspy-error {-tag shellspy-error -syslog 127.0.0.1:514} shellfilter::log::write shellspy-error "logtidyup error $errMsg\n [set ::errorInfo]" - after 500 + after 200 } #don't open more logs.. #puts stdout ">$tidyinfo" @@ -709,7 +766,7 @@ source [file normalize $scriptname] } errMsg]} { shellfilter::log::open shellspy-final {-tag shellspy-final -syslog 127.0.0.1:514} shellfilter::log::write shellspy-final "FINAL logtidyup error $errMsg\n [set ::errorInfo]" - after 500 + after 200 }