3 * getansreadline.c -- get an answer from the user, with readline
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.
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 getans_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') {
46 printf("Options are:\n");
47 print_sw(ALL
, ansp
, "", stdout
);
52 strncpy(ansbuf
, ans
, sizeof(ansbuf
));
53 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
);
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
)
84 matches
= (char **) NULL
;
87 matches
= rl_completion_matches(text
, nmh_command_generator
);
93 nmh_command_generator(const char *text
, int state
)
95 static int list_index
, len
;
104 while ((name
= rl_cmds
[list_index
].sw
)) {
106 strncpy(buf
, name
, sizeof(buf
));
107 buf
[sizeof(buf
) - 1] = '\0';
108 p
= *brkstring(buf
, " ", NULL
);
109 if (strncmp(p
, text
, len
) == 0)
115 #endif /* READLINE_SUPPORT */