using shell in unix Write a simple single thread CSC4420 shell , i.e., no concur
ID: 3751509 • Letter: U
Question
using shell in unix
Write a simple single thread CSC4420 shell , i.e., no concurrent commands, to support at least the following built-in commands:
prompt can set to any string you input, in addition to the current history event number, i.e, %h as in csh;
hour : list class times
room : list classroom location
desp : list the description of this course
text : list the textbook
ref : list the reference books
prof : list the professor's name
pol : professor's office location
poh : professor's office hours
pma : professor's email address
pre : list the course pre-requisites
cor : list the course co-requisite
hw : list the number of home works
gr : list the grading policy (what percentage for home works, midterm exam, and final exam)
history : list history of events up to the number you set
help : list all the available commands
exit or quit : exit CSC4420 shell
thats is how the question was giving
Explanation / Answer
Here is a sample program which suits the question. Ensure to modify this program so that you enter correct details and more commands. I have implemented a few simple commands and two little difficult commands promt and history.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int ch_num = 0;
char display_prompt[260];
char prompt[50] = "CSC2240 >> ";
char cmd[100];
char *p;
char *history[1024]; // keep history upto 1024 commands
sprintf(display_prompt, "(%d) %s",ch_num, prompt);
while (1) {
ch_num++;
printf("(%d) %s", ch_num, prompt);
fgets(cmd, sizeof(cmd), stdin);
// handle commands entered by user
// is the command a prompt command?
cmd[strlen(cmd)-1] = 0; // remove trailing new line
history[ch_num-1] = strdup(cmd);
if (strstr(cmd, "prompt") == cmd) {
// so prompt will have an additional string with it, extract it
strtok(cmd, " ");
p = strtok(NULL, " ");
strcpy(prompt, p); // set the new prompt to be displayed
} else if (!strcmp(cmd, "exit") || !strcmp(cmd, "quit")) {
exit(0);
} else if (!strcmp(cmd, "help")) {
printf ("Available commands are prompt, hour, room <list additional commands here>");
} else if (!strcmp(cmd, "hour")) {
printf("class timings are 10AM - 11 AM, Thursdays");
} else if (!strcmp(cmd, "room")) {
printf("class location is 1st floor, Jenkins building");
} else if (!strcmp(cmd, "desp")) {
printf("books required are as follows");
} else if (!strcmp(cmd, "ref")) {
printf("list ref books here");
} else if (!strcmp(cmd, "prof")) {
printf("list prof details here");
} else if (!strcmp(cmd, "history")) {
for (int j = ch_num-1; j >= 0; j--) {
printf (" %d %s", j, history[j]);
}
}
// add more commands as more else if statements
printf(" ");
}
}
Sample Output -
vagrant@ubuntu-xenial ~/chegg> ./a.out
(1) CSC2240 >> hour
class timings are 10AM - 11 AM, Thursdays
(2) CSC2240 >> room
class location is 1st floor, Jenkins building
(3) CSC2240 >> prof
list prof details here
(4) CSC2240 >> history
3 history
2 prof
1 room
0 hour
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.