1 /* $NetBSD: eln.c,v 1.19 2015/05/18 15:07:04 christos Exp $ */
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
36 #if !defined(lint) && !defined(SCCSID)
37 __RCSID("$NetBSD: eln.c,v 1.19 2015/05/18 15:07:04 christos Exp $");
38 #endif /* not lint && not SCCSID */
48 el_getc(EditLine
*el
, char *cp
)
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
;
66 el_push(EditLine
*el
, const char *str
)
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
));
75 el_gets(EditLine
*el
, int *nread
)
79 if (!(el
->el_flags
& CHARSET_IS_UTF8
))
80 el
->el_flags
|= IGNORE_EXTCHARS
;
81 tmp
= el_wgets(el
, nread
);
84 for (int i
= 0; i
< *nread
; i
++)
85 nwread
+= ct_enc_width(tmp
[i
]);
88 if (!(el
->el_flags
& CHARSET_IS_UTF8
))
89 el
->el_flags
&= ~IGNORE_EXTCHARS
;
90 return ct_encode_string(tmp
, &el
->el_lgcyconv
);
95 el_parse(EditLine
*el
, int argc
, const char *argv
[])
98 const wchar_t **wargv
;
100 wargv
= (const wchar_t **)
101 ct_decode_argv(argc
, argv
, &el
->el_lgcyconv
);
104 ret
= el_wparse(el
, argc
, wargv
);
112 el_set(EditLine
*el
, int op
, ...)
122 case EL_PROMPT
: /* el_pfunc_t */
124 el_pfunc_t p
= va_arg(ap
, el_pfunc_t
);
125 ret
= prompt_set(el
, p
, 0, op
, 0);
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
);
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
);
144 case EL_RPROMPT_ESC
: {
145 el_pfunc_t p
= va_arg(ap
, el_pfunc_t
);
146 int c
= va_arg(ap
, int);
148 ret
= prompt_set(el
, p
, c
, op
, 0);
152 case EL_TERMINAL
: /* const char * */
153 ret
= el_wset(el
, op
, va_arg(ap
, char *));
156 case EL_EDITOR
: /* const wchar_t * */
157 ret
= el_wset(el
, op
, ct_decode_string(va_arg(ap
, char *),
161 case EL_SIGNAL
: /* int */
165 ret
= el_wset(el
, op
, va_arg(ap
, int));
168 case EL_BIND
: /* const char * list -> const wchar_t * list */
173 const char *argv
[20];
175 const wchar_t **wargv
;
176 for (i
= 1; i
< (int)__arraycount(argv
) - 1; ++i
)
177 if ((argv
[i
] = va_arg(ap
, const char *)) == NULL
)
179 argv
[0] = argv
[i
] = NULL
;
180 wargv
= (const wchar_t **)
181 ct_decode_argv(i
+ 1, argv
, &el
->el_lgcyconv
);
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.
193 wargv
[0] = STR("bind");
194 ret
= map_bind(el
, i
, wargv
);
197 wargv
[0] = STR("telltc");
198 ret
= terminal_telltc(el
, i
, wargv
);
201 wargv
[0] = STR("settc");
202 ret
= terminal_settc(el
, i
, wargv
);
205 wargv
[0] = STR("echotc");
206 ret
= terminal_echotc(el
, i
, wargv
);
209 wargv
[0] = STR("setty");
210 ret
= tty_stty(el
, i
, wargv
);
219 /* XXX: do we need to change el_func_t too? */
220 case EL_ADDFN
: { /* const char *, const char *, el_func_t */
225 args
[0] = va_arg(ap
, const char *);
226 args
[1] = va_arg(ap
, const char *);
227 func
= va_arg(ap
, el_func_t
);
229 wargv
= ct_decode_argv(2, args
, &el
->el_lgcyconv
);
234 // XXX: The two strdup's leak
235 ret
= map_addfunc(el
, Strdup(wargv
[0]), Strdup(wargv
[1]),
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
;
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
;
254 case EL_CLIENTDATA
: /* void * */
255 ret
= el_wset(el
, op
, va_arg(ap
, void *));
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
);
266 re_clear_display(el
);
284 el_get(EditLine
*el
, int op
, ...)
295 case EL_PROMPT
: /* el_pfunc_t * */
297 el_pfunc_t
*p
= va_arg(ap
, el_pfunc_t
*);
298 ret
= prompt_get(el
, p
, 0, op
);
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 *);
307 ret
= prompt_get(el
, p
, &wc
, op
);
313 const char **p
= va_arg(ap
, const char **);
315 ret
= el_wget(el
, op
, &pw
);
316 *p
= ct_encode_string(pw
, &el
->el_lgcyconv
);
317 if (!el
->el_lgcyconv
.csize
)
322 case EL_TERMINAL
: /* const char ** */
323 ret
= el_wget(el
, op
, va_arg(ap
, const char **));
326 case EL_SIGNAL
: /* int * */
330 ret
= el_wget(el
, op
, va_arg(ap
, int *));
335 static char gettc
[] = "gettc";
337 for (i
= 1; i
< (int)__arraycount(argv
); ++i
)
338 if ((argv
[i
] = va_arg(ap
, char *)) == NULL
)
341 ret
= terminal_gettc(el
, i
, argv
);
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
*));
350 case EL_CLIENTDATA
: /* void ** */
351 ret
= el_wget(el
, op
, va_arg(ap
, void **));
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
);
372 el_line(EditLine
*el
)
374 const LineInfoW
*winfo
= el_wline(el
);
375 LineInfo
*info
= &el
->el_lgcylinfo
;
379 info
->buffer
= ct_encode_string(winfo
->buffer
, &el
->el_lgcyconv
);
382 for (p
= winfo
->buffer
; p
< winfo
->cursor
; p
++)
383 offset
+= ct_enc_width(*p
);
384 info
->cursor
= info
->buffer
+ offset
;
387 for (p
= winfo
->buffer
; p
< winfo
->lastchar
; p
++)
388 offset
+= ct_enc_width(*p
);
389 info
->lastchar
= info
->buffer
+ offset
;
396 el_insertstr(EditLine
*el
, const char *str
)
398 return el_winsertstr(el
, ct_decode_string(str
, &el
->el_lgcyconv
));