File: exec.tcl

package info (click to toggle)
exmh 1%3A2.9.0-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 4,216 kB
  • sloc: tcl: 38,046; perl: 1,647; makefile: 130; sh: 101; exp: 75; csh: 9; sed: 2
file content (57 lines) | stat: -rw-r--r-- 1,371 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# exec.tcl
#
# wrapper around exec that caches location in your PATH.
# the built-in exec doesn't do this, so the directories on your
# path are constantly searched, which is silly for long lived processes.
#

if {[string length [info commands exec-orig]] == 0} {
    rename exec exec-orig
    trace variable env(PATH) w ExecCacheReset
}

proc Exec_Init {} {
    # Just to fault in this file.
}

proc exec {args} {
    global ExecCache env

# Caution:  Enabling the line below will cause PGP passphrases to be logged!
    Exmh_Debug exec [join $args]

    if {![regexp {^( 	)*([^<>	 ]+)(.*)$} $args all x cmd rest]} {
	# auto-exec generates commands like:
	#	>&@stdout <@stdin /bin/ls
	return [eval {exec-orig} $args]
    }

    if {[info exists ExecCache($cmd)]} {
	if [catch {eval {exec-orig $ExecCache($cmd)} $rest} x] {
	    if {[info exist ExecCache($cmd)]} {
		unset ExecCache($cmd)
	    }
	    return -code error $x
	}
	return $x
    } else {
	foreach dir [split $env(PATH) :] {
	    set path [file join $dir $cmd]
	    if {[file executable $path] && ![file isdirectory $path]} {
		if [catch {eval {exec-orig $path} $rest} x] {
		    return -code error $x
		}
		set ExecCache($cmd) $path
		return $x
	    }
	}
    }
    eval {exec-orig $cmd} $rest
}

proc ExecCacheReset {args} {
    global ExecCache env
    if {[info exist ExecCache]} {
	unset ExecCache
    }
}