home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / GhostScript / !GhostScr / 6_01 / lib / gs_cmdl.ps < prev    next >
Text File  |  2000-03-09  |  5KB  |  183 lines

  1. %    Copyright (C) 1994, 1996, 1999 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. % $Id: gs_cmdl.ps,v 1.1 2000/03/09 08:40:39 lpd Exp $
  16. % Parse and execute the command line.
  17. % C code handles the following switches: -h/-? -I -M -v
  18.  
  19. /cmddict 50 dict def
  20. cmddict begin
  21.  
  22. % ---------------- Utility procedures ---------------- %
  23.  
  24. % Get the next argument from the parsed argument list.
  25. /nextarg        % - nextarg <arg> true
  26.             % - nextarg false
  27.  { argv length 0 eq
  28.     { false }
  29.     { argv dup 0 get exch dup length 1 sub 1 exch getinterval /argv exch def }
  30.    ifelse
  31.  } bind def
  32.  
  33. % Run a file, under job control if implemented.
  34. /runjob            % <file> runjob -
  35.  { end        % cmddict
  36.    /startjob where { pop false () startjob pop }
  37.    run
  38.    //cmddict begin
  39.  } bind def
  40. /runfilejob        % <filename> runfilejob -
  41.  { findlibfile { exch pop } { (r) file } runjob
  42.  } bind def
  43.  
  44. % Expand arguments.  Free variables: expand@.
  45. /expandarg        % <string> expandarg <args...>
  46.  { dup () eq
  47.     { pop
  48.     }
  49.     { dup dup (--) eq exch (-+) eq or
  50.        { pop /expand@ false def
  51.        }
  52.        { expand@ { (@) anchorsearch } { false } ifelse
  53.       { pop findlibfile
  54.          { exch pop }
  55.          { (r) file }        % let the error happen
  56.         expandargfile
  57.       }
  58.      if
  59.        }
  60.       ifelse
  61.     }
  62.  } bind def
  63. /expandargfile        % <file> expandargfile <args...>
  64.  { [ exch cvlit
  65.     { token not { exit } if
  66.       dup type /stringtype ne { =string cvs dup length string copy } if
  67.       expandarg
  68.     }
  69.    /exec cvx
  70.    ] cvx loop
  71.  } bind def
  72.  
  73. % ---------------- Recognized switches ---------------- %
  74.  
  75. % Switches with arguments are defined as <x>;
  76. % switches without arguments are defined as -<x>.
  77.  
  78. % Switches without arguments
  79. /--
  80.  { nextarg not
  81.     { (-- and -+ require a file name.) = flush }
  82.     { //systemdict /ARGUMENTS argv put /argv [] def runjob }
  83.    ifelse
  84.  } bind def
  85. /-+ /-- load def
  86. /-@ /-- load def
  87. /-A { (@) Z } bind def
  88. /-c
  89.  {  { argv length 0 eq { exit } if
  90.       argv 0 get (-) anchorsearch { pop pop exit } if
  91.       pop nextarg token
  92.        { exch pop            % Probably should check for empty.
  93.      end exec //cmddict begin
  94.        }
  95.       if
  96.     }
  97.    loop
  98.  } bind def
  99. /-e { (#) Z } bind def
  100. /-E /-e load def
  101. /-f { } def
  102. /-q { //systemdict /QUIET true put } bind def
  103.  
  104. % Switches with arguments
  105. /d
  106.  { (=) search not { (#) search not { () exch dup } if } if
  107.    exch pop cvn dup where
  108.     { pop (Redefining ) print print ( is not allowed.) = flush pop }
  109.     { exch token
  110.        { exch pop }        % Probably should check for empty.
  111.        { true }
  112.       ifelse
  113.       //systemdict 3 1 roll put
  114.     }
  115.    ifelse
  116.  } bind def
  117. /D /d load def
  118. /f { dup length 0 ne { runfilejob } if } bind def
  119. /g
  120.  { (x) search { cvi pop exch cvi } { cvi dup } ifelse
  121.    //systemdict begin /DEVICEHEIGHT exch def /DEVICEWIDTH exch def end
  122.  } bind def
  123. /r
  124.  { (x) search { cvr pop exch cvr } { cvr dup } ifelse
  125.    //systemdict begin /DEVICEYRESOLUTION exch def /DEVICEXRESOLUTION exch def end
  126.  } bind def
  127. /s
  128.  { (=) search not { (#) search not { () exch dup } if } if
  129.    exch pop cvn dup where { pop dup load } { () } ifelse
  130.    type /stringtype ne
  131.     { (Redefining ) print print ( is not allowed.) = flush pop }
  132.     { exch //systemdict 3 1 roll put }
  133.    ifelse
  134.  } bind def
  135. /S /s load def
  136. /Z { true .setdebug } bind def
  137.  
  138. % ---------------- Main program ---------------- %
  139.  
  140. % We process the command line in two passes.  In the first pass,
  141. % we read and expand any @-files as necessary.  The second pass
  142. % does the real work.
  143.  
  144. /cmdstart
  145.  { //cmddict begin
  146.    /expand@ true def
  147.    [
  148.         % Process the GS_OPTIONS environment variable.
  149.    (GS_OPTIONS) getenv { 0 () /SubFileDecode filter expandargfile } if
  150.         % Process the actual command line.
  151.    .getargv { expandarg } forall
  152.    ] readonly /argv exch def
  153.         % Now interpret the commands.
  154.     { nextarg not { exit } if
  155.       dup 0 get (-) 0 get eq
  156.        { dup length 1 eq
  157.       { pop (%stdin) (r) file runjob
  158.       }
  159.       { dup length 2 gt
  160.          { dup dup length 2 sub 2 exch getinterval exch 1 1 getinterval }
  161.         if currentdict .knownget
  162.          { exec
  163.          }
  164.          { (Ignoring unknown switch ) print
  165.            dup length 1 eq { (-) print print } if print
  166.            () = flush
  167.          }
  168.         ifelse
  169.       }
  170.      ifelse
  171.        }
  172.        { runfilejob
  173.        }
  174.       ifelse
  175.     }
  176.    loop end
  177.  } bind def
  178.  
  179. end        % cmddict
  180.