roxen.lists.pike.general

Subject Author Date
Communicating with a spawned program ian Carr-de Avelon <carrdeavelon[at]gmail[dot]com> 14-09-2009
Some time ago I asked for ideas on typesetting data from Pike scripts. In
the end I decided to do it in Postscript, using ghostscript to measure how
much space things take up on the page. As I could not figure out how to
communicate with ghostscrpt as a spawned program I did it in a very slow way
and I'd now like to improve it.

I can put postscript commands in a file and get the ghostscript output like:
Stdio.write_file("temp.ps", out);
out="gs < temp.ps";
in=Process.popen(out);

But this is ugly and means restarting ghostscript every time. I thought I'd
be able to spawn ghostscript and have the pike script communicate with
ghostscript as if it were me typing at a console, by  using spawn:

Stdio.Stream gs_in, gs_out, gs_err;
string to;
Process.spawn("gs",gs_in, gs_out, gs_err);
to=gs_in->read(10);
werror("From gs %s\n", to);

I'm not sure if I shouldread from gs_in or gs_out here , but either way, I
expect to see Pike print the first 10 lettters of the ghostscript startup
message, but instead I see a crash:
This software comes with NO WARRANTY: see the file PUBLIC for details.
GS>
Unexpected interpreter error -12.
Error object: (f80)op(0)0x80b3070
Operand stack at 0x85c7ec8:
0x85bc4e4: 0x03 file --S-rx--- 0x0001 0x085ac7e8
0x85bc4ec: 0x01 bool --F------ 0x0001 0x085e0001 = true
0x85bc4f4: 0x0b int  --F------ 0x0841 0x00000000 = 0
0x85bc4fc: 0x12 str  --L------ 0x0032 0x0875252e
Execution stack at 0x85c7e8c:
0x85bde4c: 0x0f oper --F---e-- 0x0000 0x080a1680 = %interp_exit
0x85bde54: 0x0d name --S---e-- 0x030d 0x085abc00 = .runexec2
0x85bde5c: 0x03 file --G-rx--- 0x0001 0x08764d8c
0x85bde64: 0x05 mpry --G-rxe-- 0x0006 0x085dfd94
0x85bde6c: 0x0e null --F---e-- 0x0003 0x080ae1a0
0x85bde74: 0x0e null --F------ 0x0823 0x085a6ecc
0x85bde7c: 0x0b int  --F------ 0x0002 0x00000002 = 2
0x85bde84: 0x0f oper --F---e-- 0x0000 0x080ae130 = %stopped_push
0x85bde8c: 0x03 file --G-rxe-- 0x0001 0x08764d8c
0x85bde94: 0x0e null --F---e-- 0x0002 0x080ae1a0
0x85bde9c: 0x05 mpry --G-rxe-- 0x0008 0x085dfb46
0x85bdea4: 0x0f oper --F---e-- 0x0000 0x080ae0f0 = %loop_continue
0x85bdeac: 0x05 mpry --G-rxe-- 0x0005 0x085dfb58
0x85bdeb4: 0x0e null --F---e-- 0x0003 0x080ae1a0
0x85bdebc: 0x01 bool --F------ 0x0000 0x080b0000 = false
0x85bdec4: 0x0b int  --F------ 0x0000 0x00000001 = 1
0x85bdecc: 0x0f oper --F---e-- 0x0000 0x080ae130 = %stopped_push
0x85bded4: 0x0f oper --F---e-- 0x0000 0x080b7300 = @0x80b7300
0x85bdedc: 0x0f oper --F---e-- 0x0000 0x080b5a90 = %s_proc_read_continue
0x85bdee4: 0x03 file --S-rx--- 0x0001 0x085ac7e8
0x85bdeec: 0x0b int  --F------ 0x085b 0x00000000 = 0
Dictionary stack at 0x85c7e34:
0x85c7aec: 0x02 dict --Gwrx--- 0x0000 0x085ae63c
0x85c7af4: 0x02 dict --Gwrx--- 0x0000 0x085fed7c
0x85c7afc: 0x02 dict --Lwrx--- 0x0000 0x085b7fb0

Any ideas how I can do what I'm attepting to do and have ghostscript
interact with pike? Preferably without resorting to writing a ghostscript
API CMOD. That may be an option in this case, but I'd like a general
solution for interfacing pike to interactive command line programs.
Yours
Ian