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