]> diplodocus.org Git - nmh/blob - sbr/read_switch_multiword_via_readline.c
mhbuildsbr.c: Flip logic, moving goto to then-block; no need for else.
[nmh] / sbr / read_switch_multiword_via_readline.c
1 /* read_switch_multiword_via_readline.c -- get an answer from the user, with readline
2 *
3 * This code is Copyright (c) 2012, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9
10 #ifdef READLINE_SUPPORT
11 #include <readline/readline.h>
12 #include <readline/history.h>
13
14 static struct swit *rl_cmds;
15
16 static char *nmh_command_generator(const char *, int);
17 static char **nmh_completion(const char *, int, int);
18 static void initialize_readline(void);
19
20 static char ansbuf[BUFSIZ];
21
22 /*
23 * getans, but with readline support
24 */
25
26 char **
27 read_switch_multiword_via_readline(char *prompt, struct swit *ansp)
28 {
29 char *ans, **cpp;
30
31 initialize_readline();
32 rl_cmds = ansp;
33
34 for (;;) {
35 ans = readline(prompt);
36 /*
37 * If we get an EOF, return
38 */
39
40 if (ans == NULL)
41 return NULL;
42
43 if (ans[0] == '?' || ans[0] == '\0') {
44 puts("Options are:");
45 print_sw(ALL, ansp, "", stdout);
46 free(ans);
47 continue;
48 }
49 add_history(ans);
50 strncpy(ansbuf, ans, sizeof(ansbuf));
51 ansbuf[sizeof(ansbuf) - 1] = '\0';
52 cpp = brkstring(ansbuf, " ", NULL);
53 switch (smatch(*cpp, ansp)) {
54 case AMBIGSW:
55 ambigsw(*cpp, ansp);
56 continue;
57 case UNKWNSW:
58 printf(" -%s unknown. Hit <CR> for help.\n", *cpp);
59 continue;
60 default:
61 free(ans);
62 return cpp;
63 }
64 free(ans);
65 }
66 }
67
68 static void
69 initialize_readline(void)
70 {
71 rl_readline_name = "Nmh";
72 rl_attempted_completion_function = nmh_completion;
73 }
74
75 static char **
76 nmh_completion(const char *text, int start, int end)
77 {
78 char **matches;
79
80 NMH_UNUSED (end);
81
82 matches = (char **) NULL;
83
84 if (start == 0)
85 matches = rl_completion_matches(text, nmh_command_generator);
86
87 return matches;
88 }
89
90 static char *
91 nmh_command_generator(const char *text, int state)
92 {
93 static int list_index, len;
94 char *name, *p;
95 char buf[256];
96
97 if (!state) {
98 list_index = 0;
99 len = strlen(text);
100 }
101
102 while ((name = rl_cmds[list_index].sw)) {
103 list_index++;
104 strncpy(buf, name, sizeof(buf));
105 buf[sizeof(buf) - 1] = '\0';
106 p = *brkstring(buf, " ", NULL);
107 if (strncmp(p, text, len) == 0)
108 return strdup(p);
109 }
110
111 return NULL;
112 }
113 #endif /* READLINE_SUPPORT */