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.
12 #ifdef READLINE_SUPPORT
13 #include <readline/readline.h>
14 #include <readline/history.h>
16 static struct swit
*rl_cmds
;
18 static char *nmh_command_generator(const char *, int);
19 static char **nmh_completion(const char *, int, int);
20 static void initialize_readline(void);
22 static char ansbuf
[BUFSIZ
];
25 * getans, but with readline support
29 read_switch_multiword_via_readline(char *prompt
, struct swit
*ansp
)
33 initialize_readline();
37 ans
= readline(prompt
);
39 * If we get an EOF, return
45 if (ans
[0] == '?' || ans
[0] == '\0') {
47 print_sw(ALL
, ansp
, "", stdout
);
52 strncpy(ansbuf
, ans
, sizeof(ansbuf
));
53 ansbuf
[sizeof(ansbuf
) - 1] = '\0';
56 cpp
= brkstring(ansbuf
, " ", NULL
);
57 switch (smatch(*cpp
, ansp
)) {
62 printf(" -%s unknown. Hit <CR> for help.\n", *cpp
);
71 initialize_readline(void)
73 rl_readline_name
= "Nmh";
74 rl_attempted_completion_function
= nmh_completion
;
78 nmh_completion(const char *text
, int start
, int end
)
83 return rl_completion_matches(text
, nmh_command_generator
);
89 nmh_command_generator(const char *text
, int state
)
91 static int list_index
, len
;
100 while ((name
= rl_cmds
[list_index
].sw
)) {
102 strncpy(buf
, name
, sizeof(buf
));
103 buf
[sizeof(buf
) - 1] = '\0';
104 p
= *brkstring(buf
, " ", NULL
);
105 if (strncmp(p
, text
, len
) == 0)
111 #endif /* READLINE_SUPPORT */