getc windows != linux

R

Roeland Moors

I'm trying to communicate with a program (gnucap) using ruby.

The versions of ruby (1.8.1) and gnucap (0.34) are the same on windows
and linux.

When I start gnucap, this is the output:

Gnucap 0.34
The Gnu Circuit Analysis Package
Never trust any version less than 1.0
Copyright 1982-2002, Albert Davis
Gnucap comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome
to redistribute it under certain conditions
according to the GNU General Public License.
See the file "COPYING" for details.
gnucap> _



Here is a simple ruby test program:

<-- CODE -->
#!/usr/bin/env ruby -w

gnucap = IO.popen("gnucap", "w+")
output = ""
while output !~ /.*#{"gnucap>"}$/
char = gnucap.getc.chr
output += char
end
puts "ok"
<-- CODE -->

In Linux this program works and shows me "ok". But on windows the
program stops reading after the last newline: "...details."
This means the program never reach ok.
It keeps waiting for input.

Any ideas or suggestions?

Thanks
 
A

Ara.T.Howard

I'm trying to communicate with a program (gnucap) using ruby.

The versions of ruby (1.8.1) and gnucap (0.34) are the same on windows
and linux.

When I start gnucap, this is the output:

Gnucap 0.34
The Gnu Circuit Analysis Package
Never trust any version less than 1.0
Copyright 1982-2002, Albert Davis
Gnucap comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome
to redistribute it under certain conditions
according to the GNU General Public License.
See the file "COPYING" for details.
gnucap> _



Here is a simple ruby test program:

<-- CODE -->
#!/usr/bin/env ruby -w

gnucap = IO.popen("gnucap", "w+")
output = ""
while output !~ /.*#{"gnucap>"}$/
char = gnucap.getc.chr
output += char
end
puts "ok"
<-- CODE -->

In Linux this program works and shows me "ok". But on windows the
program stops reading after the last newline: "...details."
This means the program never reach ok.
It keeps waiting for input.

Any ideas or suggestions?

Thanks

the output from gnucap on window must be line buffered - because it hasn't
sent a newline the last line with 'gnucap> ' on it has not been flushed and is
not available for reading. i bet that, if you ran a few hundred times, you'd
see this behaviour with linux too. i dont know if

gnucap.sync = true

will work

if not perhaps you could write alittle wrapper like this

file: wgnucap.rb
==============
#!/usr/bin/env ruby
STDOUT.sync = true
STDERR.sync = true
cmd = 'gnucap ' + ARGV.join(' ')
exec cmd

though i don't really know about windows...

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 
R

Roeland Moors

Ara.T.Howard said:
the output from gnucap on window must be line buffered - because it hasn't
sent a newline the last line with 'gnucap> ' on it has not been flushed and is
not available for reading. i bet that, if you ran a few hundred times, you'd
see this behaviour with linux too. i dont know if

gnucap.sync = true

will work

if not perhaps you could write alittle wrapper like this

file: wgnucap.rb
==============
#!/usr/bin/env ruby
STDOUT.sync = true
STDERR.sync = true
cmd = 'gnucap ' + ARGV.join(' ')
exec cmd

though i don't really know about windows...
thanks for the respone!

gnucap.sync doesn't work. (I didn't try the wrapper yet)

But maybe there is a different solution to this problem.
Is het possible to detect the last line on the output?
(I don't know the contents of the last line.)
 
R

Roeland Moors

Ara.T.Howard said:
the output from gnucap on window must be line buffered - because it hasn't
sent a newline the last line with 'gnucap> ' on it has not been flushed and is
not available for reading. i bet that, if you ran a few hundred times, you'd
see this behaviour with linux too. i dont know if

gnucap.sync = true

will work

if not perhaps you could write alittle wrapper like this

file: wgnucap.rb
==============
#!/usr/bin/env ruby
STDOUT.sync = true
STDERR.sync = true
cmd = 'gnucap ' + ARGV.join(' ')
exec cmd

though i don't really know about windows...

-a
The wrapper didn't work, but I found a solution
I just send an extra empty command after each command.
 
D

daz

[cc: email]

Roeland said:
[...] I found a solution
I just send an extra empty command after each command.

Thanks for the feedback, Roeland.

Could you show a small example like your original one
with the change you made, please ?

It may be useful to others with a similar problem
and I might try it if I can get gnucap to compile
with Borland 5.5.


Cheers,

daz
 
A

Ara.T.Howard

The wrapper didn't work, but I found a solution I just send an extra empty
command after each command.

this, essentially, how my session library works. when i send a command i
create a spcial string that looks something like

__BEGIN__CMD_42__
__END__CMD_42__

and ask the interpreter (for example sh) to echo the begining string, run the
command, and then echo the end one. that way i'm guaranteed that the output
stream will look like

(junk)__BEGIN__CMD_42__\n(output from command)__END__CMD_42__\n(junk)


it's then pretty straight forward to parse the output out. you may want to
look at my session package - if you pulled out the popen3 and replaced with
popen i can't think of any reason it couldn't be made to work on windows.


-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 
R

Roeland Moors

daz said:
[cc: email]

Roeland said:
Here is a simple ruby test program:

<-- CODE -->
#!/usr/bin/env ruby -w

gnucap = IO.popen("gnucap", "w+")
output = ""
while output !~ /.*#{"gnucap>"}$/
char = gnucap.getc.chr
output += char
end
puts "ok"
<-- CODE -->
[...] I found a solution
I just send an extra empty command after each command.


Thanks for the feedback, Roeland.

Could you show a small example like your original one
with the change you made, please ?

It may be useful to others with a similar problem
and I might try it if I can get gnucap to compile
with Borland 5.5.

<-- CODE -->
#!/usr/bin/env ruby -w

gnucap = IO.popen("gnucap", "w+")
gnucap.puts "" # <--- Empty command
output = ""
while output !~ /.*#{"gnucap>"}$/
char = gnucap.getc.chr
output += char
end
puts "ok"
<-- CODE -->

The output is now:
<-- OUTPUT -->
gnucap>

gnucap>
<-- OUTPUT -->
I stop reading after the first "gnucap>"
When I want to read the output of the next command I first skip the
extra "gnucap>"


I'm thinking of making ruby bindings for gnucap.
When I've got the basics working I will anounce it in this group and the
gnucap mailinglist.


PS:
This is how I compiled MinGW under windows:

- Install mingw 3.1.0-1
- Install msys 1.0.10
- Download en extract gnucap 0.34
- edit the file "gnucap-0.34/src/Make2.mingw32" en change the compiler
from "i586-mingw32msvc-g++" to "mingw32-c++"
- go to gnucap-0.34/src/ en type "make mingw"
- gnucap.exe is now in "gnucap-0.34/src/Msw/"

I don't know if this is the best method, but it works for me.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,219
Latest member
KristieKoh

Latest Threads

Top