2017-05-11 15:16:06 +00:00
|
|
|
//
|
|
|
|
// Created by Antoine Bartuccio on 11/05/2017.
|
|
|
|
//
|
2017-06-11 00:10:11 +00:00
|
|
|
|
2017-05-11 15:16:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-05-14 18:54:39 +00:00
|
|
|
#include <unistd.h>
|
2017-05-14 21:15:12 +00:00
|
|
|
#include <sys/wait.h>
|
2017-06-11 00:10:11 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <pcre.h>
|
2017-05-14 18:54:39 +00:00
|
|
|
#include "fish_core.h"
|
2017-05-15 12:32:41 +00:00
|
|
|
#include "fish_globbing.h"
|
2017-05-16 17:15:51 +00:00
|
|
|
#include "fish_types.h"
|
2017-05-11 15:16:06 +00:00
|
|
|
|
2017-06-11 00:10:11 +00:00
|
|
|
pipe_redirection * getRedirection(){
|
|
|
|
static pipe_redirection * redirection = NULL;
|
|
|
|
|
|
|
|
if (redirection == NULL){
|
|
|
|
redirection = (pipe_redirection*) malloc(sizeof(pipe_redirection));
|
|
|
|
if (redirection == NULL) crash();
|
2017-06-11 00:42:33 +00:00
|
|
|
redirection->file_name = strdup((char*) "/tmp/fishXXXXXX");
|
2017-06-11 00:10:11 +00:00
|
|
|
redirection->tmp_file = mkstemp(redirection->file_name);
|
|
|
|
redirection->to_use = 0;
|
|
|
|
redirection->read = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirection;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeRedirection(){
|
|
|
|
pipe_redirection * redirection = getRedirection();
|
|
|
|
close(redirection->tmp_file);
|
|
|
|
unlink(redirection->file_name);
|
|
|
|
remove(redirection->file_name);
|
|
|
|
free(redirection->file_name);
|
|
|
|
free(redirection);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-13 20:40:41 +00:00
|
|
|
void fishLoop(Settings * settings){
|
2017-05-15 09:22:53 +00:00
|
|
|
char * line = NULL;
|
2017-05-15 14:53:56 +00:00
|
|
|
WordList* splited = NULL;
|
2017-05-15 09:22:53 +00:00
|
|
|
int status = 1;
|
2017-06-11 00:10:11 +00:00
|
|
|
pipe_redirection *r = getRedirection();
|
2017-05-13 20:40:41 +00:00
|
|
|
|
2017-05-15 12:32:41 +00:00
|
|
|
do {
|
|
|
|
printf("%s", settings->PS1);
|
|
|
|
line = fishReadLine();
|
2017-05-13 20:40:41 +00:00
|
|
|
|
2017-06-11 00:10:11 +00:00
|
|
|
r->to_use = 0;
|
|
|
|
r->read = 0;
|
2017-05-16 20:14:06 +00:00
|
|
|
splited = split(line, (char*) FISH_TOKENS);
|
2017-05-15 12:32:41 +00:00
|
|
|
splited = fishExpand(splited);
|
2017-05-14 18:54:39 +00:00
|
|
|
|
2017-05-16 17:15:51 +00:00
|
|
|
status = fishExecute(splited);
|
2017-05-13 20:40:41 +00:00
|
|
|
|
2017-05-15 09:22:53 +00:00
|
|
|
free(line);
|
2017-05-16 17:15:51 +00:00
|
|
|
|
2017-05-16 20:14:06 +00:00
|
|
|
} while(status != EXIT_SIGNAL);
|
2017-05-11 15:16:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 00:10:11 +00:00
|
|
|
int countSeparators(char *string, char *regex) {
|
|
|
|
const char* error;
|
|
|
|
int error_offset;
|
|
|
|
int ovector[100];
|
2017-05-15 09:22:53 +00:00
|
|
|
int nb = 0;
|
2017-06-11 00:10:11 +00:00
|
|
|
int string_size = (int) strlen(string);
|
|
|
|
int ovector_size = 100;
|
|
|
|
int offset = 0;
|
|
|
|
pcre *re = pcre_compile(regex, 0, &error, &error_offset, 0);
|
|
|
|
|
|
|
|
if (!re) crash();
|
|
|
|
|
|
|
|
while (pcre_exec(re, 0, string, string_size, offset, 0, ovector, ovector_size) >= 0){
|
|
|
|
offset = ovector[1];
|
|
|
|
nb++;
|
2017-05-15 09:22:53 +00:00
|
|
|
}
|
2017-06-11 00:10:11 +00:00
|
|
|
pcre_free(re);
|
|
|
|
|
2017-05-15 09:22:53 +00:00
|
|
|
return nb;
|
2017-05-11 15:16:06 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 14:53:56 +00:00
|
|
|
WordList * split(char *string, char *separator){
|
2017-05-15 14:27:44 +00:00
|
|
|
WordList *list = createWordList();
|
2017-05-15 09:22:53 +00:00
|
|
|
char *to_delete = strdup(string);
|
2017-05-15 10:32:00 +00:00
|
|
|
char *to_delete_bak = to_delete;
|
2017-05-15 09:22:53 +00:00
|
|
|
char *token = NULL;
|
|
|
|
|
2017-05-15 14:27:44 +00:00
|
|
|
if (to_delete == NULL){
|
2017-05-15 12:08:10 +00:00
|
|
|
crash();
|
2017-05-15 09:22:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 14:27:44 +00:00
|
|
|
while((token = strsep(&to_delete, separator)) != NULL)
|
2017-05-20 14:25:00 +00:00
|
|
|
addEndWordList(list, token);
|
2017-05-15 09:22:53 +00:00
|
|
|
|
2017-05-15 10:32:00 +00:00
|
|
|
free(to_delete_bak);
|
2017-05-15 09:22:53 +00:00
|
|
|
|
2017-05-15 14:53:56 +00:00
|
|
|
return list;
|
2017-05-11 15:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *fishReadLine() {
|
2017-06-11 00:10:11 +00:00
|
|
|
struct sigaction signal_handler;
|
2017-05-15 09:22:53 +00:00
|
|
|
size_t bufferSize = FISH_BUFFER_SIZE;
|
|
|
|
int position = 0;
|
2017-05-15 22:08:07 +00:00
|
|
|
char *line = (char*) malloc(sizeof(char*) * bufferSize);
|
2017-05-15 09:22:53 +00:00
|
|
|
int c;
|
|
|
|
|
2017-06-11 00:10:11 +00:00
|
|
|
if (line == NULL) crash();
|
|
|
|
|
|
|
|
signal_handler.sa_handler = fishSignalHandler;
|
|
|
|
sigemptyset(&signal_handler.sa_mask);
|
|
|
|
signal_handler.sa_flags = 0;
|
2017-05-15 09:22:53 +00:00
|
|
|
|
|
|
|
while (1){
|
2017-06-11 00:10:11 +00:00
|
|
|
sigaction(SIGINT, &signal_handler, NULL);
|
2017-05-15 09:22:53 +00:00
|
|
|
c = getchar();
|
|
|
|
|
|
|
|
switch (c){
|
|
|
|
case '\n':
|
|
|
|
line[position] = '\0';
|
|
|
|
return line;
|
|
|
|
default:
|
|
|
|
line[position] = (char) c;
|
|
|
|
}
|
|
|
|
|
|
|
|
position++;
|
|
|
|
|
|
|
|
if ((size_t) position > bufferSize){
|
2017-06-11 00:10:11 +00:00
|
|
|
bufferSize += bufferSize;
|
2017-05-15 22:08:07 +00:00
|
|
|
line = (char*) realloc(line, bufferSize);
|
2017-05-15 09:22:53 +00:00
|
|
|
if (line == NULL){
|
2017-05-15 12:08:10 +00:00
|
|
|
crash();
|
2017-05-15 09:22:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2017-05-11 15:16:06 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 10:32:00 +00:00
|
|
|
|
2017-05-14 18:54:39 +00:00
|
|
|
int fishLoad(WordArray *array) {
|
2017-05-15 09:22:53 +00:00
|
|
|
pid_t pid;
|
2017-05-16 17:15:51 +00:00
|
|
|
int status = 1;
|
2017-06-11 00:10:11 +00:00
|
|
|
pipe_redirection *redirection = getRedirection();
|
2017-05-15 09:22:53 +00:00
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid == 0){
|
2017-06-11 00:10:11 +00:00
|
|
|
if (redirection->to_use){
|
|
|
|
if (redirection->read){
|
|
|
|
dup2(redirection->tmp_file, STDIN_FILENO);
|
|
|
|
} else {
|
|
|
|
dup2(redirection->tmp_file, STDOUT_FILENO);
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 09:22:53 +00:00
|
|
|
/* Executes only in the child process */
|
|
|
|
if (execvp(array->words[0], array->words) == -1){
|
|
|
|
/* Error during system call */
|
2017-05-16 17:15:51 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2017-05-15 09:22:53 +00:00
|
|
|
}
|
2017-05-16 17:15:51 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2017-05-15 09:22:53 +00:00
|
|
|
} else if (pid < 0){
|
|
|
|
/* Fork failed */
|
|
|
|
perror("fish");
|
|
|
|
} else {
|
|
|
|
/* Handle parent process */
|
|
|
|
/* Wait for the child process to finish */
|
|
|
|
do {
|
|
|
|
waitpid(pid, &status, WUNTRACED);
|
|
|
|
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
2017-05-16 17:15:51 +00:00
|
|
|
if (status) fprintf(stderr, "%s\n", getInsult());
|
|
|
|
}
|
2017-05-17 23:30:46 +00:00
|
|
|
freeWordArray(array);
|
2017-06-11 00:10:11 +00:00
|
|
|
|
2017-05-16 17:15:51 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fishExecute(WordList *list) {
|
|
|
|
WordList *splited = NULL;
|
|
|
|
shell_operator op = NONE;
|
|
|
|
WordArray *array = NULL;
|
2017-06-11 00:10:11 +00:00
|
|
|
pipe_redirection *redirection = NULL;
|
|
|
|
int pid;
|
2017-05-16 20:14:06 +00:00
|
|
|
int signal = 1;
|
2017-05-16 17:15:51 +00:00
|
|
|
|
|
|
|
splited = parseWordList(list, &op);
|
|
|
|
switch (op) {
|
|
|
|
case AND:
|
2017-05-26 16:34:30 +00:00
|
|
|
signal = fishExecute(list);
|
2017-05-16 20:14:06 +00:00
|
|
|
if (!signal) signal = fishExecute(splited);
|
|
|
|
else {
|
|
|
|
if (splited != NULL) freeWordList(splited);
|
2017-05-26 16:34:30 +00:00
|
|
|
splited = NULL;
|
2017-05-16 20:14:06 +00:00
|
|
|
}
|
2017-05-16 17:15:51 +00:00
|
|
|
break;
|
2017-05-18 00:09:44 +00:00
|
|
|
case REVERSE_AND:
|
2017-05-26 16:34:30 +00:00
|
|
|
signal = fishExecute(list);
|
|
|
|
if (signal && signal != EXIT_SIGNAL) signal = fishExecute(splited);
|
2017-05-18 00:09:44 +00:00
|
|
|
else {
|
|
|
|
if (splited != NULL) freeWordList(splited);
|
2017-05-26 16:34:30 +00:00
|
|
|
splited = NULL;
|
2017-05-18 00:09:44 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-05-16 17:15:51 +00:00
|
|
|
case OR:
|
2017-05-26 16:34:30 +00:00
|
|
|
signal = fishExecute(list);
|
|
|
|
if (signal != EXIT_SIGNAL) signal = fishExecute(splited);
|
2017-05-16 17:15:51 +00:00
|
|
|
break;
|
2017-06-11 00:10:11 +00:00
|
|
|
case PIPE:
|
|
|
|
redirection = getRedirection();
|
|
|
|
redirection->to_use = 1;
|
|
|
|
redirection->read = 0;
|
|
|
|
fishExecute(list);
|
|
|
|
lseek(redirection->tmp_file, 0L, 0);
|
|
|
|
redirection->read = 1;
|
|
|
|
signal = fishExecute(splited);
|
|
|
|
lseek(redirection->tmp_file, 0L, 0);
|
|
|
|
ftruncate(redirection->tmp_file, 0);
|
|
|
|
redirection->to_use = 0;
|
|
|
|
break;
|
|
|
|
case BACKGROUND_PROCESS:
|
|
|
|
printf("Exécution en tache de fond\n");
|
|
|
|
pid = fork();
|
|
|
|
if(pid == 0){
|
|
|
|
exit(fishExecute(list));
|
|
|
|
} else if (pid < 0){
|
|
|
|
perror("fish");
|
|
|
|
} else if (splited->size > 0) signal = fishExecute(splited);
|
|
|
|
else {
|
|
|
|
freeWordList(splited);
|
|
|
|
splited = NULL;
|
|
|
|
signal = EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
break;
|
2017-05-16 17:15:51 +00:00
|
|
|
default:
|
2017-05-26 16:34:30 +00:00
|
|
|
array = wordListToWordArray(list);
|
|
|
|
signal = loadRightCommand(array);
|
|
|
|
splited = NULL;
|
2017-05-15 09:22:53 +00:00
|
|
|
}
|
2017-05-26 16:34:30 +00:00
|
|
|
|
|
|
|
if (signal == EXIT_SIGNAL){
|
|
|
|
if (splited != NULL) freeWordList(splited);
|
|
|
|
if (array != NULL) freeWordArray(array);
|
|
|
|
}
|
|
|
|
|
2017-05-16 20:14:06 +00:00
|
|
|
return signal;
|
2017-05-15 09:22:53 +00:00
|
|
|
|
2017-05-14 18:54:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 17:15:51 +00:00
|
|
|
int loadRightCommand(WordArray *array){
|
2017-05-15 09:22:53 +00:00
|
|
|
int i;
|
2017-05-16 05:32:19 +00:00
|
|
|
if (array->size < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (i=0; i < getNbBuiltins(); i++){
|
|
|
|
if(array->words[0] != NULL){
|
|
|
|
if (!strcmp(array->words[0], getBuiltinCommandsStr()[i])){
|
|
|
|
return getBuiltinCommands()[i](array);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
crash();
|
2017-05-15 09:22:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fishLoad(array);
|
2017-05-14 18:54:39 +00:00
|
|
|
}
|
2017-05-16 17:15:51 +00:00
|
|
|
|
|
|
|
WordList * parseWordList(WordList *list, shell_operator *an_operator) {
|
|
|
|
char *op_str[] = {
|
2017-05-18 00:09:44 +00:00
|
|
|
(char*) ";",
|
2017-05-20 14:25:00 +00:00
|
|
|
(char*) "\\|\\|",
|
2017-06-11 00:10:11 +00:00
|
|
|
(char*) "&&",
|
|
|
|
(char*) "\\|",
|
|
|
|
(char*) "&"
|
2017-05-16 17:15:51 +00:00
|
|
|
};
|
|
|
|
shell_operator op[] = {
|
2017-05-16 20:14:06 +00:00
|
|
|
OR,
|
2017-05-18 00:09:44 +00:00
|
|
|
REVERSE_AND,
|
2017-06-11 00:10:11 +00:00
|
|
|
AND,
|
|
|
|
PIPE,
|
|
|
|
BACKGROUND_PROCESS
|
2017-05-16 17:15:51 +00:00
|
|
|
};
|
|
|
|
WordList *newList = NULL;
|
|
|
|
int max = sizeof(op_str) / sizeof(char*);
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (i < max && newList == NULL){
|
|
|
|
newList = splitWordList(list, op_str[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newList != NULL) *an_operator = op[i-1];
|
|
|
|
else *an_operator = NONE;
|
|
|
|
|
|
|
|
return newList;
|
|
|
|
}
|