1 /* $NetBSD: eln.c,v 1.34 2016/05/09 21:37:34 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.
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.
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 */
41 el_getc(EditLine
*el
, char *cp
)
46 num_read
= el_wgetc(el
, &wc
);
51 if (num_read
== EOF
) {
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
));
71 el_gets(EditLine
*el
, int *nread
)
75 tmp
= el_wgets(el
, nread
);
80 for (i
= 0; i
< *nread
; i
++)
81 nwread
+= ct_enc_width(tmp
[i
]);
84 return ct_encode_string(tmp
, &el
->el_lgcyconv
);
89 el_parse(EditLine
*el
, int argc
, const char *argv
[])
92 const wchar_t **wargv
;
94 wargv
= (void *)ct_decode_argv(argc
, argv
, &el
->el_lgcyconv
);
97 ret
= el_wparse(el
, argc
, wargv
);
105 el_set(EditLine
*el
, int op
, ...)
115 case EL_PROMPT
: /* el_pfunc_t */
117 el_pfunc_t p
= va_arg(ap
, el_pfunc_t
);
118 ret
= prompt_set(el
, p
, 0, op
, 0);
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
);
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
);
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);
145 case EL_TERMINAL
: /* const char * */
146 ret
= el_wset(el
, op
, va_arg(ap
, char *));
149 case EL_EDITOR
: /* const wchar_t * */
150 ret
= el_wset(el
, op
, ct_decode_string(va_arg(ap
, char *),
154 case EL_SIGNAL
: /* int */
158 ret
= el_wset(el
, op
, va_arg(ap
, int));
161 case EL_BIND
: /* const char * list -> const wchar_t * list */
166 const char *argv
[20];
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
)
172 argv
[0] = argv
[i
] = NULL
;
173 wargv
= (void *)ct_decode_argv(i
+ 1, argv
, &el
->el_lgcyconv
);
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.
186 ret
= map_bind(el
, i
, wargv
);
189 wargv
[0] = L
"telltc";
190 ret
= terminal_telltc(el
, i
, wargv
);
194 ret
= terminal_settc(el
, i
, wargv
);
197 wargv
[0] = L
"echotc";
198 ret
= terminal_echotc(el
, i
, wargv
);
202 ret
= tty_stty(el
, i
, wargv
);
211 /* XXX: do we need to change el_func_t too? */
212 case EL_ADDFN
: { /* const char *, const char *, el_func_t */
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
);
226 /* XXX: The two strdup's leak */
227 ret
= map_addfunc(el
, wcsdup(wargv
[0]), wcsdup(wargv
[1]),
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
;
240 case EL_GETCFN
: /* el_rfunc_t */
241 ret
= el_wset(el
, op
, va_arg(ap
, el_rfunc_t
));
244 case EL_CLIENTDATA
: /* void * */
245 ret
= el_wset(el
, op
, va_arg(ap
, void *));
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
);
256 re_clear_display(el
);
274 el_get(EditLine
*el
, int op
, ...)
285 case EL_PROMPT
: /* el_pfunc_t * */
287 el_pfunc_t
*p
= va_arg(ap
, el_pfunc_t
*);
288 ret
= prompt_get(el
, p
, 0, op
);
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 *);
297 ret
= prompt_get(el
, p
, &wc
, op
);
303 const char **p
= va_arg(ap
, const char **);
305 ret
= el_wget(el
, op
, &pw
);
306 *p
= ct_encode_string(pw
, &el
->el_lgcyconv
);
307 if (!el
->el_lgcyconv
.csize
)
312 case EL_TERMINAL
: /* const char ** */
313 ret
= el_wget(el
, op
, va_arg(ap
, const char **));
316 case EL_SIGNAL
: /* int * */
320 ret
= el_wget(el
, op
, va_arg(ap
, int *));
325 static char gettc
[] = "gettc";
327 for (i
= 1; i
< (int)__arraycount(argv
); ++i
)
328 if ((argv
[i
] = va_arg(ap
, char *)) == NULL
)
331 ret
= terminal_gettc(el
, i
, argv
);
335 case EL_GETCFN
: /* el_rfunc_t */
336 ret
= el_wget(el
, op
, va_arg(ap
, el_rfunc_t
*));
339 case EL_CLIENTDATA
: /* void ** */
340 ret
= el_wget(el
, op
, va_arg(ap
, void **));
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
);
361 el_line(EditLine
*el
)
363 const LineInfoW
*winfo
= el_wline(el
);
364 LineInfo
*info
= &el
->el_lgcylinfo
;
368 info
->buffer
= ct_encode_string(winfo
->buffer
, &el
->el_lgcyconv
);
371 for (p
= winfo
->buffer
; p
< winfo
->cursor
; p
++)
372 offset
+= ct_enc_width(*p
);
373 info
->cursor
= info
->buffer
+ offset
;
376 for (p
= winfo
->buffer
; p
< winfo
->lastchar
; p
++)
377 offset
+= ct_enc_width(*p
);
378 info
->lastchar
= info
->buffer
+ offset
;
385 el_insertstr(EditLine
*el
, const char *str
)
387 return el_winsertstr(el
, ct_decode_string(str
, &el
->el_lgcyconv
));