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