-
Notifications
You must be signed in to change notification settings - Fork 0
/
set2.c
154 lines (142 loc) · 2.32 KB
/
set2.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "main.h"
/**
* is_chain - it test for current char
* @info: para struct
* @buf: buf char
* @p: address
*
* Return: 1
*/
int is_chain(info_t *info, char *buf, size_t *p)
{
size_t q = *p;
if (buf[q] == '|' && buf[q + 1] == '|')
{
buf[q] = 0;
q++;
info->cmd_buf_type = CMD_OR;
}
else if (buf[q] == '&' && buf[q + 1] == '&')
{
buf[q] = 0;
q++;
info->cmd_buf_type = CMD_AND;
}
else if (buf[q] == ';')
{
buf[q] = 0;
info->cmd_buf_type = CMD_CHAIN;
}
else
return (0);
*p = q;
return (1);
}
/**
* check_chain - checks on last status
* @info: struct
* @buf: its char buf
* @p: buf addres
* @i: its position in buf
* @len: len buf
*
* Return: Void
*/
void check_chain(info_t *info, char *buf, size_t *p, size_t i, size_t len)
{
size_t q = *p;
if (info->cmd_buf_type == CMD_AND)
{
if (info->status)
{
buf[i] = 0;
q = len;
}
}
if (info->cmd_buf_type == CMD_OR)
{
if (!info->status)
{
buf[i] = 0;
q = len;
}
}
*p = q;
}
/**
* replace_alias - function replace
* @info: its p struct
*
* Return: 1
*/
int replace_alias(info_t *info)
{
int a;
list_t *node;
char *p;
for (a = 0; a < 10; a++)
{
node = node_starts_with(info->alias, info->argv[0], '=');
if (!node)
return (0);
free(info->argv[0]);
p = _strchr(node->str, '=');
if (!p)
return (0);
p = _strdup(p + 1);
if (!p)
return (0);
info->argv[0] = p;
}
return (1);
}
/**
* replace_vars - function vars
* @info: struct
*
* Return: 0
*/
int replace_vars(info_t *info)
{
int a = 0;
list_t *node;
for (a = 0; info->argv[a]; a++)
{
if (info->argv[a][0] != '$' || !info->argv[a][1])
continue;
if (!_strcmp(info->argv[a], "$?"))
{
replace_string(&(info->argv[a]),
_strdup(convert_number(info->status, 10, 0)));
continue;
}
if (!_strcmp(info->argv[a], "$$"))
{
replace_string(&(info->argv[a]),
_strdup(convert_number(getpid(), 10, 0)));
continue;
}
node = node_starts_with(info->env, &info->argv[a][1], '=');
if (node)
{
replace_string(&(info->argv[a]),
_strdup(_strchr(node->str, '=') + 1));
continue;
}
replace_string(&info->argv[a], _strdup(""));
}
return (0);
}
/**
* replace_string - func str replace
* @old: addres
* @new: new string
*
* Return: 1
*/
int replace_string(char **old, char *new)
{
free(*old);
*old = new;
return (1);
}