-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinput.c
More file actions
96 lines (81 loc) · 2.66 KB
/
input.c
File metadata and controls
96 lines (81 loc) · 2.66 KB
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
#include "quantis.h"
char *read_command_line(int prompt_len) {
(void)prompt_len;
char *line_buffer = calloc(MAX_LINE, 1);
if (!line_buffer) {
perror("calloc");
return NULL;
}
int len = 0;
history_current = history_count;
while (1) {
char c;
if (read(STDIN_FILENO, &c, 1) <= 0) {
free(line_buffer);
return NULL;
}
if (c == '\t') {
int result = handle_tab_completion(line_buffer, &len);
if (result == 2) {
char *prompt = build_prompt();
printf("%s%s", prompt, line_buffer);
fflush(stdout);
free(prompt);
}
continue;
}
if (c == '\r' || c == '\n') {
line_buffer[len] = '\0';
write(STDOUT_FILENO, "\n", 1);
break;
}
if (c == 127 || c == '\b') {
if (len > 0) {
len--;
line_buffer[len] = '\0';
write(STDOUT_FILENO, "\b \b", 3);
}
continue;
}
if (c == 27) {
char seq[3];
if (read(STDIN_FILENO, &seq[0], 1) != 1) continue;
if (read(STDIN_FILENO, &seq[1], 1) != 1) continue;
if (seq[0] == '[' &&
(seq[1] == 'A' || seq[1] == 'B')) {
int new_history_index = history_current;
if (seq[1] == 'A') {
if (new_history_index > 0) new_history_index--;
} else if (seq[1] == 'B') {
if (new_history_index < history_count)
new_history_index++;
}
if (new_history_index != history_current) {
history_current = new_history_index;
printf("\r\033[K");
fflush(stdout);
char *history_line = NULL;
if (history_current < history_count) {
history_line = history[history_current];
}
if (history_line) {
len = snprintf(line_buffer, MAX_LINE,
"%s", history_line);
} else {
len = 0;
line_buffer[0] = '\0';
}
printf(" %s", line_buffer);
fflush(stdout);
}
}
continue;
}
if (len < MAX_LINE - 1) {
line_buffer[len++] = c;
line_buffer[len] = '\0';
write(STDOUT_FILENO, &c, 1);
}
}
return line_buffer;
}