Hi,

last month using libeditline or libedit with eesh was briefly
discussed. Since having no history and no decent line editing in eesh
has annoyed me for ages, I gave it a try.

My qualification for this job was (and still is) outstanding: I didn't
know anything about X11 programming till last sunday, I haven't
written a line of C in the last two years (and not really much before
either), and I never used libreadline, libeditline or libedit. So
please have a look at my code if i'm doing something stupid.

Furthermore I don't know anything about autoconf or automake. So I
just hacked the auto-generated "Makefile" to link to libeditline and
libedit (no patch attached).

Because I couldn't connect to the CVS server of sourceforge last
friday, I used enlightenment-0.16.7-0.65.tar.gz as a template. Since
main.c of eesh isn't that large I'll mail the complete file and not a
patch (see attachment). I introduced to #defines, USE_EDITLINE and
USE_EDIT, to control which library is used.

About libedit: you can use $HOME/.editrc to configure it, e.g. "bind
-v" invokes vi keyset, see editrc (5el).

My setup:
    Debian testing/unstable
    official Debian enlightenment 0.16.6-1 (I'm just using eesh from
        0.16.7 to talk to it, maybe I shouldn't this?)
    official Debian libeditline0 1.12-5
    official Debian libedit2 2.6.cvs.200201

Why did I implement it this way:
    1) The prompt: to see if the user can type now. I could get rid of
       it when everything works. IMHO it looks better with a prompt.

    2) I couldn't just replace the lines
          k = 0;
            while ((ret = read(0, &(buf[j]), 1) > 0))
              {
                ...
       with the appropriate code for editline/edit or leaving the
       whole FD_SET-select-stuff out because
         a) the prompt resp. input will be messed up:
            no prompt shows but you can write without visible
            line-editing, type <ENTER> and the prompt shows with the
            text the user entered before including the line-editing,
            e.g. history.
         b) no output shows.
         c) if the eesh command doesn't produce any output
         d) misc. other symptoms (I went through several stages to get
            to the present state, I don't want to recount everything.)

       So I had use two fd_set variables and two calls to select: one,
       which looks for readfds and writefds, and another, which only
       looks for readfds. BTW, I could use only one variable and clear
       it by FD_ZERO, any preferences on your side?

       Maybe this is all self-evident to you but I included it because
       I hope it is easier for you to spot any errors.

Bugs:
    - <CTRL d> doesn't quit the interactive mode, use <CTRL c>
    - libeditline: type <CTRL d> => segfault
    - libedit: type <CTRL d>, line-editing etc. don't work anymore;
      just continue typing, focus other windows, re-focus eesh,
      do whatever, eventually it will work again
    - just type <ENTER>, mostly the terminal hangs; focus another
      window, focus terminal with eesh again, get a new prompt
    - the indent isn't consistent with the rest of the file, I'll
      correct this for the final patch
    - I'm sure there are others, we just have to find them :-)


First of all I'd be grateful if someone could look over my code and
tell me if there any obvious errors (see my programming skills above).


Ralf

PS: I hope my further mails will be shorter. :-)
/*
 Copyright (C) 2000-2004 Carsten Haitzler, Geoff Harrison and various contributors

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to
 deal in the Software without restriction, including without limitation the
 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 sell copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies of the Software, its documentation and marketing & publicity
 materials, and acknowledgment shall be given in the documentation, materials
 and software packages that this Software was used.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#define USE_EDITLINE 0
#define USE_EDIT 1

#include "E.h"
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#if USE_EDITLINE
#include <editline.h>
#elif USE_EDIT
#include <histedit.h>
#endif

extern char         waitonly;

static int          stdin_state;
void                restore_stdin_state(void);
#if USE_EDIT
char*               prompt(EditLine *);
#endif

void
restore_stdin_state(void)
{
   fcntl(0, F_SETFL, stdin_state);
}

#if USE_EDIT
char *
prompt(EditLine *e)
{
   return "> ";
}
#endif

int
main(int argc, char **argv)
{
   XEvent              ev;
   Client             *me, *e;
   fd_set              fd;
   signed char         ret;
   char               *command;
   int                 i;
#if USE_EDITLINE
   char                prompt[3] = "> ";
   char               *buf;
   fd_set              fd0;
#elif USE_EDIT
   EditLine           *edit;
   History            *hist;
   HistEvent           histev;
   int                 count;
   char               *buf;
   fd_set              fd0;
#else
   int                 j, k;
   char                buf[10240];
#endif

   waitonly = 0;
   lists.next = NULL;
   display_name = NULL;
   command = NULL;

   for (i = 0; i < argc; i++)
     {
	if (!strcmp(argv[i], "-e"))
	  {
	     if (i != (argc - 1))
	       {
		  command = argv[++i];
	       }
	  }
	else if (!strcmp(argv[i], "-ewait"))
	  {
	     waitonly = 1;
	     if (i != (argc - 1))
		command = argv[++i];
	  }
	else if (!strcmp(argv[i], "-display"))
	  {
	     if (i != (argc - 1))
	       {
		  display_name = argv[++i];
		  display_name = Estrdup(display_name);
	       }
	  }
	else if ((!strcmp(argv[i], "-h")) ||
		 (!strcmp(argv[i], "--h")) ||
		 (!strcmp(argv[i], "-help")) || (!strcmp(argv[i], "--help")))
	  {
	     printf("%s [ -e \"Command to Send to Enlightenment then exit\"]\n"
		    "     [ -ewait \"Command to Send to E then wait for a reply then exit\"]\n",
		    argv[0]);
	     printf("Use \"%s\" by itself to enter the \"interactive mode\"\n"
		    "Ctrl-D will exit interactive mode (EOF)\n"
		    "use \"help\" from inside interactive mode for further "
		    "assistance\n", argv[0]);
	     exit(0);
	  }
     }

   SetupX();
   CommsSetup();
   CommsFindCommsWindow();
   XSelectInput(disp, comms_win, StructureNotifyMask);
   XSelectInput(disp, root.win, PropertyChangeMask);
   e = MakeClient(comms_win);
   AddItem(e, "E", e->win, LIST_TYPE_CLIENT);
   me = MakeClient(my_win);
   AddItem(me, "ME", me->win, LIST_TYPE_CLIENT);
   CommsSend(e, "set clientname eesh");
   CommsSend(e, "set version 0.1");
   CommsSend(e, "set author The Rasterman");
   CommsSend(e, "set email [EMAIL PROTECTED]");
   CommsSend(e, "set web http://www.enlightenment.org";);
/*  CommsSend(e, "set address NONE"); */
   CommsSend(e, "set info Enlightenment IPC Shell - talk to E direct");
