1313981Spfg/*	$NetBSD: prompt.c,v 1.23 2016/02/16 15:53:48 christos Exp $	*/
2276881Sbapt
31573Srgrimes/*-
41573Srgrimes * Copyright (c) 1992, 1993
51573Srgrimes *	The Regents of the University of California.  All rights reserved.
61573Srgrimes *
71573Srgrimes * This code is derived from software contributed to Berkeley by
81573Srgrimes * Christos Zoulas of Cornell University.
91573Srgrimes *
101573Srgrimes * Redistribution and use in source and binary forms, with or without
111573Srgrimes * modification, are permitted provided that the following conditions
121573Srgrimes * are met:
131573Srgrimes * 1. Redistributions of source code must retain the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer.
151573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161573Srgrimes *    notice, this list of conditions and the following disclaimer in the
171573Srgrimes *    documentation and/or other materials provided with the distribution.
18148834Sstefanf * 3. Neither the name of the University nor the names of its contributors
191573Srgrimes *    may be used to endorse or promote products derived from this software
201573Srgrimes *    without specific prior written permission.
211573Srgrimes *
221573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321573Srgrimes * SUCH DAMAGE.
331573Srgrimes */
341573Srgrimes
35276881Sbapt#include "config.h"
361573Srgrimes#if !defined(lint) && !defined(SCCSID)
37276881Sbapt#if 0
381573Srgrimesstatic char sccsid[] = "@(#)prompt.c	8.1 (Berkeley) 6/4/93";
39276881Sbapt#else
40313981Spfg__RCSID("$NetBSD: prompt.c,v 1.23 2016/02/16 15:53:48 christos Exp $");
41276881Sbapt#endif
421573Srgrimes#endif /* not lint && not SCCSID */
4384260Sobrien#include <sys/cdefs.h>
4484260Sobrien__FBSDID("$FreeBSD: stable/11/lib/libedit/prompt.c 313981 2017-02-20 03:33:59Z pfg $");
451573Srgrimes
461573Srgrimes/*
471573Srgrimes * prompt.c: Prompt printing functions
481573Srgrimes */
491573Srgrimes#include <stdio.h>
501573Srgrimes#include "el.h"
511573Srgrimes
52276881Sbaptprivate Char	*prompt_default(EditLine *);
53276881Sbaptprivate Char	*prompt_default_r(EditLine *);
541573Srgrimes
551573Srgrimes/* prompt_default():
561573Srgrimes *	Just a default prompt, in case the user did not provide one
571573Srgrimes */
58276881Sbaptprivate Char *
591573Srgrimes/*ARGSUSED*/
60276881Sbaptprompt_default(EditLine *el __attribute__((__unused__)))
611573Srgrimes{
62276881Sbapt	static Char a[3] = {'?', ' ', '\0'};
6384260Sobrien
64276881Sbapt	return a;
651573Srgrimes}
661573Srgrimes
671573Srgrimes
6884260Sobrien/* prompt_default_r():
6984260Sobrien *	Just a default rprompt, in case the user did not provide one
7084260Sobrien */
71276881Sbaptprivate Char *
7284260Sobrien/*ARGSUSED*/
73276881Sbaptprompt_default_r(EditLine *el __attribute__((__unused__)))
7484260Sobrien{
75276881Sbapt	static Char a[1] = {'\0'};
7684260Sobrien
77276881Sbapt	return a;
7884260Sobrien}
7984260Sobrien
8084260Sobrien
811573Srgrimes/* prompt_print():
821573Srgrimes *	Print the prompt and update the prompt position.
831573Srgrimes */
841573Srgrimesprotected void
8584260Sobrienprompt_print(EditLine *el, int op)
861573Srgrimes{
8784260Sobrien	el_prompt_t *elp;
88276881Sbapt	Char *p;
89237448Spfg	int ignore = 0;
901573Srgrimes
9184260Sobrien	if (op == EL_PROMPT)
9284260Sobrien		elp = &el->el_prompt;
9384260Sobrien	else
9484260Sobrien		elp = &el->el_rprompt;
951573Srgrimes
96276881Sbapt	if (elp->p_wide)
97276881Sbapt		p = (*elp->p_func)(el);
98276881Sbapt	else
99276881Sbapt		p = ct_decode_string((char *)(void *)(*elp->p_func)(el),
100276881Sbapt		    &el->el_scratch);
101276881Sbapt
102276881Sbapt	for (; *p; p++) {
103237448Spfg		if (elp->p_ignore == *p) {
104237448Spfg			ignore = !ignore;
105237448Spfg			continue;
106237448Spfg		}
107237448Spfg		if (ignore)
108276881Sbapt			terminal__putc(el, *p);
109237448Spfg		else
110237448Spfg			re_putc(el, *p, 1);
111237448Spfg	}
112237448Spfg
11384260Sobrien	elp->p_pos.v = el->el_refresh.r_cursor.v;
11484260Sobrien	elp->p_pos.h = el->el_refresh.r_cursor.h;
11584260Sobrien}
1161573Srgrimes
1171573Srgrimes
1181573Srgrimes/* prompt_init():
1191573Srgrimes *	Initialize the prompt stuff
1201573Srgrimes */
1218870Srgrimesprotected int
12284260Sobrienprompt_init(EditLine *el)
1231573Srgrimes{
1241573Srgrimes
12584260Sobrien	el->el_prompt.p_func = prompt_default;
12684260Sobrien	el->el_prompt.p_pos.v = 0;
12784260Sobrien	el->el_prompt.p_pos.h = 0;
128237448Spfg	el->el_prompt.p_ignore = '\0';
12984260Sobrien	el->el_rprompt.p_func = prompt_default_r;
13084260Sobrien	el->el_rprompt.p_pos.v = 0;
13184260Sobrien	el->el_rprompt.p_pos.h = 0;
132237448Spfg	el->el_rprompt.p_ignore = '\0';
133237448Spfg	return 0;
13484260Sobrien}
1351573Srgrimes
13684260Sobrien
1371573Srgrimes/* prompt_end():
1381573Srgrimes *	Clean up the prompt stuff
1391573Srgrimes */
1401573Srgrimesprotected void
1418870Srgrimes/*ARGSUSED*/
142276881Sbaptprompt_end(EditLine *el __attribute__((__unused__)))
1431573Srgrimes{
14484260Sobrien}
1451573Srgrimes
1461573Srgrimes
1471573Srgrimes/* prompt_set():
1481573Srgrimes *	Install a prompt printing function
1491573Srgrimes */
1508870Srgrimesprotected int
151276881Sbaptprompt_set(EditLine *el, el_pfunc_t prf, Char c, int op, int wide)
1521573Srgrimes{
15384260Sobrien	el_prompt_t *p;
15484260Sobrien
155237448Spfg	if (op == EL_PROMPT || op == EL_PROMPT_ESC)
15684260Sobrien		p = &el->el_prompt;
15784260Sobrien	else
15884260Sobrien		p = &el->el_rprompt;
159237448Spfg
16084260Sobrien	if (prf == NULL) {
161237448Spfg		if (op == EL_PROMPT || op == EL_PROMPT_ESC)
16284260Sobrien			p->p_func = prompt_default;
16384260Sobrien		else
16484260Sobrien			p->p_func = prompt_default_r;
165276881Sbapt	} else {
16684260Sobrien		p->p_func = prf;
167276881Sbapt	}
168237448Spfg
169237448Spfg	p->p_ignore = c;
170237448Spfg
17184260Sobrien	p->p_pos.v = 0;
17284260Sobrien	p->p_pos.h = 0;
173276881Sbapt	p->p_wide = wide;
174237448Spfg
175237448Spfg	return 0;
17684260Sobrien}
17784260Sobrien
17884260Sobrien
17984260Sobrien/* prompt_get():
18084260Sobrien *	Retrieve the prompt printing function
18184260Sobrien */
18284260Sobrienprotected int
183276881Sbaptprompt_get(EditLine *el, el_pfunc_t *prf, Char *c, int op)
18484260Sobrien{
185237448Spfg	el_prompt_t *p;
18684260Sobrien
18784260Sobrien	if (prf == NULL)
188237448Spfg		return -1;
189237448Spfg
19084260Sobrien	if (op == EL_PROMPT)
191237448Spfg		p = &el->el_prompt;
19284260Sobrien	else
193237448Spfg		p = &el->el_rprompt;
194237448Spfg
195276881Sbapt	if (prf)
196276881Sbapt		*prf = p->p_func;
197237448Spfg	if (c)
198237448Spfg		*c = p->p_ignore;
199237448Spfg
200237448Spfg	return 0;
20184260Sobrien}
202