diff options
| author | nass1pro <nass1pro@gmail.com> | 2020-06-10 18:02:58 +0200 |
|---|---|---|
| committer | nass1pro <nass1pro@gmail.com> | 2020-06-13 11:34:45 +0200 |
| commit | c4c60ea0f74fc593b0181e1fc8c71c27f0497180 (patch) | |
| tree | b07464986ba6a80542c2409a1271c4a9da87a0f4 | |
| parent | 579a26f5593039ffbbd1a81e45ecf0ef8797cb5d (diff) | |
| download | minishell-c4c60ea0f74fc593b0181e1fc8c71c27f0497180.tar.gz minishell-c4c60ea0f74fc593b0181e1fc8c71c27f0497180.tar.bz2 minishell-c4c60ea0f74fc593b0181e1fc8c71c27f0497180.zip | |
Fixing quote detection
Create token list
| -rwxr-xr-x | test_mini/a.out | bin | 17620 -> 17828 bytes | |||
| -rw-r--r-- | test_mini/cat | 36 | ||||
| -rw-r--r-- | test_mini/lexer.c | 70 | ||||
| -rw-r--r-- | test_mini/lexer.h | 1 | ||||
| -rw-r--r-- | test_mini/lexer_utils.c | 43 | ||||
| -rw-r--r-- | test_mini/main.c | 28 | ||||
| -rw-r--r-- | test_mini/speudo_code | 55 |
7 files changed, 144 insertions, 89 deletions
diff --git a/test_mini/a.out b/test_mini/a.out Binary files differindex 9622ab0..9b7deeb 100755 --- a/test_mini/a.out +++ b/test_mini/a.out diff --git a/test_mini/cat b/test_mini/cat new file mode 100644 index 0000000..45dcc70 --- /dev/null +++ b/test_mini/cat @@ -0,0 +1,36 @@ +ARGV: +[0] ./a.out +[1] echo +[2] salutsalut +[3] parfait + +ENV: +[0] USER=nassimehaddacha +[1] PATH=/Users/nassimehaddacha/miniconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/nassimehaddacha/miniconda/bin:/Users/nassimehaddacha/miniconda3/condabin +[2] LOGNAME=nassimehaddacha +[3] SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.VYiVzHotCo/Listeners +[4] HOME=/Users/nassimehaddacha +[5] SHELL=/bin/zsh +[6] __CF_USER_TEXT_ENCODING=0x1F5:0x0:0x62 +[7] TMPDIR=/var/folders/3s/wv71r7ws2hj4d1ss15_3vg4w0000gn/T/ +[8] XPC_SERVICE_NAME=0 +[9] XPC_FLAGS=0x0 +ARGV: +[0] ./a.out +[1] echo +[2] salutsalut +[3] parfait + +ENV: +[0] USER=nassimehaddacha +[1] PATH=/Users/nassimehaddacha/miniconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/nassimehaddacha/miniconda/bin:/Users/nassimehaddacha/miniconda3/condabin +[2] LOGNAME=nassimehaddacha +[3] SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.VYiVzHotCo/Listeners +[4] HOME=/Users/nassimehaddacha +[5] SHELL=/bin/zsh +[6] __CF_USER_TEXT_ENCODING=0x1F5:0x0:0x62 +[7] TMPDIR=/var/folders/3s/wv71r7ws2hj4d1ss15_3vg4w0000gn/T/ +[8] XPC_SERVICE_NAME=0 +[9] XPC_FLAGS=0x0 +echo4 +0 diff --git a/test_mini/lexer.c b/test_mini/lexer.c index c847c1d..7e4fad2 100644 --- a/test_mini/lexer.c +++ b/test_mini/lexer.c @@ -10,6 +10,8 @@ int len_is_not_sep(char *input) i = -1; while(input[++i]) { + if (input[i] == '\\' && input[i + 1] == ' ') + i += 2; if (lexer_sep(input[i])) { if (input[i + 1] == ' ') @@ -19,7 +21,7 @@ int len_is_not_sep(char *input) } if (input[i] == '\'' || input[i] == '"') { - i = lexer_verif_entre_cote(input, i); + i += lexer_verif_entre_cote(input, i); if (input[i] == ' ') return(i + 1); return(i); @@ -57,7 +59,9 @@ int check_input(char *input) return(len_is_not_sep(&input[i])); } -void check_input_out(char *input) + + +int check_input_out(char *input) { int i; int j; @@ -70,43 +74,67 @@ void check_input_out(char *input) j += len_is_not_sep(&input[i]); if (j != 0) { - str = malloc(sizeof(char) * j + 1); - ft_strlcpy(str, &input[i], j + 1); - printf("%s%d\n",str, j); - free(str); + return(j); } i += j; j = check_input(&input[i]); - str = malloc(sizeof(char) * j + 1); - ft_strlcpy(str, &input[i], j + 1); - printf("%s%d\n",str, j); - free(str); - i += j; + return(j); } - + return(0); } -t_token *create_token_list(void) +t_token *lexer_lst_token_str(char *input, int i, int j) { - t_token *lst_token; + t_token *lst_token; if (!(lst_token = malloc(sizeof(t_token) * 1))) return (NULL); - - free(lst_token); + lst_token->token = 0; + lst_token->value = NULL; + if (!(lst_token->value = malloc(sizeof(char) * j + 1))) + return(0); + if (!(ft_strlcpy(lst_token->value, &input[i], j + 1))) + { + free(lst_token); + return(0); + } + printf("%s\n", lst_token->value); return (lst_token); } +static t_ftlst *create_token_list(char *input, t_ftlst **lst) +{ + t_token *lst_token; + t_ftlst *new; + int i; + int j; + + i = 0; + while (i < ft_strlen(input)) + { + j = 0; + j += check_input(&input[i]); + lst_token = lexer_lst_token_str(input,i,j); + new = ft_lstnew((void *) lst_token); + ft_lstpush_back(lst, new); + i += j; + } + return (*lst); +} + t_ftlst *lexer(char *input) { - t_ftlst *lst; + t_ftlst **lst; + int i; if (!input) return (0); - if(!(lst = malloc(sizeof(t_ftlst) * 1))) - return(NULL); - - check_input_out(input); + lst = malloc(sizeof(t_ftlst *) * 1); + if (!lst) + return(0); + *lst = create_token_list(input, lst); + i = ft_lstsize(*lst); + printf("%d\n", i); free(lst); return (0); } diff --git a/test_mini/lexer.h b/test_mini/lexer.h index a8680da..6ae832c 100644 --- a/test_mini/lexer.h +++ b/test_mini/lexer.h @@ -1,7 +1,6 @@ # include <stdio.h> # include <stdlib.h> -# include <libc.h> # include "libft/include/libft_lst.h" # include "libft/include/libft_str.h" diff --git a/test_mini/lexer_utils.c b/test_mini/lexer_utils.c index 35050b3..d7fe8f4 100644 --- a/test_mini/lexer_utils.c +++ b/test_mini/lexer_utils.c @@ -56,26 +56,33 @@ int lexe_space(char *input) return(i); } -int simple_cote(char *input, int i) +static int lex_verif_simple_cote(char *input, int i) { - int cote; - - cote = 39; - if (cote == input[i]) - return (1); - return(0); + i++; + while(input[i] != '\0') + { + ++i; + if(input[i] == '\'') + break; + } + if (input[i + 1] == ' ') + while(input[i] == ' ') + i++; + return(i + 1); } - - int lexer_verif_entre_cote(char *input, int i) { - while((input[++i] != '"' || simple_cote(input,i)) && (input[i] != '\0')) - ; - //i++; - //if(input[i] == '"' || simple_cote(input,i)) - // return(lexer_verif_entre_cote(input, i)); - //if (input[i] == ' ') - // while(input[i] == ' ') - // i++; - return(i); + if(input[i] == '\'') + return(lex_verif_simple_cote(input, i)); + i++; + while(input[i] != '"' && (input[i] != '\0')) + { + ++i; + if (input[i] == '\'') + break; + } + if (input[i + 1] == ' ') + while(input[i] == ' ') + i++; + return(i + 1); } diff --git a/test_mini/main.c b/test_mini/main.c index 88a5127..a0006d5 100644 --- a/test_mini/main.c +++ b/test_mini/main.c @@ -2,11 +2,33 @@ #include "lexer.h" #include "parse/parse.h" -int main(void) +int main(int argc, char **argv) { - int i = -1; + int i = 0; + char *input; - lexer("echo \'bonjour\' \"ssss\'"); + if (!(input = malloc(sizeof(char) * ft_strlen(argv[1]) + 2))) + return(0); + ft_strlcpy(input, argv[1], ft_strlen(argv[1]) + 1); + i = ft_strlen(input); + input[i + 1] = '\0'; + printf("%s\n",argv[1] ); + printf("%s\n",input ); + lexer(input); + free(input); exit(0); return (0); } +/* +#include <stdio.h> +int main(int argc, char **argv, char **envp) +{ + printf("ARGV:\n"); + for (int i = 0; i < argc; i++) + printf("[%d] %s\n", i, argv[i]); + printf("\nENV:\n"); + for (int i = 0; envp[i] != NULL && i < 10; i++) + printf("[%d] %s\n", i, envp[i]); + return 0; +} +*/ diff --git a/test_mini/speudo_code b/test_mini/speudo_code index c6ad4c1..ef776ce 100644 --- a/test_mini/speudo_code +++ b/test_mini/speudo_code @@ -2,51 +2,14 @@ is_space -t_lex *lexer(char *input) +#include <stdio.h> +int main(int argc, char **argv, char **envp) { - char **out; - int j; - int i; - - i = 0; - j = 0; - if (!input) - return (0); - i = lexer_count_nb_element(input); - if (!(out = malloc(sizeof(char *) * i + 1))) - return (0); - out[i + 1] = NULL; - out = lexer_malloc_len_elem(input,i, out); - return(out); - -} - - -t_ftlst *lexer(char *input) -{ - int i; - int j; - char *temp; - enum e_token_tag token; - - i = -1; - j = 0; - if (!input) - return (0); - while(input[++i]) - { - if(lexer_sep(input[i])) - { - printf("%d\n", i - j); - token = ret_token_sep(input, i); - temp = malloc(sizeof(char) * i - j + 1); - ft_strlcpy(temp, &input[j], i - j); - j = i; - printf("%s\n", &input[i]); - } - - } - printf("%s\n", temp); - free(temp); - return (0); + printf("ARGV:\n"); + for (int i = 0; i < argc; i++) + printf("[%d] %s\n", i, argv[i]); + printf("\nENV:\n"); + for (int i = 0; envp[i] != NULL && i < 10; i++) + printf("[%d] %s\n", i, envp[i]); + return 0; } |
