[m-dev.] for review: add readline support to mdb [round 2]

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Mar 26 04:01:18 AEDT 1999


Actually the hooks for installing readline if it was present turned out 
to be a bit more complicated than I had anticipated.  And it seemed cleaner
to commit the two parts of the change (the readline support and the hooks)
separately.  So for now at least I will not include those hooks; they can
be committed as a separate change later if need be.

Dropping the hooks removed a significant portion of my old changes.
I also made a few significant new changes (e.g. the configure
script now checks for the headers not just for the libs).
And I have added documentation in the NEWS file and the INSTALL files.
(I won't mention it on the WWW page until it has had more testing.)
Anyway given all these changes, probably a relative diff is not very useful,
so I will just post a new full diff.  I suppose I can produce a relative
diff if anyone really wants one...

So, anyone care to review this?

--------------------- 

Estimated hours taken: 16

Add GNU readline support to the debugger.

Because GNU readline is licensed under the GPL, not the LGPL,
I was careful to ensure that (1) the readline library is
only linked into your application if it is compiled with
debugging enabled and (2) even in the latter case, the readline
support is disable-able (if you use the source distribution).

Mmakefile:
bindist/Mmakefile:
	Add dependency of configure on new file aclocal.m4.

aclocal.m4:
	Define a macro MERCURY_CHECK_READLINE to check for the readline
	headers, the readline library, and for libraries needed by readline,
	in particular -l{termcap,curses,ncurses}.
	If they're not found, define MR_NO_USE_READLINE.
	Otherwise, define various variables for using & installing readline.

bindist/bindist.configure.in:
configure.in:
	Add call to MERCURY_CHECK_READLINE to check for readline.

scripts/ml.in:
	If tracing is enabled, then (by default) link in readline,
	and hence also termcap/curses/ncurses.
	Add a new option `--no-readline' to disable this.

trace/mercury_trace_internal.c:
	Delete the FILE * parameter from the MR_getline() function,
	because there are actually two files involved (MR_mdb_in and
	MR_mdb_out), and they are always the same for all calls to this
	function.
	Also change this function so that it calls MR_trace_readline() rather
	than printing the prompt and then calling MR_trace_getline_raw().
	Delete the MR_trace_getline_raw() function -- this has been renamed
	as MR_trace_readline_raw() and moved to mercury_trace_readline.c.

trace/mercury_trace_readline.h:
trace/mercury_trace_readline.c:
	New module containing a new function MR_trace_readline() that prints
	a prompt and reads a line in by calling the readline library.
	I put all this new code inside `#ifndef MR_NO_USE_READLINE' so that
	the use of readline can be disabled.  If that symbol is defined,
	it reverts to the old implementation using MR_trace_readline_raw()
	(formerly called MR_trace_getline_raw()).

runtime/mercury_conf.h.in:
	Define the new configuration parameter `MR_NO_USE_READLINE'.

trace/Mmakefile:
	Add the new files mercury_trace_readline.[ch].
	Ensure that this new module is compiled with warnings disabled,
	to avoid spurious warnings resulting from the readline header files.
	Also simplify the code by deleting unnecessary uses of $(EXTRA_*) --
	that's handled by scripts/Mmake.vars now.

NEWS:
	Mention the readline support.

.INSTALL.in:
	Mention that it is helpful to have GNU Readline installed.

bindist/bindist.INSTALL:
	Mention that you may need to have GNU Readline installed.

Index: .INSTALL.in
===================================================================
RCS file: /home/staff/zs/imp/mercury/.INSTALL.in,v
retrieving revision 1.4
diff -u -r1.4 .INSTALL.in
--- .INSTALL.in	1999/03/10 18:35:44	1.4
+++ .INSTALL.in	1999/03/25 15:55:11
@@ -5,7 +5,14 @@
 # You need GNU C (2.7.2 or later -- 2.6.3 might work too, but it is known
 # to have problems compiling Mercury on some architectures, e.g. SPARC)
 # and GNU Make (3.69 or later).  Make sure that they are somewhere in
-# your PATH.
+# your PATH.  It's also helpful if you have GNU Readline installed
+# in one of the standard locations (normally /usr/local or /usr),
+# but this is not essential.
+#
+# The source distributions for GNU C, GNU Make, and GNU Readline are
+# available from <ftp://ftp.gnu.org/gnu/>.  Binaries for these are
+# included in almost all Linux distributions and also in the Cygwin
+# distribution for Windows (see README.MS-Windows).
 #
 # Step 0.  Extract the files from the gzipped tar archive.
 #
Index: Mmakefile
===================================================================
RCS file: /home/staff/zs/imp/mercury/Mmakefile,v
retrieving revision 1.36
diff -u -r1.36 Mmakefile
--- Mmakefile	1999/03/18 22:00:49	1.36
+++ Mmakefile	1999/03/25 14:57:59
@@ -174,7 +174,7 @@
 
 #-----------------------------------------------------------------------------#
 
-configure: configure.in
+configure: configure.in aclocal.m4
 	autoconf
 
 config.status: configure VERSION
Index: NEWS
===================================================================
RCS file: /home/staff/zs/imp/mercury/NEWS,v
retrieving revision 1.140
diff -u -r1.140 NEWS
--- NEWS	1999/03/24 05:32:17	1.140
+++ NEWS	1999/03/25 15:57:52
@@ -92,10 +92,22 @@
 Changes to the Mercury implementation:
 **************************************
 
-* The debugger now includes support for interactive queries.
+* The Mercury debugger (mdb) now includes support for interactive queries.
 
   See the "Interactive query commands" subsection of the "Debugger commands"
   section of the "Debugging" chapter of the Mercury User's Guide for details.
+
+* The Mercury debugger (mdb) now optionally supports command-line editing
+  and command-line history.
+
+  This support uses the GNU Readline library.  For the source distribution,
+  the Mercury configure script will detect whether readline has been
+  installed and will only enable the command-line editing and history
+  support if readline has been installed.  For the binary distribution,
+  if the binary distribution was built with readline, then you will
+  need to install GNU readline in order to use the debugger.
+
+  For information on where to obtain GNU Readline, see the INSTALL file.
 
 * We've removed the support for using a Prolog debugger on Mercury programs.
 
Index: aclocal.m4
===================================================================
RCS file: aclocal.m4
diff -N aclocal.m4
--- /dev/null	Wed May 28 10:49:58 1997
+++ aclocal.m4	Fri Mar 26 03:09:14 1999
@@ -0,0 +1,56 @@
+dnl----------------------------------------------------------------------------
+dnl
+dnl This file contains Mercury specific tests
+dnl
+dnl We ought to move most of the code in configure.in and
+dnl bindist/bindist.configure.in into this file...
+dnl
+dnl----------------------------------------------------------------------------
+#
+# Check for readline and related header files and libraries
+#
+AC_DEFUN(MERCURY_CHECK_READLINE,
+[
+
+# check for the readline header files
+AC_CHECK_HEADER(readline/readline.h, HAVE_READLINE_READLINE_H=1)
+if test "$HAVE_READLINE_READLINE_H" = 1; then
+	AC_DEFINE(HAVE_READLINE_READLINE)
+fi
+AC_CHECK_HEADER(readline/history.h, HAVE_READLINE_HISTORY_H=1)
+if test "$HAVE_READLINE_HISTORY_H" = 1; then
+	AC_DEFINE(HAVE_READLINE_HISTORY)
+fi
+
+# check for the readline library
+AC_CHECK_LIB(readline, readline, mercury_cv_have_readline=yes,
+	mercury_cv_have_readline=no)
+
+# check for the libraries that readline depends on
+if test $mercury_cv_have_readline = yes; then
+	MERCURY_MSG('looking for termcap or curses (needed by readline)...')
+	AC_CHECK_LIB(termcap, tgetent, mercury_cv_termcap_lib=-ltermcap,
+	 [AC_CHECK_LIB(curses,  tgetent, mercury_cv_termcap_lib=-lcurses,
+	  [AC_CHECK_LIB(ncurses, tgetent, mercury_cv_termcap_lib=-lncurses,
+	   mercury_cv_termcap_lib=none)])])
+fi
+
+# Now figure out whether we can use readline, and define variables according.
+# Note that on most systems, we don't actually need the header files in
+# order to use readline. (Ain't C grand? ;-).
+
+if test $mercury_cv_have_readline = no ||
+	test $mercury_cv_termcap_lib = none
+then
+	TERMCAP_LIBRARY=""
+	READLINE_LIBRARIES=""
+	AC_DEFINE(MR_NO_USE_READLINE)
+else
+	TERMCAP_LIBRARY="$mercury_cv_termcap_lib"
+	READLINE_LIBRARIES="-lreadline $TERMCAP_LIBRARY"
+fi
+AC_SUBST(TERMCAP_LIBRARY)
+AC_SUBST(READLINE_LIBRARIES)
+
+])
+dnl----------------------------------------------------------------------------
Index: configure.in
===================================================================
RCS file: /home/staff/zs/imp/mercury/configure.in,v
retrieving revision 1.153
diff -u -r1.153 configure.in
--- configure.in	1999/03/18 00:14:00	1.153
+++ configure.in	1999/03/25 16:09:21
@@ -245,7 +245,7 @@
 if test "$HAVE_DLFCN_H" = 1; then
 	AC_DEFINE(HAVE_DLFCN_H)
 fi
-AC_CHECK_LIB(dl,dlopen,DL_LIBRARY="-ldl",DL_LIBRARY="")
+AC_CHECK_LIB(dl, dlopen, DL_LIBRARY="-ldl", DL_LIBRARY="")
 AC_SUBST(DL_LIBRARY)
 AC_HAVE_FUNCS(dlopen dlclose dlsym dlerror)
 #-----------------------------------------------------------------------------#
@@ -1806,13 +1806,12 @@
 AC_SUBST_FILE(PARSE_GRADE_OPTIONS)
 AC_SUBST_FILE(FINAL_GRADE_OPTIONS)
 
-
 #-----------------------------------------------------------------------------#
 
 #
 # check whether we need -lsocket
 #
-AC_CHECK_LIB(socket,socket,SOCKET_LIBRARY=-lsocket,SOCKET_LIBRARY="")
+AC_CHECK_LIB(socket, socket, SOCKET_LIBRARY=-lsocket, SOCKET_LIBRARY="")
 AC_SUBST(SOCKET_LIBRARY)
 
 #
@@ -1992,7 +1991,8 @@
 # restore the previous value of LIBS
 #
 LIBS="$save_LIBS"
-
+#-----------------------------------------------------------------------------#
+MERCURY_CHECK_READLINE
 #-----------------------------------------------------------------------------#
 AC_OUTPUT(Mmake.common scripts/Mmake.vars scripts/mmc scripts/mprof
 scripts/mercury_update_interface scripts/mgnuc scripts/ml
Index: bindist/Mmakefile
===================================================================
RCS file: /home/staff/zs/imp/mercury/bindist/Mmakefile,v
retrieving revision 1.18
diff -u -r1.18 Mmakefile
--- Mmakefile	1999/03/16 06:11:35	1.18
+++ Mmakefile	1999/03/21 20:00:08
@@ -109,8 +109,8 @@
 	gzip ../$(MERCURY_VERSION).tar
 	rm -rf $(MERCURY_VERSION)
 
-bindist.configure: bindist.configure.in
-	autoconf bindist.configure.in > bindist.configure
+bindist.configure: bindist.configure.in ../aclocal.m4
+	autoconf -l.. bindist.configure.in > bindist.configure
 
 bindist.build_vars: bindist.build_vars.in
 	CONFIG_FILES=bindist.build_vars CONFIG_HEADERS= \
Index: bindist/bindist.INSTALL
===================================================================
RCS file: /home/staff/zs/imp/mercury/bindist/bindist.INSTALL,v
retrieving revision 1.7
diff -u -r1.7 bindist.INSTALL
--- bindist.INSTALL	1998/12/21 11:33:54	1.7
+++ bindist.INSTALL	1999/03/25 16:01:45
@@ -5,6 +5,7 @@
 #
 # You need GNU C (2.6.3 or later) and GNU Make (3.69 or later) in order
 # to use Mercury, though you don't need them to install this distribution.
+# To use the Mercury debugger, you may also need GNU Readline.
 #
 # Step 0.  Extract the files from the gzipped tar archive.
 #
Index: bindist/bindist.configure.in
===================================================================
RCS file: /home/staff/zs/imp/mercury/bindist/bindist.configure.in,v
retrieving revision 1.23
diff -u -r1.23 bindist.configure.in
--- bindist.configure.in	1999/03/15 08:47:39	1.23
+++ bindist.configure.in	1999/03/25 16:08:50
@@ -225,6 +225,8 @@
 AC_SUBST(SOCKET_LIBRARY)
 
 #-----------------------------------------------------------------------------#
+MERCURY_CHECK_READLINE
+#-----------------------------------------------------------------------------#
 AC_OUTPUT(Makefile scripts/mmc scripts/mprof
 scripts/mercury_update_interface scripts/mgnuc scripts/ml
 scripts/mmake scripts/c2init scripts/sicstus_conv
Index: runtime/mercury_conf.h.in
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_conf.h.in,v
retrieving revision 1.11
diff -u -r1.11 mercury_conf.h.in
--- mercury_conf.h.in	1999/03/11 19:18:08	1.11
+++ mercury_conf.h.in	1999/03/21 19:08:40
@@ -260,6 +260,15 @@
 */
 #undef  MR_USE_EXTERNAL_DEBUGGER
 
+/*
+** MR_NO_USE_READLINE
+**	Set this if you want to prevent the debugger from using the GNU
+**	readline library for the command-line prompt.
+**	The autoconfiguration script sets this if it can't find a termcap
+**	library.
+*/
+#undef  MR_NO_USE_READLINE
+
 /*---------------------------------------------------------------------------*/
 
 #include "mercury_conf_param.h"
Index: scripts/ml.in
===================================================================
RCS file: /home/staff/zs/imp/mercury/scripts/ml.in,v
retrieving revision 1.55
diff -u -r1.55 ml.in
--- ml.in	1999/03/25 14:45:39	1.55
+++ ml.in	1999/03/25 14:46:11
@@ -65,6 +65,8 @@
 		then you don't need to explicitly specify \`--trace'.
 		Note that \`--trace' is incompatible with \`--static'
 		on some platforms (e.g. sparc-sun-solaris2.6).
+	-r-, --no-readline
+		Don't link in the GPL'd GNU Readline Library.
 	-g, --c-debug, --no-strip
 		Do not strip C debugging information.
 
@@ -111,6 +113,9 @@
 # Likewise for -ldl (libdl.so), which is often needed for dlopen() etc.
 DL_LIBRARY="@DL_LIBRARY@"
 
+# Likewise for -lreadline -l{termcap,curses,ncurses}
+READLINE_LIBRARIES="@READLINE_LIBRARIES@"
+
 # If you change these, you will also need to change Mmake.common.in,
 # scripts/c2init.in, tools/bootcheck, tools/binary, tools/binary_step
 # and tools/linear.
@@ -121,6 +126,7 @@
 
 verbose=false
 trace=false
+readline=true
 case $FULLARCH in
 	*-win95|*-winnt|*-win32|*-cygwin32|*-cygwin)
 		# `gcc -s' is broken in gnu-win32
@@ -161,6 +167,12 @@
 	-t-|--no-trace)
 		trace=false
 		;;
