-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize.c
122 lines (117 loc) · 2.66 KB
/
tokenize.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
#include "shell.h"
/**
* tokenize_buf - tokenize buffer by inputting NULLs and filling **av
* @b: The buffer with the command string
* @argv: Pointer to the command argument vector
* Description: This function accepts a string such as 'ls -l' and changes
* it to 'ls\0-l'. It puts pointers to 'ls' and '-l' into *av
*/
void tokenize_buf(buffer *b, char ***argv)
{
int i, ap, flag, whitespace;
_av_init(b->buf + b->bp, argv);
/* Build the argument vector from the given buffer */
for (i = b->bp, ap = 0, flag = 1; !_is_endofcmd(b->buf[i]); i++)
{
whitespace = _is_whitespace(b->buf[i]);
if (flag && !whitespace)
{
(*argv)[ap++] = b->buf + i;
flag = 0;
}
if (whitespace)
{
b->buf[i] = '\0';
flag = 1;
}
}
(*argv)[ap] = NULL;
/* If we end because of comments */
if (b->buf[i] == '#')
while (b->buf[i] != '\n' && b->buf[i] != '\0')
i++;
/* If we ended because of newline, we MAY have more content */
if (b->buf[i] == '\n' && b->buf[i + 1] != '\0')
b->buf[i] = ';';
else if (b->buf[i] == '\n')
b->buf[i] = '\0';
/* If we ended because of flow control commands, */
/* increment the buffer point and add a null before the character */
if (b->buf[i] == ';' || b->buf[i] == '|' || b->buf[i] == '&')
{
b->bp += i - b->bp;
_add_null(b->buf + b->bp);
b->bp++;
}
else
b->bp = 0;
}
/**
* _av_init - resize av if needed
* @buf: The buffer with the command string
* @argv: Command argument vector
*/
void _av_init(char *buf, char ***argv)
{
int c, i, flag, whitespace;
for (c = 0, flag = 1; !_is_endofcmd(*buf); buf++)
{
whitespace = _is_whitespace(*buf);
if (flag && !whitespace)
flag = 0, c++;
else if (whitespace)
flag = 1;
}
if (*argv != NULL)
{
for (i = 0; (*argv)[i] != NULL; i++)
;
if (c > i)
{
_free(*argv);
*argv = safe_malloc(sizeof(char *) * (c + 1));
}
return;
}
else
*argv = safe_malloc(sizeof(char *) * (c + 1));
}
/**
* _add_null - Add a null before the multi-command operator
* @buf: buffer structure to add null into
*/
void _add_null(char *buf)
{
int i;
for (i = 0; buf[i] != '\0'; i++)
;
i++;
for ( ; i > 0; i--)
buf[i + 1] = buf[i];
buf[i + 1] = buf[i];
buf[i] = '\0';
}
/**
* _is_whitespace - Boolean true for false for whitespace
* @c: char to evalute.
*
* Return: 1 if whitespace, 0 if not
*/
int _is_whitespace(char c)
{
if (c == ' ' || c == '\n' || c == '\t')
return (1);
return (0);
}
/**
* _is_endofcmd - Boolean true or false if char signals end of command
* @c: char to evaluate
*
* Return: 1 if end of command, 0 if not
*/
int _is_endofcmd(char c)
{
if (c == '\0' || c == '\n' || c == '|' || c == '&' || c == ';' || c == '#')
return (1);
return (0);
}