Skip to content

Commit 17d7e1f

Browse files
author
Jan Wielemaker
committed
DOC: New program initialization handling.
1 parent a637b68 commit 17d7e1f

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

library/main.pl

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Author: Jan Wielemaker
44
55
WWW: http://www.swi-prolog.org
6-
Copyright (c) 2002-2016, University of Amsterdam
6+
Copyright (c) 2002-2017, University of Amsterdam
77
VU University Amsterdam
88
All rights reserved.
99
@@ -49,7 +49,7 @@
4949
==
5050
#!/usr/bin/env swipl
5151
52-
:- initialization main.
52+
:- initialization(main, main).
5353
5454
main(Argv) :-
5555
echo(Argv).
@@ -71,7 +71,9 @@
7171

7272
%! main
7373
%
74-
% Call main/1 using the passed command-line arguments.
74+
% Call main/1 using the passed command-line arguments. Before calling
75+
% main/1 this predicate installs a signal handler for =SIGINT=
76+
% (Control-C) that terminates the process with status 1.
7577

7678
main :-
7779
context_module(M),

man/builtin.doc

+34-7
Original file line numberDiff line numberDiff line change
@@ -809,13 +809,40 @@ compatibility issue.
809809
Similar to initialization/1, but allows for specifying when \arg{Goal}
810810
is executed while loading the program text:
811811

812-
\begin{description}
813-
\termitem{now}{} Execute \arg{Goal} immediately.
814-
\termitem{after_load}{} Execute \arg{Goal} after loading
815-
program text. This is the same as initialization/1.
816-
\termitem{restore}{} Do not execute \arg{Goal} while loading
817-
the program, but \emph{only} when restoring a state.
818-
\end{description}
812+
\begin{description}
813+
\termitem{now}{} Execute \arg{Goal} immediately.
814+
815+
\termitem{after_load}{} Execute \arg{Goal} after loading the
816+
program text in which the directive appears. This is the same as
817+
initialization/1.
818+
819+
\termitem{restore}{}
820+
Do not execute \arg{Goal} while loading the program, but \emph{only}
821+
when restoring a saved state.
822+
823+
\termitem{program}{}
824+
Execute \arg{Goal} once after executing the \cmdlineoption{-g} goals
825+
at program startup. Registered goals are executed in the order
826+
encountered and a failure or exception causes the Prolog to exit with
827+
non-zero exit status. These goals are \emph{not} executed if the
828+
\cmdlineoption{-l} is given to merely \emph{load} files. In that case
829+
they may be executed explicitly using initialize/0. See also
830+
\secref{plscript}.
831+
832+
\termitem{main}{}
833+
When Prolog starts, the last goal registered using
834+
\term{initialization}{Goal, main} is executed as main goal. If
835+
\arg{Goal} fails or raises an exception, the process terminates with
836+
non-zero exit code. If \arg{Goal} succeeds, the process terminates
837+
with zero exit code. See also \secref{plscript}.
838+
\end{description}
839+
840+
\predicate[det]{initialization}{0}{}
841+
Run all initialization goals registered using
842+
\term{initialization}{Goal, program}. Raises an error
843+
\term{initialization_error}{Reason, Goal, File:Line} if \arg{Goal}
844+
fails or raises an exception. \arg{Reason} is \const{failed} or
845+
the exception raised.
819846

820847
\predicate{compiling}{0}{}
821848
True if the system is compiling source files with the \cmdlineoption{-c}

man/overview.doc

+18-22
Original file line numberDiff line numberDiff line change
@@ -844,16 +844,14 @@ vs.\ Windows).
844844
\subsubsection{Using PrologScript} \label{sec:plscript}
845845

846846
A Prolog source file can be used directly as a Unix program using the
847-
Unix \verb$#!$ magic start. The same mechanism is useful for specifying
848-
additional parameters for running a Prolog file on Windows. The Unix
849-
\verb$#!$ magic is allowed because if the first letter of a Prolog file
850-
is \verb$#$, the first line is treated as a comment.\footnote{The
851-
\texttt{\#}-sign can be the legal start of a normal Prolog clause. In
852-
the unlikely case this is required, leave the first line blank or add a
853-
header comment.} To create a Prolog script, use one of the two
854-
alternatives below as first line. The first can be used to bind a script
855-
to a specific Prolog installation, while the latter uses the default
856-
prolog installed in \verb"$PATH".
847+
Unix \verb$#!$ magic start. The Unix \verb$#!$ magic is allowed because
848+
if the first letter of a Prolog file is \verb$#$, the first line is
849+
treated as a comment.\footnote{The \texttt{\#}-sign can be the legal
850+
start of a normal Prolog clause. In the unlikely case this is required,
851+
leave the first line blank or add a header comment.} To create a Prolog
852+
script, use one of the two alternatives below as first line. The first
853+
can be used to bind a script to a specific Prolog installation, while
854+
the latter uses the default prolog installed in \verb"$PATH".
857855

858856
\begin{code}
859857
!/path/to/swipl
@@ -866,26 +864,25 @@ portability, the \verb$#!$ must be followed immediately with an absolute
866864
path to the executable and should have none or one argument. Neither the
867865
executable path, nor the argument shall use quotes or spaces. When
868866
started this way, the Prolog flag \prologflag{argv} contains the command
869-
line arguments that follow the script invocation. Below is a simple
870-
script doing expression evaluation:
867+
line arguments that follow the script invocation.
868+
869+
Starting with version 7.5.8, initialization/2 support the \arg{When}
870+
options \const{program} and \const{main}, allowing for the following
871+
definition of a Prolog script that evaluates an arithmetic expression on
872+
the command line. Note that main/0 is defined lib the library
873+
\pllib{main}. It calls main/1 with the command line arguments after
874+
disabling signal handling.
871875

872876
\begin{code}
873877
#!/usr/bin/env swipl
874878

875-
:- initialization main.
879+
:- initialization(main, main).
876880

877-
eval :-
878-
current_prolog_flag(argv, Argv),
881+
main(Argv) :-
879882
concat_atom(Argv, ' ', SingleArg),
880883
term_to_atom(Term, SingleArg),
881884
Val is Term,
882885
format('~w~n', [Val]).
883-
884-
main :-
885-
catch(eval, E, (print_message(error, E), fail)),
886-
halt.
887-
main :-
888-
halt(1).
889886
\end{code}
890887

891888
And here are two example runs:
@@ -895,7 +892,6 @@ And here are two example runs:
895892
3
896893
% ./eval foo
897894
ERROR: is/2: Arithmetic: `foo/0' is not a function
898-
%
899895
\end{code}
900896

901897
The Windows version simply ignores the \verb$#!$ line.\footnote{Older

man/summary.doc

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ suggest predicates from a keyword.
337337
\predicatesummary{include}{1}{Include a file with declarations}
338338
\predicatesummary{initialization}{1}{Initialization directive}
339339
\predicatesummary{initialization}{2}{Initialization directive}
340+
\predicatesummary{initialize}{0}{Run program initialization}
340341
\predicatesummary{instance}{2}{Fetch clause or record from reference}
341342
\predicatesummary{integer}{1}{Type check for integer}
342343
\predicatesummary{interactor}{0}{Start new thread with console and top level}

0 commit comments

Comments
 (0)