xboard-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[XBoard-devel] patch for gnu-readline


From: APersaud
Subject: [XBoard-devel] patch for gnu-readline
Date: Tue, 19 Apr 2005 00:05:53 -0700

Hi,

here is a small hack for adding readline support to xboard (kind of). 

It's not perfect, but I got it running on linux and am using it on FICS.
What's still wrong: it uses "fics%" as a prompt, which obviously only
works on FICS and even there it overwrites the login and password prompt
(should be fixable though)...

Not sure if this is of interest for you, if so I could make it a bit
nicer and upload it as a patch to savannah. I added the patch in its
current state at the end of the email.

Apart fromt he patch it also need -lreadline while linking everything
together.

have fun
      ARUN

--- xboard-cvs.c         2005-04-18 23:39:50.000000000 -0700
+++ xboard-readline.c    2005-04-18 23:42:10.000000000 -0700
@@ -58,6 +58,9 @@
 #include <sys/stat.h>
 #include <pwd.h>
 
+#include <readline/readline.h>
+#include <readline/history.h>
+
 #if !OMIT_SOCKETS
 # if HAVE_SYS_SOCKET_H
 #  include <sys/socket.h>
@@ -193,6 +196,9 @@
 #include "xedittags.h"
 #include "gettext.h"
 
+static char* readline_buffer;
+static int readline_complete=0;
+
 #ifdef __EMX__
 #ifndef HAVE_USLEEP
 #define HAVE_USLEEP
@@ -416,6 +422,7 @@
 static void DragPieceEnd P((int x, int y));
 static void DrawDragPiece P((void));
 char *ModeToWidgetName P((GameMode mode));
+static void ReadlineCompleteHandler(char *);
 
 /*
 * XBoard depends on Xt R4 or higher
@@ -1863,7 +1870,9 @@
     setbuf(stdout, NULL);
     setbuf(stderr, NULL);
     debugFP = stderr;
-    
+
+    rl_callback_handler_install("fics% ", ReadlineCompleteHandler);    
+
     programName = strrchr(argv[0], '/');
     if (programName == NULL)
       programName = argv[0];
@@ -2574,6 +2583,8 @@
     }
     unlink(gameCopyFilename);
     unlink(gamePasteFilename);
+
+    rl_callback_handler_remove();
 }
 
 RETSIGTYPE
@@ -7764,12 +7775,43 @@
        }
        is->unused = q;
     } else {
-       count = read(is->fd, is->buf, INPUT_SOURCE_BUF_SIZE);
-       if (count == -1)
-         error = errno;
-       else
-         error = 0;
-       (is->func)(is, is->closure, is->buf, count, error);
+      if(is->fd==fileno(stdin) /*&& is->fd==0*/)
+       {
+         /* to clear the line */
+         printf("\r                                                 \r");
+
+         rl_callback_read_char(); /* stdin */
+         rl_reset_line_state();
+         
+         if(readline_complete)
+           {
+             count=strlen(readline_buffer);
+             if (count>INPUT_SOURCE_BUF_SIZE-1)
+               {
+                 printf("PROBLEM with readline\n");
+                 count=INPUT_SOURCE_BUF_SIZE;
+               };
+             strncpy(is->buf,readline_buffer,count);
+             is->buf[count]='\n';count++; 
+             free(readline_buffer);
+             readline_buffer=NULL;
+             readline_complete=0;
+             if (count == -1)
+               error = errno;
+             else
+               error = 0;
+             (is->func)(is, is->closure, is->buf, count, error);
+           }
+       }
+      else
+       {
+         count = read(is->fd, is->buf, INPUT_SOURCE_BUF_SIZE);
+         if (count == -1)
+           error = errno;
+         else
+           error = 0;
+         (is->func)(is, is->closure, is->buf, count, error);
+       };
     }  
 }
 
@@ -8594,3 +8636,11 @@
   damage[player.startBoardY][player.startBoardX] = TRUE;
 }
 
+static void ReadlineCompleteHandler(char* line)
+{
+    readline_buffer = line;
+    readline_complete = 1;
+
+    if (line && *line)
+      add_history(line);
+}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]