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