PATCH: Readline for MinGW

Mark Mitchell mark@codesourcery.com
Wed Jun 8 17:43:00 GMT 2005


With much help from the GDB and readline maintainers, it's *almost*
possible to use FSF GDB on MinGW.  The missing bit is that readline
will not compile.  I posted a previous hack, which was shot down; this
is my long-promised attempt to do a better job.  

With these changes, it is possible to build GDB for MinGW with no
additional patches.  Furthermore, readline works, in that using arrow
keys to access the history, and using key sequences like C-a for
cursor movement have their expected effects.  I have not tried to
comprehensively test all readline key sequences, but I have not found
any problems.

The problems solved by this patch are: 

1. Windows does not have termcap/curses/etc.  So, we provide a stub
   files that contains basic implementations of the relevant
   functions.  That permits the remainder of the code to continue
   using the POSIX interfaces unchanged.

2. The arrow keys use different extended key sequences on Windows than
   on POSIX systems; we introduce readline macros to transform the
   Windows sequences into their POSIX equivalents.  This approach
   again confines the Windows-isms to a single point.

The patch itself isn't quite ready to commit (even if it met with
reviewer approval!) in that:

1. The stub file is called "rlnotty.c", which is horrible.  I would
   like to rename it "rlwin32tty.c".

2. The stub file is entirely lacking in comments.

Assuming the above changes, would this patch be acceptable?  Or are
their problems requiring another trip back to the drawing board?

Thanks,

--
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com

2005-03-28  Mark Mitchell <mark@codesourcery.com>

	* Makefile.in (RLTTYOBJ): New variable.
	(OBJECTS): Use RLTTYOBJ.
	* configure.in: Handle MinGW when cross compiling.  
	Set RLTTYOBJ.
	* configure: Regenerated.
	* input.c (rl_getc): Use getch to read console input on
	Windows.
	* readline.c (bind_arrow_keys_internal): Translate
	Windows keysequences into POSIX key sequences.
	* rldefs.h (NEW_TTY_DRIVER): Do not define for Windows.
	* rlnotty.c: New file.

Index: Makefile.in
===================================================================
RCS file: /cvs/src/src/readline/Makefile.in,v
retrieving revision 1.5
retrieving revision 1.5.56.1
diff -c -5 -p -r1.5 -r1.5.56.1
*** Makefile.in	3 Mar 2003 14:45:42 -0000	1.5
--- Makefile.in	28 Mar 2005 19:34:24 -0000	1.5.56.1
*************** HSOURCES = readline.h rldefs.h chardefs.
*** 108,119 ****
  	   ansi_stdlib.h tcap.h rlstdc.h xmalloc.h rlprivate.h rlshell.h \
  	   rltypedefs.h rlmbutil.h
  
  HISTOBJ = history.o histexpand.o histfile.o histsearch.o shell.o mbutil.o
  TILDEOBJ = tilde.o
  OBJECTS = readline.o vi_mode.o funmap.o keymaps.o parens.o search.o \
! 	  rltty.o complete.o bind.o isearch.o display.o signals.o \
  	  util.o kill.o undo.o macro.o input.o callback.o terminal.o \
  	  text.o nls.o misc.o compat.o xmalloc.o $(HISTOBJ) $(TILDEOBJ) \
  	  $(WCWIDTH_OBJ)
  
  # The texinfo files which document this library.
--- 108,120 ----
  	   ansi_stdlib.h tcap.h rlstdc.h xmalloc.h rlprivate.h rlshell.h \
  	   rltypedefs.h rlmbutil.h
  
  HISTOBJ = history.o histexpand.o histfile.o histsearch.o shell.o mbutil.o
  TILDEOBJ = tilde.o
+ RLTTYOBJ = @RLTTYOBJ@
  OBJECTS = readline.o vi_mode.o funmap.o keymaps.o parens.o search.o \
! 	  $(RLTTYOBJ) complete.o bind.o isearch.o display.o signals.o \
  	  util.o kill.o undo.o macro.o input.o callback.o terminal.o \
  	  text.o nls.o misc.o compat.o xmalloc.o $(HISTOBJ) $(TILDEOBJ) \
  	  $(WCWIDTH_OBJ)
  
  # The texinfo files which document this library.
