-2
我需要一些帮助开始使用shell解析命令行上的元素。我必须采用由空格,单引号和双引号分隔的不同元素,并将它们转换为令牌。任何关于我可以使用什么功能或方法的建议?解析C中的命令行
这是我到目前为止的代码:
#include <stdio.h> //printf
#include <unistd.h> //isatty
#include <string.h> //strlen
int main(int argc, char **argv[]){
int MaxLength = 1024; //size of buffer
char buffer[MaxLength]; //bufer
bzero(buffer,sizeof(buffer)); //zeros out the buffer
char *command; // character pointer of strings
int inloop =1;
/* part 1 isatty */
if (isatty(0))
{
while(inloop ==1) // check if the standard input is from terminal
{
printf("$");
command = fgets(buffer,sizeof(buffer),stdin); //fgets(string of char pointer,size of,input from where
//check for empty space
//check for ''
//check for ""
//check for |
if(strcmp(command,"exit\n")==0)
inloop =0;
}
}
else
printf("the standard input is NOT from a terminal\n");
return 0;
}
''提供了22种功能,其中一半以上用于扫描和比较。这不是很多的文件阅读... –
Notlikethat
好的,我会看看他们谢谢。 – dspaces1
你是否试图阅读这篇文章?这涉及类似的东西http://stackoverflow.com/questions/9642732/parsing-command-line-arguments – user376507