2014-02-19 122 views
-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; 
} 
+0

''提供了22種功能,其中一半以上用於掃描和比較。這不是很多的文件閱讀... – Notlikethat

+0

好的,我會看看他們謝謝。 – dspaces1

+0

你是否試圖閱讀這篇文章?這涉及類似的東西http://stackoverflow.com/questions/9642732/parsing-command-line-arguments – user376507

回答