maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
loc_env_var_handler_heredoc.c File Reference
#include "shell.h"
#include "tokens.h"
#include "redirection.h"
#include "execute.h"
#include <fcntl.h>
#include <sys/wait.h>
#include "signals.h"
Include dependency graph for loc_env_var_handler_heredoc.c:

Go to the source code of this file.

Functions

static char * assemble_result (char **tokens, size_t result_len)
 
char * token_adj (char *arg)
 
char * finalize_token (char *tmp_token, char *orig_token)
 
size_t process_single_token (char **token, t_ms_data *data)
 
size_t process_tokens (char **tokens, t_ms_data *data)
 
char * process_and_reassemble (char *line, t_ms_data *data)
 

Function Documentation

◆ assemble_result()

static char * assemble_result ( char **  tokens,
size_t  result_len 
)
static

Definition at line 96 of file loc_env_var_handler_heredoc.c.

97{
98 char *result;
99 int i;
100
101 result = malloc(result_len + 1);
102 if (!result)
103 return (NULL);
104 *result = '\0';
105 i = 0;
106 while (tokens[i])
107 {
108 ft_strcat(result, tokens[i]);
109 if (tokens[i + 1])
110 ft_strcat(result, " ");
111 i++;
112 }
113 return (result);
114}
char * ft_strcat(char *dest, char *src)
Definition ft_strcat.c:26

References ft_strcat().

Referenced by process_and_reassemble().

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

◆ finalize_token()

char * finalize_token ( char *  tmp_token,
char *  orig_token 
)

Definition at line 74 of file loc_env_var_handler_heredoc.c.

75{
76 char *processed_token;
77 char *new_token;
78
79 if (tmp_token)
80 {
81 processed_token = token_adj(tmp_token);
82 if (processed_token != tmp_token)
83 {
84 new_token = ft_strdup(processed_token);
85 free(tmp_token);
86 }
87 else
88 new_token = tmp_token;
89 free(processed_token);
90 }
91 else
92 new_token = ft_strdup(orig_token);
93 return (new_token);
94}
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
char * token_adj(char *arg)
t_token * new_token(char *value, t_token_type type)
Definition tokeniser.c:67

References ft_strdup(), new_token(), and token_adj().

Referenced by process_single_token().

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

◆ process_and_reassemble()

char * process_and_reassemble ( char *  line,
t_ms_data data 
)

Definition at line 27 of file loc_env_var_handler_heredoc.c.

28{
29 char **tokens;
30 size_t result_len;
31 char *result;
32
33 tokens = ft_split(line, ' ');
34 free(line);
35 if (!tokens)
36 return (NULL);
37 result_len = process_tokens(tokens, data);
38 result = assemble_result(tokens, result_len);
39 ft_free_2d_arr(tokens);
40 return (result);
41}
void ft_free_2d_arr(char **arr)
char ** ft_split(char const *s, char c)
Definition ft_split.c:95
size_t process_tokens(char **tokens, t_ms_data *data)
static char * assemble_result(char **tokens, size_t result_len)

References assemble_result(), ft_free_2d_arr(), ft_split(), and process_tokens().

Referenced by redirect_here_doc().

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

◆ process_single_token()

size_t process_single_token ( char **  token,
t_ms_data data 
)

Definition at line 58 of file loc_env_var_handler_heredoc.c.

59{
60 char *orig_token;
61 char *tmp_token;
62 char *new_token;
63 size_t new_len;
64
65 orig_token = *token;
66 tmp_token = expand_variable(token, data);
67 new_token = finalize_token(tmp_token, orig_token);
68 *token = new_token;
69 free(orig_token);
70 new_len = ft_strlen(new_token) + 1;
71 return (new_len);
72}
size_t ft_strlen(const char *s)
Definition ft_strlen.c:15
char * finalize_token(char *tmp_token, char *orig_token)
char * expand_variable(char **start, t_ms_data *data)

References expand_variable(), finalize_token(), ft_strlen(), and new_token().

Referenced by process_tokens().

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

◆ process_tokens()

size_t process_tokens ( char **  tokens,
t_ms_data data 
)

Definition at line 43 of file loc_env_var_handler_heredoc.c.

44{
45 int i;
46 size_t result_len;
47
48 result_len = 0;
49 i = 0;
50 while (tokens[i])
51 {
52 result_len += process_single_token(&tokens[i], data);
53 i++;
54 }
55 return (result_len);
56}
size_t process_single_token(char **token, t_ms_data *data)

References process_single_token().

Referenced by process_and_reassemble().

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

◆ token_adj()

char * token_adj ( char *  arg)

Definition at line 21 of file loc_env_var_handler_heredoc_utils.c.

22{
23 char *ptr;
24 char *new_arg;
25
26 if (*arg == '\"')
27 {
28 ptr = arg + 1;
29 while (*ptr && *ptr != '\"')
30 {
31 if (!ft_isdigit(*ptr))
32 return (ft_strdup(arg));
33 ptr++;
34 }
35 if (*ptr == '\0')
36 {
37 new_arg = malloc(ft_strlen(arg) + 2);
38 if (!new_arg)
39 return (NULL);
40 ft_strcpy(new_arg, arg);
41 ft_strcat(new_arg, "\"");
42 return (new_arg);
43 }
44 if (*ptr == '\"' && *(ptr + 1) == '\0')
45 return (ft_strdup(arg));
46 }
47 return (ft_strdup(arg));
48}
int ft_isdigit(int c)
Definition ft_isdigit.c:13
char * ft_strcpy(char *dest, const char *src)
Definition ft_strcpy.c:25

References ft_isdigit(), ft_strcat(), ft_strcpy(), ft_strdup(), and ft_strlen().

Referenced by finalize_token().

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