Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / libntp / ntp_lineedit.c
blobf630b9945fb8ad1012c0956b0229d32043a9b780
1 /* $NetBSD$ */
3 /*
4 * ntp_lineedit.c - generic interface to various line editing libs
5 */
6 #ifdef HAVE_CONFIG_H
7 # include <config.h>
8 #endif
10 #include <errno.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <stdio.h>
15 #if defined(HAVE_READLINE_HISTORY)
16 # include <readline/readline.h>
17 # include <readline/history.h>
18 #else
19 # if defined(HAVE_HISTEDIT_H)
20 # include <histedit.h>
21 # endif
22 #endif
24 #include "ntp.h"
25 #include "ntp_stdlib.h"
26 #include "ntp_lineedit.h"
28 #define MAXEDITLINE 512
31 * external references
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;
48 static HistEvent hev;
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
58 int
59 ntp_readline_init(
60 const char * prompt
63 int success;
65 success = 1;
67 if (prompt) {
68 if (lineedit_prompt)
69 free(lineedit_prompt);
70 lineedit_prompt = estrdup(prompt);
73 #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H)
75 if (NULL == ntp_el) {
77 ntp_el = el_init(progname, stdin, stdout, stderr);
78 if (ntp_el) {
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",
88 strerror(errno));
89 fflush(stderr);
91 el_end(ntp_el);
92 ntp_el = NULL;
94 success = 0;
96 } else {
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);
106 } else
107 success = 0;
110 #endif /* !HAVE_READLINE_HISTORY && HAVE_HISTEDIT_H */
112 ntp_readline_initted = success;
114 return success;
119 * ntp_readline_uninit - release resources
121 void
122 ntp_readline_uninit(
123 void
126 #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H)
128 if (ntp_el) {
129 el_end(ntp_el);
130 ntp_el = NULL;
132 history_end(ntp_hist);
133 ntp_hist = NULL;
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()
153 char *
154 ntp_readline(
155 int * pcount
158 #if !defined(HAVE_READLINE_HISTORY) && !defined(HAVE_HISTEDIT_H)
159 char line_buf[MAXEDITLINE];
160 #endif
161 #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H)
162 const char * cline;
163 #endif
164 char * line;
166 if (!ntp_readline_initted)
167 return NULL;
169 *pcount = 0;
171 #if defined(HAVE_READLINE_HISTORY)
173 line = readline(lineedit_prompt ? lineedit_prompt : "");
174 if (NULL != line) {
175 if (*line) {
176 add_history(line);
177 *pcount = strlen(line);
178 } else {
179 free(line);
180 line = NULL;
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);
194 } else
195 line = NULL;
197 #endif /* !HAVE_READLINE_HISTORY && HAVE_HISTEDIT_H */
199 #if !defined(HAVE_READLINE_HISTORY) && !defined(HAVE_HISTEDIT_H)
200 /* stone hammers */
201 if (lineedit_prompt) {
202 # ifdef VMS
204 * work around problem mixing
205 * stdout & stderr
207 fputs("", stdout);
208 # endif /* VMS */
210 fputs(lineedit_prompt, stderr);
211 fflush(stderr);
214 line = fgets(line_buf, sizeof(line_buf), stdin);
215 if (NULL != line && *line) {
216 *pcount = strlen(line);
217 line = estrdup(line);
218 } else
219 line = NULL;
221 #endif /* !HAVE_READLINE_HISTORY && !HAVE_HISTEDIT_H */
224 if (!line) /* EOF */
225 fputs("\n", stderr);
227 return line;
231 #if !defined(HAVE_READLINE_HISTORY) && defined(HAVE_HISTEDIT_H)
233 * ntp_prompt_callback - return prompt string to el_gets()
235 char *
236 ntp_prompt_callback(
237 EditLine *el
240 UNUSED_ARG(el);
242 return lineedit_prompt;
244 #endif /* !HAVE_READLINE_HISTORY_H && HAVE_HISTEDIT_H */