readline库实现命令行自动补全-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 752961
  • 博文数量: 144
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1150
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-17 14:32
个人简介

小公司研发总监,既当司令也当兵!

文章分类

全部博文(144)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: linux

2015-05-25 01:46:59

readline库实现命令行自动补全

   linux下应用程序可能需要交互式输入命令,但普通的标准io进行命令输入显得有些呆板,人性化不足。本文讲述使用libreadline库,实现类似sh的交换终端:支持命令自动补全,支持历史命令等。

    part1: readline安装
    (1) 下载readline源码:
    (2) 解压后, 在源码目录依次执行 ./configure, make, sudo make install 完成安装

    part2:readline使用举例
点击(此处)折叠或打开
  1. #include <stdlib.h>
  2. #include <stdio.h> //注意,readline.h中可能需要调用标准io库的内容,所以stdio.h必须在readline.h之前被包含
  3. #include <readline/readline.h>
  4. #include <readline/history.h>


  5. /*
  6. * 真正的命令执行函数
  7. * 测试时,被定义为桩函数了
  8. */
  9. int com_list(char *para)
  10. {
  11.     printf("do com_list:%s\n", para);
  12.     return 0;
  13. }
  14.     
  15. int com_view(char *para)
  16. {
  17.     printf("do com_view:%s\n", para);
  18.     return 0;
  19. }

  20. int com_rename(char *para)
  21. {
  22.     printf("do com_rename:%s\n", para);
  23.     return 0;
  24. }
  25. int com_stat(char *para)
  26. {
  27.     printf("do com_stat:%s\n", para);
  28.     return 0;
  29. }

  30. int com_pwd(char *para)
  31. {
  32.     printf("do com_pwd:%s\n", para);
  33.     return 0;
  34. }
  35. int com_delete(char *para)
  36. {
  37.     printf("do com_delete:%s\n", para);
  38.     return 0;
  39. }
  40. int com_help(char *para)
  41. {
  42.     printf("do com_help:%s\n", para);
  43.     return 0;
  44. }

  45. int com_cd(char *para)
  46. {
  47.     printf("do com_cd:%s\n", para);
  48.     return 0;
  49. }
  50. int com_quit(char *para)
  51. {
  52.     printf("do com_quit:%s\n", para);
  53.     exit(0);
  54. }


  55. /*
  56. * a structure which contains information on the commands this program
  57. * can understand.
  58. */
  59. typedef struct {
  60.     char *name;            /* user printable name of the function. */
  61.     rl_icpfunc_t *func;        /* function to call to do the job. */
  62.     char *doc;            /* documentation for this function. */
  63. } command;

  64. command commands[] = {
  65.   { "cd", com_cd, "change to directory dir" },
  66.   { "delete", com_delete, "delete file" },
  67.   { "help", com_help, "display this text" },
  68.   { "?", com_help, "synonym for `help'" },
  69.   { "list", com_list, "list files in dir" },
  70.   { "ls", com_list, "synonym for `list'" },
  71.   { "pwd", com_pwd, "print the current working directory" },
  72.   { "quit", com_quit, "quit using fileman" },
  73.   { "rename", com_rename, "rename file to newname" },
  74.   { "stat", com_stat, "print out statistics on file" },
  75.   { "view", com_view, "view the contents of file" },
  76.   { (char *)null, (rl_icpfunc_t *)null, (char *)null }
  77. };


  78. char* dupstr(char *s)
  79. {
  80.   char *r;

  81.   r = malloc (strlen (s) 1);
  82.   strcpy(r, s);
  83.   return (r);
  84. }

  85. // clear up white spaces
  86. char* stripwhite (char *string)
  87. {
  88.     register char *s, *t;
  89.     
  90.     for (s = string; whitespace (*s); s)
  91.         ;
  92.     
  93.     if (*s == 0)
  94.         return (s);

  95.     t = s strlen (s) - 1;
  96.     while (t > s && whitespace (*t))
  97.         t--;

  98.     *t = '\0';

  99.     return s;
  100. }


  101. /*
  102. * look up name as the name of a command, and return a pointer to that
  103. * command. return a null pointer if name isn't a command name.
  104. */
  105. command *find_command (char *name)
  106. {
  107.     register int i;
  108.     
  109.     for (i = 0; commands[i].name; i)
  110.         if (strcmp (name, commands[i].name) == 0)
  111.             return (&commands[i]);

  112.     return ((command *)null);
  113. }

  114. /* execute a command line. */
  115. int execute_line (char *line)
  116. {
  117.     register int i;
  118.     command *command;
  119.     char *word;

  120.     /* isolate the command word. */
  121.     i = 0;
  122.     while (line[i] && whitespace (line[i]))
  123.         i;
  124.     word = line i;

  125.     while (line[i] && !whitespace (line[i]))
  126.         i;

  127.     if (line[i])
  128.         line[i] = '\0';

  129.     command = find_command (word);

  130.     if (!command)
  131.     {
  132.         fprintf (stderr, "%s: no such command for fileman.\n", word);
  133.         return (-1);
  134.     }

  135.     /* get argument to command, if any. */
  136.     while (whitespace (line[i]))
  137.         i;

  138.     word = line i;

  139.     /* call the function. */
  140.     return ((*(command->func)) (word));
  141. }

  142. /*
  143. * generator function for command completion. state lets us know whether
  144. * to start from scratch; without any state (i.e. state == 0), then we
  145. * start at the top of the list.
  146. */
  147. char* command_generator (const char *text, int state)
  148. {
  149.     static int list_index, len;
  150.     char *name;

  151.     /*
  152.     * if this is a new word to complete, initialize now. this includes
  153.     * saving the length of text for efficiency, and initializing the index
  154.     * variable to 0.
  155.     */
  156.     if (!state)
  157.     {
  158.          list_index = 0;
  159.         len = strlen (text);
  160.     }

  161.     /* return the next name which partially matches from the command list. */
  162.     while (name = commands[list_index].name)
  163.     {
  164.          list_index;
  165.     
  166.          if (strncmp (name, text, len) == 0)
  167.              return (dupstr(name));
  168.     }

  169.     /* if no names matched, then return null. */
  170.     return ((char *)null);
  171. }

  172. /*
  173. * attempt to complete on the contents of text. start and end bound the
  174. * region of rl_line_buffer that contains the word to complete. text is
  175. * the word to complete. we can use the entire contents of rl_line_buffer
  176. * in case we want to do some simple parsing. return the array of matches,
  177. * or null if there aren't any.
  178. */
  179. char** fileman_completion (const char *text, int start, int end)
  180. {
  181.     char **matches;

  182.     matches = (char **)null;

  183.     /*
  184.     * if this word is at the start of the line, then it is a command
  185.     * to complete. otherwise it is the name of a file in the current
  186.     * directory.
  187.     */
  188.     if (start == 0)
  189.         matches = rl_completion_matches (text, command_generator);

  190.     return (matches);
  191. }


  192. /*
  193. * tell the gnu readline library how to complete. we want to try to complete
  194. * on command names if this is the first word in the line, or on filenames
  195. * if not.
  196. */
  197. void initialize_readline ()
  198. {
  199.     /* allow conditional parsing of the ~/.inputrc file. */
  200.     rl_readline_name = ">";

  201.     /* tell the completer that we want a crack first. */
  202.     rl_attempted_completion_function = fileman_completion;
  203. }




  204. int main (int argc, char **argv)
  205. {
  206.     char *line, *s;
  207.     
  208.     initialize_readline();    /* bind our completer. */


  209.     /* loop reading and executing lines until the user quits. */
  210.     for ( ; ;)
  211.     {
  212.         line = readline (">: ");

  213.         if (!line)
  214.             break;

  215.         /*
  216.         * remove leading and trailing whitespace from the line.
  217.         * then, if there is anything left, add it to the history list
  218.         * and execute it.
  219.         */
  220.          s = stripwhite (line);
  221.          if (*s)
  222.          {
  223.              add_history(s);
  224.              execute_line(s);
  225.          }

  226.         free(line);
  227.     }
  228.     exit(0);
  229. }
    注意,编译的时候需要连接readline库,例如:gcc  -o  irlt  ireadlinetest.c  -lreadline   

    part3: readline下的io复用
    

   但是如果我们有多个 io 要处理,比如既要从一个网络 io 中读数据,又要从标准输入中读取命令,上面的方法就不合适了。为了解决这个问题,我们需要自己用 select 函数监控这两个 io,当它们可读的时候通知这两个模块中的输入函数。形象地说,就是把数据“喂给”这两个模块。这样的模式需要 readline 提供“被动喂给”的工作方式。这种工作方式在 readline 中已有实现。首先,我们需要往 readline 注册回调函数,当 readline 读取到一行后,这个回调函数将被调用:

  rl_callback_handler_install ("prompt> ", handle_command);

