msg_sys.def revision 1.6 1 /* $NetBSD: msg_sys.def,v 1.6 1999/06/22 15:00:37 cgd Exp $ */
2
3 /*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
6 *
7 * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software develooped for the NetBSD Project by
20 * Piermont Information Systems Inc.
21 * 4. The name of Piermont Information Systems Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
38
39 static WINDOW *msg_win = NULL;
40 static char *cbuffer;
41 static size_t cbuffersize;
42 static int do_echo = 1;
43
44 /* Routines */
45
46 void msg_beep (void)
47 {
48 fprintf (stderr, "\a");
49 }
50
51 int msg_window(WINDOW *window)
52 {
53 size_t ncbuffersize;
54 char *ncbuffer;
55
56 msg_win = window;
57
58 ncbuffersize = getmaxx(window) * getmaxy(window) + 1;
59 if (ncbuffersize > cbuffersize) {
60 ncbuffer = malloc(ncbuffersize);
61 if (ncbuffer == NULL)
62 return 1;
63 if (cbuffer != NULL)
64 free(cbuffer);
65 cbuffer = ncbuffer;
66 cbuffersize = ncbuffersize;
67 }
68 return 0;
69 }
70
71 char *msg_string (int msg_no)
72 {
73 return msg_list[msg_no];
74 }
75
76 void msg_clear(void)
77 {
78 wclear (msg_win);
79 wrefresh (msg_win);
80 }
81
82 void msg_standout(void)
83 {
84 wstandout(msg_win);
85 }
86
87 void msg_standend(void)
88 {
89 wstandend(msg_win);
90 }
91
92 int msg_vprintf (char *fmt, va_list ap)
93 {
94 int ret;
95
96 ret = vsnprintf (cbuffer, cbuffersize, fmt, ap);
97 waddstr (msg_win, cbuffer);
98 wrefresh (msg_win);
99 return ret;
100 }
101
102 void msg_display(int msg_no, ...)
103 {
104 va_list ap;
105
106 va_start(ap, msg_no);
107 wclear (msg_win);
108 wmove (msg_win, 0, 0);
109 (void)msg_vprintf (msg_list[msg_no], ap);
110 va_end(ap);
111 }
112
113 void msg_display_add(int msg_no, ...)
114 {
115 va_list ap;
116
117 va_start (ap, msg_no);
118 (void)msg_vprintf (msg_list[msg_no], ap);
119 va_end (ap);
120 }
121
122 int msg_printf (char *fmt, ...)
123 {
124 va_list ap;
125 int res;
126
127 va_start (ap, fmt);
128 wclear (msg_win);
129 wmove (msg_win, 0, 0);
130 res = msg_vprintf (fmt, ap);
131 va_end (ap);
132 return res;
133 }
134
135 int msg_printf_add (char *fmt, ...)
136 {
137 va_list ap;
138 int res;
139
140 va_start (ap, fmt);
141 res = msg_vprintf (fmt, ap);
142 va_end (ap);
143 return res;
144 }
145
146
147 static void msg_vprompt (char *msg, char *def, char *val, int max_chars,
148 va_list ap)
149 {
150 int ch;
151 int count = 0;
152 int y,x;
153 char *ibuf = alloca(max_chars);
154
155 msg_vprintf (msg, ap);
156 if (def != NULL && *def) {
157 waddstr (msg_win, " [");
158 waddstr (msg_win, def);
159 waddstr (msg_win, "]");
160 }
161 waddstr (msg_win, ": ");
162 wrefresh (msg_win);
163
164 while ((ch = wgetch(msg_win)) != '\n') {
165 if (ch == 0x08 || ch == 0x7f) { /* bs or del */
166 if (count > 0) {
167 count--;
168 if (do_echo) {
169 getyx(msg_win, y, x);
170 x--;
171 wmove(msg_win, y, x);
172 wdelch(msg_win);
173 }
174 } else
175 msg_beep ();
176 } else if (ch == 0x15) { /* ^U; line kill */
177 while (count > 0) {
178 count--;
179 if (do_echo) {
180 getyx(msg_win, y, x);
181 x--;
182 wmove(msg_win, y, x);
183 wdelch(msg_win);
184 }
185 }
186 } else if (count < (max_chars - 1) && isprint(ch)) {
187 if (do_echo)
188 waddch (msg_win, ch);
189 ibuf[count++] = ch;
190 } else
191 msg_beep ();
192 if (do_echo)
193 wrefresh(msg_win);
194 }
195 if (do_echo)
196 waddch(msg_win, '\n');
197
198 /* copy the appropriate string to the output */
199 if (count != 0) {
200 ibuf[count] = '\0';
201 strcpy(val, ibuf); /* size known to be OK */
202 } else if (def != NULL && val != def) {
203 strncpy(val, def, max_chars);
204 val[max_chars - 1] = '\0';
205 }
206 }
207
208 void msg_prompt_addstr (char *fmt, char *def, char *val, int max_chars, ...)
209 {
210 va_list ap;
211
212 va_start (ap, max_chars);
213 msg_vprompt (fmt, def, val, max_chars, ap);
214 va_end(ap);
215 }
216
217 void msg_prompt_add (int msg_no, char *def, char *val, int max_chars, ...)
218 {
219 va_list ap;
220
221 va_start (ap, max_chars);
222 msg_vprompt (msg_list[msg_no], def, val, max_chars, ap);
223 va_end(ap);
224 }
225
226 void msg_prompt_str (char *msg, char *def, char *val, int max_chars, ...)
227 {
228 va_list ap;
229
230 va_start (ap, max_chars);
231 wclear (msg_win);
232 wmove (msg_win, 0, 0);
233 msg_vprompt (msg, def, val, max_chars, ap);
234 va_end (ap);
235 }
236
237 void msg_prompt (int msg_no, char *def, char *val, int max_chars, ...)
238 {
239 va_list ap;
240
241 va_start (ap, max_chars);
242 wclear (msg_win);
243 wmove (msg_win, 0, 0);
244 msg_vprompt (msg_list[msg_no], def, val, max_chars, ap);
245 va_end (ap);
246 }
247
248 void msg_noecho()
249 {
250 do_echo = 0;
251 }
252
253 void msg_echo()
254 {
255 do_echo = 1;
256 }
257