Merge branch 'troff'
[unleashed.git] / lib / libedit / prompt.c
blob360042466b9ceb81c3c6087d031968b483e26d10
1 /* $OpenBSD: prompt.c,v 1.13 2016/04/11 21:17:29 schwarze Exp $ */
2 /* $NetBSD: prompt.c,v 1.25 2016/04/11 18:56:31 christos Exp $ */
4 /*-
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
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.
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.
36 #include "config.h"
39 * prompt.c: Prompt printing functions
41 #include <stdio.h>
42 #include "el.h"
44 static wchar_t *prompt_default(EditLine *);
45 static wchar_t *prompt_default_r(EditLine *);
47 /* prompt_default():
48 * Just a default prompt, in case the user did not provide one
50 static wchar_t *
51 /*ARGSUSED*/
52 prompt_default(EditLine *el __attribute__((__unused__)))
54 static wchar_t a[3] = L"? ";
56 return a;
60 /* prompt_default_r():
61 * Just a default rprompt, in case the user did not provide one
63 static wchar_t *
64 /*ARGSUSED*/
65 prompt_default_r(EditLine *el __attribute__((__unused__)))
67 static wchar_t a[1] = L"";
69 return a;
73 /* prompt_print():
74 * Print the prompt and update the prompt position.
76 protected void
77 prompt_print(EditLine *el, int op)
79 el_prompt_t *elp;
80 wchar_t *p;
81 int ignore = 0;
83 if (op == EL_PROMPT)
84 elp = &el->el_prompt;
85 else
86 elp = &el->el_rprompt;
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);
94 for (; *p; p++) {
95 if (elp->p_ignore == *p) {
96 ignore = !ignore;
97 continue;
99 if (ignore)
100 terminal__putc(el, *p);
101 else
102 re_putc(el, *p, 1);
105 elp->p_pos.v = el->el_refresh.r_cursor.v;
106 elp->p_pos.h = el->el_refresh.r_cursor.h;
110 /* prompt_init():
111 * Initialize the prompt stuff
113 protected int
114 prompt_init(EditLine *el)
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;
129 /* prompt_end():
130 * Clean up the prompt stuff
132 protected void
133 /*ARGSUSED*/
134 prompt_end(EditLine *el __attribute__((__unused__)))
139 /* prompt_set():
140 * Install a prompt printing function
142 protected int
143 prompt_set(EditLine *el, el_pfunc_t prf, wchar_t c, int op, int wide)
145 el_prompt_t *p;
147 if (op == EL_PROMPT || op == EL_PROMPT_ESC)
148 p = &el->el_prompt;
149 else
150 p = &el->el_rprompt;
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;
161 p->p_ignore = c;
163 p->p_pos.v = 0;
164 p->p_pos.h = 0;
165 p->p_wide = wide;
167 return 0;
171 /* prompt_get():
172 * Retrieve the prompt printing function
174 protected int
175 prompt_get(EditLine *el, el_pfunc_t *prf, wchar_t *c, int op)
177 el_prompt_t *p;
179 if (prf == NULL)
180 return -1;
182 if (op == EL_PROMPT)
183 p = &el->el_prompt;
184 else
185 p = &el->el_rprompt;
187 if (prf)
188 *prf = p->p_func;
189 if (c)
190 *c = p->p_ignore;
192 return 0;