maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
tokens.h File Reference
#include "../lib/libft/inc/libft.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <pwd.h>
#include "shell.h"
#include "env.h"
Include dependency graph for tokens.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  s_token
 
struct  s_ast
 
struct  s_loop_data
 

Macros

#define HISTORY_PATH   "./src/utils/.maxishell_history"
 

Typedefs

typedef enum e_token_type t_token_type
 
typedef struct s_token t_token
 
typedef struct s_ast t_ast
 
typedef struct s_loop_data t_loop_data
 

Enumerations

enum  e_token_type {
  PHRASE , PIPE , ENV_VAR , REDIR_IN ,
  REDIR_OUT , REDIR_APPEND , REDIR_HEREDOC , NONE
}
 

Functions

void final_quote_removal (int arg_count, t_ast *command_node)
 
char * expand_env_var (char *arg, t_ms_data *data)
 
void post_process_command_args (t_ast *command_node, int arg_count, t_ms_data *data)
 
void add_node (t_token **head, char *str)
 
void print_stack (t_token **stack)
 
void free_stack (t_token **stack)
 
int calc_stack_size (t_token *stack)
 
char ** list_to_array (t_token *head)
 
void build_linked_list (t_token **tokens, char **argv)
 
void handle_quotes (char **tokens, int *pos, char **input)
 
char * handle_special_chars (char *str, t_token **tokens)
 
void skip_delimiters (char **input, char *delim)
 
void reallocate_tokens (char ***tokens, int *bufsize)
 
void parse_loop (char **input, char **tokens, int *pos, int *bufsize)
 
char ** parse_input (char *input)
 
char * generate_prompt (t_ms_data *data)
 
void make_history (char *line)
 
void loop_cleanup (t_loop_data *loop_data, t_token *tokens_head)
 
void free_ms_data (t_ms_data *data)
 
char * check_heredoc (char *line)
 
char * heredoc (char *eof)
 
void init_ms_data (t_ms_data *data, char **argv, char **envp)
 
void initialise (int argc, char **argv)
 
void execute_command (char **parsed_text, t_token **tokens)
 
void print_maxishell (void)
 
int input_error_checks (t_loop_data *loop_data)
 
t_tokentokenise (char *str)
 
void print_tokens (t_token *tokens, char *name)
 
t_astparse_tokens (t_token **tokens, t_ms_data *data)
 
void visualize_ast (t_ast *root)
 
void free_ast (t_ast *node)
 
void free_all_tokens (t_token *tokens)
 
t_tokennew_token (char *value, t_token_type type)
 
void append_token (t_token **tokens, t_token *new_token)
 
int valid_operator (const char **str)
 
t_astnew_ast_node (void)
 
t_astcreate_redir (t_token **tokens, t_token *tmp, t_ms_data *data)
 
int arg_len (t_token *current)
 
void set_command_args (t_ast *command_node, t_token **tokens, int arg_count)
 
t_astmanage_commands (t_token **tokens, t_ms_data *data)
 
void handle_local_vars (t_ms_data *data, char *arg)
 
t_astcreate_redir_node (t_token *token)
 
int is_redir_node (t_token *tokens)
 
t_astmanage_redirs (t_token **tokens, t_ms_data *data)
 
t_astmanage_pipe (t_token **tokens, t_ms_data *data)
 
char * trim_input (char *str)
 
void print_ast_root (t_ast *root)
 
void execute_tree (t_ast *node, t_ms_data *data)
 
char * expand_env_and_loc_var (char *arg, t_ms_data *data)
 
char * append_literal (char **start, char *processed_arg)
 
char * process_argument (char *arg, t_ms_data *data)
 
char * expand_variable (char **start, t_ms_data *data)
 
void clear_history_file (void)
 
int is_in_single_quotes (char *arg)
 
void print_ast_args (t_ast *node)
 
void append_word_if_valid (char *start, char *str, t_token **tokens)
 
char * exit_status_adj (char *arg)
 
char * str_start_adj (char *arg)
 
char * tmp_adj (char *arg)
 
int cmd_arg_len (t_token *current)
 

Macro Definition Documentation

◆ HISTORY_PATH

#define HISTORY_PATH   "./src/utils/.maxishell_history"

Definition at line 27 of file tokens.h.

Typedef Documentation

◆ t_ast

typedef struct s_ast t_ast

◆ t_loop_data

typedef struct s_loop_data t_loop_data

◆ t_token

typedef struct s_token t_token

◆ t_token_type

typedef enum e_token_type t_token_type

Enumeration Type Documentation

◆ e_token_type

Enumerator
PHRASE 
PIPE 
ENV_VAR 
REDIR_IN 
REDIR_OUT 
REDIR_APPEND 
REDIR_HEREDOC 
NONE 

Definition at line 29 of file tokens.h.

