Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //by Maksym Oboznyi
- #include <iostream>
- using namespace std;
- int main(int argc, char ** argv, char ** envp)
- {
- cout << "Welcome to command line emulator\nVersion 0.0.7 by Maksym Oboznyi, K-16\n";
- while (1) {
- cout << "promt>";
- string query;
- cin >> query;
- if (query == "quit") {
- cout << "Slava Bitu! Have a nice day.\n";
- return 0;
- }
- if (query == "argc") {
- cout << argc << "\n";
- getline(cin, query);///spaces, other trash after the command
- continue;
- }
- if (query == "argv") {
- for (int i = 0; i < argc; i++)
- cout << argv[i] << "\n";
- getline(cin, query);///spaces, other trash after the command
- continue;
- }
- if (query == "envp") {
- for (int i = 0; envp[i]; i++)
- cout << envp[i] << "\n";
- getline(cin, query);///spaces, other trash after the command
- continue;
- }
- if (query == "echo") {
- getline(cin, query);
- int first_good = 0;
- while (first_good < query.size() && query[first_good] == ' ' )
- first_good++;
- if (query[first_good] == '\n') { ///empty string
- cout << "\n";
- continue;
- }
- if (query[first_good] != '\"') { ///one-line query
- string_view s = query;
- while (s.size() > 0) {
- s.remove_prefix(min(s.find_first_not_of(" "), s.size()));
- if (s.size() > 0) {
- cout << s.substr(0, s.find(" ")) << endl;
- s.remove_prefix(min(s.find(" "), s.size()));
- } else
- break;
- }
- } else {
- string whole_query = "";
- for (int i = first_good + 1; i < query.size(); i++)
- whole_query += query[i];
- bool was = 0; ///reading until the second "
- while (!was) {
- whole_query += " ";///divide words
- getline(cin, query);
- for (int i = 0; i < query.size(); i++)
- if (query[i] == '\"') {
- was = 1;
- break;
- }
- else
- whole_query += query[i];
- }
- string_view s = whole_query;
- while (s.size() > 0) {
- s.remove_prefix(min(s.find_first_not_of(" "), s.size()));
- if (s.size() > 0) {
- cout << s.substr(0, s.find(" ")) << endl;
- s.remove_prefix(min(s.find(" "), s.size()));
- } else
- break;
- }
- }
- continue;
- }
- cout << "ERROR: No command found: " << query << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement