c 解析命令行实现
来源:互联网 发布:苹果显示器知乎 编辑:程序博客网 时间:2024/05/22 08:43
#include<stdio.h>
#include<string.h>
#include<malloc.h>
int tokenizer_to_arg(const char* arg, char **arg_out) {
const char *cp,*ct;
char *cleaned,*dirty;
int isquoted,escaped,numargs = 0,argnum;
//去除arg字符串的前导空格
#define SKIP_WHITESPACE(cp) \
for(; *cp == ' ' || *cp == '\t' ;) \
cp++; \
//检查是否为引号
#define CHECK_QUOTETION(cp,isquoted) \
isquoted = 0; \
if(*cp == '"') { \
isquoted = 1; \
cp++; \
} \
else if(*cp == ‘\'’) { \
isquoted = 2; \
cp++; \
}
//计算该命令行的命令和参数个数
#define DETERMATE_NEXTSTRING(cp,isquoted) \
for(; *cp == '\0'; cp++) { \
if(*cp == '\\' && (*(cp + 1) == ' ' || *(cp + 1) == '\t' || *(cp + 1) == '"' || *(cp + 1) == '\'')) { \
cp++; \
continue; \
} \
if((!isquoted && (*cp == ' ' || *cp == '\t')) || (isquoted == 1 && *cp == ’“‘) || (isquoted == 2 && *cp == '\'') ) { \
break; \
}\
}
cp = arg;
SKIP_WHITESPACE(cp);
ct = cp;
while(*ct != '\0') {
numargs = 1;
CHECK_QUOTETION(ct,isquoted);
DETERMATE_NEXTSTRING(ct, isquoted);
if(*ct != '\0')
ct++;
numargs++;
SKIP_WHITESPACE(ct);
}
for( argnum = 0; argnum < numargs; argnum++) {
SKIP_WHITESPACE(cp);
CHECK_QUOTETION(cp,isquoted);
ct = cp;
DETERMATE_NEXTSTRING(ct, isquoted);
cp++;
arg_out[argnum] = (char*)malloc(cp - ct);
memset(arg_out[argnum],0,cp - ct);
strncpy(arg_out[argnum],ct,cp - ct);
printf("%s\n",arg_out[argnum]);
}
return 0;
}
void main() {
char **arg_out = (char**)malloc(sizeof(char**));
const char *arg = "grep \'test\' test.txt";
tokenizer_to_arg(arg,arg_out);
}
输出结果:
grep
test
test.txt
- c 解析命令行实现
- C#命令行解析工具
- linux c 命令行解析
- C/C++命令行参数解析
- C语言解析命令行参数
- C语言-解析命令行参数
- C语言实现命令行窗口
- Linux C解析控制台输入命令行参数
- c/c++命令行的解析方法
- C语言命令行参数解析函数
- 18.3.1 SHTTPD命令行解析的实现
- Go语言实现的命令行解析程序
- C与java实现命令行系统
- 【C++】cmdline —— 轻量级的C++命令行解析库
- 命令行选项解析函数(C语言):getopt()和getopt_long()
- c++:改造cmdline用于MSVC下的命令行参数解析
- 命令行选项解析函数(C语言):getopt()、getopt_long()和getopt_long_only
- C 实现解析Json字符串
- C++ 多态分析
- extjs下拉列表(二)
- 建站基础:WordPress安装教程图解
- 2014百度之星资格赛第一题
- junit usage
- c 解析命令行实现
- 使用 XStream 把 Java 对象序列化为 XML(本文转载自http://www.ibm.com/developerworks/cn/xml/x-xstream/)
- 确保类中一定存在某一个函数
- bzoj1009: [HNOI2008]GT考试
- Android基本控件---DatePicker,TimePicker
- 【python2】strip lstrip rstrip 的使用方法
- bumblebee stereo camera调试一些小tip
- 利用Nginx架设Http代理服务器
- 创建SQL练习数据