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