@c -*-texinfo-*-
@c This is part of the GNU Guile Reference Manual.
@c Copyright (C) 1996-1997,2000-2005,2010-2011,2013-2014,2016,2019
@c   Free Software Foundation, Inc.
@c See the file guile.texi for copying conditions.

@node Invoking Guile
@section Invoking Guile
@cindex invocation

Many features of Guile depend on and can be changed by information that
the user provides either before or when Guile is started.  Below is a
description of what information to provide and how to provide it.

@menu
* Command-line Options::        Command-line options understood by Guile.
* Environment Variables::       Variables that affect Guile's behavior.
@end menu

@node Command-line Options
@subsection Command-line Options
@cindex Command-line Options
@cindex command-line arguments
@cindex arguments (command line)
@cindex options (command line)
@cindex switches (command line)
@cindex startup (command-line arguments)
@cindex invocation (command-line arguments)

Here we describe Guile's command-line processing in detail.  Guile
processes its arguments from left to right, recognizing the switches
described below.  For examples, see @ref{Scripting Examples}.

@table @code

@item @var{script} @var{arg...}
@itemx -s @var{script} @var{arg...}
@cindex script mode
By default, Guile will read a file named on the command line as a
script.  Any command-line arguments @var{arg...} following @var{script}
become the script's arguments; the @code{command-line} function returns
a list of strings of the form @code{(@var{script} @var{arg...})}.

It is possible to name a file using a leading hyphen, for example,
@file{-myfile.scm}.  In this case, the file name must be preceded by
@option{-s} to tell Guile that a (script) file is being named.

Scripts are read and evaluated as Scheme source code just as the
@code{load} function would.  After loading @var{script}, Guile exits.

@item -c @var{expr} @var{arg...}
@cindex evaluate expression, command-line argument
Evaluate @var{expr} as Scheme code, and then exit.  Any command-line
arguments @var{arg...} following @var{expr} become command-line
arguments; the @code{command-line} function returns a list of strings of
the form @code{(@var{guile} @var{arg...})}, where @var{guile} is the
path of the Guile executable.

@item -- @var{arg...}
Run interactively, prompting the user for expressions and evaluating
them.  Any command-line arguments @var{arg...} following the @option{--}
become command-line arguments for the interactive session; the
@code{command-line} function returns a list of strings of the form
@code{(@var{guile} @var{arg...})}, where @var{guile} is the path of the
Guile executable.

@item -L @var{directory}
Add @var{directory} to the front of Guile's module load path.  The given
directories are searched in the order given on the command line and
before any directories in the @env{GUILE_LOAD_PATH} environment
variable.  Paths added here are @emph{not} in effect during execution of
the user's @file{.guile} file.

@item -C @var{directory}
Like @option{-L}, but adjusts the load path for @emph{compiled} files.

@item -x @var{extension}
Add @var{extension} to the front of Guile's load extension list
(@pxref{Load Paths, @code{%load-extensions}}).  The specified extensions
are tried in the order given on the command line, and before the default
load extensions.  Extensions added here are @emph{not} in effect during
execution of the user's @file{.guile} file.

@item -l @var{file}
Load Scheme source code from @var{file}, and continue processing the
command line.

@item -e @var{function}
Make @var{function} the @dfn{entry point} of the script.  After loading
the script file (with @option{-s}) or evaluating the expression (with
@option{-c}), apply @var{function} to a list containing the program name
and the command-line arguments---the list provided by the
@code{command-line} function.

A @option{-e} switch can appear anywhere in the argument list, but Guile
always invokes the @var{function} as the @emph{last} action it performs.
This is weird, but because of the way script invocation works under
POSIX, the @option{-s} option must always come last in the list.

The @var{function} is most often a simple symbol that names a function
that is defined in the script.  It can also be of the form @code{(@@
@var{module-name} @var{symbol})}, and in that case, the symbol is
looked up in the module named @var{module-name}.

As a shorthand you can use the form @code{(symbol ...)}, that is, a list
of only symbols that doesn't start with @code{@@}.  It is equivalent to
@code{(@@ @var{module-name} main)}, where @var{module-name} is
@code{(symbol ...)} form.  @xref{Using Guile Modules} and @ref{Scripting
Examples}.

