mirror of
https://github.com/akelge/zsh
synced 2025-07-04 21:49:05 +00:00
Added conque, terminal emulator
This commit is contained in:
195
vim/autoload/conque_term/conque_subprocess.py
Normal file
195
vim/autoload/conque_term/conque_subprocess.py
Normal file
@ -0,0 +1,195 @@
|
||||
# FILE: autoload/conque_term/conque_subprocess.py {{{
|
||||
# AUTHOR: Nico Raffo <nicoraffo@gmail.com>
|
||||
# WEBSITE: http://conque.googlecode.com
|
||||
# MODIFIED: 2010-11-15
|
||||
# VERSION: 2.0, for Vim 7.0
|
||||
# LICENSE:
|
||||
# Conque - Vim terminal/console emulator
|
||||
# Copyright (C) 2009-2010 Nico Raffo
|
||||
#
|
||||
# MIT License
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE. }}}
|
||||
|
||||
"""
|
||||
ConqueSubprocess
|
||||
|
||||
Create and interact with a subprocess through a pty.
|
||||
|
||||
Usage:
|
||||
|
||||
p = ConqueSubprocess()
|
||||
p.open('bash', {'TERM':'vt100'})
|
||||
output = p.read()
|
||||
p.write('cd ~/vim' + "\r")
|
||||
p.write('ls -lha' + "\r")
|
||||
output += p.read(timeout = 500)
|
||||
p.close()
|
||||
"""
|
||||
|
||||
if CONQUE_PLATFORM == 'nix':
|
||||
import os
|
||||
import signal
|
||||
import pty
|
||||
import tty
|
||||
import select
|
||||
import fcntl
|
||||
import termios
|
||||
import struct
|
||||
|
||||
|
||||
class ConqueSubprocess:
|
||||
|
||||
# process id
|
||||
pid = 0
|
||||
|
||||
# stdout+stderr file descriptor
|
||||
fd = None
|
||||
|
||||
# constructor
|
||||
def __init__(self): # {{{
|
||||
self.pid = 0
|
||||
# }}}
|
||||
|
||||
# create pty + subprocess
|
||||
def open(self, command, env={}): # {{{
|
||||
|
||||
# parse command
|
||||
command_arr = command.split()
|
||||
executable = command_arr[0]
|
||||
args = command_arr
|
||||
|
||||
# try to fork a new pty
|
||||
try:
|
||||
self.pid, self.fd = pty.fork()
|
||||
|
||||
except:
|
||||
|
||||
return False
|
||||
|
||||
# child proc, replace with command after altering terminal attributes
|
||||
if self.pid == 0:
|
||||
|
||||
# set requested environment variables
|
||||
for k in env.keys():
|
||||
os.environ[k] = env[k]
|
||||
|
||||
# set some attributes
|
||||
try:
|
||||
attrs = tty.tcgetattr(1)
|
||||
attrs[0] = attrs[0] ^ tty.IGNBRK
|
||||
attrs[0] = attrs[0] | tty.BRKINT | tty.IXANY | tty.IMAXBEL
|
||||
attrs[2] = attrs[2] | tty.HUPCL
|
||||
attrs[3] = attrs[3] | tty.ICANON | tty.ECHO | tty.ISIG | tty.ECHOKE
|
||||
attrs[6][tty.VMIN] = 1
|
||||
attrs[6][tty.VTIME] = 0
|
||||
tty.tcsetattr(1, tty.TCSANOW, attrs)
|
||||
except:
|
||||
|
||||
pass
|
||||
|
||||
# replace this process with the subprocess
|
||||
os.execvp(executable, args)
|
||||
|
||||
# else master, do nothing
|
||||
else:
|
||||
pass
|
||||
|
||||
# }}}
|
||||
|
||||
# read from pty
|
||||
# XXX - select.poll() doesn't work in OS X!!!!!!!
|
||||
def read(self, timeout=1): # {{{
|
||||
|
||||
output = ''
|
||||
read_timeout = float(timeout) / 1000
|
||||
|
||||
try:
|
||||
# read from fd until no more output
|
||||
while 1:
|
||||
s_read, s_write, s_error = select.select([self.fd], [], [], read_timeout)
|
||||
|
||||
lines = ''
|
||||
for s_fd in s_read:
|
||||
try:
|
||||
lines = os.read(self.fd, 32)
|
||||
except:
|
||||
pass
|
||||
output = output + lines.decode('utf-8')
|
||||
|
||||
if lines == '':
|
||||
break
|
||||
except:
|
||||
pass
|
||||
|
||||
return output
|
||||
# }}}
|
||||
|
||||
# I guess this one's not bad
|
||||
def write(self, input): # {{{
|
||||
try:
|
||||
if CONQUE_PYTHON_VERSION == 2:
|
||||
os.write(self.fd, input)
|
||||
else:
|
||||
os.write(self.fd, bytes(input, 'utf-8'))
|
||||
except:
|
||||
|
||||
pass
|
||||
# }}}
|
||||
|
||||
# signal process
|
||||
def signal(self, signum): # {{{
|
||||
try:
|
||||
os.kill(self.pid, signum)
|
||||
except:
|
||||
pass
|
||||
# }}}
|
||||
|
||||
# close process
|
||||
def close(self): # {{{
|
||||
self.signal(15)
|
||||
# }}}
|
||||
|
||||
# get process status
|
||||
def is_alive(self): #{{{
|
||||
|
||||
p_status = True
|
||||
|
||||
try:
|
||||
if os.waitpid(self.pid, os.WNOHANG)[0]:
|
||||
p_status = False
|
||||
except:
|
||||
p_status = False
|
||||
|
||||
return p_status
|
||||
|
||||
# }}}
|
||||
|
||||
# update window size in kernel, then send SIGWINCH to fg process
|
||||
def window_resize(self, lines, columns): # {{{
|
||||
try:
|
||||
fcntl.ioctl(self.fd, termios.TIOCSWINSZ, struct.pack("HHHH", lines, columns, 0, 0))
|
||||
os.kill(self.pid, signal.SIGWINCH)
|
||||
except:
|
||||
pass
|
||||
|
||||
# }}}
|
||||
|
||||
|
||||
# vim:foldmethod=marker
|
Reference in New Issue
Block a user