文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>GNU readline

GNU readline

时间:2009-03-15  来源:yanghoo

GNU readline implement filename auto-complete by default, it will list all the files in the current directory. We can disable it by binds our TAB key to some other operation. In previous post, I simply abort the operation to ignore users hitting TABs.

Auto-complete are useful if only we can customize it. Well, readline allows us do it by assign our own callback functions. First of all, you may want to read up the manual from HERE. It does provides a c sample codes as well but I find it too complicated, here I provide a simplified version that can be compiled under c++.

view plainprint?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <readline/readline.h>  
  4. #include <readline/history.h>  
  5.   
  6. static char** my_completion(const char*, int ,int);  
  7. char* my_generator(const char*,int);  
  8. char * dupstr (char*);  
  9. void *xmalloc (int);  
  10.   
  11. char* cmd [] ={ "hello", "world", "hell" ,"word", "quit", " " };  
  12.   
  13. int main()  
  14. {  
  15.     char *buf;  
  16.   
  17.     rl_attempted_completion_function = my_completion;  
  18.   
  19.     while((buf = readline("\n >> "))!=NULL) {  
  20.         //enable auto-complete  
  21.         rl_bind_key('\t',rl_complete);  
  22.   
  23.         printf("cmd [%s]\n",buf);  
  24.         if (strcmp(buf,"quit")==0)  
  25.             break;  
  26.         if (buf[0]!=0)  
  27.             add_history(buf);  
  28.     }  
  29.   
  30.     free(buf);  
  31.   
  32.     return 0;  
  33. }  
  34.   
  35.   
  36. static char** my_completion( const char * text , int start,  int end)  
  37. {  
  38.     char **matches;  
  39.   
  40.     matches = (char **)NULL;  
  41.   
  42.     if (start == 0)  
  43.         matches = rl_completion_matches ((char*)text, &my_generator);  
  44.     else  
  45.         rl_bind_key('\t',rl_abort);  
  46.   
  47.     return (matches);  
  48.   
  49. }  
  50.   
  51. char* my_generator(const char* text, int state)  
  52. {  
  53.     static int list_index, len;  
  54.     char *name;  
  55.   
  56.     if (!state) {  
  57.         list_index = 0;  
  58.         len = strlen (text);  
  59.     }  
  60.     
  61.     while (name = cmd[list_index]) {  
  62.         list_index++;  
  63.   
  64.         if (strncmp (name, text, len) == 0)  
  65.             return (dupstr(name));  
  66.     }  
  67.   
  68.     /* If no names matched, then return NULL. */  
  69.     return ((char *)NULL);  
  70.   
  71. }  
  72.   
  73. char * dupstr (char* s) {  
  74.   char *r;  
  75.   
  76.   r = (char*) xmalloc ((strlen (s) + 1));  
  77.   strcpy (r, s);  
  78.   return (r);  
  79. }  
  80.   
  81. void * xmalloc (int size)  
  82. {  
  83.     void *buf;  
  84.   
  85.     buf = malloc (size);  
  86.     if (!buf) {  
  87.         fprintf (stderr, "Error: Out of memory. Exiting.'n");  
  88.         exit (1);  
  89.     }  
  90.   
  91.     return buf;  
  92. }  
  93. http://www.math.utah.edu/docs/info/rlman_2.html#SEC36
排行榜 更多 +
开局一个小兵最新版

开局一个小兵最新版

休闲益智 下载
火柴人联盟2腾讯qq登录版

火柴人联盟2腾讯qq登录版

体育竞技 下载
tsuki odyssey游戏(月兔冒险奥德赛)

tsuki odyssey游戏(月兔冒险奥德赛)

休闲益智 下载