From b4e66f273e793adcf1910df7c658042aeb4f1bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Wed, 27 Apr 2005 17:55:32 +0000 Subject: [PATCH] Use the readline library in the text interface. In particular, it provides a history of commands --- configure.in | 7 +++++- test/training_play.ref | 26 +++++++++---------- utils/Makefile.am | 4 +-- utils/eliottxt.cpp | 57 ++++++++++++++++++++++++++++++++---------- 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/configure.in b/configure.in index edd5ca1..279f9df 100644 --- a/configure.in +++ b/configure.in @@ -3,7 +3,7 @@ dnl Process this file with autoconf to produce a configure script. dnl -------------------------------------------------------------- dnl configure.in for Eliot dnl -------------------------------------------------------------- -dnl AC_REVISION($Id: configure.in,v 1.11 2005/04/09 19:16:56 afrab Exp $) +dnl AC_REVISION($Id: configure.in,v 1.12 2005/04/27 17:55:32 ipkiss Exp $) AC_INIT(eliot, 1.5-cvs) AC_CONFIG_SRCDIR(wxwin/main.cc) AM_INIT_AUTOMAKE @@ -115,6 +115,11 @@ AM_CONDITIONAL([BUILD_NCURSES], [test "${want_ncurses}" = "1"]) dnl Enable/disable text version AC_ARG_ENABLE(text, [ --enable-text text interface support (default enabled)]) +if test "${enable_text}" != "no" +then + AC_CHECK_HEADERS(readline/readline.h, want_text=1, + [AC_MSG_ERROR([Could not find the readline library on your system])]) +fi AM_CONDITIONAL([BUILD_TEXT], [test "${enable_text}" != "no"]) dnl Internationalization macros diff --git a/test/training_play.ref b/test/training_play.ref index f06f7ec..8fddcd9 100644 --- a/test/training_play.ref +++ b/test/training_play.ref @@ -2,19 +2,19 @@ commande> mode entraînement [?] pour l'aide commande> commande> UDEEEIJ -Mot incorrect ou mal placé (2) -Mot incorrect ou mal placé (3) -Mot incorrect ou mal placé (4) -Mot incorrect ou mal placé (10) -Mot incorrect ou mal placé (11) -commande> commande> commande> commande> commande> commande> commande> commande> commande> DEEI +commande> Mot incorrect ou mal placé (2) +commande> Mot incorrect ou mal placé (3) +commande> Mot incorrect ou mal placé (4) +commande> Mot incorrect ou mal placé (10) +commande> Mot incorrect ou mal placé (11) +commande> commande> commande> commande> DEEI commande> commande> DEEIPEG -Mot incorrect ou mal placé (6) -Mot incorrect ou mal placé (9) -commande> commande> commande> commande> DE -Mot incorrect ou mal placé (5) -Mot incorrect ou mal placé (7) -Mot incorrect ou mal placé (8) -commande> commande> commande> commande> commande> commande> EEEGIP +commande> Mot incorrect ou mal placé (6) +commande> Mot incorrect ou mal placé (9) +commande> commande> DE +commande> commande> Mot incorrect ou mal placé (5) +commande> Mot incorrect ou mal placé (7) +commande> Mot incorrect ou mal placé (8) +commande> commande> EEEGIP commande> fin du mode entraînement commande> \ No newline at end of file diff --git a/utils/Makefile.am b/utils/Makefile.am index 8f33299..b8071de 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -16,7 +16,7 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# $Id: Makefile.am,v 1.6 2005/04/03 21:12:03 ipkiss Exp $ +# $Id: Makefile.am,v 1.7 2005/04/27 17:55:32 ipkiss Exp $ top_srcdir = @top_srcdir@ @@ -26,7 +26,7 @@ noinst_PROGRAMS = if BUILD_TEXT noinst_PROGRAMS += eliottxt eliottxt_SOURCES = game_io.h game_io.cpp eliottxt.cpp -eliottxt_LDADD = ../game/libgame.a ../dic/libdic.a +eliottxt_LDADD = ../game/libgame.a ../dic/libdic.a -lreadline endif if BUILD_NCURSES diff --git a/utils/eliottxt.cpp b/utils/eliottxt.cpp index 0758db4..1b90614 100644 --- a/utils/eliottxt.cpp +++ b/utils/eliottxt.cpp @@ -3,7 +3,7 @@ * Authors: Antoine Fraboulet * Olivier Teuliere * - * $Id: eliottxt.cpp,v 1.5 2005/04/10 12:15:40 ipkiss Exp $ + * $Id: eliottxt.cpp,v 1.6 2005/04/27 17:55:33 ipkiss Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include "dic.h" #include "dic_search.h" @@ -36,6 +38,34 @@ #include "freegame.h" +/* A static variable for holding the line. */ +static char *line_read = NULL; + +/** + * Read a string, and return a pointer to it. + * Returns NULL on EOF. + */ +char *rl_gets() +{ + // If the buffer has already been allocated, return the memory to the free + // pool + if (line_read) + { + free(line_read); + line_read = NULL; + } + + // Get a line from the user + line_read = readline("commande> "); + + // If the line has any text in it, save it on the history + if (line_read && *line_read) + add_history(line_read); + + return line_read; +} + + char * next_token_alpha(char *cmd, const char *delim) { @@ -308,7 +338,7 @@ void loop_training(Training &iGame) { char *token; - char commande[100]; + char *commande = NULL; char delim[] = " \t"; int quit = 0; @@ -316,8 +346,7 @@ loop_training(Training &iGame) cout << "[?] pour l'aide\n"; while (quit == 0) { - printf("commande> "); - fgets(commande, sizeof(commande), stdin); + commande = rl_gets(); token = strtok(commande, delim); if (token) { @@ -433,7 +462,7 @@ void loop_freegame(FreeGame &iGame) { char *token; - char commande[100]; + char *commande = NULL; char delim[] = " \t"; int quit = 0; @@ -441,8 +470,7 @@ loop_freegame(FreeGame &iGame) printf("[?] pour l'aide\n"); while (quit == 0) { - printf("commande> "); - fgets(commande, sizeof(commande), stdin); + commande = rl_gets(); token = strtok(commande, delim); if (token) { @@ -527,7 +555,7 @@ void loop_duplicate(Duplicate &iGame) { char *token; - char commande[100]; + char *commande = NULL; char delim[] = " \t"; int quit = 0; @@ -535,8 +563,7 @@ loop_duplicate(Duplicate &iGame) printf("[?] pour l'aide\n"); while (quit == 0) { - printf("commande> "); - fgets(commande, sizeof(commande), stdin); + commande = rl_gets(); token = strtok(commande, delim); if (token) { @@ -625,15 +652,14 @@ void main_loop(const Dictionary &iDic) { char *token; - char commande[100]; + char *commande = NULL; char delim[] = " \t"; int quit = 0; printf("[?] pour l'aide\n"); while (quit == 0) { - printf("commande> "); - fgets(commande, sizeof(commande), stdin); + commande = rl_gets(); token = strtok(commande, delim); if (token) { @@ -822,5 +848,10 @@ main(int argc, char *argv[]) GameFactory::Destroy(); Dic_destroy(dic); + + // Free the readline static variable + if (line_read) + free(line_read); + return 0; }