diff --git a/Makefile b/Makefile index 29425b2..194227d 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,9 @@ SRCS = $(PATH_SRC)/check_args.c \ $(PATH_SRC)/shell_loop/prompt/display_bonus_prompt.c \ $(PATH_SRC)/shell_loop/prompt/display_prompt.c \ $(PATH_SRC)/shell_loop/parsing/get_command_line.c \ + $(PATH_SRC)/shell_loop/parsing/parsing/get_word.c \ + $(PATH_SRC)/shell_loop/parsing/parsing/my_str_to_words.c \ + $(PATH_SRC)/shell_loop/parsing/parsing/my_strcpy_words.c \ $(PATH_SRC)/shell_loop/parsing/get_number_instruction.c \ $(PATH_SRC)/shell_loop/parsing/get_pipe_number.c \ $(PATH_SRC)/shell_loop/parsing/fill_up_instruction.c \ diff --git a/lib/my/my_str_to_words_array.c b/lib/my/my_str_to_words_array.c index 4252e93..1b876f7 100644 --- a/lib/my/my_str_to_words_array.c +++ b/lib/my/my_str_to_words_array.c @@ -49,10 +49,8 @@ char **cut_line(char *line) tab_arg = malloc(sizeof(*tab_arg) * (nb_arg + 1)); if (tab_arg == NULL) return (NULL); - for (int i = 0; i < nb_arg; i += 1) { + for (int i = 0; i < nb_arg; i += 1) tab_arg[i] = copy_args(line, &j); - printf("%s\n", tab_arg[i]); - } tab_arg[nb_arg] = NULL; return (tab_arg); -} \ No newline at end of file +} diff --git a/src/shell_loop/parsing/get_pipe.c b/src/shell_loop/parsing/get_pipe.c index 5cbe9af..dcb2932 100644 --- a/src/shell_loop/parsing/get_pipe.c +++ b/src/shell_loop/parsing/get_pipe.c @@ -17,7 +17,7 @@ static unsigned int get_args_pipe(pipe_t **pipe, char **env) int a = 0; for (unsigned int i = 0; pipe[i]; i++) { - pipe[i]->args = cut_line(pipe[i]->full_instruction); + pipe[i]->args = my_str_to_words(pipe[i]->full_instruction); if (pipe[i]->args == NULL) return (FAILURE); check_env_variable(pipe[i]->args, env); diff --git a/src/shell_loop/parsing/parsing/count_words.c b/src/shell_loop/parsing/parsing/count_words.c deleted file mode 100644 index 377c48c..0000000 --- a/src/shell_loop/parsing/parsing/count_words.c +++ /dev/null @@ -1,26 +0,0 @@ -/* -** EPITECH PROJECT, 2017 -** count_words.c -** File description: -** florian.davasse@epitech.eu -*/ - -#include "shell.h" - -int count_words(char *str) -{ - int i; - int words = 0; - - if (str == NULL) - return (0); - if (strlen(str) == 0) - return (0); - for (i = 0; str[i] != '\0'; i++) { - if ((str[i] == ' ' || str[i] == '\t') && - is_printable(str[i+1])) { - words += 1; - } - } - return (++words); -} diff --git a/src/shell_loop/parsing/parsing/get_word.c b/src/shell_loop/parsing/parsing/get_word.c index 0e14f2a..1eee70a 100644 --- a/src/shell_loop/parsing/parsing/get_word.c +++ b/src/shell_loop/parsing/parsing/get_word.c @@ -6,9 +6,10 @@ */ #include "shell.h" + static void check_reset(int *i, int *j, int reset) { - if (reset == 1) { + if (reset == 0) { *i = 0; *j = 0; } @@ -41,14 +42,3 @@ char *get_word(char *str, int reset) } return (my_strcpy_words_parse(str, j, i++)); } - -int main(void) -{ - //gcc get_word.c my_strcpy_words.c -I../../../../include -g3 && ./a.out - char *str = "\"lol c un test \"mdr 'xd sa march lol' ptdr"; - char **st = malloc(sizeof(char*) * 4); - for (int i = 0; i < 4; i++) { - printf("LE MOT : '%s'\n", get_word(str, 0)); - } - return (0); -} \ No newline at end of file diff --git a/src/shell_loop/parsing/parsing/my_str_to_words.c b/src/shell_loop/parsing/parsing/my_str_to_words.c index 5ef90d3..c2a7e7b 100644 --- a/src/shell_loop/parsing/parsing/my_str_to_words.c +++ b/src/shell_loop/parsing/parsing/my_str_to_words.c @@ -9,17 +9,19 @@ char **my_str_to_words(char *str) { - int words = count_words(str); - char **word = malloc(sizeof(char *) * (words + 1)); + //gcc get_word.c my_strcpy_words.c -I../../../../include -g3 && ./a.out + //char *str = "\"lol c un test \"mdr 'xd sa march lol' ptdr"; + char **words = malloc(sizeof(char*) * 1); + char *temp = NULL; - if (words == 0) - return (NULL); - for (int i = 0; i < words; i++) { - if (i == 0) - word[i] = get_word(str, 1); - else - word[i] = get_word(str, 0); + words[0] = NULL; + for (int i = 0; (temp = get_word(str, i)) != NULL; i++) { + words = realloc(words, sizeof(char*) * (i+2)); + words[i] = temp; + //printf("LE MOT : '%s'\n", words[i]); + words[i+1] = NULL; } - word[words] = NULL; - return (word); + if (words[0] == NULL) + words = NULL; + return (words); } diff --git a/src/shell_loop/parsing/parsing/my_strcpy_words.c b/src/shell_loop/parsing/parsing/my_strcpy_words.c index 47e0080..d182119 100644 --- a/src/shell_loop/parsing/parsing/my_strcpy_words.c +++ b/src/shell_loop/parsing/parsing/my_strcpy_words.c @@ -13,6 +13,8 @@ char *my_strcpy_words_parse(char *str, int start, int end) int j = 0; char *new_str = malloc(sizeof(char) * ((end - start) + 2)); + if (start == end) + return (NULL); while (i < end && str[i] != '\0') { new_str[j] = str[i]; i++;