1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <sys/select.h> |
---|
4 | #include <editline/readline.h> |
---|
5 | |
---|
6 | static char *line; |
---|
7 | |
---|
8 | static void rlhandler(char *text) |
---|
9 | { |
---|
10 | line = text; |
---|
11 | rl_callback_handler_remove(); |
---|
12 | } |
---|
13 | |
---|
14 | int main(void) |
---|
15 | { |
---|
16 | int has_input = 0; |
---|
17 | fd_set selectset; |
---|
18 | struct timeval *timeoutp = NULL; |
---|
19 | char *not_done_reading = ""; |
---|
20 | |
---|
21 | do { |
---|
22 | line = not_done_reading; |
---|
23 | FD_ZERO(&selectset); |
---|
24 | rl_callback_handler_install(">>> ", rlhandler); |
---|
25 | while (line == not_done_reading) { |
---|
26 | while (!has_input) { |
---|
27 | FD_SET(fileno(stdin), &selectset); |
---|
28 | has_input = select(fileno(stdin) + 1, &selectset, |
---|
29 | NULL, NULL, timeoutp); |
---|
30 | } |
---|
31 | if (has_input > 0) { |
---|
32 | rl_callback_read_char(); |
---|
33 | } |
---|
34 | } |
---|
35 | if (line) { |
---|
36 | printf("You typed: %s\n", line); |
---|
37 | add_history(line); |
---|
38 | free(line); |
---|
39 | } |
---|
40 | } while (line); |
---|
41 | |
---|
42 | return 0; |
---|
43 | } |
---|