#include <stdio.h>
#include <error.h>
#include <stdlib.h>
#include <string.h>  

#define CMD_LEN 1024    //Maximum length of Command
#define MAX_LEN 1024    //Maximum Length of the input
#define MAX_CMD 2       //Maximum number of Commands
#define ARG_LEN 128     //Maximum Length of Argument
#define ARG_COUNT 64    //Maximum number of arguments

int main(int argc, char *argv[], char *envp[])
{
        while(1){
                char *arg[MAX_CMD][ARG_COUNT];
                char cmd[MAX_LEN];

                char *command[MAX_CMD+1];
                char *pos=&cmd[0];
                int cmd_len;  int pid;  int status;

                printf("\n$ ");           //Displaying the prompt
                fgets(cmd,MAX_LEN,stdin); //Reading user input/
                cmd_len=strnlen(cmd,MAX_LEN);

                int i=0,k;
                do{
                        int len;
                        char a[CMD_LEN];
                        sscanf(pos,"%[^|]",a);//Parsing out the commands
                        len=strnlen(a,CMD_LEN);

                        command[i]=malloc(len);
                        if(!command[i]){
                                printf("Unable to allocate memory\n");
                                exit(1);
                        }

                        strncpy(command[i++],a,CMD_LEN);
                        pos+=(len+1);    //Repositioning for next command
                }while(pos<cmd+cmd_len);

                command[i]=NULL;

                i=0;

                while(command[i]){
                        pos=command[i];
                        int arg_count=0;
                        cmd_len=strnlen(command[i],CMD_LEN);

                        do{
                                int len;
                                char a[ARG_LEN];
                                int count=sscanf(pos,"%s",a);

                                //Parsing out the arguments
                                if(count < 1)
                                        break;

                                len=strnlen(a,ARG_LEN);

                                arg[i][arg_count]=malloc(len);
                                if(!arg[i][arg_count]){
                                        printf("Unable to allocate memory\n");
                                        exit(1);
                                }

                                strncpy(arg[i][arg_count++],a,ARG_LEN);
                                pos+=(len+1);  //Repositioning for next argument
                        }while(pos<command[i]+cmd_len);

                        arg[i][arg_count]=NULL;   i++;
                }

                if(i!=2){
                        printf("Two commands required");
                        continue;
                }

                //Obtained the arguments

                pid=fork();                     //Creating a process to execute command 2

                if(pid==0){
                        int fd[2];
                        if(pipe(fd)==-1){       //Creating a Pipe
                                perror("pipe:");
                                exit(1);
                        }

                        pid=fork();             //Creating process to execute command 1

                        if(pid==0){
                                close(fd[0]);
                                close(1);        //closing stdout

                                if(dup(fd[1])==-1){      //stdout now points to fd[1]
                                        perror("dup");
                                        exit(1);
                                }

                                //In child process
                                if(execve(arg[0][0],&arg[0][0],envp)==-1){
                                        perror("execve");
                                }

                                exit(1); //Reached if Child failed to execute process
                        }

                        else{
                                close(fd[1]);//close the write end
                                close(0);//closing the stdin

                                if(dup(fd[0])==-1){//stdin now points to fd[0]
                                        perror("dup:");
                                        exit(1);
                                }

                                //In parent process
                                if(execve(arg[1][0],&arg[1][0],envp)==-1){
                                        perror("execve");
                                }
                                exit(1); //Reached if failed to execute process
                        }
                }
                wait(&status);    //Wait for process to terminate //continue
        }
        return 0;
}


