1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2002-2017, University of Amsterdam 7 VU University Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(prolog_main, 37 [ main/0, 38 argv_options/3 % +Argv, -RestArgv, -Options 39 ]).
69:- module_transparent
70 main/0.
SIGINT
(Control-C) that terminates the process with status 1.78main :- 79 context_module(M), 80 set_signals, 81 current_prolog_flag(argv, Av), 82 call(M:main, Av). 83 84set_signals :- 85 on_signal(int, _, interrupt).
92interrupt(_Sig) :-
93 halt(1).
no-
, in which
case the option is mapped to Name(false). Numeric option values
are mapped to Prolog numbers.
107argv_options([], [], []). 108argv_options([H0|T0], R, [H|T]) :- 109 sub_atom(H0, 0, _, _, --), 110 !, 111 ( sub_atom(H0, B, _, A, =) 112 -> B2 is B-2, 113 sub_atom(H0, 2, B2, _, Name), 114 sub_string(H0, _, A, 0, Value0), 115 convert_option(Name, Value0, Value) 116 ; sub_atom(H0, 2, _, 0, Name0), 117 ( sub_atom(Name0, 0, _, _, 'no-') 118 -> sub_atom(Name0, 3, _, 0, Name), 119 Value = false 120 ; Name = Name0, 121 Value = true 122 ) 123 ), 124 H =.. [Name,Value], 125 argv_options(T0, R, T). 126argv_options([H|T0], [H|R], T) :- 127 argv_options(T0, R, T). 128 129convert_option(password, String, String) :- !. 130convert_option(_, String, Number) :- 131 number_string(Number, String), 132 !. 133convert_option(_, String, Atom) :- 134 atom_string(Atom, String). 135 136:- multifile 137 prolog:called_by/2. 138 139prologcalled_by(main, [main(_)])
Provide entry point for scripts
This library is intended for supporting PrologScript on Unix using the
#!
magic sequence for scripts using commandline options. The entry point main/0 calls the user-supplied predicate main/1 passing a list of commandline options. Below is `echo' in Prolog (adjust /usr/bin/swipl to where SWI-Prolog is installed)