prompt.c revision 84260
1/*- 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Christos Zoulas of Cornell University. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * $NetBSD: prompt.c,v 1.7 2000/09/04 22:06:31 lukem Exp $ 37 */ 38 39#include <sys/cdefs.h> 40__FBSDID("$FreeBSD: head/lib/libedit/prompt.c 84260 2001-10-01 08:41:27Z obrien $"); 41#if !defined(lint) && !defined(SCCSID) 42static char sccsid[] = "@(#)prompt.c 8.1 (Berkeley) 6/4/93"; 43#endif /* not lint && not SCCSID */ 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: head/lib/libedit/prompt.c 84260 2001-10-01 08:41:27Z obrien $"); 46 47/* 48 * prompt.c: Prompt printing functions 49 */ 50#include "sys.h" 51#include <stdio.h> 52#include "el.h" 53 54private char *prompt_default(EditLine *); 55private char *prompt_default_r(EditLine *); 56 57/* prompt_default(): 58 * Just a default prompt, in case the user did not provide one 59 */ 60private char * 61/*ARGSUSED*/ 62prompt_default(EditLine *el) 63{ 64 static char a[3] = {'?', ' ', '\0'}; 65 66 return (a); 67} 68 69 70/* prompt_default_r(): 71 * Just a default rprompt, in case the user did not provide one 72 */ 73private char * 74/*ARGSUSED*/ 75prompt_default_r(EditLine *el) 76{ 77 static char a[1] = {'\0'}; 78 79 return (a); 80} 81 82 83/* prompt_print(): 84 * Print the prompt and update the prompt position. 85 * We use an array of integers in case we want to pass 86 * literal escape sequences in the prompt and we want a 87 * bit to flag them 88 */ 89protected void 90prompt_print(EditLine *el, int op) 91{ 92 el_prompt_t *elp; 93 char *p; 94 95 if (op == EL_PROMPT) 96 elp = &el->el_prompt; 97 else 98 elp = &el->el_rprompt; 99 p = (elp->p_func) (el); 100 while (*p) 101 re_putc(el, *p++, 1); 102 103 elp->p_pos.v = el->el_refresh.r_cursor.v; 104 elp->p_pos.h = el->el_refresh.r_cursor.h; 105} 106 107 108/* prompt_init(): 109 * Initialize the prompt stuff 110 */ 111protected int 112prompt_init(EditLine *el) 113{ 114 115 el->el_prompt.p_func = prompt_default; 116 el->el_prompt.p_pos.v = 0; 117 el->el_prompt.p_pos.h = 0; 118 el->el_rprompt.p_func = prompt_default_r; 119 el->el_rprompt.p_pos.v = 0; 120 el->el_rprompt.p_pos.h = 0; 121 return (0); 122} 123 124 125/* prompt_end(): 126 * Clean up the prompt stuff 127 */ 128protected void 129/*ARGSUSED*/ 130prompt_end(EditLine *el) 131{ 132} 133 134 135/* prompt_set(): 136 * Install a prompt printing function 137 */ 138protected int 139prompt_set(EditLine *el, el_pfunc_t prf, int op) 140{ 141 el_prompt_t *p; 142 143 if (op == EL_PROMPT) 144 p = &el->el_prompt; 145 else 146 p = &el->el_rprompt; 147 if (prf == NULL) { 148 if (op == EL_PROMPT) 149 p->p_func = prompt_default; 150 else 151 p->p_func = prompt_default_r; 152 } else 153 p->p_func = prf; 154 p->p_pos.v = 0; 155 p->p_pos.h = 0; 156 return (0); 157} 158 159 160/* prompt_get(): 161 * Retrieve the prompt printing function 162 */ 163protected int 164prompt_get(EditLine *el, el_pfunc_t *prf, int op) 165{ 166 167 if (prf == NULL) 168 return (-1); 169 if (op == EL_PROMPT) 170 *prf = el->el_prompt.p_func; 171 else 172 *prf = el->el_rprompt.p_func; 173 return (0); 174} 175