readline 命令行程序

来源:互联网 发布:傲剑护身符升级数据 编辑:程序博客网 时间:2024/05/22 12:37

readline 命令行程序

一、readline简介

readline 是一个强大的库,只要使用了它的程序,都可以用同一个配置文件配置,而且用同样的方法操作命令行,让你可以方便的编辑命令行。

readline 方法:

从 命令行中获取一整行(一直到换行符,但不包括换行符),并返回由此得到的字符串。

 

二、示例代码

[cpp] view plaincopyprint?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <stdint.h>  
  4. #include <sys/types.h>  
  5. #include <sys/stat.h>  
  6. #include <fcntl.h>  
  7. #include <sys/ioctl.h>  
  8. #include <string.h>  
  9. #include <sys/mman.h>  
  10. #include <sys/select.h>  
  11. #include <sys/socket.h>  
  12. #include <netinet/in.h>  
  13. #include <arpa/inet.h>  
  14. #include <netdb.h>  
  15. #include <unistd.h>  
  16. #include <pthread.h>  
  17. #include <sys/resource.h>  
  18. #include <sys/stat.h>  
  19. #include <sys/time.h>  
  20. #include <sys/poll.h>  
  21. #include <getopt.h>  
  22. #include <readline/readline.h>  
  23. #include <readline/history.h>  
  24.   
  25.   
  26. #define READLINE_PROMPT "command->"  
  27. #define MAX_COMMAND_LINE_LENGTH 1024  
  28.   
  29. int print_help()  
  30. {  
  31.     printf("option command... \n\  
  32.                               \n");  
  33.     return 0;  
  34. }  
  35.   
  36. int is_command_line(char *command)  
  37. {  
  38.     char *p = command;  
  39.       
  40.     if (p == NULL)  
  41.     {  
  42.         return 0;  
  43.     }  
  44.   
  45.     while (*p == ' ' || *p == '\t' || *p == '\n')  
  46.     {  
  47.         p++;  
  48.     }  
  49.   
  50.     if (*p == '#' || *p == 0)  
  51.     {  
  52.         return 0;  
  53.     }  
  54.     else  
  55.     {  
  56.         return 1;  
  57.     }  
  58. }  
  59.   
  60. int command_handler(char *pCommandLine)  
  61. {  
  62.     int Ret = 0;  
  63.     char Param[20][30];  
  64.     char *pToken = NULL;  
  65.     int i =0;  
  66.     int Address = 0;  
  67.     int Value = 0;  
  68.   
  69.     if (0 == is_command_line(pCommandLine))  
  70.     {  
  71.         printf("invalid command or parameter\n ");  
  72.         print_help();  
  73.         return 1;  
  74.     }  
  75.   
  76.     pToken = strtok(pCommandLine, " \t\n\r");  
  77.     for(i =0; pToken != NULL&& i<16;i++ )  
  78.     {  
  79.         if((strlen(pToken) > 28) || (*pToken == '\0'))  
  80.         {  
  81.             printf("invalid command or parameter\n ");  
  82.             print_help();  
  83.             return 0;  
  84.         }  
  85.         strcpy(Param[i],pToken);  
  86.         pToken = strtok(NULL," \t\n\r");  
  87.     }  
  88.   
  89.     if(0 == strcmp(Param[0],"read"))  
  90.     {  
  91.         if(0 == strcmp(Param[1],"\0"))  
  92.         {  
  93.             printf("invalid conmmand or parameter\n");  
  94.             return -1;  
  95.         }  
  96.   
  97.         Address = strtol(Param[1],NULL,16);  
  98.         Ret = xxxx_read(Address,&Value);  
  99.         if(Ret != 0)  
  100.         {  
  101.             printf("xxxx read eroor\n");  
  102.             return -1;  
  103.         }  
  104.         printf("xxxx read 0x%x , 0x%x\n", Address, Value);  
  105.     }  
  106.     else if(0 == strcmp(Param[0],"q"))  
  107.     {  
  108.         exit(0);  
  109.     }  
  110.     else  
  111.     {  
  112.         printf("invalid command or parameter\n ");  
  113.         print_help();  
  114.         return -1;  
  115.     }  
  116.     return 0;  
  117. }  
  118.   
  119. int main()  
  120. {  
  121.     char *pReadLine = NULL;  
  122.     char CommandLine[MAX_COMMAND_LINE_LENGTH] = {0};  
  123.     int Ret = -1;  
  124.   
  125.     print_help();  
  126.   
  127.     while(1)  
  128.     {  
  129.         pReadLine = readline(READLINE_PROMPT);  
  130.         if (pReadLine && *pReadLine && strlen(pReadLine) < MAX_COMMAND_LINE_LENGTH - 1)  
  131.         {  
  132.             strcpy(CommandLine, pReadLine);  
  133.         }  
  134.         else if (strlen(pReadLine) >= MAX_COMMAND_LINE_LENGTH - 1)  
  135.         {  
  136.             printf("   -- Error: Too long command line!\n");  
  137.             continue;  
  138.         }  
  139.   
  140.         Ret = command_handler(CommandLine);  
  141.         if (0 == Ret)  
  142.         {  
  143.             add_history(pReadLine);  
  144.   
  145.             if (pReadLine != NULL)  
  146.             {  
  147.                 free(pReadLine);  
  148.                 pReadLine = NULL;  
  149.                 CommandLine[0] = 0;  
  150.             }  
  151.         }  
  152.     }  
  153.   
  154.     return 0;  
  155. }  

原文链接: http://blog.csdn.net/tchonggang77/article/details/7366274

原创粉丝点击