@node Starting and Stopping
@comment  node-name,  next,  previous,  up
@chapter Starting and Stopping

@menu
* Starting SBCL::               
* Stopping SBCL::               
* Command Line Options::        
* Initialization Files::        
@end menu

@node Starting SBCL
@comment  node-name,  next,  previous,  up
@section Starting SBCL

@menu
* Running from Shell::          
* Running from Emacs::          
* Shebang Scripts::             
@end menu

@node Running from Shell
@comment  node-name,  next,  previous,  up
@subsection From Shell to Lisp

To run SBCL type @command{sbcl} at the command line.

You should end up in the toplevel @dfn{REPL} (read, eval, print
-loop), where you can interact with SBCL by typing expressions.

@smallexample
$ sbcl
This is SBCL 0.8.13.60, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* (+ 2 2)

4
* (quit)
$
@end smallexample

See also @ref{Command Line Options} and @ref{Stopping SBCL}.

@node Running from Emacs
@comment  node-name,  next,  previous,  up
@subsection Running from Emacs

To run SBCL as an inferior-lisp from Emacs in your @file{.emacs} do
something like:

@lisp
;;; The SBCL binary and command-line arguments
(setq inferior-lisp-program "/usr/local/bin/sbcl --noinform")
@end lisp

For more information on using SBCL with Emacs, see @ref{Editor
Integration}.


@node Shebang Scripts
@comment  node-name,  next,  previous,  up
@subsection Shebang Scripts

SBCL doesn't come with built-in support for shebang-line execution,
but this can be provided with a shell trampoline, or by dispatching
from initialization files (@pxref{Unix-style Command Line Protocol} for
an example.)


@node Stopping SBCL
@comment  node-name,  next,  previous,  up
@section Stopping SBCL

@menu
* Quit::                        
* End of File::
* Saving a Core Image::              
* Exit on Errors::
@end menu

@node Quit
@comment  node-name,  next,  previous,  up
@subsection Quit

SBCL can be stopped at any time by calling @code{sb-ext:quit},
optionally returning a specified numeric value to the calling process.
See notes in @ref{Threading} about the interaction between this
feature and sessions.

@include fun-sb-ext-quit.texinfo

@node End of File
@comment  node-name,  next,  previous,  up
@subsection End of File

By default SBCL also exits on end of input, caused either by user
pressing @kbd{Control-D} on an attached terminal, or end of input when
using SBCL as part of a shell pipeline.

@node Saving a Core Image
@comment  node-name,  next,  previous,  up
@subsection Saving a Core Image

SBCL has the ability to save its state as a file for later
execution. This functionality is important for its bootstrapping
process, and is also provided as an extension to the user.  

@include fun-sb-ext-save-lisp-and-die.texinfo

@node Exit on Errors
@comment  node-name,  next,  previous,  up
@subsection Exit on Errors

SBCL can also be configured to exit if an unhandled error occurs,
which is mainly useful for acting as part of a shell pipeline; doing
so under most other circumstances would mean giving up large parts of
the flexibility and robustness of Common Lisp. See @ref{Debugger Entry}.

@node Command Line Options
@comment  node-name,  next,  previous,  up
@section Command Line Options

@c FIXME: This is essentially cut-and-paste from the manpage
@c What should probably be done is generate both this and the
@c man-page from ``sbcl --help'' output.

Command line options can be considered an advanced topic; for ordinary
interactive use, no command line arguments should be necessary.

In order to understand the command line argument syntax for SBCL, it
is helpful to understand that the SBCL system is implemented as two
components, a low-level runtime environment written in C and a
higher-level system written in Common Lisp itself. Some command line
arguments are processed during the initialization of the low-level
runtime environment, some command line arguments are processed during
the initialization of the Common Lisp system, and any remaining
command line arguments are passed on to user code.

The full, unambiguous syntax for invoking SBCL at the command line is:

@command{sbcl} @var{runtime-option}* @code{--end-runtime-options} @var{toplevel-option}* @code{--end-toplevel-options} @var{user-options}*

For convenience, the @code{--end-runtime-options} and
@code{--end-toplevel-options} elements can be omitted. Omitting these
elements can be convenient when you are running the program
interactively, and you can see that no ambiguities are possible with
the option values you are using. Omitting these elements is probably a
bad idea for any batch file where any of the options are under user
control, since it makes it impossible for SBCL to detect erroneous
command line input, so that erroneous command line arguments will be
passed on to the user program even if they was intended for the
runtime system or the Lisp system.

@menu
* Runtime Options::             
* Toplevel Options::            
@end menu

@node Runtime Options
@comment  node-name,  next,  previous,  up
@subsection Runtime Options

@table @code

@item --core @var{corefilename}
Run the specified Lisp core file instead of the default. Note that if
the Lisp core file is a user-created core file, it may run a
nonstandard toplevel which does not recognize the standard toplevel
options.

@item --noinform
Suppress the printing of any banner or other informational message at
startup. This makes it easier to write Lisp programs which work
cleanly in Unix pipelines. See also the @code{--noprint} and
@code{--disable-debugger} options.

@item --help
Print some basic information about SBCL, then exit.

@item --version
Print SBCL's version information, then exit.

@end table

In the future, runtime options may be added to control behaviour such
as lazy allocation of memory.

