-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.c
More file actions
302 lines (269 loc) · 8.1 KB
/
Copy pathcommand.c
File metadata and controls
302 lines (269 loc) · 8.1 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* Classes to hold the data associated with a single command.
A command can be broken up into small subcommands.
*/
#include <dirent.h>
#include <regex.h>
#include <stdio.h>
#include<stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "command.h"
static const char * const shell_name = "buc";
sub_command_t* sub_command_factory() {
/**
* Allocate space for sub_command_t.
*
* Allocate space for 1 element in array and make NULL per execvp() documentation.
*/
sub_command_t *sub_cmd = malloc(sizeof(sub_command_t));
sub_cmd->args = malloc(sizeof(char *));
sub_cmd->args[0] = NULL;
sub_cmd->arg_count = 0;
return sub_cmd;
}
void delete_sub_command(sub_command_t* sub_cmd)
{
for (int i=0; i<sub_cmd->arg_count; ++i) {
free(sub_cmd->args[i]);
}
free(sub_cmd->args);
free(sub_cmd);
}
void sub_command_insert(sub_command_t* sub_cmd, char* argument)
{
/**
* Append new argument to args array and increase counter.
*
* Maintain extra NULL element in args array.
*/
++(sub_cmd->arg_count);
sub_cmd->args = realloc(sub_cmd->args, (1 + sub_cmd->arg_count) * sizeof(char *));
sub_cmd->args[sub_cmd->arg_count - 1] = argument;
sub_cmd->args[sub_cmd->arg_count] = NULL;
}
/**
* Convert an expression to a regular expression.
*
* Dynamically allocates new memory for regular expression.
* Adds on end and start of line matching
*/
char* convert_to_regex(const char *str) {
char * const regex = (char*) malloc((2 * strlen(str) + 2) * sizeof(char));
char *r = regex;
*r = '^'; // Match start of line
++r;
while (*str) {
if (*str == '*') {
*r = '.';
++r;
*r = '*';
} else {
*r = *str;
}
++r;
++str;
}
*r = '$'; // Match end of line
++r;
*r = '\0'; // End c-string
return regex;
}
void insert_expand_wildcards(sub_command_t *sub_cmd, char *arg) {
// Insert arg and return if no special charecters
if (strchr(arg, '*') == NULL) {
sub_command_insert(sub_cmd, arg);
return;
}
char *regex = convert_to_regex(arg);
regex_t re;
if (regcomp(&re, regex, 0) != 0) {
perror("regcomp");
return;
}
DIR * dir = opendir("."); // Probably need to split for e.g dir/file*
if (dir == NULL) {
perror("opendir");
return;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (regexec(&re, entry->d_name, 0, NULL, 0) == 0) {
sub_command_insert(sub_cmd, strdup(entry->d_name));
}
}
closedir(dir);
// Cleanup dynamically allocated strings
free(regex);
free(arg);
}
command_t* command_factory()
{
command_t *cmd = malloc(sizeof(command_t));
cmd->sub_cmd_count = 0;
cmd->sub_cmds = NULL;
return cmd;
}
void delete_command(command_t *cmd)
{
for (int i=0; i<cmd->sub_cmd_count; ++i) {
delete_sub_command(cmd->sub_cmds[i]);
}
free(cmd->sub_cmds);
free(cmd);
}
void command_insert(command_t* cmd, sub_command_t *sub_cmd)
{
++(cmd->sub_cmd_count);
cmd->sub_cmds = realloc(cmd->sub_cmds, cmd->sub_cmd_count * sizeof(sub_command_t *));
cmd->sub_cmds[cmd->sub_cmd_count - 1] = sub_cmd;
}
void restore_file_desc(int fd_saved, int fd_to_restore) {
dup2(fd_saved, fd_to_restore);
close(fd_saved);
}
int setup_output_file(int std_desc, bool append, const char * const file_name) {
int fd;
if (file_name) { // If user specified a file
int flags;
flags = O_WRONLY | O_CREAT;
if (append) {
flags = flags | O_APPEND;
}
fd = open(file_name, flags, 0777); // Open new file, fd
if (fd < 0) {
printf("%s: No such file or directory: %s\n", shell_name, file_name);
return -1;
}
} else {
fd = dup(std_desc); // err_out points to stderr
}
return fd;
}
/**
* Check to see if the requested command is a builtin function.
*
* Cannot fork in this case since need the change to be associated
* with the shell's process.
*
* If found run the proper command
*
* Returns:
* - 0 for success
* - -1 on error
* - 1 if not a builtin command
*/
bool check_and_run_builtin(sub_command_t *sub_cmd, int* const exit_code) {
if (strcmp(sub_cmd->args[0], "cd") == 0) {
if (sub_cmd->arg_count != 2) {
*exit_code = -1;
} else {
*exit_code = chdir(sub_cmd->args[1]);
}
return true;
}
return false;
}
/**
* Fork and handle child and parent paths.
*/
bool spawn_subproc(sub_command_t *sub_cmd, command_t *cmd, int *status, bool last) {
int exit_code;
bool found = check_and_run_builtin(sub_cmd, &exit_code);
if (found) {
return exit_code == 0;
}
int ret = fork();
if (ret == 0) { // Child
execvp(sub_cmd->args[0], sub_cmd->args); // Should never return
return false;
} else if (ret < 0) { // Error in fork
perror("fork");
exit(1);
} else { // parent, ret == pid
if (last && !cmd->background) {
waitpid(ret, status, 0);
}
}
return true;
}
bool execute(command_t *cmd)
{
if (cmd->sub_cmd_count == 0) return true; // If no sub-commands nothing to-do
// Save copies of all the std in/out/err file descriptors.
int tmp_in = dup(0); // New fd pointing to stdin, saving for later in case we override
int tmp_out = dup(1); // New fd pointing to stdout, saving for later in case we override
int tmp_err = dup(2); // New fd pointing to stderr, saving for later in case we override
// Open all the requester in/out/err files.
// Do this before hand so that:
// - all fork()'s have access for error file
// - first fork() has access to in file
// - any invalid files will be caught before fork() calls.
// Open specified err file
int fd_err = setup_output_file(2, cmd->append_err, cmd->err_file);
if (fd_err < 0) {
close(tmp_err);
close(tmp_in);
close(tmp_out);
return false;
}
dup2(fd_err, 2); // stderr now is fd_err, either the pipe, out_file, or stdout
close(fd_err); // don't need 2 FileTable entries for this
// Open specified infile
int fd_in;
if (cmd->in_file) { // If user specified a file to read from
fd_in = open(cmd->in_file, O_RDONLY); // Open a new file, fd_in
if (fd_in < 0) {
restore_file_desc(tmp_err, 2); // restore stderr
close(tmp_in);
close(tmp_out);
printf("%s: No such file or directory: %s\n", shell_name, cmd->in_file);
return false;
}
} else {
fd_in = dup(0); // fd_in points to stdin
}
// Open specified outfile (won't be used until last fork()
int svd_fd_out = setup_output_file(1, cmd->append_out, cmd->out_file);
if (svd_fd_out < 0) {
restore_file_desc(tmp_err, 2); // restore stderr
close(tmp_in); // Don't restore in since haven't modified stdin yet
close(fd_in);
close(tmp_out);
printf("%s: No such file or directory: %s\n", shell_name, cmd->out_file);
return false;
}
// Loop through all subcommands and fork() a process off for each.
// Create a pipe to go between each process.
int ret;
int fd_out;
int status = 0;
bool last;
for (int i=0; i<cmd->sub_cmd_count; ++i) { // Iterate through all sub-commands
// At beginning of each iteration, fd_in will always point to fd to read from
// Will have to create the fd to print to each iteration
dup2(fd_in, 0); // stdin now is fd_in, which was already set properly
close(fd_in); // stdin now points to this file, don't need 2 entries in File Table pointing to this file object
last = i == (cmd->sub_cmd_count - 1);
if (last) { // Last sub command, will not be pipeing
fd_out = svd_fd_out;
} else { // Sub command after this, will send data to pipe
int fd_pipe[2];
pipe(fd_pipe); // Pipe from fd_pipe[0] to fd_pipe[1]
fd_out = fd_pipe[1]; // fd_out is inflow of pipe
fd_in = fd_pipe[0]; // fd_in is outflow of pipe
}
dup2(fd_out, 1); // stdout now is fd_out, either the pipe, out_file, or stdout
close(fd_out); // don't need 2 FileTable entries for this
if (!spawn_subproc(cmd->sub_cmds[i], cmd, &status, last)) {
break;
}
}
// Restore std in/out/err to point to their origional files.
restore_file_desc(tmp_in, 0); // restore stdin
restore_file_desc(tmp_out, 1); // restore stdout
restore_file_desc(tmp_err, 2); // restore stderr
return (bool) (status == 0);
}