Commit | Line | Data |
---|---|---|
4a11f206 PP |
1 | /* Standard include files. stdio.h is required. */ |
2 | #include <stdlib.h> | |
3 | #include <unistd.h> | |
4 | #include <string.h> | |
5 | ||
6 | /* Used for select(2) */ | |
7 | #include <sys/types.h> | |
8 | #include <sys/select.h> | |
9 | ||
10 | #include <errno.h> | |
11 | #include <stdio.h> | |
12 | ||
13 | /* Standard readline include files. */ | |
14 | #if defined (READLINE_LIBRARY) | |
15 | # include "readline.h" | |
16 | # include "history.h" | |
17 | #else | |
18 | # include <readline/readline.h> | |
19 | # include <readline/history.h> | |
20 | #endif | |
21 | ||
22 | extern int errno; | |
23 | ||
24 | static void cb_linehandler (char *); | |
25 | ||
26 | int running; | |
27 | const char *prompt = "rltest$ "; | |
28 | ||
29 | /* Callback function called for each line when accept-line executed, EOF | |
30 | seen, or EOF character read. This sets a flag and returns; it could | |
31 | also call exit(3). */ | |
32 | static void | |
33 | cb_linehandler (char *line) | |
34 | { | |
35 | /* Can use ^D (stty eof) or `exit' to exit. */ | |
36 | if (line == NULL || strcmp (line, "exit") == 0) | |
37 | { | |
38 | if (line == 0) | |
39 | printf ("\n"); | |
40 | printf ("exit\n"); | |
41 | /* This function needs to be called to reset the terminal settings, | |
42 | and calling it from the line handler keeps one extra prompt from | |
43 | being displayed. */ | |
44 | rl_callback_handler_remove (); | |
45 | ||
46 | running = 0; | |
47 | } | |
48 | else | |
49 | { | |
50 | if (*line) | |
51 | add_history (line); | |
52 | printf ("input line: %s\n", line); | |
53 | free (line); | |
54 | } | |
55 | } | |
56 | ||
57 | int | |
58 | main (int c, char **v) | |
59 | { | |
60 | fd_set fds; | |
61 | int r; | |
62 | ||
63 | /* Install the line handler. */ | |
64 | rl_callback_handler_install (prompt, cb_linehandler); | |
65 | ||
66 | /* Enter a simple event loop. This waits until something is available | |
67 | to read on readline's input stream (defaults to standard input) and | |
68 | calls the builtin character read callback to read it. It does not | |
69 | have to modify the user's terminal settings. */ | |
70 | running = 1; | |
71 | while (running) | |
72 | { | |
73 | FD_ZERO (&fds); | |
74 | FD_SET (fileno (rl_instream), &fds); | |
75 | ||
76 | r = select (FD_SETSIZE, &fds, NULL, NULL, NULL); | |
77 | if (r < 0 && errno != EINTR) | |
78 | { | |
79 | perror ("rltest: select"); | |
80 | rl_callback_handler_remove (); | |
81 | break; | |
82 | } | |
83 | ||
84 | if (FD_ISSET (fileno (rl_instream), &fds)) | |
85 | rl_callback_read_char (); | |
86 | } | |
87 | ||
88 | printf ("rltest: Event loop has exited\n"); | |
89 | return 0; | |
90 | } |