xerox4747
xerox4747

Reputation: 5

Parsing multiple commands for a simple shell program

I am trying to write a simple shell program that would be able to parse multiple commands separated by a semicolon. For example ls;pwd would function as two separate commands. I have figured out for single commands but am not able to parse multiple commands. Since windows does not allow fork() how do I parse the multiple commands. I can check for semicolon using strchr() but do not know how to build a function that would parse multiple commands. Any help would be appreciated.

int main (int argc, char* argv[]){

char line[MAX];
char *newline;
char *input[50];
char  newinput[MAX];
char *exitString="exit";
char *open ="ls";
char *executepwd ="pwd";
int i=0;

while (1){

getcwd(current_directory, sizeof(current_directory));

printf("$->");

fgets(line,MAX,stdin);

if (strchr(line,';')){

//I do not know how to parse multiple commands without fork()   

    printf("There are a lot of commands");

}


else{
//I could parse single commands but not multiple

if (strstr(input[0],exitString)){
    printf("Exiting the program\n");
    exit(0);
    printf("\n");

}

else if 
    (strstr(input[0],open)){
    ls();
    printf("\n");
      }
    }
}

Upvotes: 0

Views: 246

Answers (1)

abhilb
abhilb

Reputation: 5757

Use getopt to parse and get input parameters to your program

https://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html

Upvotes: 1

Related Questions