aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--include/env.h23
-rw-r--r--include/minishell.h3
-rw-r--r--include/token.h5
-rw-r--r--lib/libft/Makefile1
-rw-r--r--lib/libft/ft_memset.c14
-rw-r--r--lib/libft/ft_strncpy.c21
-rw-r--r--lib/libft/libft.h5
-rw-r--r--src/main.c6
-rw-r--r--src/repl.c19
-rw-r--r--src/tokenizer.c113
11 files changed, 199 insertions, 15 deletions
diff --git a/Makefile b/Makefile
index a56ada3..3450e3c 100644
--- a/Makefile
+++ b/Makefile
@@ -12,8 +12,8 @@ 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 parser.c parse_cmd.c \
- print_ast.c
+ free_token.c new_node.c free_node.c tokenizer.c parser.c \
+ parse_cmd.c print_ast.c
OBJ_DIR := _obj
OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
diff --git a/include/env.h b/include/env.h
new file mode 100644
index 0000000..1ea6f2e
--- /dev/null
+++ b/include/env.h
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* env.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */
+/* Updated: 2024/08/08 17:05:11 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+typedef struct s_env {
+ char *name;
+ char *value;
+ struct s_env *next;
+} t_env;
+
+char *env_get(t_env *env, char *name);
+void env_export(t_env *env, char *name, char *value);
+void env_unset(t_env *env, char *name);
+char **env_to_strlst(t_env *env);
+t_env **env_from_strlst(char **lst);
diff --git a/include/minishell.h b/include/minishell.h
index b21acbc..4f04682 100644
--- a/include/minishell.h
+++ b/include/minishell.h
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */
-/* Updated: 2024/08/02 13:55:48 by dkaiser ### ########.fr */
+/* Updated: 2024/08/11 10:59:16 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,6 +16,7 @@
# include "debug_tools.h"
# include "ast.h"
# include "token.h"
+# include "env.h"
# include "libft.h"
# include <stdio.h>
# include <readline/readline.h>
diff --git a/include/token.h b/include/token.h
index 72ca5e5..49d51e1 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/08/02 14:13:19 by dkaiser ### ########.fr */
+/* Updated: 2024/08/11 11:00:22 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -47,5 +47,6 @@ t_token *new_redir_token(int type, t_token *previous,
void free_token(t_token *token);
void free_token_and_connect(t_token *token);
void free_tokens(t_token *tokens);
+void tokenizer(char *s, t_token **token_list);
#endif
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
{
diff --git a/src/main.c b/src/main.c
index 016ede6..8523b9e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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)
{
diff --git a/src/repl.c b/src/repl.c
index 85d227f..1fd7be7 100644
--- a/src/repl.c
+++ b/src/repl.c
@@ -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;
+ }
+ }
+}