Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Terina Burr
- //
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <cstring>
- #include <sstream>
- #include <vector>
- #include <utility>
- #include <iterator>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <cstdlib>
- #include <cstdio>
- using namespace std;
- /********************************************************************************
- FUNCTION: get_cmds
- ARGUMENTS: const string& s - string to split
- const string& delim - delimiter
- RETURNS: vector<char*> - container of the new strings
- NOTES: parses string of commands, spliting string by delimiter
- ********************************************************************************/
- vector<string> get_cmds(const string& s, const string& delim)
- {
- stringstream ss(s);
- vector<string> commands;
- string t;
- while(getline(ss >> ws, t, delim[0]))
- {
- commands.push_back(t);
- ss >> t;//skip delimiter
- }
- return commands;
- }
- /********************************************************************************
- FUNCTION: split_to_cstr
- ARGUMENTS: const string& - string to split
- RETURNS: vector<char*> - container of the new strings
- NOTES: splits string by whitespace
- ********************************************************************************/
- vector<char*> split_to_cstr(const string& s)
- {
- stringstream ss(s);
- string t;
- vector<char*> args_cmd;
- while(ss >> t)//split by whitespace
- {
- args_cmd.push_back(new char[t.length() + 1]);//allocate
- strcpy(args_cmd.back(), t.c_str());//copy
- }
- return args_cmd;
- }
- /********************************************************************************
- FUNCTION: execute_cmds
- ARGUMENTS: int fd[2] - array of file descriptors
- vector<string>& - vector of commands
- RETURNS: none
- NOTES: fork new process for each command in vector and execute it
- ********************************************************************************/
- void execute_cmds(int fd[2], const vector<string>& commands)
- {
- for(size_t i = 0; i < commands.size(); ++i)
- {
- int pid = fork();
- vector<char*> cmd_args;
- cmd_args = split_to_cstr(commands[i]);
- if(pid == 0)
- {
- if(i == 0)//first command
- {
- if(commands.size() > 1)
- {
- close(fd[0]);
- dup2(fd[1], 1);
- //close(fd[1]);
- }
- }
- else if(i == commands.size() - 1)//last command
- {
- close(fd[1]);
- dup2(fd[0], 0);
- //close(fd[0]);
- }
- else//inner commands
- {
- dup2(fd[0], 0);
- //close(fd[0]);
- dup2(fd[1], 1);
- //close(fd[1]);
- }
- if(commands[i] == "q" || commands[i] == "quit")
- {
- //exit(-1);
- kill(pid, SIGTERM);//kill process and send sig
- }
- if(execvp(cmd_args[0], &cmd_args[0]) < 0)
- {
- perror("could'nt execute"); //print error
- exit(127);
- }
- }
- else if(pid < 0)
- {
- perror("fork");
- exit(EXIT_FAILURE);
- }
- }
- }
- int main()
- {
- string prompt("microshell>"),
- input;
- int fd[2];
- while(cout << prompt && getline(cin, input))
- {
- if(pipe(fd) < 0)
- {
- perror("pipe");
- exit(EXIT_FAILURE);
- }
- vector<string> commands(get_cmds(input, "||"));
- execute_cmds(fd, commands);
- close(fd[0]);
- close(fd[1]);
- //wait for children to finish
- int pid, status;
- while ((pid = wait(&status)) != -1) ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement