Repository navigation
Expand file tree
/
Copy pathspawn.tcl
More file actions
93 lines (75 loc) · 2.82 KB
/
Copy pathspawn.tcl
File metadata and controls
93 lines (75 loc) · 2.82 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package provide 9pm::spawn 1.0
namespace eval ::9pm::spawn {
namespace eval int {
proc pop_fail { } {
::9pm::fatal ::9pm::output::error "Can't pop to an expired spawn"
}
}
proc open {alias create} {
global spawn_id
variable data
variable active
# Write console output to logfile
log_file ;#Stop any old logging
log_file -a "$::9pm::output::log_path/${alias}_console.log"
# Unregister any running command catchers for the previous spawn
::9pm::cmd::int::unreg_exp_after
if {[info exists data($alias)]} {
::9pm::output::debug "Now looking at spawn: $alias"
set spawn_id [dict get $data($alias) "spawn_id"]
} else {
::9pm::output::debug "Creating new spawn: \"$alias\""
# Create a new spawn by calling the implementation specific
# `create`. The `create` call shall return a list in the format of
# [spawn_id, pid] where pid is non-zero on success.
set metadata [{*}$create]
set spawn_id [lindex $metadata 0]
set pid [lindex $metadata 1]
if {$pid == 0} {
::9pm::fatal ::9pm::output::error "Failed to spawn \"$alias\""
}
dict append data($alias) "spawn_id" $spawn_id
dict append data($alias) "pid" $pid
}
set active $alias
::9pm::cmd::int::reg_exp_after
return TRUE
}
proc close {alias} {
variable data
variable active
if {![info exists data($alias)]} {
::9pm::fatal ::9pm::output::user_error "Trying to close spawn \"$alias\" that doesn't exist"
}
# Unset active spawn if that is the one we are closing
if {[info exists active] && ($active == $alias)} {
::9pm::output::debug "Closing active spawn: $alias"
unset active
} else {
::9pm::output::debug "Closing spawn: $alias"
}
::close -i [dict get $data($alias) "spawn_id"]
# Remove it from internal data structure and stop logging
unset data($alias)
log_file
}
proc push {alias create} {
variable active
variable stack
if {![info exists active]} {
::9pm::fatal ::9pm::output::user_error "You need to have a spawn in order to push it"
}
lappend stack $active
return [::9pm::spawn::open $alias $create]
}
proc pop { } {
variable active
variable stack
if {[llength $stack] == 0} {
::9pm::fatal ::9pm::output::user_error "Can't pop spawn, stack is empty"
}
set alias [lindex $stack end]
set stack [lreplace $stack [set stack end] end]
return [::9pm::spawn::open $alias "::9pm::spawn::int::pop_fail"]
}
}