Update libedit from version 2014-06-20 to 2015-03-25 on vendor branch
[dragonfly.git] / contrib / libedit / src / eln.c
CommitLineData
a0c9eb18 1/* $NetBSD: eln.c,v 1.18 2015/03/24 21:26:50 christos Exp $ */
32fe07f8
JM
2
3/*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
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.
22 *
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.
34 */
35#include "config.h"
36#if !defined(lint) && !defined(SCCSID)
a0c9eb18 37__RCSID("$NetBSD: eln.c,v 1.18 2015/03/24 21:26:50 christos Exp $");
32fe07f8
JM
38#endif /* not lint && not SCCSID */
39
40#include "histedit.h"
41#include "el.h"
42#include "read.h"
43#include <stdarg.h>
44#include <stdio.h>
45#include <stdlib.h>
46
47public int
48el_getc(EditLine *el, char *cp)
49{
50 int num_read;
51 wchar_t wc = 0;
52
53 if (!(el->el_flags & CHARSET_IS_UTF8))
54 el->el_flags |= IGNORE_EXTCHARS;
55 num_read = el_wgetc (el, &wc);
56 if (!(el->el_flags & CHARSET_IS_UTF8))
57 el->el_flags &= ~IGNORE_EXTCHARS;
58
59 if (num_read > 0)
60 *cp = (char)wc;
61 return num_read;
62}
63
64
65public void
66el_push(EditLine *el, const char *str)
67{
68 /* Using multibyte->wide string decoding works fine under single-byte
69 * character sets too, and Does The Right Thing. */
70 el_wpush(el, ct_decode_string(str, &el->el_lgcyconv));
71}
72
73
74public const char *
75el_gets(EditLine *el, int *nread)
76{
77 const wchar_t *tmp;
78
a0c9eb18
JM
79 if (!(el->el_flags & CHARSET_IS_UTF8))
80 el->el_flags |= IGNORE_EXTCHARS;
32fe07f8 81 tmp = el_wgets(el, nread);
a0c9eb18
JM
82 if (!(el->el_flags & CHARSET_IS_UTF8))
83 el->el_flags &= ~IGNORE_EXTCHARS;
32fe07f8
JM
84 return ct_encode_string(tmp, &el->el_lgcyconv);
85}
86
87
88public int
89el_parse(EditLine *el, int argc, const char *argv[])
90{
91 int ret;
92 const wchar_t **wargv;
93
94 wargv = (const wchar_t **)
95 ct_decode_argv(argc, argv, &el->el_lgcyconv);
96 if (!wargv)
97 return -1;
98 ret = el_wparse(el, argc, wargv);
99 ct_free_argv(wargv);
100
101 return ret;
102}
103
104
105public int
106el_set(EditLine *el, int op, ...)
107{
108 va_list ap;
109 int ret;
110
111 if (!el)
112 return -1;
113 va_start(ap, op);
114
115 switch (op) {
116 case EL_PROMPT: /* el_pfunc_t */
117 case EL_RPROMPT: {
118 el_pfunc_t p = va_arg(ap, el_pfunc_t);
119 ret = prompt_set(el, p, 0, op, 0);
120 break;
121 }
122
123 case EL_RESIZE: {
124 el_zfunc_t p = va_arg(ap, el_zfunc_t);
125 void *arg = va_arg(ap, void *);
126 ret = ch_resizefun(el, p, arg);
127 break;
128 }
129
84b940c1
JM
130 case EL_ALIAS_TEXT: {
131 el_afunc_t p = va_arg(ap, el_afunc_t);
132 void *arg = va_arg(ap, void *);
133 ret = ch_aliasfun(el, p, arg);
134 break;
135 }
136
137 case EL_PROMPT_ESC:
138 case EL_RPROMPT_ESC: {
139 el_pfunc_t p = va_arg(ap, el_pfunc_t);
140 int c = va_arg(ap, int);
141
142 ret = prompt_set(el, p, c, op, 0);
143 break;
144 }
145
32fe07f8
JM
146 case EL_TERMINAL: /* const char * */
147 ret = el_wset(el, op, va_arg(ap, char *));
148 break;
149
150 case EL_EDITOR: /* const wchar_t * */
151 ret = el_wset(el, op, ct_decode_string(va_arg(ap, char *),
152 &el->el_lgcyconv));
153 break;
154
155 case EL_SIGNAL: /* int */
156 case EL_EDITMODE:
157 case EL_UNBUFFERED:
158 case EL_PREP_TERM:
159 ret = el_wset(el, op, va_arg(ap, int));
160 break;
161
162 case EL_BIND: /* const char * list -> const wchar_t * list */
163 case EL_TELLTC:
164 case EL_SETTC:
165 case EL_ECHOTC:
166 case EL_SETTY: {
167 const char *argv[20];
168 int i;
169 const wchar_t **wargv;
84b940c1
JM
170 for (i = 1; i < (int)__arraycount(argv) - 1; ++i)
171 if ((argv[i] = va_arg(ap, const char *)) == NULL)
32fe07f8 172 break;
84b940c1 173 argv[0] = argv[i] = NULL;
32fe07f8
JM
174 wargv = (const wchar_t **)
175 ct_decode_argv(i + 1, argv, &el->el_lgcyconv);
176 if (!wargv) {
177 ret = -1;
178 goto out;
179 }
180 /*
181 * AFAIK we can't portably pass through our new wargv to
182 * el_wset(), so we have to reimplement the body of
183 * el_wset() for these ops.
184 */
185 switch (op) {
186 case EL_BIND:
187 wargv[0] = STR("bind");
188 ret = map_bind(el, i, wargv);
189 break;
190 case EL_TELLTC:
191 wargv[0] = STR("telltc");
192 ret = terminal_telltc(el, i, wargv);
193 break;
194 case EL_SETTC:
195 wargv[0] = STR("settc");
196 ret = terminal_settc(el, i, wargv);
197 break;
198 case EL_ECHOTC:
199 wargv[0] = STR("echotc");
200 ret = terminal_echotc(el, i, wargv);
201 break;
202 case EL_SETTY:
203 wargv[0] = STR("setty");
204 ret = tty_stty(el, i, wargv);
205 break;
206 default:
207 ret = -1;
208 }
209 ct_free_argv(wargv);
210 break;
211 }
212
213 /* XXX: do we need to change el_func_t too? */
214 case EL_ADDFN: { /* const char *, const char *, el_func_t */
215 const char *args[2];
216 el_func_t func;
217 wchar_t **wargv;
218
219 args[0] = va_arg(ap, const char *);
220 args[1] = va_arg(ap, const char *);
221 func = va_arg(ap, el_func_t);
222
223 wargv = ct_decode_argv(2, args, &el->el_lgcyconv);
224 if (!wargv) {
225 ret = -1;
226 goto out;
227 }
228 // XXX: The two strdup's leak
229 ret = map_addfunc(el, Strdup(wargv[0]), Strdup(wargv[1]),
230 func);
231 ct_free_argv(wargv);
232 break;
233 }
234 case EL_HIST: { /* hist_fun_t, const char * */
235 hist_fun_t fun = va_arg(ap, hist_fun_t);
236 void *ptr = va_arg(ap, void *);
237 ret = hist_set(el, fun, ptr);
238 el->el_flags |= NARROW_HISTORY;
239 break;
240 }
84b940c1 241
32fe07f8
JM
242 /* XXX: do we need to change el_rfunc_t? */
243 case EL_GETCFN: /* el_rfunc_t */
244 ret = el_wset(el, op, va_arg(ap, el_rfunc_t));
245 el->el_flags |= NARROW_READ;
246 break;
84b940c1 247
32fe07f8
JM
248 case EL_CLIENTDATA: /* void * */
249 ret = el_wset(el, op, va_arg(ap, void *));
250 break;
84b940c1 251
32fe07f8
JM
252 case EL_SETFP: { /* int, FILE * */
253 int what = va_arg(ap, int);
254 FILE *fp = va_arg(ap, FILE *);
255 ret = el_wset(el, op, what, fp);
256 break;
257 }
84b940c1
JM
258
259 case EL_REFRESH:
260 re_clear_display(el);
261 re_refresh(el);
262 terminal__flush(el);
263 ret = 0;
32fe07f8 264 break;
84b940c1 265
32fe07f8
JM
266 default:
267 ret = -1;
268 break;
269 }
270
271out:
272 va_end(ap);
273 return ret;
274}
275
276
277public int
278el_get(EditLine *el, int op, ...)
279{
280 va_list ap;
281 int ret;
282
283 if (!el)
284 return -1;
285
286 va_start(ap, op);
287
288 switch (op) {
289 case EL_PROMPT: /* el_pfunc_t * */
290 case EL_RPROMPT: {
291 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
292 ret = prompt_get(el, p, 0, op);
293 break;
294 }
295
296 case EL_PROMPT_ESC: /* el_pfunc_t *, char **/
297 case EL_RPROMPT_ESC: {
298 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
299 char *c = va_arg(ap, char *);
300 wchar_t wc = 0;
301 ret = prompt_get(el, p, &wc, op);
302 *c = (char)wc;
303 break;
304 }
305
306 case EL_EDITOR: {
307 const char **p = va_arg(ap, const char **);
308 const wchar_t *pw;
309 ret = el_wget(el, op, &pw);
310 *p = ct_encode_string(pw, &el->el_lgcyconv);
311 if (!el->el_lgcyconv.csize)
312 ret = -1;
313 break;
314 }
315
316 case EL_TERMINAL: /* const char ** */
317 ret = el_wget(el, op, va_arg(ap, const char **));
318 break;
319
320 case EL_SIGNAL: /* int * */
321 case EL_EDITMODE:
322 case EL_UNBUFFERED:
323 case EL_PREP_TERM:
324 ret = el_wget(el, op, va_arg(ap, int *));
325 break;
326
327 case EL_GETTC: {
328 char *argv[20];
329 static char gettc[] = "gettc";
330 int i;
331 for (i = 1; i < (int)__arraycount(argv); ++i)
332 if ((argv[i] = va_arg(ap, char *)) == NULL)
333 break;
334 argv[0] = gettc;
335 ret = terminal_gettc(el, i, argv);
336 break;
337 }
338
339 /* XXX: do we need to change el_rfunc_t? */
340 case EL_GETCFN: /* el_rfunc_t */
341 ret = el_wget(el, op, va_arg(ap, el_rfunc_t *));
342 break;
343
344 case EL_CLIENTDATA: /* void ** */
345 ret = el_wget(el, op, va_arg(ap, void **));
346 break;
347
348 case EL_GETFP: { /* int, FILE ** */
349 int what = va_arg(ap, int);
350 FILE **fpp = va_arg(ap, FILE **);
351 ret = el_wget(el, op, what, fpp);
352 break;
353 }
354
355 default:
356 ret = -1;
357 break;
358 }
359
360 va_end(ap);
361 return ret;
362}
363
364
365const LineInfo *
366el_line(EditLine *el)
367{
368 const LineInfoW *winfo = el_wline(el);
369 LineInfo *info = &el->el_lgcylinfo;
370 size_t offset;
371 const Char *p;
372
373 info->buffer = ct_encode_string(winfo->buffer, &el->el_lgcyconv);
374
375 offset = 0;
376 for (p = winfo->buffer; p < winfo->cursor; p++)
377 offset += ct_enc_width(*p);
378 info->cursor = info->buffer + offset;
379
380 offset = 0;
381 for (p = winfo->buffer; p < winfo->lastchar; p++)
382 offset += ct_enc_width(*p);
383 info->lastchar = info->buffer + offset;
384
385 return info;
386}
387
388
389int
390el_insertstr(EditLine *el, const char *str)
391{
392 return el_winsertstr(el, ct_decode_string(str, &el->el_lgcyconv));
393}