+	-r|--readline)
+		readline=true
+		;;
+	-r-|--no-readline)
+		readline=false
+		;;
 	-g-|--no-c-debug|--strip)
 		strip=true
 		;;
@@ -365,13 +377,19 @@
 		;;
 esac
 
+case $readline in
+	true)	;;
+	false)	READLINE_LIBRARIES=
+		;;
+esac
+
 case $trace in
 	true)	TRACE_LIBS="-l$TRACE_LIB_NAME -l$BROWSER_LIB_NAME \
-			$SOCKET_LIBRARY $DL_LIBRARY"
+			$SOCKET_LIBRARY $DL_LIBRARY $READLINE_LIBRARIES"
 		TRACE_STATIC_LIBS="\
 			$LIBDIR/$GRADE/$FULLARCH/lib$TRACE_LIB_NAME.a \
 			$LIBDIR/$GRADE/$FULLARCH/lib$BROWSER_LIB_NAME.a \
-			$SOCKET_LIBRARY $DL_LIBRARY"
+			$SOCKET_LIBRARY $DL_LIBRARY $READLINE_LIBRARIES"
 		;;
 	false)	TRACE_LIBS=
 		TRACE_STATIC_LIBS=
Index: trace/Mmakefile
===================================================================
RCS file: /home/staff/zs/imp/mercury/trace/Mmakefile,v
retrieving revision 1.6
diff -u -r1.6 Mmakefile
--- Mmakefile	1999/01/11 03:52:30	1.6
+++ Mmakefile	1999/03/25 16:22:01
@@ -16,13 +16,20 @@
 
 CFLAGS		= -I$(MERCURY_DIR)/browser -I$(MERCURY_DIR)/library \
 		  -I$(MERCURY_DIR)/runtime -I$(MERCURY_DIR)/boehm_gc \
