-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.cpp
More file actions
123 lines (105 loc) · 2.92 KB
/
Commands.cpp
File metadata and controls
123 lines (105 loc) · 2.92 KB
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
120
121
122
123
#include <unistd.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <sstream>
#include <sys/wait.h>
#include <iomanip>
#include "Commands.h"
#include <time.h>
#include <utime.h>
using namespace std;
#if 0
#define FUNC_ENTRY() \
cout << __PRETTY_FUNCTION__ << " --> " << endl;
#define FUNC_EXIT() \
cout << __PRETTY_FUNCTION__ << " <-- " << endl;
#else
#define FUNC_ENTRY()
#define FUNC_EXIT()
#endif
const std::string WHITESPACE = " \n\r\t\f\v";
string _ltrim(const std::string& s)
{
size_t start = s.find_first_not_of(WHITESPACE);
return (start == std::string::npos) ? "" : s.substr(start);
}
string _rtrim(const std::string& s)
{
size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
string _trim(const std::string& s)
{
return _rtrim(_ltrim(s));
}
int _parseCommandLine(const char* cmd_line, char** args) {
FUNC_ENTRY()
int i = 0;
std::istringstream iss(_trim(string(cmd_line)).c_str());
for(std::string s; iss >> s; ) {
args[i] = (char*)malloc(s.length()+1);
memset(args[i], 0, s.length()+1);
strcpy(args[i], s.c_str());
args[++i] = NULL;
}
return i;
FUNC_EXIT()
}
bool _isBackgroundComamnd(const char* cmd_line) {
const string str(cmd_line);
return str[str.find_last_not_of(WHITESPACE)] == '&';
}
void _removeBackgroundSign(char* cmd_line) {
const string str(cmd_line);
// find last character other than spaces
unsigned int idx = str.find_last_not_of(WHITESPACE);
// if all characters are spaces then return
if (idx == string::npos) {
return;
}
// if the command line does not end with & then return
if (cmd_line[idx] != '&') {
return;
}
// replace the & (background sign) with space and then remove all tailing spaces.
cmd_line[idx] = ' ';
// truncate the command line string up to the last non-space character
cmd_line[str.find_last_not_of(WHITESPACE, idx) + 1] = 0;
}
// TODO: Add your implementation for classes in Commands.h
SmallShell::SmallShell() {
// TODO: add your implementation
}
SmallShell::~SmallShell() {
// TODO: add your implementation
}
/**
* Creates and returns a pointer to Command class which matches the given command line (cmd_line)
*/
Command * SmallShell::CreateCommand(const char* cmd_line) {
// For example:
/*
string cmd_s = _trim(string(cmd_line));
string firstWord = cmd_s.substr(0, cmd_s.find_first_of(" \n"));
if (firstWord.compare("pwd") == 0) {
return new GetCurrDirCommand(cmd_line);
}
else if (firstWord.compare("showpid") == 0) {
return new ShowPidCommand(cmd_line);
}
else if ...
.....
else {
return new ExternalCommand(cmd_line);
}
*/
return nullptr;
}
void SmallShell::executeCommand(const char *cmd_line) {
// TODO: Add your implementation here
// for example:
// Command* cmd = CreateCommand(cmd_line);
// cmd->execute();
// Please note that you must fork smash process for some commands (e.g., external commands....)
}