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