|
16 | 16 | * along with N8. If not, see <https://www.gnu.org/licenses/>. |
17 | 17 | */ |
18 | 18 |
|
| 19 | +/** |
| 20 | + * @file MyShell.hpp |
| 21 | + * @author [Nathanne Isip](https://github.com/nthnn) |
| 22 | + * @brief Declaration of the MyShell class for managing shell processes |
| 23 | + * and interprocess communication. |
| 24 | + * |
| 25 | + * This header defines the MyShell class, which provides an interface |
| 26 | + * for creating and interacting with shell processes. It supports both |
| 27 | + * Windows and Unix-like platforms, enabling platform-specific process |
| 28 | + * handling. |
| 29 | + */ |
19 | 30 | #ifndef MYSHELL_HPP |
20 | 31 | #define MYSHELL_HPP |
21 | 32 |
|
|
37 | 48 | # include <unistd.h> |
38 | 49 | #endif |
39 | 50 |
|
| 51 | +/** |
| 52 | + * @class MyShell |
| 53 | + * @brief Provides functionality to interact with and control |
| 54 | + * shell processes. |
| 55 | + * |
| 56 | + * MyShell facilitates spawning a shell process, sending input |
| 57 | + * to it, and reading its output and error streams. The class |
| 58 | + * is designed to be non-copyable and manages resources specific |
| 59 | + * to the target platform. |
| 60 | + */ |
40 | 61 | class MyShell { |
41 | 62 | public: |
| 63 | + /** |
| 64 | + * @brief Constructs a MyShell instance and starts the specified shell process. |
| 65 | + * @param command The command to execute in the shell. |
| 66 | + */ |
42 | 67 | MyShell(std::string command); |
| 68 | + |
| 69 | + /** |
| 70 | + * @brief Destructor that ensures the process is |
| 71 | + * terminated and resources are released. |
| 72 | + */ |
43 | 73 | ~MyShell(); |
44 | 74 |
|
| 75 | + // Deleted copy constructor and assignment operator |
| 76 | + // to ensure uniqueness of process ownership. |
45 | 77 | MyShell(const MyShell&) = delete; |
46 | 78 | MyShell& operator=(const MyShell&) = delete; |
47 | 79 |
|
| 80 | + /** |
| 81 | + * @brief Reads from the process's standard output stream. |
| 82 | + * @return A string containing the output read from the process. |
| 83 | + */ |
48 | 84 | std::string readShellOutputStream(); |
| 85 | + |
| 86 | + /** |
| 87 | + * @brief Reads from the process's standard error stream. |
| 88 | + * @return A string containing the error output read from the process. |
| 89 | + */ |
49 | 90 | std::string readShellErrorStream(); |
50 | 91 |
|
| 92 | + /** |
| 93 | + * @brief Writes input to the process's standard input stream. |
| 94 | + * @param input The string to write to the process. |
| 95 | + */ |
51 | 96 | void writeToShell(std::string input); |
52 | 97 |
|
| 98 | + /** |
| 99 | + * @brief Forces the process to terminate. |
| 100 | + * |
| 101 | + * This function sends a termination signal to the process, ensuring that it exits. |
| 102 | + */ |
53 | 103 | void forceExit(); |
| 104 | + |
| 105 | + /** |
| 106 | + * @brief Checks if the process has exited. |
| 107 | + * @return True if the process has exited; otherwise, false. |
| 108 | + */ |
54 | 109 | bool hasExited(); |
55 | 110 |
|
| 111 | + /** |
| 112 | + * @brief Retrieves the exit code of the process. |
| 113 | + * @return The exit code of the process, or -1 if the process is still running. |
| 114 | + */ |
56 | 115 | int exitCode(); |
| 116 | + |
| 117 | + /** |
| 118 | + * @brief Retrieves the ID of the process. |
| 119 | + * @return The process ID. |
| 120 | + */ |
57 | 121 | int processId(); |
58 | 122 |
|
59 | 123 | private: |
60 | | - bool procHasExited; |
61 | | - bool stopSignal; |
62 | | - int procExitCode; |
| 124 | + // Process state variables |
| 125 | + bool procHasExited; ///< Flag indicating if the process has exited. |
| 126 | + bool stopSignal; ///< Signal to stop reading threads. |
| 127 | + int procExitCode; ///< Exit code of the process. |
63 | 128 |
|
64 | | - std::string outputBuffer; |
65 | | - std::string errorBuffer; |
66 | | - std::mutex outputMutex; |
67 | | - std::mutex errorMutex; |
| 129 | + // Buffers and synchronization mechanisms |
| 130 | + std::string outputBuffer; ///< Buffer for storing standard output data. |
| 131 | + std::string errorBuffer; ///< Buffer for storing standard error data. |
| 132 | + std::mutex outputMutex; ///< Mutex for synchronizing access to the output buffer. |
| 133 | + std::mutex errorMutex; ///< Mutex for synchronizing access to the error buffer. |
68 | 134 |
|
69 | | - std::thread outputReader; |
70 | | - std::thread errorReader; |
| 135 | + // Threads for reading process output |
| 136 | + std::thread outputReader; ///< Thread for reading standard output. |
| 137 | + std::thread errorReader; ///< Thread for reading standard error. |
71 | 138 |
|
72 | | - void outputReaderThread(); |
73 | | - void errorReaderThread(); |
| 139 | + // Private methods |
| 140 | + void outputReaderThread(); ///< Thread function for reading standard output. |
| 141 | + void errorReaderThread(); ///< Thread function for reading standard error. |
74 | 142 |
|
75 | 143 | #ifdef _WIN32 |
76 | | - HANDLE procHandle; |
77 | | - HANDLE inputWriteHandle; |
78 | | - HANDLE outputReadHandle; |
79 | | - HANDLE errorReadHandle; |
80 | | - DWORD procId; |
81 | | - |
82 | | - void createWindowsProcess(const std::string& command); |
83 | | - void readFromPipe(HANDLE pipe, std::string& buffer, std::mutex& mutex); |
| 144 | + // Windows-specific members |
| 145 | + HANDLE procHandle; ///< Handle to the process. |
| 146 | + HANDLE inputWriteHandle; ///< Handle to the write end of the input pipe. |
| 147 | + HANDLE outputReadHandle; ///< Handle to the read end of the output pipe. |
| 148 | + HANDLE errorReadHandle; ///< Handle to the read end of the error pipe. |
| 149 | + DWORD procId; ///< ID of the process. |
| 150 | + |
| 151 | + // Windows-specific methods |
| 152 | + void createWindowsProcess(const std::string& command); ///< Creates a process on Windows. |
| 153 | + void readFromPipe(HANDLE pipe, std::string& buffer, std::mutex& mutex); ///< Reads data from a pipe. |
84 | 154 |
|
85 | 155 | #else |
| 156 | + // Unix-specific members |
| 157 | + pid_t procId; ///< ID of the process. |
| 158 | + int inputPipe[2]; ///< Pipe for process input. |
| 159 | + int outputPipe[2]; ///< Pipe for process output. |
| 160 | + int errorPipe[2]; ///< Pipe for process error. |
86 | 161 |
|
87 | | - pid_t procId; |
88 | | - int inputPipe[2]; |
89 | | - int outputPipe[2]; |
90 | | - int errorPipe[2]; |
91 | | - |
92 | | - void createUnixProcess(const std::string& command); |
93 | | - void readFromPipe(int pipe, std::string& buffer, std::mutex& mutex); |
94 | | - void closeUnixPipes(); |
| 162 | + // Unix-specific methods |
| 163 | + void createUnixProcess(const std::string& command); ///< Creates a process on Unix-like systems. |
| 164 | + void readFromPipe(int pipe, std::string& buffer, std::mutex& mutex); ///< Reads data from a pipe. |
| 165 | + void closeUnixPipes(); ///< Closes Unix pipes. |
95 | 166 |
|
96 | 167 | #endif |
97 | 168 |
|
98 | | - static const int bufferSize = 4096; |
| 169 | + static const int bufferSize = 4096; ///< Size of the buffer for reading process streams. |
99 | 170 | }; |
100 | 171 |
|
101 | | -#endif |
| 172 | +#endif // MYSHELL_HPP |
0 commit comments