xref: /netbsd/lib/libedit/prompt.c (revision bf9ec67e)
1 /*	$NetBSD: prompt.c,v 1.9 2002/03/18 16:00:56 christos 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 "config.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.9 2002/03/18 16:00:56 christos Exp $");
45 #endif
46 #endif /* not lint && not SCCSID */
47 
48 /*
49  * prompt.c: Prompt printing functions
50  */
51 #include <stdio.h>
52 #include "el.h"
53 
54 private char	*prompt_default(EditLine *);
55 private char	*prompt_default_r(EditLine *);
56 
57 /* prompt_default():
58  *	Just a default prompt, in case the user did not provide one
59  */
60 private char *
61 /*ARGSUSED*/
62 prompt_default(EditLine *el)
63 {
64 	static char a[3] = {'?', ' ', '\0'};
65 
66 	return (a);
67 }
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(EditLine *el)
76 {
77 	static char a[1] = {'\0'};
78 
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(EditLine *el, int op)
91 {
92 	el_prompt_t *elp;
93 	char *p;
94 
95 	if (op == EL_PROMPT)
96 		elp = &el->el_prompt;
97 	else
98 		elp = &el->el_rprompt;
99 	p = (elp->p_func) (el);
100 	while (*p)
101 		re_putc(el, *p++, 1);
102 
103 	elp->p_pos.v = el->el_refresh.r_cursor.v;
104 	elp->p_pos.h = el->el_refresh.r_cursor.h;
105 }
106 
107 
108 /* prompt_init():
109  *	Initialize the prompt stuff
110  */
111 protected int
112 prompt_init(EditLine *el)
113 {
114 
115 	el->el_prompt.p_func = prompt_default;
116 	el->el_prompt.p_pos.v = 0;
117 	el->el_prompt.p_pos.h = 0;
118 	el->el_rprompt.p_func = prompt_default_r;
119 	el->el_rprompt.p_pos.v = 0;
120 	el->el_rprompt.p_pos.h = 0;
121 	return (0);
122 }
123 
124 
125 /* prompt_end():
126  *	Clean up the prompt stuff
127  */
128 protected void
129 /*ARGSUSED*/
130 prompt_end(EditLine *el)
131 {
132 }
133 
134 
135 /* prompt_set():
136  *	Install a prompt printing function
137  */
138 protected int
139 prompt_set(EditLine *el, el_pfunc_t prf, int op)
140 {
141 	el_prompt_t *p;
142 
143 	if (op == EL_PROMPT)
144 		p = &el->el_prompt;
145 	else
146 		p = &el->el_rprompt;
147 	if (prf == NULL) {
148 		if (op == EL_PROMPT)
149 			p->p_func = prompt_default;
150 		else
151 			p->p_func = prompt_default_r;
152 	} else
153 		p->p_func = prf;
154 	p->p_pos.v = 0;
155 	p->p_pos.h = 0;
156 	return (0);
157 }
158 
159 
160 /* prompt_get():
161  *	Retrieve the prompt printing function
162  */
163 protected int
164 prompt_get(EditLine *el, el_pfunc_t *prf, int op)
165 {
166 
167 	if (prf == NULL)
168 		return (-1);
169 	if (op == EL_PROMPT)
170 		*prf = el->el_prompt.p_func;
171 	else
172 		*prf = el->el_rprompt.p_func;
173 	return (0);
174 }
175