-		  -g $(DLL_CFLAGS) $(EXTRA_CFLAGS)
+		  -g $(DLL_CFLAGS)
 MGNUC		= MERCURY_C_INCL_DIR=. $(SCRIPTS_DIR)/mgnuc
-MGNUCFLAGS	= --no-ansi $(EXTRA_MGNUCFLAGS) $(CFLAGS)
+MGNUCFLAGS	= --no-ansi
 MOD2C		= $(SCRIPTS_DIR)/mod2c
 
 #-----------------------------------------------------------------------------#
 
+# mercury_readline.c #includes the GNU readline headers, which
+# lack prototypes and `const', so we need to disable warnings
+# when compiling that file.
+MGNUCFLAGS-mercury_trace_readline = --no-check
+
+#-----------------------------------------------------------------------------#
+
 # keep this list in alphabetical order, please
 HDRS		=	\
 			mercury_trace.h			\
@@ -32,6 +39,7 @@
 			mercury_trace_external.h 	\
 			mercury_trace_help.h		\
 			mercury_trace_internal.h	\
+			mercury_trace_readline.h	\
 			mercury_trace_spy.h		\
 			mercury_trace_tables.h		\
 			mercury_trace_util.h
@@ -45,6 +53,7 @@
 			mercury_trace_external.c 	\
 			mercury_trace_help.c		\
 			mercury_trace_internal.c	\
