-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.tcl
More file actions
100 lines (83 loc) · 3.17 KB
/
shell.tcl
File metadata and controls
100 lines (83 loc) · 3.17 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
94
95
96
97
98
99
100
# Copyright (C) 2011-2014 Richard Alpe <rical@highwind.se>
#
# This file is part of 9pm.
#
# 9pm is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# 9pm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package provide 9pm::shell 1.0
namespace eval ::9pm::shell {
proc open {alias {shell "/bin/bash -norc"}} {
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 shell: $alias"
set spawn_id [dict get $data($alias) "spawn_id"]
} else {
::9pm::output::debug "Spawning new shell: \"$alias\""
set ::env(HISTFILE) "/dev/null"
set pid [spawn {*}$shell]
if {$pid == 0} {
::9pm::fatal ::9pm::output::error "Failed to spawn shell \"$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 shell \"$alias\" that doesn't exist"
}
# Unset active shell if that is the one we are closing
if {[info exists active] && ($active == $alias)} {
::9pm::output::debug "Closing active shell: $alias"
unset active
} else {
::9pm::output::debug "Closing shell: $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} {
variable active
variable stack
if {![info exists active]} {
::9pm::fatal ::9pm::output::user_error "You need to have a shell in order to push it"
}
lappend stack $active
return [::9pm::shell::open $alias]
}
proc pop { } {
variable active
variable stack
if {[llength $stack] == 0} {
::9pm::fatal ::9pm::output::user_error "Can't pop shell, stack is empty"
}
set alias [lindex $stack end]
set stack [lreplace $stack [set stack end] end]
return [::9pm::shell::open $alias]
}
}