Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Minimal example that uses readline - and tries to use _rl_trace to
- * output a debugging message to /var/tmp/readline.%d.
- *
- * Compile via:
- *
- * gcc -o rl rl.c -lreadline
- *
- * Enter a few lines, and see if the /var/tmp file appears? TAB-completion
- * will work if you enter filenames.
- *
- * Main code from: http://en.wikipedia.org/wiki/GNU_Readline#Sample_code
- *
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <readline/readline.h>
- #include <readline/history.h>
- int main()
- {
- char* input, shell_prompt[100];
- // Configure readline to auto-complete paths when the tab key is hit.
- rl_bind_key('\t', rl_complete);
- for(;;) {
- // Create prompt string from user name and current working directory.
- snprintf(shell_prompt, sizeof(shell_prompt), "%s:%s $ ", getenv("USER"), getcwd(NULL, 1024));
- // Display prompt and read input (n.b. input must be freed after use)...
- input = readline(shell_prompt);
- _rl_trace( "Read %s", input );
- // Check for EOF.
- if (!input)
- break;
- // Add input to history.
- add_history(input);
- // Do stuff...
- // Free input.
- free(input);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment