[m-dev.] for review: use readline in browser/

Mark Anthony BROWN dougl at cs.mu.OZ.AU
Thu May 27 20:50:11 AEST 1999


This completes a change I partially implemented earlier.


Estimated hours taken: 5

If calling from the internal debugger, use readline input for the
interactive term browser and interactive queries.

browser/browse.m:
	Remove the io__input_stream argument to browse__browse/7 and
	predicates it calls, since the argument is no longer used.
	Also change some if-then-elses to switches, which will help
	catch errors if a new functor is added to the debugger type.

browser/parse.m:
browser/util.m:
	Return a string from util__trace_getline/4 rather than a
	list of chars, which saves converting from a string to a list
	of chars and then back again.

browser/declarative_oracle.m:
	Call util__trace_getline/4 to do input via readline (if
	available).  Improve error handling.

browser/interactive_query.m:
	Remove the io__input_stream argument to interactive_query__query/6,
	and call util__trace_getline instead.  Also, reorder some
	arguments to ml so that the ml options are before the '-o'.

trace/mercury_trace_browse.c:
	Don't pass MR_mdb_in to ML_BROWSE_browse and ML_query.

Index: browser/browse.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/browse.m,v
retrieving revision 1.9
diff -u -r1.9 browse.m
--- browse.m	1999/05/21 14:38:18	1.9
+++ browse.m	1999/05/27 04:42:16
@@ -31,9 +31,9 @@
 :- mode browse__init_state(out, di, uo) is det.
 
 	% The interactive term browser.
-:- pred browse__browse(T, io__input_stream, io__output_stream,
-			browser_state, browser_state, io__state, io__state).
-:- mode browse__browse(in, in, in, in, out, di, uo) is det.
+:- pred browse__browse(T, io__output_stream, browser_state, browser_state,
+		io__state, io__state).
+:- mode browse__browse(in, in, in, out, di, uo) is det.
 
 :- pred browse__browse_external(T, io__input_stream, io__output_stream,
 			browser_state, browser_state, io__state, io__state).
@@ -58,7 +58,7 @@
 
 :- pragma export(browse__init_state(out, di, uo),
 	"ML_BROWSE_init_state").
-:- pragma export(browse__browse(in, in, in, in, out, di, uo),
+:- pragma export(browse__browse(in, in, in, out, di, uo),
 	"ML_BROWSE_browse").
 :- pragma export(browse__print(in, in, in, di, uo),
 	"ML_BROWSE_print").
@@ -69,8 +69,9 @@
 	"ML_BROWSE_browse_external").
 
 %---------------------------------------------------------------------------%
-% If the term browser is called from the internal debugger, Input/Output are 
-% done via MR_mdb_in/MR_mdb_out. If it is called from the external debugger,
+% If the term browser is called from the internal debugger, input is
+% done via a call to the readline library (if available), and output
+% goes to the stream MR_mdb_out.  If it is called from the external debugger,
 % Input/Output are done via MR_debugger_socket_in/MR_debugger_socket_out. 
 % In the latter case we need to output terms; their type is 
 % term_browser_response.
@@ -193,44 +194,46 @@
 % Interactive display
 %
 
-browse__browse(Object, InputStream, OutputStream, State0, State) -->
-	browse_common(internal, Object, InputStream, OutputStream, 
-		State0, State).
+browse__browse(Object, OutputStream, State0, State) -->
+	browse_common(internal, Object, OutputStream, State0, State).
 
 browse__browse_external(Object, InputStream, OutputStream, State0, State) -->
-	browse_common(external, Object, InputStream, OutputStream, 
-		State0, State).
+	io__set_input_stream(InputStream, OldInputStream),
+	browse_common(external, Object, OutputStream, State0, State),
+	io__set_input_stream(OldInputStream, _).
 
 
-:- pred browse_common(debugger, T, io__input_stream, io__output_stream,
-			browser_state, browser_state, io__state, io__state).
-:- mode browse_common(in, in, in, in, in, out, di, uo) is det.
-browse_common(Debugger, Object, InputStream, OutputStream, State0, State) -->
+:- pred browse_common(debugger, T, io__output_stream, browser_state,
+		browser_state, io__state, io__state).
+:- mode browse_common( in, in, in, in, out, di, uo) is det.
+
+browse_common(Debugger, Object, OutputStream, State0, State) -->
 	{ type_to_univ(Object, Univ) },
 	{ set_term(Univ, State0, State1) },
-	io__set_input_stream(InputStream, OldInputStream),
 	io__set_output_stream(OutputStream, OldOutputStream),
 	% startup_message,
 	browse_main_loop(Debugger, State1, State),
