1 /* $NetBSD: eln.c,v 1.2 2009/12/30 23:54:52 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.2 2009/12/30 23:54:52 christos Exp $");
38 #endif /* not lint && not SCCSID */
48 el_getc(EditLine
*el
, char *cp
)
53 el
->el_flags
|= IGNORE_EXTCHARS
;
54 num_read
= el_wgetc (el
, &wc
);
55 el
->el_flags
&= ~IGNORE_EXTCHARS
;
58 *cp
= (unsigned char)wc
;
64 el_push(EditLine
*el
, const char *str
)
66 /* Using multibyte->wide string decoding works fine under single-byte
67 * character sets too, and Does The Right Thing. */
68 el_wpush(el
, ct_decode_string(str
, &el
->el_lgcyconv
));
73 el_gets(EditLine
*el
, int *nread
)
75 el
->el_flags
|= IGNORE_EXTCHARS
;
77 el
->el_flags
&= ~IGNORE_EXTCHARS
;
78 return ct_encode_string(el
->el_line
.buffer
, &el
->el_lgcyconv
);
83 el_parse(EditLine
*el
, int argc
, const char *argv
[])
86 const wchar_t **wargv
;
88 wargv
= (const wchar_t **)
89 ct_decode_argv(argc
, argv
, &el
->el_lgcyconv
);
92 ret
= el_wparse(el
, argc
, wargv
);
100 el_set(EditLine
*el
, int op
, ...)
110 case EL_PROMPT
: /* el_pfunc_t */
112 el_pfunc_t p
= va_arg(ap
, el_pfunc_t
);
113 ret
= prompt_set(el
, p
, 0, op
, 0);
117 case EL_TERMINAL
: /* const char * */
118 ret
= el_wset(el
, op
, va_arg(ap
, char *));
121 case EL_SIGNAL
: /* int */
125 ret
= el_wset(el
, op
, va_arg(ap
, int));
128 case EL_BIND
: /* const char * list -> const wchar_t * list */
133 const char *argv
[20];
135 const wchar_t **wargv
;
136 for (i
= 1; i
< (int)__arraycount(argv
); ++i
)
137 if ((argv
[i
] = va_arg(ap
, char *)) == NULL
)
140 wargv
= (const wchar_t **)
141 ct_decode_argv(i
, argv
, &el
->el_lgcyconv
);
147 * AFAIK we can't portably pass through our new wargv to
148 * el_wset(), so we have to reimplement the body of
149 * el_wset() for these ops.
153 wargv
[0] = STR("bind");
154 ret
= map_bind(el
, i
, wargv
);
157 wargv
[0] = STR("telltc");
158 ret
= term_telltc(el
, i
, wargv
);
161 wargv
[0] = STR("settc");
162 ret
= term_settc(el
, i
, wargv
);
165 wargv
[0] = STR("echotc");
166 ret
= term_echotc(el
, i
, wargv
);
169 wargv
[0] = STR("setty");
170 ret
= tty_stty(el
, i
, wargv
);
179 /* XXX: do we need to change el_func_t too? */
180 case EL_ADDFN
: { /* const char *, const char *, el_func_t */
185 args
[0] = va_arg(ap
, const char *);
186 args
[1] = va_arg(ap
, const char *);
187 func
= va_arg(ap
, el_func_t
);
189 wargv
= ct_decode_argv(2, args
, &el
->el_lgcyconv
);
194 ret
= el_wset(el
, op
, wargv
[0], wargv
[1], func
);
198 case EL_HIST
: { /* hist_fun_t, const char * */
199 hist_fun_t fun
= va_arg(ap
, hist_fun_t
);
200 ptr_t ptr
= va_arg(ap
, ptr_t
);
201 ret
= hist_set(el
, fun
, ptr
);
202 el
->el_flags
|= NARROW_HISTORY
;
205 /* XXX: do we need to change el_rfunc_t? */
206 case EL_GETCFN
: /* el_rfunc_t */
207 ret
= el_wset(el
, op
, va_arg(ap
, el_rfunc_t
));
208 el
->el_flags
|= NARROW_READ
;
210 case EL_CLIENTDATA
: /* void * */
211 ret
= el_wset(el
, op
, va_arg(ap
, void *));
213 case EL_SETFP
: { /* int, FILE * */
214 int what
= va_arg(ap
, int);
215 FILE *fp
= va_arg(ap
, FILE *);
216 ret
= el_wset(el
, op
, what
, fp
);
219 case EL_PROMPT_ESC
: /* el_pfunc_t, char */
220 case EL_RPROMPT_ESC
: {
221 el_pfunc_t p
= va_arg(ap
, el_pfunc_t
);
222 char c
= va_arg(ap
, int);
223 ret
= prompt_set(el
, p
, c
, op
, 0);
238 el_get(EditLine
*el
, int op
, ...)
249 case EL_PROMPT
: /* el_pfunc_t * */
251 el_pfunc_t
*p
= va_arg(ap
, el_pfunc_t
*);
252 ret
= prompt_get(el
, p
, 0, op
);
256 case EL_PROMPT_ESC
: /* el_pfunc_t *, char **/
257 case EL_RPROMPT_ESC
: {
258 el_pfunc_t
*p
= va_arg(ap
, el_pfunc_t
*);
259 char *c
= va_arg(ap
, char *);
261 ret
= prompt_get(el
, p
, &wc
, op
);
262 *c
= (unsigned char)wc
;
267 const char **p
= va_arg(ap
, const char **);
269 ret
= el_wget(el
, op
, &pw
);
270 *p
= ct_encode_string(pw
, &el
->el_lgcyconv
);
271 if (!el
->el_lgcyconv
.csize
)
276 case EL_TERMINAL
: /* const char ** */
277 ret
= el_wget(el
, op
, va_arg(ap
, const char **));
280 case EL_SIGNAL
: /* int * */
284 ret
= el_wget(el
, op
, va_arg(ap
, int *));
289 static char gettc
[] = "gettc";
291 for (i
= 1; i
< (int)__arraycount(argv
); ++i
)
292 if ((argv
[i
] = va_arg(ap
, char *)) == NULL
)
295 ret
= term_gettc(el
, i
, argv
);
299 /* XXX: do we need to change el_rfunc_t? */
300 case EL_GETCFN
: /* el_rfunc_t */
301 ret
= el_wget(el
, op
, va_arg(ap
, el_rfunc_t
*));
304 case EL_CLIENTDATA
: /* void ** */
305 ret
= el_wget(el
, op
, va_arg(ap
, void **));
308 case EL_GETFP
: { /* int, FILE ** */
309 int what
= va_arg(ap
, int);
310 FILE **fpp
= va_arg(ap
, FILE **);
311 ret
= el_wget(el
, op
, what
, fpp
);
326 el_line(EditLine
*el
)
328 const LineInfoW
*winfo
= el_wline(el
);
329 LineInfo
*info
= &el
->el_lgcylinfo
;
331 info
->buffer
= ct_encode_string(winfo
->buffer
, &el
->el_lgcyconv
);
332 info
->cursor
= info
->buffer
+ (winfo
->cursor
- winfo
->buffer
);
333 info
->lastchar
= info
->buffer
+ (winfo
->lastchar
- winfo
->buffer
);
339 el_insertstr(EditLine
*el
, const char *str
)
341 return el_winsertstr(el
, ct_decode_string(str
, &el
->el_lgcyconv
));