gs_cmdl.ps 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. % Copyright (C) 1994, 1996, 1999 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This software is provided AS-IS with no warranty, either express or
  4. % implied.
  5. %
  6. % This software is distributed under license and may not be copied,
  7. % modified or distributed except as expressly authorized under the terms
  8. % of the license contained in the file LICENSE in this distribution.
  9. %
  10. % For more information about licensing, please refer to
  11. % http://www.ghostscript.com/licensing/. For information on
  12. % commercial licensing, go to http://www.artifex.com/licensing/ or
  13. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  14. % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  15. % $Id: gs_cmdl.ps,v 1.4 2002/02/21 21:49:28 giles Exp $
  16. % Parse and execute the command line.
  17. % C code handles the following switches: -h/-? -I -M -v
  18. /cmddict 50 dict def
  19. cmddict begin
  20. % ---------------- Utility procedures ---------------- %
  21. % Get the next argument from the parsed argument list.
  22. /nextarg % - nextarg <arg> true
  23. % - nextarg false
  24. { argv length 0 eq
  25. { false }
  26. { argv dup 0 get exch dup length 1 sub 1 exch getinterval /argv exch def }
  27. ifelse
  28. } bind def
  29. % Run a file, under job control if implemented.
  30. /runjob % <file> runjob -
  31. { end % cmddict
  32. /startjob where { pop false () startjob pop }
  33. run
  34. //cmddict begin
  35. } bind def
  36. /runfilejob % <filename> runfilejob -
  37. { findlibfile { exch pop } { (r) file } runjob
  38. } bind def
  39. % Expand arguments. Free variables: expand@.
  40. /expandarg % <string> expandarg <args...>
  41. { dup () eq
  42. { pop
  43. }
  44. { dup dup (--) eq exch (-+) eq or
  45. { pop /expand@ false def
  46. }
  47. { expand@ { (@) anchorsearch } { false } ifelse
  48. { pop findlibfile
  49. { exch pop }
  50. { (r) file } % let the error happen
  51. expandargfile
  52. }
  53. if
  54. }
  55. ifelse
  56. }
  57. } bind def
  58. /expandargfile % <file> expandargfile <args...>
  59. { [ exch cvlit
  60. { token not { exit } if
  61. dup type /stringtype ne { =string cvs dup length string copy } if
  62. expandarg
  63. }
  64. /exec cvx
  65. ] cvx loop
  66. } bind def
  67. % ---------------- Recognized switches ---------------- %
  68. % Switches with arguments are defined as <x>;
  69. % switches without arguments are defined as -<x>.
  70. % Switches without arguments
  71. /--
  72. { nextarg not
  73. { (-- and -+ require a file name.) = flush }
  74. { //systemdict /ARGUMENTS argv put /argv [] def runjob }
  75. ifelse
  76. } bind def
  77. /-+ /-- load def
  78. /-@ /-- load def
  79. /-A { (@) Z } bind def
  80. /-c
  81. { { argv length 0 eq { exit } if
  82. argv 0 get (-) anchorsearch { pop pop exit } if
  83. pop nextarg token
  84. { exch pop % Probably should check for empty.
  85. end exec //cmddict begin
  86. }
  87. if
  88. }
  89. loop
  90. } bind def
  91. /-e { (#) Z } bind def
  92. /-E /-e load def
  93. /-f { } def
  94. /-q { //systemdict /QUIET true put } bind def
  95. % Switches with arguments
  96. /d
  97. { (=) search not { (#) search not { () exch dup } if } if
  98. exch pop cvn dup where
  99. { pop (Redefining ) print print ( is not allowed.) = flush pop }
  100. { exch token
  101. { exch pop } % Probably should check for empty.
  102. { true }
  103. ifelse
  104. //systemdict 3 1 roll put
  105. }
  106. ifelse
  107. } bind def
  108. /D /d load def
  109. /f { dup length 0 ne { runfilejob } if } bind def
  110. /g
  111. { (x) search { cvi pop exch cvi } { cvi dup } ifelse
  112. //systemdict begin /DEVICEHEIGHT exch def /DEVICEWIDTH exch def end
  113. } bind def
  114. /r
  115. { (x) search { cvr pop exch cvr } { cvr dup } ifelse
  116. //systemdict begin /DEVICEYRESOLUTION exch def /DEVICEXRESOLUTION exch def end
  117. } bind def
  118. /s
  119. { (=) search not { (#) search not { () exch dup } if } if
  120. exch pop cvn dup where { pop dup load } { () } ifelse
  121. type /stringtype ne
  122. { (Redefining ) print print ( is not allowed.) = flush pop }
  123. { exch //systemdict 3 1 roll put }
  124. ifelse
  125. } bind def
  126. /S /s load def
  127. /Z { true .setdebug } bind def
  128. % ---------------- Main program ---------------- %
  129. % We process the command line in two passes. In the first pass,
  130. % we read and expand any @-files as necessary. The second pass
  131. % does the real work.
  132. /cmdstart
  133. { //cmddict begin
  134. /expand@ true def
  135. [
  136. % Process the GS_OPTIONS environment variable.
  137. (GS_OPTIONS) getenv { 0 () /SubFileDecode filter expandargfile } if
  138. % Process the actual command line.
  139. .getargv { expandarg } forall
  140. ] readonly /argv exch def
  141. % Now interpret the commands.
  142. { nextarg not { exit } if
  143. dup 0 get (-) 0 get eq
  144. { dup length 1 eq
  145. { pop (%stdin) (r) file runjob
  146. }
  147. { dup length 2 gt
  148. { dup dup length 2 sub 2 exch getinterval exch 1 1 getinterval }
  149. if currentdict .knownget
  150. { exec
  151. }
  152. { (Ignoring unknown switch ) print
  153. dup length 1 eq { (-) print print } if print
  154. () = flush
  155. }
  156. ifelse
  157. }
  158. ifelse
  159. }
  160. { runfilejob
  161. }
  162. ifelse
  163. }
  164. loop end
  165. } bind def
  166. end % cmddict