Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360408
  • 博文数量: 104
  • 博客积分: 2519
  • 博客等级: 少校
  • 技术积分: 1025
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-22 06:54
文章分类

全部博文(104)

文章存档

2009年(90)

2008年(14)

我的朋友

分类: LINUX

2009-03-15 19:42:03

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 , 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 . 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++.

  1. #include   
  2. #include   
  3. #include   
  4. #include   
  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. }  

阅读(1871) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~