2 * read_switch_multiword_via_readline.c -- get an answer from the user, with readline
4 * This code is Copyright (c) 2012, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #ifdef READLINE_SUPPORT
12 #include <readline/readline.h>
13 #include <readline/history.h>
15 static struct swit
*rl_cmds
;
17 static char *nmh_command_generator(const char *, int);
18 static char **nmh_completion(const char *, int, int);
19 static void initialize_readline(void);
21 static char ansbuf
[BUFSIZ
];
24 * getans, but with readline support
28 read_switch_multiword_via_readline(char *prompt
, struct swit
*ansp
)
32 initialize_readline();
36 ans
= readline(prompt
);
38 * If we get an EOF, return
44 if (ans
[0] == '?' || ans
[0] == '\0') {
46 print_sw(ALL
, ansp
, "", stdout
);
51 strncpy(ansbuf
, ans
, sizeof(ansbuf
));
52 ansbuf
[sizeof(ansbuf
) - 1] = '\0';
53 cpp
= brkstring(ansbuf
, " ", NULL
);
54 switch (smatch(*cpp
, ansp
)) {
59 printf(" -%s unknown. Hit <CR> for help.\n", *cpp
);
70 initialize_readline(void)
72 rl_readline_name
= "Nmh";
73 rl_attempted_completion_function
= nmh_completion
;
77 nmh_completion(const char *text
, int start
, int end
)
83 matches
= (char **) NULL
;
86 matches
= rl_completion_matches(text
, nmh_command_generator
);
92 nmh_command_generator(const char *text
, int state
)
94 static int list_index
, len
;
103 while ((name
= rl_cmds
[list_index
].sw
)) {
105 strncpy(buf
, name
, sizeof(buf
));
106 buf
[sizeof(buf
) - 1] = '\0';
107 p
= *brkstring(buf
, " ", NULL
);
108 if (strncmp(p
, text
, len
) == 0)
114 #endif /* READLINE_SUPPORT */