1 /* $OpenBSD: prompt.c,v 1.14 2023/03/08 04:43:05 guenther Exp $ */ 2 /* $NetBSD: prompt.c,v 1.25 2016/04/11 18:56:31 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 static wchar_t *prompt_default(EditLine *); 45 static wchar_t *prompt_default_r(EditLine *); 46 47 /* prompt_default(): 48 * Just a default prompt, in case the user did not provide one 49 */ 50 static wchar_t * 51 prompt_default(EditLine *el __attribute__((__unused__))) 52 { 53 static wchar_t a[3] = L"? "; 54 55 return a; 56 } 57 58 59 /* prompt_default_r(): 60 * Just a default rprompt, in case the user did not provide one 61 */ 62 static wchar_t * 63 prompt_default_r(EditLine *el __attribute__((__unused__))) 64 { 65 static wchar_t a[1] = L""; 66 67 return a; 68 } 69 70 71 /* prompt_print(): 72 * Print the prompt and update the prompt position. 73 */ 74 protected void 75 prompt_print(EditLine *el, int op) 76 { 77 el_prompt_t *elp; 78 wchar_t *p; 79 int ignore = 0; 80 81 if (op == EL_PROMPT) 82 elp = &el->el_prompt; 83 else 84 elp = &el->el_rprompt; 85 86 if (elp->p_wide) 87 p = (*elp->p_func)(el); 88 else 89 p = ct_decode_string((char *)(void *)(*elp->p_func)(el), 90 &el->el_scratch); 91 92 for (; *p; p++) { 93 if (elp->p_ignore == *p) { 94 ignore = !ignore; 95 continue; 96 } 97 if (ignore) 98 terminal__putc(el, *p); 99 else 100 re_putc(el, *p, 1); 101 } 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 */ 111 protected int 112 prompt_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_prompt.p_ignore = '\0'; 119 el->el_rprompt.p_func = prompt_default_r; 120 el->el_rprompt.p_pos.v = 0; 121 el->el_rprompt.p_pos.h = 0; 122 el->el_rprompt.p_ignore = '\0'; 123 return 0; 124 } 125 126 127 /* prompt_end(): 128 * Clean up the prompt stuff 129 */ 130 protected void 131 prompt_end(EditLine *el __attribute__((__unused__))) 132 { 133 } 134 135 136 /* prompt_set(): 137 * Install a prompt printing function 138 */ 139 protected int 140 prompt_set(EditLine *el, el_pfunc_t prf, wchar_t c, int op, int wide) 141 { 142 el_prompt_t *p; 143 144 if (op == EL_PROMPT || op == EL_PROMPT_ESC) 145 p = &el->el_prompt; 146 else 147 p = &el->el_rprompt; 148 149 if (prf == NULL) { 150 if (op == EL_PROMPT || op == EL_PROMPT_ESC) 151 p->p_func = prompt_default; 152 else 153 p->p_func = prompt_default_r; 154 } else { 155 p->p_func = prf; 156 } 157 158 p->p_ignore = c; 159 160 p->p_pos.v = 0; 161 p->p_pos.h = 0; 162 p->p_wide = wide; 163 164 return 0; 165 } 166 167 168 /* prompt_get(): 169 * Retrieve the prompt printing function 170 */ 171 protected int 172 prompt_get(EditLine *el, el_pfunc_t *prf, wchar_t *c, int op) 173 { 174 el_prompt_t *p; 175 176 if (prf == NULL) 177 return -1; 178 179 if (op == EL_PROMPT) 180 p = &el->el_prompt; 181 else 182 p = &el->el_rprompt; 183 184 if (prf) 185 *prf = p->p_func; 186 if (c) 187 *c = p->p_ignore; 188 189 return 0; 190 } 191