]> git.dkaiser.de - 42/minishell.git/commitdiff
made it norm comform exept one funkction to much
authorChristopher Uhlig <chuhlig@2-B-6.42heilbronn.de>
Sun, 4 Aug 2024 12:36:51 +0000 (14:36 +0200)
committerChristopher Uhlig <chuhlig@2-B-6.42heilbronn.de>
Sun, 4 Aug 2024 12:36:51 +0000 (14:36 +0200)
src/tokenizer.c

index ddc33da0390d8f1b29ac4b90ed3cb4c34c684a09..4059526d99680d0653378b10c1b0754bb9d02aac 100644 (file)
@@ -6,31 +6,20 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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