Skip to content

Instantly share code, notes, and snippets.

@mwgamera
Created March 29, 2017 11:39
Show Gist options
  • Save mwgamera/4b1ff829d1fe06969d51553957ed71e1 to your computer and use it in GitHub Desktop.
Save mwgamera/4b1ff829d1fe06969d51553957ed71e1 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Execute a window manager but allow changing it easily at run time.
# Requires sufficiently dumb WM that eofs on exit (beware of its children
# sharing standard output) and leaves sane state when simply killed.
# klg, Mar 2017
# \
exec wish "$0" "$@"
package require Tk
wm withdraw .
if {[llength $argv] < 1} {
set argv x-window-manager
}
if {[regexp {^(.*/)?chwm[^/]*$} $argv0]} {
set exe [auto_execok [lindex $argv 0]]
if {$exe eq {}} {
puts stderr [list Unknown command [lindex $argv 0]]
exit -1
}
send execwm [list chwm [list $exe {*}[lrange $argv 1 end]]]
exit 0
}
tk appname execwm
proc chwm {command} {
upvar #0 argv argv pid pid
set argv $command
if {[catch {exec kill $pid}]} {
respawn
}
}
proc respawn {} {
upvar #0 argv argv pid pid
if {$argv eq {}} {exit 0}
set fd [open [list | {*}$argv]]
fileevent $fd readable [list readable_cb $fd]
set argv {}
set pid [pid $fd]
}
proc readable_cb {fd} {
if {[gets $fd line] > 0} {
puts stderr $line}
if {[eof $fd]} {
fileevent $fd readable {}
catch {close $fd}
respawn
}
}
respawn
vwait forever
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment