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>
15 #ifdef READLINE_SUPPORT
16 #include <readline/readline.h>
17 #include <readline/history.h>
19 static struct swit
*rl_cmds
;
21 static char *nmh_command_generator(const char *, int);
22 static char **nmh_completion(const char *, int, int);
23 static void initialize_readline(void);
25 static char ansbuf
[BUFSIZ
];
28 * getans, but with readline support
32 getans_via_readline(char *prompt
, struct swit
*ansp
)
36 initialize_readline();
40 ans
= readline(prompt
);
42 * If we get an EOF, return
48 if (ans
[0] == '?' || ans
[0] == '\0') {
49 printf("Options are:\n");
50 print_sw(ALL
, ansp
, "", stdout
);
55 strncpy(ansbuf
, ans
, sizeof(ansbuf
));
56 ansbuf
[sizeof(ansbuf
) - 1] = '\0';
57 cpp
= brkstring(ansbuf
, " ", NULL
);
58 switch (smatch(*cpp
, ansp
)) {
63 printf(" -%s unknown. Hit <CR> for help.\n", *cpp
);
74 initialize_readline(void)
76 rl_readline_name
= "Nmh";
77 rl_attempted_completion_function
= nmh_completion
;
81 nmh_completion(const char *text
, int start
, int end
)
87 matches
= (char **) NULL
;
90 matches
= rl_completion_matches(text
, nmh_command_generator
);
96 nmh_command_generator(const char *text
, int state
)
98 static int list_index
, len
;
107 while ((name
= rl_cmds
[list_index
].sw
)) {
109 strncpy(buf
, name
, sizeof(buf
));
110 buf
[sizeof(buf
) - 1] = '\0';
111 p
= *brkstring(buf
, " ", NULL
);
112 if (strncmp(p
, text
, len
) == 0)
118 #endif /* READLINE_SUPPORT */