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