1 /* $NetBSD: eln.c,v 1.18 2015/03/24 21:26:50 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.18 2015/03/24 21:26:50 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
)
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
]);
89 return ct_encode_string(tmp
, &el
->el_lgcyconv
);
94 el_parse(EditLine
*el
, int argc
, const char *argv
[])
97 const wchar_t **wargv
;
99 wargv
= (const wchar_t **)
100 ct_decode_argv(argc
, argv
, &el
->el_lgcyconv
);
103 ret
= el_wparse(el
, argc
, wargv
);
111 el_set(EditLine
*el
, int op
, ...)
121 case EL_PROMPT
: /* el_pfunc_t */
123 el_pfunc_t p
= va_arg(ap
, el_pfunc_t
);
124 ret
= prompt_set(el
, p
, 0, op
, 0);
129 el_zfunc_t p
= va_arg(ap
, el_zfunc_t
);
130 void *arg
= va_arg(ap
, void *);
131 ret
= ch_resizefun(el
, p
, arg
);
135 case EL_ALIAS_TEXT
: {
136 el_afunc_t p
= va_arg(ap
, el_afunc_t
);
137 void *arg
= va_arg(ap
, void *);
138 ret
= ch_aliasfun(el
, p
, arg
);
143 case EL_RPROMPT_ESC
: {
144 el_pfunc_t p
= va_arg(ap
, el_pfunc_t
);
145 int c
= va_arg(ap
, int);
147 ret
= prompt_set(el
, p
, c
, op
, 0);
151 case EL_TERMINAL
: /* const char * */
152 ret
= el_wset(el
, op
, va_arg(ap
, char *));
155 case EL_EDITOR
: /* const wchar_t * */
156 ret
= el_wset(el
, op
, ct_decode_string(va_arg(ap
, char *),
160 case EL_SIGNAL
: /* int */
164 ret
= el_wset(el
, op
, va_arg(ap
, int));
167 case EL_BIND
: /* const char * list -> const wchar_t * list */
172 const char *argv
[20];
174 const wchar_t **wargv
;
175 for (i
= 1; i
< (int)__arraycount(argv
) - 1; ++i
)
176 if ((argv
[i
] = va_arg(ap
, const char *)) == NULL
)
178 argv
[0] = argv
[i
] = NULL
;
179 wargv
= (const wchar_t **)
180 ct_decode_argv(i
+ 1, argv
, &el
->el_lgcyconv
);
186 * AFAIK we can't portably pass through our new wargv to
187 * el_wset(), so we have to reimplement the body of
188 * el_wset() for these ops.
192 wargv
[0] = STR("bind");
193 ret
= map_bind(el
, i
, wargv
);
196 wargv
[0] = STR("telltc");
197 ret
= terminal_telltc(el
, i
, wargv
);
200 wargv
[0] = STR("settc");
201 ret
= terminal_settc(el
, i
, wargv
);
204 wargv
[0] = STR("echotc");
205 ret
= terminal_echotc(el
, i
, wargv
);
208 wargv
[0] = STR("setty");
209 ret
= tty_stty(el
, i
, wargv
);
218 /* XXX: do we need to change el_func_t too? */
219 case EL_ADDFN
: { /* const char *, const char *, el_func_t */
224 args
[0] = va_arg(ap
, const char *);
225 args
[1] = va_arg(ap
, const char *);
226 func
= va_arg(ap
, el_func_t
);
228 wargv
= ct_decode_argv(2, args
, &el
->el_lgcyconv
);
233 // XXX: The two strdup's leak
234 ret
= map_addfunc(el
, Strdup(wargv
[0]), Strdup(wargv
[1]),
239 case EL_HIST
: { /* hist_fun_t, const char * */
240 hist_fun_t fun
= va_arg(ap
, hist_fun_t
);
241 void *ptr
= va_arg(ap
, void *);
242 ret
= hist_set(el
, fun
, ptr
);
243 el
->el_flags
|= NARROW_HISTORY
;
247 /* XXX: do we need to change el_rfunc_t? */
248 case EL_GETCFN
: /* el_rfunc_t */
249 ret
= el_wset(el
, op
, va_arg(ap
, el_rfunc_t
));
250 el
->el_flags
|= NARROW_READ
;
253 case EL_CLIENTDATA
: /* void * */
254 ret
= el_wset(el
, op
, va_arg(ap
, void *));
257 case EL_SETFP
: { /* int, FILE * */
258 int what
= va_arg(ap
, int);
259 FILE *fp
= va_arg(ap
, FILE *);
260 ret
= el_wset(el
, op
, what
, fp
);
265 re_clear_display(el
);
283 el_get(EditLine
*el
, int op
, ...)
294 case EL_PROMPT
: /* el_pfunc_t * */
296 el_pfunc_t
*p
= va_arg(ap
, el_pfunc_t
*);
297 ret
= prompt_get(el
, p
, 0, op
);
301 case EL_PROMPT_ESC
: /* el_pfunc_t *, char **/
302 case EL_RPROMPT_ESC
: {
303 el_pfunc_t
*p
= va_arg(ap
, el_pfunc_t
*);
304 char *c
= va_arg(ap
, char *);
306 ret
= prompt_get(el
, p
, &wc
, op
);
312 const char **p
= va_arg(ap
, const char **);
314 ret
= el_wget(el
, op
, &pw
);
315 *p
= ct_encode_string(pw
, &el
->el_lgcyconv
);
316 if (!el
->el_lgcyconv
.csize
)
321 case EL_TERMINAL
: /* const char ** */
322 ret
= el_wget(el
, op
, va_arg(ap
, const char **));
325 case EL_SIGNAL
: /* int * */
329 ret
= el_wget(el
, op
, va_arg(ap
, int *));
334 static char gettc
[] = "gettc";
336 for (i
= 1; i
< (int)__arraycount(argv
); ++i
)
337 if ((argv
[i
] = va_arg(ap
, char *)) == NULL
)
340 ret
= terminal_gettc(el
, i
, argv
);
344 /* XXX: do we need to change el_rfunc_t? */
345 case EL_GETCFN
: /* el_rfunc_t */
346 ret
= el_wget(el
, op
, va_arg(ap
, el_rfunc_t
*));
349 case EL_CLIENTDATA
: /* void ** */
350 ret
= el_wget(el
, op
, va_arg(ap
, void **));
353 case EL_GETFP
: { /* int, FILE ** */
354 int what
= va_arg(ap
, int);
355 FILE **fpp
= va_arg(ap
, FILE **);
356 ret
= el_wget(el
, op
, what
, fpp
);
371 el_line(EditLine
*el
)
373 const LineInfoW
*winfo
= el_wline(el
);
374 LineInfo
*info
= &el
->el_lgcylinfo
;
378 info
->buffer
= ct_encode_string(winfo
->buffer
, &el
->el_lgcyconv
);
381 for (p
= winfo
->buffer
; p
< winfo
->cursor
; p
++)
382 offset
+= ct_enc_width(*p
);
383 info
->cursor
= info
->buffer
+ offset
;
386 for (p
= winfo
->buffer
; p
< winfo
->lastchar
; p
++)
387 offset
+= ct_enc_width(*p
);
388 info
->lastchar
= info
->buffer
+ offset
;
395 el_insertstr(EditLine
*el
, const char *str
)
397 return el_winsertstr(el
, ct_decode_string(str
, &el
->el_lgcyconv
));