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