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-2016, 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 ]). 40 41/** <module> Provide entry point for scripts 42 43This library is intended for supporting PrologScript on Unix using the 44=|#!|= magic sequence for scripts using commandline options. The entry 45point main/0 calls the user-supplied predicate main/1 passing a list of 46commandline options. Below is `echo' in Prolog (adjust /usr/bin/swipl to 47where SWI-Prolog is installed) 48 49== 50#!/usr/bin/env swipl 51 52:- initialization main. 53 54main(Argv) :- 55 echo(Argv). 56 57echo([]) :- nl. 58echo([Last]) :- !, 59 write(Last), nl. 60echo([H|T]) :- 61 write(H), write(' '), 62 echo(T). 63== 64 65@see XPCE users should have a look at library(pce_main), which 66 starts the GUI and processes events until all windows have gone. 67*/ 68 69:- module_transparent 70 main/0. 71 72%! main 73% 74% Call main/1 using the passed command-line arguments. 75 76main :- 77 context_module(M), 78 set_signals, 79 current_prolog_flag(argv, Av), 80 run_main(M, Av). 81 82%! run_main(+Module, +Args) 83% 84% Run the main routine, guarding for exceptions and failure of the 85% main/1 routine 86 87run_main(Module, Av) :- 88 ( catch(call(Module:main, Av), E, true) 89 -> ( var(E) 90 -> halt(0) 91 ; print_message(error, E), 92 halt(1) 93 ) 94 ; print_message(error, goal_failed(main(Av))), 95 halt(1) 96 ). 97 98set_signals :- 99 on_signal(int, _, interrupt). 100 101%! interrupt(+Signal) 102% 103% We received an interrupt. This handler is installed using 104% on_signal/3. 105 106interrupt(_Sig) :- 107 halt(1). 108 109%! argv_options(+Argv, -RestArgv, -Options) is det. 110% 111% Generic transformation of long commandline arguments to options. 112% Each --Name=Value is mapped to Name(Value). Each plain name is 113% mapped to Name(true), unless Name starts with =|no-|=, in which 114% case the option is mapped to Name(false). Numeric option values 115% are mapped to Prolog numbers. 116% 117% @see library(optparse) provides a more involved option library, 118% providing both short and long options, help and error handling. 119% This predicate is more for quick-and-dirty scripts. 120 121argv_options([], [], []). 122argv_options([H0|T0], R, [H|T]) :- 123 sub_atom(H0, 0, _, _, --), 124 !, 125 ( sub_atom(H0, B, _, A, =) 126 -> B2 is B-2, 127 sub_atom(H0, 2, B2, _, Name), 128 sub_string(H0, _, A, 0, Value0), 129 convert_option(Name, Value0, Value) 130 ; sub_atom(H0, 2, _, 0, Name0), 131 ( sub_atom(Name0, 0, _, _, 'no-') 132 -> sub_atom(Name0, 3, _, 0, Name), 133 Value = false 134 ; Name = Name0, 135 Value = true 136 ) 137 ), 138 H =.. [Name,Value], 139 argv_options(T0, R, T). 140argv_options([H|T0], [H|R], T) :- 141 argv_options(T0, R, T). 142 143convert_option(password, String, String) :- !. 144convert_option(_, String, Number) :- 145 number_string(Number, String), 146 !. 147convert_option(_, String, Atom) :- 148 atom_string(Atom, String). 149 150:- multifile 151 prolog:called_by/2. 152 153prologcalled_by(main, [main(_)]).