Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.14 KB | None | 0 0
  1. /*
  2.  * SHT.cpp
  3.  *
  4.  *  Created on: 29.01.2016
  5.  *      Author: armised
  6.  */
  7. #include <algorithm>
  8. #include <string>
  9. #include <sstream>
  10. #include <iostream>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <fstream>
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18.  
  19. using namespace std;
  20. int ghi = 0;
  21.  
  22. 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; }
  23. bool is_file_exist(const char *fileName) { std::ifstream infile(fileName); return infile.good(); }
  24. bool StartsWith(const std::string& text,const std::string& token)
  25. {
  26.     if(text.length() < token.length())
  27.         return false;
  28.     return (text.compare(0, token.length(), token) == 0);
  29. }
  30. void clear() {
  31.     // CSI[2J clears screen, CSI[H moves the cursor to top-left corner
  32.     std::cout << "\x1B[2J\x1B[H";
  33. }
  34.  
  35.  
  36. ///////// MAIN \\\\\\\\/
  37.  
  38. int main(int argc, char* argv[])
  39. {
  40.     char path[256];
  41.     getcwd(path, 256);
  42.     char rawin[256];
  43.     string exit =rawin;
  44.  
  45.     cout << "SHT Shell - Version 1.0.3\nBereit Befehle entgegen zu nehmen.\n";
  46.  
  47.     while (true)
  48.     {
  49.  
  50.         system("echo \"\\033[1;33m\\c\"");
  51.             cout << path << " # ";
  52.         cin.getline(rawin, 256);
  53.         system("echo \"\\033[00m\\c\"");
  54.         //cin >> rawin;
  55.         //cout << rawin << endl;
  56.  
  57.         string in = rawin;
  58.         string args[300];
  59.         int i = 0;
  60.         stringstream ssin(in);
  61.  
  62.         while (ssin.good() && i < 300)
  63.         {
  64.             ssin >> args[i];
  65.             ++i;
  66.         }
  67.  
  68.         string xstr = "/system/bin/"+args[0];
  69.  
  70.         if (args[0] == "print")
  71.         {
  72.             int ix;
  73.             int argv = sizeof(args)/sizeof(*args);
  74.  
  75.             for(int ix = 1; ix < argv; ix++)
  76.             {
  77.                 cout << args[ix] << " ";
  78.             }
  79.  
  80.             cout << endl;
  81.         }
  82.         else if (args[0] == "exit")
  83.         {
  84.             string ans;
  85.             cout << "Wollen sie die Shell wirklich stoppen?\n[j/n]: ";
  86.             cin >> ans;
  87.             if (ans == "j" || ans == "J")
  88.             {
  89.                 cout << "Shuttding down...\nBB ;)" << endl;
  90.                 system("shutdown -s 1");
  91.                 return 0;
  92.             }
  93.             else
  94.             {
  95.                 cout << "Vorgang vom User abgebrochen.\n";
  96.             }
  97.  
  98.         }
  99.         else if (args[0] == "list")
  100.         {
  101.             system("ls");
  102.         }
  103.         else if (args[0] == "cd")
  104.         {
  105.             string x = args[1].c_str();
  106.             const char* f = x.c_str();
  107.             if (chdir(f) != 0)
  108.             {
  109.                 cout << "Das angegebene Verzeichnis konnte nicht gefunden werden\noder sie besitzen keine Berechtigung dazu.\n";
  110.             }
  111.             else
  112.             {
  113.                 char xpath[256];
  114.                 getcwd(xpath, 256);
  115.                 getcwd(path, 256);
  116.                 cout << "Du befindest dich jetzt in: " << xpath << endl;
  117.             }
  118.         }
  119.         else if(args[0] == "mf")
  120.         {
  121.             string f = args[1];
  122.             const char *x = f.c_str();
  123.  
  124.             ofstream file;
  125.             file.open(x);
  126.             file << "New File";
  127.             file.close();
  128.  
  129.             if (::ghi == 0)
  130.             {
  131.                 cout << "Hinweis: Sie können nur eine Datei pro Kommando erstellen.\n";
  132.                 ::ghi++;
  133.             }
  134.         }
  135.         else if(args[0] == "cls" || args[0] == "clear")
  136.         {
  137.             clear();
  138.             cout << "";
  139.         }
  140.         else if(args[0] == "md")
  141.         {
  142.             if(!mkdir(args[1].c_str(), 0755))
  143.             {
  144.                 cout << "Das Verzeichnis "+args[1]+" wurde erfolgreich erstellt!" << endl;
  145.             }
  146.             else
  147.             {
  148.                 cout << "Unbekannter Error beim erstellen aufgetreten!" << endl;
  149.             }
  150.         }
  151.         else if(args[0] == "help")
  152.         {
  153.             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;
  154.         }
  155.         if(args[0] == "del")
  156.         {
  157.             string answer;
  158.  
  159.             cout << "Sind Sie sich sicher? [J/N]: ";
  160.             cin >> answer;
  161.  
  162.             if (answer == "j" || answer == "J")
  163.             {
  164.                 remove(args[1].c_str());
  165.                 cout << endl;
  166.             }
  167.             else if (answer == "n" || answer == "N")
  168.             {
  169.                 cout << "Vorgang vom User abgebrochen.\n";
  170.             }
  171.             else
  172.             {
  173.                 cout << "Ungueltige Eingabe: " << answer << endl;
  174.             }
  175.         }
  176.         else if(args[0] == "mov")
  177.         {
  178.             try
  179.             {
  180.                 rename(args[1].c_str(), args[2].c_str());
  181.             }
  182.             catch(int ex)
  183.             {
  184.                 cout << "Datei oder Ordner nicht gefunden!" << endl;
  185.             }
  186.         }
  187.         else if(args[0] == "shell")
  188.         {
  189.             string cmd = args[1]+" "+args[2]+" "+args[3]+" "+args[4]+" "+args[5]+" "+args[6]+" "+args[7];
  190.             system(cmd.c_str());
  191.         }
  192.         else if(args[0] == "whoami")
  193.         {
  194.             cout << "User: " << getuid() << endl;
  195.         }
  196.         else if(args[0] == "script")
  197.         {
  198.             string varn[100];
  199.             string varv[100];
  200.             int i = 0;
  201.  
  202.             try
  203.             {
  204.                 string src;
  205.                 ifstream f;
  206.  
  207.                 f.open(args[1].c_str());
  208.                 while (std::getline(f, src))
  209.                 {
  210.                     if(StartsWith(src, "start"))
  211.                     {
  212.                         cout << "### START ### --------------------|" << endl;
  213.                     }
  214.                     else if (StartsWith(src, "clear"))
  215.                     {
  216.                         clear();
  217.                         cout << "";
  218.                     }
  219.                     else if (StartsWith(src, "end"))
  220.                     {
  221.                         cout << "###  END  ### --------------------|" << endl;
  222.                     }
  223.                     else if (StartsWith(src, "print"))
  224.                     {
  225.                         string oust = src;
  226.                         string nvar;
  227.                         string vvar;
  228.                         string var;
  229.  
  230.                         var = oust.replace(oust.find("print "), string("print ").size(), "");
  231.                         var = oust.replace(oust.find(";"), string(";").size(), "");
  232.  
  233.                         int ic = 0;
  234.                         while(varn[i] != var && ic <= 100)
  235.                         {
  236.                             nvar = varn[ic];
  237.                             vvar = varv[ic];
  238.                             ic++;
  239.                         }
  240.                         if (nvar == var)
  241.                         {
  242.                             cout << vvar;
  243.                         }
  244.                         else
  245.                         {
  246.                             oust = oust.replace(oust.find("print "), string("print \"").size(), "");
  247.                             oust = oust.replace(oust.find("\";"), string("\";").size(), "");
  248.                             cout << oust << endl;
  249.                         }
  250.                     }
  251.                     else if (StartsWith(src, "in"))
  252.                     {
  253.                         string oust = src;
  254.                         //char xh[256] = {x};
  255.                         string x = NULL;
  256.                         cin >> x;
  257.                         cout << /*varv[i]*/x;
  258.                         //varn[i] = oust.replace(oust.find("in "), string("in ").size(), "");
  259.                         //varn[i] = oust.replace(oust.find(";"), string(";").size(), "");
  260.                         i++;
  261.                     }
  262.                     else if (StartsWith(src, ""))
  263.                     {
  264.                         string oust = src;
  265.  
  266.                     }
  267.                     else if (StartsWith(src, ""))
  268.                     {
  269.                     }
  270.                     else if (StartsWith(src, ""))
  271.                     {
  272.  
  273.                     }
  274.                     else if (StartsWith(src, ""))
  275.                     {
  276.  
  277.                     }
  278.                     else if (StartsWith(src, ""))
  279.                     {
  280.  
  281.                     }
  282.                     else
  283.                     {
  284.                         cout << "Error: Unbekannter Befehl \"" << src << "\"!" << endl;
  285.                     }
  286.                 }
  287.             }
  288.             catch(int ex)
  289.             {
  290.  
  291.             }
  292.         }
  293.         else if(args[0].find("history") == 0)
  294.         {
  295.               string user = getlogin();
  296.               string dat = "/home/"+user+"/.sht_history";
  297.               string line;
  298.               ifstream f;
  299.  
  300.               f.open(dat.c_str(), ios::out);
  301.               while(getline(f, line) != NULL)
  302.               {
  303.                 cout << line << endl;
  304.               }
  305.         }
  306.         /*else if(args[0].find("") == 0)
  307.         {
  308.  
  309.         }
  310.         /*else if(args[0].find("") == 0)
  311.         {
  312.  
  313.         }
  314.         /*else if(args[0].find("") == 0)
  315.         {
  316.  
  317.         }
  318.         /*else if(args[0].find("") == 0)
  319.         {
  320.  
  321.         }
  322.         /*else if(args[0].find("") == 0)
  323.         {
  324.  
  325.         }
  326.         /*else if(args[0].find("") == 0)
  327.         {
  328.  
  329.         }
  330.         /*else if(args[0].find("") == 0)
  331.         {
  332.  
  333.         }*/
  334.         else
  335.         {
  336.             //cout << "Undefinierter Behfehl: " << args[0] << endl;
  337.         }
  338.     }
  339.  
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement