2 * ntp_lineedit.c - generic interface to various line editing libs
13 #if defined(HAVE_READLINE_HISTORY) && \
14 (!defined(HAVE_READLINE_HISTORY_H) || \
15 !defined(HAVE_READLINE_READLINE_H))
16 # undef HAVE_READLINE_HISTORY
18 #if defined(HAVE_READLINE_HISTORY)
19 # include <readline/readline.h>
20 # include <readline/history.h>
22 #elif defined(HAVE_HISTEDIT_H)
23 # include <histedit.h>
30 #include "ntp_stdlib.h"
31 #include "ntp_lineedit.h"
34 #define MAXEDITLINE 512
40 extern char const * progname;
43 * globals, private prototypes
46 static int ntp_readline_initted;
47 static char * lineedit_prompt;
52 # define H_SETSIZE H_EVENT
54 static EditLine * ntp_el;
55 static History * ntp_hist;
58 char * ntp_prompt_callback(EditLine *);
59 #endif /* LE_EDITLINE */
63 * ntp_readline_init - setup, set or reset prompt string
76 free(lineedit_prompt);
77 lineedit_prompt = estrdup(prompt);
83 # if 4 == EL_INIT_ARGS
84 ntp_el = el_init(progname, stdin, stdout, stderr);
86 ntp_el = el_init(progname, stdin, stdout);
90 el_set(ntp_el, EL_PROMPT, ntp_prompt_callback);
91 el_set(ntp_el, EL_EDITOR, "emacs");
93 ntp_hist = history_init();
95 if (NULL == ntp_hist) {
97 mfprintf(stderr, "history_init(): %m\n");
108 history(ntp_hist, &hev, H_SETSIZE, 128);
110 el_set(ntp_el, EL_HIST, history,
112 /* use any .editrc */
113 el_source(ntp_el, NULL);
118 #endif /* LE_EDITLINE */
120 ntp_readline_initted = success;
127 * ntp_readline_uninit - release resources
139 history_end(ntp_hist);
142 #endif /* LE_EDITLINE */
144 if (lineedit_prompt) {
145 free(lineedit_prompt);
146 lineedit_prompt = NULL;
149 ntp_readline_initted = 0;
154 * ntp_readline - read a line with the line editor available
156 * The string returned must be released with free()
166 char line_buf[MAXEDITLINE];
172 if (!ntp_readline_initted)
178 line = readline(lineedit_prompt ? lineedit_prompt : "");
183 *pcount = strlen(line);
185 #endif /* LE_READLINE */
188 cline = el_gets(ntp_el, pcount);
191 history(ntp_hist, &hev, H_ENTER, cline);
192 line = estrdup(cline);
193 } else if (*pcount == -1) {
198 #endif /* LE_EDITLINE */
202 if (lineedit_prompt) {
205 * work around problem mixing
211 fputs(lineedit_prompt, stderr);
215 line = fgets(line_buf, sizeof(line_buf), stdin);
216 if (NULL != line && *line) {
217 *pcount = (int)strlen(line); /* cannot overflow here */
218 line = estrdup(line);
234 * ntp_prompt_callback - return prompt string to el_gets()
243 return lineedit_prompt;
245 #endif /* LE_EDITLINE */