-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmallShell.h
executable file
·119 lines (100 loc) · 2.51 KB
/
SmallShell.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once
#include "BuiltInCommands.h"
#include "ExternalCommands.h"
// #include "PipeCommand.h"
// #include "RedirectionCommand.h"
#include <string>
#include <map>
#include <linux/limits.h>
#include "CommandUtils.h"
#include "JobsList.h"
#define NO_JOB -1
class SmallShell
{
private:
std::string prompt = "smash> ";
int PID = 0;
std::string prevPath;
int foregroundJob = NO_JOB;
JobsList _jobsList;
bool running = true;
SmallShell();
public:
~SmallShell();
SmallShell(SmallShell const &) = delete; // disable copy ctor
void operator=(SmallShell const &) = delete; // disable = operator
void executeCommand(const char *cmd_line);
Command *CreateCommand(const char *cmd_line);
// todo: err enum
bool ctrlZHandler()
{
if (foregroundJob == NO_JOB)
{
return false;
}
auto &job = SmallShell::getInstance().getJobsList().getJobById(foregroundJob);
job.stopProcess();
job.timeStarted = time(nullptr);
job.isForeground = false;
foregroundJob = NO_JOB;
return true;
}
bool ctrlCHandler()
{
if (foregroundJob == NO_JOB)
{
return false;
}
auto &job = SmallShell::getInstance().getJobsList().getJobById(foregroundJob);
job.killProcess();
foregroundJob = NO_JOB;
return true;
}
bool alarmHandler()
{
SmallShell::getInstance().getJobsList().timeoutJob();
}
void setForeground(int job)
{
foregroundJob = job;
if (job != NO_JOB)
{
_jobsList.getJobById(job).isForeground = true;
return;
}
}
int
getPid();
std::string getWorkingDir();
JobsList &getJobsList();
void setPrevPath(const std::string &path)
{
prevPath = path;
}
std::string getPrevPath()
{
return prevPath;
}
std::string getPrompt()
{
return prompt;
}
void setPrompt(std::string &prompt)
{
this->prompt = prompt + "> ";
}
static SmallShell &getInstance() // make SmallShell singleton
{
static SmallShell instance; // Guaranteed to be destroyed.
// Instantiated on first use.
return instance;
}
bool isRunning() const
{
return running;
}
void setRunning(bool running)
{
this->running = running;
}
};