| | |
Building a Shell. I'm in Hell
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
So I'm trying to build a shell, and I've been stuck for days just trying to think about how to take in the input from the user. I need to be able to have a list of dynamically growing commands, that each has its list of dynamically growing arguments, so that I can handle situations where pipes occur, or regular commands occur with arguments, or redirections etc.
my first idea was make a struct of command types that held the char* command name, an array of arguments, and the size for the array of args. I could then make a vector of this. This idea seems pretty difficult to do though
The second idea was make a vector of vectors of char*s. so like
vector< <vector<char*> >, but this gives me errors like ISO C++ forbids items with no type. So i guess I cannot declare a vector of vectors? or am I declaring it wrong.
I want the easiest possible way to do this, because I am just stuck on it for three days straight now.
If someone could help me I would much appreciate it.
my first idea was make a struct of command types that held the char* command name, an array of arguments, and the size for the array of args. I could then make a vector of this. This idea seems pretty difficult to do though
The second idea was make a vector of vectors of char*s. so like
vector< <vector<char*> >, but this gives me errors like ISO C++ forbids items with no type. So i guess I cannot declare a vector of vectors? or am I declaring it wrong.
I want the easiest possible way to do this, because I am just stuck on it for three days straight now.
If someone could help me I would much appreciate it.
•
•
•
•
Originally Posted by Rashakil Fol
You're declaring it wrong; that'd be vector< vector<char *> >
okay so problem with this vector of vector of chars is it prnts me garbage when i test it
I have the take input functoin here:
C++ Syntax (Toggle Plain Text)
takeInpt(vector< vector<char*> > &cmds) { char inpt[255]; char* cmnd; int count=0; vector<char*> in; //ask for and get the input cout << "statsh$: "; cin.getline(inpt,255); //leave plenty of room for input cmnd = strtok(inpt, " "); //first thing should be some command in.push_back(cmnd); //all the rest will either be commands or arguments cmnd = strtok(NULL, " "); //get next item while(cmnd != NULL) { count=1; //gotten first command looking for arguments if(strcmp(cmnd,"|") == 0) { piped = true; cmnd = strtok(NULL, " "); //go over the pipe and get next token if(cmnd != NULL) { cmds.push_back(in); vector<char*> pipe; in = pipe; //fill command with relevant information in.push_back(cmnd); } else { cerr << "You didn't pipe into anything" << endl; } } else if(strcmp(cmnd,">") == 0) { cmnd = strtok(NULL, " "); //go over > and get next token if(cmnd != NULL) { redFile = cmnd; //first redirection file redType = STDOUT; } else { cerr<< "Redirect to what now?? " << endl; } } else if (strcmp(cmnd, "<") == 0) { cmnd = strtok (NULL, " "); //go over < and get next token if (cmnd != NULL) { redFile = cmnd; redType = STDIN; } else { cerr<< "Redirect what now?? " << endl; } } else //get arguments to the current command { in.push_back(cmnd); } cmnd = strtok(NULL, " "); } cmds.push_back(in); return count; //return size of input }
From main i call the following:
C++ Syntax (Toggle Plain Text)
int main() { tms systemTime; clock_t sysTime; int argNums = 0; char* cmnd; vector< vector<char*> > commands; //init link list ignore for now lnkLst* head; head = new lnkLst; head->process = " "; head->sysTm = 0.0; head->useTm = 0.0; head->nxt = NULL; displayID(); displayInfo(); while(1) { //init just in case runmode = 0; redFile = NULL; //take input from user argNums = takeInpt(commands); if( !commands.empty() ) { for(int i=0; i < commands.size(); i++) { //vector<char*> com = commands[i]; cout<< "Command" << i << " is: "; for(int j=0; j < commands[i].size(); j++) { cout<< commands[i][j]; } } //freeList(head); return 0; }
This prints out command0 is: command1: (garbage prints here) etc..
Why isn't it echoing back what i typed into the command line for take input?
I am missing something crucial here I think about C/c++
If you could point me in the right direction i would much appreciate. Thanks again for all of your continued help btw.
![]() |
Similar Threads
- Good books on Shell Programming/Perl (Perl)
- Shell Programming (Shell Scripting)
- Making a UNIX Shell, so inexperienced at it (C++)
- My Computer Flashes The 'lsa ' Shell Error!!! (Windows NT / 2000 / XP)
- Shell HELP! (Getting Started and Choosing a Distro)
Other Threads in the C++ Forum
- Previous Thread: MFC art program
- Next Thread: What am I doing wrong?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






