-
Notifications
You must be signed in to change notification settings - Fork 3
/
commandfilter.c
129 lines (122 loc) · 2.27 KB
/
commandfilter.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
#include "holberton.h"
/**
* _ignorecomments - ignorecomments.
* @buf: node tha has the builtin command
* @i: integer
*
* Return: no return
*/
void _ignorecomments(char *buf, int *i)
{
while (buf[*i] != 0 && buf[*i] != '\n')
{
*i = *i + 1;
}
*i = *i + 1;
}
/**
* _isalias - show help document of some functions.
* @buf: node tha has the builtin command
* @i: integer
*
* Return: no return
*/
alias *_isalias(char *buf, int *i)
{
alias *ali = NULL;
int cb = *i, j = 0;
char *name = NULL;
if (cb != 0 && !_isespecialchr(buf[(cb - 1)]))
return (NULL);
name = _calloc(500, 1);
for (; !_isespecialchr(buf[cb]); cb++)
;
for (; (j + *i) < cb; j++)
name[j] = buf[j + *i];
ali = setalias(NULL);
ali = buscar_alias(ali, name);
free(name);
if (ali)
{
*i = cb;
return (ali);
}
return (NULL);
}
/**
* _replacealias - show help document of some functions.
* @ali: node tha has the builtin command
* @newbuf: new buffer
* @pos: size all line
*
* Return: no return
*/
void _replacealias(alias *ali, char *newbuf, int *pos)
{
int i = 0;
char *value = NULL;
alias *aux = NULL;
aux = setalias(NULL);
aux = buscar_alias(aux, ali->value);
if (aux)
ali = aux;
value = ali->value;
while (value[i])
{
if (value[i] != 39)
{
newbuf[*pos] = value[i];
*pos = *pos + 1;
}
i++;
}
}
/**
* buffer_filter - show help document of some functions.
* @buffer: node tha has the builtin command
* @p: p
*
* Return: no return
*/
void buffer_filter(char **buffer, ssize_t *p)
{
char *newbuf = _calloc(4096 * 2, 1);
alias *ali = NULL;
char *buf = *buffer;
int pos = 0, i = 0, cpy = 0;
while (i <= *p)
{
if (buf[i] == '#')
{
if (i == 0)
{
_ignorecomments(buf, &i);
continue; }
else if (buf[i - 1] == ' ' || buf[i - 1] == '\t' ||
buf[i - 1] == ';')
{
_ignorecomments(buf, &i);
continue; }
}
if (buf[i] == '$' && buf[i + 1] != ' ' && buf[i + 1] != '\t'
&& buf[i + 1] != 0 && buf[i + 1] != '\n'
&& buf[i + 1] != ';')
{
_replacevar(buf, newbuf, &i, &pos);
continue; }
cpy = i;
ali = _isalias(buf, &i);
if (ali)
{
if (!have_alias(buf, i))
{
_replacealias(ali, newbuf, &pos);
continue; }
i = cpy; }
i = cpy;
newbuf[pos] = buf[i];
i++, pos++; }
free(buf);
*p = pos;
*buffer = newbuf;
}