Skip to content

Commit 1814fed

Browse files
committed
Comprehensive in-source implementation on MyShell header file.
1 parent 38303a0 commit 1814fed

File tree

1 file changed

+100
-29
lines changed

1 file changed

+100
-29
lines changed

include/myshell.hpp

Lines changed: 100 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
* along with N8. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818

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+
*/
1930
#ifndef MYSHELL_HPP
2031
#define MYSHELL_HPP
2132

@@ -37,65 +48,125 @@
3748
# include <unistd.h>
3849
#endif
3950

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+
*/
4061
class MyShell {
4162
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+
*/
4267
MyShell(std::string command);
68+
69+
/**
70+
* @brief Destructor that ensures the process is
71+
* terminated and resources are released.
72+
*/
4373
~MyShell();
4474

75+
// Deleted copy constructor and assignment operator
76+
// to ensure uniqueness of process ownership.
4577
MyShell(const MyShell&) = delete;
4678
MyShell& operator=(const MyShell&) = delete;
4779

80+
/**
81+
* @brief Reads from the process's standard output stream.
82+
* @return A string containing the output read from the process.
83+
*/
4884
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+
*/
4990
std::string readShellErrorStream();
5091

92+
/**
93+
* @brief Writes input to the process's standard input stream.
94+
* @param input The string to write to the process.
95+
*/
5196
void writeToShell(std::string input);
5297

98+
/**
99+
* @brief Forces the process to terminate.
100+
*
101+
* This function sends a termination signal to the process, ensuring that it exits.
102+
*/
53103
void forceExit();
104+
105+
/**
106+
* @brief Checks if the process has exited.
107+
* @return True if the process has exited; otherwise, false.
108+
*/
54109
bool hasExited();
55110

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+
*/
56115
int exitCode();
116+
117+
/**
118+
* @brief Retrieves the ID of the process.
119+
* @return The process ID.
120+
*/
57121
int processId();
58122

59123
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.
63128

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.
68134

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.
71138

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.
74142

75143
#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.
84154

85155
#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.
86161

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.
95166

96167
#endif
97168

98-
static const int bufferSize = 4096;
169+
static const int bufferSize = 4096; ///< Size of the buffer for reading process streams.
99170
};
100171

101-
#endif
172+
#endif // MYSHELL_HPP

0 commit comments

Comments
 (0)