-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.h
177 lines (147 loc) · 4.18 KB
/
builtins.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
namespace builtins
{
using args_t = std::vector<std::string>;
int cd(const args_t& args)
{
const std::size_t len = args.size();
if(len > 2)
{
print_err_fmt("shellter: cd: too many arguments\n");
}
fs::path next_path;
if(len == 1)
{
next_path = home;
}
else
{
if(args[1] == "..")
{
next_path = fs::current_path().parent_path();
}
else if(args[1] == "-")
{
if(!old_path_set)
{
print_err_fmt("shellter: OLDPATH not set\n");
return EXIT_FAILURE;
}
next_path = old_path;
}
else
{
next_path = args[1];
}
}
if(!fs::is_directory(next_path))
{
print_err_fmt("shellter: cd: {}: No such file or directory\n",
next_path.c_str());
return EXIT_FAILURE;
}
old_path = fs::current_path();
old_path_set = true;
fs::current_path(next_path);
return EXIT_SUCCESS;
}
int echo(const args_t& args)
{
const std::size_t len = args.size();
for(std::size_t i = 1; i < len - 1; ++i)
{
fmt::print("{} ", args[i]);
}
if(len > 1)
{
fmt::print("{}\n", args.back());
}
return EXIT_SUCCESS;
}
int exit(const args_t& args)
{
const std::size_t len = args.size();
if(len == 1)
{
::exit(EXIT_SUCCESS);
}
if(len == 2)
{
::exit(std::stoi(args[1]));
}
print_err_fmt("shellter: exit: too many arguments\n");
return EXIT_FAILURE;
}
int pwd(const args_t& args)
{
const std::size_t len = args.size();
if(len > 1)
{
print_err_fmt("shellter: pwd: too many arguments\n");
return EXIT_FAILURE;
}
fmt::print("{}\n", fs::current_path().c_str());
return EXIT_SUCCESS;
}
int history(const args_t& args)
{
const std::size_t len = args.size();
if(len > 1)
{
print_err_fmt("shellter: history: too many arguments\n");
return EXIT_FAILURE;
}
for(std::size_t i = 0; i < line_history.size(); ++i)
{
fmt::print(" {} {}\n", i + 1, line_history[i]);
}
return EXIT_SUCCESS;
}
int addenv(const args_t& args)
{
const std::size_t len = args.size();
if(len != 3 || args[1].front() != '$')
{
print_err_fmt("shellter: addenv usage: addenv $VARNAME VALUE\n");
return EXIT_FAILURE;
}
environment_vars[args[1]] = args[2];
return EXIT_SUCCESS;
}
int eaddenv(const args_t& args)
{
const std::size_t len = args.size();
if(len != 3 || args[1].front() != '$')
{
print_err_fmt("shellter: eaddenv usage: eaddenv $VARNAME VALUE\n");
return EXIT_FAILURE;
}
if(setenv(args[1].data() + 1, args[2].data(), 1) < 0)
{
return EXIT_FAILURE;
}
environment_vars[args[1]] = args[2];
return EXIT_SUCCESS;
}
int quit(const args_t& args)
{
const std::size_t len = args.size();
if(len > 1)
{
print_err_fmt("shellter: quit: too many arguments\n");
return EXIT_FAILURE;
}
running = false;
return EXIT_SUCCESS;
}
} // namespace builtins
using builtin_func_t = int (*)(const builtins::args_t&);
static const std::unordered_map<std::string_view, builtin_func_t> builtin_funcs = {
{ "cd", &builtins::cd },
{ "echo", &builtins::echo },
{ "exit", &builtins::exit },
{ "pwd", &builtins::pwd },
{ "history", &builtins::history },
{ "addenv", &builtins::addenv },
{ "eaddenv", &builtins::eaddenv },
{ "quit", &builtins::quit }
};