-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.c
232 lines (195 loc) · 5 KB
/
builtin.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "builtin.h"
#include <limits.h>
#include "utils.h"
#include "parsing.h"
#include "freecmd.h"
#include "exec.h"
static const int EXECUTED = (int) true, NOT_EXECUTED = (int) false;
static const char EXIT_CMD_STR[] = "exit";
static const int LEN_EXIT_CMD_STR = 5;
static const char CD_CMD_STR[] = "cd";
static const int LEN_CD_CMD_STR = 3;
static const char PWD_CMD_STR[] = "pwd";
static const int LEN_PWD_CMD_STR = 4;
static const char HISTORY_CMD_STR[] = "history";
static const int LEN_HISTORY_CMD_STR = 8;
#ifndef SHELL_NO_INTERACTIVE
static const int HISTORY_OPTIONAL_PARAM_COUNT = 1;
static const int HISTORY_LAST_NTH_AMOUNT_POS = 1;
#endif
// returns true if the 'exit' call
// should be performed
//
// (It must not be called from here)
int
exit_shell(char *cmd, int *status)
{
if (strncmp(cmd, EXIT_CMD_STR, LEN_EXIT_CMD_STR) != 0) {
return NOT_EXECUTED;
}
*status = EXIT_SUCCESS;
return EXECUTED;
}
/*
* Returns true if the `cmd` string starts with `CD_CMD_STR`. Returns false otherwise
*/
static bool
is_cd_command(char *cmd)
{
int cmd_len = strlen(cmd);
int max_iter = MIN(LEN_CD_CMD_STR - 1, cmd_len);
int i = 0;
for (i = 0; i < max_iter; i++) {
if (cmd[i] != CD_CMD_STR[i]) {
return false;
}
}
if (i <= cmd_len && i >= LEN_CD_CMD_STR - 1) {
return cmd[i] == END_STRING || cmd[i] == SPACE;
}
return false;
}
/*
* Returns true if the string `path` has at least a character that is not a
* space character. Returns false otherwise
*/
static bool
is_non_empty(char *path)
{
int max_iter = MIN(PATH_MAX, strlen(path));
for (int i = 0; i < max_iter; i++) {
if (path[i] != SPACE) {
return true;
}
}
return false;
}
// returns true if "chdir" was performed
// this means that if 'cmd' contains:
// 1. $ cd directory (change to 'directory')
// 2. $ cd (change to $HOME)
// it has to be executed and then return true
//
// Remember to update the 'prompt' with the
// new directory.
//
// Examples:
// 1. cmd = ['c','d', ' ', '/', 'b', 'i', 'n', '\0']
// 2. cmd = ['c','d', '\0']
int
cd(char *cmd, char *prompt, int *status)
{
if (!is_cd_command(cmd)) {
return NOT_EXECUTED;
}
char dest_path[PATH_MAX] = { END_STRING };
char *temp_path = strchr(cmd, SPACE);
if (temp_path != NULL && is_non_empty(temp_path)) {
strncpy(dest_path, temp_path + 1, PATH_MAX - 1);
} else {
char *home_path = getenv(HOME_ENV_VAR_KEY);
if (home_path == NULL) {
perror("Error while retrieving home path");
*status = EXIT_FAILURE;
return EXECUTED;
}
strncpy(dest_path, home_path, PATH_MAX - 1);
}
char full_rest_path[PATH_MAX] = { END_STRING };
char *res = realpath(dest_path, full_rest_path);
if (res == NULL) {
perror("Error while retrieving realpath");
*status = EXIT_FAILURE;
return EXECUTED;
}
int _res = chdir(full_rest_path);
if (_res == GENERIC_ERROR_CODE) {
perror("Error while changing directory");
*status = EXIT_FAILURE;
return EXECUTED;
}
update_prompt(prompt, full_rest_path);
*status = EXIT_SUCCESS;
return EXECUTED;
}
// returns true if 'pwd' was invoked
// in the command line
//
// (It has to be executed here and then
// return true)
int
pwd(char *cmd, int *status)
{
if (strncmp(cmd, PWD_CMD_STR, LEN_PWD_CMD_STR) != 0) {
return NOT_EXECUTED;
}
char cwd_path[PATH_MAX] = { END_STRING };
char *res = getcwd(cwd_path, PATH_MAX);
if (res == NULL) {
perror("Error while executing pwd");
*status = EXIT_FAILURE;
return EXECUTED;
}
printf("%s\n", cwd_path);
*status = EXIT_SUCCESS;
return EXECUTED;
}
static bool
is_history_command(char *cmd)
{
int cmd_len = strlen(cmd);
int max_iter = MIN(LEN_HISTORY_CMD_STR - 1, cmd_len);
int i = 0;
for (i = 0; i < max_iter; i++) {
if (cmd[i] != HISTORY_CMD_STR[i]) {
return false;
}
}
if (i <= cmd_len && i >= LEN_HISTORY_CMD_STR - 1) {
return cmd[i] == END_STRING || cmd[i] == SPACE;
}
return false;
}
// returns true if `history` was invoked
// in the command line
//
// (It has to be executed here and then
// return true)
int
history(char *cmd, int *status, history_data_t *history)
{
#ifndef SHELL_NO_INTERACTIVE
struct cmd *parsed_cmd;
parsed_cmd = parse_line(cmd, status);
if (parsed_cmd->type != EXEC) {
free_command(parsed_cmd);
return NOT_EXECUTED;
}
struct execcmd *history_cmd = (struct execcmd *) parsed_cmd;
if (!is_history_command(history_cmd->argv[0])) {
free_command(parsed_cmd);
return NOT_EXECUTED;
}
if (history_cmd->argc > HISTORY_OPTIONAL_PARAM_COUNT + 1) {
perror("Error at calling history command. Expected: history "
"[n]");
free_command(parsed_cmd);
return EXECUTED;
}
set_environ_vars(history_cmd->eargv, history_cmd->eargc);
if (history_cmd->argc == HISTORY_OPTIONAL_PARAM_COUNT + 1) {
int n = atoi(history_cmd->argv[HISTORY_LAST_NTH_AMOUNT_POS]);
history_print_last_n(history, n, status);
} else {
history_print_all(history, status);
}
free_command(parsed_cmd);
return EXECUTED;
#else
MARK_UNUSED_ALWAYS(cmd);
MARK_UNUSED_ALWAYS(status);
MARK_UNUSED_ALWAYS(history);
MARK_UNUSED_ALWAYS(is_history_command);
return NOT_EXECUTED;
#endif
}