+			mercury_trace_readline.c	\
 			mercury_trace_spy.c		\
 			mercury_trace_tables.c
 
Index: trace/mercury_trace_internal.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/trace/mercury_trace_internal.c,v
retrieving revision 1.33
diff -u -r1.33 mercury_trace_internal.c
--- mercury_trace_internal.c	1999/03/18 08:19:17	1.33
+++ mercury_trace_internal.c	1999/03/21 17:47:29
@@ -20,10 +20,13 @@
 #include "mercury_trace_spy.h"
 #include "mercury_trace_tables.h"
 #include "mercury_trace_util.h"
+#include "mercury_trace_readline.h"
 #include "mercury_layout_util.h"
 #include "mercury_array_macros.h"
 #include "mercury_getopt.h"
+
 #include "browse.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -36,9 +39,6 @@
 /* The initial size of arrays of words. */
 #define	MR_INIT_WORD_COUNT	20
 
-/* The initial size of arrays of characters. */
-#define	MR_INIT_BUF_LEN		80
-
 /* The initial number of lines in documentation entries. */
 #define	MR_INIT_DOC_CHARS	800
 
@@ -202,9 +202,8 @@
 			int *word_max, int *word_count);
 static	bool	MR_trace_source(const char *filename);
 static	void	MR_trace_source_from_open_file(FILE *fp);
