1/*
2 * BEGIN LICENSE BLOCK
3 * Version: CMPL 1.1
4 *
5 * The contents of this file are subject to the Cisco-style Mozilla Public
6 * License Version 1.1 (the "License"); you may not use this file except
7 * in compliance with the License.  You may obtain a copy of the License
8 * at www.eclipse-clp.org/license.
9 *
10 * Software distributed under the License is distributed on an "AS IS"
11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
12 * the License for the specific language governing rights and limitations
13 * under the License.
14 *
15 * The Original Code is  The ECLiPSe Constraint Logic Programming System.
16 * The Initial Developer of the Original Code is  Cisco Systems, Inc.
17 * Portions created by the Initial Developer are
18 * Copyright (C) 1990,2006 Cisco Systems, Inc.  All Rights Reserved.
19 *
20 * Contributor(s): Mireille Ducasse, ECRC.
21 *
22 * END LICENSE BLOCK
23 *
24 *	$Id: opium.pl,v 1.1 2006/09/23 01:54:34 snovello Exp $
25 *
26 */
27
28
29/*
30    !!!!!!!!!!!!!!!!!
31
32    THIS FILE SHOULD ACTUALLY BE SPLIT INTO TWO PIECES
33    - ONE WITH THE COMMON PARTS BETWEEN OPIUM AND OPIUM LIGHT
34    - ONE WITH THE PARTICULARITIES OF EACH
35*/
36
37
38
39/*
40 *   everything which has to be defined in Opium but which is
41 *   not part of an Opium scenario
42 */
43
44%:- import opium_level/1  from sepia_kernel.
45:- import fd_open/3 	 from sepia_kernel.
46
47/* set print_depth */
48:- set_flag(print_depth, 1000).  % for writing lists and terms
49
50
51/*  -----------------------
52 *   init system variables
53 *  -----------------------
54 */
55
56:- make_local_array(opium_loop_running).
57% :- setval(opium_loop_running, 0).
58
59:- make_local_array(tracing).
60% :- setval(tracing, 0).
61
62%% when opium_light is called there is necessarily an opium_loop_running !
63:- setval(opium_loop_running, 1), setval(tracing, 1).
64
65
66/*  ----------------------------------
67 *   initialize toplevel module opium
68 *  ----------------------------------
69 */
70% :- get_opium_file("opium_module", File),
71%    compile(File, opium).
72
73
74
75/*  ---------------------------------------------------------
76 *   definition of MU-Prolog builtins not available in Sepia
77 *  ---------------------------------------------------------
78 */
79
80:- global sprintf/3.
81:- global namevar/2.
82
83% :- skipped sprintf/3.
84% :- skipped namevar/2.
85
86/*
87 *  sprintf/3
88 *  the formatted string is converted to an atom an
89 *  instantiated to the first parameter
90 */
91sprintf(Atom, Format, List) :-
92	open(_, string, Stream),
93	printf(Stream, Format, List),
94	current_stream(String, _, Stream),
95	atom_string(Atom, String),
96	close(Stream).
97
98/*
99 *  namevar/2
100 *  returns the name of a sepia variable as atom
101 */
102namevar(V, VN) :-
103	var(V),
104	open(_, string, Stream),
105	printf(Stream, "%QDw", [V]),
106	current_stream(S, _, Stream),
107	atom_string(VN, S),
108	close(Stream).
109
110
111/*  -----------------------------------------------------
112 *   opium_error_handler/3
113 *   common declaration for all the error handlers which
114 *   are modified in Opium
115 *  -----------------------------------------------------
116 */
117
118opium_error_handler(68, Goal, Module) :-	% undefined procedure called
119	to_be_autoloaded(Goal, Module, NewGoal),
120	!,
121	call(NewGoal, Module).
122opium_error_handler(68, Goal, Module) :-
123	error(default(68), Goal, Module).
124opium_error_handler(134, Goal, Module) :-	% not consecutive clauses
125	is_opium_obj(Goal),
126	!,
127	fail.
128opium_error_handler(134, Goal, Module) :-
129	error(default(134), Goal, Module).
130
131is_opium_obj(opium_scenario/4).
132is_opium_obj(opium_command/9).
133is_opium_obj(opium_parameter/7).
134is_opium_obj(opium_primitive/6).
135is_opium_obj(opium_procedure/5).
136is_opium_obj(opium_type/3).
137is_opium_obj(opium_demo/4).
138
139:- set_error_handler(134, opium_error_handler/3).
140%:- set_error_handler(68,  opium_error_handler/3).  	% Opium's autoload
141
142
143/*  -------------------------------
144 *   re-definition of Sepia events
145 *  -------------------------------
146 */
147
148/*
149 *  banner printed in Opium session
150 */
151% :- dynamic boot_date/1.
152% :- date(D), assert(boot_date(D)).
153%
154% opium_banner(_, Sepiabanner) :-
155% 	write(toplevel_output, "SEPIA + Opium 3.1  "),
156% 	boot_date(D),
157% 	substring(D, 1, 10, D1),
158% 	substring(D, 20, 5, D2),
159% 	concat_strings(D1, D2, DD),
160% 	write(toplevel_output, DD),
161 *
162% 	flush(toplevel_output).
163%
164% :- set_error_handler(164, opium_banner/2).
165
166
167/*
168 *  re-definition of opium_answer/2
169 *  defined in ~/sepia/workdir/sepia/pl/boot_bips.pl
170 */
171opium_answer(_, yes).
172opium_answer(_, no) :-
173	write(toplevel_output, 'no.\n').
174opium_answer(_, no_answer) :-
175	write(toplevel_output, 'no (more) solution.\n').
176opium_answer(_, last_yes).
177opium_answer(_, last_answer) :-
178	write(toplevel_output, '\n').
179opium_answer(_, more_answers) :-
180	write(toplevel_output, '     More? (;) '),
181	flush(toplevel_output),
182	tyi(toplevel_input, C),
183	(C == 0'; ->
184		write(toplevel_output, '\n'),
185		fail
186	;
187		write(toplevel_output, '\n')
188	).
189
190:- set_error_handler(156, opium_answer/2).
191
192
193/*
194 *  to get number of break level in toplevel prompt
195 */
196% opium_toplevel_prompt(_, Module) :-
197% 	get_prompt(toplevel_input, _, Out),
198% 	write(Out, "["),
199% 	write(Out, Module),
200% 	get_flag(break_level, L),
201% 	(L > 0 ->
202% 		write(Out, " "),
203% 		write(Out, L)
204% 	;
205% 		true
206% 	),
207% 	write(Out, "]: "),
208% 	flush(Out).
209%
210% :- set_error_handler(153, opium_toplevel_prompt/2).
211
212
213
214
215/*  --------------------------------------
216 *   init_opium
217 *   to be executed when Opium is started
218 *  --------------------------------------
219 */
220
221%%% REPLACEMENT FOR OPIUM_LIGHT
222
223:- setval(opium_level, 1).
224
225% init_opium(X1, X2) :-
226% 	set_interrupt_handler(18, true/0),	% disable ^Z in Opium
227% 	set_interrupt_handler(2,  true/0),	% disable ^C
228% 	argc(NArgs),
229% 	NInitGoal is NArgs - 6,
230% 	argv(NInitGoal, SInitGoal),
231% 	term_string(InitGoal, SInitGoal),
232% 	NP is NArgs - 5,
233% 	argv(NP, PIDS),
234% 	term_string(PID, PIDS),
235% 	NL is NArgs - 4,
236% 	argv(NL, LevS),
237% 	term_string(Lev, LevS),
238% 	set_opium_level(Lev),		% set level of current session
239% 	NI is NArgs - 3,
240% 	argv(NI, FD_Input),
241% 	term_string(I, FD_Input),
242% 	fd_open(I, read, from_prolog),	% open pipe to read from Prolog
243% 	NO is NArgs - 2,
244% 	argv(NO, FD_Output),
245% 	term_string(O, FD_Output),
246% 	fd_open(O, write, to_prolog),	% open pipe to write to Prolog
247% 	NIAsy is NArgs - 1,
248% 	argv(NIAsy, FD_InputAsy),
249% 	term_string(IAsy, FD_InputAsy),
250% 	fd_open(IAsy, read, from_prolog_asynch),
251% 	setval(prolog_pid, PID),
252% 	set_opium_pid_in_prolog,
253% 	initialize_c_parameters,	% system parameters
254% 	set_opium(X1, X2),		% opium is toplevel module
255% 	call(InitGoal, X2),		% call the init goal
256% 	opium_init_file,		% compile ".opiumrc"
257% 	set_error_handler(150, true/0),	% no opium_init on re-start
258% 	reset_interrupt_handler(2).	% enable ^C
259%
260% set_opium_pid_in_prolog :-
261% 	get_flag(pid, OpiumPid),
262% 	remote_once(setval(opium_pid, OpiumPid), sepia_kernel).
263
264/*
265 *  initialize parameters of type 'c'  (cannot be done without traced session)
266 */
267% initialize_c_parameters :-
268% 	set_default_in_module(limit_depth,  opium_kernel),
269% 	set_default_in_module(limit_call,   opium_kernel),
270% 	set_default_in_module(record_trace, opium_kernel).
271
272/*
273 *  get opium as toplevel module
274 */
275% set_opium(_, opium).
276
277/*
278 *  load ./.opiumrc or ~/.opiumrc if you are an Opium session
279 */
280opium_init_file :-
281	opium_level(0),
282	!.
283opium_init_file :-
284	exists('.opiumrc'),
285	!,
286	compile('.opiumrc', opium).
287opium_init_file :-
288	getenv('HOME', X),
289	append_strings(X, "/.opiumrc", M),
290	atom_string(F, M),
291	exists(F),
292	!,
293	compile(F, opium).
294opium_init_file.
295
296
297% :- set_error_handler(150, init_opium/2).
298
299
300
301
302/*
303 * Put the demo directory inside the library path.
304 * This is duplicated in the opium_slave.pl as for the development
305 *   version of sepium opium_slave.p is not loaded in opium-1 unless a
306 *   second opium is explicitely called.
307 */
308:-	get_opiumdir(OD),
309  	concat_strings(OD, "demo", OpiumDemo),
310	get_flag(library_path, PathList),
311	( member(OpiumDemo, PathList)
312	-> true
313	; append(PathList,[OpiumDemo], NewPathList),
314	  set_flag(library_path, NewPathList)
315	).
316