|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 - Nathanne Isip |
| 3 | + * This file is part of MyShell. |
| 4 | + * |
| 5 | + * N8 is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published |
| 7 | + * by the Free Software Foundation, either version 3 of the License, |
| 8 | + * or (at your option) any later version. |
| 9 | + * |
| 10 | + * N8 is distributed in the hope that it will be useful, but |
| 11 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with N8. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <chrono> |
| 20 | +#include <iostream> |
| 21 | +#include <myshell.hpp> |
| 22 | + |
| 23 | +int main() { |
| 24 | + try { |
| 25 | + // Create shell instance withcommand |
| 26 | + MyShell shell( |
| 27 | + #ifdef _WIN32 |
| 28 | + "dir" |
| 29 | + #else |
| 30 | + "ls -ral" |
| 31 | + #endif |
| 32 | + ); |
| 33 | + |
| 34 | + // Give some time for output to be collected |
| 35 | + std::this_thread::sleep_for(std::chrono::milliseconds(20)); |
| 36 | + |
| 37 | + // Read output |
| 38 | + std::string output = shell.readShellOutputStream(); |
| 39 | + std::string error = shell.readShellErrorStream(); |
| 40 | + |
| 41 | + // Wait for process completion |
| 42 | + while(!shell.hasExited()); |
| 43 | + |
| 44 | + // Print results |
| 45 | + std::cout << "Exit Code: " << shell.exitCode() << std::endl; |
| 46 | + std::cout << "Output:\n" << output << std::endl; |
| 47 | + |
| 48 | + if(!error.empty()) |
| 49 | + std::cout << "Error:\n" << error << std::endl; |
| 50 | + } |
| 51 | + catch(const std::exception& e) { |
| 52 | + std::cerr << "Error: " << e.what() << std::endl; |
| 53 | + } |
| 54 | +} |
0 commit comments