Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem reading from command pipelined channel

Status
Not open for further replies.

iritab

Technical User
Jul 9, 2012
2
0
0
US
Hello,

I'm trying to read from a command pipelined channel but the channel closes before I've read the wanted output. I'm trying to read the boundingbox parameters (see below) but the channel closes at the very same line, before the line is properly read.

The wanted data could be extracted by using the exceptional return but I'm curious as to why this behavior appears and what could be a proper way to perform this read.

Any ideas and/or comments are much appreciated.

Here is the code I'm running:
Code:
set cmd "|\"C:/Program Files/gs/gs9.05/bin/gswin64c.exe\" -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox \"C:/Program Files (x86)/term/outTest.pdf\""

set f [open $cmd "r"]

while {true} {
	if {[gets $f line] < 0} {
		if {[catch {close $f} msg]} {
			puts "Channel closed (<0):\n$msg"
		}
		break
	} elseif {[eof $f]} {
		if {[catch {close $f} msg]} {
			puts "Channel closed (EOF):\n$msg"
		}
		break
	} else {
		puts $line
	}
}
Output:
Code:
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
Channel closed (<0):
%%BoundingBox: 0 336 596 756
%%HiResBoundingBox: 0.090000 336.563990 595.007982 755.909977

Here is the output when the GS script is run in the command promt:
Code:
C:\Users\Me>"C:\Program Files\gs\gs9.05\bin\gswin64c.exe" -dSAFER -dNOPAUSE -dBA
TCH -sDEVICE=bbox "C:\Program Files (x86)\term\outTest.pdf"
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
%%BoundingBox: 0 336 596 756
%%HiResBoundingBox: 0.090000 336.563990 595.007982 755.909977

C:\Users\Me>

Thanks

Peter
 
Are you sure it's the pipeline that's misbehaving and not Ghostscript? I don't know how the arguments work for GS so I would try a simpler pipeline and work up from there. Also, "while true" is going to go berserk if you ever get stuck in "else".

_________________
Bob Rashkin
 
Thanks for the reply,

Hmm, it seems like GS is returning a '-1' right after the line stating "Page 1" causing the pipeline to close.

Otherwise I believe the GS arguments to be ok. And I hear you regarding while{true}-loop. It's not pretty and was merely used for debugging. It will be adjusted as soon as this start to work [smile].

If I'm not mistaking, the pipeline automatically closes when a '-1' is read (please correct me if I'm wrong).

So if my thesis is correct and a -1 is returned after "Page 1" causing the pipeline to automatically close: Is there a way to get tcl to disregard the first '-1' and keep on reading until the second '-1'?

 
I know nothing of GS. As I suggested above, I'd work out the kinks in the process pipeline with something simpler. Also, a lot of these OS-type deals in Tcl were really designed for Unix and I don't trust that they always work well in Windows (just my own paranoia).

Anyway, you're telling it to close if it reads <0 so you could just as well put in a counter and complicate your condition:
Code:
if {[gets $f line] < 0 && $counter>1}

That said, I think GS sending -1 is ominous, but I don't know anything about GS as I said.

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top