From c89e8359a41cb82b52e6b80ae76488c9e274425c Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 28 Jun 2024 22:03:41 +0200 Subject: added tokenizer funktion with connection to struct the test with the printable function didnt worked have to update main as well added currently strncpy in tokenizer test reason later needs to be move also need to add the token part for pipe --- src/tokenizer.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/tokenizer.c (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c new file mode 100644 index 0000000..086d4b0 --- /dev/null +++ b/src/tokenizer.c @@ -0,0 +1,158 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tokenizer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ +/* Updated: 2024/06/28 21:58:38 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include "token.h" + +char *ft_strncpy(char *dst, char *src, size_t n) +{ + char *start; + + start = dst; + while (n-- > 0 && *src) + *dst++ = *src++; + if (n > 0) + ft_memset(dst, '\0', n); + return (start); +} + +void print_token(t_token *token) +{ + if (token->type == STRING_TOKEN) + { + printf("STRING_TOKEN: %s\n", token->content.string); + } + else if (token->type == REDIR_TOKEN) + { + printf("REDIR_TOKEN: %d\n", token->content.redir_type); + } + else if (token->type == PIPE_TOKEN) + { + printf("PIPE_TOKEN\n"); + } +} + + + +void conditional_print(char *string, int start_of_string, int i, int offset, t_token **token_list) +{ + char *trimmed_line; + char *line; + int len; + + len = i + offset - start_of_string; + if (len > 0) + { + line = (char *)malloc(len + 1); + if (!line) + { + exit(EXIT_FAILURE); + } + ft_strncpy(line, string + start_of_string, len); + line[len] = '\0'; + trimmed_line = line; + while (*trimmed_line == ' ' || *trimmed_line == '\t') + { + trimmed_line++; + } + if (*trimmed_line != '\0') + { + *token_list = new_str_token(trimmed_line, *token_list, NULL); + print_token(*token_list); + } + free(line); + } +} + +void tokenizer(char *s, t_token **token_list) +{ + char *quotes; + char quote_check; + char c; + int start_of_string; + int ignore_space; + int flag; + int i; + int skip; + + quotes = "\"\'"; + quote_check = '\0'; + start_of_string = 0; + ignore_space = 0; + flag = 0; + i = 0; + skip = 0; + while (s[i]) + { + c = s[i]; + if (skip) + { + skip = 0; + i++; + continue ; + } + if (!ignore_space && (c == '|' || c == ';' || c == '<' || c == '>')) + { + conditional_print(s, start_of_string, i, 0, token_list); + if ((c == '<' || c == '>') && s[i + 1] == c) + { + if (c == '<') + { + *token_list = new_redir_token(INPUT_FILE, + *token_list, NULL); + print_token(*token_list); + } + else + { + *token_list = new_redir_token(OUTPUT_APPEND, + *token_list, NULL); + print_token(*token_list); + } + i++; + } + else + { + if (c == '<') + { + *token_list = new_redir_token(INPUT_FILE, + *token_list, NULL); + } + else + { + *token_list = new_redir_token(OUTPUT_OVERRIDE, + *token_list, NULL); + } + print_token(*token_list); + } + start_of_string = i + 1; + } + else if (ignore_space && c == quote_check) + { + quote_check = '\0'; + ignore_space = 0; + } + else if (!ignore_space && ft_strchr(quotes, c)) + { + quote_check = c; + ignore_space = 1; + } + else if ((!ignore_space && (s[i] != ' ' || s[i] != '\t')) || s[i + 1] == '\0') + { + if (s[i + 1]) + conditional_print(s, start_of_string, i, 1, token_list); + else + conditional_print(s, start_of_string, i, 0, token_list); + start_of_string = i + 1; + } + i++; + } +} \ No newline at end of file -- cgit v1.2.3 From e0be5c1a5f65107468781b8dd84f2b32d5f12bec Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Mon, 22 Jul 2024 14:33:24 +0200 Subject: some testcases and changes --- src/tokenizer.c | 381 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 321 insertions(+), 60 deletions(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index 086d4b0..ddc33da 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,23 +6,32 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/06/28 21:58:38 by chuhlig ### ########.fr */ +/* Updated: 2024/07/22 14:18:02 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "token.h" -char *ft_strncpy(char *dst, char *src, size_t n) +// char *ft_strncpy(char *dst, const char *src, size_t n) +// { +// char *start; + +// start = dst; +// while (n-- > 0 && *src) +// *dst++ = *src++; +// while (n-- > 0) +// *dst++ = '\0'; +// return (start); +// } +char *ft_strncpy(char *s1, char *s2, int n) { - char *start; - - start = dst; - while (n-- > 0 && *src) - *dst++ = *src++; - if (n > 0) - ft_memset(dst, '\0', n); - return (start); + int i = -1; + + while (++i < n && s2[i]) + s1[i] = s2[i]; + s1[i] = '\0'; + return (s1); } void print_token(t_token *token) @@ -39,37 +48,36 @@ void print_token(t_token *token) { printf("PIPE_TOKEN\n"); } + else if (token->type == NEWLINE_TOKEN) + { + printf("NEWLINE_TOKEN\n"); + } } - - +//s[i] should be right bc i wanna start at specific pos void conditional_print(char *string, int start_of_string, int i, int offset, t_token **token_list) { - char *trimmed_line; char *line; int len; - len = i + offset - start_of_string; + len = i + offset - start_of_string + 1; if (len > 0) { - line = (char *)malloc(len + 1); + line = (char *)malloc(len); if (!line) { exit(EXIT_FAILURE); } ft_strncpy(line, string + start_of_string, len); line[len] = '\0'; - trimmed_line = line; - while (*trimmed_line == ' ' || *trimmed_line == '\t') + while (*line == ' ' || *line == '\t' || *line == '\0') + line++; + if (*line != '\0') { - trimmed_line++; - } - if (*trimmed_line != '\0') - { - *token_list = new_str_token(trimmed_line, *token_list, NULL); + *token_list = new_str_token(line, *token_list, NULL); print_token(*token_list); } - free(line); + // free(line); } } @@ -77,82 +85,335 @@ void tokenizer(char *s, t_token **token_list) { char *quotes; char quote_check; - char c; + // char c; int start_of_string; int ignore_space; - int flag; int i; - int skip; quotes = "\"\'"; quote_check = '\0'; start_of_string = 0; ignore_space = 0; - flag = 0; i = 0; - skip = 0; - while (s[i]) + if (!s || !*s) + return ; + while (s[i] && s) { - c = s[i]; - if (skip) - { - skip = 0; - i++; - continue ; - } - if (!ignore_space && (c == '|' || c == ';' || c == '<' || c == '>')) + // c = s[i]; + // if (!ignore_space && (c == '|' || c == '\n' || c == '<' || c == '>')) + if (!ignore_space && (s[i] == '|' || s[i] == '\n' || s[i] == '<' || s[i] == '>')) { conditional_print(s, start_of_string, i, 0, token_list); - if ((c == '<' || c == '>') && s[i + 1] == c) + // if ((c == '<' || c == '>') && s[i + 1] == c) + if ((s[i] == '<' || s[i] == '>') && s[i + 1] == s[i]) { - if (c == '<') - { - *token_list = new_redir_token(INPUT_FILE, + // if (c == '<') + if (s[i] == '<') + *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); - print_token(*token_list); - } else - { *token_list = new_redir_token(OUTPUT_APPEND, *token_list, NULL); - print_token(*token_list); - } + print_token(*token_list); i++; } else { - if (c == '<') - { + // if (c == '<') + if (s[i] == '<') *token_list = new_redir_token(INPUT_FILE, *token_list, NULL); - } + // else if (c == '|') + else if (s[i] == '|') + *token_list = new_token(PIPE_TOKEN, *token_list, NULL); + // else if (c == '\n') + else if (s[i] == '\n') + *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); else - { *token_list = new_redir_token(OUTPUT_OVERRIDE, *token_list, NULL); - } print_token(*token_list); + // i++; } start_of_string = i + 1; } - else if (ignore_space && c == quote_check) + // else if (ignore_space && c == quote_check) + // mew update for the sting part 18.07 + else if (ignore_space && s[i] == quote_check) { quote_check = '\0'; ignore_space = 0; } - else if (!ignore_space && ft_strchr(quotes, c)) + // else if (!ignore_space && ft_strchr(quotes, c)) + else if (!ignore_space && ft_strchr(quotes, s[i])) { - quote_check = c; + quote_check = s[i]; ignore_space = 1; } - else if ((!ignore_space && (s[i] != ' ' || s[i] != '\t')) || s[i + 1] == '\0') + // else if ((!ignore_space && (c == '\0' || c == ' ' || c == '\t')) || i == ft_strlen(s) - 1) + if ((!ignore_space && (s[i] == '\0' || s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) { - if (s[i + 1]) - conditional_print(s, start_of_string, i, 1, token_list); - else - conditional_print(s, start_of_string, i, 0, token_list); + if (s[i + 1] == '\0') + i++; + conditional_print(s, start_of_string, i, 0, token_list); start_of_string = i + 1; } + // else if (!ignore_space && ft_strchr(quotes, c)) + // else if (!ignore_space) + // { + // if(ft_strchr(quotes, s[i])) + // { + // quote_check = s[i]; + // ignore_space = 1; + // } + // else if (s[i] == '\0' || s[i] == ' ' || s[i] == '\t' || i == ft_strlen(s) - 1) + // { + // if (s[i + 1] == '\0') + // i++; + // conditional_print(s, start_of_string, i, 0, token_list); + // start_of_string = i + 1; + // } + // } + // else if (ignore_space && (s[i] == quote_check || s[i + 1] == '\0'))// secon part out of while loop? + // { + // quote_check = '\0'; + // ignore_space = 0; + // conditional_print(s, start_of_string, i, 0, token_list); + // start_of_string = i + 1; + // } i++; } -} \ No newline at end of file +} + +///s[i] != ' ' +//mazbe remove c +// why handle space? +//readline delete mazbe? + +// errors today if i change the con print cond removing *line == '\0' +// ./minishell +// Minishell $ test +// Minishell $ idk +// Minishell $ why you idiot a re doing nothing +// zsh: segmentation fault ./minishell +// chuhlig@1-E-9 minishell % ./minishell +// Minishell $ a +// Minishell $ aaa +// Minishell $ aaaaaaaaaaaaaa +// Minishell $ aa aa +// zsh: segmentation fault ./minishell + +// also some space reconizing is wrong +//also the part with cond &s[i] or just s seem f up +//tested different strncpy still errors +// removed anoying brackets to short it +//added pipe and \n token +// string still not works fine mazybe readline and input understanding + +// error with extra condition: +// ./minishell +// Minishell $ test +// STRING_TOKEN: � +// ================================================================= +// ==66482==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000002835 at pc 0x00010bb02c68 bp 0x7ffee41034b0 sp 0x7ffee41034a8 +// READ of size 1 at 0x602000002835 thread T0 +// #0 0x10bb02c67 in tokenizer tokenizer.c:91 +// #1 0x10bb00c66 in repl repl.c:30 +// #2 0x10bb00684 in main main.c:19 +// #3 0x7fff733a7cc8 in start+0x0 (libdyld.dylib:x86_64+0x1acc8) + +// 0x602000002835 is located 0 bytes to the right of 5-byte region [0x602000002830,0x602000002835) +// allocated by thread T0 here: +// #0 0x10bba517d in wrap_malloc+0x9d (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x4917d) +// #1 0x10bb38c44 in xmalloc+0x8 (libreadline.8.dylib:x86_64+0x25c44) +// #2 0x10bb16096 in readline_internal_teardown+0xfa (libreadline.8.dylib:x86_64+0x3096) +// #3 0x10bb15bb4 in readline+0x5b (libreadline.8.dylib:x86_64+0x2bb4) +// #4 0x10bb00ba3 in repl repl.c:25 +// #5 0x10bb00684 in main main.c:19 +// #6 0x7fff733a7cc8 in start+0x0 (libdyld.dylib:x86_64+0x1acc8) + +// SUMMARY: AddressSanitizer: heap-buffer-overflow tokenizer.c:91 in tokenizer +// Shadow bytes around the buggy address: +// 0x1c04000004b0: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa +// 0x1c04000004c0: fa fa fd fa fa fa fd fa fa fa fd fd fa fa fd fd +// 0x1c04000004d0: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd +// 0x1c04000004e0: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd +// 0x1c04000004f0: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd +// =>0x1c0400000500: fa fa 00 00 fa fa[05]fa fa fa 00 04 fa fa 05 fa +// 0x1c0400000510: fa fa 05 fa fa fa fa fa fa fa fa fa fa fa fa fa +// 0x1c0400000520: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa +// 0x1c0400000530: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa +// 0x1c0400000540: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa +// 0x1c0400000550: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa +// Shadow byte legend (one shadow byte represents 8 application bytes): +// Addressable: 00 +// Partially addressable: 01 02 03 04 05 06 07 +// Heap left redzone: fa +// Freed heap region: fd +// Stack left redzone: f1 +// Stack mid redzone: f2 +// Stack right redzone: f3 +// Stack after return: f5 +// Stack use after scope: f8 +// Global redzone: f9 +// Global init order: f6 +// Poisoned by user: f7 +// Container overflow: fc +// Array cookie: ac +// Intra object redzone: bb +// ASan internal: fe +// Left alloca redzone: ca +// Right alloca redzone: cb +// Shadow gap: cc +// ==66482==ABORTING + +// othere error currently +// test +// STRING_TOKEN: +// minishell(86892,0x111ae2dc0) malloc: *** error for object 0x7fedf3200014: pointer being freed was not allocated +// minishell(86892,0x111ae2dc0) malloc: *** set a breakpoint in malloc_error_break to debug +// zsh: abort ./minishell +// chuhlig@1-E-9 minishell % ./minishell +// Minishell $ "test" +// Minishell $ "testtest" +// Minishell $ "test test" +// Minishell $ te +// STRING_TOKEN: ` +// minishell(86928,0x114a76dc0) malloc: *** error for object 0x7fcaa8d04f27: pointer being freed was not allocated +// minishell(86928,0x114a76dc0) malloc: *** set a breakpoint in malloc_error_break to debug +// zsh: abort ./minishell +// chuhlig@1-E-9 minishell % ./minishell +// Minishell $ a +// STRING_TOKEN: +// Minishell $ a +// zsh: segmentation fault ./minishell +// chuhlig@1-E-9 minishell % ./minishell +// Minishell $ a +// STRING_TOKEN: ^ +// minishell(86958,0x11c50adc0) malloc: *** error for object 0x7fb24a800011: pointer being freed was not allocated +// minishell(86958,0x11c50adc0) malloc: *** set a breakpoint in malloc_error_break to debug +// zsh: abort ./minishell +// chuhlig@1-E-9 minishell % ./minishell +// Minishell $ a +// zsh: segmentation fault ./minishell + +///do i need to update after cont print i? +// end quote not works atm and have to check the lenth +// how to handle not in quote text command +// space generell need to be fixed + +/// 1425 2207 +// Minishell $ test test "ters" +// STRING_TOKEN: test +// STRING_TOKEN: test +// STRING_TOKEN: "ters" +// Minishell $ test test "ters +// STRING_TOKEN: test +// STRING_TOKEN: test +// STRING_TOKEN: "ters +// Minishell $ test test "ters " "a +// STRING_TOKEN: test +// STRING_TOKEN: test +// STRING_TOKEN: "ters " +// STRING_TOKEN: "a +// Minishell $ |||| <><> <><> <<< >>>> ||| +// STRING_TOKEN: | +// PIPE_TOKEN +// STRING_TOKEN: | +// PIPE_TOKEN +// STRING_TOKEN: | +// PIPE_TOKEN +// STRING_TOKEN: | +// PIPE_TOKEN +// STRING_TOKEN: +// STRING_TOKEN: < +// REDIR_TOKEN: 0 +// STRING_TOKEN: > +// REDIR_TOKEN: 2 +// STRING_TOKEN: < +// REDIR_TOKEN: 0 +// STRING_TOKEN: > +// REDIR_TOKEN: 2 +// STRING_TOKEN: ^ +// STRING_TOKEN: < +// REDIR_TOKEN: 0 +// STRING_TOKEN: > +// REDIR_TOKEN: 2 +// STRING_TOKEN: < +// REDIR_TOKEN: 0 +// STRING_TOKEN: > +// REDIR_TOKEN: 2 +// STRING_TOKEN: ^ +// STRING_TOKEN: < +// REDIR_TOKEN: 1 +// STRING_TOKEN: < +// REDIR_TOKEN: 0 +// STRING_TOKEN: � +// STRING_TOKEN: > +// REDIR_TOKEN: 3 +// STRING_TOKEN: > +// REDIR_TOKEN: 3 +// STRING_TOKEN: +// STRING_TOKEN: | +// PIPE_TOKEN +// STRING_TOKEN: | +// PIPE_TOKEN +// STRING_TOKEN: | +// PIPE_TOKEN +// STRING_TOKEN: +// Minishell $ test test "ters " "a asddddsa asdss +// STRING_TOKEN: test +// STRING_TOKEN: test +// STRING_TOKEN: "ters " +// STRING_TOKEN: "a asddddsa asdss +// Minishell $ test test "ters " "a asddddsa asdss ' test +// STRING_TOKEN: test +// STRING_TOKEN: test +// STRING_TOKEN: "ters " +// STRING_TOKEN: "a asddddsa asdss ' test +// Minishell $ test test "ters " "a asddddsa asdss ' test" +// STRING_TOKEN: test +// STRING_TOKEN: test +// STRING_TOKEN: "ters " +// STRING_TOKEN: "a asddddsa asdss ' test" +// Minishell $ test test "ters " "a asddddsa asdss ' test +// STRING_TOKEN: test +// STRING_TOKEN: test +// STRING_TOKEN: "ters " +// STRING_TOKEN: "a asddddsa asdss ' test +// Minishell $ test test "ters " "a asddddsa asdss ' te' +// STRING_TOKEN: test +// STRING_TOKEN: test +// STRING_TOKEN: "ters " +// STRING_TOKEN: "a asddddsa asdss ' te' +// Minishell $ test test "ters " "a asddddsa asdss ' te'" 'tes +// STRING_TOKEN: test +// STRING_TOKEN: test +// STRING_TOKEN: "ters " +// STRING_TOKEN: "a asddddsa asdss ' te'" +// STRING_TOKEN: 'tes +// Minishell $ +// STRING_TOKEN: � +// STRING_TOKEN: ��D� +// minishell(95695,0x10c1addc0) malloc: *** error for object 0x7f844c004407: pointer being freed was not allocated +// minishell(95695,0x10c1addc0) malloc: *** set a breakpoint in malloc_error_break to debug +// zsh: abort ./minishell +// chuhlig@1-E-11 minishell % ./minishell +// Minishell $ +// STRING_TOKEN: � +// STRING_TOKEN: � +// minishell(95853,0x118e33dc0) malloc: *** error for object 0x7fe72b405977: pointer being freed was not allocated +// minishell(95853,0x118e33dc0) malloc: *** set a breakpoint in malloc_error_break to debug +// zsh: abort ./minishell +// chuhlig@1-E-11 minishell % test +// chuhlig@1-E-11 minishell % pwd +// /Users/chuhlig/Desktop/minishell +// chuhlig@1-E-11 minishell % ./minishell +// Minishell $ test +// STRING_TOKEN: +// STRING_TOKEN: test +// Minishell $ test +// STRING_TOKEN: ` +// STRING_TOKEN: +// STRING_TOKEN: test +// Minishell $ \ No newline at end of file -- cgit v1.2.3 From 0c14e472ffa09b741ed549208fbc028d759dba17 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Sun, 4 Aug 2024 14:36:51 +0200 Subject: made it norm comform exept one funkction to much --- src/tokenizer.c | 318 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 201 insertions(+), 117 deletions(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index ddc33da..4059526 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,31 +6,20 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/07/22 14:18:02 by chuhlig ### ########.fr */ +/* Updated: 2024/08/04 14:32:56 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "token.h" -// char *ft_strncpy(char *dst, const char *src, size_t n) -// { -// char *start; - -// start = dst; -// while (n-- > 0 && *src) -// *dst++ = *src++; -// while (n-- > 0) -// *dst++ = '\0'; -// return (start); -// } -char *ft_strncpy(char *s1, char *s2, int n) +char *ft_strncpy(char *s1, char *s2, int n) { - int i = -1; + int i; + i = -1; while (++i < n && s2[i]) s1[i] = s2[i]; - s1[i] = '\0'; return (s1); } @@ -54,13 +43,13 @@ void print_token(t_token *token) } } -//s[i] should be right bc i wanna start at specific pos -void conditional_print(char *string, int start_of_string, int i, int offset, t_token **token_list) +void conditional_print(char *string, int start_of_string, int i, + t_token **token_list) { char *line; int len; - len = i + offset - start_of_string + 1; + len = i - start_of_string + 1; if (len > 0) { line = (char *)malloc(len); @@ -70,117 +59,95 @@ void conditional_print(char *string, int start_of_string, int i, int offset, t_t } ft_strncpy(line, string + start_of_string, len); line[len] = '\0'; - while (*line == ' ' || *line == '\t' || *line == '\0') + while (*line == ' ' || *line == '\t') line++; if (*line != '\0') { *token_list = new_str_token(line, *token_list, NULL); print_token(*token_list); } - // free(line); } } -void tokenizer(char *s, t_token **token_list) +int symbol_checker(char *s, int i, t_token **token_list) +{ + if ((s[i] == '<' || s[i] == '>') && s[i + 1] == s[i]) + { + if (s[i] == '<') + *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); + else + *token_list = new_redir_token(OUTPUT_APPEND, *token_list, NULL); + print_token(*token_list); + i++; + } + else + { + if (s[i] == '<') + *token_list = new_redir_token(INPUT_FILE, *token_list, NULL); + else if (s[i] == '|') + *token_list = new_token(PIPE_TOKEN, *token_list, NULL); + else if (s[i] == '\n') + *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); + else + *token_list = new_redir_token(OUTPUT_OVERRIDE, *token_list, NULL); + print_token(*token_list); + } + return (i); +} + +int check_for_string(char *s, int i, int *start_of_string, t_token **token_list) { - char *quotes; char quote_check; - // char c; + int ignore_space; + + ignore_space = 0; + quote_check = '\0'; + if (ignore_space && s[i] == quote_check) + { + quote_check = '\0'; + ignore_space = 0; + } + else if (!ignore_space && ft_strchr("\"\'", s[i])) + { + quote_check = s[i]; + ignore_space = 1; + } + if ((!ignore_space && (s[i] == '\0' || s[i] == ' ' + || s[i] == '\t')) || i == ft_strlen(s) - 1) + { + if (s[i + 1] == '\0') + i++; + conditional_print(s, *start_of_string, i - 1, token_list); + *start_of_string = i + 1; + } + return (i); +} + +void tokenizer(char *s, t_token **token_list) +{ int start_of_string; int ignore_space; int i; + char quote_check; - quotes = "\"\'"; quote_check = '\0'; start_of_string = 0; ignore_space = 0; i = 0; if (!s || !*s) return ; - while (s[i] && s) + while (s && s[i]) { - // c = s[i]; - // if (!ignore_space && (c == '|' || c == '\n' || c == '<' || c == '>')) - if (!ignore_space && (s[i] == '|' || s[i] == '\n' || s[i] == '<' || s[i] == '>')) + if (!ignore_space && (s[i] == '|' || s[i] == '\n' + || s[i] == '<' || s[i] == '>')) { - conditional_print(s, start_of_string, i, 0, token_list); - // if ((c == '<' || c == '>') && s[i + 1] == c) - if ((s[i] == '<' || s[i] == '>') && s[i + 1] == s[i]) - { - // if (c == '<') - if (s[i] == '<') - *token_list = new_redir_token(INPUT_LIMITER, - *token_list, NULL); - else - *token_list = new_redir_token(OUTPUT_APPEND, - *token_list, NULL); - print_token(*token_list); - i++; - } - else - { - // if (c == '<') - if (s[i] == '<') - *token_list = new_redir_token(INPUT_FILE, - *token_list, NULL); - // else if (c == '|') - else if (s[i] == '|') - *token_list = new_token(PIPE_TOKEN, *token_list, NULL); - // else if (c == '\n') - else if (s[i] == '\n') - *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); - else - *token_list = new_redir_token(OUTPUT_OVERRIDE, - *token_list, NULL); - print_token(*token_list); - // i++; - } + i = symbol_checker(s, i, token_list); start_of_string = i + 1; } - // else if (ignore_space && c == quote_check) - // mew update for the sting part 18.07 - else if (ignore_space && s[i] == quote_check) + else { - quote_check = '\0'; - ignore_space = 0; + i = check_for_string(s, i, &start_of_string, token_list); } - // else if (!ignore_space && ft_strchr(quotes, c)) - else if (!ignore_space && ft_strchr(quotes, s[i])) - { - quote_check = s[i]; - ignore_space = 1; - } - // else if ((!ignore_space && (c == '\0' || c == ' ' || c == '\t')) || i == ft_strlen(s) - 1) - if ((!ignore_space && (s[i] == '\0' || s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) - { - if (s[i + 1] == '\0') - i++; - conditional_print(s, start_of_string, i, 0, token_list); - start_of_string = i + 1; - } - // else if (!ignore_space && ft_strchr(quotes, c)) - // else if (!ignore_space) - // { - // if(ft_strchr(quotes, s[i])) - // { - // quote_check = s[i]; - // ignore_space = 1; - // } - // else if (s[i] == '\0' || s[i] == ' ' || s[i] == '\t' || i == ft_strlen(s) - 1) - // { - // if (s[i + 1] == '\0') - // i++; - // conditional_print(s, start_of_string, i, 0, token_list); - // start_of_string = i + 1; - // } - // } - // else if (ignore_space && (s[i] == quote_check || s[i + 1] == '\0'))// secon part out of while loop? - // { - // quote_check = '\0'; - // ignore_space = 0; - // conditional_print(s, start_of_string, i, 0, token_list); - // start_of_string = i + 1; - // } i++; } } @@ -215,18 +182,23 @@ void tokenizer(char *s, t_token **token_list) // Minishell $ test // STRING_TOKEN: � // ================================================================= -// ==66482==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000002835 at pc 0x00010bb02c68 bp 0x7ffee41034b0 sp 0x7ffee41034a8 +// ==66482==ERROR: AddressSanitizer: +//heap-buffer-overflow on address 0x602000002835 +//at pc 0x00010bb02c68 bp 0x7ffee41034b0 sp 0x7ffee41034a8 // READ of size 1 at 0x602000002835 thread T0 // #0 0x10bb02c67 in tokenizer tokenizer.c:91 // #1 0x10bb00c66 in repl repl.c:30 // #2 0x10bb00684 in main main.c:19 // #3 0x7fff733a7cc8 in start+0x0 (libdyld.dylib:x86_64+0x1acc8) -// 0x602000002835 is located 0 bytes to the right of 5-byte region [0x602000002830,0x602000002835) +// 0x602000002835 is located 0 bytes +//to the right of 5-byte region [0x602000002830,0x602000002835) // allocated by thread T0 here: -// #0 0x10bba517d in wrap_malloc+0x9d (libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x4917d) +// #0 0x10bba517d in wrap_malloc+0x9d +//(libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x4917d) // #1 0x10bb38c44 in xmalloc+0x8 (libreadline.8.dylib:x86_64+0x25c44) -// #2 0x10bb16096 in readline_internal_teardown+0xfa (libreadline.8.dylib:x86_64+0x3096) +// #2 0x10bb16096 in readline_internal_teardown+0xfa +//(libreadline.8.dylib:x86_64+0x3096) // #3 0x10bb15bb4 in readline+0x5b (libreadline.8.dylib:x86_64+0x2bb4) // #4 0x10bb00ba3 in repl repl.c:25 // #5 0x10bb00684 in main main.c:19 @@ -270,8 +242,10 @@ void tokenizer(char *s, t_token **token_list) // othere error currently // test // STRING_TOKEN: -// minishell(86892,0x111ae2dc0) malloc: *** error for object 0x7fedf3200014: pointer being freed was not allocated -// minishell(86892,0x111ae2dc0) malloc: *** set a breakpoint in malloc_error_break to debug +// minishell(86892,0x111ae2dc0) malloc: *** +//error for object 0x7fedf3200014: pointer being freed was not allocated +// minishell(86892,0x111ae2dc0) malloc: *** +//set a breakpoint in malloc_error_break to debug // zsh: abort ./minishell // chuhlig@1-E-9 minishell % ./minishell // Minishell $ "test" @@ -279,8 +253,10 @@ void tokenizer(char *s, t_token **token_list) // Minishell $ "test test" // Minishell $ te // STRING_TOKEN: ` -// minishell(86928,0x114a76dc0) malloc: *** error for object 0x7fcaa8d04f27: pointer being freed was not allocated -// minishell(86928,0x114a76dc0) malloc: *** set a breakpoint in malloc_error_break to debug +// minishell(86928,0x114a76dc0) malloc: *** +//error for object 0x7fcaa8d04f27: pointer being freed was not allocated +// minishell(86928,0x114a76dc0) malloc: *** +//set a breakpoint in malloc_error_break to debug // zsh: abort ./minishell // chuhlig@1-E-9 minishell % ./minishell // Minishell $ a @@ -290,8 +266,10 @@ void tokenizer(char *s, t_token **token_list) // chuhlig@1-E-9 minishell % ./minishell // Minishell $ a // STRING_TOKEN: ^ -// minishell(86958,0x11c50adc0) malloc: *** error for object 0x7fb24a800011: pointer being freed was not allocated -// minishell(86958,0x11c50adc0) malloc: *** set a breakpoint in malloc_error_break to debug +// minishell(86958,0x11c50adc0) malloc: *** +//error for object 0x7fb24a800011: pointer being freed was not allocated +// minishell(86958,0x11c50adc0) malloc: *** +//set a breakpoint in malloc_error_break to debug // zsh: abort ./minishell // chuhlig@1-E-9 minishell % ./minishell // Minishell $ a @@ -395,15 +373,19 @@ void tokenizer(char *s, t_token **token_list) // Minishell $ // STRING_TOKEN: � // STRING_TOKEN: ��D� -// minishell(95695,0x10c1addc0) malloc: *** error for object 0x7f844c004407: pointer being freed was not allocated -// minishell(95695,0x10c1addc0) malloc: *** set a breakpoint in malloc_error_break to debug +// minishell(95695,0x10c1addc0) malloc: *** error +//for object 0x7f844c004407: pointer being freed was not allocated +// minishell(95695,0x10c1addc0) malloc: *** +//set a breakpoint in malloc_error_break to debug // zsh: abort ./minishell // chuhlig@1-E-11 minishell % ./minishell // Minishell $ // STRING_TOKEN: � // STRING_TOKEN: � -// minishell(95853,0x118e33dc0) malloc: *** error for object 0x7fe72b405977: pointer being freed was not allocated -// minishell(95853,0x118e33dc0) malloc: *** set a breakpoint in malloc_error_break to debug +// minishell(95853,0x118e33dc0) malloc: *** error for object +//0x7fe72b405977: pointer being freed was not allocated +// minishell(95853,0x118e33dc0) malloc: *** +//set a breakpoint in malloc_error_break to debug // zsh: abort ./minishell // chuhlig@1-E-11 minishell % test // chuhlig@1-E-11 minishell % pwd @@ -416,4 +398,106 @@ void tokenizer(char *s, t_token **token_list) // STRING_TOKEN: ` // STRING_TOKEN: // STRING_TOKEN: test -// Minishell $ \ No newline at end of file +// Minishell $ +///////////////////////////////// +// ./minishell +// Minishell $ test +// STRING_TOKEN: test +// Minishell $ test +// STRING_TOKEN: test +// Minishell $ test +// STRING_TOKEN: test +// Minishell $ test dsads a +// STRING_TOKEN: test +// STRING_TOKEN: dsads +// STRING_TOKEN: a +// Minishell $ test dsads adsadsa saddas as dadsdas a dsdsa d "test" +// STRING_TOKEN: test +// STRING_TOKEN: dsads +// STRING_TOKEN: adsadsa +// STRING_TOKEN: saddas +// STRING_TOKEN: as +// STRING_TOKEN: dadsdas +// STRING_TOKEN: a +// STRING_TOKEN: dsdsa +// STRING_TOKEN: d +// STRING_TOKEN: "test" +// Minishell $ test dsads adsadsa saddas as dadsdas a dsdsa d "test +// STRING_TOKEN: test +// STRING_TOKEN: dsads +// STRING_TOKEN: adsadsa +// STRING_TOKEN: saddas +// STRING_TOKEN: as +// STRING_TOKEN: dadsdas +// STRING_TOKEN: a +// STRING_TOKEN: dsdsa +// STRING_TOKEN: d +// STRING_TOKEN: "test +// Minishell test dsads adsadsa saddas as dadsdas a dsdsa d "test ... <>< +// STRING_TOKEN: test +// STRING_TOKEN: dsads +// STRING_TOKEN: adsadsa +// STRING_TOKEN: saddas +// STRING_TOKEN: as +// STRING_TOKEN: dadsdas +// STRING_TOKEN: a +// STRING_TOKEN: dsdsa +// STRING_TOKEN: d +// STRING_TOKEN: "test ... <><><>|| +/// now follow a older version where i tried to eliminate the "" case + // else if (!ignore_space && ft_strchr(quotes, c)) + // else if (!ignore_space) + // { + // if(ft_strchr(quotes, s[i])) + // { + // quote_check = s[i]; + // ignore_space = 1; + // } + // else if (s[i] == '\0' || s[i] == ' ' || + //s[i] == '\t' || i == ft_strlen(s) - 1) + // { + // if (s[i + 1] == '\0') + // i++; + // conditional_print(s, start_of_string, i, 0, token_list); + // start_of_string = i + 1; + // } + // } + // else if (ignore_space && (s[i] == quote_check + //|| s[i + 1] == '\0'))// secon part out of while loop? + // { + // quote_check = '\0'; + // ignore_space = 0; + // conditional_print(s, start_of_string, i, 0, token_list); + // start_of_string = i + 1; + // } + +// char *ft_strncpy(char *dst, const char *src, size_t n) +// { +// char *start; + +// start = dst; +// while (n-- > 0 && *src) +// *dst++ = *src++; +// while (n-- > 0) +// *dst++ = '\0'; +// return (start); +// } +//costum is space? + // else if (ignore_space && s[i] == quote_check) + // { + // quote_check = '\0'; + // ignore_space = 0; + // } + // else if (!ignore_space && ft_strchr("\"\'", s[i])) + // { + // quote_check = s[i]; + // ignore_space = 1; + // } + // if ((!ignore_space && (s[i] == '\0' + //|| s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) + // { + // if (s[i + 1] == '\0') + // i++; + // conditional_print(s, start_of_string, i - 1, 0, token_list); + // start_of_string = i + 1; + // } \ No newline at end of file -- cgit v1.2.3 From 429730860776816050abd40a318de993c0c69e17 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Mon, 5 Aug 2024 13:25:33 +0200 Subject: removed notes out of tokenizer.c --- src/tokenizer.c | 352 +------------------------------------------------------- 1 file changed, 1 insertion(+), 351 deletions(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index 4059526..5aa4895 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/04 14:32:56 by chuhlig ### ########.fr */ +/* Updated: 2024/08/05 13:24:59 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -151,353 +151,3 @@ void tokenizer(char *s, t_token **token_list) i++; } } - -///s[i] != ' ' -//mazbe remove c -// why handle space? -//readline delete mazbe? - -// errors today if i change the con print cond removing *line == '\0' -// ./minishell -// Minishell $ test -// Minishell $ idk -// Minishell $ why you idiot a re doing nothing -// zsh: segmentation fault ./minishell -// chuhlig@1-E-9 minishell % ./minishell -// Minishell $ a -// Minishell $ aaa -// Minishell $ aaaaaaaaaaaaaa -// Minishell $ aa aa -// zsh: segmentation fault ./minishell - -// also some space reconizing is wrong -//also the part with cond &s[i] or just s seem f up -//tested different strncpy still errors -// removed anoying brackets to short it -//added pipe and \n token -// string still not works fine mazybe readline and input understanding - -// error with extra condition: -// ./minishell -// Minishell $ test -// STRING_TOKEN: � -// ================================================================= -// ==66482==ERROR: AddressSanitizer: -//heap-buffer-overflow on address 0x602000002835 -//at pc 0x00010bb02c68 bp 0x7ffee41034b0 sp 0x7ffee41034a8 -// READ of size 1 at 0x602000002835 thread T0 -// #0 0x10bb02c67 in tokenizer tokenizer.c:91 -// #1 0x10bb00c66 in repl repl.c:30 -// #2 0x10bb00684 in main main.c:19 -// #3 0x7fff733a7cc8 in start+0x0 (libdyld.dylib:x86_64+0x1acc8) - -// 0x602000002835 is located 0 bytes -//to the right of 5-byte region [0x602000002830,0x602000002835) -// allocated by thread T0 here: -// #0 0x10bba517d in wrap_malloc+0x9d -//(libclang_rt.asan_osx_dynamic.dylib:x86_64h+0x4917d) -// #1 0x10bb38c44 in xmalloc+0x8 (libreadline.8.dylib:x86_64+0x25c44) -// #2 0x10bb16096 in readline_internal_teardown+0xfa -//(libreadline.8.dylib:x86_64+0x3096) -// #3 0x10bb15bb4 in readline+0x5b (libreadline.8.dylib:x86_64+0x2bb4) -// #4 0x10bb00ba3 in repl repl.c:25 -// #5 0x10bb00684 in main main.c:19 -// #6 0x7fff733a7cc8 in start+0x0 (libdyld.dylib:x86_64+0x1acc8) - -// SUMMARY: AddressSanitizer: heap-buffer-overflow tokenizer.c:91 in tokenizer -// Shadow bytes around the buggy address: -// 0x1c04000004b0: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa -// 0x1c04000004c0: fa fa fd fa fa fa fd fa fa fa fd fd fa fa fd fd -// 0x1c04000004d0: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd -// 0x1c04000004e0: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd -// 0x1c04000004f0: fa fa fd fd fa fa fd fd fa fa fd fd fa fa fd fd -// =>0x1c0400000500: fa fa 00 00 fa fa[05]fa fa fa 00 04 fa fa 05 fa -// 0x1c0400000510: fa fa 05 fa fa fa fa fa fa fa fa fa fa fa fa fa -// 0x1c0400000520: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa -// 0x1c0400000530: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa -// 0x1c0400000540: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa -// 0x1c0400000550: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa -// Shadow byte legend (one shadow byte represents 8 application bytes): -// Addressable: 00 -// Partially addressable: 01 02 03 04 05 06 07 -// Heap left redzone: fa -// Freed heap region: fd -// Stack left redzone: f1 -// Stack mid redzone: f2 -// Stack right redzone: f3 -// Stack after return: f5 -// Stack use after scope: f8 -// Global redzone: f9 -// Global init order: f6 -// Poisoned by user: f7 -// Container overflow: fc -// Array cookie: ac -// Intra object redzone: bb -// ASan internal: fe -// Left alloca redzone: ca -// Right alloca redzone: cb -// Shadow gap: cc -// ==66482==ABORTING - -// othere error currently -// test -// STRING_TOKEN: -// minishell(86892,0x111ae2dc0) malloc: *** -//error for object 0x7fedf3200014: pointer being freed was not allocated -// minishell(86892,0x111ae2dc0) malloc: *** -//set a breakpoint in malloc_error_break to debug -// zsh: abort ./minishell -// chuhlig@1-E-9 minishell % ./minishell -// Minishell $ "test" -// Minishell $ "testtest" -// Minishell $ "test test" -// Minishell $ te -// STRING_TOKEN: ` -// minishell(86928,0x114a76dc0) malloc: *** -//error for object 0x7fcaa8d04f27: pointer being freed was not allocated -// minishell(86928,0x114a76dc0) malloc: *** -//set a breakpoint in malloc_error_break to debug -// zsh: abort ./minishell -// chuhlig@1-E-9 minishell % ./minishell -// Minishell $ a -// STRING_TOKEN: -// Minishell $ a -// zsh: segmentation fault ./minishell -// chuhlig@1-E-9 minishell % ./minishell -// Minishell $ a -// STRING_TOKEN: ^ -// minishell(86958,0x11c50adc0) malloc: *** -//error for object 0x7fb24a800011: pointer being freed was not allocated -// minishell(86958,0x11c50adc0) malloc: *** -//set a breakpoint in malloc_error_break to debug -// zsh: abort ./minishell -// chuhlig@1-E-9 minishell % ./minishell -// Minishell $ a -// zsh: segmentation fault ./minishell - -///do i need to update after cont print i? -// end quote not works atm and have to check the lenth -// how to handle not in quote text command -// space generell need to be fixed - -/// 1425 2207 -// Minishell $ test test "ters" -// STRING_TOKEN: test -// STRING_TOKEN: test -// STRING_TOKEN: "ters" -// Minishell $ test test "ters -// STRING_TOKEN: test -// STRING_TOKEN: test -// STRING_TOKEN: "ters -// Minishell $ test test "ters " "a -// STRING_TOKEN: test -// STRING_TOKEN: test -// STRING_TOKEN: "ters " -// STRING_TOKEN: "a -// Minishell $ |||| <><> <><> <<< >>>> ||| -// STRING_TOKEN: | -// PIPE_TOKEN -// STRING_TOKEN: | -// PIPE_TOKEN -// STRING_TOKEN: | -// PIPE_TOKEN -// STRING_TOKEN: | -// PIPE_TOKEN -// STRING_TOKEN: -// STRING_TOKEN: < -// REDIR_TOKEN: 0 -// STRING_TOKEN: > -// REDIR_TOKEN: 2 -// STRING_TOKEN: < -// REDIR_TOKEN: 0 -// STRING_TOKEN: > -// REDIR_TOKEN: 2 -// STRING_TOKEN: ^ -// STRING_TOKEN: < -// REDIR_TOKEN: 0 -// STRING_TOKEN: > -// REDIR_TOKEN: 2 -// STRING_TOKEN: < -// REDIR_TOKEN: 0 -// STRING_TOKEN: > -// REDIR_TOKEN: 2 -// STRING_TOKEN: ^ -// STRING_TOKEN: < -// REDIR_TOKEN: 1 -// STRING_TOKEN: < -// REDIR_TOKEN: 0 -// STRING_TOKEN: � -// STRING_TOKEN: > -// REDIR_TOKEN: 3 -// STRING_TOKEN: > -// REDIR_TOKEN: 3 -// STRING_TOKEN: -// STRING_TOKEN: | -// PIPE_TOKEN -// STRING_TOKEN: | -// PIPE_TOKEN -// STRING_TOKEN: | -// PIPE_TOKEN -// STRING_TOKEN: -// Minishell $ test test "ters " "a asddddsa asdss -// STRING_TOKEN: test -// STRING_TOKEN: test -// STRING_TOKEN: "ters " -// STRING_TOKEN: "a asddddsa asdss -// Minishell $ test test "ters " "a asddddsa asdss ' test -// STRING_TOKEN: test -// STRING_TOKEN: test -// STRING_TOKEN: "ters " -// STRING_TOKEN: "a asddddsa asdss ' test -// Minishell $ test test "ters " "a asddddsa asdss ' test" -// STRING_TOKEN: test -// STRING_TOKEN: test -// STRING_TOKEN: "ters " -// STRING_TOKEN: "a asddddsa asdss ' test" -// Minishell $ test test "ters " "a asddddsa asdss ' test -// STRING_TOKEN: test -// STRING_TOKEN: test -// STRING_TOKEN: "ters " -// STRING_TOKEN: "a asddddsa asdss ' test -// Minishell $ test test "ters " "a asddddsa asdss ' te' -// STRING_TOKEN: test -// STRING_TOKEN: test -// STRING_TOKEN: "ters " -// STRING_TOKEN: "a asddddsa asdss ' te' -// Minishell $ test test "ters " "a asddddsa asdss ' te'" 'tes -// STRING_TOKEN: test -// STRING_TOKEN: test -// STRING_TOKEN: "ters " -// STRING_TOKEN: "a asddddsa asdss ' te'" -// STRING_TOKEN: 'tes -// Minishell $ -// STRING_TOKEN: � -// STRING_TOKEN: ��D� -// minishell(95695,0x10c1addc0) malloc: *** error -//for object 0x7f844c004407: pointer being freed was not allocated -// minishell(95695,0x10c1addc0) malloc: *** -//set a breakpoint in malloc_error_break to debug -// zsh: abort ./minishell -// chuhlig@1-E-11 minishell % ./minishell -// Minishell $ -// STRING_TOKEN: � -// STRING_TOKEN: � -// minishell(95853,0x118e33dc0) malloc: *** error for object -//0x7fe72b405977: pointer being freed was not allocated -// minishell(95853,0x118e33dc0) malloc: *** -//set a breakpoint in malloc_error_break to debug -// zsh: abort ./minishell -// chuhlig@1-E-11 minishell % test -// chuhlig@1-E-11 minishell % pwd -// /Users/chuhlig/Desktop/minishell -// chuhlig@1-E-11 minishell % ./minishell -// Minishell $ test -// STRING_TOKEN: -// STRING_TOKEN: test -// Minishell $ test -// STRING_TOKEN: ` -// STRING_TOKEN: -// STRING_TOKEN: test -// Minishell $ -///////////////////////////////// -// ./minishell -// Minishell $ test -// STRING_TOKEN: test -// Minishell $ test -// STRING_TOKEN: test -// Minishell $ test -// STRING_TOKEN: test -// Minishell $ test dsads a -// STRING_TOKEN: test -// STRING_TOKEN: dsads -// STRING_TOKEN: a -// Minishell $ test dsads adsadsa saddas as dadsdas a dsdsa d "test" -// STRING_TOKEN: test -// STRING_TOKEN: dsads -// STRING_TOKEN: adsadsa -// STRING_TOKEN: saddas -// STRING_TOKEN: as -// STRING_TOKEN: dadsdas -// STRING_TOKEN: a -// STRING_TOKEN: dsdsa -// STRING_TOKEN: d -// STRING_TOKEN: "test" -// Minishell $ test dsads adsadsa saddas as dadsdas a dsdsa d "test -// STRING_TOKEN: test -// STRING_TOKEN: dsads -// STRING_TOKEN: adsadsa -// STRING_TOKEN: saddas -// STRING_TOKEN: as -// STRING_TOKEN: dadsdas -// STRING_TOKEN: a -// STRING_TOKEN: dsdsa -// STRING_TOKEN: d -// STRING_TOKEN: "test -// Minishell test dsads adsadsa saddas as dadsdas a dsdsa d "test ... <>< -// STRING_TOKEN: test -// STRING_TOKEN: dsads -// STRING_TOKEN: adsadsa -// STRING_TOKEN: saddas -// STRING_TOKEN: as -// STRING_TOKEN: dadsdas -// STRING_TOKEN: a -// STRING_TOKEN: dsdsa -// STRING_TOKEN: d -// STRING_TOKEN: "test ... <><><>|| -/// now follow a older version where i tried to eliminate the "" case - // else if (!ignore_space && ft_strchr(quotes, c)) - // else if (!ignore_space) - // { - // if(ft_strchr(quotes, s[i])) - // { - // quote_check = s[i]; - // ignore_space = 1; - // } - // else if (s[i] == '\0' || s[i] == ' ' || - //s[i] == '\t' || i == ft_strlen(s) - 1) - // { - // if (s[i + 1] == '\0') - // i++; - // conditional_print(s, start_of_string, i, 0, token_list); - // start_of_string = i + 1; - // } - // } - // else if (ignore_space && (s[i] == quote_check - //|| s[i + 1] == '\0'))// secon part out of while loop? - // { - // quote_check = '\0'; - // ignore_space = 0; - // conditional_print(s, start_of_string, i, 0, token_list); - // start_of_string = i + 1; - // } - -// char *ft_strncpy(char *dst, const char *src, size_t n) -// { -// char *start; - -// start = dst; -// while (n-- > 0 && *src) -// *dst++ = *src++; -// while (n-- > 0) -// *dst++ = '\0'; -// return (start); -// } -//costum is space? - // else if (ignore_space && s[i] == quote_check) - // { - // quote_check = '\0'; - // ignore_space = 0; - // } - // else if (!ignore_space && ft_strchr("\"\'", s[i])) - // { - // quote_check = s[i]; - // ignore_space = 1; - // } - // if ((!ignore_space && (s[i] == '\0' - //|| s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) - // { - // if (s[i + 1] == '\0') - // i++; - // conditional_print(s, start_of_string, i - 1, 0, token_list); - // start_of_string = i + 1; - // } \ No newline at end of file -- cgit v1.2.3 From 68af24d0597b06516891199bde4178a2d1552e09 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Mon, 5 Aug 2024 13:43:07 +0200 Subject: remove strncpy out of tokenizer f --- src/tokenizer.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index 5aa4895..a2f6fa4 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,23 +6,13 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/05 13:24:59 by chuhlig ### ########.fr */ +/* Updated: 2024/08/05 13:41:54 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "token.h" -char *ft_strncpy(char *s1, char *s2, int n) -{ - int i; - - i = -1; - while (++i < n && s2[i]) - s1[i] = s2[i]; - return (s1); -} - void print_token(t_token *token) { if (token->type == STRING_TOKEN) -- cgit v1.2.3 From b0cd3bb37124f19fcf285976d59ae8c0ae262b66 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Mon, 5 Aug 2024 13:44:04 +0200 Subject: update for sanityzer in condiprint --- src/tokenizer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index a2f6fa4..bb76b9e 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/05 13:41:54 by chuhlig ### ########.fr */ +/* Updated: 2024/08/05 13:43:39 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,7 +48,7 @@ void conditional_print(char *string, int start_of_string, int i, exit(EXIT_FAILURE); } ft_strncpy(line, string + start_of_string, len); - line[len] = '\0'; + line[len - 1] = '\0'; while (*line == ' ' || *line == '\t') line++; if (*line != '\0') -- cgit v1.2.3 From e9909e4159ab95f2d8faf9a92ff9a53dc0bd791a Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Mon, 5 Aug 2024 13:46:24 +0200 Subject: made the if debug around print token --- src/tokenizer.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index bb76b9e..4babe52 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/05 13:43:39 by chuhlig ### ########.fr */ +/* Updated: 2024/08/05 13:45:55 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,21 +15,24 @@ void print_token(t_token *token) { - if (token->type == STRING_TOKEN) + if (DEBUG) { - printf("STRING_TOKEN: %s\n", token->content.string); - } - else if (token->type == REDIR_TOKEN) - { - printf("REDIR_TOKEN: %d\n", token->content.redir_type); - } - else if (token->type == PIPE_TOKEN) - { - printf("PIPE_TOKEN\n"); - } - else if (token->type == NEWLINE_TOKEN) - { - printf("NEWLINE_TOKEN\n"); + if (token->type == STRING_TOKEN) + { + printf("STRING_TOKEN: %s\n", token->content.string); + } + else if (token->type == REDIR_TOKEN) + { + printf("REDIR_TOKEN: %d\n", token->content.redir_type); + } + else if (token->type == PIPE_TOKEN) + { + printf("PIPE_TOKEN\n"); + } + else if (token->type == NEWLINE_TOKEN) + { + printf("NEWLINE_TOKEN\n"); + } } } -- cgit v1.2.3 From 243d01380b6cf295a540c37bc83f2e669fbc09ea Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Mon, 5 Aug 2024 13:47:00 +0200 Subject: fixed norm stuff --- src/tokenizer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index 4babe52..5bef39b 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/05 13:45:55 by chuhlig ### ########.fr */ +/* Updated: 2024/08/05 13:46:42 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,7 @@ void print_token(t_token *token) } } + void conditional_print(char *string, int start_of_string, int i, t_token **token_list) { -- cgit v1.2.3 From 0539428e0b7e17713904d4dae33e3150b74e964f Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Mon, 5 Aug 2024 21:47:31 +0200 Subject: tried without manz changes to improve it pls test it also doueble and single quote again --- lib/libft/ft_strncpy.c | 5 +++-- src/repl.c | 2 +- src/tokenizer.c | 52 +++++++++++++++++++++++++++++++++----------------- 3 files changed, 39 insertions(+), 20 deletions(-) (limited to 'src/tokenizer.c') diff --git a/lib/libft/ft_strncpy.c b/lib/libft/ft_strncpy.c index 30a9d3d..9d772cb 100644 --- a/lib/libft/ft_strncpy.c +++ b/lib/libft/ft_strncpy.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/05 13:41:47 by chuhlig #+# #+# */ -/* Updated: 2024/08/05 13:42:00 by chuhlig ### ########.fr */ +/* Updated: 2024/08/05 14:22:26 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,5 +17,6 @@ char *ft_strncpy(char *s1, char *s2, int n) i = -1; while (++i < n && s2[i]) s1[i] = s2[i]; + // s1[i] = '\0'; return (s1); -} \ No newline at end of file +} diff --git a/src/repl.c b/src/repl.c index 99293e2..e1ca7a6 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2024/07/15 19:53:52 by chuhlig ### ########.fr */ +/* Updated: 2024/08/05 20:20:02 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/tokenizer.c b/src/tokenizer.c index 5bef39b..635e31e 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/05 13:46:42 by chuhlig ### ########.fr */ +/* Updated: 2024/08/05 21:45:55 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,13 +46,13 @@ void conditional_print(char *string, int start_of_string, int i, len = i - start_of_string + 1; if (len > 0) { - line = (char *)malloc(len); + line = (char *)malloc(len + 1); if (!line) { exit(EXIT_FAILURE); } ft_strncpy(line, string + start_of_string, len); - line[len - 1] = '\0'; + line[len] = '\0'; while (*line == ' ' || *line == '\t') line++; if (*line != '\0') @@ -91,12 +91,13 @@ int symbol_checker(char *s, int i, t_token **token_list) int check_for_string(char *s, int i, int *start_of_string, t_token **token_list) { - char quote_check; - int ignore_space; + static char quote_check; + static int ignore_space; - ignore_space = 0; - quote_check = '\0'; - if (ignore_space && s[i] == quote_check) + // ignore_space = 0; + // quote_check = '\0'; + if (ignore_space && (s[i] == '\0' || s[i] == '|' || s[i] == '\n' + || s[i] == '<' || s[i] == '>')) { quote_check = '\0'; ignore_space = 0; @@ -107,11 +108,10 @@ int check_for_string(char *s, int i, int *start_of_string, t_token **token_list) ignore_space = 1; } if ((!ignore_space && (s[i] == '\0' || s[i] == ' ' - || s[i] == '\t')) || i == ft_strlen(s) - 1) + || s[i] == '\t' || s[i + 1] == '|' || s[i + 1] == '\n' + || s[i + 1] == '<' || s[i + 1] == '>')) || i == ft_strlen(s) - 1) { - if (s[i + 1] == '\0') - i++; - conditional_print(s, *start_of_string, i - 1, token_list); + conditional_print(s, *start_of_string, i, token_list); *start_of_string = i + 1; } return (i); @@ -120,19 +120,17 @@ int check_for_string(char *s, int i, int *start_of_string, t_token **token_list) void tokenizer(char *s, t_token **token_list) { int start_of_string; - int ignore_space; + int f; int i; - char quote_check; - quote_check = '\0'; start_of_string = 0; - ignore_space = 0; + f = 0; i = 0; if (!s || !*s) return ; while (s && s[i]) { - if (!ignore_space && (s[i] == '|' || s[i] == '\n' + if (!f && (s[i] == '|' || s[i] == '\n' || s[i] == '<' || s[i] == '>')) { i = symbol_checker(s, i, token_list); @@ -140,8 +138,28 @@ void tokenizer(char *s, t_token **token_list) } else { + f = start_of_string; i = check_for_string(s, i, &start_of_string, token_list); + if (f != start_of_string) + f = 0; + else + f = 1; } i++; } } + +// Minishell $ |abc|cba +// PIPE_TOKEN +// STRING_TOKEN: abc +// PIPE_TOKEN +// STRING_TOKEN: cba +// Minishell $ ||abc a||cba +// PIPE_TOKEN +// PIPE_TOKEN +// STRING_TOKEN: abc +// STRING_TOKEN: a +// PIPE_TOKEN +// PIPE_TOKEN +// STRING_TOKEN: cba +// Minishell $ \ No newline at end of file -- cgit v1.2.3 From c1f342c71f703a73c83b960bfa4321ce9be90e9a Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 8 Aug 2024 18:53:32 +0200 Subject: old version improved for edgecase and removed leaks --- src/tokenizer.c | 112 ++++++++++++++++++++++---------------------------------- 1 file changed, 44 insertions(+), 68 deletions(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index 635e31e..40d54c8 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/05 21:45:55 by chuhlig ### ########.fr */ +/* Updated: 2024/08/08 18:50:53 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,92 +63,68 @@ void conditional_print(char *string, int start_of_string, int i, } } -int symbol_checker(char *s, int i, t_token **token_list) -{ - if ((s[i] == '<' || s[i] == '>') && s[i + 1] == s[i]) - { - if (s[i] == '<') - *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); - else - *token_list = new_redir_token(OUTPUT_APPEND, *token_list, NULL); - print_token(*token_list); - i++; - } - else - { - if (s[i] == '<') - *token_list = new_redir_token(INPUT_FILE, *token_list, NULL); - else if (s[i] == '|') - *token_list = new_token(PIPE_TOKEN, *token_list, NULL); - else if (s[i] == '\n') - *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); - else - *token_list = new_redir_token(OUTPUT_OVERRIDE, *token_list, NULL); - print_token(*token_list); - } - return (i); -} - -int check_for_string(char *s, int i, int *start_of_string, t_token **token_list) -{ - static char quote_check; - static int ignore_space; - - // ignore_space = 0; - // quote_check = '\0'; - if (ignore_space && (s[i] == '\0' || s[i] == '|' || s[i] == '\n' - || s[i] == '<' || s[i] == '>')) - { - quote_check = '\0'; - ignore_space = 0; - } - else if (!ignore_space && ft_strchr("\"\'", s[i])) - { - quote_check = s[i]; - ignore_space = 1; - } - if ((!ignore_space && (s[i] == '\0' || s[i] == ' ' - || s[i] == '\t' || s[i + 1] == '|' || s[i + 1] == '\n' - || s[i + 1] == '<' || s[i + 1] == '>')) || i == ft_strlen(s) - 1) - { - conditional_print(s, *start_of_string, i, token_list); - *start_of_string = i + 1; - } - return (i); -} - void tokenizer(char *s, t_token **token_list) { + char *quotes; + char quote_check; int start_of_string; - int f; + int ignore_space; int i; + quotes = "\"\'"; + quote_check = '\0'; start_of_string = 0; - f = 0; + ignore_space = 0; i = 0; if (!s || !*s) return ; - while (s && s[i]) + while (s[i]) { - if (!f && (s[i] == '|' || s[i] == '\n' - || s[i] == '<' || s[i] == '>')) + if (!ignore_space && (s[i] == '|' || s[i] == '\n' || s[i] == '<' || s[i] == '>')) { - i = symbol_checker(s, i, token_list); + conditional_print(s, start_of_string, i - 1, token_list); + if ((s[i] == '<' || s[i] == '>') && s[i + 1] == s[i]) + { + if (s[i] == '<') + *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); + else + *token_list = new_redir_token(OUTPUT_APPEND, *token_list, NULL); + i++; + } + else + { + if (s[i] == '<') + *token_list = new_redir_token(INPUT_FILE, *token_list, NULL); + else if (s[i] == '|') + *token_list = new_token(PIPE_TOKEN, *token_list, NULL); + else if (s[i] == '\n') + *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); + else + *token_list = new_redir_token(OUTPUT_OVERRIDE, *token_list, NULL); + } + print_token(*token_list); start_of_string = i + 1; } - else + else if (ignore_space && s[i] == quote_check) { - f = start_of_string; - i = check_for_string(s, i, &start_of_string, token_list); - if (f != start_of_string) - f = 0; - else - f = 1; + quote_check = '\0'; + ignore_space = 0; + } + else if (!ignore_space && ft_strchr(quotes, s[i])) + { + quote_check = s[i]; + ignore_space = 1; + } + else if ((!ignore_space && (s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) + { + conditional_print(s, start_of_string, i, token_list); + start_of_string = i + 1; } i++; } } + // Minishell $ |abc|cba // PIPE_TOKEN // STRING_TOKEN: abc -- cgit v1.2.3 From ea3195deefdc728b7fe8268bfeab82dc85415111 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 9 Aug 2024 11:45:22 +0200 Subject: shorten version with bugg --- src/tokenizer.c | 187 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 110 insertions(+), 77 deletions(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index 40d54c8..fd4fefd 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/08 18:50:53 by chuhlig ### ########.fr */ +/* Updated: 2024/08/09 11:43:51 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,35 +15,30 @@ void print_token(t_token *token) { - if (DEBUG) + if (token->type == STRING_TOKEN) { - if (token->type == STRING_TOKEN) - { - printf("STRING_TOKEN: %s\n", token->content.string); - } - else if (token->type == REDIR_TOKEN) - { - printf("REDIR_TOKEN: %d\n", token->content.redir_type); - } - else if (token->type == PIPE_TOKEN) - { - printf("PIPE_TOKEN\n"); - } - else if (token->type == NEWLINE_TOKEN) - { - printf("NEWLINE_TOKEN\n"); - } + printf("STRING_TOKEN: %s\n", token->content.string); + } + else if (token->type == REDIR_TOKEN) + { + printf("REDIR_TOKEN: %d\n", token->content.redir_type); + } + else if (token->type == PIPE_TOKEN) + { + printf("PIPE_TOKEN\n"); + } + else if (token->type == NEWLINE_TOKEN) + { + printf("NEWLINE_TOKEN\n"); } } - -void conditional_print(char *string, int start_of_string, int i, - t_token **token_list) +void conditional_print(char *string, int start_of_string, int i, t_token **token_list) { char *line; int len; - len = i - start_of_string + 1; + len = i - start_of_string; if (len > 0) { line = (char *)malloc(len + 1); @@ -63,79 +58,117 @@ void conditional_print(char *string, int start_of_string, int i, } } +void handle_special_chars(char *s, int *i, int *start, t_token **token_list) +{ + conditional_print(s, *start, *i, token_list); // Pass correct boundaries + if (s[*i] == '<' && s[*i + 1] == '<') + *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); + else if (s[*i] == '>' && s[*i + 1] == '>') + *token_list = new_redir_token(OUTPUT_APPEND, *token_list, NULL); + else if (s[*i] == '<') + *token_list = new_redir_token(INPUT_FILE, *token_list, NULL); + else if (s[*i] == '>') + *token_list = new_redir_token(OUTPUT_OVERRIDE, *token_list, NULL); + else if (s[*i] == '|') + *token_list = new_token(PIPE_TOKEN, *token_list, NULL); + else if (s[*i] == '\n') + *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); + print_token(*token_list); + if (s[*i] == '<' || s[*i] == '>') + (*i)++; + *start = *i + 1; +} + void tokenizer(char *s, t_token **token_list) { - char *quotes; char quote_check; - int start_of_string; - int ignore_space; + int pos; int i; + int f; - quotes = "\"\'"; - quote_check = '\0'; - start_of_string = 0; - ignore_space = 0; - i = 0; - if (!s || !*s) - return ; - while (s[i]) + f = 0; + i = -1; + pos = 0; + while (s[++i]) { - if (!ignore_space && (s[i] == '|' || s[i] == '\n' || s[i] == '<' || s[i] == '>')) - { - conditional_print(s, start_of_string, i - 1, token_list); - if ((s[i] == '<' || s[i] == '>') && s[i + 1] == s[i]) - { - if (s[i] == '<') - *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); - else - *token_list = new_redir_token(OUTPUT_APPEND, *token_list, NULL); - i++; - } - else - { - if (s[i] == '<') - *token_list = new_redir_token(INPUT_FILE, *token_list, NULL); - else if (s[i] == '|') - *token_list = new_token(PIPE_TOKEN, *token_list, NULL); - else if (s[i] == '\n') - *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); - else - *token_list = new_redir_token(OUTPUT_OVERRIDE, *token_list, NULL); - } - print_token(*token_list); - start_of_string = i + 1; - } - else if (ignore_space && s[i] == quote_check) - { - quote_check = '\0'; - ignore_space = 0; - } - else if (!ignore_space && ft_strchr(quotes, s[i])) + if (!f && ft_strchr("|<>\\n", s[i])) + handle_special_chars(s, &i, &pos, token_list); + else if (f && s[i] == quote_check) + f = 0; + else if (!f && ft_strchr("\'\"", s[i])) { + f = 1; quote_check = s[i]; - ignore_space = 1; } - else if ((!ignore_space && (s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) + if ((!f && (s[i] == ' ' || s[i] == '\t')) || s[i + 1] == '\0') { - conditional_print(s, start_of_string, i, token_list); - start_of_string = i + 1; + conditional_print(s, pos, i, token_list); + pos = i + 1; } - i++; } } +// Minishell $ echo "Hello World"|grep 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" +// PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// Minishell $ -// Minishell $ |abc|cba +// Minishell $ Minishell $ echo "Hello World"|grep 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" +// PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// Minishell $ +// STRING_TOKEN: Mi +// STRING_TOKEN: Mi +// STRING_TOKEN: ishell +// STRING_TOKEN: $ +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" +// PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" +// PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello // PIPE_TOKEN -// STRING_TOKEN: abc +// STRING_TOKEN: cat +// STRING_TOKEN: -e +// Mi +// STRING_TOKEN: -e +// Mi +// STRING_TOKEN: ishell +// STRING_TOKEN: $ +// Minishell $ echo "Hello World"|grep 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" // PIPE_TOKEN -// STRING_TOKEN: cba -// Minishell $ ||abc a||cba +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// Minishell $ echo "Hello World"|grep 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" // PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// Minishell $ echo "Hello World"|grep 'Hello|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" // PIPE_TOKEN -// STRING_TOKEN: abc -// STRING_TOKEN: a +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello|cat -e +// Minishell $ echo "Hello World"|grep 'Hello'|cat -e +// STRING_TOKEN: echo +// STRING_TOKEN: "Hello World" // PIPE_TOKEN +// STRING_TOKEN: grep +// STRING_TOKEN: 'Hello' // PIPE_TOKEN -// STRING_TOKEN: cba -// Minishell $ \ No newline at end of file +// STRING_TOKEN: cat +// STRING_TOKEN: -e \ No newline at end of file -- cgit v1.2.3 From 4bcb095085729d9681f58ae110a3b550e4c697eb Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 9 Aug 2024 12:40:16 +0200 Subject: fixed the -1 letter tstill dont try \\ xd and for the case < in.txt cat -e > out.txt | grep test im working on it >D --- src/tokenizer.c | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index fd4fefd..f8ab9c4 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/09 11:43:51 by chuhlig ### ########.fr */ +/* Updated: 2024/08/09 12:37:08 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,30 +15,34 @@ void print_token(t_token *token) { - if (token->type == STRING_TOKEN) + if (DEBUG) { - printf("STRING_TOKEN: %s\n", token->content.string); - } - else if (token->type == REDIR_TOKEN) - { - printf("REDIR_TOKEN: %d\n", token->content.redir_type); - } - else if (token->type == PIPE_TOKEN) - { - printf("PIPE_TOKEN\n"); - } - else if (token->type == NEWLINE_TOKEN) - { - printf("NEWLINE_TOKEN\n"); + if (token->type == STRING_TOKEN) + { + printf("STRING_TOKEN: %s\n", token->content.string); + } + else if (token->type == REDIR_TOKEN) + { + printf("REDIR_TOKEN: %d\n", token->content.redir_type); + } + else if (token->type == PIPE_TOKEN) + { + printf("PIPE_TOKEN\n"); + } + else if (token->type == NEWLINE_TOKEN) + { + printf("NEWLINE_TOKEN\n"); + } } } -void conditional_print(char *string, int start_of_string, int i, t_token **token_list) +void conditional_print(char *string, int start_of_string, int i, + t_token **token_list) { char *line; int len; - len = i - start_of_string; + len = i - start_of_string + 1; if (len > 0) { line = (char *)malloc(len + 1); @@ -60,7 +64,7 @@ void conditional_print(char *string, int start_of_string, int i, t_token **token void handle_special_chars(char *s, int *i, int *start, t_token **token_list) { - conditional_print(s, *start, *i, token_list); // Pass correct boundaries + conditional_print(s, *start, *i - 1, token_list); if (s[*i] == '<' && s[*i + 1] == '<') *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); else if (s[*i] == '>' && s[*i + 1] == '>') @@ -86,9 +90,9 @@ void tokenizer(char *s, t_token **token_list) int i; int f; - f = 0; - i = -1; pos = 0; + i = -1; + f = 0; while (s[++i]) { if (!f && ft_strchr("|<>\\n", s[i])) @@ -100,7 +104,7 @@ void tokenizer(char *s, t_token **token_list) f = 1; quote_check = s[i]; } - if ((!f && (s[i] == ' ' || s[i] == '\t')) || s[i + 1] == '\0') + if ((!f && (s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) { conditional_print(s, pos, i, token_list); pos = i + 1; @@ -108,6 +112,7 @@ void tokenizer(char *s, t_token **token_list) } } + // Minishell $ echo "Hello World"|grep 'Hello|cat -e // STRING_TOKEN: echo // STRING_TOKEN: "Hello World" -- cgit v1.2.3 From 665e9eb6fe20730b2f98cbc0f6617985204accc7 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 9 Aug 2024 13:00:47 +0200 Subject: test version with a bunch of fixes --- src/tokenizer.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index f8ab9c4..672b6dc 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/09 12:37:08 by chuhlig ### ########.fr */ +/* Updated: 2024/08/09 12:59:03 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,7 +78,7 @@ void handle_special_chars(char *s, int *i, int *start, t_token **token_list) else if (s[*i] == '\n') *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); print_token(*token_list); - if (s[*i] == '<' || s[*i] == '>') + if (s[*i + 1] == '<' || s[*i + 1] == '>') (*i)++; *start = *i + 1; } @@ -95,7 +95,7 @@ void tokenizer(char *s, t_token **token_list) f = 0; while (s[++i]) { - if (!f && ft_strchr("|<>\\n", s[i])) + if (!f && ft_strchr("|<>\n", s[i])) handle_special_chars(s, &i, &pos, token_list); else if (f && s[i] == quote_check) f = 0; -- cgit v1.2.3 From 88030338953372eb0d223cf27fd5c96063ac7ee2 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 9 Aug 2024 15:28:03 +0200 Subject: fixed norm errors that i saw --- lib/libft/ft_strncpy.c | 3 +-- out.txt~ | 1 + src/repl.c | 6 ++--- src/tokenizer.c | 68 +------------------------------------------------- 4 files changed, 6 insertions(+), 72 deletions(-) create mode 100644 out.txt~ (limited to 'src/tokenizer.c') diff --git a/lib/libft/ft_strncpy.c b/lib/libft/ft_strncpy.c index 9d772cb..a1a2293 100644 --- a/lib/libft/ft_strncpy.c +++ b/lib/libft/ft_strncpy.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/05 13:41:47 by chuhlig #+# #+# */ -/* Updated: 2024/08/05 14:22:26 by chuhlig ### ########.fr */ +/* Updated: 2024/08/09 15:26:40 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,5 @@ char *ft_strncpy(char *s1, char *s2, int n) i = -1; while (++i < n && s2[i]) s1[i] = s2[i]; - // s1[i] = '\0'; return (s1); } diff --git a/out.txt~ b/out.txt~ new file mode 100644 index 0000000..73b4a2f --- /dev/null +++ b/out.txt~ @@ -0,0 +1 @@ +Hello World jdksan dsajklasdj dasjkldsajkl dasjkldsajkladsjkl dasjkl diff --git a/src/repl.c b/src/repl.c index e1ca7a6..1fd7be7 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2024/08/05 20:20:02 by chuhlig ### ########.fr */ +/* Updated: 2024/08/09 15:27:11 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,8 @@ void repl(const char *prompt) { char *input; t_token *token_list; - t_token *current; - t_token *next; + t_token *current; + t_token *next; while (1) { diff --git a/src/tokenizer.c b/src/tokenizer.c index 672b6dc..267068a 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/09 12:59:03 by chuhlig ### ########.fr */ +/* Updated: 2024/08/09 15:27:31 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -111,69 +111,3 @@ void tokenizer(char *s, t_token **token_list) } } } - - -// Minishell $ echo "Hello World"|grep 'Hello|cat -e -// STRING_TOKEN: echo -// STRING_TOKEN: "Hello World" -// PIPE_TOKEN -// STRING_TOKEN: grep -// STRING_TOKEN: 'Hello|cat -e -// Minishell $ - -// Minishell $ Minishell $ echo "Hello World"|grep 'Hello|cat -e -// STRING_TOKEN: echo -// STRING_TOKEN: "Hello World" -// PIPE_TOKEN -// STRING_TOKEN: grep -// STRING_TOKEN: 'Hello|cat -e -// Minishell $ -// STRING_TOKEN: Mi -// STRING_TOKEN: Mi -// STRING_TOKEN: ishell -// STRING_TOKEN: $ -// STRING_TOKEN: echo -// STRING_TOKEN: "Hello World" -// PIPE_TOKEN -// STRING_TOKEN: grep -// STRING_TOKEN: 'Hello|cat -e -// STRING_TOKEN: echo -// STRING_TOKEN: "Hello World" -// PIPE_TOKEN -// STRING_TOKEN: grep -// STRING_TOKEN: 'Hello -// PIPE_TOKEN -// STRING_TOKEN: cat -// STRING_TOKEN: -e -// Mi -// STRING_TOKEN: -e -// Mi -// STRING_TOKEN: ishell -// STRING_TOKEN: $ -// Minishell $ echo "Hello World"|grep 'Hello|cat -e -// STRING_TOKEN: echo -// STRING_TOKEN: "Hello World" -// PIPE_TOKEN -// STRING_TOKEN: grep -// STRING_TOKEN: 'Hello|cat -e -// Minishell $ echo "Hello World"|grep 'Hello|cat -e -// STRING_TOKEN: echo -// STRING_TOKEN: "Hello World" -// PIPE_TOKEN -// STRING_TOKEN: grep -// STRING_TOKEN: 'Hello|cat -e -// Minishell $ echo "Hello World"|grep 'Hello|cat -e -// STRING_TOKEN: echo -// STRING_TOKEN: "Hello World" -// PIPE_TOKEN -// STRING_TOKEN: grep -// STRING_TOKEN: 'Hello|cat -e -// Minishell $ echo "Hello World"|grep 'Hello'|cat -e -// STRING_TOKEN: echo -// STRING_TOKEN: "Hello World" -// PIPE_TOKEN -// STRING_TOKEN: grep -// STRING_TOKEN: 'Hello' -// PIPE_TOKEN -// STRING_TOKEN: cat -// STRING_TOKEN: -e \ No newline at end of file -- cgit v1.2.3 From 645f4455d403e7f25386be25256718e9597161ca Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 9 Aug 2024 15:40:42 +0200 Subject: removed normitte errors in tokenizer --- src/tokenizer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/tokenizer.c') diff --git a/src/tokenizer.c b/src/tokenizer.c index 267068a..34685ac 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/09 15:27:31 by chuhlig ### ########.fr */ +/* Updated: 2024/08/09 15:40:00 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ -- cgit v1.2.3