/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:22:57 by ljiriste #+# #+# */
-/* Updated: 2024/08/31 18:41:22 by ljiriste ### ########.fr */
+/* Updated: 2024/09/01 09:13:19 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
# include "libft.h"
# include <signal.h>
-enum token_types
-{
- WORD,
- ASSIGNMENT_WORD,
- IO_NUMBER,
- AND_IF,
- OR_IF,
- LESS,
- GREAT,
- DLESS,
- DGREAT,
- PIPE,
- LPARA,
- RPARA,
-};
-
extern volatile sig_atomic_t g_last_signal;
void handler(int sig_num);
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */
-/* Updated: 2024/08/31 18:41:46 by ljiriste ### ########.fr */
+/* Updated: 2024/09/01 09:18:49 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
+enum token_type
+{
+ WORD,
+ ASSIGNMENT_WORD,
+ IO_NUMBER,
+ AND_IF,
+ OR_IF,
+ LESS,
+ GREAT,
+ DLESS,
+ DGREAT,
+ PIPE,
+ LPARA,
+ RPARA,
+};
+
typedef struct s_vars
{
t_vec exported;
char *get_var_name(const char *line);
int add_var_line(t_vec *vec, const char *line);
+const char *type_enum_to_str(enum token_type type);
+
void free_str(void *str);
void free_token(void *token);
void clean_vars(t_vars *vars);
+++ /dev/null
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* tokens.h :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2024/08/31 15:41:11 by lnikolov #+# #+# */
-/* Updated: 2024/08/31 15:42:19 by lnikolov ### ########.fr */
-/* */
-/* ************************************************************************** */
-
-#ifndef TOKENS_H
-# define TOKENS_H
-
-static const char *g_tokens[12] = {
- "WORD",
- "ASSIGNMENT_WORD",
- "IO_NUMBER",
- "AND_IF",
- "OR_IF",
- "LESS",
- "GREAT",
- "DLESS",
- "DGREAT",
- "PIPE",
- "LPARA",
- "RPARA"};
-
-#endif
\ No newline at end of file
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 15:12:50 by lnikolov #+# #+# */
-/* Updated: 2024/08/31 16:26:41 by lnikolov ### ########.fr */
+/* Updated: 2024/09/01 09:44:20 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
-#include "tokens.h"
+#include "minishell_structs.h"
#include "minishell.h"
#include <stdlib.h>
+const char *type_enum_to_str(enum token_type type)
+{
+ static const char *tokens[12] = {
+ "WORD",
+ "ASSIGNMENT_WORD",
+ "IO_NUMBER",
+ "AND_IF",
+ "OR_IF",
+ "LESS",
+ "GREAT",
+ "DLESS",
+ "DGREAT",
+ "PIPE",
+ "LPARA",
+ "RPARA"};
+
+ return (tokens[type]);
+}
+
static int only_contains_digits(const char *str)
{
while (ft_isdigit(*str))
static const char *get_token_type(const char *str, char next)
{
if (!ft_strcmp(str, "&&"))
- return (g_tokens[AND_IF]);
+ return (type_enum_to_str(AND_IF));
if (!ft_strcmp(str, "||"))
- return (g_tokens[OR_IF]);
+ return (type_enum_to_str(OR_IF));
if (!ft_strcmp(str, "<"))
- return (g_tokens[LESS]);
+ return (type_enum_to_str(LESS));
if (!ft_strcmp(str, ">"))
- return (g_tokens[GREAT]);
+ return (type_enum_to_str(GREAT));
if (!ft_strcmp(str, "<<"))
- return (g_tokens[DLESS]);
+ return (type_enum_to_str(DLESS));
if (!ft_strcmp(str, ">>"))
- return (g_tokens[DGREAT]);
+ return (type_enum_to_str(DGREAT));
if (!ft_strcmp(str, "|"))
- return (g_tokens[PIPE]);
+ return (type_enum_to_str(PIPE));
if (!ft_strcmp(str, "("))
- return (g_tokens[LPARA]);
+ return (type_enum_to_str(LPARA));
if (!ft_strcmp(str, ")"))
- return (g_tokens[RPARA]);
+ return (type_enum_to_str(RPARA));
if (is_assignment_word(str))
- return (g_tokens[ASSIGNMENT_WORD]);
+ return (type_enum_to_str(ASSIGNMENT_WORD));
if (only_contains_digits(str) && (next == '<' || next == '>'))
- return (g_tokens[IO_NUMBER]);
- return (g_tokens[WORD]);
+ return (type_enum_to_str(IO_NUMBER));
+ return (type_enum_to_str(WORD));
}
// This function turns the input char string into a string of tokens
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 15:08:00 by lnikolov #+# #+# */
-/* Updated: 2024/08/31 18:56:53 by ljiriste ### ########.fr */
+/* Updated: 2024/09/01 09:19:08 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
-#include "tokens.h"
+#include "minishell_structs.h"
#include "minishell.h"
#include <stdlib.h>
-static int is_redirection_operator(const t_token *token)
+static int is_redirection_operator(const char *type)
{
- return (token->type == g_tokens[LESS]
- || token->type == g_tokens[DLESS]
- || token->type == g_tokens[GREAT]
- || token->type == g_tokens[DGREAT]);
+ return (type == type_enum_to_str(LESS)
+ || type == type_enum_to_str(DLESS)
+ || type == type_enum_to_str(GREAT)
+ || type == type_enum_to_str(DGREAT));
}
static int assignment_may_follow(const char *type)
{
- return (type == g_tokens[ASSIGNMENT_WORD]
- || type == g_tokens[AND_IF]
- || type == g_tokens[OR_IF]
- || type == g_tokens[LPARA]);
+ return (type == type_enum_to_str(ASSIGNMENT_WORD)
+ || type == type_enum_to_str(AND_IF)
+ || type == type_enum_to_str(OR_IF)
+ || type == type_enum_to_str(LPARA));
}
static void filter_assignment_word(t_vec *tokens)
{
++i;
token = ft_vec_access(tokens, i);
- if (i == 0 || token->type != g_tokens[ASSIGNMENT_WORD])
+ if (i == 0 || token->type != type_enum_to_str(ASSIGNMENT_WORD))
continue ;
prev_token = ft_vec_caccess(tokens, i - 1);
if (assignment_may_follow(prev_token->type))
continue ;
- if (i == 1 || prev_token->type != g_tokens[WORD])
+ if (i == 1 || prev_token->type != type_enum_to_str(WORD))
{
- token->type = (char *)g_tokens[WORD];
+ token->type = (char *)type_enum_to_str(WORD);
continue ;
}
prev_token = ft_vec_caccess(tokens, i - 2);
- if (!is_redirection_operator(prev_token))
- token->type = (char *)g_tokens[WORD];
+ if (!is_redirection_operator(prev_token->type))
+ token->type = (char *)type_enum_to_str(WORD);
}
}