2

I begin all my scripts with %!PS, and I am wondering what I can do to make the scripts system executable. So far i have been runing everything from the ghostscript terminal. I tried making the files executeable, but it results in errors amounting to the script being run line by line, and being missinterpreted.

Heres a simple source:

%!PS
/Times findfont 72 scalefont setfont
306 396 translate % move center to here

4{
         2 2 moveto
        90 rotate
        (H@x0rz) true charpath stroke

}repeat
showpage

and here are the errors that follow when trying to run it.

$ ./rotate.ps 
./rotate.ps: line 1: fg: no job control
./rotate.ps: line 2: /Times: No such file or directory
./rotate.ps: line 3: 306: command not found
./rotate.ps: line 5: 4{: command not found
./rotate.ps: line 6: 2: command not found
./rotate.ps: line 7: 90: command not found
./rotate.ps: line 8: syntax error near unexpected token `true'
./rotate.ps: line 8: `  (H@x0rz) true charpath stroke'

how can I make my postscript files system executable. using Ubuntu Mate 18.04 or later.

EDIT trying to use binfmt-misc::

OK so I am looking at setting up binfmt-misc to execute the postcript, but I am not sure how to set it up. In

#/proc/sys/fs/binfmt_misc$ ls
register  status

the status file says enabled, and the register file is blank.

on the binfmt-misc wiki page I see some examples, but I also see a reference to a "TYPE CODE" and i do not know what that is for postscript.

I tried adding GS:M:MZ::/home/user/bin/gs to register, via vim and echo but I got read and write errors even as root, and now I get permission denied when I try to edit the register file.

 root@xy:/proc/sys/fs/binfmt_misc# echo 'GS:M:MZ::/home/user/bin/gs'  > register  
-bash: echo: write error: Invalid argument

Admitadly, I have no clue what I am doing.

I also tried putting a shebang with the ghostscript path at the first line of my script, and that comes back with ghostscript having an unrecoverable error, it loads a window that immediately closes. Im not sure what to do.

the websites I have been reading are: https://elixir.bootlin.com/linux/v4.6/source/Documentation/binfmt_misc.txt https://en.wikipedia.org/wiki/Binfmt_misc

j0h
  • 15,365

2 Answers2

3

Adapting the example from the linked U&L post works fine enough for me:

$ echo ':postscript:M::%!PS::/usr/bin/gs:'  | sudo tee /proc/sys/fs/binfmt_misc/register
:postscript:M::%!PS::/usr/bin/gs:
$ cat > foo.ps
%!PS
/Times findfont 72 scalefont setfont
306 396 translate % move center to here

4{
         2 2 moveto
        90 rotate
        (H@x0rz) true charpath stroke

}repeat
showpage
$ chmod +x foo.ps
$ ./foo.ps
GPL Ghostscript 9.25 (2018-09-13)
Copyright (C) 2018 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Querying operating system for font files...
Can't find (or can't open) font file /usr/share/ghostscript/9.25/Resource/Font/Times.
Can't find (or can't open) font file Times.
Didn't find this font on the system!
Substituting font Times-Roman for Times.
Loading NimbusRoman-Regular font from /usr/share/ghostscript/9.25/Resource/Font/NimbusRoman-Regular... 4646060 3103684 11124488 9679005 1 done.
>>showpage, press <return> to continue<<

Opens up a ps file with H@x0rz written in various orientations.

muru
  • 207,228
1

Here's a Horrible Hack (TM) that solves your original problem without binfmt. I know that this is not exactly answering the edited question, but I remembered having hacked together something similar for C files in my irresponsible youth, so here it goes.

You can put the following prefix into your PS file and make it executable:

#!/usr/bin/awk !/^#!/ { print >> ".tmp.ps" } END { system("/usr/bin/ghostscript .tmp.ps ; rm .tmp.ps ") }

This runs the entire script through awk, removes all lines starting with '#!' (i.e. the first one), pipes the result into a tmpfile, and runs that through ghostscript.