/*  CommsSend(e, "set pixmap 0"); */

   if (command)
     {
	CommsSend(e, command);
	if (!waitonly)
	  {
	     XSync(disp, False);
	     exit(0);
	  }
     }

   XSync(disp, False);
#if USE_EDITLINE
   /* nothing */
#elif USE_EDIT
   edit = el_init(argv[0], stdin, stdout, stderr);
   hist = history_init();
   /*
   if (hist == 0) {
      fprintf(stderr, "history could not be initialized\n");
      return 1;
   }
   */
   el_set(edit, EL_HIST, history, hist);
   el_set(edit, EL_EDITOR, "emacs");
   el_set(edit, EL_PROMPT, &prompt);
   el_source(edit, NULL);			/* read $HOME/.editrc */
   history(hist, &histev, H_SETSIZE, 800);
#else
   j = 0;
#endif
#if !(USE_EDITLINE)	/* with editline: segfault */
   stdin_state = fcntl(0, F_GETFL, 0);
   fcntl(0, F_SETFL, O_NONBLOCK);
   atexit(restore_stdin_state);
#endif
   for (;;)
     {
	if (waitonly)
	  {
	     XNextEvent(disp, &ev);
	     if (ev.type == ClientMessage)
		HandleComms(&ev);
	     else if (ev.type == DestroyNotify)
		exit(0);
	     XSync(disp, False);
	  }
	else
	  {
#if USE_EDITLINE
	     FD_SET(0, &fd0);
	     if (select(1, &fd0, &fd0, NULL, NULL) < 0)
		exit(0);
	     XSync(disp, False);
	     if (FD_ISSET(0, &fd0))
	     {
		/* XXX: just type <RETURN>, leave terminal, enter terminal */
		buf = readline(&prompt);
		if (strlen(buf) > 0)
		{
		   CommsSend(e, buf);
		   XSync(disp, False);
		}
		add_history(buf);
		free(buf);
	     }
	     FD_SET(ConnectionNumber(disp), &fd);
	     if (select(ConnectionNumber(disp) + 1, &fd, NULL, NULL, NULL) < 0)
		exit(0);
	     XSync(disp, False);
	     if (FD_ISSET(ConnectionNumber(disp), &fd))
	     {
		while (XPending(disp))
		{
		XNextEvent(disp, &ev);
		if (ev.type == ClientMessage)
		   HandleComms(&ev);
		else if (ev.type == DestroyNotify)
		   exit(0);
		}
		XSync(disp, False);
	     }
#elif USE_EDIT
	     FD_SET(0, &fd0);
	     if (select(1, &fd0, &fd0, NULL, NULL) < 0)
		exit(0);
	     XSync(disp, False);
	     if (FD_ISSET(0, &fd0))
	     {
		buf = el_gets(edit, &count);
		/* XXX: just type <ENTER>, leave terminal, enter terminal */
		/* XXX: just type <CTRL d>, vi keyset doesn't work anymore */
		if (count > 1)
		{
		   /* libedit: the returned string contains the ending \n */
		   /* are multi-byte strings a problem here? */
		   buf[count - 1] = 0;
		   history(hist, &histev, H_ENTER, buf);
		   CommsSend(e, buf);
		   XSync(disp, False);
		}
	     }
	     FD_SET(ConnectionNumber(disp), &fd);
	     if (select(ConnectionNumber(disp) + 1, &fd, NULL, NULL, NULL) < 0)
		exit(0);
	     XSync(disp, False);
	     if (FD_ISSET(ConnectionNumber(disp), &fd))
	     {
		while (XPending(disp))
		{
		   XNextEvent(disp, &ev);
		   if (ev.type == ClientMessage)
		      HandleComms(&ev);
		   else if (ev.type == DestroyNotify)
		      exit(0);
		}
		XSync(disp, False);
	     }
#else
	     FD_SET(0, &fd);
	     FD_SET(ConnectionNumber(disp), &fd);
	     if (select(ConnectionNumber(disp) + 1, &fd, NULL, NULL, NULL) < 0)
		exit(0);
	     XSync(disp, False);
	     if (FD_ISSET(0, &fd))
	       {
		  k = 0;
		  while ((ret = read(0, &(buf[j]), 1) > 0))
		    {
		       k = 1;
		       if (buf[j] == '\n')
			 {
			    buf[j] = 0;
			    if (strlen(buf) > 0)
			      {
				 CommsSend(e, buf);
				 XSync(disp, False);
			      }
			    j = -1;
			 }
		       j++;
		    }
		  if ((ret < 0) || ((k == 0) && (ret == 0)))
		     exit(0);
	       }
	     else if (FD_ISSET(ConnectionNumber(disp), &fd))
	       {
		  while (XPending(disp))
		    {
		       XNextEvent(disp, &ev);
		       if (ev.type == ClientMessage)
			  HandleComms(&ev);
		       else if (ev.type == DestroyNotify)
			  exit(0);
		    }
		  XSync(disp, False);
	       }
#endif
	  }
     }
#if USE_EDIT
   history_end(hist);
   el_end(edit);
#endif
   return 0;
}

void
Alert(const char *fmt, ...)
{
   va_list             ap;

   EDBUG(7, "Alert");
   va_start(ap, fmt);
   vfprintf(stderr, fmt, ap);
   va_end(ap);
   EDBUG_RETURN_;
}

Reply via email to