30{
31 PHRASE,
32 PIPE,
33 ENV_VAR,
38 NONE
@ REDIR_IN
Definition tokens.h:34
@ PHRASE
Definition tokens.h:31
@ REDIR_HEREDOC
Definition tokens.h:37
@ PIPE
Definition tokens.h:32
@ NONE
Definition tokens.h:38
@ REDIR_OUT
Definition tokens.h:35
@ REDIR_APPEND
Definition tokens.h:36
@ ENV_VAR
Definition tokens.h:33
enum e_token_type t_token_type

Function Documentation

◆ add_node()

void add_node ( t_token **  head,
char *  str 
)

Definition at line 15 of file stack_control.c.

16{
17 t_token *new_node;
18 t_token *curr_node;
19
20 new_node = (t_token *)malloc(sizeof(t_token));
21 if (!new_node)
22 return ;
23 new_node->data = str;
24 new_node->next = NULL;
25 if (*head == NULL)
26 {
27 *head = new_node;
28 return ;
29 }
30 curr_node = *head;
31 while (curr_node->next != NULL)
32 curr_node = curr_node->next;
33 curr_node->next = new_node;
34}
struct s_token * next
Definition tokens.h:45
char * data
Definition tokens.h:44

References s_token::data, and s_token::next.

Referenced by build_linked_list().

Here is the caller graph for this function:

◆ append_literal()

char * append_literal ( char **  start,
char *  processed_arg 
)

Definition at line 74 of file loc_env_var_handler_utils_utils_utils.c.

75{
76 char *literal_part;
77 char *literal_start;
78 char *tmp;
79
80 literal_start = *start;
81 while (**start != '\0' && **start != '$')
82 (*start)++;
83 literal_part = ft_substr(literal_start, 0, *start - literal_start);
84 tmp = ft_strjoin(processed_arg, literal_part);
85 free(literal_part);
86 free(processed_arg);
87 return (tmp);
88}
char * ft_substr(char const *s, unsigned int start, size_t len)
Definition ft_substr.c:27
char * ft_strjoin(char const *s1, char const *s2)
Definition ft_strjoin.c:23

References ft_strjoin(), and ft_substr().

Referenced by process_argument().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ append_token()

void append_token ( t_token **  tokens,
t_token new_token 
)

Definition at line 85 of file tokeniser.c.

86{
87 t_token *curr;
88
89 if (!*tokens)
90 *tokens = new_token;
91 else
92 {
93 curr = *tokens;
94 while (curr->next)
95 curr = curr->next;
96 curr->next = new_token;
97 }
98}
t_token * new_token(char *value, t_token_type type)
Definition tokeniser.c:67

References new_token(), and s_token::next.

Referenced by append_word_if_valid(), and handle_special_chars().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ append_word_if_valid()

void append_word_if_valid ( char *  start,
char *  str,
t_token **  tokens 
)

Definition at line 49 of file tokeniser_helpers.c.

50{
51 char *word;
52
53 if (str > start)
54 {
55 word = ft_strndup(start, str - start);
56 if (word)
57 {
58 append_token(tokens, new_token(word, PHRASE));
59 free(word);
60 }
61 else
62 ft_printf("Error: unable to allocate memory for token\n");
63 }
64}
int ft_printf(const char *format,...)
Definition ft_printf.c:37
char * ft_strndup(const char *s, size_t n)
Definition ft_strndup.c:23
t_token * new_token(char *value, t_token_type type)
Definition tokeniser.c:67
void append_token(t_token **tokens, t_token *new_token)
Definition tokeniser.c:85

References append_token(), ft_printf(), ft_strndup(), new_token(), and PHRASE.

Referenced by handle_phrase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ arg_len()

int arg_len ( t_token current)

◆ build_linked_list()

void build_linked_list ( t_token **  tokens,
char **  argv 
)

Definition at line 15 of file stack_control_2.c.

16{
17 int i;
18
19 i = 0;
20 while (argv[i])
21 {
22 add_node(tokens, argv[i]);
23 i++;
24 }
25}
void add_node(t_token **head, char *str)

References add_node().

Here is the call graph for this function:

◆ calc_stack_size()

int calc_stack_size ( t_token stack)

Definition at line 65 of file stack_control.c.

66{
67 int size;
68 t_token *current;
69
70 size = 0;
71 current = stack;
72 while (current != NULL)
73 {
74 size++;
75 current = current->next;
76 }
77 return (size);
78}

References s_token::next.

Referenced by list_to_array().

Here is the caller graph for this function:

◆ check_heredoc()

char * check_heredoc ( char *  line)

◆ clear_history_file()

void clear_history_file ( void  )

Definition at line 28 of file exit.c.

29{
30 remove(HISTORY_PATH);
31}
#define HISTORY_PATH
Definition tokens.h:27

References HISTORY_PATH.

Referenced by handle_exit(), main(), and main_loop().

Here is the caller graph for this function:

◆ cmd_arg_len()

int cmd_arg_len ( t_token current)

Definition at line 88 of file AST_utils.c.

89{
90 int i;
91
92 i = 0;
93 while (current && current->type == PHRASE)
94 {
95 i++;
96 current = current->next;
97 }
98 return (i);
99}
t_token_type type
Definition tokens.h:43

References s_token::next, PHRASE, and s_token::type.

Referenced by manage_commands().

Here is the caller graph for this function:

◆ create_redir()

t_ast * create_redir ( t_token **  tokens,
t_token tmp,
t_ms_data data 
)

◆ create_redir_node()

t_ast * create_redir_node ( t_token token)

Definition at line 62 of file AST.c.

63{
64 t_ast *node;
65
66 node = new_ast_node();
67 node->type = token->type;
68 node->args = malloc(sizeof(char *) * 2);
69 if (!node->args)
70 {
71 free(node);
72 return (NULL);
73 }
74 node->args[0] = ft_strdup(token->data);
75 node->args[1] = NULL;
76 return (node);
77}
t_ast * new_ast_node(void)
Definition AST.c:89
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
Definition tokens.h:50
char ** args
Definition tokens.h:52
t_token_type type
Definition tokens.h:51

References s_ast::args, s_token::data, ft_strdup(), new_ast_node(), s_token::type, and s_ast::type.

Referenced by manage_redirs().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute_command()

void execute_command ( char **  parsed_text,
t_token **  tokens 
)

◆ execute_tree()

void execute_tree ( t_ast node,
t_ms_data data 
)

◆ exit_status_adj()

char * exit_status_adj ( char *  arg)

Definition at line 21 of file loc_env_var_handler_utils_utils_utils.c.

22{
23 if (ft_strcmp(arg, "$") == 0)
24 return ("$?");
25 return (arg);
26}
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24

References ft_strcmp().

Referenced by expand_env_and_loc_var().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expand_env_and_loc_var()

char * expand_env_and_loc_var ( char *  arg,
t_ms_data data 
)

Definition at line 45 of file loc_env_var_handler_utils_utils.c.

46{
47 char *env_value_dup;
48
49 arg = exit_status_adj(arg);
50 if (ft_strcmp(arg, "$?") == 0)
51 return (get_exit_status(data));
52 else if (arg[0] == '$')
53 {
55 env_value_dup = get_env_variable(arg, data);
56 return (env_value_dup);
57 }
58 return (ft_strdup(""));
59}
char * get_env_variable(char *arg, t_ms_data *data)
void handle_trailing_quote(char *arg)
char * get_exit_status(t_ms_data *data)
char * exit_status_adj(char *arg)

References exit_status_adj(), ft_strcmp(), ft_strdup(), get_env_variable(), get_exit_status(), and handle_trailing_quote().

Referenced by ev_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expand_env_var()

char * expand_env_var ( char *  arg,
t_ms_data data 
)

◆ expand_variable()

char * expand_variable ( char **  start,
t_ms_data data 
)

Definition at line 40 of file loc_env_var_handler_utils.c.

41{
42 char *expanded_str;
43 char *str_start;
44 char *result;
45
46 result = NULL;
47 str_start = *start;
48 expanded_str = ft_strdup("");
49 while (**start && **start != '\0')
50 {
51 if (**start == '$')
52 {
53 expanded_str = ev_loop(start, data, str_start, expanded_str);
54 str_start = *start;
55 str_start = str_start_adj(str_start);
56 }
57 else
58 (*start)++;
59 }
60 if (str_start != *start)
61 {
62 result = ft_substr(str_start, 0, *start - str_start);
63 expanded_str = ft_strjoin_free(expanded_str, result);
64 free(result);
65 }
66 return (expanded_str);
67}
char * ft_strjoin_free(char *s1, char *s2)
char * ev_loop(char **start, t_ms_data *data, char *str_start, char *expanded_str)
char * str_start_adj(char *arg)

References ev_loop(), ft_strdup(), ft_strjoin_free(), ft_substr(), and str_start_adj().

Referenced by process_argument(), and process_single_token().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ final_quote_removal()

void final_quote_removal ( int  arg_count,
t_ast command_node 
)

Definition at line 21 of file loc_env_var_handler_utils_utils.c.

22{
23 int i;
24 size_t len;
25 char *arg;
26 char *trimmed_arg;
27
28 i = 0;
29 ft_print_2d_arr(command_node->args, "final-quote_removal");
30 while (i < arg_count)
31 {
32 arg = command_node->args[i];
33 len = ft_strlen(arg);
34 if ((arg[0] == '"' && arg[len - 1] == '"') || (arg[0] == '\''
35 && arg[len - 1] == '\''))
36 {
37 trimmed_arg = ft_strndup(arg + 1, len - 2);
38 free(command_node->args[i]);
39 command_node->args[i] = trimmed_arg;
40 }
41 i++;
42 }
43}
void ft_print_2d_arr(char **arr, char *name)
size_t ft_strlen(const char *s)
Definition ft_strlen.c:15

References s_ast::args, ft_print_2d_arr(), ft_strlen(), and ft_strndup().

Here is the call graph for this function:

◆ free_all_tokens()

void free_all_tokens ( t_token tokens)

Definition at line 17 of file clean_utils.c.

18{
19 t_token *temp;
20
21 while (tokens)
22 {
23 temp = tokens;
24 tokens = tokens->next;
25 if (temp)
26 {
27 if (temp->data)
28 {
29 free(temp->data);
30 temp->data = NULL;
31 }
32 }
33 free(temp);
34 temp = NULL;
35 }
36}

References s_token::data, and s_token::next.

◆ free_ast()

void free_ast ( t_ast node)

Definition at line 74 of file clean.c.

75{
76 int i;
77
78 i = 0;
79 if (!node)
80 return ;
81 if (node->args)
82 {
83 while (node->args && node->args[i])
84 {
85 free(node->args[i]);
86 i++;
87 }
88 free(node->args);
89 }
90 free_ast(node->left);
91 free_ast(node->right);
92 free(node);
93}
void free_ast(t_ast *node)
Definition clean.c:74
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53

References s_ast::args, free_ast(), s_ast::left, and s_ast::right.

Referenced by free_ast(), and loop_cleanup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_ms_data()

void free_ms_data ( t_ms_data data)

Definition at line 51 of file clean.c.

52{
53 if (data)
54 {
57 free(data->current_dir);
58 if (data->std_in >= 0)
59 close(data->std_in);
60 if (data->std_out >= 0)
61 close(data->std_out);
62 if (data->std_err >= 0)
63 close(data->std_err);
64 }
65}
void free_shell_var_list(t_env *shell_var)
t_env * envp
Definition shell.h:24
int std_err
Definition shell.h:28
int std_out
Definition shell.h:27
t_env * shell_variables
Definition shell.h:25
char * current_dir
Definition shell.h:29
int std_in
Definition shell.h:26

References s_ms_data::current_dir, s_ms_data::envp, free_shell_var_list(), s_ms_data::shell_variables, s_ms_data::std_err, s_ms_data::std_in, and s_ms_data::std_out.

Referenced by handle_exit(), and main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_stack()

void free_stack ( t_token **  stack)

Definition at line 51 of file stack_control.c.

52{
53 t_token *current;
54 t_token *next;
55
56 current = *stack;
57 while (current != NULL)
58 {
59 next = current->next;
60 free(current);
61 current = next;
62 }
63}

References s_token::next.

◆ generate_prompt()

char * generate_prompt ( t_ms_data data)

Definition at line 20 of file prompt.c.

21{
22 char *prompt;
23 size_t prompt_len;
24
25 prompt_len = ft_strlen("🌴 @maxishell$ ") + \
26 ft_strlen(getenv("LOGNAME"));
27 prompt = (char *)malloc(prompt_len + 1);
28 if (!prompt)
29 exit(EXIT_FAILURE);
30 ft_strcpy(prompt, "🌴 ");
31 ft_strcat(prompt, getenv("LOGNAME"));
32 ft_strcat(prompt, "@maxishell$ ");
33 (void)data;
34 return (prompt);
35}
#define EXIT_FAILURE
Definition exit_status.h:17
char * ft_strcpy(char *dest, const char *src)
Definition ft_strcpy.c:25
char * ft_strcat(char *dest, char *src)
Definition ft_strcat.c:26

References EXIT_FAILURE, ft_strcat(), ft_strcpy(), and ft_strlen().

Here is the call graph for this function:

◆ handle_local_vars()

void handle_local_vars ( t_ms_data data,
char *  arg 
)

Definition at line 69 of file loc_env_var_handler_utils.c.

70{
71 char *p;
72 int valid_var;
73
74 p = arg;
75 valid_var = 1;
76 if (arg[0] != '=' && ft_strchr(arg, '='))
77 {
78 while (p < ft_strchr(arg, '='))
79 {
80 if (!ft_isalnum(*p++))
81 {
82 valid_var = 0;
83 break ;
84 }
85 }
86 if (valid_var)
88 }
89}
int handle_add_set_shell_variable(t_env **shell_var, char *line)
int ft_isalnum(int c)
Definition ft_isalnum.c:15
char * ft_strchr(const char *s, int c)
Definition ft_strchr.c:25

References ft_isalnum(), ft_strchr(), handle_add_set_shell_variable(), and s_ms_data::shell_variables.

Here is the call graph for this function:

◆ handle_quotes()

void handle_quotes ( char **  tokens,
int *  pos,
char **  input 
)

◆ handle_special_chars()

char * handle_special_chars ( char *  str,
t_token **  tokens 
)

Definition at line 39 of file tokeniser.c.

40{
41 if (*str == '<')
42 {
43 if (*(str + 1) == '<')
44 {
45 append_token(tokens, new_token("<<", REDIR_HEREDOC));
46 (str)++;
47 }
48 else
49 append_token(tokens, new_token("<", REDIR_IN));
50 }
51 else if (*str == '>')
52 {
53 if (*(str + 1) == '>')
54 {
55 append_token(tokens, new_token(">>", REDIR_APPEND));
56 (str)++;
57 }
58 else
59 append_token(tokens, new_token(">", REDIR_OUT));
60 }
61 else if (*str == '|')
62 append_token(tokens, new_token("|", PIPE));
63 (str)++;
64 return (str);
65}
void append_token(t_token **tokens, t_token *new_token)
Definition tokeniser.c:85

References append_token(), new_token(), PIPE, REDIR_APPEND, REDIR_HEREDOC, REDIR_IN, and REDIR_OUT.

Referenced by tokenise().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ heredoc()

char * heredoc ( char *  eof)

◆ init_ms_data()

void init_ms_data ( t_ms_data data,
char **  argv,
char **  envp 
)

Definition at line 20 of file initialise.c.

21{
22 (void)argv;
23 data->args = NULL;
24 data->envp = NULL;
25 init_env(&data->envp, envp);
26 data->shell_variables = NULL;
27 handle_add_set_shell_variable(&data->shell_variables, "_=/usr/bin/env");
28 set_shell_var(&data->shell_variables, "?", "0");
29 data->current_dir = getcwd(NULL, 0);
30 data->exit_status = 0;
31 data->std_in = -1;
32 data->std_out = -1;
33 data->std_err = 2;
34}
void init_env(t_env **data_envp, char **envp)
Definition env.c:24
void set_shell_var(t_env **shell_var, const char *key, const char *value)
int exit_status
Definition shell.h:30
char ** args
Definition shell.h:23

References s_ms_data::args, s_ms_data::current_dir, s_ms_data::envp, s_ms_data::exit_status, handle_add_set_shell_variable(), init_env(), set_shell_var(), s_ms_data::shell_variables, s_ms_data::std_err, s_ms_data::std_in, and s_ms_data::std_out.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initialise()

void initialise ( int  argc,
char **  argv 
)

Definition at line 36 of file initialise.c.

37{
38 if (argc > 1)
39 {
40 ft_printf("Usage: %s\n", argv[0]);
41 exit(EXIT_FAILURE);
42 }
43 read_history(HISTORY_PATH);
44}

References EXIT_FAILURE, ft_printf(), and HISTORY_PATH.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ input_error_checks()

int input_error_checks ( t_loop_data loop_data)

Definition at line 96 of file input_checker.c.

97{
98 const char *str = loop_data->trimmed_input;
99
100 if (check_redirections(str))
101 ft_printf("Input error: invalid redirection.\n");
102 else if (check_operators(loop_data))
103 ft_printf("Input error: invalid operator.\n");
104 else if (check_open_quotes(str))
105 ft_printf("Input error: open quote.\n");
106 else
107 return (0);
108 return (1);
109}
int check_redirections(const char *str)
int check_open_quotes(const char *str)
int check_operators(t_loop_data *loop_data)
char * trimmed_input
Definition tokens.h:61

References check_open_quotes(), check_operators(), check_redirections(), ft_printf(), and s_loop_data::trimmed_input.

Referenced by main_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_in_single_quotes()

int is_in_single_quotes ( char *  arg)

Definition at line 28 of file loc_env_var_handler_utils_utils_utils.c.

29{
30 int len;
31
32 if (!arg || arg[0] != '\'')
33 return (0);
34 len = 0;
35 while (arg[len] != '\0')
36 len++;
37 if (arg[len - 1] == '\'')
38 return (1);
39 return (0);
40}

Referenced by post_process_command_args().

Here is the caller graph for this function:

◆ is_redir_node()

int is_redir_node ( t_token tokens)

Definition at line 79 of file AST.c.

80{
81 if (tokens->type == REDIR_IN
82 || tokens->type == REDIR_OUT
83 || tokens->type == REDIR_APPEND
84 || tokens->type == REDIR_HEREDOC)
85 return (1);
86 return (0);
87}

References REDIR_APPEND, REDIR_HEREDOC, REDIR_IN, REDIR_OUT, and s_token::type.

Referenced by manage_redirs().

Here is the caller graph for this function:

◆ list_to_array()

char ** list_to_array ( t_token head)

Definition at line 80 of file stack_control.c.

81{
82 int count;
83 int i;
84 char **arr;
85 t_token *current;
86
87 current = head;
88 count = calc_stack_size(current);
89 arr = (char **)malloc((count + 1) * sizeof(char *));
90 if (!arr)
91 return (NULL);
92 current = head;
93 i = 0;
94 while (i < count)
95 {
96 arr[i] = ft_strdup(current->data);
97 current = current->next;
98 i++;
99 }
100 arr[count] = NULL;
101 return (arr);
102}
int calc_stack_size(t_token *stack)

References calc_stack_size(), s_token::data, ft_strdup(), and s_token::next.

Here is the call graph for this function:

◆ loop_cleanup()

void loop_cleanup ( t_loop_data loop_data,
t_token tokens_head 
)

Definition at line 67 of file clean.c.

68{
69 free(loop_data->trimmed_input);
70 free_all_tokens(tokens_head);
71 free_ast(loop_data->tree);
72}
void free_all_tokens(t_token *tokens)
Definition clean_utils.c:17
t_ast * tree
Definition tokens.h:63

References free_all_tokens(), free_ast(), s_loop_data::tree, and s_loop_data::trimmed_input.

Referenced by process_ast_and_io(), and status_handler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_history()

void make_history ( char *  line)

Definition at line 15 of file aux.c.

16{
17 if (*line)
18 add_history(line);
19 write_history(HISTORY_PATH);
20}

References HISTORY_PATH.

Referenced by main_loop().

Here is the caller graph for this function:

◆ manage_commands()

t_ast * manage_commands ( t_token **  tokens,
t_ms_data data 
)

Definition at line 72 of file AST_utils.c.

73{
74 t_ast *command_node;
75 int cmd_arg_count;
76
77 command_node = new_ast_node();
78 command_node->type = PHRASE;
79 cmd_arg_count = cmd_arg_len(*tokens);
80 command_node->args = malloc(sizeof(char *) * (cmd_arg_count + 1));
81 if (!command_node->args)
82 return (NULL);
83 set_command_args(command_node, tokens, cmd_arg_count);
84 post_process_command_args(command_node, cmd_arg_count, data);
85 return (command_node);
86}
int cmd_arg_len(t_token *current)
Definition AST_utils.c:88
t_ast * new_ast_node(void)
Definition AST.c:89
void set_command_args(t_ast *command_node, t_token **tokens, int arg_count)
Definition AST.c:48
void post_process_command_args(t_ast *command_node, int arg_count, t_ms_data *data)

References s_ast::args, cmd_arg_len(), new_ast_node(), PHRASE, post_process_command_args(), set_command_args(), and s_ast::type.

Referenced by manage_redirs().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ manage_pipe()

t_ast * manage_pipe ( t_token **  tokens,
t_ms_data data 
)

Definition at line 56 of file AST_utils.c.

57{
58 t_token *tmp;
59 t_token *next_token;
60
61 tmp = *tokens;
62 while (*tokens && (*tokens)->next)
63 {
64 next_token = (*tokens)->next;
65 if (next_token->type == PIPE)
66 return (create_pipe_node(next_token, tokens, data, tmp));
67 *tokens = next_token;
68 }
69 return (manage_redirs(&tmp, data));
70}
t_ast * create_pipe_node(t_token *next_token, t_token **tokens, t_ms_data *data, t_token *tmp)
Definition AST_utils.c:30
t_ast * manage_redirs(t_token **tokens, t_ms_data *data)
Definition AST.c:20

References create_pipe_node(), manage_redirs(), s_token::next, PIPE, and s_token::type.

Referenced by create_pipe_node(), and parse_tokens().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ manage_redirs()

t_ast * manage_redirs ( t_token **  tokens,
t_ms_data data 
)

Definition at line 20 of file AST.c.

21{
22 t_ast *command_node;
23 t_ast *redirect_node;
24 t_token *current_token;
25
26 if (!tokens || !*tokens)
27 return (NULL);
28 command_node = manage_commands(tokens, data);
29 current_token = *tokens;
30 while (current_token && is_redir_node(current_token))
31 {
32 redirect_node = create_redir_node(current_token);
33 redirect_node->left = command_node;
34 *tokens = current_token->next;
35 if (*tokens)
36 {
37 redirect_node->right = create_redir_node(*tokens);
38 *tokens = (*tokens)->next;
39 }
40 else
41 redirect_node->right = NULL;
42 command_node = redirect_node;
43 current_token = *tokens;
44 }
45 return (command_node);
46}
t_ast * create_redir_node(t_token *token)
Definition AST.c:62
int is_redir_node(t_token *tokens)
Definition AST.c:79
t_ast * manage_commands(t_token **tokens, t_ms_data *data)
Definition AST_utils.c:72

References create_redir_node(), is_redir_node(), s_ast::left, manage_commands(), s_token::next, and s_ast::right.

Referenced by create_pipe_node(), and manage_pipe().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ new_ast_node()

t_ast * new_ast_node ( void  )

Definition at line 89 of file AST.c.

90{
91 t_ast *node;
92
93 node = malloc(sizeof(t_ast));
94 if (!node)
95 return (NULL);
96 node->type = NONE;
97 node->args = NULL;
98 node->left = NULL;
99 node->right = NULL;
100 return (node);
101}

References s_ast::args, s_ast::left, NONE, s_ast::right, and s_ast::type.

Referenced by create_pipe_node(), create_redir_node(), and manage_commands().

Here is the caller graph for this function:

◆ new_token()

t_token * new_token ( char *  value,
t_token_type  type 
)

Definition at line 67 of file tokeniser.c.

68{
69 t_token *token;
70
71 token = malloc(sizeof(t_token));
72 if (!token)
73 return (NULL);
74 token->data = ft_strdup(value);
75 if (!token->data)
76 {
77 free(token);
78 return (NULL);
79 }
80 token->type = type;
81 token->next = NULL;
82 return (token);
83}

References s_token::data, ft_strdup(), s_token::next, and s_token::type.

Referenced by append_token(), append_word_if_valid(), finalize_token(), handle_special_chars(), and process_single_token().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_input()

char ** parse_input ( char *  input)

◆ parse_loop()

void parse_loop ( char **  input,
char **  tokens,
int *  pos,
int *  bufsize 
)

◆ parse_tokens()

t_ast * parse_tokens ( t_token **  tokens,
t_ms_data data 
)

Definition at line 23 of file AST_utils.c.

24{
25 if (!tokens || !*tokens)
26 return (NULL);
27 return (manage_pipe(tokens, data));
28}
t_ast * manage_pipe(t_token **tokens, t_ms_data *data)
Definition AST_utils.c:56

References manage_pipe().

Referenced by main_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ post_process_command_args()

void post_process_command_args ( t_ast command_node,
int  arg_count,
t_ms_data data 
)

Definition at line 59 of file loc_env_var_handler.c.

61{
62 int i;
63 char *processed_arg;
64 int current_size;
65 char *tmp_proc_arg;
66
67 i = 0;
68 tmp_proc_arg = NULL;
69 current_size = arg_count;
70 while (i < current_size)
71 {
72 if (!is_in_single_quotes(command_node->args[i]))
73 {
74 handle_local_vars(data, command_node->args[i]);
75 tmp_proc_arg = process_argument(command_node->args[i], data);
76 }
77 else
78 tmp_proc_arg = ft_substr(command_node->args[i], 1, \
79 ft_strlen(command_node->args[i]) - 2);
80 free(command_node->args[i]);
81 processed_arg = ft_remove_quotes(tmp_proc_arg, '\"');
82 free(tmp_proc_arg);
83 split_loc_vars(command_node, processed_arg, &current_size, &i);
84 free(processed_arg);
85 }
86 command_node->args[current_size] = NULL;
87}
char * ft_remove_quotes(char *str, char quote_type)
void handle_local_vars(t_ms_data *data, char *arg)
void split_loc_vars(t_ast *command_node, char *processed_arg, int *current_size, int *i)
char * process_argument(char *arg, t_ms_data *data)
int is_in_single_quotes(char *arg)

References s_ast::args, ft_remove_quotes(), ft_strlen(), ft_substr(), handle_local_vars(), is_in_single_quotes(), process_argument(), and split_loc_vars().

Referenced by manage_commands().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_ast_args()

void print_ast_args ( t_ast node)

Definition at line 37 of file tokeniser_helpers.c.

38{
39 int i;
40
41 i = 0;
42 while (node->args[i] != NULL)
43 {
44 ft_printf("ast arg[%d] -> %s \n", i, node->args[i]);
45 i++;
46 }
47}

References s_ast::args, and ft_printf().

Here is the call graph for this function:

◆ print_ast_root()

void print_ast_root ( t_ast root)

Definition at line 103 of file visualiser.c.

104{
105 if (!root)
106 return ;
107 print_ast_graphical(root, 0, "", 0);
108}
void print_ast_graphical(t_ast *node, int depth, char *prefix, int is_left)
Definition visualiser.c:91

References print_ast_graphical().

Here is the call graph for this function:

◆ print_maxishell()

void print_maxishell ( void  )

Definition at line 56 of file prompt.c.

57{
58 ft_printf("\033[1;33m\n\n\n\n\n");
59 ft_printf("███╗░░░███╗░█████╗░██╗░░██╗██╗░██████╗██╗░"
60 "░██╗███████╗██╗░░░░░██╗░░░░░\n");
61 ft_printf("████╗░████║██╔══██╗╚██╗██╔╝██║██╔════╝██║░"
62 "░██║██╔════╝██║░░░░░██║░░░░░\n");
63 ft_printf("██╔████╔██║███████║░╚███╔╝░██║╚█████╗░█████"
64 "██║█████╗░░██║░░░░░██║░░░░░\n");
65 ft_printf("██║╚██╔╝██║██╔══██║░██╔██╗░██║░╚═══██╗██╔══"
66 "██║██╔══╝░░██║░░░░░██║░░░░░\n");
67 ft_printf("██║░╚═╝░██║██║░░██║██╔╝╚██╗██║██████╔╝██║░░"
68 "██║███████╗███████╗███████╗\n");
69 ft_printf("╚═╝░░░░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═╝╚═════╝░╚═╝░░"
70 "╚═╝╚══════╝╚══════╝╚══════╝\n");
71 ft_printf("\n");
72 ft_printf(" \033[1;34m"
73 "Version 1.0.0\033[0m\n");
74 ft_printf(" \033[1;36m"
75 "By Димас и Ромас\033[0m\n");
76 ft_printf("\n");
77}

References ft_printf().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_stack()

void print_stack ( t_token **  stack)

Definition at line 36 of file stack_control.c.

37{
38 t_token *current_node;
39 int i;
40
41 i = 0;
42 current_node = *stack;
43 while (current_node != NULL)
44 {
45 ft_printf("input[%d] -> %s \n", i, current_node->data);
46 current_node = current_node->next;
47 i++;
48 }
49}

References s_token::data, ft_printf(), and s_token::next.

Here is the call graph for this function:

◆ print_tokens()

void print_tokens ( t_token tokens,
char *  name 
)

Definition at line 19 of file tokeniser_helpers.c.

20{
21 t_token *token;
22 int i;
23
24 i = 0;
25 token = tokens;
26 if (ft_strlen(name) > 0)
27 ft_printf(GRN"----- %s[] -----\n"RESET, name);
28 while (token != NULL)
29 {
30 ft_printf(GRN"input[%d] -> %s at add: %p\n"RESET, \
31 i, token->data, &token->data);
32 token = token->next;
33 i++;
34 }
35}
#define RESET
Definition libft.h:111
#define GRN
Definition libft.h:115

References s_token::data, ft_printf(), ft_strlen(), GRN, s_token::next, and RESET.

Here is the call graph for this function:

◆ process_argument()

char * process_argument ( char *  arg,
t_ms_data data 
)

Definition at line 103 of file loc_env_var_handler.c.

104{
105 char *start;
106 char *processed_arg;
107 char *tmp_ad;
108 char *expanded_var;
109
110 tmp_ad = NULL;
111 processed_arg = ft_strdup("");
112 start = arg;
113 while (*start != '\0')
114 {
115 if (*start == '$')
116 {
117 expanded_var = expand_variable(&start, data);
118 if (expanded_var != NULL)
119 {
120 processed_arg = append_expanded_var(processed_arg, \
121 tmp_ad, expanded_var);
122 free(tmp_ad);
123 }
124 }
125 else
126 processed_arg = append_literal(&start, processed_arg);
127 }
128 return (processed_arg);
129}
char * append_expanded_var(char *processed_arg, char *tmp_ad, char *expanded_var)
char * append_literal(char **start, char *processed_arg)
char * expand_variable(char **start, t_ms_data *data)

References append_expanded_var(), append_literal(), expand_variable(), and ft_strdup().

Referenced by post_process_command_args().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reallocate_tokens()

void reallocate_tokens ( char ***  tokens,
int *  bufsize 
)

◆ set_command_args()

void set_command_args ( t_ast command_node,
t_token **  tokens,
int  arg_count 
)

Definition at line 48 of file AST.c.

49{
50 int i;
51
52 i = 0;
53 while (i < arg_count)
54 {
55 command_node->args[i] = ft_strdup((*tokens)->data);
56 *tokens = (*tokens)->next;
57 i++;
58 }
59 command_node->args[arg_count] = NULL;
60}

References s_ast::args, ft_strdup(), and s_token::next.

Referenced by manage_commands().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ skip_delimiters()

void skip_delimiters ( char **  input,
char *  delim 
)

◆ str_start_adj()

char * str_start_adj ( char *  arg)

Definition at line 42 of file loc_env_var_handler_utils_utils_utils.c.

43{
44 if (!ft_strcmp(arg, "?") || !ft_strcmp(arg, "?\""))
45 return ("");
46 else if (!ft_strcmp(arg, "?\'"))
47 return ("\'");
48 return (arg);
49}

References ft_strcmp().

Referenced by expand_variable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tmp_adj()

char * tmp_adj ( char *  arg)

Definition at line 51 of file loc_env_var_handler_utils_utils_utils.c.

52{
53 char *ptr;
54 char *tmp_adj_dup;
55
56 if (*arg == '\"')
57 {
58 ptr = arg + 1;
59 while (*ptr)
60 {
61 if (!ft_isdigit(*ptr))
62 return (arg);
63 ptr++;
64 }
65 tmp_adj_dup = ft_strdup(arg + 1);
66 free(arg);
67 return (tmp_adj_dup);
68 }
69 tmp_adj_dup = ft_strdup(arg);
70 free(arg);
71 return (tmp_adj_dup);
72}
int ft_isdigit(int c)
Definition ft_isdigit.c:13

References ft_isdigit(), and ft_strdup().

Referenced by append_expanded_var().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tokenise()

t_token * tokenise ( char *  str)

Definition at line 22 of file tokeniser.c.

23{
24 t_token *tokens;
25
26 tokens = NULL;
27 while (*str)
28 {
29 while (*str && ft_strchr(" \t\n\r\v\f", *str) != NULL)
30 str++;
31 if (ft_strchr("<|>", *str) != NULL)
32 str = handle_special_chars(str, &tokens);
33 else
34 str = handle_phrase(str, &tokens);
35 }
36 return (tokens);
37}
char * handle_phrase(char *str, t_token **tokens)
Definition tokeniser.c:100
char * handle_special_chars(char *str, t_token **tokens)
Definition tokeniser.c:39

References ft_strchr(), handle_phrase(), and handle_special_chars().

Referenced by main_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ trim_input()

char * trim_input ( char *  str)

Definition at line 15 of file input_checker_helpers.c.

16{
17 char *trimmed_str;
18
19 trimmed_str = ft_strtrim(str, " \t\n\r\v\f");
20 if (!trimmed_str)
21 return (0);
22 return (trimmed_str);
23}
char * ft_strtrim(char const *s1, char const *set)
Definition ft_strtrim.c:39

References ft_strtrim().

Referenced by main_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ valid_operator()

int valid_operator ( const char **  str)

Definition at line 25 of file input_checker_helpers.c.

26{
27 const char *start;
28
29 start = (*str)++;
30 if (*start == **str)
31 (*str)++;
32 *str = ft_exclude_delimiters(*str, " \t\n\r\v\f");
33 if (**str == '<' || **str == '>' || **str == '|' || **str == '\0')
34 return (0);
35 return (1);
36}
const char * ft_exclude_delimiters(const char *str, char *delims)

References ft_exclude_delimiters().

Referenced by check_redirections().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ visualize_ast()

void visualize_ast ( t_ast root)