11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1992, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Christos Zoulas of Cornell University.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
16148834Sstefanf * 3. Neither the name of the University nor the names of its contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
3184260Sobrien *
32268782Spfg *	$NetBSD: prompt.c,v 1.14 2009/03/31 17:38:27 christos Exp $
331573Srgrimes */
341573Srgrimes
351573Srgrimes#if !defined(lint) && !defined(SCCSID)
361573Srgrimesstatic char sccsid[] = "@(#)prompt.c	8.1 (Berkeley) 6/4/93";
371573Srgrimes#endif /* not lint && not SCCSID */
3884260Sobrien#include <sys/cdefs.h>
3984260Sobrien__FBSDID("$FreeBSD$");
401573Srgrimes
411573Srgrimes/*
421573Srgrimes * prompt.c: Prompt printing functions
431573Srgrimes */
441573Srgrimes#include "sys.h"
451573Srgrimes#include <stdio.h>
461573Srgrimes#include "el.h"
471573Srgrimes
4884260Sobrienprivate char	*prompt_default(EditLine *);
4984260Sobrienprivate char	*prompt_default_r(EditLine *);
501573Srgrimes
511573Srgrimes/* prompt_default():
521573Srgrimes *	Just a default prompt, in case the user did not provide one
531573Srgrimes */
541573Srgrimesprivate char *
551573Srgrimes/*ARGSUSED*/
56148834Sstefanfprompt_default(EditLine *el __unused)
571573Srgrimes{
5884260Sobrien	static char a[3] = {'?', ' ', '\0'};
5984260Sobrien
6084260Sobrien	return (a);
611573Srgrimes}
621573Srgrimes
631573Srgrimes
6484260Sobrien/* prompt_default_r():
6584260Sobrien *	Just a default rprompt, in case the user did not provide one
6684260Sobrien */
6784260Sobrienprivate char *
6884260Sobrien/*ARGSUSED*/
69148834Sstefanfprompt_default_r(EditLine *el __unused)
7084260Sobrien{
7184260Sobrien	static char a[1] = {'\0'};
7284260Sobrien
7384260Sobrien	return (a);
7484260Sobrien}
7584260Sobrien
7684260Sobrien
771573Srgrimes/* prompt_print():
781573Srgrimes *	Print the prompt and update the prompt position.
791573Srgrimes *	We use an array of integers in case we want to pass
801573Srgrimes * 	literal escape sequences in the prompt and we want a
811573Srgrimes *	bit to flag them
821573Srgrimes */
831573Srgrimesprotected void
8484260Sobrienprompt_print(EditLine *el, int op)
851573Srgrimes{
8684260Sobrien	el_prompt_t *elp;
8784260Sobrien	char *p;
88237448Spfg	int ignore = 0;
891573Srgrimes
9084260Sobrien	if (op == EL_PROMPT)
9184260Sobrien		elp = &el->el_prompt;
9284260Sobrien	else
9384260Sobrien		elp = &el->el_rprompt;
941573Srgrimes
95237448Spfg	for (p = (*elp->p_func)(el); *p; p++) {
96237448Spfg		if (elp->p_ignore == *p) {
97237448Spfg			ignore = !ignore;
98237448Spfg			continue;
99237448Spfg		}
100237448Spfg		if (ignore)
101237448Spfg			term__putc(el, *p);
102237448Spfg		else
103237448Spfg			re_putc(el, *p, 1);
104237448Spfg	}
105237448Spfg
10684260Sobrien	elp->p_pos.v = el->el_refresh.r_cursor.v;
10784260Sobrien	elp->p_pos.h = el->el_refresh.r_cursor.h;
10884260Sobrien}
1091573Srgrimes
1101573Srgrimes
1111573Srgrimes/* prompt_init():
1121573Srgrimes *	Initialize the prompt stuff
1131573Srgrimes */
1148870Srgrimesprotected int
11584260Sobrienprompt_init(EditLine *el)
1161573Srgrimes{
1171573Srgrimes
11884260Sobrien	el->el_prompt.p_func = prompt_default;
11984260Sobrien	el->el_prompt.p_pos.v = 0;
12084260Sobrien	el->el_prompt.p_pos.h = 0;
121237448Spfg	el->el_prompt.p_ignore = '\0';
12284260Sobrien	el->el_rprompt.p_func = prompt_default_r;
12384260Sobrien	el->el_rprompt.p_pos.v = 0;
12484260Sobrien	el->el_rprompt.p_pos.h = 0;
125237448Spfg	el->el_rprompt.p_ignore = '\0';
126237448Spfg	return 0;
12784260Sobrien}
1281573Srgrimes
12984260Sobrien
1301573Srgrimes/* prompt_end():
1311573Srgrimes *	Clean up the prompt stuff
1321573Srgrimes */
1331573Srgrimesprotected void
1348870Srgrimes/*ARGSUSED*/
135148834Sstefanfprompt_end(EditLine *el __unused)
1361573Srgrimes{
13784260Sobrien}
1381573Srgrimes
1391573Srgrimes
1401573Srgrimes/* prompt_set():
1411573Srgrimes *	Install a prompt printing function
1421573Srgrimes */
1438870Srgrimesprotected int
144237448Spfgprompt_set(EditLine *el, el_pfunc_t prf, char c, int op)
1451573Srgrimes{
14684260Sobrien	el_prompt_t *p;
14784260Sobrien
148237448Spfg	if (op == EL_PROMPT || op == EL_PROMPT_ESC)
14984260Sobrien		p = &el->el_prompt;
15084260Sobrien	else
15184260Sobrien		p = &el->el_rprompt;
152237448Spfg
15384260Sobrien	if (prf == NULL) {
154237448Spfg		if (op == EL_PROMPT || op == EL_PROMPT_ESC)
15584260Sobrien			p->p_func = prompt_default;
15684260Sobrien		else
15784260Sobrien			p->p_func = prompt_default_r;
15884260Sobrien	} else
15984260Sobrien		p->p_func = prf;
160237448Spfg
161237448Spfg	p->p_ignore = c;
162237448Spfg
16384260Sobrien	p->p_pos.v = 0;
16484260Sobrien	p->p_pos.h = 0;
165237448Spfg
166237448Spfg	return 0;
16784260Sobrien}
16884260Sobrien
16984260Sobrien
17084260Sobrien/* prompt_get():
17184260Sobrien *	Retrieve the prompt printing function
17284260Sobrien */
17384260Sobrienprotected int
174237448Spfgprompt_get(EditLine *el, el_pfunc_t *prf, char *c, int op)
17584260Sobrien{
176237448Spfg	el_prompt_t *p;
17784260Sobrien
17884260Sobrien	if (prf == NULL)
179237448Spfg		return -1;
180237448Spfg
18184260Sobrien	if (op == EL_PROMPT)
182237448Spfg		p = &el->el_prompt;
18384260Sobrien	else
184237448Spfg		p = &el->el_rprompt;
185237448Spfg
186237448Spfg	*prf = el->el_rprompt.p_func;
187237448Spfg
188237448Spfg	if (c)
189237448Spfg		*c = p->p_ignore;
190237448Spfg
191237448Spfg	return 0;
19284260Sobrien}
193