Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // mp2
- //
- // Created by Jack Shirley on 2/14/18.
- // Copyright © 2018 Jack Shirley. All rights reserved.
- //
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #include <vector>
- #include <sstream>
- #include <unistd.h>
- #include <time.h>
- #include <ctime>
- using namespace std;
- string str;
- string command = "";
- vector<string> tokens;
- vector<vector<string> > commands;
- void multInputs(vector<vector<string> > &commands){ //for when theres piping
- for(int i = 0; i < commands.size(); i++){
- char * args[commands[i].size()];
- for(int j = 0; j < tokens.size(); j++){
- args[j] = (char *)commands[i][j].c_str();
- }
- args[commands[i].size()] = NULL;
- execvp(args[0],args);
- }
- tokens.clear();
- }
- void singleInput(vector<vector<string> > &commands){ // jumps here if there is no piping involved
- char * args[commands[0].size()];
- for(int j = 0; j < commands[0].size(); j++){
- args[j] = (char *)commands[0][j].c_str();
- }
- args[commands[0].size()] = NULL;
- execvp(args[0], args);
- }
- void split(const string &s) { //Parser function
- stringstream ss(s);
- string item = "";
- while (ss >> item) {
- if(item == "|"){
- commands.push_back(tokens);
- tokens.clear();
- }else{
- tokens.push_back(item);
- }
- }
- commands.push_back(tokens);
- tokens.clear();
- }
- int main() {
- //*********************************************************
- //Custom Prompt features
- bool cust = false;
- time_t now;
- struct tm nowLocal;
- now = time(NULL);
- nowLocal = *localtime(&now);
- string terminalOut = "asdf";
- cout << "Custom or Default Prompt?" << endl;
- cin >> str;
- if(str == "Custom"){ //custom prompt or no?
- terminalOut = "/Users/jshirley/Documents/mp2.1 ";
- cust = true;
- }else{
- terminalOut = "$ ";
- }
- //*********************************************************
- while(true){ //continuosly ask for user input
- if(cust == true){
- cout << terminalOut << nowLocal.tm_mon << "/" << nowLocal.tm_year << " " << nowLocal.tm_hour << "-" << nowLocal.tm_min;
- }
- cout << terminalOut;
- command = "";
- getline(cin, command);
- if(command == "logout"){ //exit loop if user logs out
- cout << "Logging off" << endl;
- break;
- }
- split(command);
- if(fork() == 0){
- if(commands.size() == 1){ //only run if single command
- singleInput(commands);
- }else{
- multInputs(commands);
- }
- }else{
- wait(0);
- }
- commands.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement