How does one add the functionality for command recognition and implement the HEL
ID: 3771521 • Letter: H
Question
How does one add the functionality for command recognition and implement the HELP, QUIT, and COPY, LIST, and RUN functions for a command interpreter?
After a line of user input has been read, this program should determine if the first element of the array of cstrings is one of the valid commands. If it is a valid command, this program will check that the number of parameters is correct. The program should give detailed error messages if the command is not valid or the number of parameters is incorrect.
The valid commands are:
RUN executable-file
LIST
LIST directory
COPY old-filename new-filename
CD new-directory
SHOW filename
HELP
QUIT
If the entered command is invalid or if the number of parameters to that command is incorrect your program will give an appropriate and specific error message.
If the command is valid, the program will carry out the command.
Explanation / Answer
#include <iostream>
#include <string.h>
#include <dirent.h>
#include <fstream>
using namespace std;
void parse(char **a, int &num)
{
char c;
int i=0,j=0;
bool space=false;
bool nonempty=false;
cout<<"dysh>";
do
{
c=cin.get();
}while(c==' '&& c!=' ');
while(c!=' ')
{
if(c==' ')
{
space=true;
a[i][j]='';
i++;
j=0;
while(c==' ')
{
c=cin.get();
}
}
else
{
nonempty=true;
space=false;
a[i][j]=c;
j++;
c=cin.get();
}
}
a[i][j]='';
if(!space)
num=i+1;
else
num=i;
if(!nonempty)
num=0;
}
int main()
{
int n;
char **a= new char*[30];
if(!a)
{
cerr<<"Memory Allocation error ";
exit(1);
}
for(int i=0;i<30;i++)
{
a[i]=new char[1024];
if(!a)
{
cerr<<"Memory Allocation error ";
exit(1);
}
}
do
{
parse(a,n);
for(int i=0;i<n;i++)
{
cout<<"a["<<i<<"]='"<<a[i]<<"'"<<endl;
}
char valid_cmd[][5]={"RUN","LIST","COPY","CD","SHOW","HELP"};
int num_parameters[]={1,1,2,1,1,1};
int valid=-1;
for(int i=0;i<n;i++)
{
if(a[0]==valid_cmd[i])
{
valid=i;
break;
}
}
if(valid==-1)
{
cout<<endl<<"Invalid command";
exit(0);
}
else
{
int p=num_parameters[valid];
if(a[0].compare("LIST")!=0)
{
if(p!=n-1)
{
cout<<endl<<"Command cant be processed with less number of arguments";
exit(0);
}
}
else
{
string line;
switch(valid+1)
{
case 1: execv (a[1].c_str());break;
case 2: DIR *d;
struct dirent *dir;
d = opendir(a[1]);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
cout<<endl;
dir->d_name;
}
closedir(d);
}
break;
case 3:
ofstream outfile; //output file stream variable
ifstream infile; //input file stream variable
infile.open(a[1].c_str());
outfile.open(a[2].c_str());
while(!infile.eof())
{
infile>>line;
outfile<<line;
}
break;
case 4: cout<<endl<<"Executing CD sommand";break;
case 5:
infile.open(a[1].c_str());
while(!infile.eof())
{
infile>>line;
cout<<line;
}
break;
case 6: cout<<endl<<"Executing HELP sommand";break;
}
}
}
}while(strcmp(a[0],"QUIT"));
for(int i=0;i<30;i++)
delete[] a[i];
delete[] a;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.