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"
33 #define MAXEDITLINE 512
39 extern char const * progname;
42 * globals, private prototypes
45 static int ntp_readline_initted;
46 static char * lineedit_prompt;
51 # define H_SETSIZE H_EVENT
53 static EditLine * ntp_el;
54 static History * ntp_hist;
57 char * ntp_prompt_callback(EditLine *);
58 #endif /* LE_EDITLINE */
62 * ntp_readline_init - setup, set or reset prompt string
75 free(lineedit_prompt);
76 lineedit_prompt = estrdup(prompt);
82 # if 4 == EL_INIT_ARGS
83 ntp_el = el_init(progname, stdin, stdout, stderr);
85 ntp_el = el_init(progname, stdin, stdout);
89 el_set(ntp_el, EL_PROMPT, ntp_prompt_callback);
90 el_set(ntp_el, EL_EDITOR, "emacs");
92 ntp_hist = history_init();
94 if (NULL == ntp_hist) {
96 mfprintf(stderr, "history_init(): %m\n");
107 history(ntp_hist, &hev, H_SETSIZE, 128);
109 el_set(ntp_el, EL_HIST, history,
111 /* use any .editrc */
112 el_source(ntp_el, NULL);
117 #endif /* LE_EDITLINE */
119 ntp_readline_initted = success;
126 * ntp_readline_uninit - release resources
138 history_end(ntp_hist);
141 #endif /* LE_EDITLINE */
143 if (lineedit_prompt) {
144 free(lineedit_prompt);
145 lineedit_prompt = NULL;
148 ntp_readline_initted = 0;
153 * ntp_readline - read a line with the line editor available
155 * The string returned must be released with free()
165 char line_buf[MAXEDITLINE];
171 if (!ntp_readline_initted)
177 line = readline(lineedit_prompt ? lineedit_prompt : "");
182 *pcount = strlen(line);
184 #endif /* LE_READLINE */
187 cline = el_gets(ntp_el, pcount);
190 history(ntp_hist, &hev, H_ENTER, cline);
191 line = estrdup(cline);
192 } else if (*pcount == -1) {
197 #endif /* LE_EDITLINE */
201 if (lineedit_prompt) {
204 * work around problem mixing
210 fputs(lineedit_prompt, stderr);
214 line = fgets(line_buf, sizeof(line_buf), stdin);
215 if (NULL != line && *line) {
216 *pcount = strlen(line);
217 line = estrdup(line);
233 * ntp_prompt_callback - return prompt string to el_gets()
242 return lineedit_prompt;
244 #endif /* LE_EDITLINE */