ptyrun: Run anything in a pseudo-tty

444 views
Skip to first unread message

Jeroen Demeyer

unread,
Apr 14, 2011, 4:08:57 AM4/14/11
to sage-devel
Hello sage-devel,

I wrote a program called "ptyrun", which runs an arbitrary program in a
pseudo-tty. I personally found it useful, so maybe somebody else will
find it useful also. You can find it at
http://boxen.math.washington.edu/home/jdemeyer/ptyrun/ptyrun.c

Compile this with
$ gcc -O2 -Wall -lutil ptyrun.c -o ptyrun

You use it by executing
$ ptyrun COMMAND
This executes COMMAND in a pseudo-terminal. From the outside, it looks
like COMMAND just runs normally, but COMMAND itself will see that it's
running in a pty. COMMAND will be executed through /bin/sh, so it can
be any shell command.

Since Python changes its output buffering strategies depending on
whether the output is a terminal or not, this can be used to emulate
terminal behaviour. I personally found this useful in the following
setting:

$ ./sage -t -verbose devel/sage/sage | tee test.log

Since the doctesting process (i.e. Python) does buffering, the output
will not be fluent. Instead, if you do

$ ptyrun "./sage -t -verbose devel/sage/sage" | tee test.log

the output will appear line-by-line, just as if the "tee test.log"
wasn't there.


Jeroen.

David Kirkby

unread,
Apr 14, 2011, 5:37:35 AM4/14/11
to sage-...@googlegroups.com
On 14 April 2011 09:08, Jeroen Demeyer <jdem...@cage.ugent.be> wrote:
> Hello sage-devel,
>
> I wrote a program called "ptyrun", which runs an arbitrary program in a
> pseudo-tty.  I personally found it useful, so maybe somebody else will
> find it useful also.  You can find it at
> http://boxen.math.washington.edu/home/jdemeyer/ptyrun/ptyrun.c
>
> Compile this with
> $ gcc -O2 -Wall -lutil ptyrun.c -o ptyrun
On a laptop running Solaris 11 Express:

drkirkby@laptop:~$ gcc -O2 -Wall -lutil ptyrun.c -o ptyrun
ptyrun.c:33:17: fatal error: pty.h: No such file or directory
compilation terminated.


Dave

Jeroen Demeyer

unread,
Apr 14, 2011, 7:52:43 AM4/14/11
to sage-...@googlegroups.com

Well, I developed this program for Linux machines. I don't know what
the Solaris equivalent is for the openpty() function. Maybe there isn't
even one??

Jeroen.

David Kirkby

unread,
Apr 14, 2011, 8:05:38 AM4/14/11
to sage-...@googlegroups.com
On 14 April 2011 12:52, Jeroen Demeyer <jdem...@cage.ugent.be> wrote:

> Well, I developed this program for Linux machines.  I don't know what
> the Solaris equivalent is for the openpty() function.  Maybe there isn't
> even one??
>
> Jeroen.


II dont' think there is one. That is part of glibc.

http://h30097.www3.hp.com/docs/transition/openpty.pdf

describes ho to implement it on HP-UX. I guess the same would work for
OS X and Solaris.

dave

Jeroen Demeyer

unread,
Apr 14, 2011, 3:35:14 PM4/14/11
to sage-...@googlegroups.com
On 2011-04-14 14:05, David Kirkby wrote:
> http://h30097.www3.hp.com/docs/transition/openpty.pdf
>
> describes ho to implement it on HP-UX. I guess the same would work for
> OS X and Solaris.
Thanks for the pointer. If anybody finds my "ptyrun" particularly
useful, I might consider porting it to Solaris. But for the moment, I
have no reason to do so (it served its purpose for me on Linux).

Jeroen.

Justin C. Walker

unread,
Apr 14, 2011, 5:20:35 PM4/14/11
to sage-...@googlegroups.com

On Apr 14, 2011, at 01:08 , Jeroen Demeyer wrote:

> Hello sage-devel,
>
> I wrote a program called "ptyrun", which runs an arbitrary program in a
> pseudo-tty. I personally found it useful, so maybe somebody else will
> find it useful also. You can find it at
> http://boxen.math.washington.edu/home/jdemeyer/ptyrun/ptyrun.c
>
> Compile this with
> $ gcc -O2 -Wall -lutil ptyrun.c -o ptyrun

FWIW, on Mac OS X (at least, 10.6), there is no "pty.h", but there is a "openpty()". Use "util.h" in place of "pty.h".

Also, there is no libutil. Just a simple "make ptyrun" will complete w/o errors (once the header change is made). I have not yet tried the program, so I don't vouch for its effectiveness on Mac OS X.

HTH

Justin

--
Justin C. Walker, Curmudgeon at Large
Institute for the Absorption of Federal Funds
-----------
I'm beginning to like the cut of his jibberish.
-----------

koffie

unread,
Apr 15, 2011, 4:46:30 AM4/15/11
to sage-devel


On Apr 14, 10:08 am, Jeroen Demeyer <jdeme...@cage.ugent.be> wrote:
> Hello sage-devel,
>
> I wrote a program called "ptyrun", which runs an arbitrary program in a
> pseudo-tty.  I personally found it useful, so maybe somebody else will
> find it useful also.  
> I personally found this useful in the following
> setting:
>
> $ ./sage -t -verbose devel/sage/sage | tee test.log
>
> Since the doctesting process (i.e. Python) does buffering, the output
> will not be fluent. Instead, if you do
>
> $ ptyrun "./sage -t -verbose devel/sage/sage" | tee test.log
>
> the output will appear line-by-line, just as if the "tee test.log"
> wasn't there.
>
Nice program. This will be very useful when doing huge computations
again of which I want the output logged and also want to check on the
progress from time to time.
Note for the non linux users. To deal with this particular use case we
could also use any of the solutions as found on
http://stackoverflow.com/questions/107705/python-output-buffering
The environment variable solution will work out of the box with sage
also. Maybe -u mentioned there would also be a good command line
option for sage.

> Jeroen.
Reply all
Reply to author
Forward
0 new messages