Guest User

Untitled

a guest
Mar 14th, 2014
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. /**
  2.  * Minimal example that uses readline - and tries to use _rl_trace to
  3.  * output a debugging message to /var/tmp/readline.%d.
  4.  *
  5.  * Compile via:
  6.  *
  7.  *    gcc -o rl rl.c -lreadline
  8.  *
  9.  * Enter a few lines, and see if the /var/tmp file appears?  TAB-completion
  10.  * will work if you enter filenames.
  11.  *
  12.  * Main code from: http://en.wikipedia.org/wiki/GNU_Readline#Sample_code
  13.  *
  14.  */
  15.  
  16.  
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <readline/readline.h>
  21. #include <readline/history.h>
  22.  
  23. int main()
  24. {
  25.     char* input, shell_prompt[100];
  26.  
  27.     // Configure readline to auto-complete paths when the tab key is hit.
  28.     rl_bind_key('\t', rl_complete);
  29.  
  30.     for(;;) {
  31.         // Create prompt string from user name and current working directory.
  32.         snprintf(shell_prompt, sizeof(shell_prompt), "%s:%s $ ", getenv("USER"), getcwd(NULL, 1024));
  33.  
  34.         // Display prompt and read input (n.b. input must be freed after use)...
  35.         input = readline(shell_prompt);
  36.  
  37.         _rl_trace( "Read %s", input );
  38.  
  39.         // Check for EOF.
  40.         if (!input)
  41.             break;
  42.  
  43.         // Add input to history.
  44.         add_history(input);
  45.  
  46.         // Do stuff...
  47.  
  48.         // Free input.
  49.         free(input);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment