Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // mp2
  4. //
  5. // Created by Jack Shirley on 2/14/18.
  6. // Copyright © 2018 Jack Shirley. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <vector>
  13. #include <sstream>
  14. #include <unistd.h>
  15. #include <time.h>
  16. #include <ctime>
  17.  
  18. using namespace std;
  19.  
  20.  
  21. string str;
  22. string command = "";
  23. vector<string> tokens;
  24. vector<vector<string> > commands;
  25.  
  26.  
  27. void multInputs(vector<vector<string> > &commands){ //for when theres piping
  28. for(int i = 0; i < commands.size(); i++){
  29. char * args[commands[i].size()];
  30.  
  31. for(int j = 0; j < tokens.size(); j++){
  32. args[j] = (char *)commands[i][j].c_str();
  33. }
  34.  
  35. args[commands[i].size()] = NULL;
  36. execvp(args[0],args);
  37. }
  38. tokens.clear();
  39. }
  40.  
  41.  
  42.  
  43. void singleInput(vector<vector<string> > &commands){ // jumps here if there is no piping involved
  44. char * args[commands[0].size()];
  45. for(int j = 0; j < commands[0].size(); j++){
  46. args[j] = (char *)commands[0][j].c_str();
  47. }
  48. args[commands[0].size()] = NULL;
  49. execvp(args[0], args);
  50. }
  51.  
  52.  
  53.  
  54.  
  55. void split(const string &s) { //Parser function
  56. stringstream ss(s);
  57. string item = "";
  58. while (ss >> item) {
  59. if(item == "|"){
  60. commands.push_back(tokens);
  61. tokens.clear();
  62. }else{
  63. tokens.push_back(item);
  64. }
  65. }
  66. commands.push_back(tokens);
  67. tokens.clear();
  68.  
  69.  
  70. }
  71.  
  72.  
  73. int main() {
  74.  
  75. //*********************************************************
  76. //Custom Prompt features
  77.  
  78. bool cust = false;
  79.  
  80. time_t now;
  81. struct tm nowLocal;
  82. now = time(NULL);
  83. nowLocal = *localtime(&now);
  84.  
  85. string terminalOut = "asdf";
  86. cout << "Custom or Default Prompt?" << endl;
  87. cin >> str;
  88. if(str == "Custom"){ //custom prompt or no?
  89. terminalOut = "/Users/jshirley/Documents/mp2.1 ";
  90. cust = true;
  91.  
  92. }else{
  93. terminalOut = "$ ";
  94. }
  95. //*********************************************************
  96.  
  97.  
  98.  
  99. while(true){ //continuosly ask for user input
  100. if(cust == true){
  101. cout << terminalOut << nowLocal.tm_mon << "/" << nowLocal.tm_year << " " << nowLocal.tm_hour << "-" << nowLocal.tm_min;
  102. }
  103. cout << terminalOut;
  104. command = "";
  105. getline(cin, command);
  106. if(command == "logout"){ //exit loop if user logs out
  107. cout << "Logging off" << endl;
  108. break;
  109. }
  110.  
  111. split(command);
  112.  
  113.  
  114. if(fork() == 0){
  115. if(commands.size() == 1){ //only run if single command
  116. singleInput(commands);
  117. }else{
  118. multInputs(commands);
  119. }
  120. }else{
  121. wait(0);
  122. }
  123. commands.clear();
  124. }
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement