473,383 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 'pqrstuvwxyzPQRST':
for y in '0123456789abcdef':
pty_name = '/dev/pty' + x + y
try:
fd = os.open(pty_name, os.O_RDWR)
except os.error:
continue
return (fd, '/dev/tty' + x + y)

def slave_open(tty_name):
result = os.open(tty_name, 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_fd, STDIN_FILENO)
os.dup2(slave_fd, STDOUT_FILENO)
os.dup2(slave_fd, 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_new_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
Jul 18 '05 #1
0 1940

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: les ander | last post by:
Hi, in perl one can do all sorts of stuff in a single line form the command shell. python has the -c option. it is easy to do stuff like, python -c "print 10+20" etc statements but suppose I...
4
by: Dennis | last post by:
Hi, I suppose this post could fall under comp.os.linux/comp.os.unix as well, but I figured since my end goal was control from C++ code I figured this group might be the place to start. As the...
2
by: Paddy | last post by:
Hi, Anyone know of something that can turn an interactive bash or tcsh session into a test the way doctest does? - Cheers, Paddy.
4
by: Gregory Bloom | last post by:
I'm running Python 2.5 under Windows. If I fire up IDLE and enter: it works like a champ, opening the page in Firefox. Same thing goes from a Windows cmd shell: it works as advertised. But...
12
by: Krypto | last post by:
I have been using python shell to test small parts of the big program. What other ways can I use the shell effectively. My mentor told me that you can virtually do anything from testing your...
1
by: Svenn Are Bjerkem | last post by:
Hi, as a user on a linux system I am member of the groups "users" and "design" with users as my default group. To controll the accessibility of some parts of the file system, creation of files and...
1
by: iamforsecurity | last post by:
Hi, I am a newbie in Shell Script. I am trying to open 10 new sessions,each session running on a separate thread. For each session, I open a new xterm, go to a particular directory and run a...
4
by: vokinloksar | last post by:
hi using python and wpython. when using run module or python shell on the run menu in the GUI i get "socket error, connection refused". it worked before, what si wrong now? and i cant...
9
by: gaurav kashyap | last post by:
Dear all, Can u tell me a python program that when executed in a shell prompt,opens another shell prompt. Simply i want to open a shell prompt from a python program. Thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.