Advertisement
fimas

tash.cpp

Jan 19th, 2014
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9.  
  10. using namespace std;
  11.  
  12. void point(int j = 0, string msg = "")
  13. {
  14.     if (msg.length())
  15.         cout << msg << endl;
  16.     else if (j != 0)
  17.         cout << j << endl;
  18.     else
  19.         cout << "Here" << endl;
  20. }
  21.  
  22. class envMod
  23. {
  24.   public:
  25.     envMod()
  26.     {
  27.         this->pathModC = this->pMod.begin();
  28.         this->pathModC++;
  29.     }
  30.  
  31.     void parse(string e)
  32.     {
  33.         size_t j = 0, old = 0;
  34.         j = e.find("=", 0);
  35.         string action = e.substr(old, j);
  36.  
  37.         if (action == "PATH")
  38.         {
  39.             this->loadPaths(e.substr(j + 1));
  40.         }
  41.     }
  42.  
  43.     string pathMod(string * orig)
  44.     {
  45.         string p = *(this->pathModC);
  46.         orig->insert(0, p + "/");
  47.  
  48.         if (this->pathModC + 1 == this->pMod.end())
  49.         {
  50.             this->pathModC = this->pMod.begin();
  51.         }
  52.         else
  53.         {
  54.             this->pathModC++;
  55.         }
  56.  
  57.         return *orig;
  58.     }
  59.  
  60.     int numPaths()
  61.     {
  62.         return this->pMod.size();
  63.     }
  64.  
  65.     void dumpPaths()
  66.     {
  67.         for (vector < string >::iterator it = this->pMod.begin();
  68.              it != this->pMod.end(); ++it)
  69.         {
  70.             cout << *it << endl;
  71.         }
  72.     }
  73.  
  74.   private:
  75.  
  76.     void loadPaths(string pathString)
  77.     {
  78.         size_t j = 0;
  79.  
  80.         if (pathString.substr(0, 1) == ":")
  81.         {
  82.             pMod.push_back("./");
  83.             j = 1;
  84.         }
  85.  
  86.         while (j != string::npos)
  87.         {
  88.             size_t old = j;
  89.             j = pathString.find(":", old);
  90.             if (j == string::npos)
  91.             {
  92.                 string p = pathString.substr(old);
  93.                 this->pMod.push_back(p);
  94.             }
  95.             else
  96.             {
  97.                 string p = pathString.substr(old, j - old);
  98.                 this->pMod.push_back(p);
  99.                 j++;
  100.             }
  101.         }
  102.         this->resetIterator();
  103.     }
  104.  
  105.     void resetIterator()
  106.     {
  107.         this->pathModC = this->pMod.begin();
  108.     }
  109.  
  110.     vector < string >::iterator pathModC;
  111.     vector < string > pMod;
  112. };
  113.  
  114. class Term
  115. {
  116.   public:
  117.     Term()
  118.     {
  119.         this->exit = false;
  120.         this->setEnv();
  121.     }
  122.  
  123.      ~Term()
  124.     {
  125.     }
  126.  
  127.     string getCwd()
  128.     {
  129.         char *cwd = new char[256];
  130.         getcwd(cwd, 256);
  131.         string ret = cwd;
  132.         delete[]cwd;
  133.         return ret;
  134.     }
  135.  
  136.     void displayPrompt()
  137.     {
  138.         cout << this->getCwd() << " $ ";
  139.     }
  140.  
  141.     void getInput()
  142.     {
  143.         string input;
  144.         getline(cin, input);
  145.         this->parseKeyword(input);
  146.     }
  147.  
  148.     bool getExit()
  149.     {
  150.         return this->exit;
  151.     }
  152.  
  153.     void parseKeyword(string k)
  154.     {
  155.         if (k == "exit")
  156.         {
  157.             this->exit = true;
  158.         }
  159.         else
  160.         {
  161.             string cmd = k;
  162.             int ret = 0;
  163.  
  164.             for (int j = 0; j <= this->em.numPaths(); j++)
  165.             {
  166.                 this->em.pathMod(&cmd);
  167.                 if ((ret = this->parseCommand(cmd)) == 0)
  168.                 {
  169.                     break;
  170.                 }
  171.  
  172.                 cmd = k;
  173.             }
  174.  
  175.             if (ret)
  176.             {
  177.                 this->getError(k);
  178.             }
  179.         }
  180.     }
  181.  
  182.     void setEnv()
  183.     {
  184.         ifstream enf;
  185.         enf.open("./env.conf");
  186.         string envstr;
  187.         getline(enf, envstr);
  188.         enf.close();
  189.  
  190.         this->em.parse(envstr);
  191.         this->em.dumpPaths();
  192.     }
  193.  
  194.     void setError(string err)
  195.     {
  196.         ofstream oerr;
  197.         oerr.open(".error");
  198.         oerr << err;
  199.         oerr.close();
  200.     }
  201.  
  202.     void getError(string msg = "")
  203.     {
  204.         string error;
  205.         ifstream ierr;
  206.         ierr.open(".error");
  207.         getline(ierr, error);
  208.         ierr.close();
  209.  
  210.         if (msg.length())
  211.             cout << msg << ": ";
  212.  
  213.         cout << error << endl;
  214.     }
  215.  
  216.     int parseCommand(string command)
  217.     {
  218.         int argc = 0;
  219.  
  220.         char *cstr = new char[command.length() + 1];
  221.         char *cstrcpy = new char[command.length() + 1];
  222.         strcpy(cstr, command.c_str());
  223.         strcpy(cstrcpy, cstr);
  224.  
  225.         char *pch = strtok(cstr, " ");
  226.  
  227.         while (pch != NULL)
  228.         {
  229.             pch = strtok(NULL, " ");
  230.             argc++;
  231.         }
  232.  
  233.         char **argv = new char *[argc + 1];
  234.  
  235.         char *pch2;
  236.         pch2 = strtok(cstrcpy, " ");
  237.         argv[0] = new char[strlen(pch2) + 1];
  238.         strcpy(argv[0], pch2);
  239.  
  240.         for (int k = 1; pch2 != NULL; k++)
  241.         {
  242.             pch2 = strtok(NULL, " ");
  243.             if (pch2 == NULL)
  244.             {
  245.                 argv[k] = 0;
  246.                 continue;
  247.             }
  248.  
  249.             argv[k] = new char[strlen(pch2) + 1];
  250.             strcpy(argv[k], pch2);
  251.         }
  252.  
  253.         argv[argc] = NULL;
  254.  
  255.         pid_t chld = fork();
  256.  
  257.         if (chld == 0)
  258.         {
  259.             // replace chld with command
  260.             // C functions need to die
  261.             int status = execv(argv[0], argv);
  262.  
  263.             if (status != 0)
  264.             {
  265.                 // an error occurred
  266.                 this->setError(strerror(errno));
  267.                 _exit(1);
  268.             }
  269.  
  270.             _exit(0);
  271.         }
  272.         else if (chld < 0)
  273.         {
  274.             // couldn't fork
  275.             this->setError(strerror(errno));
  276.         }
  277.         else
  278.         {
  279.             int chldExit;
  280.             pid_t w;
  281.  
  282.             do
  283.             {
  284.                 // wait for chld to exit
  285.                 w = wait(&chldExit);
  286.             }
  287.             while (w != chld);
  288.  
  289.             delete[]cstr;
  290.             delete[]cstrcpy;
  291.             cstr = 0;
  292.  
  293.             for (int k = argc; k > 0; k--)
  294.             {
  295.                 if (argv[k] != NULL)
  296.                     delete[]argv[k];
  297.             }
  298.  
  299.             delete[]argv;
  300.             argv = 0;
  301.  
  302.             return chldExit;
  303.         }
  304.     }
  305.  
  306.   private:
  307.     bool exit;
  308.     envMod em;
  309. };
  310.  
  311. int main()
  312. {
  313.     Term *t = new Term();
  314.  
  315.     while (1)
  316.     {
  317.         t->displayPrompt();
  318.         t->getInput();
  319.  
  320.         if (t->getExit())
  321.         {
  322.             break;
  323.         }
  324.     }
  325.  
  326.     delete t;
  327.  
  328.     return 0;
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement