Skip to content

Commit fd03224

Browse files
committed
Initial MyShell header file.
1 parent 3bb5acf commit fd03224

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

include/myshell.hpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
#ifndef MYSHELL_HPP
20+
#define MYSHELL_HPP
21+
22+
#include <condition_variable>
23+
#include <memory>
24+
#include <mutex>
25+
#include <queue>
26+
#include <string>
27+
#include <thread>
28+
#include <vector>
29+
30+
#ifdef _WIN32
31+
# include <windows.h>
32+
#else
33+
# include <fcntl.h>
34+
# include <signal.h>
35+
# include <sys/types.h>
36+
# include <sys/wait.h>
37+
# include <unistd.h>
38+
#endif
39+
40+
class MyShell {
41+
public:
42+
MyShell(std::string command);
43+
~MyShell();
44+
45+
MyShell(const MyShell&) = delete;
46+
MyShell& operator=(const MyShell&) = delete;
47+
48+
std::string readShellOutputStream();
49+
std::string readShellErrorStream();
50+
51+
void writeToShell(std::string input);
52+
53+
void forceExit();
54+
bool hasExited();
55+
56+
int exitCode();
57+
int processId();
58+
59+
private:
60+
bool procHasExited;
61+
bool stopSignal;
62+
int procExitCode;
63+
64+
std::string outputBuffer;
65+
std::string errorBuffer;
66+
std::mutex outputMutex;
67+
std::mutex errorMutex;
68+
69+
std::thread outputReader;
70+
std::thread errorReader;
71+
72+
void outputReaderThread();
73+
void errorReaderThread();
74+
75+
#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);
84+
85+
#else
86+
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();
95+
96+
#endif
97+
98+
static const int bufferSize = 4096;
99+
};
100+
101+
#endif

0 commit comments

Comments
 (0)