popen hangs out for user input

F

felix.leg

Hi,

I want to use Ruby with latex,so I wrote a simple program which waits
for "\typein" command called from pdflatex:

<CODE>
#!/usr/bin/ruby

file = IO.popen('pdflatex -interaction scrollmode test.tex','r+') #1

until file.readline.include? 'typein'; end
file.write "a line from RUBY"
file.readlines #2
file.close
</CODE>

If I run the command from #1 in a sheel it works right: scrolls
"standard" latex asks, and stops for my "\@typein=". After I answer it
continues normally.

But when I want do the same in Ruby with above code, it hangs out
waiting for input. It looks like #2 causes this, but I don't know why...
 
J

Jeremy Bopp

Hi,

I want to use Ruby with latex,so I wrote a simple program which waits
for "\typein" command called from pdflatex:

<CODE>
#!/usr/bin/ruby

file = IO.popen('pdflatex -interaction scrollmode test.tex','r+') #1

until file.readline.include? 'typein'; end
file.write "a line from RUBY"
file.readlines #2
file.close
</CODE>

If I run the command from #1 in a sheel it works right: scrolls
"standard" latex asks, and stops for my "\@typein=". After I answer it
continues normally.

But when I want do the same in Ruby with above code, it hangs out
waiting for input. It looks like #2 causes this, but I don't know why...

It's probably because the write buffer for the pipe is not being
flushed. My preferred option to fix this sort of thing is to make the
file object synchronous:

file = IO.popen(.....)
file.sync = true

However, you might prefer to explicitly flush things instead:

file.write(.....)
file.flush
...
file.write(.....)
file.write(.....)
file.flush

-Jeremy
 
J

Jeremy Bopp

It's probably because the write buffer for the pipe is not being
flushed. My preferred option to fix this sort of thing is to make the
file object synchronous:

file = IO.popen(.....)
file.sync = true

However, you might prefer to explicitly flush things instead:

file.write(.....)
file.flush
...
file.write(.....)
file.write(.....)
file.flush

One more thing: you may need to insert a newline at the end of the
strings you write or use #puts instead of #write. Most interactive
programs like this expect a newline character to flag the end of data
entry for a field.

-Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top