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
|
#ifndef shell_main_func
#define shell_main_func
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdbool.h>
#include <fcntl.h>
#include "main.h"
#include "helper_functions.c"
#include "lsh_split_line.c"
#define LSH_RL_BUFSIZE 1024
char *lsh_read_line(void)
{
int bufsize = LSH_RL_BUFSIZE;
int position = 0;
char *buffer = malloce(sizeof(char) * bufsize);
int c;
while (1) {
// Read a character
c = getchar();
// If we hit EOF, replace it with a null character and return.
if (c == EOF || c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
// If we have exceeded the buffer, reallocate.
if (position >= bufsize) {
bufsize += LSH_RL_BUFSIZE;
buffer = realloce(buffer, bufsize);
}
}
}
int cleanup_token(char **parse_args_last_p, int ch_i){
(*parse_args_last_p)[ch_i]='\0';
if((*parse_args_last_p)[ch_i+1]=='\0')
return -1;
//remove spaces and tabs around sep char
int i=ch_i-1;
while ((*parse_args_last_p)[i]==' '|| (*parse_args_last_p)[0]=='\t')
(*parse_args_last_p)[i--]='\0';
i=ch_i+1;
while((*parse_args_last_p)[i]==' ' || (*parse_args_last_p)[i]=='\t')
(*parse_args_last_p)[i++]='\0';
*parse_args_last_p=&(*parse_args_last_p)[i];
return 0;
}
char *strtok_cmd(char *token, cmd_t *type){
*type=Stdout;
static char* parse_args_last_p;
if(token != NULL){
parse_args_last_p=token;
}
if(parse_args_last_p==NULL){
return NULL;
}
char *return_char=parse_args_last_p;
char in_quote=' ';
if(parse_args_last_p != NULL){
int ch_i=0;
while(1){
if (parse_args_last_p[ch_i] == '\0')
break;
if (in_quote==' ' && (parse_args_last_p[ch_i]=='"' || parse_args_last_p[ch_i]=='\'')){
bool pass_check=true;
if(ch_i>0 && parse_args_last_p[ch_i-1]=='\\')
pass_check=false;
if(pass_check==true){
in_quote=parse_args_last_p[ch_i];
if (parse_args_last_p[ch_i+1] == '\0')
break;
ch_i++;
}
}
if (in_quote!=' '){
if (parse_args_last_p[ch_i]==in_quote){
bool pass_check=true;
if(ch_i>0 && parse_args_last_p[ch_i-1]=='\\')
pass_check=false;
if (pass_check)
in_quote=' ';
}
} else{
switch (parse_args_last_p[ch_i]){
case ';':
if(cleanup_token(&parse_args_last_p, ch_i)==-1) break;
return return_char;
break;
case '|':
if(cleanup_token(&parse_args_last_p, ch_i)==-1) break;
*type=Pipe;
return return_char;
break;
case '>':
if(cleanup_token(&parse_args_last_p, ch_i)==-1) break;
*type=Redirect;
// detect and cleanup append
if(parse_args_last_p[0]=='>'){
parse_args_last_p=&parse_args_last_p[1];
int i=0;
while(parse_args_last_p[i]==' ' || parse_args_last_p[i]=='\t')
parse_args_last_p[i++]='\0';
parse_args_last_p=&parse_args_last_p[i];
*type=Append;
}
return return_char;
break;
}
}
ch_i++;
}
parse_args_last_p=NULL;
}
return return_char;
}
CMD **lsh_split_command(char *line)
{
int bufsize = sizeof(CMD*), position = 0;
CMD **tokens = malloce(bufsize);
char *token;
cmd_t cmd_type;
token = strtok_cmd(line,&cmd_type);
while (token != NULL) {
tokens[position] = malloce(sizeof(CMD));
tokens[position]->cmd = token;
tokens[position]->type = cmd_type;
tokens[position]->stdInFd = 0;
tokens[position]->next = NULL;
tokens[position]->out_dir= NULL;
if(position>0) tokens[position-1]->next = tokens[position];
position++;
if (position * bufsize >= bufsize) {
bufsize += sizeof(CMD*);
tokens = realloce(tokens, bufsize);
}
// do not make Redirect or Append a actual command to run
// sets the current commands out_dir (redirection) to the next tokens value
if(cmd_type==Redirect || cmd_type==Append){
token = strtok_cmd(NULL,&cmd_type);
tokens[position-1]->out_dir=handle_vars(token);
}
token = strtok_cmd(NULL,&cmd_type);
}
tokens[position] = NULL;
return tokens;
}
int lsh_launch(char **args, CMD *cmd)
{
pid_t pid, wpid;
int status;
int stdOutPipe[2];
int redir_out;
pipe(stdOutPipe);
if(cmd->type==Pipe){
cmd->next->stdInFd=stdOutPipe[0];
}
if (cmd->type==Redirect){
redir_out = open(cmd->out_dir,O_RDWR|O_CREAT|O_TRUNC, 0600);
if (-1 == redir_out) {
fprintf(stderr,"opening %s",cmd->out_dir);
return -1;
}
} else if(cmd->type==Append){
redir_out = open(cmd->out_dir,O_RDWR|O_CREAT|O_APPEND, 0600);
if (-1 == redir_out) {
fprintf(stderr,"opening %s",cmd->out_dir);
return -1;
}
}
pid = fork();
if (pid == 0) {
// Child process
if (cmd->type==Redirect || cmd->type==Append){
dup2(redir_out, 1); // send stdout to the pipe
} else if(cmd->type!=Stdout){
close(stdOutPipe[0]); // close reading end in the child
dup2(stdOutPipe[1], 1); // send stdout to the pipe
dup2(stdOutPipe[1], 2); // send stderr to the pipe
close(stdOutPipe[1]); // this descriptor is no longer needed
}
if(cmd->stdInFd!=0)
dup2(cmd->stdInFd,STDIN_FILENO);
if (execvp(args[0], args) == -1) perror("lsh");
exit(EXIT_FAILURE);
} else if (pid < 0) {
// Error forking
perror("lsh");
} else {
// Parent process
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
close(stdOutPipe[1]); // close the write end of the pipe in the parent
if(cmd->stdInFd!=0) close(cmd->stdInFd);
}
return 1;
}
#endif
|