-	io__set_input_stream(OldInputStream, _),
 	io__set_output_stream(OldOutputStream, _).
 
 :- pred browse_main_loop(debugger, browser_state, browser_state, 
 	io__state, io__state).
 :- mode browse_main_loop(in, in, out, di, uo) is det.
 browse_main_loop(Debugger, State0, State) -->
-	{ prompt(Prompt) },
-	( { Debugger = internal } ->
+	(
+		{ Debugger = internal },
+		{ prompt(Prompt) },
 		parse__read_command(Prompt, Command)
 	;
+		{ Debugger = external },
 		parse__read_command_external(Command)
 	),
 	( { Command = quit } ->
 		% write_string_debugger(Debugger, "quitting...\n")
-		( { Debugger = external } ->
+		(
+			{ Debugger = external },
 			send_term_to_socket(browser_quit)
 		;
-			{ true }
+			{ Debugger = internal }
 		),
 		{ State = State0 }
 	;
@@ -1002,30 +1005,24 @@
 
 :- pred write_string_debugger(debugger, string, io__state, io__state).
 :- mode write_string_debugger(in, in, di, uo) is det.
-write_string_debugger(Debugger, String) -->
-	( { Debugger = internal } ->
-		io__write_string(String)
-	;
-		send_term_to_socket(browser_str(String))
-	).
+write_string_debugger(internal, String) -->
+	io__write_string(String).
+write_string_debugger(external, String) -->
+	send_term_to_socket(browser_str(String)).
 
 :- pred nl_debugger(debugger, io__state, io__state).
 :- mode nl_debugger(in, di, uo) is det.
-nl_debugger(Debugger) -->
-	( { Debugger = internal } ->
-		io__nl
-	;
-		send_term_to_socket(browser_nl)
-	).
+nl_debugger(internal) -->
+	io__nl.
+nl_debugger(external) -->
+	send_term_to_socket(browser_nl).
 
 :- pred write_int_debugger(debugger, int, io__state, io__state).
 :- mode write_int_debugger(in, in, di, uo) is det.
-write_int_debugger(Debugger, Int) -->
-	( { Debugger = internal } ->
-		io__write_int(Int)
-	;
-		send_term_to_socket(browser_int(Int))
-	).
+write_int_debugger(internal, Int) -->
+	io__write_int(Int).
+write_int_debugger(external, Int) -->
+	send_term_to_socket(browser_int(Int)).
 
 
 :- pred print_format_debugger(debugger, portray_format, io__state, io__state).
Index: browser/declarative_oracle.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/declarative_oracle.m,v
retrieving revision 1.2
diff -u -r1.2 declarative_oracle.m
--- declarative_oracle.m	1999/04/30 03:59:50	1.2
+++ declarative_oracle.m	1999/05/20 07:03:27
@@ -41,7 +41,8 @@
 %-----------------------------------------------------------------------------%
 
 :- implementation.
-:- import_module bool, list, char, require, std_util.
+:- import_module bool, list, char, require, std_util, string.
+:- import_module util.
 
 :- type oracle_data == unit.
 
@@ -58,9 +59,7 @@
 
 
 query_oracle(Node, Valid, Oracle0, Oracle) -->
-	query_user(Node),
-	io__flush_output,
-	get_command(Answer),
+	query_user(Node, Answer),
 	(
 		{ Answer = yes },
 		{ Valid = yes },
@@ -96,34 +95,38 @@
 	).
 
 
-:- pred query_user(edt_node, io__state, io__state).
-:- mode query_user(in, di, uo) is det.
+:- pred query_user(edt_node, debugger_command, io__state, io__state).
+:- mode query_user(in, out, di, uo) is det.
 
-query_user(Node) -->
+query_user(Node, Answer) -->
 	write_node(Node),
 	io__nl,
-	io__write_string("Valid? ").
+	get_command("Valid? ", Answer).
 
 
-:- pred get_command(debugger_command, io__state, io__state).
-:- mode get_command(out, di, uo) is det.
+:- pred get_command(string, debugger_command, io__state, io__state).
+:- mode get_command(in, out, di, uo) is det.
 
-get_command(Command) -->
-	io__read_line(Res),
-	{ 
-		Res = ok(Line)
-	->
-		(
+get_command(Prompt, Command) -->
+	util__trace_getline(Prompt, Result),
+	( { Result = ok(String) },
+		{ string__to_char_list(String, Line) },
+		{
 			command_chars(Line, Command0)
 		->
 			Command = Command0
 		;
 			Command = illegal_command
-		)
-	;
+		}
+	; { Result = error(Error) },
+		{ io__error_message(Error, Msg) },
+		io__write_string(Msg),
+		io__nl,
+		get_command(Prompt, Command)
+	; { Result = eof },
 		% XXX this should definitely be handled better.
-		Command = illegal_command
-	}.
+		{ Command = illegal_command }
+	).
 
 
 :- pred command_chars(list(char), debugger_command).
Index: browser/interactive_query.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/interactive_query.m,v
retrieving revision 1.3
diff -u -r1.3 interactive_query.m
--- interactive_query.m	1999/04/22 06:58:48	1.3
+++ interactive_query.m	1999/05/21 06:47:57
@@ -19,8 +19,7 @@
 :- import_module io, list.
 
 :- pred query(query_type::in, imports::in, options::in,
-		io__input_stream::in, io__output_stream::in,
-		state::di, state::uo) is det.
+		io__output_stream::in, state::di, state::uo) is det.
 
 % query_external/7 is the same as query/7 but for the use of the external 
 % debugger.
@@ -33,26 +32,40 @@
 :- type options == string.
 
 :- implementation.
-:- import_module std_util, bool, string, term, varset, term_io.
-:- import_module dl, name_mangle.
+:- import_module std_util, bool, string, term, varset, term_io, parser.
+:- import_module dl, name_mangle, util.
 
-:- pragma export(query(in, in, in, in, in, di, uo), "ML_query").
+:- pragma export(query(in, in, in, in, di, uo), "ML_query").
 
 :- type prog ---> prog(query_type, imports, term, varset).
 
-query(QueryType, Imports, Options, MDB_Stdin, MDB_Stdout) -->
+query(QueryType, Imports, Options, MDB_Stdout) -->
 	% write_import_list(Imports),
-	print(MDB_Stdout, query_prompt(QueryType)),
-	io__flush_output(MDB_Stdout),
-	io__set_input_stream(MDB_Stdin, OldStdin),
-	term_io__read_term(Result),
-	io__set_input_stream(OldStdin, _),
+	util__trace_getline(query_prompt(QueryType), Result),
 	( { Result = eof },
 		io__nl(MDB_Stdout)
-	; { Result = error(Msg, _Line) },
+	; { Result = error(Error) },
+		{ io__error_message(Error, Msg) },
 		io__write_string(MDB_Stdout, Msg), io__nl(MDB_Stdout),
-		query(QueryType, Imports, Options, MDB_Stdin, MDB_Stdout)
-	; { Result = term(VarSet, Term) },
+		query(QueryType, Imports, Options, MDB_Stdout)
+	; { Result = ok(Line) },
+		{ parser__read_term_from_string("", Line, _, ReadTerm) },
+		query_2(QueryType, Imports, Options, MDB_Stdout, ReadTerm)
+	).
+
+
+:- pred query_2(query_type::in, imports::in, options::in,
+		io__output_stream::in, read_term(generic)::in,
+		state::di, state::uo) is det.
+
+query_2(QueryType, Imports, Options, MDB_Stdout, ReadTerm) -->
+	( { ReadTerm = eof },
+		io__nl(MDB_Stdout)
+	; { ReadTerm = error(Msg, _Line) },
+		io__write_string(MDB_Stdout, Msg),
+		io__nl(MDB_Stdout),
+		query(QueryType, Imports, Options, MDB_Stdout)
+	; { ReadTerm = term(VarSet, Term) },
 		% io__write_string("Read term: "),
 		% term_io__write_term(Term, VarSet),
 		% io__write_string("\n"),
@@ -64,18 +77,15 @@
 			print(MDB_Stdout, "Compilation options: "),
 			print(MDB_Stdout, NewOptions),
 			io__nl(MDB_Stdout),
-			query(QueryType, Imports, NewOptions,
-				MDB_Stdin, MDB_Stdout)
+			query(QueryType, Imports, NewOptions, MDB_Stdout)
 		else if { term_to_list(Term, ModuleList) } then
 			{ list__append(Imports, ModuleList, NewImports) },
 			write_import_list(MDB_Stdout, NewImports),
-			query(QueryType, NewImports, Options,
-				MDB_Stdin, MDB_Stdout)
+			query(QueryType, NewImports, Options, MDB_Stdout)
 		else
 			run_query(Options,
 				prog(QueryType, Imports, Term, VarSet)),
-			query(QueryType, Imports, Options,
-				MDB_Stdin, MDB_Stdout)
+			query(QueryType, Imports, Options, MDB_Stdout)
 		)
 	).
 
@@ -411,8 +421,8 @@
 		{ string__append_list([
 			"ml --grade ", grade_option,
 			" --trace",
-			" --make-shared-lib -o libquery.so ", Options,
-			" query.o"], Command2) },
+			" --make-shared-lib ", Options,
+			" -o libquery.so query.o"], Command2) },
 		invoke_system_command(Command2, Succeeded)
 	;
 		{ Succeeded = no }
