prompt.c revision 268782
10SN/A/*- 217756Sserb * Copyright (c) 1992, 1993 30SN/A * The Regents of the University of California. All rights reserved. 40SN/A * 50SN/A * This code is derived from software contributed to Berkeley by 60SN/A * Christos Zoulas of Cornell University. 72362SN/A * 80SN/A * Redistribution and use in source and binary forms, with or without 92362SN/A * modification, are permitted provided that the following conditions 100SN/A * are met: 110SN/A * 1. Redistributions of source code must retain the above copyright 120SN/A * notice, this list of conditions and the following disclaimer. 130SN/A * 2. Redistributions in binary form must reproduce the above copyright 140SN/A * notice, this list of conditions and the following disclaimer in the 150SN/A * documentation and/or other materials provided with the distribution. 160SN/A * 3. Neither the name of the University nor the names of its contributors 170SN/A * may be used to endorse or promote products derived from this software 180SN/A * without specific prior written permission. 190SN/A * 200SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 212362SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 222362SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 232362SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 240SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 250SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 260SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 270SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2810153SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2910153SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 300SN/A * SUCH DAMAGE. 3110153SN/A * 3210153SN/A * $NetBSD: prompt.c,v 1.14 2009/03/31 17:38:27 christos Exp $ 3310153SN/A */ 3410153SN/A 3510153SN/A#if !defined(lint) && !defined(SCCSID) 3610153SN/Astatic char sccsid[] = "@(#)prompt.c 8.1 (Berkeley) 6/4/93"; 3710153SN/A#endif /* not lint && not SCCSID */ 3810153SN/A#include <sys/cdefs.h> 3910153SN/A__FBSDID("$FreeBSD: stable/10/lib/libedit/prompt.c 268782 2014-07-17 02:14:25Z pfg $"); 4010153SN/A 410SN/A/* 420SN/A * prompt.c: Prompt printing functions 430SN/A */ 440SN/A#include "sys.h" 450SN/A#include <stdio.h> 460SN/A#include "el.h" 470SN/A 480SN/Aprivate char *prompt_default(EditLine *); 4910153SN/Aprivate char *prompt_default_r(EditLine *); 5010153SN/A 5117756Sserb/* prompt_default(): 5217756Sserb * Just a default prompt, in case the user did not provide one 5317756Sserb */ 549269SN/Aprivate char * 550SN/A/*ARGSUSED*/ 560SN/Aprompt_default(EditLine *el __unused) 5710153SN/A{ 5810153SN/A static char a[3] = {'?', ' ', '\0'}; 5910153SN/A 6017756Sserb return (a); 6110153SN/A} 620SN/A 630SN/A 640SN/A/* prompt_default_r(): 650SN/A * Just a default rprompt, in case the user did not provide one 660SN/A */ 6710153SN/Aprivate char * 6810153SN/A/*ARGSUSED*/ 6910153SN/Aprompt_default_r(EditLine *el __unused) 7010153SN/A{ 7110153SN/A static char a[1] = {'\0'}; 7210153SN/A 7317756Sserb return (a); 740SN/A} 750SN/A 760SN/A 770SN/A/* prompt_print(): 780SN/A * Print the prompt and update the prompt position. 7910153SN/A * We use an array of integers in case we want to pass 8010153SN/A * literal escape sequences in the prompt and we want a 810SN/A * bit to flag them 8210153SN/A */ 8310153SN/Aprotected void 8410153SN/Aprompt_print(EditLine *el, int op) 8510153SN/A{ 8610153SN/A el_prompt_t *elp; 8710153SN/A char *p; 8812845Sserb int ignore = 0; 890SN/A 900SN/A if (op == EL_PROMPT) 910SN/A elp = &el->el_prompt; 920SN/A else 930SN/A elp = &el->el_rprompt; 940SN/A 950SN/A for (p = (*elp->p_func)(el); *p; p++) { 960SN/A if (elp->p_ignore == *p) { 970SN/A ignore = !ignore; 980SN/A continue; 9910153SN/A } 1000SN/A if (ignore) 1010SN/A term__putc(el, *p); 1020SN/A else 1030SN/A re_putc(el, *p, 1); 1040SN/A } 1050SN/A 1060SN/A elp->p_pos.v = el->el_refresh.r_cursor.v; 1070SN/A elp->p_pos.h = el->el_refresh.r_cursor.h; 1080SN/A} 10910153SN/A 1100SN/A 11110153SN/A/* prompt_init(): 1120SN/A * Initialize the prompt stuff 1130SN/A */ 1140SN/Aprotected int 1150SN/Aprompt_init(EditLine *el) 1160SN/A{ 1170SN/A 1180SN/A el->el_prompt.p_func = prompt_default; 11910153SN/A el->el_prompt.p_pos.v = 0; 12010153SN/A el->el_prompt.p_pos.h = 0; 12110153SN/A el->el_prompt.p_ignore = '\0'; 12210153SN/A el->el_rprompt.p_func = prompt_default_r; 12310153SN/A el->el_rprompt.p_pos.v = 0; 12410153SN/A el->el_rprompt.p_pos.h = 0; 1250SN/A el->el_rprompt.p_ignore = '\0'; 12610153SN/A return 0; 12710153SN/A} 12810153SN/A 12910153SN/A 13010153SN/A/* prompt_end(): 1310SN/A * Clean up the prompt stuff 1320SN/A */ 1330SN/Aprotected void 1340SN/A/*ARGSUSED*/ 13510153SN/Aprompt_end(EditLine *el __unused) 13610153SN/A{ 1370SN/A} 1380SN/A 1390SN/A 1400SN/A/* prompt_set(): 1410SN/A * Install a prompt printing function 1420SN/A */ 1430SN/Aprotected int 1440SN/Aprompt_set(EditLine *el, el_pfunc_t prf, char c, int op) 14510153SN/A{ 14610153SN/A el_prompt_t *p; 14710153SN/A 14810153SN/A if (op == EL_PROMPT || op == EL_PROMPT_ESC) 1490SN/A p = &el->el_prompt; 15010153SN/A else 1510SN/A p = &el->el_rprompt; 1520SN/A 1530SN/A if (prf == NULL) { 1540SN/A if (op == EL_PROMPT || op == EL_PROMPT_ESC) 1550SN/A p->p_func = prompt_default; 1560SN/A else 1570SN/A p->p_func = prompt_default_r; 1580SN/A } else 1590SN/A p->p_func = prf; 1600SN/A 1610SN/A p->p_ignore = c; 1620SN/A 1630SN/A p->p_pos.v = 0; 16410153SN/A p->p_pos.h = 0; 16510153SN/A 16610153SN/A return 0; 1670SN/A} 1680SN/A 1690SN/A 1700SN/A/* prompt_get(): 1710SN/A * Retrieve the prompt printing function 1720SN/A */ 1730SN/Aprotected int 1740SN/Aprompt_get(EditLine *el, el_pfunc_t *prf, char *c, int op) 17515409Sserb{ 1760SN/A el_prompt_t *p; 1770SN/A 1780SN/A if (prf == NULL) 17910153SN/A return -1; 18010153SN/A 1810SN/A if (op == EL_PROMPT) 1820SN/A p = &el->el_prompt; 1830SN/A else 1840SN/A p = &el->el_rprompt; 1850SN/A 18610153SN/A *prf = el->el_rprompt.p_func; 1870SN/A 1880SN/A if (c) 1890SN/A *c = p->p_ignore; 19017756Sserb 19110153SN/A return 0; 19210153SN/A} 19317756Sserb