Index: configure.in
===================================================================
RCS file: /cvs/src/src/readline/configure.in,v
retrieving revision 1.6
retrieving revision 1.6.60.1
diff -c -5 -p -r1.6 -r1.6.60.1
*** configure.in	17 Dec 2002 02:52:32 -0000	1.6
--- configure.in	28 Mar 2005 19:34:27 -0000	1.6.60.1
*************** dnl load up the cross-building cache fil
*** 54,73 ****
  dnl files as necessary
  if test "x$cross_compiling" = "xyes"; then
      case "${host}" in
      *-cygwin*)
  	cross_cache=${srcdir}/cross-build/cygwin.cache
- 	if test -r "${cross_cache}"; then
- 	    echo "loading cross-build cache file ${cross_cache}"
- 	    . ${cross_cache}
- 	fi
  	LOCAL_CFLAGS="$LOCAL_CFLAGS -I${srcdir}/../libtermcap"
- 	unset cross_cache
  	;;
      *)  echo "configure: cross-compiling for a non-cygwin target is not supported" >&2
  	;;
      esac
  fi
   
  if test "x$cross_compiling" = "xyes"; then
    CROSS_COMPILING_FLAG=-DCROSS_COMPILING
  else
--- 54,79 ----
  dnl files as necessary
  if test "x$cross_compiling" = "xyes"; then
      case "${host}" in
      *-cygwin*)
  	cross_cache=${srcdir}/cross-build/cygwin.cache
  	LOCAL_CFLAGS="$LOCAL_CFLAGS -I${srcdir}/../libtermcap"
  	;;
+    *-mingw32*)	
+ 	cross_cache=${srcdir}/cross-build/mingw.cache
+         ;;
      *)  echo "configure: cross-compiling for a non-cygwin target is not supported" >&2
  	;;
      esac
+ 
+     if test "x$cross_cache" != "x"; then
+ 	if test -r "${cross_cache}"; then
+ 	    echo "loading cross-build cache file ${cross_cache}"
+ 	    . ${cross_cache}
+ 	fi
+ 	unset cross_cache
+     fi
  fi
   
  if test "x$cross_compiling" = "xyes"; then
    CROSS_COMPILING_FLAG=-DCROSS_COMPILING
  else
*************** AC_SUBST(host_os)
*** 234,243 ****
--- 242,257 ----
  
  AC_SUBST(LIBVERSION)
  
  AC_SUBST(TERMCAP_LIB)
  
+ case "$host_os" in
+ mingw*)		RLTTYOBJ=rlnotty.o ;;
+ *)		RLTTYOBJ=rltty.o ;;
+ esac
+ AC_SUBST(RLTTYOBJ)
+ 
  AC_OUTPUT([Makefile doc/Makefile examples/Makefile shlib/Makefile],
  [
  # Makefile uses this timestamp file to record whether config.h is up to date.
  echo > stamp-h
  ])
