[m-dev.] diff: allow multiple commands on one line in the browser

Mark Anthony BROWN dougl at cs.mu.OZ.AU
Sun Aug 27 04:44:35 AEDT 2000


Fergus Henderson writes:
> On 26-Aug-2000, Mark Anthony BROWN <dougl at cs.mu.OZ.AU> wrote:
> > 
> > +:- pragma c_code(util__trace_get_command(Prompt::in, Line::out, MdbIn::in,
> > +			MdbOut::in, State0::di, State::uo),
> > +	[will_not_call_mercury],
> > +	"
> > +		char		*line;
> > +		char		*mercury_string;
> > +		MercuryFile	*mdb_in = (MercuryFile *) MdbIn;
> > +		MercuryFile	*mdb_out = (MercuryFile *) MdbOut;
> > +
> > +		if (MR_address_of_trace_getline != NULL) {
> > +			line = (*MR_address_of_trace_get_command)(
> > +					(char *) Prompt,
> > +					MR_file(*mdb_in), MR_file(*mdb_out));
> > +		} else {
> > +			MR_tracing_not_enabled();
> > +			/* not reached */
> > +		}
> > +
> > +		MR_make_aligned_string_copy(mercury_string, line);
> > +		free(line);
> 
> You should use MR_free() rather than free(),
> since the memory was allocated with MR_malloc().
> (It doesn't make any difference now, since MR_free() just calls free(),
> but we might later change MR_free(), e.g. to use a debugging allocator.)
> 
> > +		Line = (String) mercury_string;
> 
> That cast should not be necessary.
> 

Both of those problems were copied from another piece of code;
I've fixed both pieces of code.

> > --- trace/mercury_trace_internal.c	2000/08/25 09:53:36	1.75
> ...
> > +char *
> > +MR_trace_get_command(const char *prompt, FILE *mdb_in, FILE *mdb_out)
> > +{
> 
> You should include some documentation about what this function does.
> The function's documentation should in particular specify that the
> memory returned is allocated via MR_malloc() and it is the caller's
> responsibility to deallocate it using MR_free().
> 

Ok.  I've put a comment similar to that of MR_trace_getline.

Cheers,
Mark.

Estimated hours taken: 0.1

Minor fixups to my last change.

browser/util.m:
	- Avoid unnecessary casts.
	- Use 'MR_free' rather than 'free'.

trace/mercury_trace_internal.c:
	Better comments for MR_trace_get_command.

Index: browser/util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/util.m,v
retrieving revision 1.9
diff -u -r1.9 util.m
--- browser/util.m	2000/08/26 04:33:54	1.9
+++ browser/util.m	2000/08/26 17:25:42
@@ -100,7 +100,6 @@
 	[will_not_call_mercury],
 	"
 		char		*line;
-		char		*mercury_string;
 		MercuryFile	*mdb_in = (MercuryFile *) MdbIn;
 		MercuryFile	*mdb_out = (MercuryFile *) MdbOut;
 
@@ -115,9 +114,8 @@
 		if (line == NULL) {
 			SUCCESS_INDICATOR = FALSE;
 		} else {
-			MR_make_aligned_string_copy(mercury_string, line);
-			free(line);
-			Line = (String) mercury_string;
+			MR_make_aligned_string_copy(Line, line);
+			MR_free(line);
 			SUCCESS_INDICATOR = TRUE;
 		}
 	"
@@ -133,7 +131,6 @@
 	[will_not_call_mercury],
 	"
 		char		*line;
-		char		*mercury_string;
 		MercuryFile	*mdb_in = (MercuryFile *) MdbIn;
 		MercuryFile	*mdb_out = (MercuryFile *) MdbOut;
 
@@ -146,9 +143,8 @@
 			/* not reached */
 		}
 
-		MR_make_aligned_string_copy(mercury_string, line);
-		free(line);
-		Line = (String) mercury_string;
+		MR_make_aligned_string_copy(Line, line);
+		MR_free(line);
 
 		State = State0;
 	"
Index: trace/mercury_trace_internal.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_internal.c,v
retrieving revision 1.76
diff -u -r1.76 mercury_trace_internal.c
--- trace/mercury_trace_internal.c	2000/08/26 04:34:33	1.76
+++ trace/mercury_trace_internal.c	2000/08/26 17:26:30
@@ -2289,6 +2289,14 @@
 	MR_trace_internal_interacting = FALSE;
 }
 
+/*
+** Call MR_trace_getline to get the next line of input, then do some
+** further processing.  If the input has reached EOF, return the command
+** "quit".  If the line contains multiple commands then split it and
+** only return the first one.  The command is returned in a MR_malloc'd
+** buffer.
+*/
+
 char *
 MR_trace_get_command(const char *prompt, FILE *mdb_in, FILE *mdb_out)
 {
@@ -2301,7 +2309,7 @@
 		/*
 		** We got an EOF.
 		** We arrange things so we don't have to treat this case
-		** specially in the command interpreter below.
+		** specially in the command interpreter.
 		*/
 		line = MR_copy_string("quit");
 	}
@@ -2309,7 +2317,7 @@
 	if ((semicolon = strchr(line, ';')) != NULL) {
 		/*
 		** The line contains at least two commands.
-		** Execute only the first command now; put the others
+		** Return only the first command now; put the others
 		** back in the input to be processed later.
 		*/
 		*semicolon = '\0';
--------------------------------------------------------------------------
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