Removed some dead code, all inside #if 0's. I tried to not remove
[mmh] / sbr / getansreadline.c
1
2 /*
3  * getansreadline.c -- get an answer from the user, with readline
4  *
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.
8  */
9
10 #include <h/mh.h>
11 #include <h/signals.h>
12 #include <setjmp.h>
13 #include <signal.h>
14 #include <errno.h>
15
16 #ifdef READLINE_SUPPORT
17 #include <readline/readline.h>
18 #include <readline/history.h>
19
20 static struct swit *rl_cmds;
21
22 static char *nmh_command_generator(const char *, int);
23 static char **nmh_completion(const char *, int, int);
24 static void initialize_readline(void);
25
26 static char ansbuf[BUFSIZ];
27
28 /*
29  * getans, but with readline support
30  */
31
32 char **
33 getans_via_readline(char *prompt, struct swit *ansp)
34 {
35     char *ans, **cpp;
36
37     initialize_readline();
38     rl_cmds = ansp;
39
40     for (;;) {
41         ans = readline(prompt);
42         /*
43          * If we get an EOF, return
44          */
45
46         if (ans == NULL)
47             return NULL;
48
49         if (ans[0] == '?' || ans[0] == '\0') {
50             printf("Options are:\n");
51             print_sw(ALL, ansp, "", stdout);
52             free(ans);
53             continue;
54         }
55         add_history(ans);
56         strncpy(ansbuf, ans, sizeof(ansbuf));
57         ansbuf[sizeof(ansbuf) - 1] = '\0';
58         cpp = brkstring(ansbuf, " ", NULL);
59         switch (smatch(*cpp, ansp)) {
60             case AMBIGSW:
61                 ambigsw(*cpp, ansp);
62                 continue;
63             case UNKWNSW:
64                 printf(" -%s unknown. Hit <CR> for help.\n", *cpp);
65                 continue;
66             default:
67                 free(ans);
68                 return cpp;
69         }
70         free(ans);
71     }
72 }
73
74 static void
75 initialize_readline(void)
76 {
77     rl_readline_name = "Nmh";
78     rl_attempted_completion_function = nmh_completion;
79 }
80
81 static char **
82 nmh_completion(const char *text, int start, int end)
83 {
84     char **matches;
85
86     NMH_UNUSED (end);
87
88     matches = (char **) NULL;
89
90     if (start == 0)
91         matches = rl_completion_matches(text, nmh_command_generator);
92
93     return matches;
94 }
95
96 static char *
97 nmh_command_generator(const char *text, int state)
98 {
99     static int list_index, len;
100     char *name, *p;
101     char buf[256];
102
103     if (!state) {
104         list_index = 0;
105         len = strlen(text);
106     }
107
108     while ((name = rl_cmds[list_index].sw)) {
109         list_index++;
110         strncpy(buf, name, sizeof(buf));
111         buf[sizeof(buf) - 1] = '\0';
112         p = *brkstring(buf, " ", NULL);
113         if (strncmp(p, text, len) == 0)
114         return strdup(p);
115     }
116
117     return NULL;
118 }
119 #endif /* READLINE_SUPPORT */
120