Index: input.c
===================================================================
RCS file: /cvs/src/src/readline/input.c,v
retrieving revision 1.5
retrieving revision 1.5.60.2
diff -c -5 -p -r1.5 -r1.5.60.2
*** input.c	8 Dec 2002 22:31:37 -0000	1.5
--- input.c	8 Jun 2005 16:38:24 -0000	1.5.60.2
*************** rl_getc (stream)
*** 422,431 ****
--- 422,438 ----
    int result;
    unsigned char c;
  
    while (1)
      {
+ #ifdef __MINGW32__
+       /* On Windows, use a special routine to read a single character
+ 	 from the console.  (Otherwise, no characters are available
+ 	 until the user hits the return key.)  */
+       if (isatty (fileno (stream)))
+ 	return getch ();
+ #endif
        result = read (fileno (stream), &c, sizeof (unsigned char));
  
        if (result == sizeof (unsigned char))
  	return (c);
  
Index: readline.c
===================================================================
RCS file: /cvs/src/src/readline/readline.c,v
retrieving revision 1.7
retrieving revision 1.7.18.1
diff -c -5 -p -r1.7 -r1.7.18.1
*** readline.c	27 Jan 2004 22:25:15 -0000	1.7
--- readline.c	8 Jun 2005 16:38:25 -0000	1.7.18.1
*************** bind_arrow_keys_internal (map)
*** 866,875 ****
--- 866,891 ----
     _rl_bind_if_unbound ("\033[0B", rl_backward_char);
     _rl_bind_if_unbound ("\033[0C", rl_forward_char);
     _rl_bind_if_unbound ("\033[0D", rl_get_next_history);
  #endif
  
+ #ifdef __MINGW32__
+    /* Under Windows, when an extend key (like an arrow key) is
+       pressed, getch() will return 0xE0 followed by a code for the
+       extended key.  We use macros to transform those into the normal
+       UNIX sequences for these keys.  */
+ 
+    /* Up arrow.  */
+    rl_macro_bind ("\340H", "\033[A", map);
+    /* Left arrow.  */
+    rl_macro_bind ("\340K", "\033[D", map);
+    /* Right arrow.  */
+    rl_macro_bind ("\340M", "\033[C", map);
+    /* Down arrow.  */
+    rl_macro_bind ("\340P", "\033[B", map);
+ #endif
+ 
    _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
    _rl_bind_if_unbound ("\033[B", rl_get_next_history);
    _rl_bind_if_unbound ("\033[C", rl_forward_char);
    _rl_bind_if_unbound ("\033[D", rl_backward_char);
    _rl_bind_if_unbound ("\033[H", rl_beg_of_line);
Index: rldefs.h
===================================================================
RCS file: /cvs/src/src/readline/rldefs.h,v
retrieving revision 1.4
retrieving revision 1.4.60.1
diff -c -5 -p -r1.4 -r1.4.60.1
*** rldefs.h	8 Dec 2002 22:31:37 -0000	1.4
--- rldefs.h	28 Mar 2005 19:34:27 -0000	1.4.60.1
***************
*** 35,45 ****
  #if defined (_POSIX_VERSION) && !defined (TERMIOS_MISSING)
  #  define TERMIOS_TTY_DRIVER
  #else
  #  if defined (HAVE_TERMIO_H)
  #    define TERMIO_TTY_DRIVER
! #  else
  #    define NEW_TTY_DRIVER
  #  endif
  #endif
  
  /* Posix macro to check file in statbuf for directory-ness.
--- 35,45 ----
  #if defined (_POSIX_VERSION) && !defined (TERMIOS_MISSING)
  #  define TERMIOS_TTY_DRIVER
  #else
  #  if defined (HAVE_TERMIO_H)
  #    define TERMIO_TTY_DRIVER
! #  elif !defined (__MINGW32__)
  #    define NEW_TTY_DRIVER
  #  endif
  #endif
  
  /* Posix macro to check file in statbuf for directory-ness.
Index: rlnotty.c
===================================================================
RCS file: rlnotty.c
diff -N rlnotty.c
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- rlnotty.c	8 Jun 2005 17:29:15 -0000
***************
*** 0 ****
--- 1,101 ----
+ #define READLINE_LIBRARY
+ 
+ #if defined (HAVE_CONFIG_H)
+ #  include <config.h>
+ #endif
+ 
+ #include <stdio.h>
+ #include "readline.h"
+ #include "rlprivate.h"
+ 
+ rl_vintfunc_t *rl_prep_term_function = rl_prep_terminal;
+ rl_voidfunc_t *rl_deprep_term_function = rl_deprep_terminal;
+ 
+ int
+ tgetent (buffer, termtype)
+      char *buffer;
+      char *termtype;
+ {
+   return -1;
+ }
+ 
+ int
+ tgetnum (name)
+      char *name;
+ {
+   return -1;
+ }
+ 
+ int
+ tgetflag (name)
+      char *name;
+ {
+   return -1;
+ }
+ 
+ char *
+ tgetstr (name, area)
+      char *name;
+      char **area;
+ {
+   return NULL;
+ }
+ 
+ int
+ tputs (string, nlines, outfun)
+      char *string;
+      int nlines;
+      int (*outfun) ();
+ {
+   while (*string)
+     outfun (*string++);
+ }
+ 
+ char *
+ tgoto (cap, col, row)
+      const char *cap;
+      int col;
+      int row;
+ {
+   return NULL;
+ }
+      
+ int
+ _rl_disable_tty_signals ()
+ {
+   return 0;
+ }
+ 
+ int
+ _rl_restore_tty_signals ()
+ {
+   return 0;
+ }
+ 
+ void 
+ rl_prep_terminal (meta_flag)
+      int meta_flag;
+ {
+   readline_echoing_p = 1;
+   return;
+ }
+ 
+ void
+ rl_deprep_terminal ()
+ {
+   return;
+ }
+ 
+ int
+ rl_restart_output (count, key)
+      int count;
+      int key;
+ {
+   return 0;
+ }
+ 
+ void
+ rl_tty_set_default_bindings (kmap)
+      Keymap kmap;
+ {
+ }



More information about the Gdb-patches mailing list