Commit | Line | Data |
---|---|---|
38a93523 NJ |
1 | |
2 | @node Guile Scripting | |
3 | @chapter Guile Scripting | |
4 | ||
5 | Like AWK, Perl, or any shell, Guile can interpret script files. A Guile | |
6 | script is simply a file of Scheme code with some extra information at | |
7 | the beginning which tells the operating system how to invoke Guile, and | |
8 | then tells Guile how to handle the Scheme code. | |
9 | ||
10 | @menu | |
11 | * Invoking Guile:: How to start a Guile script. | |
12 | * The Meta Switch:: Passing complex argument lists to Guile | |
13 | from shell scripts. | |
14 | @end menu | |
15 | ||
16 | @node Invoking Guile | |
17 | @section Invoking Guile | |
18 | ||
19 | Here we describe Guile's command-line processing in detail. Guile | |
20 | processes its arguments from left to right, recognizing the switches | |
21 | described below. For examples, see @ref{Scripting Examples}. | |
22 | ||
23 | @table @code | |
24 | ||
25 | @item -s @var{script} @var{arg...} | |
26 | Read and evaluate Scheme source code from the file @var{script}, as the | |
27 | @code{load} function would. After loading @var{script}, exit. Any | |
28 | command-line arguments @var{arg...} following @var{script} become the | |
29 | script's arguments; the @code{command-line} function returns a list of | |
30 | strings of the form @code{(@var{script} @var{arg...})}. | |
31 | ||
32 | @item -c @var{expr} @var{arg...} | |
33 | Evaluate @var{expr} as Scheme code, and then exit. Any command-line | |
34 | arguments @var{arg...} following @var{expr} become command-line arguments; the | |
35 | @code{command-line} function returns a list of strings of the form | |
36 | @code{(@var{guile} @var{arg...})}, where @var{guile} is the path of the | |
37 | Guile executable. | |
38 | ||
39 | @item -- @var{arg...} | |
40 | Run interactively, prompting the user for expressions and evaluating | |
41 | them. Any command-line arguments @var{arg...} following the @code{--} | |
42 | become command-line arguments for the interactive session; the | |
43 | @code{command-line} function returns a list of strings of the form | |
44 | @code{(@var{guile} @var{arg...})}, where @var{guile} is the path of the | |
45 | Guile executable. | |
46 | ||
47 | @item -l @var{file} | |
48 | Load Scheme source code from @var{file}, and continue processing the | |
49 | command line. | |
50 | ||
51 | @item -e @var{function} | |
52 | Make @var{function} the @dfn{entry point} of the script. After loading | |
53 | the script file (with @code{-s}) or evaluating the expression (with | |
54 | @code{-c}), apply @var{function} to a list containing the program name | |
55 | and the command-line arguments --- the list provided by the | |
56 | @code{command-line} function. | |
57 | ||
58 | A @code{-e} switch can appear anywhere in the argument list, but Guile | |
59 | always invokes the @var{function} as the @emph{last} action it performs. | |
60 | This is weird, but because of the way script invocation works under | |
61 | POSIX, the @code{-s} option must always come last in the list. | |
62 | ||
63 | @xref{Scripting Examples}. | |
64 | ||
65 | @item -ds | |
66 | Treat a final @code{-s} option as if it occurred at this point in the | |
67 | command line; load the script here. | |
68 | ||
69 | This switch is necessary because, although the POSIX script invocation | |
70 | mechanism effectively requires the @code{-s} option to appear last, the | |
71 | programmer may well want to run the script before other actions | |
72 | requested on the command line. For examples, see @ref{Scripting | |
73 | Examples}. | |
74 | ||
75 | @item \ | |
76 | Read more command-line arguments, starting from the second line of the | |
77 | script file. @xref{The Meta Switch}. | |
78 | ||
79 | @item --emacs | |
80 | Assume Guile is running as an inferior process of Emacs, and use a | |
81 | special protocol to communicate with Emacs's Guile interaction mode. | |
82 | This switch sets the global variable use-emacs-interface to @code{#t}. | |
83 | ||
84 | This switch is still experimental. | |
85 | ||
fb02eb66 MG |
86 | @item --use-srfi=@var{list} |
87 | The option @code{--use-srfi} expects a comma-separated list of numbers, | |
88 | each representing a SRFI number to be loaded into the interpreter | |
89 | before starting evaluating a script file or the REPL. Additionally, | |
90 | the feature identifier for the loaded SRFIs is recognized by | |
91 | `cond-expand' when using this option. | |
92 | ||
93 | @example | |
94 | guile --use-srfi=8,13 | |
95 | @end example | |
96 | ||
38a93523 NJ |
97 | @item -h@r{, }--help |
98 | Display help on invoking Guile, and then exit. | |
99 | ||
100 | @item -v@r{, }--version | |
101 | Display the current version of Guile, and then exit. | |
102 | ||
103 | @end table | |
104 | ||
105 | ||
106 | @node The Meta Switch | |
107 | @section The Meta Switch | |
108 | ||
109 | Guile's command-line switches allow the programmer to describe | |
110 | reasonably complicated actions in scripts. Unfortunately, the POSIX | |
111 | script invocation mechanism only allows one argument to appear on the | |
112 | @samp{#!} line after the path to the Guile executable, and imposes | |
113 | arbitrary limits on that argument's length. Suppose you wrote a script | |
114 | starting like this: | |
115 | @example | |
116 | #!/usr/local/bin/guile -e main -s | |
117 | !# | |
118 | (define (main args) | |
119 | (map (lambda (arg) (display arg) (display " ")) | |
120 | (cdr args)) | |
121 | (newline)) | |
122 | @end example | |
123 | The intended meaning is clear: load the file, and then call @code{main} | |
124 | on the command-line arguments. However, the system will treat | |
125 | everything after the Guile path as a single argument --- the string | |
126 | @code{"-e main -s"} --- which is not what we want. | |
127 | ||
128 | As a workaround, the meta switch @code{\} allows the Guile programmer to | |
129 | specify an arbitrary number of options without patching the kernel. If | |
130 | the first argument to Guile is @code{\}, Guile will open the script file | |
131 | whose name follows the @code{\}, parse arguments starting from the | |
132 | file's second line (according to rules described below), and substitute | |
133 | them for the @code{\} switch. | |
134 | ||
135 | Working in concert with the meta switch, Guile treats the characters | |
136 | @samp{#!} as the beginning of a comment which extends through the next | |
137 | line containing only the characters @samp{!#}. This sort of comment may | |
138 | appear anywhere in a Guile program, but it is most useful at the top of | |
139 | a file, meshing magically with the POSIX script invocation mechanism. | |
140 | ||
141 | Thus, consider a script named @file{/u/jimb/ekko} which starts like this: | |
142 | @example | |
143 | #!/usr/local/bin/guile \ | |
144 | -e main -s | |
145 | !# | |
146 | (define (main args) | |
147 | (map (lambda (arg) (display arg) (display " ")) | |
148 | (cdr args)) | |
149 | (newline)) | |
150 | @end example | |
151 | ||
152 | Suppose a user invokes this script as follows: | |
153 | @example | |
154 | $ /u/jimb/ekko a b c | |
155 | @end example | |
156 | ||
157 | Here's what happens: | |
158 | @itemize @bullet | |
159 | ||
160 | @item | |
161 | the operating system recognizes the @samp{#!} token at the top of the | |
162 | file, and rewrites the command line to: | |
163 | @example | |
164 | /usr/local/bin/guile \ /u/jimb/ekko a b c | |
165 | @end example | |
166 | This is the usual behavior, prescribed by POSIX. | |
167 | ||
168 | @item | |
169 | When Guile sees the first two arguments, @code{\ /u/jimb/ekko}, it opens | |
170 | @file{/u/jimb/ekko}, parses the three arguments @code{-e}, @code{main}, | |
171 | and @code{-s} from it, and substitutes them for the @code{\} switch. | |
172 | Thus, Guile's command line now reads: | |
173 | @example | |
174 | /usr/local/bin/guile -e main -s /u/jimb/ekko a b c | |
175 | @end example | |
176 | ||
177 | @item | |
178 | Guile then processes these switches: it loads @file{/u/jimb/ekko} as a | |
179 | file of Scheme code (treating the first three lines as a comment), and | |
180 | then performs the application @code{(main "/u/jimb/ekko" "a" "b" "c")}. | |
181 | ||
182 | @end itemize | |
183 | ||
184 | ||
185 | When Guile sees the meta switch @code{\}, it parses command-line | |
186 | argument from the script file according to the following rules: | |
187 | @itemize @bullet | |
188 | ||
189 | @item | |
190 | Each space character terminates an argument. This means that two | |
191 | spaces in a row introduce an argument @code{""}. | |
192 | ||
193 | @item | |
194 | The tab character is not permitted (unless you quote it with the | |
195 | backslash character, as described below), to avoid confusion. | |
196 | ||
197 | @item | |
198 | The newline character terminates the sequence of arguments, and will | |
199 | also terminate a final non-empty argument. (However, a newline | |
200 | following a space will not introduce a final empty-string argument; | |
201 | it only terminates the argument list.) | |
202 | ||
203 | @item | |
204 | The backslash character is the escape character. It escapes backslash, | |
205 | space, tab, and newline. The ANSI C escape sequences like @code{\n} and | |
206 | @code{\t} are also supported. These produce argument constituents; the | |
207 | two-character combination @code{\n} doesn't act like a terminating | |
208 | newline. The escape sequence @code{\@var{NNN}} for exactly three octal | |
209 | digits reads as the character whose ASCII code is @var{NNN}. As above, | |
210 | characters produced this way are argument constituents. Backslash | |
211 | followed by other characters is not allowed. | |
212 | ||
213 | @end itemize |