diff options
| author | Dominik Kaiser | 2024-08-11 11:18:26 +0200 |
|---|---|---|
| committer | Dominik Kaiser | 2024-08-11 11:18:26 +0200 |
| commit | 8cf2aec82f98094d8175c5e5daaaa115f9b64fc2 (patch) | |
| tree | 9c06dfc42031efc26f1fdfeec4bca8c975c4199c /src | |
| parent | c7a4494fd97b7e80665cbd47ed96bc37bf5800e5 (diff) | |
| parent | 8abf7abdc9d49f52c4fcad0afc92730c4c195b7f (diff) | |
| download | minishell-8cf2aec82f98094d8175c5e5daaaa115f9b64fc2.tar.gz minishell-8cf2aec82f98094d8175c5e5daaaa115f9b64fc2.zip | |
Merge changes from main into parser branch
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 6 | ||||
| -rw-r--r-- | src/repl.c | 19 | ||||
| -rw-r--r-- | src/tokenizer.c | 113 |
3 files changed, 132 insertions, 6 deletions
@@ -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; + } + } +} |
