xref: /minix3/lib/libedit/prompt.c (revision 3e1db26a5a6252fcff0898d4cb0c3fa16ccf561d)
1*3e1db26aSLionel Sambuc /*	$NetBSD: prompt.c,v 1.20 2011/07/29 15:16:33 christos Exp $	*/
2*3e1db26aSLionel Sambuc 
3*3e1db26aSLionel Sambuc /*-
4*3e1db26aSLionel Sambuc  * Copyright (c) 1992, 1993
5*3e1db26aSLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*3e1db26aSLionel Sambuc  *
7*3e1db26aSLionel Sambuc  * This code is derived from software contributed to Berkeley by
8*3e1db26aSLionel Sambuc  * Christos Zoulas of Cornell University.
9*3e1db26aSLionel Sambuc  *
10*3e1db26aSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*3e1db26aSLionel Sambuc  * modification, are permitted provided that the following conditions
12*3e1db26aSLionel Sambuc  * are met:
13*3e1db26aSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*3e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*3e1db26aSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*3e1db26aSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*3e1db26aSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*3e1db26aSLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
19*3e1db26aSLionel Sambuc  *    may be used to endorse or promote products derived from this software
20*3e1db26aSLionel Sambuc  *    without specific prior written permission.
21*3e1db26aSLionel Sambuc  *
22*3e1db26aSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*3e1db26aSLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*3e1db26aSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*3e1db26aSLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*3e1db26aSLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*3e1db26aSLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*3e1db26aSLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*3e1db26aSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*3e1db26aSLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*3e1db26aSLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*3e1db26aSLionel Sambuc  * SUCH DAMAGE.
33*3e1db26aSLionel Sambuc  */
34*3e1db26aSLionel Sambuc 
35*3e1db26aSLionel Sambuc #include "config.h"
36*3e1db26aSLionel Sambuc #if !defined(lint) && !defined(SCCSID)
37*3e1db26aSLionel Sambuc #if 0
38*3e1db26aSLionel Sambuc static char sccsid[] = "@(#)prompt.c	8.1 (Berkeley) 6/4/93";
39*3e1db26aSLionel Sambuc #else
40*3e1db26aSLionel Sambuc __RCSID("$NetBSD: prompt.c,v 1.20 2011/07/29 15:16:33 christos Exp $");
41*3e1db26aSLionel Sambuc #endif
42*3e1db26aSLionel Sambuc #endif /* not lint && not SCCSID */
43*3e1db26aSLionel Sambuc 
44*3e1db26aSLionel Sambuc /*
45*3e1db26aSLionel Sambuc  * prompt.c: Prompt printing functions
46*3e1db26aSLionel Sambuc  */
47*3e1db26aSLionel Sambuc #include <stdio.h>
48*3e1db26aSLionel Sambuc #include "el.h"
49*3e1db26aSLionel Sambuc 
50*3e1db26aSLionel Sambuc private Char	*prompt_default(EditLine *);
51*3e1db26aSLionel Sambuc private Char	*prompt_default_r(EditLine *);
52*3e1db26aSLionel Sambuc 
53*3e1db26aSLionel Sambuc /* prompt_default():
54*3e1db26aSLionel Sambuc  *	Just a default prompt, in case the user did not provide one
55*3e1db26aSLionel Sambuc  */
56*3e1db26aSLionel Sambuc private Char *
57*3e1db26aSLionel Sambuc /*ARGSUSED*/
prompt_default(EditLine * el)58*3e1db26aSLionel Sambuc prompt_default(EditLine *el __attribute__((__unused__)))
59*3e1db26aSLionel Sambuc {
60*3e1db26aSLionel Sambuc 	static Char a[3] = {'?', ' ', '\0'};
61*3e1db26aSLionel Sambuc 
62*3e1db26aSLionel Sambuc 	return a;
63*3e1db26aSLionel Sambuc }
64*3e1db26aSLionel Sambuc 
65*3e1db26aSLionel Sambuc 
66*3e1db26aSLionel Sambuc /* prompt_default_r():
67*3e1db26aSLionel Sambuc  *	Just a default rprompt, in case the user did not provide one
68*3e1db26aSLionel Sambuc  */
69*3e1db26aSLionel Sambuc private Char *
70*3e1db26aSLionel Sambuc /*ARGSUSED*/
prompt_default_r(EditLine * el)71*3e1db26aSLionel Sambuc prompt_default_r(EditLine *el __attribute__((__unused__)))
72*3e1db26aSLionel Sambuc {
73*3e1db26aSLionel Sambuc 	static Char a[1] = {'\0'};
74*3e1db26aSLionel Sambuc 
75*3e1db26aSLionel Sambuc 	return a;
76*3e1db26aSLionel Sambuc }
77*3e1db26aSLionel Sambuc 
78*3e1db26aSLionel Sambuc 
79*3e1db26aSLionel Sambuc /* prompt_print():
80*3e1db26aSLionel Sambuc  *	Print the prompt and update the prompt position.
81*3e1db26aSLionel Sambuc  */
82*3e1db26aSLionel Sambuc protected void
prompt_print(EditLine * el,int op)83*3e1db26aSLionel Sambuc prompt_print(EditLine *el, int op)
84*3e1db26aSLionel Sambuc {
85*3e1db26aSLionel Sambuc 	el_prompt_t *elp;
86*3e1db26aSLionel Sambuc 	Char *p;
87*3e1db26aSLionel Sambuc 	int ignore = 0;
88*3e1db26aSLionel Sambuc 
89*3e1db26aSLionel Sambuc 	if (op == EL_PROMPT)
90*3e1db26aSLionel Sambuc 		elp = &el->el_prompt;
91*3e1db26aSLionel Sambuc 	else
92*3e1db26aSLionel Sambuc 		elp = &el->el_rprompt;
93*3e1db26aSLionel Sambuc 
94*3e1db26aSLionel Sambuc 	if (elp->p_wide)
95*3e1db26aSLionel Sambuc 		p = (*elp->p_func)(el);
96*3e1db26aSLionel Sambuc 	else
97*3e1db26aSLionel Sambuc 		p = ct_decode_string((char *)(void *)(*elp->p_func)(el),
98*3e1db26aSLionel Sambuc 		    &el->el_scratch);
99*3e1db26aSLionel Sambuc 
100*3e1db26aSLionel Sambuc 	for (; *p; p++) {
101*3e1db26aSLionel Sambuc 		if (elp->p_ignore == *p) {
102*3e1db26aSLionel Sambuc 			ignore = !ignore;
103*3e1db26aSLionel Sambuc 			continue;
104*3e1db26aSLionel Sambuc 		}
105*3e1db26aSLionel Sambuc 		if (ignore)
106*3e1db26aSLionel Sambuc 			terminal__putc(el, *p);
107*3e1db26aSLionel Sambuc 		else
108*3e1db26aSLionel Sambuc 			re_putc(el, *p, 1);
109*3e1db26aSLionel Sambuc 	}
110*3e1db26aSLionel Sambuc 
111*3e1db26aSLionel Sambuc 	elp->p_pos.v = el->el_refresh.r_cursor.v;
112*3e1db26aSLionel Sambuc 	elp->p_pos.h = el->el_refresh.r_cursor.h;
113*3e1db26aSLionel Sambuc }
114*3e1db26aSLionel Sambuc 
115*3e1db26aSLionel Sambuc 
116*3e1db26aSLionel Sambuc /* prompt_init():
117*3e1db26aSLionel Sambuc  *	Initialize the prompt stuff
118*3e1db26aSLionel Sambuc  */
119*3e1db26aSLionel Sambuc protected int
prompt_init(EditLine * el)120*3e1db26aSLionel Sambuc prompt_init(EditLine *el)
121*3e1db26aSLionel Sambuc {
122*3e1db26aSLionel Sambuc 
123*3e1db26aSLionel Sambuc 	el->el_prompt.p_func = prompt_default;
124*3e1db26aSLionel Sambuc 	el->el_prompt.p_pos.v = 0;
125*3e1db26aSLionel Sambuc 	el->el_prompt.p_pos.h = 0;
126*3e1db26aSLionel Sambuc 	el->el_prompt.p_ignore = '\0';
127*3e1db26aSLionel Sambuc 	el->el_rprompt.p_func = prompt_default_r;
128*3e1db26aSLionel Sambuc 	el->el_rprompt.p_pos.v = 0;
129*3e1db26aSLionel Sambuc 	el->el_rprompt.p_pos.h = 0;
130*3e1db26aSLionel Sambuc 	el->el_rprompt.p_ignore = '\0';
131*3e1db26aSLionel Sambuc 	return 0;
132*3e1db26aSLionel Sambuc }
133*3e1db26aSLionel Sambuc 
134*3e1db26aSLionel Sambuc 
135*3e1db26aSLionel Sambuc /* prompt_end():
136*3e1db26aSLionel Sambuc  *	Clean up the prompt stuff
137*3e1db26aSLionel Sambuc  */
138*3e1db26aSLionel Sambuc protected void
139*3e1db26aSLionel Sambuc /*ARGSUSED*/
prompt_end(EditLine * el)140*3e1db26aSLionel Sambuc prompt_end(EditLine *el __attribute__((__unused__)))
141*3e1db26aSLionel Sambuc {
142*3e1db26aSLionel Sambuc }
143*3e1db26aSLionel Sambuc 
144*3e1db26aSLionel Sambuc 
145*3e1db26aSLionel Sambuc /* prompt_set():
146*3e1db26aSLionel Sambuc  *	Install a prompt printing function
147*3e1db26aSLionel Sambuc  */
148*3e1db26aSLionel Sambuc protected int
prompt_set(EditLine * el,el_pfunc_t prf,Char c,int op,int wide)149*3e1db26aSLionel Sambuc prompt_set(EditLine *el, el_pfunc_t prf, Char c, int op, int wide)
150*3e1db26aSLionel Sambuc {
151*3e1db26aSLionel Sambuc 	el_prompt_t *p;
152*3e1db26aSLionel Sambuc 
153*3e1db26aSLionel Sambuc 	if (op == EL_PROMPT || op == EL_PROMPT_ESC)
154*3e1db26aSLionel Sambuc 		p = &el->el_prompt;
155*3e1db26aSLionel Sambuc 	else
156*3e1db26aSLionel Sambuc 		p = &el->el_rprompt;
157*3e1db26aSLionel Sambuc 
158*3e1db26aSLionel Sambuc 	if (prf == NULL) {
159*3e1db26aSLionel Sambuc 		if (op == EL_PROMPT || op == EL_PROMPT_ESC)
160*3e1db26aSLionel Sambuc 			p->p_func = prompt_default;
161*3e1db26aSLionel Sambuc 		else
162*3e1db26aSLionel Sambuc 			p->p_func = prompt_default_r;
163*3e1db26aSLionel Sambuc 	} else {
164*3e1db26aSLionel Sambuc 		p->p_func = prf;
165*3e1db26aSLionel Sambuc 	}
166*3e1db26aSLionel Sambuc 
167*3e1db26aSLionel Sambuc 	p->p_ignore = c;
168*3e1db26aSLionel Sambuc 
169*3e1db26aSLionel Sambuc 	p->p_pos.v = 0;
170*3e1db26aSLionel Sambuc 	p->p_pos.h = 0;
171*3e1db26aSLionel Sambuc 	p->p_wide = wide;
172*3e1db26aSLionel Sambuc 
173*3e1db26aSLionel Sambuc 	return 0;
174*3e1db26aSLionel Sambuc }
175*3e1db26aSLionel Sambuc 
176*3e1db26aSLionel Sambuc 
177*3e1db26aSLionel Sambuc /* prompt_get():
178*3e1db26aSLionel Sambuc  *	Retrieve the prompt printing function
179*3e1db26aSLionel Sambuc  */
180*3e1db26aSLionel Sambuc protected int
prompt_get(EditLine * el,el_pfunc_t * prf,Char * c,int op)181*3e1db26aSLionel Sambuc prompt_get(EditLine *el, el_pfunc_t *prf, Char *c, int op)
182*3e1db26aSLionel Sambuc {
183*3e1db26aSLionel Sambuc 	el_prompt_t *p;
184*3e1db26aSLionel Sambuc 
185*3e1db26aSLionel Sambuc 	if (prf == NULL)
186*3e1db26aSLionel Sambuc 		return -1;
187*3e1db26aSLionel Sambuc 
188*3e1db26aSLionel Sambuc 	if (op == EL_PROMPT)
189*3e1db26aSLionel Sambuc 		p = &el->el_prompt;
190*3e1db26aSLionel Sambuc 	else
191*3e1db26aSLionel Sambuc 		p = &el->el_rprompt;
192*3e1db26aSLionel Sambuc 
193*3e1db26aSLionel Sambuc 	if (prf)
194*3e1db26aSLionel Sambuc 		*prf = p->p_func;
195*3e1db26aSLionel Sambuc 	if (c)
196*3e1db26aSLionel Sambuc 		*c = p->p_ignore;
197*3e1db26aSLionel Sambuc 
198*3e1db26aSLionel Sambuc 	return 0;
199*3e1db26aSLionel Sambuc }
200