[categories] [index] [all (552)] [latest]
{
1 length
} stopped {
$error /errorname get ==
$error /command get ==
} if
prints
/typecheck
--length--
/x 1 def
/y 2 def
/=== {
{ =only ( ) =only } forall (\n) print
} def
[ [1] (asd) x y ] ===
--nostringval-- asd 1 2
/=== {
{
dup (%) 0 get eq {
pop
} {
( ) dup 0 4 -1 roll put
} ifelse
=only
} forall
(\n) print
} def
[3 4] y x (x: %, y: %, array: %) ===
x: 1, y: 2, array: --nostringval--
65 1 string dup 0 4 -1 roll put
or
/s ( ) def
s 0 65 put
100 300 translate
100 100 scale
2 % width
3 % height
4 % bits_per_comp (4 bits -> 0-F)
[1 0 0 -1 0 0] % transormation matrix
{ % data RGB RGB RGB ...
<
F00 FFF
0F0 000
00F FFF
>
}
false % no multi datasource
3 % RGB
colorimage
/MyDict
<<
/MyKey1 (asd)
/MyKey2 { (sdf) }
>> def
MyDict (MyKey1) get == % (asd)
MyDict (MyKey2) get == % { (sdf) }
MyDict (MyKey3) get == % undefined
errordict /handleerror {
(-- Error occured!n) print
(Error : ) print $error /errorname get ==
(Command : ) print $error /command get ==
(Operands : ) print $error /ostack get ==
(Dicts : ) print $error /dstack get ==
(Exec : ) print $error /estack get ==
stop
} put
With this error:
(asd) 2 div
Will print:
-- Error occured!
Error : /typecheck
Command : --div--
Operands : [(asd) 2]
Dicts : [-dict- -dict- -dict-]
Exec : [--%interp_exit-- .runexec2 -file- -packedarray- null 2 --%stopped_push-- -file- {{/execute0 --cvx-- 1 --.quit--} --if--} {$error /newerror --get-- --and-- {/handleerror --.systemvar-- --exec-- --flush-- true} {false} --ifelse--} false 1 --%stopped_push-- 1990 1 3 --%oparray_pop-- 1989 1 3 --%oparray_pop-- 1977 1 3 --%oparray_pop-- 1833 1 3 --%oparray_pop-- null --%errorexec_pop-- .ruGPL Ghostscript 9.56.1: Unrecoverable error, exit code 1
nexec2 -file- -packedarray- null 2 --%stopped_push-- -file-]
bind def
: add
lookup occurs once at definition:
save
/foo { add } bind def
1 2 foo == % 3
/add { mul } def
1 2 foo == % 3, uses original add
restore
def
alone: add
lookup occurs each time:
save
/foo { add } def
1 2 foo == % 3
/add { mul } def
1 2 foo == % 2, uses redefined add
restore
MyDict {
/v exch def
/k exch def
} forall
connect on port 9100:
nc 172.20.10.4 9100
type this:
%!PS
executive
get this:
PostScript(r) Version 3018.102
(c) Copyright 1984-2004 Adobe Systems Incorporated.
Typefaces (c) Copyright 1981 Linotype-Hell AG and/or its subsidiaries.
All Rights Reserved.
PS>
or that:
M C240FW
Version 3011.010
PS>
systemdict (product) get
(%Calendar%) currentdevparams { exch =print ( )print =string cvs print (\n) print } forall
Day 23
Minute 7
Month 3
Weekday 0
Hour 13
Type Special
Second 42
Running true
Year 2025
%!PS-Adobe-2.0
% gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="output-%d.pdf" m.ps
/Courier findfont 12 scalefont setfont
50 700 moveto
1 20 string cvs show
showpage
50 700 moveto
2 20 string cvs show
showpage
50 700 moveto
3 20 string cvs show
showpage
gs -DNOSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="%d.pdf" main.ps
PNG
gs -DNOSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -sOutputFile="%d.png" -r600 main.ps
GIF
convert x*.png x.gif
"BEST OBFUSCATED ARTWORK" at 1993 Obfuscated PostScript Contest
Deciphered from https://stuff.mit.edu/afs/sipb/contrib/postscript/obfuscated-1993/
% "load def" pairs of names
/a/add
/s/sub
/m/mul
/d/div
count 2 idiv { load def } repeat
/decode {
{
( ) dup 0 4 -1 roll put % char to string
dup (0) ge 1 index (9) le and { % is digit?
cvi % char to int
} { % is operator
cvn % char to name
dup where { % name known?
exch get exec % exec mapped operator
} {
pop % name unknown, ignore
} ifelse
} ifelse
} forall
} def
(Test 1 2 a -> 3) =
(1 2 a) decode =
(Testing 3 4 m -> 12) =
(3 4 m) decode =
(Test 12a3m -> 9) =
(12a3m) decode =
(Test 23a4m -> 20) =
(23a4m) decode =
gv x.ps
stty raw -echo; cat >> x.txt
/Sleep { 1000000 mul {} repeat } def
/buffer 8 string def
/FILENAME (/Users/nseriot/Desktop/x.txt) def
/shouldTruncate false def
% create file
{
FILENAME (w) file closefile } stopped {
(Error creating file ) print FILENAME = flush
quit
} if
0 1 10000 {
/i exch def
(-- ) =only i ==
% read file contents
{
FILENAME (r) file buffer readstring { (full) == } { (not full) == } ifelse
dup length 0 ne /shouldTruncate exch def
{ = } forall
} stopped {
(stopped) ==
stop
} if
% truncate file if needed
shouldTruncate {
{
FILENAME (w) file closefile
} stopped {
(Error truncating file) =
} if
/shouldTruncate false def
} if
flush
200 Sleep
} for
x.ps
{
(%stdin) (r) file read {
== flush
} if
} loop
Terminal 1: read pipe
rm /tmp/p; mkfifo /tmp/p; cat x.ps /tmp/p | gv -
Terminal 2: fill pipe
stty raw -echo; cat > /tmp/p
/printUsage {
(Usage:) =
( a - some stuff) =
( b - other stuff) =
( q - quit) =
} def
printUsage
{
/userInput 128 string def
(>) print flush
(%lineedit) (r) file userInput readline
pop % bool
token not { () } if
{
dup (q) eq { quit } if
dup (a) eq {
pop token pop
(a) =
exit
} if
dup (b) eq {
pop token pop
(b) =
exit
} if
clear
printUsage
exit
} loop
} loop
/DeviceRGB setcolorspace
100 400 translate
50 50 scale
<<
/ImageType 1
/Width 2
/Height 3
/BitsPerComponent 8
/ColorSpace /DeviceRGB
/ImageMatrix [1 0 0 -1 0 0]
/DataSource currentfile /ASCIIHexDecode filter
/Interpolate false % default is false
>> image
FF0000 00FF00
0000FF FFFF00
00FFFF FF00FF
>
[ 10 { 0 } repeat ]
[0 0 0 0 0 0 0 0 0 0]
/S 32 def
/_m_DrawSide {
gsave
concat
0 0 S S rectfill
0 setgray
0 0 S S rectstroke
grestore
} def
/_t_l_r_DrawCube {
aload pop setrgbcolor [ 1 0.5 0 -1 0 0 ] _m_DrawSide % top
aload pop setrgbcolor [ -1 0.5 0 -1 0 0 ] _m_DrawSide % left
aload pop setrgbcolor [ 1 0.5 -1 0.5 0 0 ] _m_DrawSide % right
} def
200 500 translate
[1 0 0] [0 1 0] [0 0 1] _t_l_r_DrawCube
Store last procedure as needed
/lastProc (-) def
/!{ /lastProc exch def } def
/CreateOrTruncateInputFile {
(CreateOrTruncateInputFile)!
% ...
} bind def
Print last saved procedure in error handler
errordict /handleerror {
(-- Error occured! n) print
(Error : ) print $error /errorname get ==
(Procedure: ) print lastProc ==
(Command : ) print $error /command get ==
(Operands : ) print $error /ostack get ==
(Dicts : ) print $error /dstack get ==
(Exec : ) print $error /estack get ==
stop
} put
Errors will look like:
-- Error occured!
Error : /stackunderflow
Procedure: (CheckFullLine_b_rowsFull_)
Command : --put--
Operands : [false false]
(...)
{1 2 add}
cvlit
==
[1 2 add]
[1 2 add]
cvx
exec
==
3
999 3 string cvs print
pstack
or
/printStack {
0 1 count 3 sub {
dup ( ) cvs =only (: ) =only index dup dup xcheck =only ( ) =only type =only ( ) =only ==
} for
} def
(hello) 42 [1 2 3] {dup mul} 3.14159
printStack
will print:
0: false realtype 3.14159
1: true arraytype {dup mul}
2: false arraytype [1 2 3]
3: false integertype 42
4: false stringtype (hello)
(=======================================) ==
pstack
(=======================================) ==
quit
Ghostscript specific:
=only
and
==only
See https://stackoverflow.com/questions/57983225/postscript-tokens-string-only-etc
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 500 400
100 200 translate
0 0 1 setrgbcolor
/N 10 def
/STEP 15 def
1 1 N {
STEP mul /r exch def
r 0 r 0 180 arc stroke
N 2 mul STEP mul r sub 0 r 180 0 arc stroke
} for
or use:
<< /PageSize [800 600] >> setpagedevice
/objMethods
<<
/addOne {
/_self exch def
_self/ivar1 _self/ivar1 get 1 add put
}
>> def
/call {
1 index /_methods get
exch get exec
} def
/d1
<<
/ivar1 1
/ivar2 2
/_methods objMethods
>> def
/d2
<<
/ivar1 10
/ivar2 20
/_methods objMethods
>> def
% Use object 1
d1/ivar1 get ==
d1/addOne call
d1/ivar1 get ==
% Use object 2
d2/ivar1 get ==
d2/addOne call
d2/ivar1 get ==
prints:
1
2
10
11
/call { exch begin load exec end } def
/undoManager 10 dict def
undoManager begin
/MAX 3 def
/ringBuffer MAX array def
/head 0 def
/print { ringBuffer == } def
/do {
ringBuffer head 3 -1 roll put
/head head 1 add MAX mod def
} def
/reset { /ringBuffer MAX array def } def
/undo {
/head head 0 eq { MAX 1 sub } { head 1 sub } ifelse def
ringBuffer head get % result
ringBuffer head null put
} def
end
3 undoManager /do call
4 undoManager /do call
5 undoManager /do call
6 undoManager /do call
undoManager /print call
[6 4 5]
undoManager /undo call ==
6
undoManager /print call
[null 4 5]
/ListFiles {
(*) { == } 256 string filenameforall
} def
/_s_fn_WriteFile {
/f exch (w) file def
f exch writestring
f flushfile
f closefile
} def
/_fn_ReadFile_s_ {
(r) file
256 string
readstring
pop
} def
(123) (test.txt) _s_fn_WriteFile
ListFiles
(test.txt)
(test.txt) _fn_ReadFile_s_ ==
(123)
(test2.txt) deletefile
ListFiles
-
gs -q -c "1 2 add = quit"
3
eg. Create a 200x200 PDF file
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf -c "<</PageSize [200 200]>> setpagedevice showpage"
/sleep {
% adjust value to match 1 second delay
100000 mul {} repeat
} def
(a) == flush
1000 sleep % milli-seconds
(b) == flush
%!PS
% gs -q http.ps | nc -l 8080
% http://localhost:8080/
(HTTP/1.1 200 OK\r\n) print
(Content-Length: 16\r\n) print
(Content-Type: text/plain\r\n) print
(\r\n) print
(Hello PostScript\r\n) print
flush
quit
/userInput 128 string def
(>) print flush
(%lineedit) (r) file userInput readline
pop % bool
{ token not{exit}if == } loop
%!PS
% nc 172.16.158.21 9100
/Courier findfont 12 scalefont setfont
32 740 moveto
/s 100 string def
(>) print flush
(%lineedit) (r) file s readline
pop
show
showpage