-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathish.c
More file actions
295 lines (270 loc) · 8.19 KB
/
ish.c
File metadata and controls
295 lines (270 loc) · 8.19 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
/* ish.c : Ingesup Shell */
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "tools.h"
#include "commands.h"
char buf[LBUF];
static int redirectionOperators[3];
int externalCommand(char **cmd, int *n, int res, int rss, int isDaemon)
{
int pid;
int i;
pid = fork();
if (pid == -1) {
perror("fork");
return -1;
}
if (pid == 0) {
for (i = 0; i < 3; i++) {
if (redirectionOperators[i] != 0) {
close(i);
dup(redirectionOperators[i]);
}
}
if (res != 0) {
dup2(res, 0);
close(res);
}
if (rss != 0) {
dup2(rss, 1);
close(rss);
}
execvpe(cmd[0], cmd, environ);
perror(cmd[0]);
exit(3);
}
if (rss == 0 && !isDaemon) {
while (wait(&i) != pid);
}
if (rss) {
close(rss);
}
if (res) {
close(res);
}
return 1;
}
void clearRedirectionOperators() {
for (int i = 0; i < 3; i++) {
if (redirectionOperators[i] != 0) {
close(i);
dup(redirectionOperators[i]);
}
}
}
/*! \brief Check if the command is present internally in the program
* \param[in] cmd, line typed by the user. contains cmd, and args splitted by '\0'
* \param[in] n, current pointer on the cmd.
* \return BOOL, False if the command is not found or an error has been encountered, True otherwise
*/
BOOL internalCommand(char **cmd, int *n) {
clearRedirectionOperators();
return run_internal_command(cmd, n);
}
/* fonction qui determine si un caractere est un separateur */
BOOL is_sepa(char c) {
if (c == ' ')
return TRUE;
if (c == '\t')
return TRUE;
if (c == '\n')
return TRUE;
return FALSE;
}
void execute(int cmdBegin, int cmdEnd, char **words, int redirectStdin, int redirectStdout, BOOL isDaemon) {
int NWords = cmdEnd - cmdBegin + 1;
int it = 0, Red = 0, ired, flag, i;
char **cmd;
int n = 0;
clearRedirectionOperators();
// Preparsing the command line to catch redirection signs.
cmd = (char **) malloc(sizeof(char *) * NWords);
for (i = cmdBegin; i < cmdEnd; i++) {
if (Red) { /* traitement de la redirection */
switch (Red) {
case 1: /* cas du > */
ired = 1;
flag = O_WRONLY | O_CREAT;
break;
case 2: /* cas du >> */
ired = 1;
flag = O_WRONLY | O_CREAT | O_APPEND;
break;
case 3: /* cas du < */
ired = 0;
flag = O_RDONLY;
break;
case 4:
ired = 2;
flag = O_WRONLY | O_CREAT;
case 5:
ired = 2;
flag = O_WRONLY | O_CREAT | O_APPEND;
default:
fprintf(stderr, "Erreur code Red = %d !\n", Red);
}
if ((redirectionOperators[ired] = open(words[i], flag, 0644)) == -1) {
perror(words[i]);
}
Red = 0;
} else { /* remplissage de tab */
if (strcmp(words[i], ">") == 0) {
Red = 1;
continue;
}
if (strcmp(words[i], ">>") == 0) {
Red = 2;
continue;
}
if (strcmp(words[i], "<") == 0) {
Red = 3;
continue;
}
if (strcmp(words[i], "2>") == 0) {
Red = 4;
continue;
}
if (strcmp(words[i], "2>>") == 0) {
Red = 5;
continue;
}
cmd[it++] = words[i];
}
}
cmd[it] = NULL;
if (!internalCommand(cmd, &n)) {
externalCommand(cmd, &n, redirectStdin, redirectStdout, isDaemon);
}
free((void *) cmd);
}
/* Adds null byte as a replacement for a separator present in the separator character set.
* \param[in] Buffer to split
* \param[out] Words, computed from the buffer passed in
* return number of words.
*
*/
int bufferToWords(char *b, char ***words) {
int i = 0;
char *d = b;
char *tmpP[LBUF];
while (TRUE) {
/* on recherche le debut du premier mot */
while (*d != '\0') {
if (!is_sepa(*d)) {
break;
}
d++;
}
if (*d == '\0')
break; /* pas de premier mot */
tmpP[i++] = d;
/* on recherche la fin du mot */
while (*d != '\0') {
if (is_sepa(*d))
break;
d++;
}
/* si c'est la fin de la chaine on casse la boucle*/
if (*d == '\0')
break;
*d = '\0';
d++;
}
tmpP[i] = NULL;
*words = (char **) malloc(sizeof(char *) * (i + 1));
for (i = 0; tmpP[i] != NULL; i++) {
*words[i] = tmpP[i];
}
words[i] = NULL;
return i;
}
void handleInput(char *buf) {
char **words = NULL;
if (bufferToWords(buf, &words) == 0 || words == NULL) {
fprintf(stderr, "la commande est vide !\n");
}
else {
int cmdBegin = 0, cmdEnd = 0, i = 0, redirectStdin = 0, redirectStdout = 0, pip[2];
BOOL isPiped = FALSE;
BOOL isDaemon = FALSE;
for (i = 0; words[i] != NULL; i++) {
if (words[i + 1] == NULL ||
(strcmp(words[i], ";") == 0) ||
(strcmp(words[i], "|") == 0) ||
(strcmp(words[i], "&")) == 0) {
redirectStdout = 0;
if (!isPiped)
redirectStdin = 0;
if (strcmp(words[i], "|") == 0) { // Pipe Case
cmdEnd = i;
if (pipe(pip) != 0) {
perror("pipe");
exit(3);
}
redirectStdin = pip[0];
redirectStdout = pip[1];
isPiped = TRUE;
execute(cmdBegin, cmdEnd, words, 0, redirectStdout, isDaemon);
} else if (strcmp(words[i], "&") == 0) { // Daemon Case
cmdEnd = i;
isPiped = FALSE;
isDaemon = TRUE;
execute(cmdBegin, cmdEnd, words, redirectStdin, redirectStdout, isDaemon);
} else {
if (strcmp(words[i], ";") == 0) // separator Case
cmdEnd = i;
else
cmdEnd = i + 1;
isPiped = FALSE;
execute(cmdBegin, cmdEnd, words, redirectStdin, redirectStdout, isDaemon);
}
cmdBegin = i + 1;
}
}
for (i = 0; i < 3; i++) {
dup2(redirectionOperators[i], i);
}
}
free((void *) words);
}
void printLaunchMessage() {
fprintf(stdout, "===============================WELCOME============================");
fprintf(stdout, "\nTry to type a command\n");
}
/* Print the begining of a new line */
void printNewLinePrefix() {
char hostname[LBUF];
char *currentDirectory = getcwd(NULL, 0);
gethostname(hostname, LBUF);
if (strncmp(getenv("HOME"), currentDirectory, strlen(getenv("HOME"))) == 0) {
fprintf(stdout, "%s@%s:~%s> ", getenv("LOGNAME"), hostname, currentDirectory);
} else {
fprintf(stdout, "%s@%s:%s> ", getenv("LOGNAME"), hostname, currentDirectory);
}
free((void *) currentDirectory);
}
int main(int N, char *P[]) {
/* initialisations diverses */
signal(SIGINT, SIG_IGN); /* on ignore l'interruption du clavier (Ctrl + C)*/
signal(SIGTSTP, SIG_IGN);
system("clear");
printLaunchMessage();
while (RUN) {
env = environ;
printNewLinePrefix();
fflush(stdout);
if (read_line(0, buf, LBUF) < 0)
fprintf(stderr, "Command is too long, the line is limited to %d chars !\n", LBUF);
else
handleInput(buf);
}
fprintf(stdout, "-Exit-\n");
return 0;
}