xref: /netbsd-src/lib/libedit/prompt.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: prompt.c,v 1.6 1999/11/12 01:05:07 lukem 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.6 1999/11/12 01:05:07 lukem 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	__P((EditLine *));
56 private char *prompt_default_r	__P((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(el)
64     EditLine *el;
65 {
66     static char a[3] = { '?', ' ', '\0' };
67     return a;
68 }
69 
70 /* prompt_default_r():
71  *	Just a default rprompt, in case the user did not provide one
72  */
73 private char *
74 /*ARGSUSED*/
75 prompt_default_r(el)
76     EditLine *el;
77 {
78     static char a[1] = { '\0' };
79     return a;
80 }
81 
82 
83 /* prompt_print():
84  *	Print the prompt and update the prompt position.
85  *	We use an array of integers in case we want to pass
86  * 	literal escape sequences in the prompt and we want a
87  *	bit to flag them
88  */
89 protected void
90 prompt_print(el, op)
91     EditLine *el;
92     int op;
93 {
94     el_prompt_t	*elp;
95     char	*p;
96 
97     if (op == EL_PROMPT)
98 	elp = &el->el_prompt;
99     else
100 	elp = &el->el_rprompt;
101     p = (elp->p_func)(el);
102     while (*p)
103 	re_putc(el, *p++);
104 
105     elp->p_pos.v = el->el_refresh.r_cursor.v;
106     elp->p_pos.h = el->el_refresh.r_cursor.h;
107 } /* end prompt_print */
108 
109 
110 /* prompt_init():
111  *	Initialize the prompt stuff
112  */
113 protected int
114 prompt_init(el)
115     EditLine *el;
116 {
117     el->el_prompt.p_func = prompt_default;
118     el->el_prompt.p_pos.v = 0;
119     el->el_prompt.p_pos.h = 0;
120     el->el_rprompt.p_func = prompt_default_r;
121     el->el_rprompt.p_pos.v = 0;
122     el->el_rprompt.p_pos.h = 0;
123     return 0;
124 } /* end prompt_init */
125 
126 
127 /* prompt_end():
128  *	Clean up the prompt stuff
129  */
130 protected void
131 /*ARGSUSED*/
132 prompt_end(el)
133     EditLine *el;
134 {
135 } /* end prompt_end */
136 
137 
138 /* prompt_set():
139  *	Install a prompt printing function
140  */
141 protected int
142 prompt_set(el, prf, op)
143     EditLine *el;
144     el_pfunc_t prf;
145     int op;
146 {
147     el_prompt_t *p;
148 
149     if (op == EL_PROMPT)
150 	p = &el->el_prompt;
151     else
152 	p = &el->el_rprompt;
153     if (prf == NULL) {
154 	if (op == EL_PROMPT)
155 	    p->p_func = prompt_default;
156 	else
157 	    p->p_func = prompt_default_r;
158     } else
159 	p->p_func = prf;
160     p->p_pos.v = 0;
161     p->p_pos.h = 0;
162     return 0;
163 } /* end prompt_set */
164 
165 
166 /* prompt_get():
167  *	Retrieve the prompt printing function
168  */
169 protected int
170 prompt_get(el, prf, op)
171     EditLine *el;
172     el_pfunc_t *prf;
173     int op;
174 {
175     if (prf == NULL)
176 	return -1;
177     if (op == EL_PROMPT)
178     	*prf = el->el_prompt.p_func;
179     else
180     	*prf = el->el_rprompt.p_func;
181     return 0;
182 } /* end prompt_get */
183