-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins2.c
124 lines (113 loc) · 2.94 KB
/
builtins2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mleclair <mleclair@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/11 16:03:36 by mleclair #+# #+# */
/* Updated: 2017/05/19 17:27:18 by mleclair ### ########.fr */
/* */
/* ************************************************************************** */
#include "chell.h"
void add_local(t_env *env, char **split)
{
int i;
int j;
(void)env;
i = -1;
while (split[++i])
{
if ((j = ft_strfind(split[i], '=')) != -1)
if (ft_isalpha(split[i][0]))
add_var_to_env(env->loc, split[i]);
}
}
void builtin_export(t_env *env, char **split)
{
int i;
int j;
i = 0;
if (!split[1] || (split[1][0] == '-' && split[1][1] == 'p'))
while (env->ev[++i])
ft_printf("%s %s\n", "export", env->ev[i]);
else if (split[1][1] && split[1][0] == '-' && split[1][1] != 'p')
ft_printf("%s %c\n", "export : bad option:", split[1][1]);
else
while (split[++i])
if (ft_strfind(split[i], '=') != -1)
{
add_local(env, split);
add_var_to_env(env, split[i]);
}
else
{
j = find_param(env->loc->ev, split[i]);
if (j == -1)
continue ;
add_var_to_env(env, env->loc->ev[j]);
}
}
void builtin_read2(char *str)
{
int i;
char *tmp;
i = -1;
while (str[++i])
if (str[i] == '\\')
{
tmp = palloc(ft_strlen(str) + 2);
*tmp = 0;
ft_strncat(tmp, str, i);
tmp[i] = '\\';
tmp[i + 1] = 0;
ft_strcat(tmp, str + i);
free(str);
str = tmp;
}
}
void builtin_read(t_env *ev, char **split)
{
char *bufe;
char truc[INPUT_SIZE];
int r[2];
r[0] = 0;
r[1] = 1;
ft_bzero(truc, INPUT_SIZE);
bufe = ft_strnew(INPUT_SIZE);
read(0, bufe, INPUT_SIZE);
if (split[1] && split[1][0] == '-' && split[1][1] == 'r')
r[0] = 1;
if (split[1] && split[1][0] == '-' && split[1][1] == 'r')
builtin_read2(bufe);
r[1] = r[0] == 1 ? 1 : 0;
while (split[++r[1]])
{
ft_strcat(truc, split[r[1]]);
ft_strcat(truc, "=");
ft_strcat(truc, bufe);
if (truc[ft_strlen(truc) - 1] == '\n')
truc[ft_strlen(truc) - 1] = 0;
add_var_to_env(ev->loc, truc);
ft_bzero(truc, INPUT_SIZE);
}
free(bufe);
}
void ft_echo(char **split)
{
int i;
i = 0;
if (split[1] && split[1][0] == '-' && split[1][1] == 'n')
i = 1;
while (split[++i])
{
ft_putstr(split[i]);
if (split[i + 1])
ft_putchar(' ');
}
if (split[1] && split[1][0] == '-' && split[1][1] == 'n')
;
else
ft_putchar('\n');
retvalue_into_loc(env(), 0);
}