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.
11 #include <h/signals.h>
13 #ifdef READLINE_SUPPORT
14 #include <readline/readline.h>
15 #include <readline/history.h>
17 static struct swit
*rl_cmds
;
19 static char *nmh_command_generator(const char *, int);
20 static char **nmh_completion(const char *, int, int);
21 static void initialize_readline(void);
23 static char ansbuf
[BUFSIZ
];
26 * getans, but with readline support
30 getans_via_readline(char *prompt
, struct swit
*ansp
)
34 initialize_readline();
38 ans
= readline(prompt
);
40 * If we get an EOF, return
46 if (ans
[0] == '?' || ans
[0] == '\0') {
47 printf("Options are:\n");
48 print_sw(ALL
, ansp
, "", stdout
);
53 strncpy(ansbuf
, ans
, sizeof(ansbuf
));
54 ansbuf
[sizeof(ansbuf
) - 1] = '\0';
55 cpp
= brkstring(ansbuf
, " ", NULL
);
56 switch (smatch(*cpp
, ansp
)) {
61 printf(" -%s unknown. Hit <CR> for help.\n", *cpp
);
72 initialize_readline(void)
74 rl_readline_name
= "Nmh";
75 rl_attempted_completion_function
= nmh_completion
;
79 nmh_completion(const char *text
, int start
, int end
)
85 matches
= (char **) NULL
;
88 matches
= rl_completion_matches(text
, nmh_command_generator
);
94 nmh_command_generator(const char *text
, int state
)
96 static int list_index
, len
;
105 while ((name
= rl_cmds
[list_index
].sw
)) {
107 strncpy(buf
, name
, sizeof(buf
));
108 buf
[sizeof(buf
) - 1] = '\0';
109 p
= *brkstring(buf
, " ", NULL
);
110 if (strncmp(p
, text
, len
) == 0)
116 #endif /* READLINE_SUPPORT */