MiniDevil As beautiful as a shell
expander_utils.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* expander_utils.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/06 17:35:44 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/09 07:26:26 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "token.h"
14 #include "libft.h"
15 
26 {
27  if (!token->connected || !token->next)
28  return (0);
29  if (ft_strncmp(token->value, "$", 2) != 0)
30  return (0);
31  return (token->next->quote_type != QUOTE_NONE);
32 }
33 
40 int is_var_char(char c)
41 {
42  return (ft_isalnum(c) || c == '_');
43 }
44 
55 char *extract_var_name(char *str, int *len)
56 {
57  int i;
58 
59  i = 0;
60  if (str[0] && ft_isdigit(str[0]))
61  {
62  *len = 1;
63  return (ft_substr(str, 0, 1));
64  }
65  while (str[i] && is_var_char(str[i]))
66  i++;
67  *len = i;
68  return (ft_substr(str, 0, i));
69 }
70 
79 char *append_char(char *str, char c)
80 {
81  char *new_str;
82  int len;
83  int i;
84 
85  len = ft_strlen(str);
86  new_str = ft_calloc(sizeof(char), len + 2);
87  if (!new_str)
88  {
89  free(str);
90  return (NULL);
91  }
92  i = 0;
93  while (i < len)
94  {
95  new_str[i] = str[i];
96  i++;
97  }
98  new_str[len] = c;
99  new_str[len + 1] = '\0';
100  free(str);
101  return (new_str);
102 }
103 
113 char *append_str(char *s1, char *s2)
114 {
115  char *result;
116 
117  if (!s2)
118  return (s1);
119  result = ft_strjoin(s1, s2);
120  free(s1);
121  return (result);
122 }
int is_var_char(char c)
Check if a character is valid in a variable name.
int is_dollar_quote(t_token *token)
Check if a token is a $ connected to a quoted token.
char * extract_var_name(char *str, int *len)
Extract a variable name from after the $
char * append_str(char *s1, char *s2)
Concatenates 2 strings and frees the first.
char * append_char(char *str, char c)
Append a single character to an allocated string.
@ QUOTE_NONE
Definition: structs.h:42
Lexer token (singly linked list)
Definition: structs.h:71
char * value
Text content (allocated)
Definition: structs.h:73
struct s_token * next
Next token.
Definition: structs.h:76
int connected
1 if connected
Definition: structs.h:75
Lexer, tokenizer, expander, and quote handling prototypes.