-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadline.c
381 lines (322 loc) · 9.28 KB
/
readline.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <unistd.h>
#include "defs.h"
#include "readline.h"
static char buffer[BUFLEN];
#ifndef SHELL_NO_INTERACTIVE
static char ansi_sequence_buff[ANSI_SEQUENCE_BUFF_SIZE] = { END_STRING };
static const char EVENT_DESIGNATOR_LAST_CMD_STR[] = "!!";
static const char EVENT_DESIGNATOR_LAST_CMD_LEN = 2;
static const char EVENT_DESIGNATOR_LAST_NTH_CMD_STR[] = "!-";
static const char EVENT_DESIGNATOR_LAST_NTH_CMD_LEN = 2;
static const int TAB_TO_SPACE_EQUIVALENCE = 4;
#endif
static void
echo_eval_symbol()
{
fprintf(stdout, "%s", "$ ");
fflush(stdout);
}
static void
echo_prompt(const char *prompt)
{
fprintf(stdout, "%s %s %s\n", COLOR_RED, prompt, COLOR_RESET);
echo_eval_symbol();
fflush(stdout);
}
// reads a line from the standard input
// and prints the prompt
char *
read_line(const char *prompt)
{
int i = 0, c = 0;
#ifndef SHELL_NO_INTERACTIVE
if (isatty(1)) {
echo_prompt(prompt);
}
#else
MARK_UNUSED(prompt);
MARK_UNUSED_ALWAYS(echo_prompt);
#endif
memset(buffer, 0, BUFLEN);
c = getchar();
while (c != END_LINE && c != EOF) {
buffer[i++] = c;
c = getchar();
}
// if the user press ctrl+D
// just exit normally
if (c == EOF)
return NULL;
buffer[i] = END_STRING;
return buffer;
}
#ifndef SHELL_NO_INTERACTIVE
static void
echo_buffer_with_prompt()
{
printf(ANSI_CODE_CLEAN_LINE /* + */
ANSI_CODE_MOVE_CURSOR_TO_LINE_START);
echo_eval_symbol();
printf("%s", buffer);
fflush(stdout);
}
static void
handle_history_switch(char *buffer,
int *buffer_index,
char *ansi_sequence_buff,
char char_read,
bool *just_handled_arrow,
history_data_t *history)
{
memset(ansi_sequence_buff, END_STRING, ANSI_SEQUENCE_BUFF_SIZE);
ansi_sequence_buff[0] = char_read;
if (read(STDIN_FILENO, &ansi_sequence_buff[1], 2) == 2) {
if (ansi_sequence_buff[1] == BEGIN_ANSI_FUNCTION_CHARACTER) {
switch (ansi_sequence_buff[2]) {
case ANSI_FUNCTION_CURSOR_UP_CHARACTER:
memset(buffer, END_STRING, BUFLEN);
*buffer_index = 0;
history_move_backwards(history,
just_handled_arrow,
buffer,
buffer_index,
&echo_buffer_with_prompt);
*just_handled_arrow = true;
break;
case ANSI_FUNCTION_CURSOR_DOWN_CHARACTER:
memset(buffer, END_STRING, BUFLEN);
*buffer_index = 0;
history_move_forwards(history,
just_handled_arrow,
buffer,
buffer_index,
&echo_buffer_with_prompt);
break;
}
}
}
}
static void
handle_end_line_read(int *buffer_index, bool *should_stop, history_data_t *history)
{
if ((*buffer_index) + 1 < BUFLEN) {
buffer[(*buffer_index) + 1] = END_STRING;
}
history_add_entry(history, buffer);
fflush(stdout);
*should_stop = true;
}
static void
delete_one_buffered_character(int *buffer_index)
{
if (*buffer_index > 0) {
(*buffer_index)--;
buffer[*buffer_index] = END_STRING;
printf(ANSI_CODE_MOVE_CURSOR_ONE_CHARACTER_TO_LEFT); // Move 1 to left, for cleaning
printf("%c",
SPACE); // Overwrite with 1 space so it appears as cleared
printf(ANSI_CODE_MOVE_CURSOR_ONE_CHARACTER_TO_LEFT); // Move 1 to left again, for positioning
fflush(stdout);
}
}
static void
echo(char *char_read)
{
write(STDOUT_FILENO, char_read, 1 * sizeof(char));
fflush(stdout);
}
static void
load_buffer_and_echo(bool *just_handled_arrow,
char *buffer,
int *buffer_index,
char *char_read)
{
if (!(*just_handled_arrow)) {
if (*char_read == TAB) {
char space_char = SPACE;
for (int i = 0; i < TAB_TO_SPACE_EQUIVALENCE; i++) {
buffer[*buffer_index] = space_char;
(*buffer_index)++;
echo(&space_char);
}
} else {
buffer[*buffer_index] = *char_read;
(*buffer_index)++;
echo(char_read);
}
}
}
static void
echo_buffer_from_position(char *buffer, int buffer_index, int start_position)
{
for (int i = start_position; i <= buffer_index; i++) {
write(STDOUT_FILENO, &buffer[i], 1 * sizeof(char));
}
fflush(stdout);
}
static bool
check_and_replace_last_cmd_designator(char *buffer,
int *buffer_index,
history_data_t *history,
char *char_read)
{
bool was_replaced = false;
char *token = strstr(buffer, EVENT_DESIGNATOR_LAST_CMD_STR);
while (token != NULL && strlen(token) != 0) {
for (int i = 0; i < EVENT_DESIGNATOR_LAST_CMD_LEN; i++) {
delete_one_buffered_character(buffer_index);
}
int original_buff_index = *buffer_index;
history_append_last_cmd(history, buffer, buffer_index, BUFLEN);
echo_buffer_from_position(buffer,
*buffer_index,
original_buff_index);
token += EVENT_DESIGNATOR_LAST_CMD_LEN * sizeof(char);
token = strstr(token, EVENT_DESIGNATOR_LAST_CMD_STR);
was_replaced = true;
}
if (was_replaced) {
*char_read = SPACE;
}
return was_replaced;
}
static bool
check_and_replace_last_nth_cmd_designator(char *buffer,
int *buffer_index,
history_data_t *history,
char *char_read)
{
bool was_replaced = false;
char *token = strstr(buffer, EVENT_DESIGNATOR_LAST_NTH_CMD_STR);
char cmd_number_str[SMALL_BUFLEN];
while (token != NULL && strlen(token) != 0) {
token += EVENT_DESIGNATOR_LAST_CMD_LEN * sizeof(char);
char *token_end = strchr(token, SPACE);
if (token_end == NULL) {
token_end = strchr(token, END_STRING);
}
if (token_end == NULL) {
break;
}
int token_len_diff = strlen(token) - strlen(token_end);
int i = 0;
for (i = 0; i < MIN(token_len_diff, SMALL_BUFLEN); i++) {
cmd_number_str[i] = token[i];
}
if (i < SMALL_BUFLEN) {
cmd_number_str[i] = END_LINE;
}
int cmd_number = atoi(cmd_number_str);
for (int i = 0;
i < (token_len_diff + EVENT_DESIGNATOR_LAST_NTH_CMD_LEN);
i++) {
delete_one_buffered_character(buffer_index);
}
int original_buff_index = *buffer_index;
history_append_last_nth_cmd(
history, cmd_number, buffer, buffer_index, BUFLEN);
echo_buffer_from_position(buffer,
*buffer_index,
original_buff_index);
token = strstr(token, EVENT_DESIGNATOR_LAST_CMD_STR);
was_replaced = true;
}
if (was_replaced) {
*char_read = SPACE;
}
return was_replaced;
}
static bool
check_for_event_designators_and_replace(char *buffer,
int *buffer_index,
history_data_t *history,
char *char_read)
{
bool was_replaced_last = false;
bool was_replaced_last_nth = false;
if (history_is_empty(history)) {
return false;
}
was_replaced_last = check_and_replace_last_cmd_designator(
buffer, buffer_index, history, char_read);
was_replaced_last_nth = check_and_replace_last_nth_cmd_designator(
buffer, buffer_index, history, char_read);
return was_replaced_last || was_replaced_last_nth;
}
static bool
is_character_eof(char char_read)
{
return (unsigned int) char_read == EOT_ASCII_CODE;
}
#endif
char *
read_line_non_canonical(const char *prompt,
bool *just_handled_arrow_action,
history_data_t *history)
{
#ifndef SHELL_NO_INTERACTIVE
int i = 0;
char char_read = 0;
bool should_stop = false;
bool was_replaced = false;
echo_prompt(prompt);
memset(buffer, END_STRING, BUFLEN);
memset(ansi_sequence_buff, END_STRING, ANSI_SEQUENCE_BUFF_SIZE);
read(STDIN_FILENO, &char_read, 1 * sizeof(char));
while (!is_character_eof(char_read) && !should_stop) {
switch (char_read) {
case BEGIN_ANSI_SEQUENCE_CHARACTER:
handle_history_switch(buffer,
&i,
ansi_sequence_buff,
char_read,
just_handled_arrow_action,
history);
read(STDIN_FILENO, &char_read, 1 * sizeof(char));
break;
case END_LINE:
handle_end_line_read(&i, &should_stop, history);
echo(&char_read);
*just_handled_arrow_action = false;
break;
case BACKSPACE:
delete_one_buffered_character(&i);
*just_handled_arrow_action = false;
read(STDIN_FILENO, &char_read, 1 * sizeof(char));
break;
case SPACE:
case TAB:
was_replaced = check_for_event_designators_and_replace(
buffer, &i, history, &char_read);
*just_handled_arrow_action = false;
if (was_replaced) {
read(STDIN_FILENO, &char_read, 1 * sizeof(char));
}
/* Expected */
/* fall through */
default:
if (was_replaced) {
break;
}
load_buffer_and_echo(just_handled_arrow_action,
buffer,
&i,
&char_read);
*just_handled_arrow_action = false;
read(STDIN_FILENO, &char_read, 1 * sizeof(char));
break;
}
was_replaced = false;
}
// if the user press ctrl+D
// just exit normally
if (is_character_eof(char_read))
return NULL;
return buffer;
#else
MARK_UNUSED_ALWAYS(prompt);
MARK_UNUSED_ALWAYS(just_handled_arrow_action);
MARK_UNUSED_ALWAYS(history);
return NULL;
#endif
}