This repository was archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshell.py
159 lines (150 loc) · 5.51 KB
/
shell.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Console Jabber Client
# Copyright (C) 2004-2010 Jacek Konieczny
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation
#
# This program 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.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import os
import locale
from cjc.plugin import PluginBase
from cjc import ui
from cjc import cjc_globals
class Plugin(PluginBase):
def __init__(self,app,name):
PluginBase.__init__(self,app,name)
ui.activate_cmdtable("shell",self)
def command_returned(self,command,ret):
if ret:
es=os.WEXITSTATUS(ret)
if not os.WIFEXITED(ret):
self.error("Command exited abnormally")
elif es:
self.warning("Command exited with status %i" % (es,))
def cmd_shell(self,args):
if args.get()=="-noterm":
args.shift()
term=False
else:
term=True
command=args.all()
if not command or not command.strip():
self.error("Command must be given")
return
try:
if term:
cjc_globals.screen.shell_mode()
try:
ret=os.system(command)
finally:
cjc_globals.screen.prog_mode()
else:
ret=os.system(command+" 0</dev/null >/dev/null 2>&1")
except OSError,e:
self.error("Shell command execution failed: %s" % (e,))
self.command_returned(command,ret)
def cmd_pipe_in(self,args):
if args.get()=="-noterm":
args.shift()
term=False
else:
term=True
command=args.all()
if not command or not command.strip():
self.error("Command must be given")
return
if term:
cjc_globals.screen.shell_mode()
else:
command+=" 0</dev/null 2>&1"
try:
try:
pipe=os.popen(command,"r")
except OSError,e:
self.error("Shell command execution failed: %s" % (e,))
return
try:
try:
while 1:
l=pipe.readline()
if not l:
break
if l.endswith("\n"):
l=l[:-1]
if not l or l[0] in ("/","\\"):
l="\\"+l
l=unicode(l,cjc_globals.screen.encoding,"replace")
cjc_globals.screen.do_user_input(l)
except (OSError,IOError),e:
self.error("Pipe read failed: %s" % (e,))
finally:
ret=pipe.close()
if ret:
self.command_returned(command,ret)
finally:
if term:
cjc_globals.screen.prog_mode()
def cmd_pipe_out(self,args):
if args.get()=="-noterm":
args.shift()
term=False
else:
term=True
command=args.all()
if not command or not command.strip():
self.error("Command must be given")
return
if term:
cjc_globals.screen.shell_mode()
else:
command+=" >/dev/null 2>&1"
try:
try:
pipe=os.popen(command,"w")
except OSError,e:
self.error("Shell command execution failed: %s" % (e,))
return
try:
try:
if cjc_globals.screen.active_window.buffer:
s=cjc_globals.screen.active_window.buffer.as_string()
s=s.encode(cjc_globals.screen.encoding,"replace")
pipe.write(s)
except (OSError,IOError),e:
self.error("Pipe read failed: %s" % (e,))
finally:
ret=pipe.close()
if ret:
self.command_returned(command,ret)
finally:
if term:
cjc_globals.screen.prog_mode()
ui.CommandTable("shell",50,(
ui.Command("shell",Plugin.cmd_shell,
"/shell [-noterm] command [arg...]",
"Executes given shelll command. If -noterm option is used"
" then the command is not given access to the terminal and the screen"
" is not refreshed after command exits.",
("-noterm","opaque")),
ui.Command("pipe_in",Plugin.cmd_pipe_in,
"/pipe_in command [arg...]",
"Takes shell command output as user input. If -noterm option is used"
" then the command is not given access to the terminal and the screen"
" is not refreshed after command exits.",
("-noterm","opaque")),
ui.Command("pipe_out",Plugin.cmd_pipe_out,
"/pipe_out command [arg...]",
"Feeds shell command with current buffer content. If -noterm"
" option is used then the command is not given access to the"
" terminal and the screen is not refreshed after command exits.",
("-noterm","opaque")),
)).install()
# vi: sts=4 et sw=4