-
Notifications
You must be signed in to change notification settings - Fork 2
/
variables_utils.c
132 lines (120 loc) · 3.91 KB
/
variables_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* variables_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jkong <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/22 13:32:39 by jkong #+# #+# */
/* Updated: 2022/06/28 02:58:34 by jkong ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "util_flag.h"
#include "safe_mem.h"
#include "libft.h"
#include "string_buffer.h"
#include "string_vector.h"
#include "generic_list.h"
#include <stdint.h>
static const uint32_t g_legal_variable_starter[] = {
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
/* */
/* ** ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
/* */
/* ** _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x87fffffe, /* 1000 0111 1111 1111 1111 1111 1111 1110 */
/* */
/* ** ~}| {zyx wvut srqp onml kjih gfed cba` */
0x07fffffe, /* 0000 0111 1111 1111 1111 1111 1111 1110 */
/* */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
};
static const uint32_t g_legal_variable_char[] = {
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
/* */
/* ** ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0x03ff0000, /* 0000 0011 1111 1111 0000 0000 0000 0000 */
/* */
/* ** _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x87fffffe, /* 1000 0111 1111 1111 1111 1111 1111 1110 */
/* */
/* ** ~}| {zyx wvut srqp onml kjih gfed cba` */
0x07fffffe, /* 0000 0111 1111 1111 1111 1111 1111 1110 */
/* */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0000 */
};
int is_legal_variable(const char *s, size_t *index_ptr)
{
if (legal_condition(g_legal_variable_starter, s[*index_ptr]))
{
(*index_ptr)++;
while (legal_condition(g_legal_variable_char, s[*index_ptr]))
(*index_ptr)++;
return (1);
}
return (0);
}
char *alloc_str_pair(const char *s, char **val_ptr, char delim)
{
const size_t len = ft_strlen(s) + 1;
char *const dup = ft_memcpy(calloc_safe(len, sizeof(char)), s, len);
char *const val = ft_strchr(dup, delim);
if (val)
{
*val = '\0';
*val_ptr = val + 1;
}
else
*val_ptr = NULL;
return (dup);
}
void strvec_to_var_list(t_list_var **list_ptr, char **arr, t_var_flags attr)
{
char **vec;
char *temp;
char *value;
vec = arr;
while (*vec)
{
temp = alloc_str_pair(*vec, &value, '=');
put_var(list_ptr, temp, value, attr);
free(temp);
vec++;
}
}
char **var_list_to_strvec(t_list_var *list, t_var_flags attr)
{
t_str_vec *vec;
t_str_buf *buf;
vec = NULL;
while (list)
{
if ((list->attr & attr) == attr && list->value)
{
buf = str_append_format(NULL, "%s=%s", list->name, list->value);
vec = strv_append(vec, str_dispose(buf));
}
list = list->next;
}
return (strv_dispose(vec));
}
void dispose_var_list(t_list_var *list)
{
t_list_var *next;
while (list)
{
next = list->next;
free(list->name);
free(list->value);
free(list);
list = next;
}
}