@ -19,23 +19,40 @@ namespace eval shellrun {
variable runerr
variable runerr
proc run {args} {
proc get_run_opts {arglist} {
set ::punk::last_run_display [list]
if {[catch {
#we provide -nonewline for 'run' even though run doesn't deliver stderr or stdout to the tcl return value
set callerinfo [info level -1]
} errM]} {
set caller ""
} else {
set caller [lindex $callerinfo 0]
}
#we provide -nonewline even for 'run' even though run doesn't deliver stderr or stdout to the tcl return value
#This is for compatibility with other runX commands, and the difference is also visible when calling from repl.
#This is for compatibility with other runX commands, and the difference is also visible when calling from repl.
set known_runopts [list "-echo" "-e" "-nonewline" "-n"]
set known_runopts [list "-echo" "-e" "-nonewline" "-n"]
set aliases [list "-e" "-echo" "-echo" "-echo" "-n" "-nonewline" "-nonewline" "-nonewline"] ;#include map to self
set aliases [list "-e" "-echo" "-echo" "-echo" "-n" "-nonewline" "-nonewline" "-nonewline"] ;#include map to self
set runopts [list]
set runopts [list]
set cmdargs [list]
set cmdargs [list]
set idx_first_cmdarg [lsearch -not $args "-*"]
set idx_first_cmdarg [lsearch -not $argli st "-*"]
set runopts [lrange $args 0 $idx_first_cmdarg-1]
set runopts [lrange $argli st 0 $idx_first_cmdarg-1]
set cmdargs [lrange $args $idx_first_cmdarg end]
set cmdargs [lrange $argli st $idx_first_cmdarg end]
foreach o $runopts {
foreach o $runopts {
if {$o ni $known_runopts} {
if {$o ni $known_runopts} {
error "run : Unknown runoption $o"
error "$calle r: Unknown runoption $o - known options $known_runopts "
}
}
}
}
set runopts [lmap o $runopts {dict get $aliases $o}]
set runopts [lmap o $runopts {dict get $aliases $o}]
return [list runopts $runopts cmdargs $cmdargs]
}
proc run {args} {
set ::punk::last_run_display [list]
set splitargs [get_run_opts $args]
set runopts [dict get $splitargs runopts]
set cmdargs [dict get $splitargs cmdargs]
if {"-nonewline" in $runopts} {
if {"-nonewline" in $runopts} {
set nonewline 1
set nonewline 1
} else {
} else {
@ -52,7 +69,7 @@ namespace eval shellrun {
set c [shellfilter::ansi::+ green]
set c [shellfilter::ansi::+ green]
set n [shellfilter::ansi::+]
set n [shellfilter::ansi::+]
if {[dict exists $exitinfo error]} {
if {[dict exists $exitinfo error]} {
error [dict get $exitinfo error]
error " [dict get $exitinfo error]\n$exitinfo"
}
}
return $exitinfo
return $exitinfo
@ -65,19 +82,10 @@ namespace eval shellrun {
set runout ""
set runout ""
set runerr ""
set runerr ""
set known_runopts [list "-echo" "-e" "-nonewline" "-n"]
set splitargs [get_run_opts $args]
set aliases [list "-e" "-echo" "-echo" "-echo" "-n" "-nonewline" "-nonewline" "-nonewline"] ;#include map to self
set runopts [dict get $splitargs runopts]
set runopts [list]
set cmdargs [dict get $splitargs cmdargs]
set cmdargs [list]
set idx_first_cmdarg [lsearch -not $args "-*"]
set runopts [lrange $args 0 $idx_first_cmdarg-1]
set cmdargs [lrange $args $idx_first_cmdarg end]
foreach o $runopts {
if {$o ni $known_runopts} {
error "runout: Unknown runoption $o"
}
}
set runopts [lmap o $runopts {dict get $aliases $o}]
if {"-nonewline" in $runopts} {
if {"-nonewline" in $runopts} {
set nonewline 1
set nonewline 1
} else {
} else {
@ -177,19 +185,11 @@ namespace eval shellrun {
variable runerr
variable runerr
set runout ""
set runout ""
set runerr ""
set runerr ""
set known_runopts [list "-echo" "-e" "-nonewline" "-n"]
set aliases [list "-e" "-echo" "-echo" "-echo" "-n" "-nonewline" "-nonewline" "-nonewline"] ;#include map to self
set splitargs [get_run_opts $args]
set runopts [list]
set runopts [dict get $splitargs runopts]
set cmdargs [list]
set cmdargs [dict get $splitargs cmdargs]
set idx_first_cmdarg [lsearch -not $args "-*"]
set runopts [lrange $args 0 $idx_first_cmdarg-1]
set cmdargs [lrange $args $idx_first_cmdarg end]
foreach o $runopts {
if {$o ni $known_runopts} {
error "runerr: Unknown runoption $o"
}
}
set runopts [lmap o $runopts {dict get $aliases $o}]
if {"-nonewline" in $runopts} {
if {"-nonewline" in $runopts} {
set nonewline 1
set nonewline 1
} else {
} else {
@ -272,6 +272,7 @@ namespace eval shellrun {
return $::shellrun::runerr
return $::shellrun::runerr
}
}
proc runx {args} {
proc runx {args} {
set ::punk::last_run_display [list]
set ::punk::last_run_display [list]
variable last_run_display
variable last_run_display
@ -280,32 +281,22 @@ namespace eval shellrun {
set runout ""
set runout ""
set runerr ""
set runerr ""
set known_runopts [list "-echo" "-e" "-nonewline" "-n"]
set splitargs [get_run_opts $args]
set aliases [list "-e" "-echo" "-echo" "-echo" "-n" "-nonewline" "-nonewline" "-nonewline"] ;#include map to self
set runopts [dict get $splitargs runopts]
set runopts [list]
set cmdargs [dict get $splitargs cmdargs]
set cmdargs [list]
set idx_first_cmdarg [lsearch -not $args "-*"]
set runopts [lrange $args 0 $idx_first_cmdarg-1]
set cmdargs [lrange $args $idx_first_cmdarg end]
foreach o $runopts {
if {$o ni $known_runopts} {
error "runx: Unknown runoption $o - known options $known_runopts"
}
}
set runopts [lmap o $runopts {dict get $aliases $o}]
if {"-nonewline" in $runopts} {
if {"-nonewline" in $runopts} {
set nonewline 1
set nonewline 1
} else {
} else {
set nonewline 0
set nonewline 0
}
}
#shellfilter::stack::remove stdout $::repl::id_outstack
#shellfilter::stack::remove stdout $::repl::id_outstack
if {"-echo" in $runopts} {
if {"-echo" in $runopts} {
set stderr_stackid [shellfilter::stack::add stderr tee_to_var -action sink-locked -settings {-varname ::shellrun::runerr}]
#float to ensure repl transform doesn't interfere with the output data
set stdout_stackid [shellfilter::stack::add stdout tee_to_var -action sink-locked -settings {-varname ::shellrun::runout}]
set stderr_stackid [shellfilter::stack::add stderr tee_to_var -action float -settings {-varname ::shellrun::runerr}]
set stdout_stackid [shellfilter::stack::add stdout tee_to_var -action float -settings {-varname ::shellrun::runout}]
} else {
} else {
#set stderr_stackid [shellfilter::stack::add stderr var -action sink-locked -settings {-varname ::shellrun::runerr}]
#set stderr_stackid [shellfilter::stack::add stderr var -action sink-locked -settings {-varname ::shellrun::runerr}]
#set stdout_stackid [shellfilter::stack::add stdout var -action sink-locked -settings {-varname ::shellrun::runout}]
#set stdout_stackid [shellfilter::stack::add stdout var -action sink-locked -settings {-varname ::shellrun::runout}]
@ -385,4 +376,46 @@ namespace eval shellrun {
#always return exitinfo $code at beginning of dict (so that punk unknown can interpret the exit code as a unix-style bool if double evaluated)
#always return exitinfo $code at beginning of dict (so that punk unknown can interpret the exit code as a unix-style bool if double evaluated)
return [list {*}$exitinfo stdout $::shellrun::runout stderr $::shellrun::runerr]
return [list {*}$exitinfo stdout $::shellrun::runout stderr $::shellrun::runerr]
}
}
proc sh_run {args} {
set splitargs [get_run_opts $args]
set runopts [dict get $splitargs runopts]
set cmdargs [dict get $splitargs cmdargs]
#e.g sh -c "ls -l *"
#we pass cmdargs to sh -c as a list, not individually
tailcall shellrun::run {*}$runopts sh -c $cmdargs
}
proc sh_runout {args} {
set splitargs [get_run_opts $args]
set runopts [dict get $splitargs runopts]
set cmdargs [dict get $splitargs cmdargs]
tailcall shellrun::runout {*}$runopts sh -c $cmdargs
}
proc sh_runerr {args} {
set splitargs [get_run_opts $args]
set runopts [dict get $splitargs runopts]
set cmdargs [dict get $splitargs cmdargs]
tailcall shellrun::runerr {*}$runopts sh -c $cmdargs
}
proc sh_runx {args} {
set splitargs [get_run_opts $args]
set runopts [dict get $splitargs runopts]
set cmdargs [dict get $splitargs cmdargs]
tailcall shellrun::runx {*}$runopts sh -c $cmdargs
}
}
}
namespace eval shellrun {
interp alias {} a+ {} shellfilter::ansi::+
interp alias {} run {} shellrun::run
interp alias {} sh_run {} shellrun::sh_run
interp alias {} runout {} shellrun::runout
interp alias {} sh_runout {} shellrun::sh_runout
interp alias {} runerr {} shellrun::runerr
interp alias {} sh_runerr {} shellrun::sh_runerr
interp alias {} runx {} shellrun::runx
interp alias {} sh_runx {} shellrun::sh_runx
}