@item -ds
Treat a final @option{-s} option as if it occurred at this point in the
command line; load the script here.

This switch is necessary because, although the POSIX script invocation
mechanism effectively requires the @option{-s} option to appear last, the
programmer may well want to run the script before other actions
requested on the command line.  For examples, see @ref{Scripting
Examples}.

@item \
Read more command-line arguments, starting from the second line of the
script file.  @xref{The Meta Switch}.

@item --use-srfi=@var{list}
@cindex loading srfi modules (command line)
The option @option{--use-srfi} expects a comma-separated list of numbers,
each representing a SRFI module to be loaded into the interpreter
before evaluating a script file or starting the REPL.  Additionally,
the feature identifier for the loaded SRFIs is recognized by
the procedure @code{cond-expand} when this option is used.

Here is an example that loads the modules SRFI-8 ('receive') and SRFI-13
('string library') before the GUILE interpreter is started:

@example
guile --use-srfi=8,13
@end example

@item --r6rs
@cindex r6rs (command line)
Adapt Guile's initial environment to better support R6RS.  @xref{R6RS
Incompatibilities}, for some caveats.

@item --r7rs
@cindex r7rs (command line)
Adapt Guile's initial environment to better support R7RS.  @xref{R7RS
Incompatibilities}, for some caveats.

@item --debug
@cindex debugging virtual machine (command line)
Start with the debugging virtual machine (VM) engine.  Using the
debugging VM will enable support for VM hooks, which are needed for
tracing, breakpoints, and accurate call counts when profiling.  The
debugging VM is slower than the regular VM, though, by about ten
percent.  @xref{VM Hooks}, for more information.

By default, the debugging VM engine is only used when entering an
interactive session.  When executing a script with @option{-s} or
@option{-c}, the normal, faster VM is used by default.

@vnew{1.8}
@item --no-debug
@cindex debugging virtual machine (command line)
Do not use the debugging VM engine, even when entering an interactive
session.

Note that, despite the name, Guile running with @option{--no-debug}
@emph{does} support the usual debugging facilities, such as printing a
detailed backtrace upon error.  The only difference with
@option{--debug} is lack of support for VM hooks and the facilities that
build upon it (see above).

@item -q
@cindex init file, not loading
@cindex @file{.guile} file, not loading
Do not load the initialization file, @file{.guile}.  This option only
has an effect when running interactively; running scripts does not load
the @file{.guile} file.  @xref{Init File}.

@item --listen[=@var{p}]
While this program runs, listen on a local port or a path for REPL
clients.  If @var{p} starts with a number, it is assumed to be a local
port on which to listen.  If it starts with a forward slash, it is
assumed to be the file name of a UNIX domain socket on which to listen.

If @var{p} is not given, the default is local port 37146.  If you look
at it upside down, it almost spells ``Guile''.  If you have netcat
installed, you should be able to @kbd{nc localhost 37146} and get a
Guile prompt.  Alternately you can fire up Emacs and connect to the
process; see @ref{Using Guile in Emacs} for more details.

@quotation Note
Opening a port allows anyone who can connect to that port to do anything
Guile can do, as the user
that the Guile process is running as.  Do not use @option{--listen} on
multi-user machines.  Of course, if you do not pass @option{--listen} to
Guile, no port will be opened.

Guile protects against the
@uref{https://en.wikipedia.org/wiki/Inter-protocol_exploitation,
@dfn{HTTP inter-protocol exploitation attack}}, a scenario whereby an
attacker can, @i{via} an HTML page, cause a web browser to send data to
TCP servers listening on a loopback interface or private network.
Nevertheless, you are advised to use UNIX domain sockets, as in
@code{--listen=/some/local/file}, whenever possible.
@end quotation

That said, @option{--listen} is great for interactive debugging and
development.

@vnew{2.0}

@item --auto-compile
Compile source files automatically (default behavior).

@vnew{2.0.1}

@item --fresh-auto-compile
Treat the auto-compilation cache as invalid, forcing recompilation.

@vnew{2.0}

@item --no-auto-compile
Disable automatic source file compilation.

@vnew{2.0.8}

@item --language=@var{lang}
For the remainder of the command line arguments, assume that files
mentioned with @code{-l} and expressions passed with @code{-c} are
written in @var{lang}.  @var{lang} must be the name of one of the
languages supported by the compiler (@pxref{Compiler Tower}).  When run
interactively, set the REPL's language to @var{lang} (@pxref{Using Guile
Interactively}).

