glug-nith-discuss
[Top][All Lists]
Advanced

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

[Glug-nith-discuss] A nice way to take inputs in C programs.


From: Debarshi 'Rishi' Ray
Subject: [Glug-nith-discuss] A nice way to take inputs in C programs.
Date: Sat, 3 Mar 2007 01:22:35 +0530
User-agent: Mutt/1.5.13 (2006-08-11)

You must have noticed how most of the GNU programs take input from the user.
eg., one can go up and down the history of the last strings entered, use
ctrl+l to clear the screen, use ctrl+r to search the history, use TAB to
complete the name of a file, use alt+d to delete a word, alt+f to move ahead
by one word, alt+b to go back by one word, etc.. If you have not then just
try any one of these to see for yourself: GNU Bash (ie., gnome-terminal or
konsole in most GNU/Linux systems), GNU Emacs, etc..

So what does it take to have such nice features in your own C program. After
all both Bash and Emacs are written in C. Here is an example:

#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>

const char*    prompt = ">>> ";
const char*    history = ".foo_history";

int do_something (const char*);

int main(void)
{
        char*    line = (char *) NULL;
        int      err;

        err = read_history (history);
        if (err)
                fputs ("Couldn't read history.\n", stderr);

        for (;;) {
                line = readline (prompt);
                if (!line)
                        break;
                
                if (*line) {
                        add_history (line);
                        do_something (line);
                        free (line);
                }
        }
        fputs ("\n", stdout);

        err = write_history (history);
        if (err)
                fputs ("Couldn't write history.\n", stderr);

        return 0;
}

int do_something (const char* str) {
        return printf ("%s\n", str);
}

It is a simple example to show one can use the GNU Readline library to take
inputs from the use in C. Right now it just takes some input from the user
and echoes it on the next line, however you will be able to:
a. Use the ctrl+p (up arrow) and ctrl+n (down arrow) keys to go back and
   forth among the values you have entered.
b. The history is saved after the program is quit, so that next time it is
   available.
c. Use ctrl+r to search the history.
d. Use ctrl+l to clear the screen.
e. Use TAB to complete the names of file in the current directory.
.... and so on.

Save the code in a file named 'foo.c', and compile it using:
$ gcc -o foo foo.c -lreadline -ltermcap
Make sure that you have readline, readline-devel and termcap installed on
your system.

If you have come this far, then here is how you run it:
$ ./foo

Enjoy all the coolness of GNU Readline in your own code!

Note: To exit from the program use ctrl+d. Remember it is the 'end of file'
character. 

For those who want to read more:
a. http://tiswww.case.edu/~chet/readline/rltop.html
b. http://tiswww.case.edu/~chet/readline/rluserman.html

Happy hacking,
Debarshi
-- 
husband    v. use sparingly; conserve; save
husbandry  n. frugality; thrift; agriculture

Attachment: pgpcmbx8JsAwb.pgp
Description: PGP signature


reply via email to

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