MiniDevil As beautiful as a shell
token.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* token.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/03 23:28:33 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 04:42:54 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "token.h"
14 #include "libft.h"
15 
26 t_token *create_token(t_token_type type, char *value)
27 {
28  t_token *token;
29 
30  token = ft_calloc(sizeof(t_token), 1);
31  if (!token)
32  return (NULL);
33  token->type = type;
34  token->value = ft_strdup(value);
35  if (!token->value)
36  {
37  free(token);
38  return (NULL);
39  }
40  token->quote_type = QUOTE_NONE;
41  token->connected = 0;
42  token->next = NULL;
43  return (token);
44 }
45 
51 void free_token(t_token *token)
52 {
53  if (!token)
54  return ;
55  free(token->value);
56  free(token);
57 }
58 
65 {
66  t_token *current;
67  t_token *next;
68 
69  current = head;
70  while (current)
71  {
72  next = current->next;
73  free_token(current);
74  current = next;
75  }
76 }
77 
84 void add_token(t_token **head, t_token *new_token)
85 {
86  t_token *current;
87 
88  if (!new_token)
89  return ;
90  if (!*head)
91  {
92  *head = new_token;
93  return ;
94  }
95  current = *head;
96  while (current->next)
97  current = current->next;
98  current->next = new_token;
99 }
t_token_type
Token types produced by Lexer.
Definition: structs.h:24
@ QUOTE_NONE
Definition: structs.h:42
Lexer token (singly linked list)
Definition: structs.h:71
char * value
Text content (allocated)
Definition: structs.h:73
t_token_type type
Token type (word, pipe, redir)
Definition: structs.h:72
struct s_token * next
Next token.
Definition: structs.h:76
t_quote_type quote_type
Quote context.
Definition: structs.h:74
int connected
1 if connected
Definition: structs.h:75
void free_token_list(t_token *head)
Free the entire token linked list.
Definition: token.c:64
void add_token(t_token **head, t_token *new_token)
Add a token to the end of a linked list.
Definition: token.c:84
void free_token(t_token *token)
Free a single token and its value str.
Definition: token.c:51
t_token * create_token(t_token_type type, char *value)
Allocate and initialize a new token.
Definition: token.c:26
Lexer, tokenizer, expander, and quote handling prototypes.