Use the readline library in the text interface. In particular, it provides

a history of commands
This commit is contained in:
Olivier Teulière 2005-04-27 17:55:32 +00:00
parent 506f31702a
commit b4e66f273e
4 changed files with 65 additions and 29 deletions

View file

@ -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

View file

@ -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>

View file

@ -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

View file

@ -3,7 +3,7 @@
* Authors: Antoine Fraboulet <antoine.fraboulet@free.fr>
* Olivier Teuliere <ipkiss@via.ecp.fr>
*
* $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 <string.h>
#include <ctype.h>
#include <fstream>
#include <readline/readline.h>
#include <readline/history.h>
#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;
}