4 * ntp_lineedit.c - generic interface to various line editing libs
15 #if defined(HAVE_READLINE_HISTORY)
16 # include <readline/readline.h>
17 # include <readline/history.h>
19 # if defined(HAVE_HISTEDIT_H)
20 # include <histedit.h>
25 #include "ntp_stdlib.h"
26 #include "ntp_lineedit.h"
28 #define MAXEDITLINE 512
34 extern char * progname
;
37 * globals, private prototypes
40 static int ntp_readline_initted
;
41 static char * lineedit_prompt
;
44 #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H)
46 static EditLine
* ntp_el
;
47 static History
* ntp_hist
;
50 char * ntp_prompt_callback(EditLine
*);
52 #endif /* !HAVE_READLINE_HISTORY_H && HAVE_HISTEDIT_H */
56 * ntp_readline_init - setup, set or reset prompt string
69 free(lineedit_prompt
);
70 lineedit_prompt
= estrdup(prompt
);
73 #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H)
77 ntp_el
= el_init(progname
, stdin
, stdout
, stderr
);
80 el_set(ntp_el
, EL_PROMPT
, ntp_prompt_callback
);
81 el_set(ntp_el
, EL_EDITOR
, "emacs");
83 ntp_hist
= history_init();
85 if (NULL
== ntp_hist
) {
87 fprintf(stderr
, "history_init(): %s\n",
97 memset(&hev
, 0, sizeof hev
);
99 history(ntp_hist
, &hev
, H_SETSIZE
, 128);
101 el_set(ntp_el
, EL_HIST
, history
, ntp_hist
);
103 /* use any .editrc */
104 el_source(ntp_el
, NULL
);
110 #endif /* !HAVE_READLINE_HISTORY && HAVE_HISTEDIT_H */
112 ntp_readline_initted
= success
;
119 * ntp_readline_uninit - release resources
126 #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H)
132 history_end(ntp_hist
);
136 #endif /* !HAVE_READLINE_HISTORY && HAVE_HISTEDIT_H */
138 if (lineedit_prompt
) {
139 free(lineedit_prompt
);
140 lineedit_prompt
= NULL
;
143 ntp_readline_initted
= 0;
148 * ntp_readline - read a line with the line editor available
150 * The string returned must be released with free()
158 #if !defined(HAVE_READLINE_HISTORY) && !defined(HAVE_HISTEDIT_H)
159 char line_buf
[MAXEDITLINE
];
161 #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H)
166 if (!ntp_readline_initted
)
171 #if defined(HAVE_READLINE_HISTORY)
173 line
= readline(lineedit_prompt
? lineedit_prompt
: "");
177 *pcount
= strlen(line
);
184 #endif /* HAVE_READLINE_HISTORY */
186 #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H)
188 cline
= el_gets(ntp_el
, pcount
);
190 if (NULL
!= cline
&& *cline
) {
191 history(ntp_hist
, &hev
, H_ENTER
, cline
);
192 *pcount
= strlen(cline
);
193 line
= estrdup(cline
);
197 #endif /* !HAVE_READLINE_HISTORY && HAVE_HISTEDIT_H */
199 #if !defined(HAVE_READLINE_HISTORY) && !defined(HAVE_HISTEDIT_H)
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
);
221 #endif /* !HAVE_READLINE_HISTORY && !HAVE_HISTEDIT_H */
231 #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H)
233 * ntp_prompt_callback - return prompt string to el_gets()
242 return lineedit_prompt
;
244 #endif /* !HAVE_READLINE_HISTORY_H && HAVE_HISTEDIT_H */