1 /* read_switch_multiword_via_readline.c -- get an answer from the user, with readline
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.
9 #include "read_switch_multiword_via_readline.h"
11 #include "brkstring.h"
15 #ifdef READLINE_SUPPORT
16 #include <readline/readline.h>
17 #include <readline/history.h>
19 static struct swit
*rl_cmds
;
21 static char *nmh_command_generator(const char *, int);
22 static char **nmh_completion(const char *, int, int);
23 static void initialize_readline(void);
25 static char ansbuf
[BUFSIZ
];
28 * getans, but with readline support
32 read_switch_multiword_via_readline(char *prompt
, struct swit
*ansp
)
36 initialize_readline();
40 ans
= readline(prompt
);
42 * If we get an EOF, return
48 if (ans
[0] == '?' || ans
[0] == '\0') {
50 print_sw(ALL
, ansp
, "", stdout
);
55 strncpy(ansbuf
, ans
, sizeof(ansbuf
));
56 ansbuf
[sizeof(ansbuf
) - 1] = '\0';
59 cpp
= brkstring(ansbuf
, " ", NULL
);
60 switch (smatch(*cpp
, ansp
)) {
65 printf(" -%s unknown. Hit <CR> for help.\n", *cpp
);
74 initialize_readline(void)
76 rl_readline_name
= "Nmh";
77 rl_attempted_completion_function
= nmh_completion
;
81 nmh_completion(const char *text
, int start
, int end
)
86 return rl_completion_matches(text
, nmh_command_generator
);
92 nmh_command_generator(const char *text
, int state
)
94 static int list_index
, len
;
103 while ((name
= rl_cmds
[list_index
].sw
)) {
105 strncpy(buf
, name
, sizeof(buf
));
106 buf
[sizeof(buf
) - 1] = '\0';
107 p
= *brkstring(buf
, " ", NULL
);
108 if (strncmp(p
, text
, len
) == 0)
114 #endif /* READLINE_SUPPORT */