home *** CD-ROM | disk | FTP | other *** search
/ swCHIP 1991 January / swCHIP_95-1.bin / utility / gs333ini / gs3.33 / gs_init.ps < prev    next >
Text File  |  1995-12-09  |  36KB  |  1,170 lines

  1. %    Copyright (C) 1989, 1995 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % Initialization file for the interpreter.
  16. % When this is run, systemdict is still writable.
  17.  
  18. % Comment lines of the form
  19. %    %% Replace <n> <file(s)>
  20. % indicate places where the next <n> lines should be replaced by
  21. % the contents of <file(s)>, when creating a single merged init file.
  22.  
  23. % Check the interpreter revision.  NOTE: the interpreter code requires
  24. % that the first non-comment token in this file be an integer.
  25. 333
  26. dup revision ne
  27.  { (gs: Interpreter revision \() print revision 10 string cvs print
  28.    (\) does not match gs_init.ps revision \() print 10 string cvs print
  29.    (\).\n) print flush null 1 .quit
  30.  }
  31. if pop
  32.  
  33. % Acquire userdict, and set its length if necessary.
  34. /userdict where
  35.  { pop userdict maxlength 0 eq }
  36.  { true }
  37. ifelse
  38.  {        % userdict wasn't already set up by iinit.c.
  39.    /userdict
  40.    currentdict dup 200 .setmaxlength        % userdict
  41.    systemdict begin def        % can't use 'put', userdict is local
  42.  }
  43.  { systemdict begin
  44.  }
  45. ifelse
  46.  
  47. % Define true and false.
  48. /true 0 0 eq def
  49. /false 0 1 eq def
  50.  
  51. % Define dummy local/global operators if needed.
  52. systemdict /.setglobal known
  53.  { true .setglobal
  54.  }
  55.  { /.setglobal { pop } def
  56.    /.currentglobal { false } def
  57.    /.gcheck { pop false } def
  58.  }
  59. ifelse
  60.  
  61. % Define .languagelevel if needed.
  62. systemdict /.languagelevel known not { /.languagelevel 1 def } if
  63.  
  64. % Optionally choose a default paper size other than U.S. letter.
  65. % (a4) /PAPERSIZE where { pop pop } { /PAPERSIZE exch def } ifelse
  66.  
  67. % Turn on array packing for the rest of initialization.
  68. true setpacking
  69.  
  70. % Acquire the debugging flags.
  71. currentdict /DEBUG known   /DEBUG exch def
  72.   /VMDEBUG
  73.     DEBUG {{print mark
  74.             systemdict /level2dict known
  75.          { .currentglobal false .setglobal vmstatus
  76.            true .setglobal vmstatus 3 -1 roll pop
  77.            6 -2 roll pop .setglobal
  78.          }
  79.          { vmstatus 3 -1 roll pop
  80.          }
  81.         ifelse usertime 16#fffff and counttomark
  82.           { ( ) print (           ) cvs print }
  83.         repeat pop
  84.         ( ) print systemdict length (    ) cvs print
  85.         ( <) print count (    ) cvs print (>\n) print flush
  86.       }}
  87.       {{pop
  88.       }}
  89.      ifelse
  90.   def
  91.  
  92. currentdict /DISKFONTS known   /DISKFONTS exch def
  93. currentdict /ESTACKPRINT known   /ESTACKPRINT exch def
  94. currentdict /FAKEFONTS known   /FAKEFONTS exch def
  95. currentdict /NOBIND known   /NOBIND exch def
  96. /.bind /bind load def
  97. NOBIND { /bind { } def } if
  98. currentdict /NOCACHE known   /NOCACHE exch def
  99. currentdict /NOCIE known   /NOCIE exch def
  100. currentdict /NODISPLAY known   not /DISPLAYING exch def
  101. currentdict /NOGC known   /NOGC exch def
  102. currentdict /NOPAUSE known   /NOPAUSE exch def
  103. currentdict /NOPLATFONTS known   /NOPLATFONTS exch def
  104. currentdict /ORIENT1 known   /ORIENT1 exch def
  105. currentdict /OSTACKPRINT known   /OSTACKPRINT exch def
  106. currentdict /OUTPUTFILE known    % obsolete
  107.  { /OutputFile /OUTPUTFILE load def
  108.    currentdict /OUTPUTFILE undef
  109.  } if
  110. currentdict /QUIET known   /QUIET exch def
  111. currentdict /SAFER known   /SAFER exch def
  112. currentdict /WRITESYSTEMDICT known   /WRITESYSTEMDICT exch def
  113.  
  114. % Acquire environment variables.
  115. currentdict /DEVICE known not
  116.  { (GS_DEVICE) getenv { /DEVICE exch def } if } if
  117.  
  118. (START) VMDEBUG
  119.  
  120. % Open the standard files, so they will be open at the outermost save level.
  121. (%stdin) (r) file pop
  122. (%stdout) (w) file pop
  123. (%stderr) (w) file pop
  124.  
  125. % Define a procedure for skipping over an unneeded section of code.
  126. % This avoids allocating space for the skipped procedures.
  127. /.skipeof    % string ->
  128.  { { dup currentfile =string readline pop eq { exit } if } loop pop
  129.  } bind def
  130.  
  131. % Define =string, which is used by some PostScript programs even though
  132. % it isn't documented anywhere.
  133. % Put it in userdict so that each context can have its own copy.
  134. userdict /=string 128 string put
  135.  
  136. % Print the greeting.
  137.  
  138. /printgreeting
  139.  { mark
  140.    product (Ghostscript) search
  141.     { pop pop pop
  142.       (This software comes with NO WARRANTY: see the file PUBLIC for details.\n)
  143.     }
  144.     { pop
  145.     }
  146.    ifelse
  147.    (\n) copyright
  148.    (\)\n) revisiondate 10000 idiv (/)
  149.    revisiondate 100 mod (/)
  150.    revisiondate 100 idiv 100 mod ( \()
  151.    revision 10 mod
  152.    revision 10 idiv 10 mod (.)
  153.    revision 100 idiv ( )
  154.    product
  155.    counttomark
  156.     { (%stdout) (w) file exch .writecvs
  157.     } repeat pop
  158.  } bind def
  159.  
  160. QUIET not { printgreeting flush } if
  161.  
  162. % Define a special version of def for making operator procedures.
  163. /odef
  164.     {1 index exch .makeoperator def} bind def
  165.  
  166. %**************** BACKWARD COMPATIBILITY
  167. /getdeviceprops
  168.  { null .getdeviceparams
  169.  } bind odef
  170. /.putdeviceprops
  171.  { null true counttomark 1 add 3 roll .putdeviceparams
  172.    dup type /nametype eq
  173.     { counttomark 4 add 1 roll cleartomark pop pop pop
  174.       /.putdeviceprops load exch signalerror
  175.     }
  176.    if
  177.  } bind odef
  178. /.devicenamedict 1 dict dup /OutputDevice dup put def
  179. /.devicename
  180.  { //.devicenamedict .getdeviceparams exch pop exch pop
  181.  } bind odef
  182. /max { .max } bind def
  183. /min { .min } bind def
  184.  
  185. % Define predefined procedures substituting for operators,
  186. % in alphabetical order.
  187.  
  188. userdict /#copies 1 put
  189. /[    /mark load def
  190. /]     {counttomark array astore exch pop} odef
  191. /abs    {dup 0 lt {neg} if} odef
  192. % .beginpage is an operator in Level 2.
  193. /.beginpage { } odef
  194. /copypage
  195.     { 1 .endpage
  196.        { #copies false .outputpage
  197.          (>>copypage, press <return> to continue<<\n) .confirm
  198.        }
  199.       if .beginpage
  200.     } odef
  201. /setcolorscreen where { pop        % not in all Level 1 configurations
  202.    /currentcolorscreen
  203.     { .currenthalftone
  204.        { { 60 exch 0 exch 3 copy 6 copy }    % halftone
  205.          { 3 copy 6 copy }            % screen
  206.          { }                % colorscreen
  207.        }
  208.       exch get exec
  209.     } odef
  210. } if
  211. /currentscreen
  212.     { .currenthalftone
  213.        { { 60 exch 0 exch }            % halftone
  214.          { }                % screen
  215.          { 12 3 roll 9 { pop } repeat }    % colorscreen
  216.        }
  217.       exch get exec
  218.     } odef
  219. /.echo /echo load def
  220. userdict /.echo.mode true put
  221. /echo    {dup /.echo.mode exch store .echo} odef
  222. /eexec
  223.     { 55665 //filterdict /eexecDecode get exec
  224.       cvx systemdict begin stopped
  225.         % Only pop systemdict if it is still the top element,
  226.         % because this is apparently what Adobe interpreters do.
  227.       currentdict systemdict eq { end } if
  228.       { stop } if
  229.     } odef
  230. % .endpage is an operator in Level 2.
  231. /.endpage { 2 ne } odef
  232. % erasepage mustn't use gsave/grestore, because we call it before
  233. % the graphics state stack has been fully initialized.
  234. /erasepage
  235.     { /currentcolor where
  236.        { pop currentcolor currentcolorspace { setcolorspace setcolor } }
  237.        { /currentcmykcolor where
  238.           { pop currentcmykcolor { setcmykcolor } }
  239.           { currentrgbcolor { setrgbcolor } }
  240.          ifelse
  241.        }
  242.       ifelse 1 setgray .fillpage exec
  243.     } odef
  244. /executive
  245.     { { prompt
  246.          { (%statementedit) (r) file } stopped
  247.          { pop pop $error /errorname get /undefinedfilename eq
  248.         { exit } if        % EOF
  249.            handleerror null        % ioerror??
  250.          }
  251.         if
  252.         cvx execute
  253.       } loop
  254.     } odef
  255. /filter
  256.     { //filterdict 1 index .knownget
  257.        { exch pop exec }
  258.        { /filter load /undefined signalerror }
  259.       ifelse
  260.     } odef
  261. /handleerror
  262.     { errordict /handleerror get exec } bind def
  263. /identmatrix [1.0 0.0 0.0 1.0 0.0 0.0] readonly def
  264. /identmatrix
  265.     { //identmatrix exch copy } odef
  266. /initgraphics
  267.     { initmatrix newpath initclip
  268.       1 setlinewidth 0 setlinecap 0 setlinejoin
  269.       [] 0 setdash 0 setgray 10 setmiterlimit
  270.     } odef
  271. /languagelevel 1 def        % gs_lev2.ps may change this
  272. /matrix    { 6 array identmatrix } odef
  273. /prompt    { flush flushpage
  274.       (GS) print
  275.       count 0 ne { (<) print count =only } if
  276.       (>) print flush
  277.     } bind def
  278. /pstack    { 0 1 count 3 sub { index == } for } bind def
  279. /putdeviceprops
  280.     { .putdeviceprops { erasepage } if } odef
  281. /quit    { /quit load 0 .quit } odef
  282. /run    { dup type /filetype ne { (r) file } if cvx
  283.         % We must close the file when execution terminates,
  284.         % regardless of the state of the stack,
  285.         % and then propagate an error, if any.
  286.       cvx .runexec
  287.     } odef
  288. /setdevice
  289.     { .setdevice { erasepage } if } odef
  290. /showpage
  291.     { 0 .endpage
  292.        { #copies true .outputpage
  293.          (>>showpage, press <return> to continue<<\n) .confirm
  294.          erasepage
  295.        }
  296.       if initgraphics .beginpage
  297.     } odef
  298. % Code output by Adobe Illustrator relies on the fact that
  299. % `stack' is a procedure, not an operator!!!
  300. /stack    { 0 1 count 3 sub { index = } for } bind def
  301. /start    { executive } def
  302. /stop    { true .stop } odef
  303. /stopped { false .stopped } odef
  304. /store    { 1 index where { 3 1 roll put } { def } ifelse } odef
  305. % When running in Level 1 mode, this interpreter is supposed to be
  306. % compatible with PostScript "version" 54.0 (I think).
  307. /version (54.0) def
  308.  
  309. % Define some additional built-in procedures (beyond the ones defined by
  310. % the PostScript Language Reference Manual).
  311. % Warning: these are not guaranteed to stay the same from one release
  312. % to the next!
  313. /concatstrings
  314.     { exch dup length 2 index length add string    % str2 str1 new
  315.       dup dup 4 2 roll copy        % str2 new new new1
  316.       length 4 -1 roll putinterval
  317.     } bind def
  318. /copyarray
  319.     { dup length array copy } bind def
  320. /copystring
  321.     { dup length string copy } bind def
  322. /.dicttomark        % (the Level 2 >> operator)
  323.     { counttomark dup 1 and 0 ne
  324.        { pop /.dicttomark cvx /rangecheck signalerror
  325.        }
  326.        { 2 idiv dict dup
  327.          2 2 2 index maxlength 2 mul
  328.           {  {    % Stack: mark key1 value1 ... keyN valueN dict dict index
  329.            dup 2 add index exch 1 add index put dup
  330.          } for
  331.           }
  332.          stopped
  333.           {    % The error must have occurred in the 'put'.
  334.         pop pop pop pop stop
  335.           }
  336.           { counttomark 1 add 1 roll cleartomark
  337.           }
  338.          ifelse
  339.        }
  340.       ifelse
  341.     } bind def
  342. /finddevice
  343.     { systemdict /devicedict get exch get
  344.     } bind def
  345. /.growdictlength    % get size for growing a dictionary
  346.     { length 3 mul 2 idiv 1 add
  347.     } bind def
  348. /.growdict        % grow a dictionary
  349.     { dup .growdictlength .setmaxlength
  350.     } bind def
  351. /.growput        % put, grow the dictionary if needed
  352.     { 2 index length 3 index maxlength eq
  353.        { 3 copy pop known not { 2 index .growdict } if
  354.        } if
  355.       put
  356.     } bind def
  357. /.packtomark
  358.     { counttomark packedarray exch pop } bind def
  359. /runlibfile
  360.     { findlibfile
  361.        { exch pop run }
  362.        { /undefinedfilename signalerror }
  363.       ifelse
  364.     } bind def
  365. /selectdevice
  366.     { finddevice setdevice } bind def
  367. /signalerror        % <object> <errorname> signalerror -
  368.     { errordict exch get exec } bind def
  369.  
  370. % Define the =[only] procedures.  Also define =print,
  371. % which is used by some PostScript programs even though
  372. % it isn't documented anywhere.
  373. /write=only
  374.     { { .writecvs } null .stopped null ne
  375.        { pop (--nostringval--) writestring
  376.        }
  377.       if
  378.     } bind def
  379. /write=
  380.     { 1 index exch write=only (\n) writestring
  381.     } bind def
  382. /=only    { (%stdout) (w) file exch write=only } bind def
  383. /=    { =only (\n) print } bind def
  384. /=print    /=only load def
  385. % Temporarily define == as = for the sake of runlibfile0.
  386. /== /= load def
  387.  
  388. % Define procedures for getting and setting the current device resolution.
  389.  
  390. /gsgetdeviceprop    % <device> <propname> gsgetdeviceprop <value>
  391.  { 2 copy mark exch null .dicttomark .getdeviceparams
  392.    dup mark eq        % if true, not found
  393.     { pop dup /undefined signalerror }
  394.     { 5 1 roll pop pop pop pop }
  395.    ifelse
  396.  } bind def
  397. /gscurrentresolution    % - gscurrentresolution <[xres yres]>
  398.  { currentdevice /HWResolution gsgetdeviceprop
  399.  } bind def
  400. /gssetresolution    % <[xres yres]> gssetresolution -
  401.  { 2 array astore mark exch /HWResolution exch
  402.    currentdevice copydevice putdeviceprops setdevice
  403.  } bind def
  404.  
  405. % Define auxiliary procedures needed for the above.
  406. /shellarguments        % -> shell_arguments true (or) false
  407.     { /ARGUMENTS where
  408.        { /ARGUMENTS get dup type /arraytype eq
  409.           { aload pop /ARGUMENTS null store true }
  410.           { pop false }
  411.          ifelse }
  412.        { false } ifelse
  413.     } bind def
  414. /.confirm
  415.     { DISPLAYING NOPAUSE not and
  416.        {    % Print a message and wait for the user to type something.
  417.         % If the user just types a newline, flush it.
  418.          print flush
  419.          .echo.mode false echo
  420.          (%stdin) (r) file dup read
  421.           { dup (\n) 0 get eq { pop pop } { unread } ifelse }
  422.           { pop }
  423.          ifelse echo
  424.        }
  425.        { pop
  426.        }
  427.       ifelse
  428.     } bind def
  429.  
  430. % Define the procedure used by .runfile, .runstdin and .runstring
  431. % for executing user input.
  432. % This is called with a procedure or executable file on the operand stack.
  433. /execute
  434.     { stopped $error /newerror get and
  435.        { handleerror flush
  436.        } if
  437.     } odef
  438. % Define an execute analogue of runlibfile0.
  439. /execute0
  440.     { stopped $error /newerror get and
  441.        { handleerror flush /execute0 cvx 1 .quit
  442.        } if
  443.     } bind def
  444. % Define the procedure that the C code uses for running files
  445. % named on the command line.
  446. /.runfile { { runlibfile } execute } def
  447. % Define the procedure that the C code uses for running piped input.
  448. /.runstdin { (%stdin) (r) file cvx execute0 } bind def
  449. % Define the procedure that the C code uses for running commands
  450. % given on the command line with -c.
  451. /.runstring { cvx execute } def
  452.  
  453. % Define a special version of runlibfile that aborts on errors.
  454. /runlibfile0
  455.     { cvlit dup /.currentfilename exch def
  456.        { findlibfile not { stop } if }
  457.       stopped
  458.        { (Can't find \(or open\) initialization file ) print
  459.          .currentfilename == flush /runlibfile0 cvx 1 .quit
  460.        } if
  461.       exch pop cvx stopped
  462.        { (While reading ) print .currentfilename print (:\n) print flush
  463.          handleerror /runlibfile0 1 .quit
  464.        } if
  465.     } bind def
  466. % Temporarily substitute it for the real runlibfile.
  467. /.runlibfile /runlibfile load def
  468. /runlibfile /runlibfile0 load def
  469.  
  470. % Create the error handling machinery.
  471. % Define the standard error handlers.
  472. % The interpreter has created the ErrorNames array.
  473. /.unstoppederrorhandler    % <command> <errorname> .unstoppederrorhandler -
  474.  {    % This is the handler that gets used for recursive errors,
  475.     % or errors outside the scope of a 'stopped'.
  476.    (Unrecoverable error: ) print dup =only flush
  477.    ( in ) print 1 index = flush
  478.    count 2 gt
  479.     { (Operand stack:\n  ) print
  480.       2 1 count 3 sub { (  ) print index =only flush } for
  481.       (\n) print flush
  482.     } if
  483.    -1 0 1 //ErrorNames length 1 sub
  484.     { dup //ErrorNames exch get 3 index eq
  485.        { not exch pop exit } { pop } ifelse
  486.     }
  487.    for exch pop .quit
  488.  } bind def
  489. /.errorhandler        % <command> <errorname> .errorhandler -
  490.   {        % Detect an internal 'stopped'.
  491.     .instopped { null eq { pop pop stop } if } if
  492.     $error /.inerror get .instopped { pop } { pop true } ifelse
  493.      { .unstoppederrorhandler
  494.      } if    % detect error recursion
  495.     $error /globalmode .currentglobal false .setglobal put
  496.     $error /.inerror true put
  497.     $error /newerror true put
  498.     $error exch /errorname exch put
  499.     $error exch /command exch put
  500.     $error /recordstacks get $error /errorname get /VMerror ne and
  501.      {        % Attempt to store the stack contents atomically.
  502.        count array astore dup $error /ostack 4 -1 roll
  503.        countexecstack array execstack $error /estack 3 -1 roll
  504.        countdictstack array dictstack $error /dstack 3 -1 roll
  505.        put put put aload pop
  506.      }
  507.      { $error /dstack undef
  508.        $error /estack undef
  509.        $error /ostack undef
  510.      }
  511.     ifelse
  512.     $error /position currentfile status
  513.      { currentfile { fileposition } null .stopped null ne { pop null } if
  514.      }
  515.      { null
  516.      }
  517.     ifelse put
  518.         % During initialization, we don't reset the allocation
  519.         % mode on errors.
  520.     $error /globalmode get $error /.nosetlocal get and .setglobal
  521.     $error /.inerror false put
  522.     stop
  523.   } bind def
  524. % Define the standard handleerror.  We break out the printing procedure
  525. % (.printerror) so that it can be extended for binary output
  526. % if the Level 2 facilities are present.
  527.   /.printerror
  528.    { (Error: ) print
  529.      $error begin
  530.        errorname ==only flush
  531.        ( in ) print
  532.        /command load ==only flush
  533.        currentdict /errorinfo .knownget
  534.     { (\nAdditional information: ) print ==only flush
  535.     } if
  536.  
  537.        % Push the (anonymous) stack printing procedure.
  538.        %  <heading> <==flag> <override-name> <stackname> proc
  539.        {
  540.      currentdict exch .knownget    % stackname defined in $error?
  541.      {
  542.        4 1 roll            % stack: <stack> <head> <==flag> <over>
  543.        errordict exch .knownget    % overridename defined?
  544.        { 
  545.          exch pop exch pop exec    % call override with <stack>
  546.        }
  547.        { 
  548.          exch print exch        % print heading. stack <==flag> <stack>
  549.          1 index not { (\n) print } if
  550.          { 1 index { (\n    ) } { (   ) } ifelse print
  551.            dup type /dicttype eq
  552.            {
  553.          (--dict:) print
  554.          dup rcheck
  555.           { dup length =only (/) print maxlength =only }
  556.           { pop }
  557.          ifelse
  558.          (--) print
  559.            }
  560.            {
  561.          dup type /stringtype eq 2 index or
  562.          { ==only } { =only } ifelse
  563.            } ifelse
  564.          } forall
  565.          pop
  566.        }
  567.        ifelse            % overridden
  568.      }
  569.      { pop pop pop
  570.      }
  571.      ifelse                % stack known
  572.        }
  573.  
  574.        (\nOperand stack:) OSTACKPRINT /.printostack /ostack 4 index exec
  575.        (\nExecution stack:) ESTACKPRINT /.printestack /estack 4 index exec
  576.        (\nBacktrace:) true /.printbacktrace /backtrace 4 index exec
  577.        (\nDictionary stack:) false /.printdstack /dstack 4 index exec
  578.        (\n) print
  579.        pop    % printing procedure
  580.  
  581.        errorname /VMerror eq
  582.     { (VM status:) print mark vmstatus
  583.       counttomark { ( ) print counttomark -1 roll dup =only } repeat
  584.       cleartomark (\n) print
  585.     } if
  586.  
  587.        .languagelevel 2 ge
  588.     { (Current allocation mode is ) print
  589.       globalmode { (global\n) } { (local\n) } ifelse print
  590.     } if
  591.  
  592.        .oserrno dup 0 ne
  593.     { (Last OS error: ) print
  594.       errorname /VMerror ne
  595.        { dup .oserrorstring { = pop } { = } ifelse }
  596.        { = }
  597.       ifelse
  598.     }
  599.     { pop
  600.     }
  601.        ifelse
  602.  
  603.        position null ne
  604.     { (Current file position is ) print position = }
  605.        if
  606.  
  607.        .clearerror
  608.      end
  609.      flush
  610.    } bind def
  611. % Define a procedure for clearing the error indication.
  612. /.clearerror
  613.  { $error /newerror false put
  614.    $error /errorinfo undef
  615.    0 .setoserrno
  616.  } bind def
  617.  
  618. % Define $error.  This must be in local VM.
  619. .currentglobal false .setglobal
  620. /$error 40 dict def        % newerror, errorname, command, errorinfo,
  621.                 % ostack, estack, dstack, recordstacks,
  622.                 % binary, globalmode,
  623.                 % .inerror, .nosetlocal, position,
  624.         % plus extra space for badly designed error handers.
  625. $error begin
  626.   /newerror false def
  627.   /recordstacks true def
  628.   /binary false def
  629.   /globalmode .currentglobal def
  630.   /.inerror false def
  631.   /.nosetlocal true def
  632.   /position null def
  633. end
  634. % Define errordict similarly.  It has one entry per error name,
  635. %   plus handleerror.
  636. /errordict ErrorNames length 1 add dict def
  637. .setglobal        % contents of errordict are global
  638. errordict begin
  639.   ErrorNames
  640.    { mark 1 index systemdict /.errorhandler get /exec load .packtomark cvx def
  641.    } forall
  642. % The handlers for interrupt and timeout are special; there is no
  643. % 'current object', so they push their own name.
  644.    { /interrupt /timeout }
  645.    { mark 1 index dup systemdict /.errorhandler get /exec load .packtomark cvx def
  646.    } forall
  647. /handleerror
  648.  { systemdict /.printerror get exec
  649.  } bind def
  650. end
  651.  
  652. % Define the [write]==[only] procedures.
  653. /.dict 26 dict dup
  654. begin def
  655.   /.cvp {1 index exch .writecvs} bind def
  656.   /.nop {exch pop .p} bind def
  657.   /.p {1 index exch writestring} bind def
  658.   /.p1 {2 index exch writestring} bind def
  659.   /.p2 {3 index exch writestring} bind def
  660.   /.print
  661.     { dup type .dict exch .knownget
  662.        { dup type /stringtype eq { .nop } { exec } ifelse }
  663.        { (-) .p1 type .cvp (-) .p }
  664.       ifelse
  665.     } bind def
  666.   /.pstring
  667.     {  { dup dup 32 lt exch 127 ge or
  668.           { (\\) .p1 2 copy -6 bitshift 48 add write
  669.         2 copy -3 bitshift 7 and 48 add write
  670.         7 and 48 add
  671.           }
  672.           { dup dup -2 and 40 eq exch 92 eq or {(\\) .p1} if
  673.           }
  674.          ifelse 1 index exch write
  675.        }
  676.       forall
  677.     } bind def  
  678.   /booleantype /.cvp load def
  679.   /conditiontype (-condition-) def
  680.   /devicetype (-device-) def
  681.   /dicttype (-dict-) def
  682.   /filetype (-file-) def
  683.   /fonttype (-fontID-) def
  684.   /gstatetype (-gstate-) def
  685.   /integertype /.cvp load def
  686.   /locktype (-lock-) def
  687.   /marktype (-mark-) def
  688.   /nulltype (-null-) def
  689.   /realtype /.cvp load def
  690.   /savetype (-save-) def
  691.   /nametype
  692.     {dup xcheck not {(/) .p1} if
  693.      1 index exch .writecvs} bind def
  694.   /arraytype
  695.     {dup rcheck
  696.       {() exch dup xcheck
  697.         {({) .p2
  698.          {exch .p1
  699.           1 index exch .print pop ( )} forall
  700.          (})}
  701.         {([) .p2
  702.          {exch .p1
  703.           1 index exch .print pop ( )} forall
  704.          (])}
  705.        ifelse exch pop .p}
  706.       {(-array-) .nop}
  707.      ifelse} bind def
  708.   /operatortype
  709.       {(--) .p1 .cvp (--) .p} bind def
  710.   /packedarraytype
  711.     { dup rcheck
  712.        { arraytype }
  713.        { (-packedarray-) .nop }
  714.       ifelse
  715.     } bind def
  716.   /stringtype
  717.     { dup rcheck
  718.        { (\() .p1 dup length 200 le
  719.           { .pstring }
  720.           { 0 200 getinterval .pstring (...) .p }
  721.          ifelse (\)) .p
  722.        }
  723.        { (-string-) .nop
  724.        }
  725.       ifelse
  726.     } bind def
  727. {//.dict begin .print pop end}
  728.   bind cvx
  729. end
  730.  
  731. /write==only exch def
  732. /write==
  733.     {1 index exch write==only (\n) writestring} bind def
  734. /==only    { (%stdout) (w) file exch write==only } bind def
  735. /==    {==only (\n) print} bind def
  736.  
  737. (END PROCS) VMDEBUG
  738.  
  739. % Define the font directory.
  740. % Make it big to leave room for transformed fonts.
  741. /FontDirectory false .setglobal 100 dict true .setglobal def
  742.  
  743. % Define the encoding dictionary.
  744. /.encodingdict 10 dict def    % enough for Level 2 + PDF standard encodings
  745.  
  746. % Define findencoding.  (This is redefined in Level 2.)
  747. /.findencoding
  748.  { //.encodingdict exch get exec
  749.  } bind def
  750. /.defineencoding
  751.  { //.encodingdict 3 1 roll put
  752.  } bind def
  753.  
  754. % Load StandardEncoding.
  755. %% Replace 1 (gs_std_e.ps)
  756. (gs_std_e.ps) dup runlibfile VMDEBUG
  757.  
  758. % Load ISOLatin1Encoding.
  759. %% Replace 1 (gs_iso_e.ps)
  760. (gs_iso_e.ps) dup runlibfile VMDEBUG
  761.  
  762. % Define stubs for the Symbol and Dingbats encodings.
  763. % Note that the first element of the procedure must be the file name,
  764. % since gs_lev2.ps extracts it to set up the Encoding resource category.
  765.  
  766.   /SymbolEncoding { /SymbolEncoding .findencoding } bind def
  767. %% Replace 3 (gs_sym_e.ps)
  768.   .encodingdict /SymbolEncoding
  769.    { (gs_sym_e.ps) systemdict begin runlibfile SymbolEncoding end }
  770.   bind put
  771.  
  772.   /DingbatsEncoding { /DingbatsEncoding .findencoding } bind def
  773. %% Replace 3 (gs_dbt_e.ps)
  774.   .encodingdict /DingbatsEncoding
  775.    { (gs_dbt_e.ps) systemdict begin runlibfile DingbatsEncoding end }
  776.   bind put
  777.  
  778. (END FONTDIR/ENCS) VMDEBUG
  779.  
  780. % Construct a dictionary of all available devices.
  781. mark
  782.     % Loop until the .getdevice gets a rangecheck.
  783.   errordict /rangecheck 2 copy get
  784.   errordict /rangecheck { pop stop } put    % pop the command
  785.   0 { {dup .getdevice exch 1 add} loop} stopped pop
  786.   dict /devicedict exch def
  787.   devicedict begin        % 2nd copy of count is on stack
  788.    { dup .devicename dup 3 -1 roll def
  789.      counttomark 1 roll
  790.    } repeat
  791.   end put
  792. counttomark packedarray /devicenames exch def pop
  793. .clearerror
  794.  
  795. (END DEVS) VMDEBUG
  796.  
  797. % Define statusdict, for the benefit of programs
  798. % that think they are running on a LaserWriter or similar printer.
  799. %% Replace 1 (gs_statd.ps)
  800. (gs_statd.ps) runlibfile
  801.  
  802. (END STATD) VMDEBUG
  803.  
  804. % Load the standard font environment.
  805. %% Replace 1 (gs_fonts.ps)
  806. (gs_fonts.ps) runlibfile
  807.  
  808. (END GS_FONTS) VMDEBUG
  809.  
  810. % Create a null font.  This is the initial font.
  811. 8 dict dup begin
  812.   /FontMatrix [ 1 0 0 1 0 0 ] readonly def
  813.   /FontType 3 def
  814.   /FontName () def
  815.   /Encoding StandardEncoding def
  816.   /FontBBox { 0 0 0 0 } readonly def % executable is bogus, but customary ...
  817.   /BuildChar { pop pop 0 0 setcharwidth } bind def
  818.   /PaintType 0 def        % shouldn't be needed!
  819. end
  820. /NullFont exch definefont setfont
  821.  
  822. % Define NullFont as the font, but remove it from FontDirectory.
  823. /NullFont currentfont def
  824. FontDirectory /NullFont undef
  825.  
  826. (END FONTS) VMDEBUG
  827.  
  828. % Load the initialization files for optional features.
  829. %% Replace 4 INITFILES
  830. systemdict /INITFILES known
  831.  { INITFILES { dup runlibfile VMDEBUG } forall
  832.  }
  833. if
  834.  
  835. % If Level 2 functionality is implemented, enable it now.
  836. /.setlanguagelevel where
  837.  { pop 2 .setlanguagelevel
  838.  } if
  839.  
  840. % If the resource machinery was loaded, convert encodings to resources.
  841. /defineresource where
  842.  { pop .encodingdict
  843.     { dup length 256 eq
  844.        { /Encoding defineresource pop }
  845.        { pop pop }
  846.       ifelse
  847.     } forall
  848.  } if
  849.  
  850. (END INITFILES) VMDEBUG
  851.  
  852. % Restore the real definition of runlibfile.
  853. /runlibfile /.runlibfile load def
  854. currentdict /.runlibfile undef
  855.  
  856. % Bind all the operators defined as procedures.
  857. /.bindoperators        % binds operators in currentdict
  858.  { % Temporarily disable the typecheck error.
  859.    errordict /typecheck 2 copy get
  860.    errordict /typecheck { pop } put    % pop the command
  861.    currentdict
  862.     { dup type /operatortype eq
  863.        { % This might be a real operator, so bind might cause a typecheck,
  864.      % but we've made the error a no-op temporarily.
  865.      .bind        % do a real bind even if NOBIND is set
  866.        }
  867.       if pop pop
  868.     } forall
  869.    put
  870.  } def
  871. NOBIND not { .bindoperators } if
  872.  
  873. % Establish a default environment.
  874.  
  875. DISPLAYING not
  876.  { nulldevice (%END DISPLAYING) .skipeof
  877.  } if
  878. /defaultdevice 0 .getdevice systemdict /DEVICE known
  879.  { pop devicedict DEVICE known not
  880.     { (Unknown device: ) print DEVICE =
  881.       flush /defaultdevice cvx 1 .quit
  882.     }
  883.    if DEVICE finddevice
  884.  }
  885. if def
  886. defaultdevice
  887. systemdict /DEVICEWIDTH known
  888. systemdict /DEVICEHEIGHT known or
  889. systemdict /DEVICEWIDTHPOINTS known or
  890. systemdict /DEVICEHEIGHTPOINTS known or
  891. systemdict /DEVICEXRESOLUTION known or
  892. systemdict /DEVICEYRESOLUTION known or
  893. systemdict /PAPERSIZE known or
  894. not { (%END DEVICE) .skipeof } if
  895. systemdict /PAPERSIZE known
  896.  {    % Convert the paper size to device dimensions.
  897.    true statusdict /.pagetypenames get
  898.     { PAPERSIZE eq
  899.        { PAPERSIZE load
  900.          dup 0 get /DEVICEWIDTHPOINTS exch def
  901.          1 get /DEVICEHEIGHTPOINTS exch def
  902.          pop false exit
  903.        }
  904.       if
  905.     }
  906.    forall
  907.     { (Unknown paper size: ) print PAPERSIZE ==only (.\n) print
  908.     }
  909.    if
  910.  }
  911. if
  912. % Adjust the device parameters per the command line.
  913. % It is possible to specify resolution, pixel size, and page size;
  914. % since any two of these determine the third, conflicts are possible.
  915. % We simply pass them to .setdeviceparams and let it sort things out.
  916.    mark /HWResolution null /HWSize null /PageSize null .dicttomark
  917.    .getdeviceparams .dicttomark begin
  918.    mark
  919.     % Check for resolution.
  920.    /DEVICEXRESOLUTION where dup
  921.     { exch pop HWResolution 0 DEVICEXRESOLUTION put }
  922.    if
  923.    /DEVICEYRESOLUTION where dup
  924.     { exch pop HWResolution 1 DEVICEYRESOLUTION put }
  925.    if
  926.    or { /HWResolution HWResolution } if
  927.     % Check for device sizes specified in pixels.
  928.    /DEVICEWIDTH where dup
  929.     { exch pop HWSize 0 DEVICEWIDTH put }
  930.    if
  931.    /DEVICEHEIGHT where dup
  932.     { exch pop HWSize 1 DEVICEHEIGHT put }
  933.    if
  934.    or { /HWSize HWSize } if
  935.     % Check for device sizes specified in points.
  936.    /DEVICEWIDTHPOINTS where dup
  937.     { exch pop PageSize 0 DEVICEWIDTHPOINTS put }
  938.    if
  939.    /DEVICEHEIGHTPOINTS where dup
  940.     { exch pop PageSize 1 DEVICEHEIGHTPOINTS put }
  941.    if
  942.    or { /PageSize PageSize } if
  943.     % Check whether any parameters were set.
  944.    dup mark eq { pop } { defaultdevice putdeviceprops } ifelse
  945.    end
  946. %END DEVICE
  947. % Set any device properties defined on the command line.
  948. dup getdeviceprops
  949. counttomark 2 idiv
  950.  { systemdict 2 index known
  951.     { pop dup load counttomark 2 roll }
  952.     { pop pop }
  953.    ifelse
  954.  } repeat
  955. systemdict /BufferSpace known
  956. systemdict /MaxBitmap known not and
  957.  { /MaxBitmap BufferSpace
  958.  } if
  959. counttomark dup 0 ne
  960.  { 2 add -1 roll putdeviceprops }
  961.  { pop pop }
  962. ifelse
  963. setdevice        % does an erasepage
  964. %END DISPLAYING
  965.  
  966. (END DEVICE) VMDEBUG
  967.  
  968. % Establish a default upper limit in the character cache,
  969. % namely, enough room for a 1/4" x 1/4" character at the resolution
  970. % of the default device, or for 5 x the "average" character size,
  971. % whichever is larger.
  972. mark
  973.     % Compute limit based on character size.
  974.   18 dup dtransform        % 1/4" x 1/4"
  975.   exch abs cvi 31 add 32 idiv 4 mul    % X raster
  976.   exch abs cvi mul        % Y
  977.     % Compute limit based on allocated space.
  978.   cachestatus 5 2 roll pop pop pop pop div 5 mul cvi exch pop
  979.   .max dup 10 idiv exch
  980. setcacheparams
  981. % Conditionally disable the character cache.
  982. NOCACHE { 0 setcachelimit } if
  983.  
  984. (END CONFIG) VMDEBUG
  985.  
  986. % Establish an appropriate halftone screen.
  987.  
  988. 72 72 dtransform abs exch abs .min    % min(|dpi x|,|dpi y|)
  989. dup 150 lt systemdict /DITHERPPI known not and
  990.  {        % Low-res device, use ordered dither spot function
  991.     % The following 'ordered dither' spot function was contributed by
  992.     % Gregg Townsend.  Thanks, Gregg!
  993.   16.001 div 0            % not 16: avoids rounding problems
  994.    { 1 add 7.9999 mul cvi exch 1 add 7.9999 mul cvi 16 mul add <
  995.     0E 8E 2E AE 06 86 26 A6 0C 8C 2C AC 04 84 24 A4
  996.     CE 4E EE 6E C6 46 E6 66 CC 4C EC 6C C4 44 E4 64
  997.     3E BE 1E 9E 36 B6 16 96 3C BC 1C 9C 34 B4 14 94
  998.     FE 7E DE 5E F6 76 D6 56 FC 7C DC 5C F4 74 D4 54
  999.     01 81 21 A1 09 89 29 A9 03 83 23 A3 0B 8B 2B AB
  1000.     C1 41 E1 61 C9 49 E9 69 C3 43 E3 63 CB 4B EB 6B
  1001.     31 B1 11 91 39 B9 19 99 33 B3 13 93 3B BB 1B 9B
  1002.     F1 71 D1 51 F9 79 D9 59 F3 73 D3 53 FB 7B DB 5B
  1003.     0D 8D 2D AD 05 85 25 A5 0F 8F 2F AF 07 87 27 A7
  1004.     CD 4D ED 6D C5 45 E5 65 CF 4F EF 6F C7 47 E7 67
  1005.     3D BD 1D 9D 35 B5 15 95 3F BF 1F 9F 37 B7 17 97
  1006.     FD 7D DD 5D F5 75 D5 55 FF 7F DF 5F F7 77 D7 57
  1007.     02 82 22 A2 0A 8A 2A AA 00 80 20 A0 08 88 28 A8
  1008.     C2 42 E2 62 CA 4A EA 6A C0 40 E0 60 C8 48 E8 68
  1009.     32 B2 12 92 3A BA 1A 9A 30 B0 10 90 38 B8 18 98
  1010.     F2 72 D2 52 FA 7A DA 5A F0 70 D0 50 F8 78 D8 58
  1011.      > exch get 256 div
  1012.    }
  1013.   bind
  1014.         % Use correct, per-plane screens for all CMYK devices.
  1015.   systemdict /setcolorscreen known processcolors 4 eq and
  1016.    { 3 copy 6 copy setcolorscreen }
  1017.    { setscreen }
  1018.   ifelse
  1019.   0 array cvx    % transfer -- Genoa CET won't accept a packed array!
  1020.   true        % strokeadjust
  1021.  }
  1022.  {        % Hi-res device, use 45 degree dot spot function.
  1023.     % According to information published by Hewlett-Packard,
  1024.     % they use a 60 line screen on 300 DPI printers and
  1025.     % an 85 line screen on 600 DPI printers.
  1026.     % 46 was suggested as a good frequency value for printers
  1027.     % between 200 and 400 DPI, so we use it for lower resolutions.
  1028.    systemdict /DITHERPPI known
  1029.     { DITHERPPI }
  1030.     { dup cvi 100 idiv 6 .min {null 46 46 60 60 60 85} exch get }
  1031.    ifelse
  1032.    1 index 4.01 div .min    % at least a 4x4 cell
  1033.    45
  1034.     % The following screen algorithm is used by permission of the author.
  1035.     { 1 add 180 mul cos 1 0.08 add mul exch 2 add 180 mul cos 
  1036.       1 0.08 sub mul add 2 div % (C) 1989 Berthold K.P. Horn
  1037.     }
  1038.    bind
  1039.     % Ghostscript currently doesn't use correct, per-plane halftones
  1040.     % unless setcolorscreen has been executed.  Since these are
  1041.     % computationally much more expensive than binary halftones,
  1042.     % we check to make sure they are really warranted, i.e., we have
  1043.     % a high-resolution CMYK device (i.e., not a display) with
  1044.     % fewer than 5 bits per plane (i.e., not a true-color device).
  1045.    4 -1 roll 150 ge
  1046.     { /setcolorscreen where
  1047.        { pop defaultdevice getdeviceprops .dicttomark
  1048.          dup dup dup /RedValues known exch /GreenValues known and
  1049.        exch /BlueValues known and
  1050.       { dup dup /RedValues get 32 lt
  1051.           exch /GreenValues get 32 lt and
  1052.           exch /BlueValues get 32 lt and
  1053.          { 3 copy 6 copy
  1054.     % For really high-quality screening on printers, we need to
  1055.     % give each plane its own screen angle.  Unfortunately,
  1056.     % this currently has very large space and time costs.
  1057.     %**************** Uncomment the next line for high-quality screening.
  1058.     %    { 45 90 15 75 } { 3 1 roll exch pop 12 3 roll } forall
  1059.            setcolorscreen
  1060.          }
  1061.          { setscreen
  1062.          }
  1063.         ifelse
  1064.       }
  1065.       { pop setscreen
  1066.       }
  1067.      ifelse
  1068.        }
  1069.        { setscreen
  1070.        }
  1071.       ifelse
  1072.     }
  1073.     { setscreen
  1074.     }
  1075.    ifelse
  1076.     % Set the transfer function to lighten up the grays.
  1077.     % We correct at the high end so that very light grays
  1078.     % don't disappear completely if they darken <1 screen pixel.
  1079.     % Parameter values closer to 1 are better for devices with
  1080.     % less dot spreading; lower values are better with more spreading.
  1081.     % The value 0.8 is a compromise that will probably please no one!
  1082.     { 0.8 exp dup dup 0.9375 gt exch 0.999 lt and    % > 15/16
  1083.        { .currentscreenlevels 1 sub    % tweak to avoid boundary
  1084.      1 exch div 1 exch sub .min
  1085.        }
  1086.       if
  1087.     }        % transfer
  1088.    false    % strokeadjust
  1089.     % Increase fill adjustment so that we effectively use Adobe's
  1090.     % any-part-of-pixel rule.
  1091.    0.5 .setfilladjust
  1092.  }
  1093. ifelse
  1094.   /setstrokeadjust where { pop setstrokeadjust } { pop } ifelse
  1095.   settransfer
  1096. initgraphics
  1097. % The interpreter relies on there being at least 2 entries
  1098. % on the graphics stack.  Establish the second one now.
  1099. gsave
  1100.  
  1101. % Define some control sequences as no-ops.
  1102. % This is a hack to get around problems
  1103. % in some common PostScript-generating applications.
  1104. % Note that <04> and <1a> are self-delimiting characters, like [.
  1105. <04> cvn { } def        % Apple job separator
  1106. %<0404> cvn { } def        % two of the same
  1107. <1b> cvn { } def        % MS Windows LaserJet 4 prologue
  1108. %<041b> cvn { } def        % MS Windows LaserJet 4 epilogue
  1109. <1a> cvn { } def        % MS-DOS EOF
  1110. (\001M) cvn { } def        % TBCP initiator
  1111. /@PJL                % H-P job control
  1112.  { currentfile //=string readline { pop } if
  1113.  } bind def
  1114.  
  1115. % If we want a "safer" system, disable some obvious ways to cause havoc.
  1116. SAFER not { (%END SAFER) .skipeof } if
  1117. /file
  1118.  { dup (r) eq 2 index (%pipe*) .stringmatch not and
  1119.     { file }
  1120.     { /invalidfileaccess signalerror }
  1121.    ifelse
  1122.  } bind odef
  1123. /renamefile { /invalidfileaccess signalerror } odef
  1124. /deletefile { /invalidfileaccess signalerror } odef
  1125. /putdeviceprops
  1126.  { counttomark
  1127.    dup 2 mod 0 eq { pop /rangecheck signalerror } if
  1128.    3 2 3 2 roll
  1129.     { dup index /OutputFile eq  
  1130.        { -2 roll 
  1131.          dup () ne { /putdeviceprops load /invalidfileaccess signalerror } if
  1132.          3 -1 roll
  1133.        }
  1134.        { pop
  1135.        }
  1136.       ifelse
  1137.     } for
  1138.    putdeviceprops
  1139.  } bind odef
  1140.  
  1141. %END SAFER
  1142.  
  1143. % Turn off array packing, since some PostScript code assumes that
  1144. % procedures are writable.
  1145. false setpacking
  1146.  
  1147. % Close up systemdict.
  1148. currentdict /.forceput undef        % remove temptation
  1149. currentdict /filterdict undef        % bound in where needed
  1150. end
  1151. WRITESYSTEMDICT not { systemdict readonly pop } if
  1152.  
  1153. (END INIT) VMDEBUG
  1154.  
  1155. % Establish local VM as the default.
  1156. false /setglobal where { pop setglobal } { .setglobal } ifelse
  1157. $error /.nosetlocal false put
  1158.  
  1159. % Clean up VM, and enable GC.
  1160. /vmreclaim where
  1161.  { pop NOGC not { 2 vmreclaim 0 vmreclaim } if
  1162.  } if
  1163.  
  1164. (END GC) VMDEBUG
  1165.  
  1166. % The interpreter will run the initial procedure (start).
  1167.