xref: /openbsd-src/lib/libedit/prompt.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: prompt.c,v 1.6 2003/06/02 20:18:40 millert Exp $	*/
2 /*	$NetBSD: prompt.c,v 1.2 1997/01/11 06:48:04 lukem 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 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)prompt.c	8.1 (Berkeley) 6/4/93";
39 #else
40 static const char rcsid[] = "$OpenBSD: prompt.c,v 1.6 2003/06/02 20:18:40 millert Exp $";
41 #endif
42 #endif /* not lint && not SCCSID */
43 
44 /*
45  * prompt.c: Prompt printing functions
46  */
47 #include "sys.h"
48 #include <stdio.h>
49 #include "el.h"
50 
51 private char *prompt_default(EditLine *);
52 
53 /* prompt_default():
54  *	Just a default prompt, in case the user did not provide one
55  */
56 private char *
57 /*ARGSUSED*/
58 prompt_default(el)
59     EditLine *el;
60 {
61     static char a[3] = { '?', ' ', '\0' };
62     return a;
63 }
64 
65 
66 /* prompt_print():
67  *	Print the prompt and update the prompt position.
68  *	We use an array of integers in case we want to pass
69  * 	literal escape sequences in the prompt and we want a
70  *	bit to flag them
71  */
72 protected void
73 prompt_print(el)
74     EditLine *el;
75 {
76     char *p = (*el->el_prompt.p_func)(el);
77     while (*p)
78 	re_putc(el, *p++);
79 
80     el->el_prompt.p_pos.v = el->el_refresh.r_cursor.v;
81     el->el_prompt.p_pos.h = el->el_refresh.r_cursor.h;
82 
83 } /* end prompt_print */
84 
85 
86 /* prompt_init():
87  *	Initialize the prompt stuff
88  */
89 protected int
90 prompt_init(el)
91     EditLine *el;
92 {
93     el->el_prompt.p_func = prompt_default;
94     el->el_prompt.p_pos.v = 0;
95     el->el_prompt.p_pos.h = 0;
96     return 0;
97 } /* end prompt_init */
98 
99 
100 /* prompt_end():
101  *	Clean up the prompt stuff
102  */
103 protected void
104 /*ARGSUSED*/
105 prompt_end(el)
106     EditLine *el;
107 {
108 } /* end prompt_end */
109 
110 
111 /* prompt_set():
112  *	Install a prompt printing function
113  */
114 protected int
115 prompt_set(el, prf)
116     EditLine *el;
117     el_pfunc_t prf;
118 {
119     if (prf == NULL)
120 	el->el_prompt.p_func = prompt_default;
121     else
122 	el->el_prompt.p_func = prf;
123     el->el_prompt.p_pos.v = 0;
124     el->el_prompt.p_pos.h = 0;
125     return 0;
126 } /* end prompt_set */
127