Runtime options, including any --end-runtime-options option, are
stripped out of the command line before the Lisp toplevel logic gets a
chance to see it.

@node Toplevel Options
@comment  node-name,  next,  previous,  up
@subsection Toplevel Options

@table @code

@item --sysinit @var{filename}
Load filename instead of the default system initialization file
(@pxref{System Initialization File}.) There is no special option to
cause no system initialization file to be read, but on a Unix
system ``@code{"--sysinit /dev/null}'' can be used to achieve the same
effect.

@item --userinit @var{filename}
Load filename instead of the default user initialization file
(@pxref{User Initialization File}.) There is no special option to
cause no user initialization file to be read, but ``@code{--userinit
/dev/null}'' can be used to achieve the same effect.

@item --eval @var{command}
After executing any initialization file, but before starting the
read-eval-print loop on standard input, read and evaluate the com-
mand given. More than one @code{--eval} option can be used, and all
will be read and executed, in the order they appear on the command
line.

@item --load @var{filename}
This is equivalent to @code{--eval '(load "@var{filename}")'}. The
special syntax is intended to reduce quoting headaches when invoking
SBCL from shell scripts.

@item --noprint
When ordinarily the toplevel "read-eval-print loop" would be exe-
cuted, execute a "read-eval loop" instead, i.e. don't print a prompt
and don't echo results. Combined with the @code{--noinform} runtime
option, this makes it easier to write Lisp "scripts" which work
cleanly in Unix pipelines.

@item --disable-debugger
This is equivalent to @code{--eval '(sb-ext:disable-debugger)'}.
@xref{Debugger Entry}.

@end table


@node Initialization Files
@comment  node-name,  next,  previous,  up
@section Initialization Files

This section covers initialization files processed at startup, which
can be used to customize the lisp environment.

@menu
* System Initialization File::  
* User Initialization File::    
* Initialization File Semantics::  
* Initialization Examples::     
@end menu

@node System Initialization File
@comment  node-name,  next,  previous,  up
@subsection System Initialization File

Site-wide startup script. Unless overridden with the command line
option @code{--sysinit} defaults to @file{@env{SBCL_HOME}/sbclrc}, or
if that doesn't exist to @file{/etc/sbclrc}.

No system initialization file is required.

@node User Initialization File
@comment  node-name,  next,  previous,  up
@subsection User Initialization File

Per-user startup script. Unless overridden with the command line
option @code{--userinit} defaults to @file{@env{HOME}/.sbclrc}.

No user initialization file is required.

@node Initialization File Semantics
@comment  node-name,  next,  previous,  up
@subsection Initialization File Semantics

SBCL processes initialization files with @code{read} and @code{eval},
not @code{load}; hence initialization files can be used to set startup
@code{*package*} and @code{*readtable*}, and for proclaiming a global
optimization policy.

@node Initialization Examples
@comment  node-name,  next,  previous,  up
@subsection Initialization Examples

Some examples of what you may consider doing in the initialization
files follow.

@menu
* Unix-style Command Line Protocol::  
* Automatic Recompilation of Stale Fasls::  
@end menu

@node Unix-style Command Line Protocol
@comment  node-name,  next,  previous,  up
@subsubsection Unix-style Command Line Protocol

Standard Unix tools that are interpreters follow a common command line
protocol that is necessary to work with ``shebang scripts''. SBCL
doesn't do this by default, but adding the following snippet to an
initialization file does the trick:

@lisp
;;; If the first user-processable command-line argument is a filename,
;;; disable the debugger, load the file handling shebang-line and quit.
(let ((script (and (second *posix-argv*) 
                   (probe-file (second *posix-argv*)))))
   (when script
      ;; Handle shebang-line
      (set-dispatch-macro-character #\# #\!
                                    (lambda (stream char arg)
                                       (declare (ignore char arg))
                                       (read-line stream)))
      ;; Disable debugger
      (setf *invoke-debugger-hook* 
            (lambda (condition hook)
              (declare (ignore hook))
              ;; Uncomment to get backtraces on errors
              ;; (sb-debug:backtrace 20)
              (format *error-output* "Error: ~A~%" condition)
              (quit)))
      (load script)
      (quit)))
@end lisp

Example file (@file{hello.lisp}):

@lisp
#!/usr/local/bin/sbcl --noinform
(write-line "Hello, World!")
@end lisp

Usage examples:

@smallexample
$ ./hello.lisp
Hello, World!
@end smallexample

@smallexample
$ sbcl hello.lisp
This is SBCL 0.8.13.70, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
Hello, World!
@end smallexample


@node Automatic Recompilation of Stale Fasls
@comment  node-name,  next,  previous,  up
@subsubsection Automatic Recompilation of Stale Fasls

SBCL fasl-format is at current stage of development undergoing
non-backwards compatible changes fairly often. The following snippet
handles recompilation automatically for ASDF-based systems.

@lisp
(require :asdf)

;;; If a fasl was stale, try to recompile and load (once). 
(defmethod asdf:perform :around ((o asdf:load-op) 
                                 (c asdf:cl-source-file))
   (handler-case (call-next-method o c)
      ;; If a fasl was stale, try to recompile and load (once).
      (sb-ext:invalid-fasl ()
         (asdf:perform (make-instance 'asdf:compile-op) c)
         (call-next-method))))
@end lisp
