You are not logged in.

#1 2007-11-23 13:35:27

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

ed with readline

For shits and giggles I aliased vim=ed and removed vi(m), python and ruby.

One thing that bothered me a lot in ed was the lack of readline abilities. So I added it to ed using an example from here. But I'm rather a C noob so I'll post it here and hopefully someone will spot a big error.

I did cut a corner with the buffer size and error catching, I have no idea how to properly use that. (the resize_buffer function was using multiples of 512 though, that's why I went for that)

It does work - seemingly the same, but with lovely readline - but I have no idea how e.g. patch will react to it, or if it has memory leaks.
Anyway, here is the diff:
Output of: diff edd/ed-0.8/ ed-0.8/

diff edd/ed-0.8/Makefile ed-0.8/Makefile
23c23
< LDFLAGS = 
---
> LDFLAGS = -lreadline
Common subdirectories: edd/ed-0.8/doc and ed-0.8/doc
diff edd/ed-0.8/io.c ed-0.8/io.c
22a23,24
> #include <readline/readline.h>
> #include <readline/history.h>
131,132d132
< 
< /* read a line of text from stdin; return pointer to buffer and line length */
135,165c135,146
<   static char *ibuf = 0;              /* ed command-line buffer */
<   static int ibufsz = 0;              /* ed command-line buffer size */
<   int i = 0, oi = -1;
< 
<   while( 1 )
<     {
<     const int c = getchar();
<     if( c == EOF )
<       {
<       if( ferror( stdin ) )
<         {
<         show_strerror( "stdin", errno ); set_error_msg( "Cannot read stdin" );
<         clearerr( stdin ); if( lenp ) *lenp = 0;
<         return 0;
<         }
<       else
<         {
<         clearerr( stdin ); if( i != oi ) { oi = i; continue; }
<         if( i ) ibuf[i] = 0; if( lenp ) *lenp = i;
<         return ibuf;
<         }
<       }
<     else
<       {
<       if( !resize_buffer( &ibuf, &ibufsz, i + 2 ) )
<         { if( lenp ) *lenp = 0; return 0; }
<       ibuf[i++] = c; if( !c ) set_binary(); if( c != '\n' ) continue;
<       ibuf[i] = 0; if( lenp ) *lenp = i;
<       return ibuf;
<       }
<     }
---
>   static char *ibuf = (char *)NULL;
>   char *templine = 0;
>   templine = readline ("");
>   if (templine && *templine) add_history (templine);
>   if (lenp) *lenp = strlen(templine);
>   (*lenp)++;
>   ibuf = malloc(512);
>   strcpy(ibuf, templine);
>   free(templine);
>   ibuf[(*lenp)-1]='\n';
>   ibuf[*lenp]=0;
>   return ibuf;
diff edd/ed-0.8/main.c ed-0.8/main.c
39a40,41
> #include <readline/readline.h>
> #include <readline/history.h>
121a124
>   rl_bind_key ('\t', rl_insert);
Common subdirectories: edd/ed-0.8/testsuite and ed-0.8/testsuite

Offline

#2 2007-11-25 21:45:10

Gilneas
Member
From: Netherlands
Registered: 2006-10-22
Posts: 320

Re: ed with readline

While I improved it a little bit, I realised something like a pipe construct would be nicer and came upon rlwrap.
And it's working just as well smile
So this was in vain, but it was funny (imo).

Offline

#3 2007-11-27 02:42:33

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: ed with readline

I thought it was funny too. ed is older than me.

Offline

Board footer

Powered by FluxBB