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