1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-11-25 18:44:19 +00:00
fish/fish_shell/fish_core.c

379 lines
8.6 KiB
C
Raw Normal View History

2017-05-11 15:16:06 +00:00
//
// Created by Antoine Bartuccio on 11/05/2017.
//
2017-05-11 15:16:06 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <pcre.h>
#include "fish_core.h"
2017-05-15 12:32:41 +00:00
#include "fish_globbing.h"
#include "fish_types.h"
2017-05-11 15:16:06 +00:00
#define REDIRECT_EXIT_FREE() signal = EXIT_FAILURE; \
freeWordList(list);\
freeWordList(splited);
#define REDIRECT_STD(std, open_mode) if (splited->size == 0) {REDIRECT_EXIT_FREE()\
} else {\
redirect_file = fopen(splited->first->word, open_mode);\
if (redirect_file == NULL) { REDIRECT_EXIT_FREE() } else {\
saved_std = dup(std);\
dup2(fileno(redirect_file), std);\
signal = fishExecute(list);\
dup2(saved_std, std);\
fclose(redirect_file);\
freeWordList(splited);\
}\
}
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 15:52:05 +00:00
redirection->files_name[0] = strdup((char*) "/tmp/fishXXXXXX");
redirection->files_name[1] = strdup((char*) "/tmp/fishXXXXXX");
redirection->tmp_files[0] = mkstemp(redirection->files_name[0]);
redirection->tmp_files[1] = mkstemp(redirection->files_name[1]);
redirection->to_use = 0;
2017-06-11 15:52:05 +00:00
redirection->read = WRITE;
redirection->nb = 0;
redirection->nb_max = 0;
redirection->file_use = 0;
}
return redirection;
}
void freeRedirection(){
pipe_redirection * redirection = getRedirection();
2017-06-11 15:52:05 +00:00
close(redirection->tmp_files[0]);
close(redirection->tmp_files[1]);
unlink(redirection->files_name[0]);
unlink(redirection->files_name[1]);
remove(redirection->files_name[0]);
remove(redirection->files_name[1]);
free(redirection->files_name[0]);
free(redirection->files_name[1]);
free(redirection);
}
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;
pipe_redirection *r = getRedirection();
2017-05-15 12:32:41 +00:00
do {
2017-06-12 11:29:19 +00:00
printPS(settings->PS1, settings);
2017-05-15 12:32:41 +00:00
line = fishReadLine();
r->to_use = 0;
r->nb = countSeparators(line, (char*) "\\|[^\\|]");
2017-06-11 15:52:05 +00:00
r->nb_max = r->nb;
splited = split(line, (char*) FISH_TOKENS);
2017-05-15 12:32:41 +00:00
splited = fishExpand(splited);
status = fishExecute(splited);
2017-05-15 09:22:53 +00:00
free(line);
} while(status != EXIT_SIGNAL);
2017-06-12 11:38:44 +00:00
freeRedirection();
2017-05-11 15:16:06 +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;
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
}
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){
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;
if (to_delete == NULL){
2017-05-15 12:08:10 +00:00
crash();
2017-05-15 09:22:53 +00:00
}
while((token = strsep(&to_delete, separator)) != NULL)
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() {
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;
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){
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){
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
int fishLoad(WordArray *array) {
2017-05-15 09:22:53 +00:00
pid_t pid;
int status = 1;
pipe_redirection *redirection = getRedirection();
2017-05-15 09:22:53 +00:00
pid = fork();
if (pid == 0){
if (redirection->to_use){
2017-06-11 15:52:05 +00:00
if (redirection->read == READ){
dup2(redirection->tmp_files[redirection->file_use], STDIN_FILENO);
} else if (redirection->read == WRITE) {
dup2(redirection->tmp_files[redirection->file_use], STDOUT_FILENO);
} else {
2017-06-11 15:52:05 +00:00
dup2(redirection->tmp_files[!redirection->file_use], STDIN_FILENO);
dup2(redirection->tmp_files[redirection->file_use], 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 */
exit(EXIT_FAILURE);
2017-05-15 09:22:53 +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));
if (status) fprintf(stderr, "%s\n", getInsult());
}
2017-05-17 23:30:46 +00:00
freeWordArray(array);
return status;
}
int fishExecute(WordList *list) {
WordList *splited = NULL;
shell_operator op = NONE;
WordArray *array = NULL;
pipe_redirection *redirection = NULL;
FILE *redirect_file = NULL;
int saved_std;
int pid;
int signal = 1;
splited = parseWordList(list, &op);
switch (op) {
case AND:
2017-05-26 16:34:30 +00:00
signal = fishExecute(list);
if (!signal) signal = fishExecute(splited);
else {
if (splited != NULL) freeWordList(splited);
2017-05-26 16:34:30 +00:00
splited = NULL;
}
break;
case REVERSE_AND:
2017-05-26 16:34:30 +00:00
signal = fishExecute(list);
if (signal && signal != EXIT_SIGNAL) signal = fishExecute(splited);
else {
if (splited != NULL) freeWordList(splited);
2017-05-26 16:34:30 +00:00
splited = NULL;
}
break;
case OR:
2017-05-26 16:34:30 +00:00
signal = fishExecute(list);
if (signal != EXIT_SIGNAL) signal = fishExecute(splited);
break;
case PIPE:
redirection = getRedirection();
2017-06-11 15:52:05 +00:00
if (redirection->nb == redirection->nb_max){
redirection->read = WRITE;
redirection->file_use = 0;
} else if (redirection->nb > 0){
redirection->read = READ_AND_WRITE;
redirection->file_use = !redirection->file_use;
} else {
redirection->read = READ;
redirection->file_use = !redirection->file_use;
}
redirection->to_use = 1;
2017-06-11 15:52:05 +00:00
--redirection->nb;
if (redirection->read == WRITE) ftruncate(redirection->tmp_files[redirection->file_use], 0);
lseek(redirection->tmp_files[0], 0L, 0);
lseek(redirection->tmp_files[1], 0L, 0);
fishExecute(list);
2017-06-11 15:52:05 +00:00
redirection->read = WRITE;
if (redirection->nb > 0){
redirection->read = READ_AND_WRITE;
} else {
redirection->read = READ;
}
if (redirection->read == WRITE) ftruncate(redirection->tmp_files[redirection->file_use], 0);
lseek(redirection->tmp_files[0], 0L, 0);
lseek(redirection->tmp_files[1], 0L, 0);
signal = fishExecute(splited);
2017-06-11 15:52:05 +00:00
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;
case REDIRECT_STDOUT_ERASE:
REDIRECT_STD(STDOUT_FILENO, "w");
break;
case REDIRECT_STDOUT_APPEND:
REDIRECT_STD(STDOUT_FILENO, "a");
break;
case REDIRECT_STDERR_ERASE:
REDIRECT_STD(STDERR_FILENO, "w");
break;
case REDIRECT_STDERR_APPEND:
REDIRECT_STD(STDERR_FILENO, "a");
break;
case REDIRECT_STDIN:
REDIRECT_STD(STDIN_FILENO, "r");
break;
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);
}
return signal;
2017-05-15 09:22:53 +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);
}
WordList * parseWordList(WordList *list, shell_operator *an_operator) {
char *op_str[] = {
(char*) ";",
(char*) "\\|\\|",
(char*) "&&",
(char*) "\\|",
(char*) "&",
(char*) "2>>",
(char*) ">>",
(char*) "2>",
(char*) ">",
(char*) "<[^<]"
};
shell_operator op[] = {
OR,
REVERSE_AND,
AND,
PIPE,
BACKGROUND_PROCESS,
REDIRECT_STDERR_APPEND,
REDIRECT_STDOUT_APPEND,
REDIRECT_STDERR_ERASE,
REDIRECT_STDOUT_ERASE,
REDIRECT_STDIN
};
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;
}