[Ilugc] [LANGUAGETIP - C ] editline sample for bash like history

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Mon Nov 2 17:16:18 2009

Please take a look at this program.

http://gayatri-hitech.com/languageclasses/editline-sample.c.html

I have also pasted the code here:

/*
   cc -g test.c -o test -ledit -ltermcap
 */

#include <stdio.h>
#include <histedit.h>

/* To print out the prompt you need to use a function.  This could be
   made to do something special, but I opt to just have a static prompt.
 */
char * prompt(EditLine *e) {
        return "test> ";
}

int main(int argc, char *argv[]) {

        /* This holds all the state for our line editor */
        EditLine *el;

        /* This holds the info for our history */
        History *myhistory;

        /* Temp variables */
        int count;
        const char *line;
        int keepreading = 1;
        HistEvent ev;

        /* Initialize the EditLine state to use our prompt function and
           emacs style editing. */

        el = el_init(argv[0], stdin, stdout, stderr);
        el_set(el, EL_PROMPT, &prompt);
        el_set(el, EL_EDITOR, "emacs");

        /* Initialize the history */
        myhistory = history_init();
        if (myhistory == 0) {
                fprintf(stderr, "history could not be initialized\n");
                return 1;
        }

        /* Set the size of the history */
        history(myhistory, &ev, H_SETSIZE, 800);

        /* This sets up the call back functions for history functionality */
        el_set(el, EL_HIST, history, myhistory);

        while (keepreading) {
                /* count is the number of characters read.
                   line is a const char* of our command line with the tailing 
\n */
                line = el_gets(el, &count);

                /* In order to use our history we have to explicitly add 
commands
                   to the history */
                if (count > 0) {
                        history(myhistory, &ev, H_ENTER, line);
                        printf("You typed \"%s\"\n", line);
                }
        }


        /* Clean up our memory */
        history_end(myhistory);
        el_end(el);


        return 0;
}

Please compile this program and see what it does. Actually bash uses
readline and editline is a
BSD licensed BSD alternative to the bulky readline library.

The other day I googled and got this code. I ran it and was very happy
with the results.

So this is what you need to do to integrate history features into your
application.

I think the code is mostly self explanatory. ;)

That guy has commented the code very well.

By the way please look at this page:

http://gayatri-hitech.com/languageclasses/

It is evolving and not ready yet. But I shall put up all the code samples here.

Not the LUG mails. You only need the code right? ;)

I shall send better language tips from next week onwards.

Best,
Girish

-- 
Gayatri Hitech
web: http://gayatri-hitech.com

SpamCheetah Spam filter:
http://spam-cheetah.com

Other related posts:

  • » [Ilugc] [LANGUAGETIP - C ] editline sample for bash like history - Girish Venkatachalam