Advertisement
MaxObznyi

Emulator

Apr 9th, 2021
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. //by Maksym Oboznyi
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char ** argv, char ** envp)
  7. {
  8.     cout << "Welcome to command line emulator\nVersion 0.0.7 by Maksym Oboznyi, K-16\n";
  9.     while (1) {
  10.         cout << "promt>";
  11.         string query;
  12.         cin >> query;
  13.         if (query == "quit") {
  14.             cout << "Slava Bitu! Have a nice day.\n";
  15.             return 0;
  16.         }
  17.         if (query == "argc") {
  18.             cout << argc << "\n";
  19.             getline(cin, query);///spaces, other trash after the command
  20.             continue;
  21.         }
  22.         if (query == "argv") {
  23.             for (int i = 0; i < argc; i++)
  24.                 cout << argv[i] << "\n";
  25.             getline(cin, query);///spaces, other trash after the command
  26.             continue;
  27.         }
  28.         if (query == "envp") {
  29.             for (int i = 0; envp[i]; i++)
  30.                 cout << envp[i] << "\n";
  31.             getline(cin, query);///spaces, other trash after the command
  32.             continue;
  33.         }
  34.         if (query == "echo") {
  35.             getline(cin, query);
  36.             int first_good = 0;
  37.             while (first_good < query.size() && query[first_good] == ' ' )
  38.                 first_good++;
  39.  
  40.             if (query[first_good] == '\n') { ///empty string
  41.                 cout << "\n";
  42.                 continue;
  43.             }
  44.  
  45.             if (query[first_good] != '\"') { ///one-line query
  46.                 string_view s = query;
  47.                 while (s.size() > 0) {
  48.                     s.remove_prefix(min(s.find_first_not_of(" "), s.size()));
  49.                     if (s.size() > 0) {
  50.                         cout << s.substr(0, s.find(" ")) << endl;
  51.                         s.remove_prefix(min(s.find(" "), s.size()));
  52.                     } else
  53.                         break;
  54.                 }
  55.             } else {
  56.                 string whole_query = "";
  57.                 for (int i = first_good + 1; i < query.size(); i++)
  58.                     whole_query += query[i];
  59.                 bool was = 0; ///reading until the second "
  60.                 while (!was) {
  61.                     whole_query += " ";///divide words
  62.                     getline(cin, query);
  63.                     for (int i = 0; i < query.size(); i++)
  64.                         if (query[i] == '\"') {
  65.                             was = 1;
  66.                             break;
  67.                         }
  68.                         else
  69.                             whole_query += query[i];
  70.                 }
  71.                 string_view s = whole_query;
  72.                 while (s.size() > 0) {
  73.                     s.remove_prefix(min(s.find_first_not_of(" "), s.size()));
  74.                     if (s.size() > 0) {
  75.                         cout << s.substr(0, s.find(" ")) << endl;
  76.                         s.remove_prefix(min(s.find(" "), s.size()));
  77.                     } else
  78.                         break;
  79.                 }
  80.             }
  81.             continue;
  82.         }
  83.         cout << "ERROR: No command found: " << query << "\n";
  84.     }
  85.     return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement