Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- #include <cstdlib>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <errno.h>
- using namespace std;
- void point(int j = 0, string msg = "")
- {
- if (msg.length())
- cout << msg << endl;
- else if (j != 0)
- cout << j << endl;
- else
- cout << "Here" << endl;
- }
- class envMod
- {
- public:
- envMod()
- {
- this->pathModC = this->pMod.begin();
- this->pathModC++;
- }
- void parse(string e)
- {
- size_t j = 0, old = 0;
- j = e.find("=", 0);
- string action = e.substr(old, j);
- if (action == "PATH")
- {
- this->loadPaths(e.substr(j + 1));
- }
- }
- string pathMod(string * orig)
- {
- string p = *(this->pathModC);
- orig->insert(0, p + "/");
- if (this->pathModC + 1 == this->pMod.end())
- {
- this->pathModC = this->pMod.begin();
- }
- else
- {
- this->pathModC++;
- }
- return *orig;
- }
- int numPaths()
- {
- return this->pMod.size();
- }
- void dumpPaths()
- {
- for (vector < string >::iterator it = this->pMod.begin();
- it != this->pMod.end(); ++it)
- {
- cout << *it << endl;
- }
- }
- private:
- void loadPaths(string pathString)
- {
- size_t j = 0;
- if (pathString.substr(0, 1) == ":")
- {
- pMod.push_back("./");
- j = 1;
- }
- while (j != string::npos)
- {
- size_t old = j;
- j = pathString.find(":", old);
- if (j == string::npos)
- {
- string p = pathString.substr(old);
- this->pMod.push_back(p);
- }
- else
- {
- string p = pathString.substr(old, j - old);
- this->pMod.push_back(p);
- j++;
- }
- }
- this->resetIterator();
- }
- void resetIterator()
- {
- this->pathModC = this->pMod.begin();
- }
- vector < string >::iterator pathModC;
- vector < string > pMod;
- };
- class Term
- {
- public:
- Term()
- {
- this->exit = false;
- this->setEnv();
- }
- ~Term()
- {
- }
- string getCwd()
- {
- char *cwd = new char[256];
- getcwd(cwd, 256);
- string ret = cwd;
- delete[]cwd;
- return ret;
- }
- void displayPrompt()
- {
- cout << this->getCwd() << " $ ";
- }
- void getInput()
- {
- string input;
- getline(cin, input);
- this->parseKeyword(input);
- }
- bool getExit()
- {
- return this->exit;
- }
- void parseKeyword(string k)
- {
- if (k == "exit")
- {
- this->exit = true;
- }
- else
- {
- string cmd = k;
- int ret = 0;
- for (int j = 0; j <= this->em.numPaths(); j++)
- {
- this->em.pathMod(&cmd);
- if ((ret = this->parseCommand(cmd)) == 0)
- {
- break;
- }
- cmd = k;
- }
- if (ret)
- {
- this->getError(k);
- }
- }
- }
- void setEnv()
- {
- ifstream enf;
- enf.open("./env.conf");
- string envstr;
- getline(enf, envstr);
- enf.close();
- this->em.parse(envstr);
- this->em.dumpPaths();
- }
- void setError(string err)
- {
- ofstream oerr;
- oerr.open(".error");
- oerr << err;
- oerr.close();
- }
- void getError(string msg = "")
- {
- string error;
- ifstream ierr;
- ierr.open(".error");
- getline(ierr, error);
- ierr.close();
- if (msg.length())
- cout << msg << ": ";
- cout << error << endl;
- }
- int parseCommand(string command)
- {
- int argc = 0;
- char *cstr = new char[command.length() + 1];
- char *cstrcpy = new char[command.length() + 1];
- strcpy(cstr, command.c_str());
- strcpy(cstrcpy, cstr);
- char *pch = strtok(cstr, " ");
- while (pch != NULL)
- {
- pch = strtok(NULL, " ");
- argc++;
- }
- char **argv = new char *[argc + 1];
- char *pch2;
- pch2 = strtok(cstrcpy, " ");
- argv[0] = new char[strlen(pch2) + 1];
- strcpy(argv[0], pch2);
- for (int k = 1; pch2 != NULL; k++)
- {
- pch2 = strtok(NULL, " ");
- if (pch2 == NULL)
- {
- argv[k] = 0;
- continue;
- }
- argv[k] = new char[strlen(pch2) + 1];
- strcpy(argv[k], pch2);
- }
- argv[argc] = NULL;
- pid_t chld = fork();
- if (chld == 0)
- {
- // replace chld with command
- // C functions need to die
- int status = execv(argv[0], argv);
- if (status != 0)
- {
- // an error occurred
- this->setError(strerror(errno));
- _exit(1);
- }
- _exit(0);
- }
- else if (chld < 0)
- {
- // couldn't fork
- this->setError(strerror(errno));
- }
- else
- {
- int chldExit;
- pid_t w;
- do
- {
- // wait for chld to exit
- w = wait(&chldExit);
- }
- while (w != chld);
- delete[]cstr;
- delete[]cstrcpy;
- cstr = 0;
- for (int k = argc; k > 0; k--)
- {
- if (argv[k] != NULL)
- delete[]argv[k];
- }
- delete[]argv;
- argv = 0;
- return chldExit;
- }
- }
- private:
- bool exit;
- envMod em;
- };
- int main()
- {
- Term *t = new Term();
- while (1)
- {
- t->displayPrompt();
- t->getInput();
- if (t->getExit())
- {
- break;
- }
- }
- delete t;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement