readline 命令行程序
来源:互联网 发布:傲剑护身符升级数据 编辑:程序博客网 时间:2024/05/22 12:37
readline 命令行程序
一、readline简介
readline 是一个强大的库,只要使用了它的程序,都可以用同一个配置文件配置,而且用同样的方法操作命令行,让你可以方便的编辑命令行。
readline 方法:
从 命令行中获取一整行(一直到换行符,但不包括换行符),并返回由此得到的字符串。
二、示例代码
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <string.h>
- #include <sys/mman.h>
- #include <sys/select.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <sys/resource.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #include <sys/poll.h>
- #include <getopt.h>
- #include <readline/readline.h>
- #include <readline/history.h>
- #define READLINE_PROMPT "command->"
- #define MAX_COMMAND_LINE_LENGTH 1024
- int print_help()
- {
- printf("option command... \n\
- \n");
- return 0;
- }
- int is_command_line(char *command)
- {
- char *p = command;
- if (p == NULL)
- {
- return 0;
- }
- while (*p == ' ' || *p == '\t' || *p == '\n')
- {
- p++;
- }
- if (*p == '#' || *p == 0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- int command_handler(char *pCommandLine)
- {
- int Ret = 0;
- char Param[20][30];
- char *pToken = NULL;
- int i =0;
- int Address = 0;
- int Value = 0;
- if (0 == is_command_line(pCommandLine))
- {
- printf("invalid command or parameter\n ");
- print_help();
- return 1;
- }
- pToken = strtok(pCommandLine, " \t\n\r");
- for(i =0; pToken != NULL&& i<16;i++ )
- {
- if((strlen(pToken) > 28) || (*pToken == '\0'))
- {
- printf("invalid command or parameter\n ");
- print_help();
- return 0;
- }
- strcpy(Param[i],pToken);
- pToken = strtok(NULL," \t\n\r");
- }
- if(0 == strcmp(Param[0],"read"))
- {
- if(0 == strcmp(Param[1],"\0"))
- {
- printf("invalid conmmand or parameter\n");
- return -1;
- }
- Address = strtol(Param[1],NULL,16);
- Ret = xxxx_read(Address,&Value);
- if(Ret != 0)
- {
- printf("xxxx read eroor\n");
- return -1;
- }
- printf("xxxx read 0x%x , 0x%x\n", Address, Value);
- }
- else if(0 == strcmp(Param[0],"q"))
- {
- exit(0);
- }
- else
- {
- printf("invalid command or parameter\n ");
- print_help();
- return -1;
- }
- return 0;
- }
- int main()
- {
- char *pReadLine = NULL;
- char CommandLine[MAX_COMMAND_LINE_LENGTH] = {0};
- int Ret = -1;
- print_help();
- while(1)
- {
- pReadLine = readline(READLINE_PROMPT);
- if (pReadLine && *pReadLine && strlen(pReadLine) < MAX_COMMAND_LINE_LENGTH - 1)
- {
- strcpy(CommandLine, pReadLine);
- }
- else if (strlen(pReadLine) >= MAX_COMMAND_LINE_LENGTH - 1)
- {
- printf(" -- Error: Too long command line!\n");
- continue;
- }
- Ret = command_handler(CommandLine);
- if (0 == Ret)
- {
- add_history(pReadLine);
- if (pReadLine != NULL)
- {
- free(pReadLine);
- pReadLine = NULL;
- CommandLine[0] = 0;
- }
- }
- }
- return 0;
- }
原文链接: http://blog.csdn.net/tchonggang77/article/details/7366274
- readline 命令行程序
- readline 命令行程序
- readline()
- readLine
- readLine
- readline
- 用readline巧妙增强mysql等linux命令行客户端
- Nodejs进阶:readline实现日志分析+简易命令行工具
- 基于readline库,实现命令行补全和记录
- 命令行程序
- 命令行参数,程序参数
- 针对命令行程序
- 如何开发命令行程序
- 通过命令行启动程序
- perl 命令行程序
- 命令行编译C程序
- SQLite命令行程序说明
- 命令行执行java程序
- Jack's Notes9——关于窗体间传值(用三层实现TreeView)
- js没权限
- 为何我喜爱读他人的代码,而你也应该去喜爱它
- 杂感系列之一--C++杂感
- 工作3年零2天
- readline 命令行程序
- 黑马程序员_学习日记53_625三层项目(FTP相关操作、递归TreeView、三层结构)
- 《那些年啊,那些事——一个程序员的奋斗史》——110
- 数据挖掘主要解决的四类问题
- 在数据库中处理image字段【把图片存储到数据库并显示出来】
- [Script]Re-Compile All Invalid Package/Package Body
- Sunday算法
- End Point URL
- Learn Objective‐C