Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <vector>
  4. #include "cmdExec.h"
  5. //#include "cmdBase.h"
  6. //#include "cmdConn.h"
  7. //#include "cmdAnd.h"
  8. //#include "cmdOr.h"
  9. //#include "cmdSemi.h"
  10.  
  11. using namespace std;
  12.  
  13. void removeSemis(vector<char*> &finishedVect)
  14. {
  15. char* last = finishedVect.back(); //last = nikhil;\0
  16.  
  17. last[strlen(last) - 1] = 0;
  18.  
  19. return;
  20.  
  21. }
  22.  
  23. void removeParenthLeft(vector<char*> &moddedVect)
  24. {
  25. char* first = moddedVect.back();
  26.  
  27. memmove(first, first+1, strlen(first));
  28.  
  29. return;
  30.  
  31. }
  32.  
  33. void removeParenthRight(vector<char*> &moddedVect)
  34. {
  35. char* last = moddedVect.back();
  36.  
  37. last[strlen(last) - 1] = 0;
  38.  
  39. return;
  40.  
  41. }
  42.  
  43.  
  44. int main()
  45. {
  46. bool checker = true;
  47. while(checker){
  48. vector< vector<char*> > cmdVector; // this is a vector that holds the 2d vector: of commands + connectors (no semis)
  49. //cmdPrompt(execVect); // this is what creates the 2d vector
  50.  
  51. string userInput = ""; //test string
  52. cout << "$ "; //outputs $
  53. getline(cin, userInput); //get inputted strin
  54. string temp = userInput.substr(0,4);
  55. if (temp == "exit")
  56. {
  57. return 0;
  58. }
  59. userInput = userInput.substr(0, userInput.find("#"));
  60.  
  61. int leftCount = 0;
  62. int rightCount = 0;
  63. for (int i = 0; i < userInput.size(); ++i)
  64. {
  65. if (userInput.at(i) == '(')
  66. {
  67. leftCount = leftCount + 1;
  68. }
  69. else if (userInput.at(i) == ')')
  70. {
  71. rightCount = rightCount + 1;
  72. }
  73. }
  74.  
  75. if (leftCount != rightCount)
  76. {
  77. cout << "Error: Missing or extra parenthesis" << endl;
  78. continue;
  79. }
  80.  
  81.  
  82.  
  83. char str[userInput.size()]; //creates character array with string size
  84. strcpy(str,userInput.c_str()); //turn string into character string and store in str[]
  85. char* pch; //used to point at tokens
  86. pch = strtok(str, " ");
  87. char* last_letter = &(pch[strlen(pch)-1]); // points at las character
  88. char* first_letter = pch;
  89. bool connectorTrue = false; // checks if previous parse was a connector
  90. char* orsign = new char('|');
  91. char* andsign = new char('&');
  92. char* parsignLeft = new char('(');
  93. char* parsignRight = new char(')');
  94. char* semi = new char(';');
  95. vector<char*> andSign;
  96. andSign.push_back(andsign);
  97. vector<char*> orSign;
  98. orSign.push_back(orsign);
  99. vector<char*> semiSign;
  100. semiSign.push_back(semi);
  101. vector<char*> parSignLeft;
  102. parSignLeft.push_back(parsignLeft);
  103. vector<char*> parSignRight;
  104. parSignRight.push_back(parsignRight);
  105. while (pch != NULL)
  106. {
  107. if(cmdVector.size() == 0) //cmdVector parse when vector is empty (i.e. first pch)
  108. {
  109. vector<char*> tempVect;
  110. tempVect.push_back(pch); // a vector containing (ls; or just ls) is made
  111. cmdVector.push_back(tempVect);// a vector containing (ls; or just ls) is pushed to first row of the cmdVector
  112. connectorTrue = false;
  113.  
  114. while (*(first_letter) == '(') // if it starts with a (
  115. {
  116. cmdVector.insert(cmdVector.begin(), parSignLeft); //a vector containing (() is pushed to first row of the cmdVector
  117. removeParenthLeft( cmdVector.back() ); // pass in the current vector of char* that just got pushed in
  118. connectorTrue = false;
  119. first_letter = cmdVector.back().at(0);
  120. }
  121.  
  122.  
  123. if (*(last_letter) == ';') //if it ends with ; (e.g. ls;)
  124. {
  125. removeSemis( cmdVector.back() ); //pass in the current vector of char* that just got pushed in
  126. cmdVector.push_back(semiSign); //a vector containing (;) is pushed to next row of the cmdVector
  127. connectorTrue = true;
  128. }
  129.  
  130.  
  131.  
  132. }
  133.  
  134.  
  135. else //not first pch
  136. {
  137. if(connectorTrue)//previous parse was a connector, this one must be added as a new row to cmdVector
  138. {
  139. vector<char*> tempVect;
  140. tempVect.push_back(pch); // a vector containing command/argument (e.g. ls) is made
  141. cmdVector.push_back(tempVect); //added as a new row
  142. connectorTrue = false;
  143.  
  144. if (*(first_letter) == '(') // if it starts with a (
  145. {
  146. while (*(first_letter) == '(')
  147. {
  148. cmdVector.insert(cmdVector.begin() + (cmdVector.size() - 1), parSignLeft); //a vector containing (() is pushed to first row of the cmdVector
  149. removeParenthLeft( cmdVector.back() ); // pass in the current vector of char* that just got pushed in
  150. connectorTrue = false;
  151. first_letter = cmdVector.back().back();
  152. }
  153. }
  154.  
  155. if (*(last_letter) == ';') //if it ends with ; (e.g. ls;)
  156. {
  157. removeSemis( cmdVector.back() ); //pass in the current vector of char* that just got pushed in
  158. cmdVector.push_back(semiSign); //a vector containing (;) is pushed to next row of the cmdVector
  159. connectorTrue = true;
  160. }
  161. }
  162.  
  163. else if ( *(last_letter) == '&' ) //lastletter is &
  164. {
  165. cmdVector.push_back(andSign);
  166. connectorTrue = true; //set previous as connector
  167. }
  168. else if ( *(last_letter) == '|' ) //lastletter is |
  169. {
  170. cmdVector.push_back(orSign);
  171. connectorTrue = true; //previous as connector
  172. }
  173.  
  174.  
  175.  
  176. else //previous was not a connector and this may be a connector or command, this one must be added to the current row
  177. {
  178. if ( *(last_letter) == ';' ) //lastletter is ;
  179. {
  180. cmdVector.back().push_back(pch);
  181. removeSemis( cmdVector.back() );
  182. cmdVector.push_back(semiSign);
  183. connectorTrue = true; //previous was a connector
  184. }
  185.  
  186. else if ( *(last_letter) == ')' ) // lastletter is )
  187. {
  188. int rightPar = cmdVector.size() - 1;
  189. cmdVector.back().push_back(pch);
  190. while ( *(last_letter) == ')' ) // lastletter is )
  191. {
  192. //cout << "it enters here atleast" << endl;
  193. //cout << pch << endl;
  194. removeParenthRight( cmdVector.at(rightPar) );
  195. cmdVector.push_back(parSignRight);
  196. connectorTrue = false;
  197.  
  198. char* temp_letter = cmdVector.at(rightPar).back();
  199. last_letter = &(temp_letter[strlen(temp_letter)-1]);
  200. }
  201. }
  202.  
  203. else
  204. {
  205. cmdVector.back().push_back(pch);
  206. }
  207. }
  208. }
  209.  
  210. pch = strtok(NULL," "); //next token
  211. if (pch != NULL)
  212. {
  213. last_letter = &(pch[strlen(pch)-1]);
  214. first_letter = pch;
  215. }
  216.  
  217. }
  218.  
  219. /* test harness for cmd Vector */
  220. for (int i = 0; i < cmdVector.size(); ++i)
  221. {
  222. for(int j = 0; j < cmdVector.at(i).size(); ++j)
  223. {
  224. cout << cmdVector.at(i).at(j) << " "; //<< cmdVector.at(i).size();
  225. }
  226. cout << endl;
  227. }
  228.  
  229. /* end test for cmd Vector */
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241. }
  242. return 0;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement