# Version von mkCmd/mkCmd_wait mit permanentem xterm-Fenster
#
# Copyright (C) 2004  G. Lamprecht, W. Lotz, R. Weibezahn; IWD, Bremen University

proc optapp {opt app} {
  global xtermcall
  set l [llength $xtermcall]
  for {set i 0} {$i<=$l} {incr i} {if {[lindex $xtermcall $i]==$opt} {break}}
  if {$i>=$l} return
  incr i
  if {[lindex $xtermcall $i]!="xtem"} return
  set xtermcall [lreplace $xtermcall $i $i "[lindex $xtermcall $i]$app"]
}

proc mkCmd_wait {w c a}  {# wie mkCmd, wartet aber auf Ende-Ausführung!
  global vv p_mkCmd n_mkCmd wait_mkCmd xtem_path w_mkCmd c_mkCmd env
  global xtermcall wishcall vst_dir bell_level testPut

  set xterm_mkCmd "xtermcall_wrong_or_X11Security-violation"

  if {[catch "which $c"]>0} {# program not found
    writescr $w "\n\n$vv(xtq01)\n*** $vv(ut56): \"$c\"\n$vv(xtq01)\n"; mybell 4; return
  }

  if  {([lsearch -glob [winfo interps] "xtem* #*"]>=0)||([tk appname]!="xtem")} {
    writescr0 $w "$vv(mk3)\n$vv(mk4)\n$vv(mk5) \"$vv(xta2m)\"\n"
    return
  }

  .a.2.x configure -state disabled
  .a.2.a configure -state normal
  update idletasks
  set wait_mkCmd 0

  if {[info exists n_mkCmd]&&($n_mkCmd=="$xterm_mkCmd")} {
    writescr .d.tt "\n***** $vv(ut51)\n  $vst_dir/install.vst\n$vv(ut52)\n  \"$xtermcall\" "
    XhostTest .d.tt "$vv(or)\n***** "
  }
  if  {![info exists w_mkCmd]}  then  {
     set n_mkCmd $xterm_mkCmd
     set name  [tk appname]
     optapp -T _async;     optapp -title _async;     optapp -n _async
     set FID [eval open \
	\"| $xtermcall -e $wishcall $xtem_path/xtem_async $name $bell_level $testPut\"]
     set w_mkCmd [pid $FID]
     fconfigure $FID -blocking 0
     fileevent  $FID readable [list mkcommand4_read $FID]
     tkwait variable n_mkCmd

     set c_mkCmd [join [lrange [.a.1.e configure -command] 4 end]]
     .a.1.e configure -command {
        global w_mkCmd c_mkCmd
        exec kill -s SIGKILL $w_mkCmd
        .a.1.e configure -command $c_mkCmd
        unset  c_mkCmd
        .a.1.e invoke
        exit
     }

  }
  if {[info exists env(TEXEDIT)]} then {
    send  $n_mkCmd  [list set env(TEXEDIT) "[set env(TEXEDIT)]"]
  }
  after  0   send  $n_mkCmd  \{startProc \[list $c $a\]\}
  tkwait variable wait_mkCmd
  if {[info exists w_mkCmd]} {.a.2.x configure -state normal} \
                             {.a.2.x configure -state disabled}
  .a.2.a configure -state disabled
  update idletasks

  return
}


proc mkCmd_cw {} {# Button zum Beenden des xterm-Fensters erzeugen
    global vv
    if {![winfo exists .a.2.x]}  then  {
      button .a.2.x -text "$vv(mk2)" -command {
        global  w_mkCmd c_mkCmd p_mkCmd wait_mkCmd
        if {[info exists w_mkCmd]}  then  {
	   TestPut 4 "*****<$w_mkCmd>"
           exec kill -s SIGKILL $w_mkCmd
           .a.1.e configure -command $c_mkCmd
           set p_mkCmd 1
           set wait_mkCmd 1
           catch "unset w_mkCmd"; #  catch fuer AIX erforderlich
        }
        .a.2.x configure -state disabled; update idletasks
      }
      pack configure .a.2.x -after .a.2.a -anchor w -padx 3 -pady 3
      Bind3HF .a.2.x mk_cmd .d.tt
    }
    .a.2.x configure -state disabled; update idletasks
}


proc mkCmd_xit {} {# Der durch Aufruf von mkcmd_wait gestartete Prozess beendet
   global wait_mkCmd p_mkCmd
   set p_mkCmd 1
   set wait_mkCmd 1
}

proc mkcommand4_read {FID} {
   global wait_mkCmd p_mkCmd w_mkCmd c_mkCmd

   # if called program writes something to stdout.
   # also necessary for recognising eof condition.
   catch {read $FID} r
   if { "$r" != "" } {TestPut 4 "read $FID: $r"}
   if [eof $FID] {
      fconfigure $FID -blocking 1
      if [catch {close $FID} c] {TestPut 4 "close $FID: $c"}
      if {[info exists c_mkCmd]} {# xterm nicht durch xtem-Ende-Knopf beendet
        .a.1.e configure -command $c_mkCmd
        set p_mkCmd 1
        set wait_mkCmd 1
        catch "unset w_mkCmd"; #  catch fuer AIX erforderlich
        if {[winfo exists .a.2.x]} {.a.2.x configure -state disabled}
        update idletasks
      }
      return

   }
}

proc killprocess {pid}  {global n_mkCmd; send $n_mkCmd killProc}
