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