-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
74 lines (68 loc) · 1.42 KB
/
input.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
#include "hsh.h"
#include "getline.h"
/**
* read_input - get input
* @info: shell information
*
* Return: line size
*/
bool read_input(info_t *info)
{
char *line = NULL, *temp = NULL;
if (info->interactive)
write(STDERR_FILENO, "$ ", 2);
info->lineno += 1;
while (_read_input(&info->line, info->fileno) &
(QUOTE_DOUBLE | QUOTE_SINGLE | QUOTE_ESCAPE))
{
temp = line;
line = strjoin(NULL, "", temp, info->line);
free(temp);
free(info->line);
if (info->interactive)
write(STDERR_FILENO, "> ", 2);
info->lineno += 1;
}
if (line)
{
temp = info->line;
info->line = strjoin(NULL, "", line, temp);
free(temp);
free(line);
}
return (info->line);
}
/**
* _read_input - read a single line
* @lineptr: line buffer
* @fd: file descriptor to read from
*
* Return: ending quote state
*/
quote_state_t _read_input(char **lineptr, int fd)
{
char *line = *lineptr = _getline(fd);
static quote_state_t state = QUOTE_NONE;
size_t index = 0;
if (line)
{
switch (state & (QUOTE_DOUBLE | QUOTE_SINGLE))
{
case QUOTE_DOUBLE:
case QUOTE_SINGLE:
do {
index += quote_state_len(line + index, state);
if (line[index] == '\0')
continue;
if (state & (QUOTE_DOUBLE | QUOTE_SINGLE))
index += 1;
/* fall through */
case 0:
state = quote_state(line[index]);
if (state & (QUOTE_DOUBLE | QUOTE_SINGLE | QUOTE_ESCAPE))
index += 1;
} while (line[index]);
}
}
return (state);
}