Building a Shell. I'm in Hell

Reply

Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

Building a Shell. I'm in Hell

 
0
  #1
Feb 4th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,003
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 137
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Building a Shell. I'm in Hell

 
0
  #2
Feb 4th, 2006
You're declaring it wrong; that'd be vector< vector<char *> >
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 56
Reputation: kharri5 is an unknown quantity at this point 
Solved Threads: 0
kharri5's Avatar
kharri5 kharri5 is offline Offline
Junior Poster in Training

Re: Building a Shell. I'm in Hell

 
0
  #3
Feb 4th, 2006
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:

  1.  
  2. takeInpt(vector< vector<char*> > &cmds)
  3. {
  4. char inpt[255];
  5. char* cmnd;
  6. int count=0;
  7. vector<char*> in;
  8.  
  9. //ask for and get the input
  10. cout << "statsh$: ";
  11.  
  12. cin.getline(inpt,255); //leave plenty of room for input
  13.  
  14. cmnd = strtok(inpt, " ");
  15.  
  16. //first thing should be some command
  17. in.push_back(cmnd);
  18.  
  19. //all the rest will either be commands or arguments
  20. cmnd = strtok(NULL, " "); //get next item
  21.  
  22. while(cmnd != NULL)
  23. {
  24. count=1; //gotten first command looking for arguments
  25.  
  26. if(strcmp(cmnd,"|") == 0)
  27. {
  28. piped = true;
  29. cmnd = strtok(NULL, " "); //go over the pipe and get next token
  30.  
  31. if(cmnd != NULL)
  32. {
  33. cmds.push_back(in);
  34. vector<char*> pipe;
  35. in = pipe;
  36. //fill command with relevant information
  37. in.push_back(cmnd);
  38. }
  39. else
  40. {
  41. cerr << "You didn't pipe into anything" << endl;
  42. }
  43. }
  44. else if(strcmp(cmnd,">") == 0)
  45. {
  46. cmnd = strtok(NULL, " "); //go over > and get next token
  47. if(cmnd != NULL)
  48. {
  49. redFile = cmnd; //first redirection file
  50. redType = STDOUT;
  51. }
  52. else
  53. {
  54. cerr<< "Redirect to what now?? " << endl;
  55. }
  56. }
  57. else if (strcmp(cmnd, "<") == 0)
  58. {
  59.  
  60. cmnd = strtok (NULL, " "); //go over < and get next token
  61.  
  62. if (cmnd != NULL)
  63. {
  64. redFile = cmnd;
  65. redType = STDIN;
  66. }
  67. else
  68. {
  69. cerr<< "Redirect what now?? " << endl;
  70. }
  71. }
  72.  
  73. else //get arguments to the current command
  74. {
  75. in.push_back(cmnd);
  76. }
  77. cmnd = strtok(NULL, " ");
  78. }
  79.  
  80. cmds.push_back(in);
  81.  
  82. return count; //return size of input
  83.  
  84. }

From main i call the following:


  1. int main()
  2. {
  3. tms systemTime;
  4. clock_t sysTime;
  5.  
  6. int argNums = 0;
  7. char* cmnd;
  8. vector< vector<char*> > commands;
  9.  
  10. //init link list ignore for now
  11. lnkLst* head;
  12. head = new lnkLst;
  13. head->process = " ";
  14. head->sysTm = 0.0;
  15. head->useTm = 0.0;
  16. head->nxt = NULL;
  17.  
  18. displayID();
  19. displayInfo();
  20.  
  21. while(1)
  22. {
  23.  
  24. //init just in case
  25. runmode = 0;
  26. redFile = NULL;
  27.  
  28. //take input from user
  29. argNums = takeInpt(commands);
  30.  
  31. if( !commands.empty() )
  32. {
  33. for(int i=0; i < commands.size(); i++)
  34. {
  35. //vector<char*> com = commands[i];
  36. cout<< "Command" << i << " is: ";
  37. for(int j=0; j < commands[i].size(); j++)
  38. {
  39. cout<< commands[i][j];
  40. }
  41. }
  42.  
  43. //freeList(head);
  44.  
  45. return 0;
  46.  
  47. }


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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC