gs_cmdl.ps 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. % Copyright (C) 1994, 1996, 1999 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This file is part of AFPL Ghostscript.
  4. %
  5. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  6. % distributor accepts any responsibility for the consequences of using it, or
  7. % for whether it serves any particular purpose or works at all, unless he or
  8. % she says so in writing. Refer to the Aladdin Free Public License (the
  9. % "License") for full details.
  10. %
  11. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  12. % in a plain ASCII text file named PUBLIC. The License grants you the right
  13. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14. % conditions described in the License. Among other things, the License
  15. % requires that the copyright notice and this notice be preserved on all
  16. % copies.
  17. % $Id: gs_cmdl.ps,v 1.2 2000/09/19 18:29:11 lpd Exp $
  18. % Parse and execute the command line.
  19. % C code handles the following switches: -h/-? -I -M -v
  20. /cmddict 50 dict def
  21. cmddict begin
  22. % ---------------- Utility procedures ---------------- %
  23. % Get the next argument from the parsed argument list.
  24. /nextarg % - nextarg <arg> true
  25. % - nextarg false
  26. { argv length 0 eq
  27. { false }
  28. { argv dup 0 get exch dup length 1 sub 1 exch getinterval /argv exch def }
  29. ifelse
  30. } bind def
  31. % Run a file, under job control if implemented.
  32. /runjob % <file> runjob -
  33. { end % cmddict
  34. /startjob where { pop false () startjob pop }
  35. run
  36. //cmddict begin
  37. } bind def
  38. /runfilejob % <filename> runfilejob -
  39. { findlibfile { exch pop } { (r) file } runjob
  40. } bind def
  41. % Expand arguments. Free variables: expand@.
  42. /expandarg % <string> expandarg <args...>
  43. { dup () eq
  44. { pop
  45. }
  46. { dup dup (--) eq exch (-+) eq or
  47. { pop /expand@ false def
  48. }
  49. { expand@ { (@) anchorsearch } { false } ifelse
  50. { pop findlibfile
  51. { exch pop }
  52. { (r) file } % let the error happen
  53. expandargfile
  54. }
  55. if
  56. }
  57. ifelse
  58. }
  59. } bind def
  60. /expandargfile % <file> expandargfile <args...>
  61. { [ exch cvlit
  62. { token not { exit } if
  63. dup type /stringtype ne { =string cvs dup length string copy } if
  64. expandarg
  65. }
  66. /exec cvx
  67. ] cvx loop
  68. } bind def
  69. % ---------------- Recognized switches ---------------- %
  70. % Switches with arguments are defined as <x>;
  71. % switches without arguments are defined as -<x>.
  72. % Switches without arguments
  73. /--
  74. { nextarg not
  75. { (-- and -+ require a file name.) = flush }
  76. { //systemdict /ARGUMENTS argv put /argv [] def runjob }
  77. ifelse
  78. } bind def
  79. /-+ /-- load def
  80. /-@ /-- load def
  81. /-A { (@) Z } bind def
  82. /-c
  83. { { argv length 0 eq { exit } if
  84. argv 0 get (-) anchorsearch { pop pop exit } if
  85. pop nextarg token
  86. { exch pop % Probably should check for empty.
  87. end exec //cmddict begin
  88. }
  89. if
  90. }
  91. loop
  92. } bind def
  93. /-e { (#) Z } bind def
  94. /-E /-e load def
  95. /-f { } def
  96. /-q { //systemdict /QUIET true put } bind def
  97. % Switches with arguments
  98. /d
  99. { (=) search not { (#) search not { () exch dup } if } if
  100. exch pop cvn dup where
  101. { pop (Redefining ) print print ( is not allowed.) = flush pop }
  102. { exch token
  103. { exch pop } % Probably should check for empty.
  104. { true }
  105. ifelse
  106. //systemdict 3 1 roll put
  107. }
  108. ifelse
  109. } bind def
  110. /D /d load def
  111. /f { dup length 0 ne { runfilejob } if } bind def
  112. /g
  113. { (x) search { cvi pop exch cvi } { cvi dup } ifelse
  114. //systemdict begin /DEVICEHEIGHT exch def /DEVICEWIDTH exch def end
  115. } bind def
  116. /r
  117. { (x) search { cvr pop exch cvr } { cvr dup } ifelse
  118. //systemdict begin /DEVICEYRESOLUTION exch def /DEVICEXRESOLUTION exch def end
  119. } bind def
  120. /s
  121. { (=) search not { (#) search not { () exch dup } if } if
  122. exch pop cvn dup where { pop dup load } { () } ifelse
  123. type /stringtype ne
  124. { (Redefining ) print print ( is not allowed.) = flush pop }
  125. { exch //systemdict 3 1 roll put }
  126. ifelse
  127. } bind def
  128. /S /s load def
  129. /Z { true .setdebug } bind def
  130. % ---------------- Main program ---------------- %
  131. % We process the command line in two passes. In the first pass,
  132. % we read and expand any @-files as necessary. The second pass
  133. % does the real work.
  134. /cmdstart
  135. { //cmddict begin
  136. /expand@ true def
  137. [
  138. % Process the GS_OPTIONS environment variable.
  139. (GS_OPTIONS) getenv { 0 () /SubFileDecode filter expandargfile } if
  140. % Process the actual command line.
  141. .getargv { expandarg } forall
  142. ] readonly /argv exch def
  143. % Now interpret the commands.
  144. { nextarg not { exit } if
  145. dup 0 get (-) 0 get eq
  146. { dup length 1 eq
  147. { pop (%stdin) (r) file runjob
  148. }
  149. { dup length 2 gt
  150. { dup dup length 2 sub 2 exch getinterval exch 1 1 getinterval }
  151. if currentdict .knownget
  152. { exec
  153. }
  154. { (Ignoring unknown switch ) print
  155. dup length 1 eq { (-) print print } if print
  156. () = flush
  157. }
  158. ifelse
  159. }
  160. ifelse
  161. }
  162. { runfilejob
  163. }
  164. ifelse
  165. }
  166. loop end
  167. } bind def
  168. end % cmddict