The default language is @code{scheme}; other interesting values include
@code{elisp} (for Emacs Lisp), and @code{ecmascript}.

The example below shows the evaluation of expressions in Scheme, Emacs
Lisp, and ECMAScript:

@example
guile -c "(apply + '(1 2))"
guile --language=elisp -c "(= (funcall (symbol-function '+) 1 2) 3)"
guile --language=ecmascript -c '(function (x) @{ return x * x; @})(2);'
@end example

To load a file written in Scheme and one written in Emacs Lisp, and then
start a Scheme REPL, type:

@example
guile -l foo.scm --language=elisp -l foo.el --language=scheme
@end example

@vnew{2.0}

@item -h@r{, }--help
Display help on invoking Guile, and then exit.

@item -v@r{, }--version
Display the current version of Guile, and then exit.

@end table

@node Environment Variables
@subsection Environment Variables
@cindex environment variables
@cindex shell
@cindex initialization
The @dfn{environment} is a feature of the operating system; it consists
of a collection of variables with names and values.  Each variable is
called an @dfn{environment variable} (or, sometimes, a ``shell
variable''); environment variable names are case-sensitive, and it is
conventional to use upper-case letters only.  The values are all text
strings, even those that are written as numerals.  (Note that here we
are referring to names and values that are defined in the operating
system shell from which Guile is invoked.  This is not the same as a
Scheme environment that is defined within a running instance of Guile.
For a description of Scheme environments, @pxref{About Environments}.)
   
How to set environment variables before starting Guile depends on the
operating system and, especially, the shell that you are using.  For
example, here is how to tell Guile to provide detailed warning messages
about deprecated features by setting @env{GUILE_WARN_DEPRECATED} using
Bash:

@example
$ export GUILE_WARN_DEPRECATED="detailed"
$ guile
@end example

@noindent
Or, detailed warnings can be turned on for a single invocation using:

@example
$ env GUILE_WARN_DEPRECATED="detailed" guile
@end example

If you wish to retrieve or change the value of the shell environment
variables that affect the run-time behavior of Guile from within a
running instance of Guile, see @ref{Runtime Environment}.

Here are the environment variables that affect the run-time behavior of
Guile:

@table @env
@item GUILE_AUTO_COMPILE
@vindex GUILE_AUTO_COMPILE
This is a flag that can be used to tell Guile whether or not to compile
Scheme source files automatically.  Starting with Guile 2.0, Scheme
source files will be compiled automatically, by default.

If a compiled (@file{.go}) file corresponding to a @file{.scm} file is
not found or is not newer than the @file{.scm} file, the @file{.scm}
file will be compiled on the fly, and the resulting @file{.go} file
stored away.  An advisory note will be printed on the console.

Compiled files will be stored in the directory
@file{$XDG_CACHE_HOME/@/guile/@/ccache}, where @env{XDG_CACHE_HOME}
defaults to the directory @file{$HOME/.cache}.  This directory will be
created if it does not already exist.

Note that this mechanism depends on the timestamp of the @file{.go} file
being newer than that of the @file{.scm} file; if the @file{.scm} or
@file{.go} files are moved after installation, care should be taken to
preserve their original timestamps.

Set @env{GUILE_AUTO_COMPILE} to zero (0), to prevent Scheme files from
being compiled automatically.  Set this variable to ``fresh'' to tell
Guile to compile Scheme files whether they are newer than the compiled
files or not.

@xref{Compilation}.

@item GUILE_HISTORY
@vindex GUILE_HISTORY
This variable names the file that holds the Guile REPL command history.
You can specify a different history file by setting this environment
variable.  By default, the history file is @file{$HOME/.guile_history}.

@item GUILE_INSTALL_LOCALE
@vindex GUILE_INSTALL_LOCALE
This is a flag that can be used to tell Guile whether or not to install
the current locale at startup, via a call to @code{(setlocale LC_ALL
"")}@footnote{The @code{GUILE_INSTALL_LOCALE} environment variable was
ignored in Guile versions prior to 2.0.9.}.  @xref{Locales}, for more
information on locales.