接下去,在主事件循环中,我们需要调用 rl_callback_read_char() 通知 readline 去读取一个字符。

下面给一个例子:

点击(此处)折叠或打开

  1. static void handle_command (char *line)
  2. {
  3.     ...
  4. }
  5.  
  6. int
  7. main (int argc, char **argv)
  8. {
  9.     int netfd
  10.     fd_set allfd;
  11.     int maxfd;
  12.  
  13.     netfd = connect_to_server ();
  14.  
  15.  
  16.     fd_zero (&allfd);
  17.     fd_set (fileno(stdin), &allfd);
  18.     fd_set (netfd, &allfd);
  19.     maxfd = netfd;
  20.  
  21.     rl_callback_handler_install ("ccnet> ", handle_command);
  22.  
  23.     while (1) {
  24.         fd_set rfds;
  25.         int retval;
  26.  
  27.         rfds = allfd;
  28.  
  29.         retval = select (maxfd 1, &rfds, null, null, null);
  30.         if (retval < 0)
  31.             perror ("select");
  32.  
  33.         if (fd_isset(0, &rfds))
  34.             rl_callback_read_char();
  35.  
  36.         if (fd_isset(netfd, &rfds))
  37.             read_from_network (netfd);
  38.     }
  39. }

关于readline更多的使用请自行阅读readline的man文件。




阅读(7422) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图