-static	char	*MR_trace_getline(const char *prompt, FILE *fp);
+static	char	*MR_trace_getline(const char *prompt);
 static	char	*MR_trace_getline_queue(void);
-static	char	*MR_trace_getline_raw(FILE *fp);
 static	void	MR_insert_line_at_head(const char *line);
 static	void	MR_insert_line_at_tail(const char *line);
 
@@ -262,7 +261,7 @@
 	jumpaddr = NULL;
 
 	do {
-		line = MR_trace_getline("mdb> ", MR_mdb_in);
+		line = MR_trace_getline("mdb> ");
 		res = MR_trace_debug_cmd(line, cmd, event_info, &event_details,
 				&ancestor_level, &jumpaddr);
 	} while (res == KEEP_INTERACTING);
@@ -1302,9 +1301,8 @@
 			if (! confirmed) {
 				char	*line2;
 
-				line2 = MR_trace_getline(
-					"mdb: are you sure you want to quit? ",
-					MR_mdb_in);
+				line2 = MR_trace_getline("mdb: "
+					"are you sure you want to quit? ");
 				if (line2 == NULL) {
 					/* This means the user input EOF. */
 					confirmed = TRUE;
@@ -1929,7 +1927,7 @@
 	int	i;
 
 	next_char_slot = 0;
-	while ((text = MR_trace_getline("cat> ", MR_mdb_in)) != NULL) {
+	while ((text = MR_trace_getline("cat> ")) != NULL) {
 		if (streq(text, "end")) {
 			free(text);
 			break;
@@ -2180,7 +2178,7 @@
 {
 	char	*line;
 
-	while ((line = MR_trace_getline_raw(fp)) != NULL) {
+	while ((line = MR_trace_readline_raw(fp)) != NULL) {
 		MR_insert_line_at_tail(line);
 	}
 
@@ -2189,14 +2187,15 @@
 
 /*
 ** If there any lines waiting in the queue, return the first of these.
-** If not, print the prompt, read a line from the given file, and return it
-** in a malloc'd buffer holding the line (without the final newline).
+** If not, print the prompt to MR_mdb_out, read a line from MR_mdb_in,
+** and return it in a malloc'd buffer holding the line (without the final
+** newline).
 ** If EOF occurs on a nonempty line, treat the EOF as a newline; if EOF
 ** occurs on an empty line, return NULL.
 */
 
 static char *
-MR_trace_getline(const char *prompt, FILE *fp)
+MR_trace_getline(const char *prompt)
 {
 	char	*line;
 
@@ -2206,10 +2205,8 @@
 	}
 
 	MR_trace_internal_interacting = TRUE;
-	fprintf(MR_mdb_out, "%s", prompt);
-	fflush(MR_mdb_out);
 
-	return MR_trace_getline_raw(fp);
+	return MR_trace_readline(prompt, MR_mdb_in, MR_mdb_out);
 }
 
 /*
@@ -2237,40 +2234,6 @@
 	}
 }
 
-/*
-**	Read a line from a file, and return a pointer to a malloc'd buffer
-**	holding the line (without the final newline). If EOF occurs on a
-**	nonempty line, treat the EOF as a newline; if EOF occurs on an empty
-**	line, return NULL.
-*/
-
-static char *
-MR_trace_getline_raw(FILE *fp)
-{
-	char	*contents;
-	int	content_max;
-	int	c;
-	int	i;
-
-	contents = NULL;
-	content_max = 0;
-
-	i = 0;
-	while ((c = getc(fp)) != EOF && c != '\n') {
-		MR_ensure_big_enough(i, content, char, MR_INIT_BUF_LEN);
-		contents[i++] = c;
-	}
-
-	if (c == '\n' || i > 0) {
-		MR_ensure_big_enough(i, content, char, MR_INIT_BUF_LEN);
-		contents[i] = '\0';
-		return contents;
-	} else {
-		free(contents);
-		return NULL;
-	}
-}
-
 static void
 MR_insert_line_at_head(const char *contents)
 {
@@ -2320,7 +2283,7 @@
 	/* We try to leave one line for the prompt itself. */
 	if (MR_scroll_control && MR_scroll_next >= MR_scroll_limit - 1) {
 	try_again:
-		buf = MR_trace_getline("--more-- ", MR_mdb_in);
+		buf = MR_trace_getline("--more-- ");
 		if (buf != NULL) {
 			for (i = 0; buf[i] != '\0' && MR_isspace(buf[i]); i++)
 				;
Index: trace/mercury_trace_readline.h
===================================================================
RCS file: mercury_trace_readline.h
diff -N mercury_trace_readline.h
--- /dev/null	Wed May 28 10:49:58 1997
+++ mercury_trace_readline.h	Mon Mar 22 04:36:02 1999
@@ -0,0 +1,30 @@
+/*
+** Copyright (C) 1998-1999 The University of Melbourne.
+** This file may only be copied under the terms of the GNU Library General
+** Public License - see the file COPYING.LIB in the Mercury distribution.
+*/
+
+/*
+** mercury_trace_readline.h: simple interface functions to read a line.
+**
+** Main authors: fjh, zs.
+*/
+
+#include <stdio.h>	/* for FILE */
+
+/*
+** Print the prompt to the `out' file, read a line from the `in' file,
+** possibly using GNU readline for command-line editing, and return it
+** in a malloc'd buffer holding the line (without the final newline).
+** If EOF occurs on a nonempty line, treat the EOF as a newline; if EOF
+** occurs on an empty line, return NULL.
+*/
+char *	MR_trace_readline(const char *prompt, FILE *in, FILE *out);
+
+/*
+** Read a line from a file, and return a pointer to a malloc'd buffer
+** holding the line (without the final newline). If EOF occurs on a
+** nonempty line, treat the EOF as a newline; if EOF occurs on an empty
+** line, return NULL.  Don't use GNU readline.
+*/
+char *	MR_trace_readline_raw(FILE *in);
Index: trace/mercury_trace_readline.c
===================================================================
RCS file: mercury_trace_readline.c
diff -N mercury_trace_readline.c
--- /dev/null	Wed May 28 10:49:58 1997
+++ mercury_trace_readline.c	Fri Mar 26 03:20:18 1999
@@ -0,0 +1,104 @@
+/*
+** Copyright (C) 1998-1999 The University of Melbourne.
+** This file may only be copied under the terms of the GNU Library General
+** Public License - see the file COPYING.LIB in the Mercury distribution.
+*/
+
+/*
+** A simple interface to read a line, normally done using GNU readline.
+**
+** This module is compiled with warnings disabled (mgnuc --no-check),
+** since the GNU readline headers don't use prototypes, const, etc.
+**
+** Main authors: fjh, zs.
+*/
+
+#include "mercury_imp.h"
+#include "mercury_trace_readline.h"
+#include "mercury_array_macros.h"
+#include "mercury_memory.h"
+#include "mercury_std.h"
+
+#ifndef MR_NO_USE_READLINE
+  #ifdef HAVE_READLINE_READLINE
+    #include "readline/readline.h"
+  #endif
+  #ifdef HAVE_READLINE_HISTORY
+    #include "readline/history.h"
+  #endif
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/* The initial size of the array of characters used to hold the line. */
+#define	MR_INIT_BUF_LEN		80
+
+/*
+** Print the prompt to the `out' file, read a line from the `in' file,
+** and return it in a malloc'd buffer holding the line (without the final
+** newline).
+** If EOF occurs on a nonempty line, treat the EOF as a newline; if EOF
+** occurs on an empty line, return NULL.
+*/
+char *
+MR_trace_readline(const char *prompt, FILE *in, FILE *out)
+{
+	char	*line;
+
+#ifdef MR_NO_USE_READLINE
+
+	fprintf(out, "%s", prompt);
+	fflush(out);
+	line = MR_trace_readline_raw(in);
+
+#else /* use readline */
+
+	size_t	len;
+
+	rl_instream = in;
+	rl_outstream = out;
+
+	line = readline((char *) prompt);
+
+	if (line != NULL && line[0] != '\0') {
+		add_history(line);
+	}
+
+#endif
+
+	return line;
+}
+
+/*
+** Read a line from a file, and return a pointer to a malloc'd buffer
+** holding the line (without the final newline). If EOF occurs on a
+** nonempty line, treat the EOF as a newline; if EOF occurs on an empty
+** line, return NULL.
+*/
+char *
+MR_trace_readline_raw(FILE *fp)
+{
+	char	*contents;
+	int	content_max;
+	int	c;
+	int	i;
+
+	contents = NULL;
+	content_max = 0;
+
+	i = 0;
+	while ((c = getc(fp)) != EOF && c != '\n') {
+		MR_ensure_big_enough(i, content, char, MR_INIT_BUF_LEN);
+		contents[i++] = c;
+	}
+
+	if (c == '\n' || i > 0) {
+		MR_ensure_big_enough(i, content, char, MR_INIT_BUF_LEN);
+		contents[i] = '\0';
+		return contents;
+	} else {
+		free(contents);
+		return NULL;
+	}
+}

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.



More information about the developers mailing list