user3402786
user3402786

Reputation: 1

open in postscript file fails

Executing the following code

%!PS-Adobe-2.0
%
/bdf { bind def } bind def  
/ldf { load def } bdf 
%
/cm  { 28.3464566929134 mul } bdf 
/icm { 28.3464566929134 div } bdf
%
/Helvetica findfont 20 scalefont setfont
%
/filespec (C:\\test1\\test2\\test3\\logo.eps) def
%
filespec status
{
    4{pop}repeat (EXISTS! \n) print
    gsave
        2.0 cm 25.0 cm translate
        0 0 moveto
        {filespec (r) file}stopped{
            (UPS!? DOES NOT EXIT! \n)print
        }{
            dup cvx exec
            closefile
        }ifelse
    grestore
}{
    (DOES NOT EXIT! \n)print
}ifelse
stroke
showpage

I receice this:

GPL Ghostscript 9.06 (2012-08-08)
Copyright (C) 2012 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Displaying non DSC file C:/__EDB__/test/zwi.ps
Loading NimbusSanL-Regu font from %rom%Resource/Font/NimbusSanL-Regu... 3486280 2151039 3424632 2124503 2 done.

EXITS! 

UPS!? DOES NOT EXIT!?

Ok, I have done some deeper investigation:

Working with "GSview 5.0 2012-01-17" it fails (double click on zwi.ps).

Working with "GPL Ghostscript 9.06 (2012-08-08)" is ok; I get my logo (command line).

I am working with 64 bit Windows 7.

Thanks to all.

Upvotes: 0

Views: 530

Answers (2)

KenS
KenS

Reputation: 31139

I'm assuming this is using Windows because of the file specification, however Ghostscript doesn't need -dNOSAFER except in very special circumstances.

NOSAFER simply prevents SAFER being applied, NOSAFER is intended for use during Ghostscript's own startup routines to allow it to access its own files (it can't start up otherwise). In general you should never use NOSAFER.

I don't see why your back channel output is not inline with the other Ghostscript output, are you executing Ghostscript from the command line, or from inside an application ?

The real problem is that because 'file' returns an error, you are assuming the file does not exist. This is fallacious. The file may well exist, but permissions may prevent Ghostscript from opening the file for reading. The file may have permissions which prevent this, or it may be an OS file, or the file may be open in another application which denies sharing.

You can use the code supplied by luser droog above to show which error occurred, or simply not execute the 'file' in a stopped context, in order to recover the error. Rather than 'undefinedfilename' (caused by the file not existing) I expect you will get an 'invalidfileaccess'.

Note that your PostScript program has an error if the file does not exist:

filespec status
{
...
}{
    pop
    (DOES NOT EXIT! \n)print
}ifelse

If the file does not exist then status returns a single boolean on the stack, value 'false', which is consumed by the ifelse. This means the stack is empty and when the 'else' clause executes 'pop' you will get a stackunderflow error.

Upvotes: 1

luser droog
luser droog

Reputation: 19504

You may need to specify the option -dNOSAFER to ghostscript to enable file operations.

To support the concept that a postscript document (although it is code) is "safe" to run on your computer, many interpreters implicitly disallow all operations on OS files (with the exception of currentfile which contains the program stream, and %stdin, %stdout and %stderr.

You may (I think) verify that this is the case by checking if status is throwing an error.

So wrap your call to status with this:

{ status } stopped { (status threw ) print $error /errorname get =} if

Upvotes: 0

Related Questions