You may explicitly indicate that you do not want to install
the locale by setting @env{GUILE_INSTALL_LOCALE} to @code{0}, or
explicitly enable it by setting the variable to @code{1}.

Usually, installing the current locale is the right thing to do.  It
allows Guile to correctly parse and print strings with non-ASCII
characters.  Therefore, this option is on by default.

@item GUILE_LOAD_COMPILED_PATH
@vindex GUILE_LOAD_COMPILED_PATH
This variable may be used to augment the path that is searched for
compiled Scheme files (@file{.go} files) when loading.  Its value should
be a colon-separated list of directories.  If it contains the special
path component @code{...} (ellipsis), then the default path is put in
place of the ellipsis, otherwise the default path is placed at the end.
The result is stored in @code{%load-compiled-path} (@pxref{Load Paths}).

Here is an example using the Bash shell that adds the current directory,
@file{.}, and the relative directory @file{../my-library} to
@code{%load-compiled-path}:

@example
$ export GUILE_LOAD_COMPILED_PATH=".:../my-library"
$ guile -c '(display %load-compiled-path) (newline)'
(. ../my-library /usr/local/lib/guile/3.0/ccache)
@end example

@item GUILE_LOAD_PATH
@vindex GUILE_LOAD_PATH
This variable may be used to augment the path that is searched for
Scheme files when loading.  Its value should be a colon-separated list
of directories.  If it contains the special path component @code{...}
(ellipsis), then the default path is put in place of the ellipsis,
otherwise the default path is placed at the end.  The result is stored
in @code{%load-path} (@pxref{Load Paths}).

Here is an example using the Bash shell that prepends the current
directory to @code{%load-path}, and adds the relative directory
@file{../srfi} to the end:

@example
$ env GUILE_LOAD_PATH=".:...:../srfi" \
guile -c '(display %load-path) (newline)'
(. /usr/local/share/guile/3.0 \
/usr/local/share/guile/site/3.0 \
/usr/local/share/guile/site \
/usr/local/share/guile \
../srfi)
@end example

(Note: The line breaks, above, are for documentation purposes only, and
not required in the actual example.)

@item GUILE_WARN_DEPRECATED
@vindex GUILE_WARN_DEPRECATED
As Guile evolves, some features will be eliminated or replaced by newer
features.  To help users migrate their code as this evolution occurs,
Guile will issue warning messages about code that uses features that
have been marked for eventual elimination.  @env{GUILE_WARN_DEPRECATED}
can be set to ``no'' to tell Guile not to display these warning
messages, or set to ``detailed'' to tell Guile to display more lengthy
messages describing the warning.  @xref{Deprecation}.

@item HOME
@vindex HOME
Guile uses the environment variable @env{HOME}, the name of your home
directory, to locate various files, such as @file{.guile} or
@file{.guile_history}.

@item GUILE_JIT_THRESHOLD
@vindex GUILE_JIT_THRESHOLD
Guile has a just-in-time (JIT) code generator that makes running Guile
code fast.  @xref{Just-In-Time Native Code}, for more.  The unit of code
generation is the function.  Each function has its own counter that gets
incremented when the function is called and at each loop iteration in
the function.  When the counter exceeds the @env{GUILE_JIT_THRESHOLD},
the function will get JIT-compiled.  Set @env{GUILE_JIT_THRESHOLD} to
@code{-1} to disable JIT compilation, or @code{0} to eagerly JIT-compile
each function as it's first seen.

@item GUILE_JIT_LOG
@vindex GUILE_JIT_LOG
Set to @code{1}, @code{2}, or @code{3} to give increasing amounts of
logging for JIT compilation events.  Used for debugging.

@item GUILE_JIT_STOP_AFTER
@vindex GUILE_JIT_STOP_AFTER
Though we have tested the JIT compiler as well as we can, it's possible
that it has bugs.  If you suspect that Guile's JIT compiler is causing
your program to fail, set @env{GUILE_JIT_STOP_AFTER} to a positive
integer indicating the maximum number of functions to JIT-compile.  By
bisecting over the value of @env{GUILE_JIT_STOP_AFTER}, you can pinpoint
the precise function that is being miscompiled.

@end table

@c Local Variables: 
@c mode: texinfo
@c TeX-master: "guile"
@c End: 
