Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
lexer_token_utils.c
Bu dosyanın dokümantasyonuna git.
2#include <stdlib.h> // free, malloc
3
4t_token *create_token(t_token_type type, char *value, t_quote_type quote_type)
5{
6 t_token *new_token;
7
8 new_token = (t_token *)ft_calloc(1, sizeof(t_token));
9 if (!new_token)
10 return (NULL);
11 if (value)
12 {
13 new_token->value = ft_strdup(value);
14 if (!new_token->value)
15 {
16 free(new_token);
17 return (NULL);
18 }
19 }
20 else
21 new_token->value = NULL;
22 new_token->type = type;
23 new_token->quote_type = quote_type;
24 return (new_token);
25}
26
27int append_token(t_shell *shell, t_token_type type, char *value,
28 t_quote_type quote_type)
29{
30 t_token *token;
31 t_list *node;
32
33 token = create_token(type, value, quote_type);
34 if (!token)
35 return (0);
36 node = ft_lstnew(token);
37 if (!node)
38 {
39 free(token->value);
40 free(token);
41 return (0);
42 }
43 ft_lstadd_back(&shell->token_list, node);
44 return (1);
45}
46
48{
49 t_list *last;
50 t_token *token;
51
52 last = ft_lstlast(shell->token_list);
53 if (last)
54 {
55 token = (t_token *)last->content;
56 token->adjacent = 1;
57 }
58}
59
60static void merge_token_pair(t_list *curr, t_list *next)
61{
62 t_token *curr_tok;
63 t_token *next_tok;
64 char *merged;
65
66 curr_tok = (t_token *)curr->content;
67 next_tok = (t_token *)next->content;
68 merged = ft_strjoin(curr_tok->value, next_tok->value);
69 free(curr_tok->value);
70 curr_tok->value = merged;
71 curr->next = next->next;
72 free_token_content(next->content);
73 free(next);
74}
75
77{
78 t_list *curr;
79 t_token *curr_tok;
80 t_token *next_tok;
81
82 curr = shell->token_list;
83 while (curr && curr->next)
84 {
85 curr_tok = (t_token *)curr->content;
86 next_tok = (t_token *)curr->next->content;
87 if (curr_tok->type == TOKEN_WORD && next_tok->type == TOKEN_WORD
88 && next_tok->adjacent == 1)
89 merge_token_pair(curr, curr->next);
90 else
91 curr = curr->next;
92 }
93}
t_token * create_token(t_token_type type, char *value, t_quote_type quote_type)
void mark_last_token_adjacent(t_shell *shell)
void merge_adjacent_tokens(t_shell *shell)
static void merge_token_pair(t_list *curr, t_list *next)
int append_token(t_shell *shell, t_token_type type, char *value, t_quote_type quote_type)
Minishell ana header dosyası
@ TOKEN_WORD
Definition minishell.h:50
enum e_token_type t_token_type
-----> LEXER <--—
enum e_quote_type t_quote_type
struct s_shell t_shell
struct s_token t_token
t_list * token_list
Definition minishell.h:148
char * value
Definition minishell.h:84
t_token_type type
Definition minishell.h:83
t_quote_type quote_type
Definition minishell.h:85
int adjacent
Definition minishell.h:86
void free_token_content(void *content)
Definition utils.c:36