实现Linux下上下键和命令补全


可以用readline库

安装readline

在eclipse中编译使用readline需要在连接时加上-Ireadline      -Itermcap

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

static char *line_read = (char *) NULL;

char *rl_gets() {
 if (line_read) {
  free(line_read);
  line_read = (char *) NULL;
 }
 line_read = readline("Please Enter:");

 if (line_read && *line_read)
  add_history(line_read);
 return (line_read);
}

int main() {
 char *mline;
 mline = rl_gets();
 printf("%s\n", mline);
}

教学资料

用libedit库

用这库需要安装libedit和ncurses-5.6  readline是GPL的,这两个是非GPL的

一个linux命令示列

  1. /* fileman.c -- A tiny application which demonstrates how to use the  
  2.  GNU Readline library.  This application interactively allows users  
  3.  to manipulate files and their modes.  
  4.  
  5.  NOTE: this was taken from the GNU Readline documentation and ported  
  6.  to libedit. A commad to output the history list was added.  
  7.  
  8.  */ 
  9.  
  10. #include <stdio.h>  
  11. #include <sys/types.h>  
  12. #include <sys/file.h>  
  13. #include <sys/stat.h>  
  14. #include <sys/errno.h>  
  15. #include <ctype.h>  
  16. #include <string.h>  
  17. #include <stdlib.h>  
  18. #include <unistd.h>  
  19. #include <locale.h>  
  20. #include <time.h>   
  21.   
  22. /* GNU readline  
  23.  #include <readline/readline.h>  
  24.  #include <readline/history.h>  
  25.  */ 
  26. #include <editline/readline.h>   
  27.   
  28. void * xmalloc(size_t size);   
  29. void too_dangerous(char *caller);   
  30. void initialize_readline();   
  31. int execute_line(char *line);   
  32. int valid_argument(char *caller, char *arg);   
  33.   
  34. typedef int rl_icpfunc_t(char *);   
  35.   
  36. /* The names of functions that actually do the manipulation. */  
  37. int com_list(char *);   
  38. int com_view(char *);   
  39. int com_history(char *);   
  40. int com_rename(char *);   
  41. int com_stat(char *);   
  42. int com_pwd(char *);   
  43. int com_delete(char *);   
  44. int com_help(char *);   
  45. int com_cd(char *);   
  46. int com_quit(char *);   
  47.   
  48. /* A structure which contains information on the commands this program  
  49.  can understand. */  
  50.   
  51. typedef struct {   
  52.     char *name; /* User printable name of the function. */  
  53.     rl_icpfunc_t *func; /* Function to call to do the job. */  
  54.     char *doc; /* Documentation for this function.  */  
  55. } COMMAND;   
  56.   
  57. COMMAND commands[] = { { "cd", com_cd, "Change to directory DIR" }, { "delete",   
  58.         com_delete, "Delete FILE" }, { "help", com_help, "Display this text" },   
  59.         { "?", com_help, "Synonym for `help'" }, { "list", com_list,   
  60.                 "List files in DIR" },   
  61.         { "ls", com_list, "Synonym for `list'" }, { "pwd", com_pwd,   
  62.                 "Print the current working directory" }, { "quit", com_quit,   
  63.                 "Quit using Fileman" }, { "rename", com_rename,   
  64.                 "Rename FILE to NEWNAME" }, { "stat", com_stat,   
  65.                 "Print out statistics on FILE" }, { "view", com_view,   
  66.                 "View the contents of FILE" }, { "history", com_history,   
  67.                 "List editline history" }, { (char *) NULL,   
  68.                 (rl_icpfunc_t *) NULL, (char *) NULL } };   
  69.   
  70. /* Forward declarations. */  
  71. char *stripwhite(char *string);   
  72. COMMAND *find_command(char *name);   
  73.   
  74. /* The name of this program, as taken from argv[0]. */  
  75. char *progname;   
  76.   
  77. /* When non-zero, this means the user is done using this program. */  
  78. int done;   
  79.   
  80. char *   
  81. dupstr(char* s) {   
  82.     char *r;   
  83.   
  84.     r = (char*)xmalloc(strlen(s) + 1);   
  85.     strcpy(r, s);   
  86.     return (r);   
  87. }   
  88.   
  89. int main(int argc, char **argv) {   
  90.     char *line, *s;   
  91.   
  92.     progname = argv[0];   
  93.   
  94.     setlocale(LC_CTYPE, "");   
  95.   
  96.     initialize_readline(); /* Bind our completer. */  
  97.   
  98.     stifle_history(7);   
  99.   
  100.     /* Loop reading and executing lines until the user quits. */  
  101.     for (; done == 0;) {   
  102.         line = readline("FileMan: ");   
  103.   
  104.         if (!line)   
  105.             break;   
  106.   
  107.         /* Remove leading and trailing whitespace from the line.  
  108.          Then, if there is anything left, add it to the history list  
  109.          and execute it. */  
  110.         s = stripwhite(line);   
  111.   
  112.         if (*s) {   
  113.   
  114.             char* expansion;   
  115.             int result;   
  116.   
  117.             result = history_expand(s, &expansion);   
  118.   
  119.             if (result < 0 || result == 2) {   
  120.                 fprintf(stderr, "%s\n", expansion);   
  121.             } else {   
  122.                 add_history(expansion);   
  123.                 execute_line(expansion);   
  124.             }   
  125.             free(expansion);   
  126.         }   
  127.   
  128.         free(line);   
  129.     }   
  130.     exit(0);   
  131.   
  132.     return 0;   
  133. }   
  134.   
  135. /* Execute a command line. */  
  136. int execute_line(char *line) {   
  137.     register int i;   
  138.     COMMAND *command;   
  139.     char *word;   
  140.   
  141.     /* Isolate the command word. */  
  142.     i = 0;   
  143.     while (line[i] && isspace(line[i]))   
  144.         i++;   
  145.     word = line + i;   
  146.   
  147.     while (line[i] && !isspace(line[i]))   
  148.         i++;   
  149.   
  150.     if (line[i])   
  151.         line[i++] = '\0';   
  152.   
  153.     command = find_command(word);   
  154.   
  155.     if (!command) {   
  156.         fprintf(stderr, "%s: No such command for FileMan.\n", word);   
  157.         return (-1);   
  158.     }   
  159.   
  160.     /* Get argument to command, if any. */  
  161.     while (isspace(line[i]))   
  162.         i++;   
  163.   
  164.     word = line + i;   
  165.   
  166.     /* Call the function. */  
  167.     return ((*(command->func))(word));   
  168. }   
  169.   
  170. /* Look up NAME as the name of a command, and return a pointer to that  
  171.  command.  Return a NULL pointer if NAME isn't a command name. */  
  172. COMMAND *   
  173. find_command(char *name) {   
  174.     register int i;   
  175.   
  176.     for (i = 0; commands[i].name; i++)   
  177.         if (strcmp(name, commands[i].name) == 0)   
  178.             return (&commands[i]);   
  179.   
  180.     return ((COMMAND *) NULL);   
  181. }   
  182.   
  183. /* Strip whitespace from the start and end of STRING.  Return a pointer  
  184.  into STRING. */  
  185. char *   
  186. stripwhite(char *string) {   
  187.     register char *s, *t;   
  188.   
  189.     for (s = string; isspace(*s); s++)   
  190.         ;   
  191.   
  192.     if (*s == 0)   
  193.         return (s);   
  194.   
  195.     t = s + strlen(s) - 1;   
  196.     while (t > s && isspace(*t))   
  197.         t--;   
  198.     *++t = '\0';   
  199.   
  200.     return s;   
  201. }   
  202.   
  203. /* **************************************************************** */  
  204. /*                                                                  */  
  205. /*                  Interface to Readline Completion                */  
  206. /*                                                                  */  
  207. /* **************************************************************** */  
  208.   
  209. char *command_generator(const char *, int);   
  210. char **fileman_completion(const char *, intint);   
  211.   
  212. /* Tell the GNU Readline library how to complete.  We want to try to  
  213.  complete on command names if this is the first word in the line, or  
  214.  on filenames if not. */  
  215. void initialize_readline() {   
  216.     /* Allow conditional parsing of the ~/.inputrc file. */  
  217.     rl_readline_name = "FileMan";   
  218.   
  219.     /* Tell the completer that we want a crack first. */  
  220.     rl_attempted_completion_function = fileman_completion;   
  221. }   
  222.   
  223. /* Attempt to complete on the contents of TEXT.  START and END  
  224.  bound the region of rl_line_buffer that contains the word to  
  225.  complete.  TEXT is the word to complete.  We can use the entire  
  226.  contents of rl_line_buffer in case we want to do some simple  
  227.  parsing.  Returnthe array of matches, or NULL if there aren't any. */  
  228. char **   
  229. fileman_completion(const char* text, int start, int end) {   
  230.     char **matches;   
  231.   
  232.     matches = (char **) NULL;   
  233.   
  234.     /* If this word is at the start of the line, then it is a command  
  235.      to complete.  Otherwise it is the name of a file in the current  
  236.      directory. */  
  237.     if (start == 0)   
  238.         /* TODO */  
  239.         matches = completion_matches(text, command_generator);   
  240.     /* matches = rl_completion_matches (text, command_generator); */  
  241.   
  242.     return (matches);   
  243. }   
  244.   
  245. /* Generator function for command completion.  STATE lets us  
  246.  know whether to start from scratch; without any state  
  247.  (i.e. STATE == 0), then we start at the top of the list. */  
  248. char *   
  249. command_generator(const char *text, int state) {   
  250.     static int list_index, len;   
  251.     char *name;   
  252.   
  253.     /* If this is a new word to complete, initialize now.  This  
  254.      includes saving the length of TEXT for efficiency, and  
  255.      initializing the index variable to 0. */  
  256.     if (!state) {   
  257.         list_index = 0;   
  258.         len = strlen(text);   
  259.     }   
  260.   
  261.     /* Return the next name which partially matches from the  
  262.      command list. */  
  263.     while (name = commands[list_index].name) {   
  264.         list_index++;   
  265.   
  266.         if (strncmp(name, text, len) == 0)   
  267.             return (dupstr(name));   
  268.     }   
  269.   
  270.     /* If no names matched, then return NULL. */  
  271.     return ((char *) NULL);   
  272. }   
  273.   
  274. /* **************************************************************** */  
  275. /*                                                                  */  
  276. /*                       FileMan Commands                           */  
  277. /*                                                                  */  
  278. /* **************************************************************** */  
  279.   
  280. /* String to pass to system ().  This is for the LIST, VIEW and RENAME  
  281.  commands. */  
  282. static char syscom[1024];   
  283.   
  284. /* List the file(s) named in arg. */  
  285. int com_list(char *arg) {   
  286.     if (!arg)   
  287.         arg = "";   
  288.   
  289.     sprintf(syscom, "ls -FClg %s", arg);   
  290.     return (system(syscom));   
  291. }   
  292.   
  293. int com_view(char *arg) {   
  294.     if (!valid_argument("view", arg))   
  295.         return 1;   
  296.   
  297.     sprintf(syscom, "more %s", arg);   
  298.     return (system(syscom));   
  299. }   
  300.   
  301. int com_history(char* arg) {   
  302.     HIST_ENTRY *he;   
  303.   
  304.     /* rewind history */  
  305.     while (next_history())   
  306.         ;   
  307.   
  308.     for (he = current_history(); he != NULL; he = previous_history()) {   
  309.         //printf("%5d  %s\n", *((int*)he->data) - 1, he->line);   
  310.         printf("%s\n", he->line);   
  311.     }   
  312.   
  313.     return 0;   
  314. }   
  315.   
  316. int com_rename(char *arg) {   
  317.     too_dangerous("rename");   
  318.     return (1);   
  319. }   
  320.   
  321. int com_stat(char *arg) {   
  322.     struct stat finfo;   
  323.   
  324.     if (!valid_argument("stat", arg))   
  325.         return (1);   
  326.   
  327.     if (stat(arg, &finfo) == -1) {   
  328.         perror(arg);   
  329.         return (1);   
  330.     }   
  331.   
  332.     printf("Statistics for `%s':\n", arg);   
  333.   
  334.     printf("%s has %ld link%s, and is %lld byte%s in length.\n", arg,   
  335.             (long) finfo.st_nlink, (finfo.st_nlink == 1) ? "" : "s",   
  336.             (long long) finfo.st_size, (finfo.st_size == 1) ? "" : "s");   
  337.     printf("Inode Last Change at: %s", ctime(&finfo.st_ctime));   
  338.     printf("      Last access at: %s", ctime(&finfo.st_atime));   
  339.     printf("    Last modified at: %s", ctime(&finfo.st_mtime));   
  340.     return (0);   
  341. }   
  342.   
  343. int com_delete(char *arg) {   
  344.     too_dangerous("delete");   
  345.     return (1);   
  346. }   
  347.   
  348. /* Print out help for ARG, or for all of the commands if ARG is  
  349.  not present. */  
  350. int com_help(char *arg) {   
  351.     register int i;   
  352.     int printed = 0;   
  353.   
  354.     for (i = 0; commands[i].name; i++) {   
  355.         if (!*arg || (strcmp(arg, commands[i].name) == 0)) {   
  356.             printf("%s\t\t%s.\n", commands[i].name, commands[i].doc);   
  357.             printed++;   
  358.         }   
  359.     }   
  360.   
  361.     if (!printed) {   
  362.         printf("No commands match `%s'.  Possibilties are:\n", arg);   
  363.   
  364.         for (i = 0; commands[i].name; i++) {   
  365.             /* Print in six columns. */  
  366.             if (printed == 6) {   
  367.                 printed = 0;   
  368.                 printf("\n");   
  369.             }   
  370.   
  371.             printf("%s\t", commands[i].name);   
  372.             printed++;   
  373.         }   
  374.   
  375.         if (printed)   
  376.             printf("\n");   
  377.     }   
  378.     return (0);   
  379. }   
  380.   
  381. /* Change to the directory ARG. */  
  382. int com_cd(char *arg) {   
  383.     if (chdir(arg) == -1) {   
  384.         perror(arg);   
  385.         return 1;   
  386.     }   
  387.   
  388.     com_pwd("");   
  389.     return (0);   
  390. }   
  391.   
  392. /* Print out the current working directory. */  
  393. int com_pwd(char* ignore) {   
  394.     char dir[1024], *s;   
  395.   
  396.     s = (char*) getcwd(dir, sizeof(dir) - 1);   
  397.     if (s == 0) {   
  398.         printf("Error getting pwd: %s\n", dir);   
  399.         return 1;   
  400.     }   
  401.   
  402.     printf("Current directory is %s\n", dir);   
  403.     return 0;   
  404. }   
  405.   
  406. /* The user wishes to quit using this program.  Just set DONE  
  407.  non-zero. */  
  408. int com_quit(char *arg) {   
  409.     done = 1;   
  410.     return (0);   
  411. }   
  412.   
  413. /* Function which tells you that you can't do this. */  
  414. void too_dangerous(char *caller) {   
  415.     fprintf(stderr, "%s: Too dangerous for me to distribute.\n", caller);   
  416.     fprintf(stderr, "Write it yourself.\n");   
  417. }   
  418.   
  419. /* Return non-zero if ARG is a valid argument for CALLER,  
  420.  else print an error message and return zero. */  
  421. int valid_argument(char *caller, char *arg) {   
  422.     if (!arg || !*arg) {   
  423.         fprintf(stderr, "%s: Argument required.\n", caller);   
  424.         return (0);   
  425.     }   
  426.   
  427.     return (1);   
  428. }   
  429.   
  430. void *   
  431. xmalloc(size_t size) {   
  432.     register void *value = (void*) malloc(size);   
  433.     if (value == 0)   
  434.         fprintf(stderr, "virtual memory exhausted");   
  435.     return value;   
  436. }  
  • 1
  • 2
  • 3
  • 下一页

相关内容