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.
10 #include "brkstring.h"
14 #ifdef READLINE_SUPPORT
15 #include <readline/readline.h>
16 #include <readline/history.h>
18 static struct swit
*rl_cmds
;
20 static char *nmh_command_generator(const char *, int);
21 static char **nmh_completion(const char *, int, int);
22 static void initialize_readline(void);
24 static char ansbuf
[BUFSIZ
];
27 * getans, but with readline support
31 read_switch_multiword_via_readline(char *prompt
, struct swit
*ansp
)
35 initialize_readline();
39 ans
= readline(prompt
);
41 * If we get an EOF, return
47 if (ans
[0] == '?' || ans
[0] == '\0') {
49 print_sw(ALL
, ansp
, "", stdout
);
54 strncpy(ansbuf
, ans
, sizeof(ansbuf
));
55 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
);
73 initialize_readline(void)
75 rl_readline_name
= "Nmh";
76 rl_attempted_completion_function
= nmh_completion
;
80 nmh_completion(const char *text
, int start
, int end
)
85 return rl_completion_matches(text
, nmh_command_generator
);
91 nmh_command_generator(const char *text
, int state
)
93 static int list_index
, len
;
102 while ((name
= rl_cmds
[list_index
].sw
)) {
104 strncpy(buf
, name
, sizeof(buf
));
105 buf
[sizeof(buf
) - 1] = '\0';
106 p
= *brkstring(buf
, " ", NULL
);
107 if (strncmp(p
, text
, len
) == 0)
113 #endif /* READLINE_SUPPORT */