msg_sys.def revision 1.20 1 1.20 dsl /* $NetBSD: msg_sys.def,v 1.20 2003/06/10 17:24:17 dsl Exp $ */
2 1.1 phil
3 1.1 phil /*
4 1.1 phil * Copyright 1997 Piermont Information Systems Inc.
5 1.1 phil * All rights reserved.
6 1.1 phil *
7 1.1 phil * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 1.1 phil *
9 1.1 phil * Redistribution and use in source and binary forms, with or without
10 1.1 phil * modification, are permitted provided that the following conditions
11 1.1 phil * are met:
12 1.1 phil * 1. Redistributions of source code must retain the above copyright
13 1.1 phil * notice, this list of conditions and the following disclaimer.
14 1.1 phil * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 phil * notice, this list of conditions and the following disclaimer in the
16 1.1 phil * documentation and/or other materials provided with the distribution.
17 1.1 phil * 3. All advertising materials mentioning features or use of this software
18 1.1 phil * must display the following acknowledgement:
19 1.1 phil * This product includes software develooped for the NetBSD Project by
20 1.1 phil * Piermont Information Systems Inc.
21 1.1 phil * 4. The name of Piermont Information Systems Inc. may not be used to endorse
22 1.1 phil * or promote products derived from this software without specific prior
23 1.1 phil * written permission.
24 1.1 phil *
25 1.1 phil * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
26 1.1 phil * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 phil * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 phil * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
29 1.1 phil * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 phil * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 phil * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 phil * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 phil * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 phil * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 1.1 phil * THE POSSIBILITY OF SUCH DAMAGE.
36 1.1 phil *
37 1.1 phil */
38 1.1 phil
39 1.1 phil static WINDOW *msg_win = NULL;
40 1.6 cgd static char *cbuffer;
41 1.6 cgd static size_t cbuffersize;
42 1.1 phil
43 1.8 cgd static int last_i_was_nl, last_i_was_space;
44 1.8 cgd static int last_o_was_punct, last_o_was_space;
45 1.7 cgd
46 1.14 cgd static void _msg_beep(void);
47 1.16 cgd static int _msg_vprintf(int auto_fill, const char *fmt, va_list ap);
48 1.16 cgd static void _msg_vprompt(const char *msg, int do_echo, const char *def,
49 1.18 christos char *val, size_t max_chars, va_list ap);
50 1.12 cgd
51 1.1 phil /* Routines */
52 1.1 phil
53 1.14 cgd static void
54 1.15 cgd _msg_beep(void)
55 1.1 phil {
56 1.14 cgd fprintf(stderr, "\a");
57 1.1 phil }
58 1.1 phil
59 1.20 dsl WINDOW *
60 1.20 dsl msg_window(WINDOW *window)
61 1.1 phil {
62 1.6 cgd size_t ncbuffersize;
63 1.6 cgd char *ncbuffer;
64 1.20 dsl WINDOW *old;
65 1.6 cgd
66 1.20 dsl old = msg_win;
67 1.20 dsl if (!window)
68 1.20 dsl return old;
69 1.1 phil msg_win = window;
70 1.6 cgd
71 1.6 cgd ncbuffersize = getmaxx(window) * getmaxy(window) + 1;
72 1.20 dsl while (ncbuffersize > cbuffersize) {
73 1.6 cgd ncbuffer = malloc(ncbuffersize);
74 1.20 dsl if (ncbuffer == NULL) {
75 1.20 dsl /* we might get truncated messages... */
76 1.20 dsl ncbuffersize <<= 1;
77 1.20 dsl continue;
78 1.20 dsl }
79 1.6 cgd if (cbuffer != NULL)
80 1.6 cgd free(cbuffer);
81 1.6 cgd cbuffer = ncbuffer;
82 1.6 cgd cbuffersize = ncbuffersize;
83 1.20 dsl break;
84 1.6 cgd }
85 1.20 dsl last_o_was_punct = 0;
86 1.20 dsl last_o_was_space = 1;
87 1.20 dsl return old;
88 1.1 phil }
89 1.1 phil
90 1.16 cgd const char *msg_string (msg msg_no)
91 1.1 phil {
92 1.19 dsl int m = (intptr_t)msg_no;
93 1.19 dsl
94 1.19 dsl if (m > sizeof msg_list / sizeof msg_list[0])
95 1.19 dsl /* guess that we were passed a text string */
96 1.19 dsl return msg_no;
97 1.19 dsl return msg_list[m];
98 1.1 phil }
99 1.1 phil
100 1.1 phil void msg_clear(void)
101 1.1 phil {
102 1.1 phil wclear (msg_win);
103 1.1 phil wrefresh (msg_win);
104 1.8 cgd last_o_was_punct = 0;
105 1.8 cgd last_o_was_space = 1;
106 1.1 phil }
107 1.1 phil
108 1.1 phil void msg_standout(void)
109 1.1 phil {
110 1.1 phil wstandout(msg_win);
111 1.1 phil }
112 1.1 phil
113 1.1 phil void msg_standend(void)
114 1.1 phil {
115 1.1 phil wstandend(msg_win);
116 1.1 phil }
117 1.1 phil
118 1.12 cgd static int
119 1.16 cgd _msg_vprintf(int auto_fill, const char *fmt, va_list ap)
120 1.1 phil {
121 1.8 cgd const char *wstart, *afterw;
122 1.8 cgd int wordlen, nspaces;
123 1.1 phil int ret;
124 1.1 phil
125 1.6 cgd ret = vsnprintf (cbuffer, cbuffersize, fmt, ap);
126 1.8 cgd
127 1.10 cgd if (!auto_fill) {
128 1.10 cgd waddstr(msg_win, cbuffer);
129 1.10 cgd
130 1.10 cgd /*
131 1.10 cgd * nothing is perfect if they flow text after a table,
132 1.10 cgd * but this may be decent.
133 1.10 cgd */
134 1.10 cgd last_i_was_nl = last_i_was_space = 1;
135 1.10 cgd last_o_was_punct = 0;
136 1.10 cgd last_o_was_space = 1;
137 1.10 cgd goto out;
138 1.10 cgd }
139 1.10 cgd
140 1.8 cgd for (wstart = afterw = cbuffer; *wstart; wstart = afterw) {
141 1.8 cgd
142 1.8 cgd /* eat one space, or a whole word of non-spaces */
143 1.8 cgd if (isspace(*afterw))
144 1.8 cgd afterw++;
145 1.8 cgd else
146 1.8 cgd while (*afterw && !isspace(*afterw))
147 1.8 cgd afterw++;
148 1.8 cgd
149 1.8 cgd /* this is an nl: special formatting necessary */
150 1.8 cgd if (*wstart == '\n') {
151 1.8 cgd if (last_i_was_nl || last_i_was_space) {
152 1.8 cgd
153 1.8 cgd if (getcurx(msg_win) != 0)
154 1.8 cgd waddch(msg_win, '\n');
155 1.8 cgd if (last_i_was_nl) {
156 1.8 cgd /* last was an nl: paragraph break */
157 1.8 cgd waddch(msg_win, '\n');
158 1.8 cgd } else {
159 1.8 cgd /* last was space: line break */
160 1.8 cgd }
161 1.8 cgd last_o_was_punct = 0;
162 1.8 cgd last_o_was_space = 1;
163 1.8 cgd } else {
164 1.8 cgd /* last_o_was_punct unchanged */
165 1.8 cgd /* last_o_was_space unchanged */
166 1.8 cgd }
167 1.8 cgd last_i_was_space = 1;
168 1.8 cgd last_i_was_nl = 1;
169 1.8 cgd continue;
170 1.8 cgd }
171 1.8 cgd
172 1.8 cgd /* this is a tab: special formatting necessary. */
173 1.8 cgd if (*wstart == '\t') {
174 1.8 cgd if (last_i_was_nl) {
175 1.8 cgd /* last was an nl: list indent */
176 1.8 cgd if (getcurx(msg_win) != 0)
177 1.8 cgd waddch(msg_win, '\n');
178 1.8 cgd } else {
179 1.8 cgd /* last was not an nl: columns */
180 1.8 cgd }
181 1.8 cgd waddch(msg_win, '\t');
182 1.8 cgd last_i_was_nl = 0;
183 1.8 cgd last_i_was_space = 1;
184 1.8 cgd last_o_was_punct = 0;
185 1.8 cgd last_o_was_space = 1;
186 1.8 cgd continue;
187 1.8 cgd }
188 1.8 cgd
189 1.8 cgd /* this is a space: ignore it but set flags */
190 1.8 cgd last_i_was_nl = 0; /* all newlines handled above */
191 1.8 cgd last_i_was_space = isspace(*wstart);
192 1.8 cgd if (last_i_was_space)
193 1.8 cgd continue;
194 1.8 cgd
195 1.8 cgd /*
196 1.8 cgd * we have a real "word," i.e. a sequence of non-space
197 1.8 cgd * characters. wstart is now the start of the word,
198 1.8 cgd * afterw is the next character after the end.
199 1.8 cgd */
200 1.8 cgd wordlen = afterw - wstart;
201 1.8 cgd nspaces = last_o_was_space ? 0 : (last_o_was_punct ? 2 : 1);
202 1.8 cgd if ((getcurx(msg_win) + nspaces + wordlen) >=
203 1.8 cgd getmaxx(msg_win) &&
204 1.8 cgd wordlen < (getmaxx(msg_win) / 3)) {
205 1.8 cgd /* wrap the line */
206 1.8 cgd waddch(msg_win, '\n');
207 1.8 cgd nspaces = 0;
208 1.8 cgd }
209 1.8 cgd
210 1.8 cgd /* output the word, preceded by spaces if necessary */
211 1.8 cgd while (nspaces-- > 0)
212 1.8 cgd waddch(msg_win, ' ');
213 1.8 cgd waddbytes(msg_win, wstart, wordlen);
214 1.8 cgd
215 1.8 cgd /* set up the 'last' state for the next time around */
216 1.8 cgd switch (afterw[-1]) {
217 1.8 cgd case '.':
218 1.8 cgd case '?':
219 1.8 cgd case '!':
220 1.8 cgd last_o_was_punct = 1;
221 1.8 cgd break;
222 1.8 cgd default:
223 1.8 cgd last_o_was_punct = 0;
224 1.8 cgd break;
225 1.8 cgd }
226 1.8 cgd last_o_was_space = 0;
227 1.8 cgd
228 1.8 cgd /* ... and do it all again! */
229 1.8 cgd }
230 1.8 cgd
231 1.8 cgd /* String ended with a newline. They really want a line break. */
232 1.8 cgd if (last_i_was_nl) {
233 1.8 cgd if (getcurx(msg_win) != 0)
234 1.8 cgd waddch(msg_win, '\n');
235 1.8 cgd last_o_was_punct = 0;
236 1.8 cgd last_o_was_space = 1;
237 1.8 cgd }
238 1.8 cgd
239 1.10 cgd out:
240 1.1 phil wrefresh (msg_win);
241 1.1 phil return ret;
242 1.1 phil }
243 1.1 phil
244 1.16 cgd void msg_display(msg msg_no, ...)
245 1.1 phil {
246 1.1 phil va_list ap;
247 1.1 phil
248 1.7 cgd msg_clear();
249 1.7 cgd
250 1.1 phil va_start(ap, msg_no);
251 1.16 cgd (void)_msg_vprintf(1, msg_string(msg_no), ap);
252 1.1 phil va_end(ap);
253 1.1 phil }
254 1.1 phil
255 1.16 cgd void msg_display_add(msg msg_no, ...)
256 1.1 phil {
257 1.1 phil va_list ap;
258 1.1 phil
259 1.1 phil va_start (ap, msg_no);
260 1.16 cgd (void)_msg_vprintf(1, msg_string(msg_no), ap);
261 1.1 phil va_end (ap);
262 1.1 phil }
263 1.1 phil
264 1.19 dsl static void
265 1.19 dsl _erase_ch(void)
266 1.19 dsl {
267 1.19 dsl int y, x;
268 1.19 dsl
269 1.19 dsl getyx(msg_win, y, x);
270 1.19 dsl x--;
271 1.19 dsl wmove(msg_win, y, x);
272 1.19 dsl waddch(msg_win, ' ');
273 1.19 dsl wmove(msg_win, y, x);
274 1.19 dsl }
275 1.1 phil
276 1.12 cgd static void
277 1.16 cgd _msg_vprompt(const char *msg, int do_echo, const char *def, char *val,
278 1.18 christos size_t max_chars, va_list ap)
279 1.1 phil {
280 1.1 phil int ch;
281 1.1 phil int count = 0;
282 1.3 cgd char *ibuf = alloca(max_chars);
283 1.1 phil
284 1.12 cgd _msg_vprintf(0, msg, ap);
285 1.1 phil if (def != NULL && *def) {
286 1.1 phil waddstr (msg_win, " [");
287 1.1 phil waddstr (msg_win, def);
288 1.1 phil waddstr (msg_win, "]");
289 1.1 phil }
290 1.1 phil waddstr (msg_win, ": ");
291 1.1 phil wrefresh (msg_win);
292 1.1 phil
293 1.1 phil while ((ch = wgetch(msg_win)) != '\n') {
294 1.1 phil if (ch == 0x08 || ch == 0x7f) { /* bs or del */
295 1.1 phil if (count > 0) {
296 1.1 phil count--;
297 1.19 dsl if (do_echo)
298 1.19 dsl _erase_ch();
299 1.1 phil } else
300 1.14 cgd _msg_beep();
301 1.4 cgd } else if (ch == 0x15) { /* ^U; line kill */
302 1.4 cgd while (count > 0) {
303 1.9 cgd count--;
304 1.19 dsl if (do_echo)
305 1.19 dsl _erase_ch();
306 1.9 cgd }
307 1.9 cgd } else if (ch == 0x17) { /* ^W; word kill */
308 1.9 cgd /*
309 1.9 cgd * word kill kills the spaces and the 'word'
310 1.9 cgd * (non-spaces) last typed. the spaces before
311 1.9 cgd * the 'word' aren't killed.
312 1.9 cgd */
313 1.9 cgd while (count > 0 && isspace(ibuf[count - 1])) {
314 1.9 cgd count--;
315 1.19 dsl if (do_echo)
316 1.19 dsl _erase_ch();
317 1.9 cgd }
318 1.9 cgd while (count > 0 && !isspace(ibuf[count - 1])) {
319 1.4 cgd count--;
320 1.19 dsl if (do_echo)
321 1.19 dsl _erase_ch();
322 1.4 cgd }
323 1.4 cgd } else if (count < (max_chars - 1) && isprint(ch)) {
324 1.1 phil if (do_echo)
325 1.1 phil waddch (msg_win, ch);
326 1.3 cgd ibuf[count++] = ch;
327 1.1 phil } else
328 1.14 cgd _msg_beep();
329 1.1 phil if (do_echo)
330 1.1 phil wrefresh(msg_win);
331 1.1 phil }
332 1.8 cgd if (do_echo) {
333 1.1 phil waddch(msg_win, '\n');
334 1.8 cgd last_o_was_punct = 0;
335 1.8 cgd last_o_was_space = 1;
336 1.8 cgd }
337 1.1 phil
338 1.3 cgd /* copy the appropriate string to the output */
339 1.3 cgd if (count != 0) {
340 1.3 cgd ibuf[count] = '\0';
341 1.3 cgd strcpy(val, ibuf); /* size known to be OK */
342 1.5 cgd } else if (def != NULL && val != def) {
343 1.19 dsl strlcpy(val, def, max_chars);
344 1.3 cgd }
345 1.1 phil }
346 1.1 phil
347 1.13 cgd void
348 1.18 christos msg_prompt(msg msg_no, const char *def, char *val, size_t max_chars, ...)
349 1.1 phil {
350 1.1 phil va_list ap;
351 1.1 phil
352 1.13 cgd msg_clear();
353 1.13 cgd
354 1.1 phil va_start (ap, max_chars);
355 1.16 cgd _msg_vprompt(msg_string(msg_no), 1, def, val, max_chars, ap);
356 1.13 cgd va_end (ap);
357 1.19 dsl }
358 1.19 dsl
359 1.19 dsl void
360 1.20 dsl msg_prompt_win(msg msg_no, int x, int y, int w, int h,
361 1.19 dsl const char *def, char *val, size_t max_chars, ...)
362 1.19 dsl {
363 1.19 dsl va_list ap;
364 1.20 dsl WINDOW *win, *svwin;
365 1.20 dsl int maxx, maxy;
366 1.19 dsl
367 1.20 dsl maxx = getmaxx(stdscr);
368 1.20 dsl maxy = getmaxy(stdscr);
369 1.20 dsl if (w == 0) {
370 1.20 dsl va_start(ap, max_chars);
371 1.20 dsl w = vsnprintf(NULL, 0, msg_string(msg_no), ap);
372 1.20 dsl va_end(ap);
373 1.20 dsl if (def != NULL && *def != 0)
374 1.20 dsl w += 2 + strlen(def);
375 1.20 dsl w += 1 + 2 + max_chars + 1;
376 1.20 dsl if (w > maxx)
377 1.20 dsl w = maxx;
378 1.20 dsl }
379 1.19 dsl
380 1.20 dsl if (x == -1)
381 1.20 dsl x = (maxx - w) / 2;
382 1.20 dsl if (h < 3)
383 1.20 dsl h = 3;
384 1.20 dsl if (y < 3)
385 1.20 dsl y = (maxy - h) / 2;
386 1.20 dsl if (y + h > maxy)
387 1.20 dsl y = maxy - h;
388 1.20 dsl
389 1.20 dsl win = newwin(h, w, y, x);
390 1.20 dsl if (win == NULL)
391 1.20 dsl wprintw(msg_win, "msg_prompt_win: "
392 1.20 dsl "newwin(%d, %d, %d, %d) failed\n",
393 1.20 dsl h, w, y, x);
394 1.20 dsl else {
395 1.20 dsl wbkgd(win, getbkgd(msg_win));
396 1.20 dsl wattrset(win, getattrs(msg_win));
397 1.20 dsl box(win, 0, 0);
398 1.20 dsl wrefresh(win);
399 1.19 dsl
400 1.20 dsl svwin = msg_window(derwin(win, -1, -1, 1, 1));
401 1.20 dsl wbkgd(msg_win, getbkgd(win));
402 1.20 dsl wattrset(msg_win, getattrs(win));
403 1.19 dsl
404 1.20 dsl msg_clear();
405 1.19 dsl }
406 1.19 dsl
407 1.20 dsl va_start(ap, max_chars);
408 1.19 dsl _msg_vprompt(msg_string(msg_no), 1, def, val, max_chars, ap);
409 1.20 dsl va_end(ap);
410 1.19 dsl
411 1.20 dsl if (win != NULL) {
412 1.20 dsl wclear(win);
413 1.20 dsl wrefresh(win);
414 1.20 dsl delwin(msg_window(svwin));
415 1.20 dsl delwin(win);
416 1.20 dsl }
417 1.13 cgd }
418 1.13 cgd
419 1.13 cgd void
420 1.18 christos msg_prompt_add(msg msg_no, const char *def, char *val, size_t max_chars, ...)
421 1.13 cgd {
422 1.13 cgd va_list ap;
423 1.13 cgd
424 1.13 cgd va_start (ap, max_chars);
425 1.16 cgd _msg_vprompt(msg_string(msg_no), 1, def, val, max_chars, ap);
426 1.1 phil va_end(ap);
427 1.1 phil }
428 1.1 phil
429 1.13 cgd void
430 1.18 christos msg_prompt_noecho(msg msg_no, const char *def, char *val, size_t max_chars, ...)
431 1.1 phil {
432 1.1 phil va_list ap;
433 1.1 phil
434 1.7 cgd msg_clear();
435 1.7 cgd
436 1.1 phil va_start (ap, max_chars);
437 1.16 cgd _msg_vprompt(msg_string(msg_no), 0, def, val, max_chars, ap);
438 1.1 phil va_end (ap);
439 1.1 phil }
440 1.10 cgd
441 1.16 cgd void msg_table_add(msg msg_no, ...)
442 1.10 cgd {
443 1.10 cgd va_list ap;
444 1.10 cgd
445 1.10 cgd va_start (ap, msg_no);
446 1.16 cgd (void)_msg_vprintf(0, msg_string(msg_no), ap);
447 1.10 cgd va_end (ap);
448 1.10 cgd }
449 1.10 cgd
450