prompt.c revision 148834
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 *
32148834Sstefanf *	$NetBSD: prompt.c,v 1.11 2003/08/07 16:44:32 agc 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: head/lib/libedit/prompt.c 148834 2005-08-07 20:55:59Z stefanf $");
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;
881573Srgrimes
8984260Sobrien	if (op == EL_PROMPT)
9084260Sobrien		elp = &el->el_prompt;
9184260Sobrien	else
9284260Sobrien		elp = &el->el_rprompt;
9384260Sobrien	p = (elp->p_func) (el);
9484260Sobrien	while (*p)
9584260Sobrien		re_putc(el, *p++, 1);
961573Srgrimes
9784260Sobrien	elp->p_pos.v = el->el_refresh.r_cursor.v;
9884260Sobrien	elp->p_pos.h = el->el_refresh.r_cursor.h;
9984260Sobrien}
1001573Srgrimes
1011573Srgrimes
1021573Srgrimes/* prompt_init():
1031573Srgrimes *	Initialize the prompt stuff
1041573Srgrimes */
1058870Srgrimesprotected int
10684260Sobrienprompt_init(EditLine *el)
1071573Srgrimes{
1081573Srgrimes
10984260Sobrien	el->el_prompt.p_func = prompt_default;
11084260Sobrien	el->el_prompt.p_pos.v = 0;
11184260Sobrien	el->el_prompt.p_pos.h = 0;
11284260Sobrien	el->el_rprompt.p_func = prompt_default_r;
11384260Sobrien	el->el_rprompt.p_pos.v = 0;
11484260Sobrien	el->el_rprompt.p_pos.h = 0;
11584260Sobrien	return (0);
11684260Sobrien}
1171573Srgrimes
11884260Sobrien
1191573Srgrimes/* prompt_end():
1201573Srgrimes *	Clean up the prompt stuff
1211573Srgrimes */
1221573Srgrimesprotected void
1238870Srgrimes/*ARGSUSED*/
124148834Sstefanfprompt_end(EditLine *el __unused)
1251573Srgrimes{
12684260Sobrien}
1271573Srgrimes
1281573Srgrimes
1291573Srgrimes/* prompt_set():
1301573Srgrimes *	Install a prompt printing function
1311573Srgrimes */
1328870Srgrimesprotected int
13384260Sobrienprompt_set(EditLine *el, el_pfunc_t prf, int op)
1341573Srgrimes{
13584260Sobrien	el_prompt_t *p;
13684260Sobrien
13784260Sobrien	if (op == EL_PROMPT)
13884260Sobrien		p = &el->el_prompt;
13984260Sobrien	else
14084260Sobrien		p = &el->el_rprompt;
14184260Sobrien	if (prf == NULL) {
14284260Sobrien		if (op == EL_PROMPT)
14384260Sobrien			p->p_func = prompt_default;
14484260Sobrien		else
14584260Sobrien			p->p_func = prompt_default_r;
14684260Sobrien	} else
14784260Sobrien		p->p_func = prf;
14884260Sobrien	p->p_pos.v = 0;
14984260Sobrien	p->p_pos.h = 0;
15084260Sobrien	return (0);
15184260Sobrien}
15284260Sobrien
15384260Sobrien
15484260Sobrien/* prompt_get():
15584260Sobrien *	Retrieve the prompt printing function
15684260Sobrien */
15784260Sobrienprotected int
15884260Sobrienprompt_get(EditLine *el, el_pfunc_t *prf, int op)
15984260Sobrien{
16084260Sobrien
16184260Sobrien	if (prf == NULL)
16284260Sobrien		return (-1);
16384260Sobrien	if (op == EL_PROMPT)
16484260Sobrien		*prf = el->el_prompt.p_func;
16584260Sobrien	else
16684260Sobrien		*prf = el->el_rprompt.p_func;
16784260Sobrien	return (0);
16884260Sobrien}
169