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>
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 getans_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') {
48 printf("Options are:\n");
49 print_sw(ALL
, ansp
, "", stdout
);
54 strncpy(ansbuf
, ans
, sizeof(ansbuf
));
55 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
);
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
)
86 matches
= (char **) NULL
;
89 matches
= rl_completion_matches(text
, nmh_command_generator
);
95 nmh_command_generator(const char *text
, int state
)
97 static int list_index
, len
;
106 while ((name
= rl_cmds
[list_index
].sw
)) {
108 strncpy(buf
, name
, sizeof(buf
));
109 buf
[sizeof(buf
) - 1] = '\0';
110 p
= *brkstring(buf
, " ", NULL
);
111 if (strncmp(p
, text
, len
) == 0)
117 #endif /* READLINE_SUPPORT */