Handout 1, CS 133F “Introduction to Fortran Programming” HW 5 HomeWork 5, Command Line interpreter General rules: Create homework, compose specifications or any text by using a common...

1 answer below »
check word doc


Handout 1, CS 133F “Introduction to Fortran Programming” HW 5 HomeWork 5, Command Line interpreter General rules: Create homework, compose specifications or any text by using a common document-creation tool, such as Microsoft® Word. Hints: Refer to the wwweb or lecture notes for this class to design, implement, and debug correctly working SW solutions. Be concise, complete, and precise. Read the man page for the system() function in C++ (or C), and consider using it to complete the homework, i.e. to execute the commands. Abstract: Design, implement, explain, test, and debug a simple, but complete command-line interpreter named cli. Cli executes any or all of: cd, exec, exit, gcc, ls, man, more, mv, rm, pwd, sh, touch, which, and $path. Detail: Design, implement, document, test and run a simple shell, known here as a command-line interpreter (cli). This tool is invoked via the cli command plus possible arguments. Commands are OS commands to be executed. Multiple commands are separated from one another by commas, and some commands may in turn require further arguments. Cli ‘knows’ the defined list of commands a-priori. When invoked, cli checks, whether the first argument is an included command. If so, cli confirms this via a brief message. If not, a contrary error message is emitted, stating this is not one the predefined commands. After the message, cli executes all commands in the order listed. After executing the last command, cli prints the current working directory, i.e. it acts as if the pwd command had been issued; then cli terminates. Sample runs are shown further below. Multiple arguments of one cli command must be separated from one another by commas. Possible parameters of any one command are separated from the command itself (and from possible further parameters) by white space. White space consists of blanks, tabs, or a combination, but at least 1 blank space. Here are 4 sample runs with single and multiple commands: ./cli pwd -- looks like Unix pwd; but is your SW ./cli rm –f temp, mv temp ../temp1 -- ditto: this is your running homework 5 ./cli ls –la -- another “single unix command” ./cli rm a.out, gcc sys.c, cp a.out cli Cli starts out identifying itself, also naming you the author, and the release date. Then cli prints the list of all predefine commands. Finally, cli executes all commands input after the cli invocation. For your own debug effort, test your solution with numerous correct and also wrong inputs, including commas omitted, multiple commas, leading commas, illegals commands, other symbols instead of commas etc. No need to hand-in your test and debug work. The output of the cli command “cli pwd” or “./cli pwd” should be as shown below, assuming your current working directory is ./classes_Sac_State/csc139. Here is the output of a sample run with a single command line argument: herbertmayer$ ./cli pwd hgm cli 4/12/2020 Legal commands: cd exec exit gcc ls man more mv rm pwd sh touch which $path 2 strings passed to argv[] next string is 'pwd' new string is 'pwd ' 1st cmd 'pwd' is one of predefined. /Users/herbertmayer/herb/academia/classes_Sac_State/csc139 Here the output of another sample run, also with a single cli command: herbertmayer$ ./cli ls hgm cli 4/12/2020 Legal commands: cd exec exit gcc ls man more mv rm pwd sh touch which $path 2 strings passed to argv[] next string is 'ls' new string is 'ls ' 1st cmd 'ls' is one of predefined. admin cli.c sac_state_yyy backup_1_24_2020 docs sac_state_hw backup_3_9_2020 grades sac_state_xxx cli l_notes /Users/herbertmayer/herb/academia/classes_Sac_State/csc139 A strong hint: Interpretation (i.e. real execution) of commands that cli handles can proceed through system(), executed from inside your C/C++ program cli. List of all commands supported by your cli: char * cmds[ ] = { "cd", "exec", "exit", "gcc", "ls", "man", "more", "mv", "rm", "pwd", "sh", "touch", "which", "$path" }; What you turn in: 1. The source program of your homework solution; well commented, preferably one single source file. 2. Four progressively more complex executions of your correctly working cli program, showing all user inputs and corresponding output responses. 3
Answered 4 days AfterNov 17, 2021

Answer To: Handout 1, CS 133F “Introduction to Fortran Programming” HW 5 HomeWork 5, Command Line interpreter...

Darshan answered on Nov 22 2021
107 Votes
###############################PROGRAM################################
#include
#include
#include
#include
#include<
unistd.h>
#include
#include
#include
char * cmds[] = {
"cd",
"exec",
"exit",
"gcc",
"ls",
"man",
"more",
"mv",
"rm",
"pwd",
"sh",
"touch",
"which",
"$path"
};
int compare(char data[])
{
    char * temptoken = strtok(data, " ");
    int temp=0;
    for(int i=0;i<14;i++)
    {
        temp = strcmp(temptoken,cmds[i]);
        if(temp==0)
            return 0;
    }
    return 1;
}
int main(int argc, char *argv[])
{
    char string[100]="";
    for(int i=1;i    {
        strcat(string,argv[i]);
        strcat(string," ");
    }        
    string[strlen(string)-1] = '\0';
    int count=0;
    char * argvtoken[argc];
    char buffer[20]="";
char * token = strtok(string, ",");
int i=0;
while( token != NULL ) {
          argvtoken[i++] = token;
token = strtok(NULL, ",");
     count++;
}
    printf("gagandeep cli 22/11/2021\n");
printf("Legal commands: ");
for(int i=0;i<14;i++)
{
        printf("%s ",cmds[i]);
}

printf("\n%d strings passed to argv[]\n",argc);

for(int i=0;i {
     printf("next string is %s\n",argvtoken[i]);
     printf("new string is %s\n",argvtoken[i]);
             strcpy(buffer,argvtoken[i]);
             if(!compare(argvtoken[i])){
            printf("%d cmd %s is one of predefined.\n",i+1,argvtoken[i]);
        system(buffer);
        }
        else
            printf("%d cmd %s is not predefined.\n",i+1,argvtoken[i]);
}
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here