Advertisement
rinab333

CSCI 480 assignment 3

Jul 16th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. //Terina Burr
  2. //
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <string>
  6. #include <cstring>
  7. #include <sstream>
  8. #include <vector>
  9. #include <utility>
  10. #include <iterator>
  11. #include <unistd.h>
  12. #include <sys/types.h>
  13. #include <sys/wait.h>
  14. #include <cstdlib>
  15. #include <cstdio>
  16. using namespace std;
  17. /********************************************************************************
  18. FUNCTION: get_cmds
  19.  
  20. ARGUMENTS: const string& s - string to split
  21. const string& delim - delimiter
  22.  
  23. RETURNS: vector<char*> - container of the new strings
  24.  
  25. NOTES: parses string of commands, spliting string by delimiter
  26. ********************************************************************************/
  27. vector<string> get_cmds(const string& s, const string& delim)
  28. {
  29. stringstream ss(s);
  30. vector<string> commands;
  31. string t;
  32. while(getline(ss >> ws, t, delim[0]))
  33. {
  34. commands.push_back(t);
  35. ss >> t;//skip delimiter
  36. }
  37. return commands;
  38. }
  39.  
  40. /********************************************************************************
  41. FUNCTION: split_to_cstr
  42.  
  43. ARGUMENTS: const string& - string to split
  44.  
  45. RETURNS: vector<char*> - container of the new strings
  46.  
  47. NOTES: splits string by whitespace
  48. ********************************************************************************/
  49. vector<char*> split_to_cstr(const string& s)
  50. {
  51. stringstream ss(s);
  52.  
  53. string t;
  54. vector<char*> args_cmd;
  55.  
  56. while(ss >> t)//split by whitespace
  57. {
  58. args_cmd.push_back(new char[t.length() + 1]);//allocate
  59. strcpy(args_cmd.back(), t.c_str());//copy
  60. }
  61. return args_cmd;
  62. }
  63.  
  64. /********************************************************************************
  65. FUNCTION: execute_cmds
  66.  
  67. ARGUMENTS: int fd[2] - array of file descriptors
  68. vector<string>& - vector of commands
  69.  
  70. RETURNS: none
  71.  
  72. NOTES: fork new process for each command in vector and execute it
  73. ********************************************************************************/
  74. void execute_cmds(int fd[2], const vector<string>& commands)
  75. {
  76. for(size_t i = 0; i < commands.size(); ++i)
  77. {
  78. int pid = fork();
  79.  
  80. vector<char*> cmd_args;
  81. cmd_args = split_to_cstr(commands[i]);
  82.  
  83. if(pid == 0)
  84. {
  85. if(i == 0)//first command
  86. {
  87. if(commands.size() > 1)
  88. {
  89. close(fd[0]);
  90. dup2(fd[1], 1);
  91. //close(fd[1]);
  92. }
  93. }
  94. else if(i == commands.size() - 1)//last command
  95. {
  96. close(fd[1]);
  97. dup2(fd[0], 0);
  98. //close(fd[0]);
  99. }
  100. else//inner commands
  101. {
  102. dup2(fd[0], 0);
  103. //close(fd[0]);
  104. dup2(fd[1], 1);
  105. //close(fd[1]);
  106. }
  107. if(commands[i] == "q" || commands[i] == "quit")
  108. {
  109. //exit(-1);
  110. kill(pid, SIGTERM);//kill process and send sig
  111. }
  112. if(execvp(cmd_args[0], &cmd_args[0]) < 0)
  113. {
  114. perror("could'nt execute"); //print error
  115. exit(127);
  116. }
  117. }
  118. else if(pid < 0)
  119. {
  120. perror("fork");
  121. exit(EXIT_FAILURE);
  122. }
  123. }
  124. }
  125.  
  126. int main()
  127. {
  128. string prompt("microshell>"),
  129. input;
  130. int fd[2];
  131.  
  132. while(cout << prompt && getline(cin, input))
  133. {
  134. if(pipe(fd) < 0)
  135. {
  136. perror("pipe");
  137. exit(EXIT_FAILURE);
  138. }
  139.  
  140. vector<string> commands(get_cmds(input, "||"));
  141.  
  142. execute_cmds(fd, commands);
  143.  
  144. close(fd[0]);
  145. close(fd[1]);
  146.  
  147. //wait for children to finish
  148. int pid, status;
  149. while ((pid = wait(&status)) != -1) ;
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement