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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* b_alias.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif
#include "b_alias_print.h"
#include "d_define.h"
#include "s_struct.h"
#include "u_alias.h"
#include "u_utils.h"
static unsigned char b_print_arg_next(char *ptr,
const char arg[],
t_bool invalid,
t_msh *msh)
{
char val[ARG_MAX];
if (*ptr == C_NUL)
{
if (u_get_alias_value(val, arg, ARG_MAX, msh) != 0)
{
ft_printf("alias %s='%s'", arg, val);
return (0);
}
else
{
ft_dprintf(STDERR_FILENO, "minishell: alias: %s: not found\n", arg);
return (1);
}
}
else if (*ptr == C_EQUALS && invalid == TRUE)
{
ft_strlcpy(val, arg, ptr - arg);
ft_dprintf(STDERR_FILENO,
"minishell: alias: `%s': invalid alias name\n",
val);
return (1);
}
return (0);
}
static unsigned char b_print_arg(const char arg[], t_msh *msh)
{
char *ptr;
t_bool invalid;
ptr = (char*)arg;
invalid = FALSE;
while (*ptr != C_NUL && *ptr != C_EQUALS)
{
if (ft_iswhitespace(*ptr) == TRUE)
invalid = TRUE;
ptr++;
}
return (b_print_arg_next(ptr, arg, invalid, msh));
}
#include <stdlib.h>
static void b_register_arg(const char arg[], t_msh *msh)
{
char *ptr;
char *rptr;
char name[255];
char value[ARG_MAX];
char tmp;
ptr = (char*)arg;
while (*ptr != C_NUL && *ptr != C_EQUALS)
{
ptr++;
}
if (*ptr == C_EQUALS)
{
ft_strlcpy(name, arg, (ptr - arg < 255) ? ((ptr - arg) + 1) : (255));
ptr += 1;
rptr = ptr;
while (*rptr != C_NUL && ft_iswhitespace(*rptr) == TRUE)
rptr++;
tmp = *(rptr + ft_strlen(name));
*(rptr + ft_strlen(name)) = C_NUL;
if (ft_strncmp(rptr, name, ft_strlen(name) + 1) == 0) {
*(rptr + ft_strlen(name)) = tmp;
ft_strlcpy(value, ptr, ARG_MAX);
ft_memmove(value + (rptr - ptr) + 1, value + (rptr - ptr), ft_strlen(value + (rptr - ptr)) + 1);
*(value + (rptr - ptr)) = C_BACKS;
}
else {
*(rptr + ft_strlen(name)) = tmp;
ft_strlcpy(value, ptr, ARG_MAX);
}
u_set_alias_value(name, value, msh);
}
}
unsigned char b_alias(char *args[], t_msh *msh)
{
const unsigned long argc = u_builtins_get_argc((const char**)args);
int i;
unsigned char ret;
ret = 0;
if (argc == 0)
{
if (msh->alias == NULL)
return (0);
b_print_alias_list(msh);
}
else if (argc > 0)
{
i = 0;
while (args[i] != NULL)
{
if (b_print_arg(args[i], msh) != 0)
{
ret = 1;
}
i++;
}
}
return (ret);
}
void b_alias_mute(char *args[], t_msh *msh)
{
const unsigned long argc = u_builtins_get_argc((const char**)args);
int i;
if (argc > 0)
{
i = 0;
while (args[i] != NULL)
{
b_register_arg(args[i], msh);
i++;
}
}
}
|