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 #ifdef READLINE_SUPPORT
11 #include <readline/readline.h>
12 #include <readline/history.h>
14 static struct swit
*rl_cmds
;
16 static char *nmh_command_generator(const char *, int);
17 static char **nmh_completion(const char *, int, int);
18 static void initialize_readline(void);
20 static char ansbuf
[BUFSIZ
];
23 * getans, but with readline support
27 read_switch_multiword_via_readline(char *prompt
, struct swit
*ansp
)
31 initialize_readline();
35 ans
= readline(prompt
);
37 * If we get an EOF, return
43 if (ans
[0] == '?' || ans
[0] == '\0') {
45 print_sw(ALL
, ansp
, "", stdout
);
50 strncpy(ansbuf
, ans
, sizeof(ansbuf
));
51 ansbuf
[sizeof(ansbuf
) - 1] = '\0';
54 cpp
= brkstring(ansbuf
, " ", NULL
);
55 switch (smatch(*cpp
, ansp
)) {
60 printf(" -%s unknown. Hit <CR> for help.\n", *cpp
);
69 initialize_readline(void)
71 rl_readline_name
= "Nmh";
72 rl_attempted_completion_function
= nmh_completion
;
76 nmh_completion(const char *text
, int start
, int end
)
81 return rl_completion_matches(text
, nmh_command_generator
);
87 nmh_command_generator(const char *text
, int state
)
89 static int list_index
, len
;
98 while ((name
= rl_cmds
[list_index
].sw
)) {
100 strncpy(buf
, name
, sizeof(buf
));
101 buf
[sizeof(buf
) - 1] = '\0';
102 p
= *brkstring(buf
, " ", NULL
);
103 if (strncmp(p
, text
, len
) == 0)
109 #endif /* READLINE_SUPPORT */