Index: browser/parse.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/parse.m,v
retrieving revision 1.4
diff -u -r1.4 parse.m
--- parse.m	1999/05/21 14:38:19	1.4
+++ parse.m	1999/05/27 04:46:45
@@ -137,17 +137,21 @@
 
 parse__read_command(Prompt, Comm) -->
 	util__trace_getline(Prompt, Result),
-	( { Result = ok(Cs) } ->
+	( { Result = ok(Line) },
+		{ string__to_char_list(Line, Cs) },
 		{ lexer(Cs, Tokens) },
 		( { parse(Tokens, Comm2) } ->
 			{ Comm = Comm2 }
 		;
 			{ Comm = unknown }
 		)
-	; { Result = eof } ->
+	; { Result = eof },
 		{ Comm = quit }
-	;
-		{ Comm = unknown }
+	; { Result = error(Error) },
+		{ io__error_message(Error, Msg) },
+		io__write_string(Msg),
+		io__nl,
+		parse__read_command(Prompt, Comm)
 	).
 
 parse__read_command_external(Comm) -->
Index: browser/util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/util.m,v
retrieving revision 1.2
diff -u -r1.2 util.m
--- util.m	1999/05/14 02:24:38	1.2
+++ util.m	1999/05/20 05:42:20
@@ -12,7 +12,7 @@
 
 	% Get user input via the same method used by the internal
 	% debugger.
-:- pred util__trace_getline(string, io__result(list(char)), io__state,
+:- pred util__trace_getline(string, io__result(string), io__state,
 		io__state).
 :- mode util__trace_getline(in, out, di, uo) is det.
 
@@ -35,8 +35,7 @@
 	{
 		impure call_trace_getline(Prompt, Line)
 	->
-		string__to_char_list(Line, Chars),
-		Result = ok(Chars)
+		Result = ok(Line)
 	;
 		Result = eof
 	}.
Index: trace/mercury_trace_browse.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_browse.c,v
retrieving revision 1.5
diff -u -r1.5 mercury_trace_browse.c
--- mercury_trace_browse.c	1999/05/21 14:38:21	1.5
+++ mercury_trace_browse.c	1999/05/27 04:48:57
@@ -47,23 +47,20 @@
 void
 MR_trace_browse(Word type_info, Word value)
 {
-	MercuryFile mdb_in, mdb_out;
+	MercuryFile mdb_out;
 
 	MR_trace_browse_ensure_init();
 
-	MR_c_file_to_mercury_file(MR_mdb_in, &mdb_in);
 	MR_c_file_to_mercury_file(MR_mdb_out, &mdb_out);
 
 	MR_TRACE_CALL_MERCURY(
-		ML_BROWSE_browse(type_info, value,
-			(Word) &mdb_in, (Word) &mdb_out,
+		ML_BROWSE_browse(type_info, value, (Word) &mdb_out,
 			MR_trace_browser_state, &MR_trace_browser_state);
 	);
 	MR_trace_browser_state = MR_make_permanent(MR_trace_browser_state,
 				(Word *) MR_trace_browser_state_type);
 }
 
-	
 /*
 ** MR_trace_browse_external() is the same as MR_trace_browse() except it 
 ** uses debugger_socket_in and debugger_socket_out to read program-readable 
@@ -131,10 +128,9 @@
 {
 	ConstString options_on_heap;
 	Word imports_list;
-	MercuryFile mdb_in, mdb_out;
+	MercuryFile mdb_out;
 	int i;
 
-	MR_c_file_to_mercury_file(MR_mdb_in, &mdb_in);
 	MR_c_file_to_mercury_file(MR_mdb_out, &mdb_out);
 
 	if (options == NULL) options = "";
@@ -152,7 +148,7 @@
 
 	MR_TRACE_CALL_MERCURY(
 		ML_query(type, imports_list, (String) options_on_heap,
-			(Word) &mdb_in, (Word) &mdb_out);
+			(Word) &mdb_out);
 	);
 }
 
-- 
Mark Brown  (dougl at cs.mu.oz.au)       )O+   |  For Microsoft to win,
MEngSc student,                             |  the customer must lose
Dept of Computer Science, Melbourne Uni     |          -- Eric S. Raymond
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list