diff options
| author | cuhlig | 2024-08-09 16:26:37 +0200 |
|---|---|---|
| committer | GitHub | 2024-08-09 16:26:37 +0200 |
| commit | 8abf7abdc9d49f52c4fcad0afc92730c4c195b7f (patch) | |
| tree | 2255b668d61572fac185c3d6b5ddf7830a675371 | |
| parent | 36d2b4da2887419705cd22eb97a6283be86816f4 (diff) | |
| parent | e197ecae60b005b0cfe7270f243740ae967bcb7d (diff) | |
| download | minishell-8abf7abdc9d49f52c4fcad0afc92730c4c195b7f.tar.gz minishell-8abf7abdc9d49f52c4fcad0afc92730c4c195b7f.zip | |
Merge pull request #12 from dpu-kaiser/tokenizer
Tokenizer
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | include/token.h | 7 | ||||
| -rw-r--r-- | lib/libft/Makefile | 1 | ||||
| -rw-r--r-- | lib/libft/ft_memset.c | 14 | ||||
| -rw-r--r-- | lib/libft/ft_strncpy.c | 21 | ||||
| -rw-r--r-- | lib/libft/libft.h | 5 | ||||
| -rw-r--r-- | src/main.c | 6 | ||||
| -rw-r--r-- | src/repl.c | 19 | ||||
| -rw-r--r-- | src/tokenizer.c | 113 |
9 files changed, 174 insertions, 14 deletions
@@ -12,7 +12,7 @@ HEADERS = -I include -I $(LIB_DIR)/libft VPATH := src SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ - free_token.c new_node.c free_node.c + free_token.c new_node.c free_node.c tokenizer.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/token.h b/include/token.h index 38e758f..d7ff9f9 100644 --- a/include/token.h +++ b/include/token.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* token.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 13:27:18 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 14:59:19 by dkaiser ### ########.fr */ +/* Updated: 2024/08/05 13:23:27 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,5 +45,6 @@ t_token *new_redir_token(int type, t_token *previous, t_token *next); void free_token(t_token *token); +void tokenizer(char *s, t_token **token_list); -#endif +#endif
\ No newline at end of file diff --git a/lib/libft/Makefile b/lib/libft/Makefile index 3c2fb91..6951c43 100644 --- a/lib/libft/Makefile +++ b/lib/libft/Makefile @@ -30,6 +30,7 @@ SRC = ft_atoi.c \ ft_strlen.c \ ft_strmapi.c \ ft_strncmp.c \ + ft_strncpy.c \ ft_strnstr.c \ ft_strrchr.c \ ft_strtrim.c \ diff --git a/lib/libft/ft_memset.c b/lib/libft/ft_memset.c index 58c5e1e..085df1d 100644 --- a/lib/libft/ft_memset.c +++ b/lib/libft/ft_memset.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* ft_memset.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 09:58:19 by dkaiser #+# #+# */ -/* Updated: 2024/03/10 13:13:09 by dkaiser ### ########.fr */ +/* Updated: 2024/07/11 23:52:13 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,3 +35,13 @@ void *ft_memset(void *b, int c, size_t len) /* printf("ft_memset: %s\n", ft_memset((char *)str, 'A' + 255, 5)); */ /* printf("memset: %s\n", memset((char *)str, 'A' + 255, 5)); */ /* } */ + +// void *ft_memset(void *b, int c, size_t len) +// { +// void *savearg; + +// savearg = b; +// while (len--) +// *(unsigned char *)b++ = (unsigned char)c; +// return (savearg); +// }
\ No newline at end of file diff --git a/lib/libft/ft_strncpy.c b/lib/libft/ft_strncpy.c new file mode 100644 index 0000000..a1a2293 --- /dev/null +++ b/lib/libft/ft_strncpy.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/05 13:41:47 by chuhlig #+# #+# */ +/* Updated: 2024/08/09 15:26:40 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +char *ft_strncpy(char *s1, char *s2, int n) +{ + int i; + + i = -1; + while (++i < n && s2[i]) + s1[i] = s2[i]; + return (s1); +} diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 2de723b..67fbb0f 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* libft.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/10 16:37:54 by dkaiser #+# #+# */ -/* Updated: 2024/06/24 16:44:44 by dkaiser ### ########.fr */ +/* Updated: 2024/08/05 13:55:13 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,6 +57,7 @@ void ft_putchar_fd(char c, int fd); void ft_putstr_fd(char *s, int fd); void ft_putendl_fd(char *s, int fd); void ft_putnbr_fd(int n, int fd); +char *ft_strncpy(char *s1, char *s2, int n); typedef struct s_list { @@ -3,14 +3,14 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2024/06/25 13:58:24 by dkaiser ### ########.fr */ +/* Updated: 2024/07/18 16:44:14 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "../include/minishell.h" int main(void) { @@ -3,18 +3,22 @@ /* ::: :::::::: */ /* repl.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2024/06/25 15:03:00 by dkaiser ### ########.fr */ +/* Updated: 2024/08/09 15:27:11 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "../include/minishell.h" +#include "token.h" void repl(const char *prompt) { char *input; + t_token *token_list; + t_token *current; + t_token *next; while (1) { @@ -22,6 +26,15 @@ void repl(const char *prompt) if (input == NULL) return ; add_history(input); + token_list = NULL; + tokenizer(input, &token_list); + current = token_list; + while (current != NULL) + { + next = current->next; + free_token(current); + current = next; + } free(input); } } diff --git a/src/tokenizer.c b/src/tokenizer.c new file mode 100644 index 0000000..34685ac --- /dev/null +++ b/src/tokenizer.c @@ -0,0 +1,113 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* tokenizer.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ +/* Updated: 2024/08/09 15:40:00 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include "token.h" + +void print_token(t_token *token) +{ + if (DEBUG) + { + 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) +{ + char *line; + int len; + + len = i - start_of_string + 1; + if (len > 0) + { + line = (char *)malloc(len + 1); + if (!line) + { + exit(EXIT_FAILURE); + } + ft_strncpy(line, string + start_of_string, len); + line[len] = '\0'; + while (*line == ' ' || *line == '\t') + line++; + if (*line != '\0') + { + *token_list = new_str_token(line, *token_list, NULL); + print_token(*token_list); + } + } +} + +void handle_special_chars(char *s, int *i, int *start, t_token **token_list) +{ + 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] == '>') + *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 + 1] == '<' || s[*i + 1] == '>') + (*i)++; + *start = *i + 1; +} + +void tokenizer(char *s, t_token **token_list) +{ + char quote_check; + int pos; + int i; + int f; + + pos = 0; + i = -1; + f = 0; + while (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]; + } + if ((!f && (s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) + { + conditional_print(s, pos, i, token_list); + pos = i + 1; + } + } +} |
