3 * getansreadline.c -- get an answer from the user, with readline
5 * This code is Copyright (c) 2012, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/signals.h>
16 #ifdef READLINE_SUPPORT
17 #include <readline/readline.h>
18 #include <readline/history.h>
20 static struct swit *rl_cmds;
22 static char *nmh_command_generator(const char *, int);
23 static char **nmh_completion(const char *, int, int);
24 static void initialize_readline(void);
26 static char ansbuf[BUFSIZ];
29 * getans, but with readline support
33 getans_via_readline(char *prompt, struct swit *ansp)
37 initialize_readline();
41 ans = readline(prompt);
43 * If we get an EOF, return
49 if (ans[0] == '?' || ans[0] == '\0') {
50 printf("Options are:\n");
51 print_sw(ALL, ansp, "", stdout);
56 strncpy(ansbuf, ans, sizeof(ansbuf));
57 ansbuf[sizeof(ansbuf) - 1] = '\0';
58 cpp = brkstring(ansbuf, " ", NULL);
59 switch (smatch(*cpp, ansp)) {
64 printf(" -%s unknown. Hit <CR> for help.\n", *cpp);
75 initialize_readline(void)
77 rl_readline_name = "Nmh";
78 rl_attempted_completion_function = nmh_completion;
82 nmh_completion(const char *text, int start, int end)
88 matches = (char **) NULL;
91 matches = rl_completion_matches(text, nmh_command_generator);
97 nmh_command_generator(const char *text, int state)
99 static int list_index, len;
108 while ((name = rl_cmds[list_index].sw)) {
110 strncpy(buf, name, sizeof(buf));
111 buf[sizeof(buf) - 1] = '\0';
112 p = *brkstring(buf, " ", NULL);
113 if (strncmp(p, text, len) == 0)
119 #endif /* READLINE_SUPPORT */