Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SHT.cpp
- *
- * Created on: 29.01.2016
- * Author: armised
- */
- #include <algorithm>
- #include <string>
- #include <sstream>
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <fstream>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <string.h>
- #include <unistd.h>
- using namespace std;
- int ghi = 0;
- bool replace(std::string& str, const std::string& from, const std::string& to) { size_t start_pos = str.find(from); if(start_pos == std::string::npos) return false; str.replace(start_pos, from.length(), to); return true; }
- bool is_file_exist(const char *fileName) { std::ifstream infile(fileName); return infile.good(); }
- bool StartsWith(const std::string& text,const std::string& token)
- {
- if(text.length() < token.length())
- return false;
- return (text.compare(0, token.length(), token) == 0);
- }
- void clear() {
- // CSI[2J clears screen, CSI[H moves the cursor to top-left corner
- std::cout << "\x1B[2J\x1B[H";
- }
- ///////// MAIN \\\\\\\\/
- int main(int argc, char* argv[])
- {
- char path[256];
- getcwd(path, 256);
- char rawin[256];
- string exit =rawin;
- cout << "SHT Shell - Version 1.0.3\nBereit Befehle entgegen zu nehmen.\n";
- while (true)
- {
- system("echo \"\\033[1;33m\\c\"");
- cout << path << " # ";
- cin.getline(rawin, 256);
- system("echo \"\\033[00m\\c\"");
- //cin >> rawin;
- //cout << rawin << endl;
- string in = rawin;
- string args[300];
- int i = 0;
- stringstream ssin(in);
- while (ssin.good() && i < 300)
- {
- ssin >> args[i];
- ++i;
- }
- string xstr = "/system/bin/"+args[0];
- if (args[0] == "print")
- {
- int ix;
- int argv = sizeof(args)/sizeof(*args);
- for(int ix = 1; ix < argv; ix++)
- {
- cout << args[ix] << " ";
- }
- cout << endl;
- }
- else if (args[0] == "exit")
- {
- string ans;
- cout << "Wollen sie die Shell wirklich stoppen?\n[j/n]: ";
- cin >> ans;
- if (ans == "j" || ans == "J")
- {
- cout << "Shuttding down...\nBB ;)" << endl;
- system("shutdown -s 1");
- return 0;
- }
- else
- {
- cout << "Vorgang vom User abgebrochen.\n";
- }
- }
- else if (args[0] == "list")
- {
- system("ls");
- }
- else if (args[0] == "cd")
- {
- string x = args[1].c_str();
- const char* f = x.c_str();
- if (chdir(f) != 0)
- {
- cout << "Das angegebene Verzeichnis konnte nicht gefunden werden\noder sie besitzen keine Berechtigung dazu.\n";
- }
- else
- {
- char xpath[256];
- getcwd(xpath, 256);
- getcwd(path, 256);
- cout << "Du befindest dich jetzt in: " << xpath << endl;
- }
- }
- else if(args[0] == "mf")
- {
- string f = args[1];
- const char *x = f.c_str();
- ofstream file;
- file.open(x);
- file << "New File";
- file.close();
- if (::ghi == 0)
- {
- cout << "Hinweis: Sie können nur eine Datei pro Kommando erstellen.\n";
- ::ghi++;
- }
- }
- else if(args[0] == "cls" || args[0] == "clear")
- {
- clear();
- cout << "";
- }
- else if(args[0] == "md")
- {
- if(!mkdir(args[1].c_str(), 0755))
- {
- cout << "Das Verzeichnis "+args[1]+" wurde erfolgreich erstellt!" << endl;
- }
- else
- {
- cout << "Unbekannter Error beim erstellen aufgetreten!" << endl;
- }
- }
- else if(args[0] == "help")
- {
- cout << "Zurzeit gibt es folgende Befehle:\n\nexit - Beendet die Shell\ncls - Löscht die Bildschirmausgabe\nmf - Erstellt eine neue Datei\nmd - Erstellt einen neuen Ordner\ndel - Kann Dateien und Ordner löschen\nmov - Verschiebt Dateien\nlist - Listet alle Dateien im aktuellen Verzeichnis auf\nprint - Gibt eine Zeile Text aus\nwhoami - Gibt den aktuellen User aus." << endl;
- }
- if(args[0] == "del")
- {
- string answer;
- cout << "Sind Sie sich sicher? [J/N]: ";
- cin >> answer;
- if (answer == "j" || answer == "J")
- {
- remove(args[1].c_str());
- cout << endl;
- }
- else if (answer == "n" || answer == "N")
- {
- cout << "Vorgang vom User abgebrochen.\n";
- }
- else
- {
- cout << "Ungueltige Eingabe: " << answer << endl;
- }
- }
- else if(args[0] == "mov")
- {
- try
- {
- rename(args[1].c_str(), args[2].c_str());
- }
- catch(int ex)
- {
- cout << "Datei oder Ordner nicht gefunden!" << endl;
- }
- }
- else if(args[0] == "shell")
- {
- string cmd = args[1]+" "+args[2]+" "+args[3]+" "+args[4]+" "+args[5]+" "+args[6]+" "+args[7];
- system(cmd.c_str());
- }
- else if(args[0] == "whoami")
- {
- cout << "User: " << getuid() << endl;
- }
- else if(args[0] == "script")
- {
- string varn[100];
- string varv[100];
- int i = 0;
- try
- {
- string src;
- ifstream f;
- f.open(args[1].c_str());
- while (std::getline(f, src))
- {
- if(StartsWith(src, "start"))
- {
- cout << "### START ### --------------------|" << endl;
- }
- else if (StartsWith(src, "clear"))
- {
- clear();
- cout << "";
- }
- else if (StartsWith(src, "end"))
- {
- cout << "### END ### --------------------|" << endl;
- }
- else if (StartsWith(src, "print"))
- {
- string oust = src;
- string nvar;
- string vvar;
- string var;
- var = oust.replace(oust.find("print "), string("print ").size(), "");
- var = oust.replace(oust.find(";"), string(";").size(), "");
- int ic = 0;
- while(varn[i] != var && ic <= 100)
- {
- nvar = varn[ic];
- vvar = varv[ic];
- ic++;
- }
- if (nvar == var)
- {
- cout << vvar;
- }
- else
- {
- oust = oust.replace(oust.find("print "), string("print \"").size(), "");
- oust = oust.replace(oust.find("\";"), string("\";").size(), "");
- cout << oust << endl;
- }
- }
- else if (StartsWith(src, "in"))
- {
- string oust = src;
- //char xh[256] = {x};
- string x = NULL;
- cin >> x;
- cout << /*varv[i]*/x;
- //varn[i] = oust.replace(oust.find("in "), string("in ").size(), "");
- //varn[i] = oust.replace(oust.find(";"), string(";").size(), "");
- i++;
- }
- else if (StartsWith(src, ""))
- {
- string oust = src;
- }
- else if (StartsWith(src, ""))
- {
- }
- else if (StartsWith(src, ""))
- {
- }
- else if (StartsWith(src, ""))
- {
- }
- else if (StartsWith(src, ""))
- {
- }
- else
- {
- cout << "Error: Unbekannter Befehl \"" << src << "\"!" << endl;
- }
- }
- }
- catch(int ex)
- {
- }
- }
- else if(args[0].find("history") == 0)
- {
- string user = getlogin();
- string dat = "/home/"+user+"/.sht_history";
- string line;
- ifstream f;
- f.open(dat.c_str(), ios::out);
- while(getline(f, line) != NULL)
- {
- cout << line << endl;
- }
- }
- /*else if(args[0].find("") == 0)
- {
- }
- /*else if(args[0].find("") == 0)
- {
- }
- /*else if(args[0].find("") == 0)
- {
- }
- /*else if(args[0].find("") == 0)
- {
- }
- /*else if(args[0].find("") == 0)
- {
- }
- /*else if(args[0].find("") == 0)
- {
- }
- /*else if(args[0].find("") == 0)
- {
- }*/
- else
- {
- //cout << "Undefinierter Behfehl: " << args[0] << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement