Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libedit / eln.c
blob166c92a015e3b3e303b137d00d0db2cc422a2cd0
1 /* $NetBSD: eln.c,v 1.2 2009/12/30 23:54:52 christos Exp $ */
3 /*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
35 #include "config.h"
36 #if !defined(lint) && !defined(SCCSID)
37 __RCSID("$NetBSD: eln.c,v 1.2 2009/12/30 23:54:52 christos Exp $");
38 #endif /* not lint && not SCCSID */
40 #include "histedit.h"
41 #include "el.h"
42 #include "read.h"
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
47 public int
48 el_getc(EditLine *el, char *cp)
50 int num_read;
51 wchar_t wc = 0;
53 el->el_flags |= IGNORE_EXTCHARS;
54 num_read = el_wgetc (el, &wc);
55 el->el_flags &= ~IGNORE_EXTCHARS;
57 if (num_read > 0)
58 *cp = (unsigned char)wc;
59 return num_read;
63 public void
64 el_push(EditLine *el, const char *str)
66 /* Using multibyte->wide string decoding works fine under single-byte
67 * character sets too, and Does The Right Thing. */
68 el_wpush(el, ct_decode_string(str, &el->el_lgcyconv));
72 public const char *
73 el_gets(EditLine *el, int *nread)
75 el->el_flags |= IGNORE_EXTCHARS;
76 el_wgets(el, nread);
77 el->el_flags &= ~IGNORE_EXTCHARS;
78 return ct_encode_string(el->el_line.buffer, &el->el_lgcyconv);
82 public int
83 el_parse(EditLine *el, int argc, const char *argv[])
85 int ret;
86 const wchar_t **wargv;
88 wargv = (const wchar_t **)
89 ct_decode_argv(argc, argv, &el->el_lgcyconv);
90 if (!wargv)
91 return -1;
92 ret = el_wparse(el, argc, wargv);
93 ct_free_argv(wargv);
95 return ret;
99 public int
100 el_set(EditLine *el, int op, ...)
102 va_list ap;
103 int ret;
105 if (!el)
106 return -1;
107 va_start(ap, op);
109 switch (op) {
110 case EL_PROMPT: /* el_pfunc_t */
111 case EL_RPROMPT: {
112 el_pfunc_t p = va_arg(ap, el_pfunc_t);
113 ret = prompt_set(el, p, 0, op, 0);
114 break;
117 case EL_TERMINAL: /* const char * */
118 ret = el_wset(el, op, va_arg(ap, char *));
119 break;
121 case EL_SIGNAL: /* int */
122 case EL_EDITMODE:
123 case EL_UNBUFFERED:
124 case EL_PREP_TERM:
125 ret = el_wset(el, op, va_arg(ap, int));
126 break;
128 case EL_BIND: /* const char * list -> const wchar_t * list */
129 case EL_TELLTC:
130 case EL_SETTC:
131 case EL_ECHOTC:
132 case EL_SETTY: {
133 const char *argv[20];
134 int i;
135 const wchar_t **wargv;
136 for (i = 1; i < (int)__arraycount(argv); ++i)
137 if ((argv[i] = va_arg(ap, char *)) == NULL)
138 break;
139 argv[0] = NULL;
140 wargv = (const wchar_t **)
141 ct_decode_argv(i, argv, &el->el_lgcyconv);
142 if (!wargv) {
143 ret = -1;
144 goto out;
147 * AFAIK we can't portably pass through our new wargv to
148 * el_wset(), so we have to reimplement the body of
149 * el_wset() for these ops.
151 switch (op) {
152 case EL_BIND:
153 wargv[0] = STR("bind");
154 ret = map_bind(el, i, wargv);
155 break;
156 case EL_TELLTC:
157 wargv[0] = STR("telltc");
158 ret = term_telltc(el, i, wargv);
159 break;
160 case EL_SETTC:
161 wargv[0] = STR("settc");
162 ret = term_settc(el, i, wargv);
163 break;
164 case EL_ECHOTC:
165 wargv[0] = STR("echotc");
166 ret = term_echotc(el, i, wargv);
167 break;
168 case EL_SETTY:
169 wargv[0] = STR("setty");
170 ret = tty_stty(el, i, wargv);
171 break;
172 default:
173 ret = -1;
175 ct_free_argv(wargv);
176 break;
179 /* XXX: do we need to change el_func_t too? */
180 case EL_ADDFN: { /* const char *, const char *, el_func_t */
181 const char *args[2];
182 el_func_t func;
183 wchar_t **wargv;
185 args[0] = va_arg(ap, const char *);
186 args[1] = va_arg(ap, const char *);
187 func = va_arg(ap, el_func_t);
189 wargv = ct_decode_argv(2, args, &el->el_lgcyconv);
190 if (!wargv) {
191 ret = -1;
192 goto out;
194 ret = el_wset(el, op, wargv[0], wargv[1], func);
195 ct_free_argv(wargv);
196 break;
198 case EL_HIST: { /* hist_fun_t, const char * */
199 hist_fun_t fun = va_arg(ap, hist_fun_t);
200 ptr_t ptr = va_arg(ap, ptr_t);
201 ret = hist_set(el, fun, ptr);
202 el->el_flags |= NARROW_HISTORY;
203 break;
205 /* XXX: do we need to change el_rfunc_t? */
206 case EL_GETCFN: /* el_rfunc_t */
207 ret = el_wset(el, op, va_arg(ap, el_rfunc_t));
208 el->el_flags |= NARROW_READ;
209 break;
210 case EL_CLIENTDATA: /* void * */
211 ret = el_wset(el, op, va_arg(ap, void *));
212 break;
213 case EL_SETFP: { /* int, FILE * */
214 int what = va_arg(ap, int);
215 FILE *fp = va_arg(ap, FILE *);
216 ret = el_wset(el, op, what, fp);
217 break;
219 case EL_PROMPT_ESC: /* el_pfunc_t, char */
220 case EL_RPROMPT_ESC: {
221 el_pfunc_t p = va_arg(ap, el_pfunc_t);
222 char c = va_arg(ap, int);
223 ret = prompt_set(el, p, c, op, 0);
224 break;
226 default:
227 ret = -1;
228 break;
231 out:
232 va_end(ap);
233 return ret;
237 public int
238 el_get(EditLine *el, int op, ...)
240 va_list ap;
241 int ret;
243 if (!el)
244 return -1;
246 va_start(ap, op);
248 switch (op) {
249 case EL_PROMPT: /* el_pfunc_t * */
250 case EL_RPROMPT: {
251 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
252 ret = prompt_get(el, p, 0, op);
253 break;
256 case EL_PROMPT_ESC: /* el_pfunc_t *, char **/
257 case EL_RPROMPT_ESC: {
258 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
259 char *c = va_arg(ap, char *);
260 wchar_t wc;
261 ret = prompt_get(el, p, &wc, op);
262 *c = (unsigned char)wc;
263 break;
266 case EL_EDITOR: {
267 const char **p = va_arg(ap, const char **);
268 const wchar_t *pw;
269 ret = el_wget(el, op, &pw);
270 *p = ct_encode_string(pw, &el->el_lgcyconv);
271 if (!el->el_lgcyconv.csize)
272 ret = -1;
273 break;
276 case EL_TERMINAL: /* const char ** */
277 ret = el_wget(el, op, va_arg(ap, const char **));
278 break;
280 case EL_SIGNAL: /* int * */
281 case EL_EDITMODE:
282 case EL_UNBUFFERED:
283 case EL_PREP_TERM:
284 ret = el_wget(el, op, va_arg(ap, int *));
285 break;
287 case EL_GETTC: {
288 char *argv[20];
289 static char gettc[] = "gettc";
290 int i;
291 for (i = 1; i < (int)__arraycount(argv); ++i)
292 if ((argv[i] = va_arg(ap, char *)) == NULL)
293 break;
294 argv[0] = gettc;
295 ret = term_gettc(el, i, argv);
296 break;
299 /* XXX: do we need to change el_rfunc_t? */
300 case EL_GETCFN: /* el_rfunc_t */
301 ret = el_wget(el, op, va_arg(ap, el_rfunc_t *));
302 break;
304 case EL_CLIENTDATA: /* void ** */
305 ret = el_wget(el, op, va_arg(ap, void **));
306 break;
308 case EL_GETFP: { /* int, FILE ** */
309 int what = va_arg(ap, int);
310 FILE **fpp = va_arg(ap, FILE **);
311 ret = el_wget(el, op, what, fpp);
312 break;
315 default:
316 ret = -1;
317 break;
320 va_end(ap);
321 return ret;
325 const LineInfo *
326 el_line(EditLine *el)
328 const LineInfoW *winfo = el_wline(el);
329 LineInfo *info = &el->el_lgcylinfo;
331 info->buffer = ct_encode_string(winfo->buffer, &el->el_lgcyconv);
332 info->cursor = info->buffer + (winfo->cursor - winfo->buffer);
333 info->lastchar = info->buffer + (winfo->lastchar - winfo->buffer);
334 return info;
339 el_insertstr(EditLine *el, const char *str)
341 return el_winsertstr(el, ct_decode_string(str, &el->el_lgcyconv));