The shell in the xterm

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stelios Xanthakis

    #1

    The shell in the xterm

    Hi.

    I'm trying to open an xterm in slave mode connected
    to a pseudo tty with an interactive python shell.
    This can be useful when working in a GUI to pop up
    xterm'd shells. The program below is a first attempt.
    It opens an xterm with an interactive python interpreter
    in which we can even run "os.system ('bash')" and
    call vi. However there are problems...

    ------------------------------------------------------
    import os
    import code
    import thread
    import time
    import sys

    STDIN_FILENO = 0
    STDOUT_FILENO = 1
    STDERR_FILENO = 2

    CHILD = 0

    def openpty ():
    for x in 'pqrstuvwxyzPQR ST':
    for y in '0123456789abcd ef':
    pty_name = '/dev/pty' + x + y
    try:
    fd = os.open(pty_nam e, os.O_RDWR)
    except os.error:
    continue
    return (fd, '/dev/tty' + x + y)

    def slave_open(tty_ name):
    result = os.open(tty_nam e, os.O_RDWR)
    try:
    from fcntl import ioctl, I_PUSH
    except ImportError:
    return result
    try:
    ioctl(result, I_PUSH, "ptem")
    ioctl(result, I_PUSH, "ldterm")
    except IOError:
    pass
    return result

    def fork():
    master_fd, slave_fdn = openpty()
    slave_fd = slave_open (slave_fdn)
    pid = os.fork()
    if pid == CHILD:
    # Establish a new session.
    os.setsid()
    os.close(master _fd)

    # Slave becomes stdin/stdout/stderr of child.
    os.dup2(slave_f d, STDIN_FILENO)
    os.dup2(slave_f d, STDOUT_FILENO)
    os.dup2(slave_f d, STDERR_FILENO)
    if (slave_fd > STDERR_FILENO):
    os.close (slave_fd)

    # Parent and child process.
    return pid, master_fd, slave_fdn

    def thrr (cmd):
    thread.start_ne w_thread (os.system, (cmd,))

    pid, fd, n = fork ()

    if pid > 0:
    print 'parent, reading', n[8:]
    cmd = 'xterm -S'+n[8:]+str (fd)
    thrr (cmd)
    time.sleep (3)
    print "Send a command to the xterm!"
    os.write (fd, 'print "Hi!"\n')
    time.sleep (10)
    print "leave..."
    else:
    code.interact ()
    print "terminated "
    sys.exit (1)
    ---------------------------------------

    This is wrong. The fact that the interactive interpreter is executed in
    a different process, means that it has its own data and cannot modify
    the main program's state.

    If we use threads, because of CLONE_FILES, we can't connect the
    thread's stdin/stdout to the pty.

    There are other problems, like xterm complaining that it cannot
    'stat /dev/pts/p0', how do we kill the xterm once the
    code.interact() terminates, zombies, etc.

    I'm confused. Ideas?

    Stelios
Working...