1*9abf018dSchristos /* $NetBSD: prompt.c,v 1.27 2017/06/27 23:25:13 christos Exp $ */
22543e3e6Slukem
36dc2f1dbScgd /*-
46dc2f1dbScgd * Copyright (c) 1992, 1993
56dc2f1dbScgd * The Regents of the University of California. All rights reserved.
66dc2f1dbScgd *
76dc2f1dbScgd * This code is derived from software contributed to Berkeley by
86dc2f1dbScgd * Christos Zoulas of Cornell University.
96dc2f1dbScgd *
106dc2f1dbScgd * Redistribution and use in source and binary forms, with or without
116dc2f1dbScgd * modification, are permitted provided that the following conditions
126dc2f1dbScgd * are met:
136dc2f1dbScgd * 1. Redistributions of source code must retain the above copyright
146dc2f1dbScgd * notice, this list of conditions and the following disclaimer.
156dc2f1dbScgd * 2. Redistributions in binary form must reproduce the above copyright
166dc2f1dbScgd * notice, this list of conditions and the following disclaimer in the
176dc2f1dbScgd * documentation and/or other materials provided with the distribution.
18eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
196dc2f1dbScgd * may be used to endorse or promote products derived from this software
206dc2f1dbScgd * without specific prior written permission.
216dc2f1dbScgd *
226dc2f1dbScgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
236dc2f1dbScgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
246dc2f1dbScgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
256dc2f1dbScgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
266dc2f1dbScgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
276dc2f1dbScgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
286dc2f1dbScgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
296dc2f1dbScgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
306dc2f1dbScgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
316dc2f1dbScgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
326dc2f1dbScgd * SUCH DAMAGE.
336dc2f1dbScgd */
346dc2f1dbScgd
350e0ac6b7Schristos #include "config.h"
366dc2f1dbScgd #if !defined(lint) && !defined(SCCSID)
372543e3e6Slukem #if 0
386dc2f1dbScgd static char sccsid[] = "@(#)prompt.c 8.1 (Berkeley) 6/4/93";
392543e3e6Slukem #else
40*9abf018dSchristos __RCSID("$NetBSD: prompt.c,v 1.27 2017/06/27 23:25:13 christos Exp $");
412543e3e6Slukem #endif
426dc2f1dbScgd #endif /* not lint && not SCCSID */
436dc2f1dbScgd
446dc2f1dbScgd /*
456dc2f1dbScgd * prompt.c: Prompt printing functions
466dc2f1dbScgd */
476dc2f1dbScgd #include <stdio.h>
486dc2f1dbScgd #include "el.h"
496dc2f1dbScgd
50469d44f8Schristos static wchar_t *prompt_default(EditLine *);
51469d44f8Schristos static wchar_t *prompt_default_r(EditLine *);
526dc2f1dbScgd
536dc2f1dbScgd /* prompt_default():
546dc2f1dbScgd * Just a default prompt, in case the user did not provide one
556dc2f1dbScgd */
56469d44f8Schristos static wchar_t *
576dc2f1dbScgd /*ARGSUSED*/
prompt_default(EditLine * el)58839ca00bSchristos prompt_default(EditLine *el __attribute__((__unused__)))
596dc2f1dbScgd {
600594af80Schristos static wchar_t a[3] = L"? ";
61d30d584aSlukem
62b71bed95Schristos return a;
636dc2f1dbScgd }
646dc2f1dbScgd
65d30d584aSlukem
66509864faSlukem /* prompt_default_r():
67509864faSlukem * Just a default rprompt, in case the user did not provide one
68509864faSlukem */
69469d44f8Schristos static wchar_t *
70509864faSlukem /*ARGSUSED*/
prompt_default_r(EditLine * el)71839ca00bSchristos prompt_default_r(EditLine *el __attribute__((__unused__)))
72509864faSlukem {
730594af80Schristos static wchar_t a[1] = L"";
74d30d584aSlukem
75b71bed95Schristos return a;
76509864faSlukem }
77509864faSlukem
786dc2f1dbScgd
796dc2f1dbScgd /* prompt_print():
806dc2f1dbScgd * Print the prompt and update the prompt position.
816dc2f1dbScgd */
82a2d6b270Schristos libedit_private void
prompt_print(EditLine * el,int op)83d30d584aSlukem prompt_print(EditLine *el, int op)
846dc2f1dbScgd {
85509864faSlukem el_prompt_t *elp;
860594af80Schristos wchar_t *p;
87509864faSlukem
88509864faSlukem if (op == EL_PROMPT)
89509864faSlukem elp = &el->el_prompt;
90509864faSlukem else
91509864faSlukem elp = &el->el_rprompt;
92b203bfe7Schristos
9334e53048Schristos if (elp->p_wide)
9434e53048Schristos p = (*elp->p_func)(el);
9534e53048Schristos else
96dc8498beSchristos p = ct_decode_string((char *)(void *)(*elp->p_func)(el),
9734e53048Schristos &el->el_scratch);
9834e53048Schristos
9934e53048Schristos for (; *p; p++) {
100c8754a5bSchristos if (elp->p_ignore == *p) {
101*9abf018dSchristos wchar_t *litstart = ++p;
102*9abf018dSchristos while (*p && *p != elp->p_ignore)
103*9abf018dSchristos p++;
104*9abf018dSchristos if (!*p || !p[1]) {
105*9abf018dSchristos // XXX: We lose the last literal
106*9abf018dSchristos break;
107*9abf018dSchristos }
108*9abf018dSchristos re_putliteral(el, litstart, p++);
109c8754a5bSchristos continue;
110c8754a5bSchristos }
111b203bfe7Schristos re_putc(el, *p, 1);
112c8754a5bSchristos }
1136dc2f1dbScgd
114509864faSlukem elp->p_pos.v = el->el_refresh.r_cursor.v;
115509864faSlukem elp->p_pos.h = el->el_refresh.r_cursor.h;
116d30d584aSlukem }
1176dc2f1dbScgd
1186dc2f1dbScgd
1196dc2f1dbScgd /* prompt_init():
1206dc2f1dbScgd * Initialize the prompt stuff
1216dc2f1dbScgd */
122a2d6b270Schristos libedit_private int
prompt_init(EditLine * el)123d30d584aSlukem prompt_init(EditLine *el)
1246dc2f1dbScgd {
125d30d584aSlukem
1266dc2f1dbScgd el->el_prompt.p_func = prompt_default;
1276dc2f1dbScgd el->el_prompt.p_pos.v = 0;
1286dc2f1dbScgd el->el_prompt.p_pos.h = 0;
129c8754a5bSchristos el->el_prompt.p_ignore = '\0';
130509864faSlukem el->el_rprompt.p_func = prompt_default_r;
131509864faSlukem el->el_rprompt.p_pos.v = 0;
132509864faSlukem el->el_rprompt.p_pos.h = 0;
133c8754a5bSchristos el->el_rprompt.p_ignore = '\0';
134c8754a5bSchristos return 0;
135d30d584aSlukem }
1366dc2f1dbScgd
1376dc2f1dbScgd
1386dc2f1dbScgd /* prompt_end():
1396dc2f1dbScgd * Clean up the prompt stuff
1406dc2f1dbScgd */
141a2d6b270Schristos libedit_private void
1426dc2f1dbScgd /*ARGSUSED*/
prompt_end(EditLine * el)143839ca00bSchristos prompt_end(EditLine *el __attribute__((__unused__)))
1446dc2f1dbScgd {
145d30d584aSlukem }
1466dc2f1dbScgd
1476dc2f1dbScgd
1486dc2f1dbScgd /* prompt_set():
1496dc2f1dbScgd * Install a prompt printing function
1506dc2f1dbScgd */
151a2d6b270Schristos libedit_private int
prompt_set(EditLine * el,el_pfunc_t prf,wchar_t c,int op,int wide)1520594af80Schristos prompt_set(EditLine *el, el_pfunc_t prf, wchar_t c, int op, int wide)
1536dc2f1dbScgd {
154509864faSlukem el_prompt_t *p;
155509864faSlukem
15659a88754Schristos if (op == EL_PROMPT || op == EL_PROMPT_ESC)
157509864faSlukem p = &el->el_prompt;
1586dc2f1dbScgd else
159509864faSlukem p = &el->el_rprompt;
160c8754a5bSchristos
161509864faSlukem if (prf == NULL) {
16259a88754Schristos if (op == EL_PROMPT || op == EL_PROMPT_ESC)
163509864faSlukem p->p_func = prompt_default;
164509864faSlukem else
165509864faSlukem p->p_func = prompt_default_r;
16634e53048Schristos } else {
167509864faSlukem p->p_func = prf;
16834e53048Schristos }
169c8754a5bSchristos
170c8754a5bSchristos p->p_ignore = c;
171c8754a5bSchristos
172509864faSlukem p->p_pos.v = 0;
173509864faSlukem p->p_pos.h = 0;
17434e53048Schristos p->p_wide = wide;
175c8754a5bSchristos
176c8754a5bSchristos return 0;
177d30d584aSlukem }
178f87d2504Slukem
179f87d2504Slukem
180f87d2504Slukem /* prompt_get():
181f87d2504Slukem * Retrieve the prompt printing function
182f87d2504Slukem */
183a2d6b270Schristos libedit_private int
prompt_get(EditLine * el,el_pfunc_t * prf,wchar_t * c,int op)1840594af80Schristos prompt_get(EditLine *el, el_pfunc_t *prf, wchar_t *c, int op)
185f87d2504Slukem {
186c8754a5bSchristos el_prompt_t *p;
187d30d584aSlukem
188f87d2504Slukem if (prf == NULL)
189c8754a5bSchristos return -1;
190c8754a5bSchristos
191509864faSlukem if (op == EL_PROMPT)
192c8754a5bSchristos p = &el->el_prompt;
193509864faSlukem else
194c8754a5bSchristos p = &el->el_rprompt;
195c8754a5bSchristos
19634e53048Schristos if (prf)
19734e53048Schristos *prf = p->p_func;
198c8754a5bSchristos if (c)
199c8754a5bSchristos *c = p->p_ignore;
200c8754a5bSchristos
201c8754a5bSchristos return 0;
202d30d584aSlukem }
203