Thread: Command Interpreter Suggestion

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Command Interpreter Suggestion

    I was working on a C program command interpreter where I have to deal with fork and other commands.
    I wrote the program and also initiated the run command in it.
    Now I am not being able to initiate a copy function in it.
    So, please help me with this.
    I need some tips on how to get started.

    here is what I need to do?
    COPY command takes two additional arguments, the source(file to be copied) and the destination(file to be created).
    The syntax is
    COPY source destination

    Algorithm is:
    Open the source file for input
    Open the destination file for output
    For each character in the source file
    Read the character from the source
    Write the character to destination
    Close the source file
    Close the destination file.
    Code:
    #include <iostream>
    #include <cstring>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    
    using namespace std;
    
    void parse(char List[][100], int &iLength);
    void CMDS(char List[][100], int &iLength);
    void run(char List[][100],int &iLength);
    
    //Pre: User enters a valid command for the input, the number of strings should 
    //     up to 30, the number of characters in each string should be no more than 100
    //Post: Parse and CMDS function returns and the valid command is executed.
    
    // main funtion
    int main()
    {
        // creates a 2 dimentional array of 30 columns and each column is 100 chars
        // creates a Int that will be used to count the number of arrays
        char List[30][100];
        int iLength;
        // do while loop to run program until user enters quit
        do
        {
            // this is the funtion call to do the work, it passes the List and Length of arrays
            parse(List, iLength);
            CMDS(List, iLength);
            if((strcmp(List[0], "quit") == 0) || (strcmp(List[0],"QUIT") == 0))
            {
                break;
            }
        }while (true);
        return 0;
    }
    // starts the function
    void parse(char List[][100],int &iLength)
    {
        // initializes variables that are going to be used in function
        iLength = 0;
        int i = 0;
        char ch2= ' ';
        char TempArray[100];
        cout<<endl;
        // give instructions to user, asks them to enter input
        cout << "CMD:";
        // do this while the input is not a new line
        while(ch2 != '\n')
        {
            // if the character is whitespace then it gets next character and doesnt count it as part of array
            if(isspace(ch2))
            {
                cin.get(ch2);
            }
            // iff the character is NOT whitespace then it will take character and add it to the TempArray, and get next character
            while(!isspace(ch2))
            {
                TempArray[i] = ch2;
                i++;
                cin.get(ch2);
                // if the next character is whitespace then it must be the end of a string, so we add the null \0 to it,
                // and strcpy it into the List of arrays, reset the TempArray[i] to [0] and increments the number for arrays
                if (isspace(ch2))
                {
                    TempArray[i] = '\0';
                    strcpy(List[iLength],TempArray);
                    i = 0;
                    iLength++;
                }
            }
        }
    }
    void CMDS(char List[][100],int &iLength)
    {
    	//if((strcmp(List[0], "RUN") == 0) && iLength < 2 )
            if(((strcmp(List[0], "RUN") == 0) || (strcmp(List[0], "run") == 0)) && iLength < 2)
            {
                cout << "too few parameters for RUN cmd"  << endl;
                return;
            }
            
            //if((strcmp(List[0], "RUN") == 0) && iLength > 2 )
            if(((strcmp(List[0], "RUN") == 0) || (strcmp(List[0], "run") == 0)) && iLength > 2)
            {
                cout << "too many parameters for RUN cmd"  << endl;
                return;
            }
            
            //if((strcmp(List[0], "RUN") == 0) && iLength == 2 )
            if(((strcmp(List[0], "RUN") == 0) || (strcmp(List[0], "run") == 0)) && iLength == 2)
            {
                cout <<List[0]<<"ning "<< List[1]  << endl;
    	    run(List, iLength);
                return;
            }
            
            //if((strcmp(List[0], "LIST") == 0) && iLength < 2)
            if(((strcmp(List[0], "LIST") == 0) || (strcmp(List[0], "list") == 0)) && iLength < 2)
            {
                cout <<List[0]<<"ing the files in this directory."  << endl;
                return;
            }
            
            //if((strcmp(List[0], "LIST") == 0) && iLength > 2)
            if(((strcmp(List[0], "LIST") == 0) || (strcmp(List[0], "list") == 0)) && iLength > 2)
            {
                cout << "too many parameters for LIST cmd"  << endl;
                return;
            }
            
            //if((strcmp(List[0], "LIST") == 0) && iLength == 2)
            if(((strcmp(List[0], "LIST") == 0) || (strcmp(List[0], "list") == 0)) && iLength == 2)
            {
                cout <<List[0]<<"ing the files in "<< List[1]  << endl;
                return;
            }
            
            //if((strcmp(List[0], "COPY") == 0) && iLength < 3)
            if(((strcmp(List[0], "COPY") == 0) || (strcmp(List[0], "copy") == 0)) && iLength < 3)
            {
                cout << "too few parameters for COPY cmd"  << endl;
                return;
            }
            
            //if((strcmp(List[0], "COPY") == 0) && iLength > 3)
            if(((strcmp(List[0], "COPY") == 0) || (strcmp(List[0], "copy") == 0)) && iLength > 3)
            {
                cout << "too many parameters for COPY cmd"  << endl;
                return;
            }
            
            //if((strcmp(List[0], "COPY") == 0) && iLength == 3)
            if(((strcmp(List[0], "COPY") == 0) || (strcmp(List[0], "copy") == 0)) && iLength == 3)
            {
                cout <<List[0]<<"ing "<< List[1] << " and creating " << List[2] << endl;
                copy(List, iLength);
                return;
            }
            
            if((strcmp(List[0], "HELP") == 0) || (strcmp(List[0],"help") == 0))
            {
                cout << "Valid Commands are :"  << endl;
                cout << " RUN filename"  << endl;
                cout << " LIST"  << endl;
                cout << " LIST directory"  << endl;
                cout << " COPY old-filename new-filename"  << endl;
                cout << " HELP"  << endl;
                cout << " QUIT"  << endl;
                return;
               
            }
            // this performs the string compare to quit or QUIT and exits if needed
            if((strcmp(List[0], "quit") == 0) || (strcmp(List[0],"QUIT") == 0))
            {
                return;
            }
            // otherwise the command entered is not recognized.
            else
            {
                cout << "Invalid command - "<< List[0] << " Command not recognized" << endl;
            }
    }
    
    void copy(char List[][100],int &iLength)
    {
           // Make sure the file exists.
           ifstream fin;
           fin.open(List[1]);
           if (fin.fail())
           {
                   cout << "File '" << List[1] << "' does not exist.\n";
                   return;
           }
           // See if the dest file already exists
           ifstream temp;
           temp.open(List[2]);
           if (!temp.fail())
           {
                   temp.close();
                   cout << "The file '" << List[2] << "' already exists.\n"
                           << "Do you want to over-write this file (y/n)? ";
                   char choice;
                   cin >> choice;
                   if (choice != 'y' && choice != 'Y')
                   {
                   	       return;
                   }
           }
           // Copy each char from fin into fout
           ofstream fout;
           fout.open(List[2]);
           char c = fin.get();
           while (!fin.eof())
           {
                   fout << c;
                   c = fin.get();
           }
    
           fin.close();
           fout.close();
    
           cout << "\nDone.\n";
           return;
    }
    
    //initiate the run command
    void run(char List[][100],int &iLength)
    {
    	pid_t pid;
            //Initiate a fork
    	pid = fork();
            //Check if the pid is less than zero or not
    	if(pid < 0)
    	{
    		fprintf(stderr, "Fork Failed");
    		return;
    	}
            //If pid is zero, then execute run
            else if (pid==0)
    	{
    		execl(List[1], List[1], NULL);
    	}
            else
    	{
    		wait(NULL);
    	}
            //Return to the start
    	return;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So do you get as far as say seeing this printed?

    cout <<List[0]<<"ing "<< List[1] << " and creating " << List[2] << endl;


    Do you get an output file, but for some reason it isn't a copy (too long, too short, compares different)?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by Salem View Post
    So do you get as far as say seeing this printed?

    cout <<List[0]<<"ing "<< List[1] << " and creating " << List[2] << endl;


    Do you get an output file, but for some reason it isn't a copy (too long, too short, compares different)?
    I just did changes and the codes look good but I am missing something which is giving me a hard time to compile. Can you help me fix that?
    I am done with the code for copyin.
    Code:
    
    #include <iostream>
    #include <cstring>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    
    using namespace std;
    
    void parse(char List[][100], int &iLength);
    void CMDS(char List[][100], int &iLength);
    void run(char List[][100],int &iLength);
    void COPY(string oldFname[][100], string newFname;)
    
    //Pre: User enters a valid command for the input, the number of strings should
    //     up to 30, the number of characters in each string should be no more than 100
    //Post: Parse and CMDS function returns and the valid command is executed.
    
    // main funtion
    int main()
    {
        // creates a 2 dimentional array of 30 columns and each column is 100 chars
        // creates a Int that will be used to count the number of arrays
        char List[30][100];
        int iLength;
        // do while loop to run program until user enters quit
        do
        {
            // this is the funtion call to do the work, it passes the List and Length of arrays
            parse(List, iLength);
            CMDS(List, iLength);
            if((strcmp(List[0], "quit") == 0) || (strcmp(List[0],"QUIT") == 0))
            {
                break;
            }
        }while (true);
        return 0;
    }
    // starts the function
    void parse(char List[][100],int &iLength)
    {
        // initializes variables that are going to be used in function
        iLength = 0;
        int i = 0;
        char ch2= ' ';
        char TempArray[100];
        cout<<endl;
        // give instructions to user, asks them to enter input
        cout << "CMD:";
        // do this while the input is not a new line
        while(ch2 != '\n')
        {
            // if the character is whitespace then it gets next character and doesnt count it as part of array
            if(isspace(ch2))
            {
                cin.get(ch2);
            }
            // iff the character is NOT whitespace then it will take character and add it to the TempArray, and get next character
            while(!isspace(ch2))
            {
                TempArray[i] = ch2;
                i++;
                cin.get(ch2);
                // if the next character is whitespace then it must be the end of a string, so we add the null \0 to it,
                // and strcpy it into the List of arrays, reset the TempArray[i] to [0] and increments the number for arrays
                if (isspace(ch2))
                {
                    TempArray[i] = '\0';
                    strcpy(List[iLength],TempArray);
                    i = 0;
                    iLength++;
                }
            }
        }
    }
    void CMDS(char List[][100],int &iLength)
    {
    	//if((strcmp(List[0], "RUN") == 0) && iLength < 2 )
            if(((strcmp(List[0], "RUN") == 0) || (strcmp(List[0], "run") == 0)) && iLength < 2)
            {
                cout << "too few parameters for RUN cmd"  << endl;
                return;
            }
            
            //if((strcmp(List[0], "RUN") == 0) && iLength > 2 )
            if(((strcmp(List[0], "RUN") == 0) || (strcmp(List[0], "run") == 0)) && iLength > 2)
            {
                cout << "too many parameters for RUN cmd"  << endl;
                return;
            }
            
            //if((strcmp(List[0], "RUN") == 0) && iLength == 2 )
            if(((strcmp(List[0], "RUN") == 0) || (strcmp(List[0], "run") == 0)) && iLength == 2)
            {
                cout <<List[0]<<"ning "<< List[1]  << endl;
    	    run(List, iLength);
                return;
            }
            
            //if((strcmp(List[0], "LIST") == 0) && iLength < 2)
            if(((strcmp(List[0], "LIST") == 0) || (strcmp(List[0], "list") == 0)) && iLength < 2)
            {
                cout <<List[0]<<"ing the files in this directory."  << endl;
                return;
            }
            
            //if((strcmp(List[0], "LIST") == 0) && iLength > 2)
            if(((strcmp(List[0], "LIST") == 0) || (strcmp(List[0], "list") == 0)) && iLength > 2)
            {
                cout << "too many parameters for LIST cmd"  << endl;
                return;
            }
            
            //if((strcmp(List[0], "LIST") == 0) && iLength == 2)
            if(((strcmp(List[0], "LIST") == 0) || (strcmp(List[0], "list") == 0)) && iLength == 2)
            {
                cout <<List[0]<<"ing the files in "<< List[1]  << endl;
                return;
            }
            
            //if((strcmp(List[0], "COPY") == 0) && iLength < 3)
            if(((strcmp(List[0], "COPY") == 0) || (strcmp(List[0], "copy") == 0)) && iLength < 3)
            {
                cout << "too few parameters for COPY cmd"  << endl;
                return;
            }
            
            //if((strcmp(List[0], "COPY") == 0) && iLength > 3)
            if(((strcmp(List[0], "COPY") == 0) || (strcmp(List[0], "copy") == 0)) && iLength > 3)
            {
                cout << "too many parameters for COPY cmd"  << endl;
                return;
            }
            
            //if((strcmp(List[0], "COPY") == 0) && iLength == 3)
            if(((strcmp(List[0], "COPY") == 0) || (strcmp(List[0], "copy") == 0)) && iLength == 3)
            {
                cout <<List[0]<<"ing "<< List[1] << " and creating " << List[2] << endl;
                copy(List, iLength);
                return;
            }
            
            if((strcmp(List[0], "HELP") == 0) || (strcmp(List[0],"help") == 0))
            {
                cout << "Valid Commands are :"  << endl;
                cout << " RUN filename"  << endl;
                cout << " LIST"  << endl;
                cout << " LIST directory"  << endl;
                cout << " COPY old-filename new-filename"  << endl;
                cout << " HELP"  << endl;
                cout << " QUIT"  << endl;
                return;
               
            }
            // this performs the string compare to quit or QUIT and exits if needed
            if((strcmp(List[0], "quit") == 0) || (strcmp(List[0],"QUIT") == 0))
            {
                return;
            }
            // otherwise the command entered is not recognized.
            else
            {
                cout << "Invalid command - "<< List[0] << " Command not recognized" << endl;
            }
    }
    
    
    //initiate the run command
    void run(char List[][100],int &iLength)
    {
    	pid_t pid;
            //Initiate a fork
    	pid = fork();
            //Check if the pid is less than zero or not
    	if(pid < 0)
    	{
    		fprintf(stderr, "Fork Failed");
    		return;
    	}
            //If pid is zero, then execute run
            else if (pid==0)
    	{
    		execl(List[1], List[1], NULL);
    	}
            else
    	{
    		wait(NULL);
    	}
            //Return to the start
    	return;
    }
    
    //Initiate the copy command
    void COPY(string oldFname, string newFname)
    {       cout << "Copying " << oldFname << " to " << newFname <<endl;
        
        
    //Converting a string to cstring
        char *copyFile =(char*)oldFname.c_str();
        char *pasteFile=(char*)newFname.c_str();
    
    //Declaring the readers and writers
        ifstream in;
        ofstream out;
        string input;
        char c;
    
    //Opening the file to read
        in.open(copyFile);
    
    //If condition if the open fails
        if(in.fail())
        {
            cout<< "The system couldn't find the file"<<endl;
    
        }
    //If condition if the open is successful
        else
        {
            ifstream outTest;
    //Checking if the file already exist
            outTest.open(pasteFile);
        if(outTest.fail())
        {
            cout<< "Creating a new file"<<endl;
            out.open(pasteFile);
    
    //Copying from a oldfile to a newfile
        while (!in.eof())
        {
            c=(char)in.get();
            out.put(c);
                    }
                    out.close();
                    outTest.close();
    
            }
    
        else
    // If the file being copied already exists, system asks user to overwrite
            {
                cout<<"The file is already there!\n Are you sure you want to overwrite it.\n Enter y to overwrite it."<<endl;
                cin>>input;
                cin.ignore(200,'\n');
    
            if(input.compare("y")==0)
            {
                out.open(pasteFile);
                while (!in.eof())
                    {
                    c=(char)in.get();
                    out.put(c);
                    }
                    out.close();
                    outTest.close();
                }
                else
                {
                    cout<<"The copy function is cancelled"<<endl;
                }
            }
    //File close
            in.close();
    
        }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post your error complete messages.

    Jim

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    I have attached the screenshot of the error.

    ImageShack® - Online Photo and Video Hosting

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What operating system are you using? What compiler are you using?

    Jim

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    E2209 (for example) appears to be a Borland (hence DOS/Windows) error message.

    Which makes almost no sense at all, since the OP is clearly trying to write a POSIX program, and would therefore be missing a whole raft of header files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help getting started with a Command Line Interpreter:
    By enzorug in forum C++ Programming
    Replies: 0
    Last Post: 11-15-2010, 10:29 AM
  2. Creation of a Command Language Interpreter
    By Sicilian_10 in forum C Programming
    Replies: 18
    Last Post: 04-16-2010, 06:36 AM
  3. Unix Command Interpreter Help
    By ashford74 in forum C Programming
    Replies: 14
    Last Post: 09-26-2008, 03:28 AM
  4. Interpreter?
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 06-10-2005, 12:15 PM
  5. C CGI Interpreter
    By johnc in forum C Programming
    Replies: 5
    Last Post: 07-19-2002, 12:56 PM