Fixes https://github.com/heimdal/heimdal/issues/294
[heimdal.git] / lib / libedit / src / eln.c
blobaa0a5b565dd7386bb77d5050d100ae3e35b2101f
1 /* $NetBSD: eln.c,v 1.34 2016/05/09 21:37:34 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.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 #include "config.h"
29 #if !defined(lint) && !defined(SCCSID)
30 __RCSID("$NetBSD: eln.c,v 1.34 2016/05/09 21:37:34 christos Exp $");
31 #endif /* not lint && not SCCSID */
33 #include <errno.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
38 #include "el.h"
40 int
41 el_getc(EditLine *el, char *cp)
43 int num_read;
44 wchar_t wc = 0;
46 num_read = el_wgetc(el, &wc);
47 *cp = '\0';
48 if (num_read <= 0)
49 return num_read;
50 num_read = wctob(wc);
51 if (num_read == EOF) {
52 errno = ERANGE;
53 return -1;
54 } else {
55 *cp = (char)num_read;
56 return 1;
61 void
62 el_push(EditLine *el, const char *str)
64 /* Using multibyte->wide string decoding works fine under single-byte
65 * character sets too, and Does The Right Thing. */
66 el_wpush(el, ct_decode_string(str, &el->el_lgcyconv));
70 const char *
71 el_gets(EditLine *el, int *nread)
73 const wchar_t *tmp;
75 tmp = el_wgets(el, nread);
76 if (tmp != NULL) {
77 int i;
78 size_t nwread = 0;
80 for (i = 0; i < *nread; i++)
81 nwread += ct_enc_width(tmp[i]);
82 *nread = (int)nwread;
84 return ct_encode_string(tmp, &el->el_lgcyconv);
88 int
89 el_parse(EditLine *el, int argc, const char *argv[])
91 int ret;
92 const wchar_t **wargv;
94 wargv = (void *)ct_decode_argv(argc, argv, &el->el_lgcyconv);
95 if (!wargv)
96 return -1;
97 ret = el_wparse(el, argc, wargv);
98 el_free(wargv);
100 return ret;
105 el_set(EditLine *el, int op, ...)
107 va_list ap;
108 int ret;
110 if (!el)
111 return -1;
112 va_start(ap, op);
114 switch (op) {
115 case EL_PROMPT: /* el_pfunc_t */
116 case EL_RPROMPT: {
117 el_pfunc_t p = va_arg(ap, el_pfunc_t);
118 ret = prompt_set(el, p, 0, op, 0);
119 break;
122 case EL_RESIZE: {
123 el_zfunc_t p = va_arg(ap, el_zfunc_t);
124 void *arg = va_arg(ap, void *);
125 ret = ch_resizefun(el, p, arg);
126 break;
129 case EL_ALIAS_TEXT: {
130 el_afunc_t p = va_arg(ap, el_afunc_t);
131 void *arg = va_arg(ap, void *);
132 ret = ch_aliasfun(el, p, arg);
133 break;
136 case EL_PROMPT_ESC:
137 case EL_RPROMPT_ESC: {
138 el_pfunc_t p = va_arg(ap, el_pfunc_t);
139 int c = va_arg(ap, int);
141 ret = prompt_set(el, p, c, op, 0);
142 break;
145 case EL_TERMINAL: /* const char * */
146 ret = el_wset(el, op, va_arg(ap, char *));
147 break;
149 case EL_EDITOR: /* const wchar_t * */
150 ret = el_wset(el, op, ct_decode_string(va_arg(ap, char *),
151 &el->el_lgcyconv));
152 break;
154 case EL_SIGNAL: /* int */
155 case EL_EDITMODE:
156 case EL_UNBUFFERED:
157 case EL_PREP_TERM:
158 ret = el_wset(el, op, va_arg(ap, int));
159 break;
161 case EL_BIND: /* const char * list -> const wchar_t * list */
162 case EL_TELLTC:
163 case EL_SETTC:
164 case EL_ECHOTC:
165 case EL_SETTY: {
166 const char *argv[20];
167 int i;
168 const wchar_t **wargv;
169 for (i = 1; i < (int)__arraycount(argv) - 1; ++i)
170 if ((argv[i] = va_arg(ap, const char *)) == NULL)
171 break;
172 argv[0] = argv[i] = NULL;
173 wargv = (void *)ct_decode_argv(i + 1, argv, &el->el_lgcyconv);
174 if (!wargv) {
175 ret = -1;
176 goto out;
179 * AFAIK we can't portably pass through our new wargv to
180 * el_wset(), so we have to reimplement the body of
181 * el_wset() for these ops.
183 switch (op) {
184 case EL_BIND:
185 wargv[0] = L"bind";
186 ret = map_bind(el, i, wargv);
187 break;
188 case EL_TELLTC:
189 wargv[0] = L"telltc";
190 ret = terminal_telltc(el, i, wargv);
191 break;
192 case EL_SETTC:
193 wargv[0] = L"settc";
194 ret = terminal_settc(el, i, wargv);
195 break;
196 case EL_ECHOTC:
197 wargv[0] = L"echotc";
198 ret = terminal_echotc(el, i, wargv);
199 break;
200 case EL_SETTY:
201 wargv[0] = L"setty";
202 ret = tty_stty(el, i, wargv);
203 break;
204 default:
205 ret = -1;
207 el_free(wargv);
208 break;
211 /* XXX: do we need to change el_func_t too? */
212 case EL_ADDFN: { /* const char *, const char *, el_func_t */
213 const char *args[2];
214 el_func_t func;
215 wchar_t **wargv;
217 args[0] = va_arg(ap, const char *);
218 args[1] = va_arg(ap, const char *);
219 func = va_arg(ap, el_func_t);
221 wargv = ct_decode_argv(2, args, &el->el_lgcyconv);
222 if (!wargv) {
223 ret = -1;
224 goto out;
226 /* XXX: The two strdup's leak */
227 ret = map_addfunc(el, wcsdup(wargv[0]), wcsdup(wargv[1]),
228 func);
229 el_free(wargv);
230 break;
232 case EL_HIST: { /* hist_fun_t, const char * */
233 hist_fun_t fun = va_arg(ap, hist_fun_t);
234 void *ptr = va_arg(ap, void *);
235 ret = hist_set(el, fun, ptr);
236 el->el_flags |= NARROW_HISTORY;
237 break;
240 case EL_GETCFN: /* el_rfunc_t */
241 ret = el_wset(el, op, va_arg(ap, el_rfunc_t));
242 break;
244 case EL_CLIENTDATA: /* void * */
245 ret = el_wset(el, op, va_arg(ap, void *));
246 break;
248 case EL_SETFP: { /* int, FILE * */
249 int what = va_arg(ap, int);
250 FILE *fp = va_arg(ap, FILE *);
251 ret = el_wset(el, op, what, fp);
252 break;
255 case EL_REFRESH:
256 re_clear_display(el);
257 re_refresh(el);
258 terminal__flush(el);
259 ret = 0;
260 break;
262 default:
263 ret = -1;
264 break;
267 out:
268 va_end(ap);
269 return ret;
274 el_get(EditLine *el, int op, ...)
276 va_list ap;
277 int ret;
279 if (!el)
280 return -1;
282 va_start(ap, op);
284 switch (op) {
285 case EL_PROMPT: /* el_pfunc_t * */
286 case EL_RPROMPT: {
287 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
288 ret = prompt_get(el, p, 0, op);
289 break;
292 case EL_PROMPT_ESC: /* el_pfunc_t *, char **/
293 case EL_RPROMPT_ESC: {
294 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
295 char *c = va_arg(ap, char *);
296 wchar_t wc = 0;
297 ret = prompt_get(el, p, &wc, op);
298 *c = (char)wc;
299 break;
302 case EL_EDITOR: {
303 const char **p = va_arg(ap, const char **);
304 const wchar_t *pw;
305 ret = el_wget(el, op, &pw);
306 *p = ct_encode_string(pw, &el->el_lgcyconv);
307 if (!el->el_lgcyconv.csize)
308 ret = -1;
309 break;
312 case EL_TERMINAL: /* const char ** */
313 ret = el_wget(el, op, va_arg(ap, const char **));
314 break;
316 case EL_SIGNAL: /* int * */
317 case EL_EDITMODE:
318 case EL_UNBUFFERED:
319 case EL_PREP_TERM:
320 ret = el_wget(el, op, va_arg(ap, int *));
321 break;
323 case EL_GETTC: {
324 char *argv[20];
325 static char gettc[] = "gettc";
326 int i;
327 for (i = 1; i < (int)__arraycount(argv); ++i)
328 if ((argv[i] = va_arg(ap, char *)) == NULL)
329 break;
330 argv[0] = gettc;
331 ret = terminal_gettc(el, i, argv);
332 break;
335 case EL_GETCFN: /* el_rfunc_t */
336 ret = el_wget(el, op, va_arg(ap, el_rfunc_t *));
337 break;
339 case EL_CLIENTDATA: /* void ** */
340 ret = el_wget(el, op, va_arg(ap, void **));
341 break;
343 case EL_GETFP: { /* int, FILE ** */
344 int what = va_arg(ap, int);
345 FILE **fpp = va_arg(ap, FILE **);
346 ret = el_wget(el, op, what, fpp);
347 break;
350 default:
351 ret = -1;
352 break;
355 va_end(ap);
356 return ret;
360 const LineInfo *
361 el_line(EditLine *el)
363 const LineInfoW *winfo = el_wline(el);
364 LineInfo *info = &el->el_lgcylinfo;
365 size_t offset;
366 const wchar_t *p;
368 info->buffer = ct_encode_string(winfo->buffer, &el->el_lgcyconv);
370 offset = 0;
371 for (p = winfo->buffer; p < winfo->cursor; p++)
372 offset += ct_enc_width(*p);
373 info->cursor = info->buffer + offset;
375 offset = 0;
376 for (p = winfo->buffer; p < winfo->lastchar; p++)
377 offset += ct_enc_width(*p);
378 info->lastchar = info->buffer + offset;
380 return info;
385 el_insertstr(EditLine *el, const char *str)
387 return el_winsertstr(el, ct_decode_string(str, &el->el_lgcyconv));