|
| 1 | +# MyShell: Cross-Platform Shell Interaction Library |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +MyShell is a modern C++ library that provides a robust, cross-platform interface for shell process interaction. It allows seamless execution and interaction with shell commands across Windows, Linux, and macOS operating systems. |
| 8 | + |
| 9 | +- **Cross-Platform Compatibility**: Works consistently across Windows, Linux, and macOS |
| 10 | +- **Bidirectional Communication**: Full support for stdin, stdout, and stderr streams |
| 11 | +- **Real-time Output Processing**: Non-blocking I/O with efficient output buffering |
| 12 | +- **Process Management**: Monitor process status and retrieve exit codes |
| 13 | +- **Resource Safety**: RAII-compliant with automatic resource cleanup |
| 14 | +- **Thread Safety**: Thread-safe output handling with mutex protection |
| 15 | +- **Error Handling**: Comprehensive error reporting using C++ exceptions |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +Simply copy `myshell.hpp` and `myshell.cpp` into your project and include them in your build system. |
| 20 | + |
| 21 | +### Basic Example |
| 22 | + |
| 23 | +```cpp |
| 24 | +#include <chrono> |
| 25 | +#include <iostream> |
| 26 | +#include <myshell.hpp> |
| 27 | + |
| 28 | +int main() { |
| 29 | + try { |
| 30 | + // Create shell instance withcommand |
| 31 | + MyShell shell( |
| 32 | + #ifdef _WIN32 |
| 33 | + "dir" |
| 34 | + #else |
| 35 | + "ls -ral" |
| 36 | + #endif |
| 37 | + ); |
| 38 | + |
| 39 | + // Give some time for output to be collected |
| 40 | + std::this_thread::sleep_for(std::chrono::milliseconds(20)); |
| 41 | + |
| 42 | + // Read output |
| 43 | + std::string output = shell.readShellOutputStream(); |
| 44 | + std::string error = shell.readShellErrorStream(); |
| 45 | + |
| 46 | + // Wait for process completion |
| 47 | + while(!shell.hasExited()); |
| 48 | + |
| 49 | + // Print results |
| 50 | + std::cout << "Exit Code: " << shell.exitCode() << std::endl; |
| 51 | + std::cout << "Output:\n" << output << std::endl; |
| 52 | + |
| 53 | + if(!error.empty()) |
| 54 | + std::cout << "Error:\n" << error << std::endl; |
| 55 | + } |
| 56 | + catch(const std::exception& e) { |
| 57 | + std::cerr << "Error: " << e.what() << std::endl; |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### Interactive Example |
| 63 | + |
| 64 | +```cpp |
| 65 | +#include <iostream> |
| 66 | +#include <myshell.hpp> |
| 67 | +#include <thread> |
| 68 | + |
| 69 | +int main() { |
| 70 | + try { |
| 71 | + // Create shell instance withcommand |
| 72 | + MyShell shell("TERM=dumb vim -u NONE -n test.txt"); |
| 73 | + |
| 74 | + shell.writeToShell("i"); // Insert mode |
| 75 | + shell.writeToShell("Hi"); // Write the text |
| 76 | + shell.writeToShell("\u001b"); // Exit insert mode (ESC key) |
| 77 | + shell.writeToShell(":wq"); // Write command to save and quit |
| 78 | + shell.writeToShell("\n"); |
| 79 | + |
| 80 | + // Wait for process completion |
| 81 | + while(!shell.hasExited()); |
| 82 | + |
| 83 | + std::cout << "Process exited with code: " << shell.exitCode() << std::endl; |
| 84 | + } |
| 85 | + catch(const std::exception& e) { |
| 86 | + std::cerr << "Error: " << e.what() << std::endl; |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## API Reference |
| 92 | + |
| 93 | +### Constructor |
| 94 | + |
| 95 | +```cpp |
| 96 | +MyShell(std::string uuid, std::string command); |
| 97 | +``` |
| 98 | +
|
| 99 | +- **command**: Command to execute |
| 100 | +
|
| 101 | +### Methods |
| 102 | +
|
| 103 | +```cpp |
| 104 | +std::string readShellOutputStream() |
| 105 | +``` |
| 106 | + |
| 107 | +Returns accumulated stdout data and clears the internal buffer. *Non-blocking operation* |
| 108 | + |
| 109 | +```cpp |
| 110 | +std::string readShellErrorStream() |
| 111 | +``` |
| 112 | + |
| 113 | +Returns accumulated stderr data and clears the internal buffer. *Non-blocking operation* |
| 114 | + |
| 115 | +```cpp |
| 116 | +void writeToShell(std::string input) |
| 117 | +``` |
| 118 | +
|
| 119 | +Writes input to the shell's stdin. Throws std::system_error if write fails. |
| 120 | +
|
| 121 | +```cpp |
| 122 | +void forceExit() |
| 123 | +``` |
| 124 | + |
| 125 | +Forcefully terminates the shell process. Sets exit code to 1. |
| 126 | + |
| 127 | +```cpp |
| 128 | +bool hasExited() |
| 129 | +``` |
| 130 | + |
| 131 | +Returns true if the process has exited. Updates internal exit code. |
| 132 | + |
| 133 | +```cpp |
| 134 | +int exitCode() |
| 135 | +``` |
| 136 | + |
| 137 | +Returns the process exit code. Returns 0 if process is still running. |
| 138 | + |
| 139 | +```cpp |
| 140 | +int processId() |
| 141 | +``` |
| 142 | + |
| 143 | +Returns the system process ID |
| 144 | + |
| 145 | +## Platform-Specific Considerations |
| 146 | + |
| 147 | +### Windows OS Considerations |
| 148 | + |
| 149 | +- Uses Windows API (CreateProcess, pipes) |
| 150 | +- Supports both console and GUI applications |
| 151 | +- UTF-8 encoding support |
| 152 | + |
| 153 | +### Linux/macOS Considerations |
| 154 | + |
| 155 | +- Uses POSIX API (fork, exec, pipes) |
| 156 | +- Full terminal support |
| 157 | +- Signal handling support |
| 158 | + |
| 159 | +### Error Handling |
| 160 | + |
| 161 | +The library uses C++ exceptions for error reporting: |
| 162 | + |
| 163 | +- `std::system_error` for system-related errors. |
| 164 | +- `std::runtime_error` for general errors. |
| 165 | +- All error messages include detailed descriptions |
| 166 | + |
| 167 | +### Performance Considerations |
| 168 | + |
| 169 | +- Non-blocking I/O operations |
| 170 | +- Efficient buffer management |
| 171 | +- Minimal CPU usage while waiting for output |
| 172 | +- Thread-safe output handling |
| 173 | + |
| 174 | +### Thread Safety |
| 175 | + |
| 176 | +- Safe for concurrent reading of output/error streams |
| 177 | +- Safe for concurrent writing to input stream |
| 178 | +- Thread-safe process status checking |
| 179 | + |
| 180 | +## Common Issues and Solutions |
| 181 | + |
| 182 | +### Process Hanging |
| 183 | + |
| 184 | +If the process seems to hang, ensure you're: |
| 185 | + |
| 186 | +- Reading both stdout and stderr streams |
| 187 | +- Not filling up the output buffers |
| 188 | +- Properly handling interactive processes |
| 189 | + |
| 190 | +### Memory Usage |
| 191 | + |
| 192 | +The library efficiently manages memory by: |
| 193 | + |
| 194 | +- Using move semantics for string handling |
| 195 | +- Clearing buffers after reading |
| 196 | +- Automatic resource cleanup |
| 197 | + |
| 198 | +### Platform-Specific Issues |
| 199 | + |
| 200 | +1. Windows: |
| 201 | + - Use proper line endings (\r\n) |
| 202 | + - Consider code page settings |
| 203 | + - Handle Unicode properly |
| 204 | + |
| 205 | +2. Linux/macOS: |
| 206 | + - Consider terminal settings |
| 207 | + - Handle signals appropriately |
| 208 | + - Check file permissions |
| 209 | + |
| 210 | +## Contributing |
| 211 | + |
| 212 | +Contributions are welcome! Feel free to submit a pull request or open an issue for bugs or feature requests. |
| 213 | + |
| 214 | +## License |
| 215 | + |
| 216 | +MyShell is licensed under the [GNU General Public License v3](LICENSE). You are free to use, modify, and distribute this library under the terms of the license. |
0 commit comments