fork download
  1. #include <string>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. const string PROMPT = "prompt> ";
  13. const string PIPE_DEL = "||";
  14.  
  15. bool checkInput(char []);
  16.  
  17.  
  18. bool checkInput(string command){
  19. transform(command.begin(), command.end(), command.begin(), ::tolower);
  20. if (command == "q" || command == "quit")
  21. return true;
  22. else
  23. return false;
  24. }
  25.  
  26.  
  27. int main(){
  28. int iteration = 0;
  29. while (true){
  30. string command;
  31. pid_t pid;
  32.  
  33. cout << '\n' << iteration << '\n';
  34. cout << PROMPT;
  35. if (getline(cin, command) && ! checkInput(command) ) {
  36. size_t pos=0, nxt=0;
  37. vector<string> commands;
  38.  
  39. do {
  40. nxt=command.find(PIPE_DEL,pos);
  41. commands.push_back(command.substr(pos, nxt-pos));
  42. if (nxt!=string::npos)
  43. nxt+=PIPE_DEL.length();
  44. pos = nxt;
  45. } while (pos!=string::npos);
  46.  
  47. cout<<"Commands:";
  48. copy(commands.begin(), commands.end(), ostream_iterator<string>(cout,"\n\t"));
  49. iteration ++;
  50. }
  51. else
  52. break;
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0s 15240KB
stdin
hello world
hello||world
q

stdout
0
prompt> Commands:hello world
	
1
prompt> Commands:hello
	world
	
2
prompt>