-
Notifications
You must be signed in to change notification settings - Fork 0
/
getLine.c
175 lines (153 loc) · 3.38 KB
/
getLine.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "shell.h"
/**
* input_buf - Buffers chained commands.
* @info: Parameter struct.
* @buf: Address of buffer.
* @len: Address of len var.
*
* Return: Bytes read.
*/
ssize_t input_buf(info_t *info, char **buf, size_t *len)
{
ssize_t r = 0;
size_t len_p = 0;
if (!*len) /* If nothing left in the buffer, fill it. */
{
/* bfree((void **)info->cmd_buf); */
free(*buf);
*buf = NULL;
signal(SIGINT, sigintHandler);
#if USE_GETLINE
r = getline(buf, &len_p, stdin);
#else
r = _getline(info, buf, &len_p);
#endif
if (r > 0)
{
if ((*buf)[r - 1] == '\n')
{
(*buf)[r - 1] = '\0'; /* Remove trailing newline. */
r--;
}
info->linecount_flag = 1;
remove_comments(*buf);
build_history_list(info, *buf, info->histcount++);
/* Check if this is a command chain. */
*len = r;
info->cmd_buf = buf;
}
}
return (r);
}
/**
* get_input - Gets a line minus the newline.
* @info: Parameter struct.
*
* Return: Bytes read.
*/
ssize_t get_input(info_t *info)
{
static char *buf; /* The ';' command chain buffer. */
static size_t i, j, len;
ssize_t r = 0;
char **buf_p = &(info->arg);
char *p;
_putchar(BUF_FLUSH);
r = input_buf(info, &buf, &len);
if (r == -1) /* EOF. */
return (-1);
if (len) /* We have commands left in the chain buffer. */
{
j = i; /* Initialize new iterator to current buf position. */
p = buf + i; /* Get pointer for return. */
check_chain(info, buf, &j, i, len);
while (j < len) /* Iterate to semicolon or end. */
{
if (is_chain(info, buf, &j))
break;
j++;
}
i = j + 1; /* Increment past nulled ';'. */
if (i >= len) /* Reached end of buffer? */
{
i = len = 0; /* Reset position and length. */
info->cmd_buf_type = CMD_NORM;
}
*buf_p = p; /* Pass back pointer to current command position. */
return (_strlen(p)); /* Return length of current command. */
}
*buf_p = buf; /* Else not a chain, pass back buffer from _getline(). */
return (r); /* Return length of buffer from _getline(). */
}
/**
* read_buf - Reads a buffer.
* @info: Parameter struct.
* @buf: Buffer.
* @i: Size.
*
* Return: r.
*/
ssize_t read_buf(info_t *info, char *buf, size_t *i)
{
ssize_t r = 0;
if (*i)
return (0);
r = read(info->readfd, buf, READ_BUF_SIZE);
if (r >= 0)
*i = r;
return (r);
}
/**
* _getline - Gets the next line of input from STDIN.
* @info: Parameter struct.
* @ptr: Address of pointer to buffer, preallocated or NULL.
* @length: Size of preallocated ptr buffer if not NULL.
*
* Return: s.
*/
int _getline(info_t *info, char **ptr, size_t *length)
{
static char buf[READ_BUF_SIZE];
static size_t i, len;
size_t k;
ssize_t r = 0, s = 0;
char *p = NULL, *new_p = NULL, *c;
p = *ptr;
if (p && length)
s = *length;
if (i == len)
{
i = len = 0;
}
r = read_buf(info, buf, &len);
if (r == -1 || (r == 0 && len == 0))
return (-1);
c = _strchr(buf + i, '\n');
k = c ? 1 + (unsigned int)(c - buf) : len;
new_p = _realloc(p, s, s ? s + k : k + 1);
if (!new_p) /* Malloc failure! */
return (p ? free(p), -1 : -1);
if (s)
_strncat(new_p, buf + i, k - i);
else
_strncpy(new_p, buf + i, k - i + 1);
s += k - i;
i = k;
p = new_p;
if (length)
*length = s;
*ptr = p;
return (s);
}
/**
* sigintHandler - Blocks ctrl-C.
* @sig_num: The signal number.
*
* Return: void.
*/
void sigintHandler(__attribute__((unused)) int sig_num)
{
_puts("\n");
_puts("$ ");
_putchar(BUF_FLUSH);
}