
# This is the UNIX version of these functions.
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc check_exec {args} {
    global exec_problem no_exec_errors
    
    set string "exec $args"
    regsub -all {\#} $string {\#} string

    if {[catch $string retstr] && ![info exists no_exec_errors]} {
	warningmsg "$retstr"
	set exec_problem 1
	return ""
    }
    set exec_problem 0
    catch {unset no_exec_errors}
    return $retstr
}

proc runxcom {name com catfile hitreturn back} {
    global runcoms xterm_params xterm_prog env rsh_command
    global override_hitreturn icem_fluent
    
    if [info exists override_hitreturn] { set hitreturn $override_hitreturn }
    
    infomsg "Running: $com"
    set prog [lindex $com 0]
    set args [lrange $com 1 end]
    
    if ![file exists $prog] {
	if [file exists $prog.bat] {
	    set prog $prog.bat
	} elseif [file exists $prog.exe] {
	    set prog $prog.exe
	} else {
	    errormsg "File $prog or $prog.bat or $prog.exe doesn't exist"
	    return
	}
    }
    
    if !$icem_fluent {
	set fp [open random_cmd.bat w]
	puts $fp "$prog $args"
	puts $fp pause
	close $fp
	set mess [eval check_exec start /WAIT random_cmd.bat]
    } else {
	set mess [eval check_exec start $prog $args]
    }
    mess $mess\n blue
}

proc runxcom_remote {name com catfile hitreturn back} {
    runxcom $name $com $catfile $hitreturn $back
}

proc runcom {com {out ""} {noerrors 0}} {
    global runcoms no_exec_errors exec_problem
    
    infomsg "Running: $com"
    set prog [lindex $com 0]
    set args [lrange $com 1 end]
    
    if ![file exists $prog] {
	if [file exists $prog.bat] {
	    set prog $prog.bat
	} elseif [file exists $prog.exe] {
	    set prog $prog.exe
	} else {
	    errormsg "File $prog or $prog.bat or $prog.exe doesn't exist"
	    return
	}
    }
    
    if $noerrors { set no_exec_errors 1 }

    set fp [open random_cmd.bat w]
    puts $fp "$prog $args"
    close $fp

    set ret [eval check_exec ./random_cmd.bat]
    if {$ret != ""} { infomsg $ret }
    
    if {($out != "") && [file exists $out] && [file size $out]} {
	infomsg "Output of command:"
	set file [open $out r]
	set stuff [read $file nonewline]
	close $file
	infomsg $stuff
	infomsg "Done."
    } else {
	infomsg "Done."
    }
    if $exec_problem { errormsg "The command did not finish correctly." }
}

proc run_editor {file} {
}

