msg_sys.def revision 1.26 1 1.26 dsl /* $NetBSD: msg_sys.def,v 1.26 2003/09/25 18:32:10 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.21 dsl static int _msg_vprintf(int, const char *, va_list);
48 1.21 dsl static void _msg_vprompt(const char *, int, const char *, char *,
49 1.21 dsl size_t, va_list);
50 1.21 dsl
51 1.21 dsl static char *msgmap = MAP_FAILED;
52 1.21 dsl static size_t msgmapsz;
53 1.21 dsl static int msgmapcount;
54 1.12 cgd
55 1.1 phil /* Routines */
56 1.1 phil
57 1.14 cgd static void
58 1.15 cgd _msg_beep(void)
59 1.1 phil {
60 1.21 dsl
61 1.14 cgd fprintf(stderr, "\a");
62 1.1 phil }
63 1.1 phil
64 1.20 dsl WINDOW *
65 1.20 dsl msg_window(WINDOW *window)
66 1.1 phil {
67 1.6 cgd size_t ncbuffersize;
68 1.6 cgd char *ncbuffer;
69 1.20 dsl WINDOW *old;
70 1.6 cgd
71 1.20 dsl old = msg_win;
72 1.20 dsl if (!window)
73 1.20 dsl return old;
74 1.1 phil msg_win = window;
75 1.6 cgd
76 1.6 cgd ncbuffersize = getmaxx(window) * getmaxy(window) + 1;
77 1.20 dsl while (ncbuffersize > cbuffersize) {
78 1.6 cgd ncbuffer = malloc(ncbuffersize);
79 1.20 dsl if (ncbuffer == NULL) {
80 1.20 dsl /* we might get truncated messages... */
81 1.20 dsl ncbuffersize <<= 1;
82 1.20 dsl continue;
83 1.20 dsl }
84 1.6 cgd if (cbuffer != NULL)
85 1.6 cgd free(cbuffer);
86 1.6 cgd cbuffer = ncbuffer;
87 1.6 cgd cbuffersize = ncbuffersize;
88 1.20 dsl break;
89 1.6 cgd }
90 1.20 dsl last_o_was_punct = 0;
91 1.20 dsl last_o_was_space = 1;
92 1.20 dsl return old;
93 1.1 phil }
94 1.1 phil
95 1.21 dsl int
96 1.21 dsl msg_file(const char *file)
97 1.21 dsl {
98 1.21 dsl int fd;
99 1.21 dsl
100 1.21 dsl if (msgmap != MAP_FAILED)
101 1.21 dsl munmap(msgmap, msgmapsz);
102 1.21 dsl msgmap = MAP_FAILED;
103 1.21 dsl if (!file)
104 1.21 dsl return 0;
105 1.21 dsl fd = open(file, O_RDONLY, 0);
106 1.21 dsl if (fd == -1)
107 1.21 dsl return -1;
108 1.21 dsl msgmapsz = lseek(fd, 0, SEEK_END);
109 1.21 dsl msgmap = mmap(0, msgmapsz, PROT_READ, MAP_SHARED, fd, 0);
110 1.21 dsl close(fd);
111 1.21 dsl if (msgmap == MAP_FAILED)
112 1.21 dsl return -1;
113 1.21 dsl /* check_magic */
114 1.22 dsl if (strcmp(msgmap, "MSGTXTS") != 0) {
115 1.21 dsl msg_file(NULL);
116 1.21 dsl return -1;
117 1.21 dsl }
118 1.21 dsl msgmapcount = atoi(msgmap + 8);
119 1.21 dsl return 0;
120 1.21 dsl }
121 1.21 dsl
122 1.21 dsl const char *
123 1.21 dsl msg_string(msg msg_no)
124 1.1 phil {
125 1.19 dsl int m = (intptr_t)msg_no;
126 1.19 dsl
127 1.19 dsl if (m > sizeof msg_list / sizeof msg_list[0])
128 1.19 dsl /* guess that we were passed a text string */
129 1.19 dsl return msg_no;
130 1.21 dsl
131 1.21 dsl if (msgmap != MAP_FAILED && m != 0 && m <= msgmapcount) {
132 1.21 dsl unsigned int offset = atoi(msgmap + 8 + 8 * m);
133 1.21 dsl if (offset != 0 && offset < msgmapsz)
134 1.21 dsl return msgmap + offset;
135 1.21 dsl }
136 1.21 dsl
137 1.19 dsl return msg_list[m];
138 1.1 phil }
139 1.1 phil
140 1.21 dsl void
141 1.21 dsl msg_clear(void)
142 1.1 phil {
143 1.21 dsl
144 1.21 dsl wclear(msg_win);
145 1.8 cgd last_o_was_punct = 0;
146 1.8 cgd last_o_was_space = 1;
147 1.1 phil }
148 1.1 phil
149 1.21 dsl void
150 1.21 dsl msg_standout(void)
151 1.1 phil {
152 1.21 dsl
153 1.1 phil wstandout(msg_win);
154 1.1 phil }
155 1.1 phil
156 1.21 dsl void
157 1.21 dsl msg_standend(void)
158 1.1 phil {
159 1.21 dsl
160 1.1 phil wstandend(msg_win);
161 1.1 phil }
162 1.1 phil
163 1.12 cgd static int
164 1.16 cgd _msg_vprintf(int auto_fill, const char *fmt, va_list ap)
165 1.1 phil {
166 1.8 cgd const char *wstart, *afterw;
167 1.8 cgd int wordlen, nspaces;
168 1.1 phil int ret;
169 1.1 phil
170 1.21 dsl ret = vsnprintf(cbuffer, cbuffersize, fmt, ap);
171 1.8 cgd
172 1.10 cgd if (!auto_fill) {
173 1.10 cgd waddstr(msg_win, cbuffer);
174 1.10 cgd
175 1.10 cgd /*
176 1.10 cgd * nothing is perfect if they flow text after a table,
177 1.10 cgd * but this may be decent.
178 1.10 cgd */
179 1.10 cgd last_i_was_nl = last_i_was_space = 1;
180 1.10 cgd last_o_was_punct = 0;
181 1.10 cgd last_o_was_space = 1;
182 1.10 cgd goto out;
183 1.10 cgd }
184 1.10 cgd
185 1.8 cgd for (wstart = afterw = cbuffer; *wstart; wstart = afterw) {
186 1.8 cgd
187 1.8 cgd /* eat one space, or a whole word of non-spaces */
188 1.8 cgd if (isspace(*afterw))
189 1.8 cgd afterw++;
190 1.8 cgd else
191 1.8 cgd while (*afterw && !isspace(*afterw))
192 1.8 cgd afterw++;
193 1.8 cgd
194 1.8 cgd /* this is an nl: special formatting necessary */
195 1.8 cgd if (*wstart == '\n') {
196 1.8 cgd if (last_i_was_nl || last_i_was_space) {
197 1.8 cgd
198 1.8 cgd if (getcurx(msg_win) != 0)
199 1.8 cgd waddch(msg_win, '\n');
200 1.8 cgd if (last_i_was_nl) {
201 1.8 cgd /* last was an nl: paragraph break */
202 1.8 cgd waddch(msg_win, '\n');
203 1.8 cgd } else {
204 1.8 cgd /* last was space: line break */
205 1.8 cgd }
206 1.8 cgd last_o_was_punct = 0;
207 1.8 cgd last_o_was_space = 1;
208 1.8 cgd } else {
209 1.8 cgd /* last_o_was_punct unchanged */
210 1.8 cgd /* last_o_was_space unchanged */
211 1.8 cgd }
212 1.8 cgd last_i_was_space = 1;
213 1.8 cgd last_i_was_nl = 1;
214 1.8 cgd continue;
215 1.8 cgd }
216 1.8 cgd
217 1.8 cgd /* this is a tab: special formatting necessary. */
218 1.8 cgd if (*wstart == '\t') {
219 1.8 cgd if (last_i_was_nl) {
220 1.8 cgd /* last was an nl: list indent */
221 1.8 cgd if (getcurx(msg_win) != 0)
222 1.8 cgd waddch(msg_win, '\n');
223 1.8 cgd } else {
224 1.8 cgd /* last was not an nl: columns */
225 1.8 cgd }
226 1.8 cgd waddch(msg_win, '\t');
227 1.8 cgd last_i_was_nl = 0;
228 1.8 cgd last_i_was_space = 1;
229 1.8 cgd last_o_was_punct = 0;
230 1.8 cgd last_o_was_space = 1;
231 1.8 cgd continue;
232 1.8 cgd }
233 1.8 cgd
234 1.8 cgd /* this is a space: ignore it but set flags */
235 1.8 cgd last_i_was_nl = 0; /* all newlines handled above */
236 1.8 cgd last_i_was_space = isspace(*wstart);
237 1.8 cgd if (last_i_was_space)
238 1.8 cgd continue;
239 1.8 cgd
240 1.8 cgd /*
241 1.8 cgd * we have a real "word," i.e. a sequence of non-space
242 1.8 cgd * characters. wstart is now the start of the word,
243 1.8 cgd * afterw is the next character after the end.
244 1.8 cgd */
245 1.8 cgd wordlen = afterw - wstart;
246 1.8 cgd nspaces = last_o_was_space ? 0 : (last_o_was_punct ? 2 : 1);
247 1.8 cgd if ((getcurx(msg_win) + nspaces + wordlen) >=
248 1.8 cgd getmaxx(msg_win) &&
249 1.8 cgd wordlen < (getmaxx(msg_win) / 3)) {
250 1.8 cgd /* wrap the line */
251 1.8 cgd waddch(msg_win, '\n');
252 1.8 cgd nspaces = 0;
253 1.8 cgd }
254 1.8 cgd
255 1.8 cgd /* output the word, preceded by spaces if necessary */
256 1.8 cgd while (nspaces-- > 0)
257 1.8 cgd waddch(msg_win, ' ');
258 1.8 cgd waddbytes(msg_win, wstart, wordlen);
259 1.8 cgd
260 1.8 cgd /* set up the 'last' state for the next time around */
261 1.8 cgd switch (afterw[-1]) {
262 1.8 cgd case '.':
263 1.8 cgd case '?':
264 1.8 cgd case '!':
265 1.8 cgd last_o_was_punct = 1;
266 1.8 cgd break;
267 1.8 cgd default:
268 1.8 cgd last_o_was_punct = 0;
269 1.8 cgd break;
270 1.8 cgd }
271 1.8 cgd last_o_was_space = 0;
272 1.8 cgd
273 1.8 cgd /* ... and do it all again! */
274 1.8 cgd }
275 1.8 cgd
276 1.8 cgd /* String ended with a newline. They really want a line break. */
277 1.8 cgd if (last_i_was_nl) {
278 1.8 cgd if (getcurx(msg_win) != 0)
279 1.8 cgd waddch(msg_win, '\n');
280 1.8 cgd last_o_was_punct = 0;
281 1.8 cgd last_o_was_space = 1;
282 1.8 cgd }
283 1.8 cgd
284 1.10 cgd out:
285 1.21 dsl wrefresh(msg_win);
286 1.1 phil return ret;
287 1.1 phil }
288 1.1 phil
289 1.21 dsl void
290 1.21 dsl msg_display(msg msg_no, ...)
291 1.1 phil {
292 1.1 phil va_list ap;
293 1.1 phil
294 1.7 cgd msg_clear();
295 1.7 cgd
296 1.1 phil va_start(ap, msg_no);
297 1.16 cgd (void)_msg_vprintf(1, msg_string(msg_no), ap);
298 1.1 phil va_end(ap);
299 1.1 phil }
300 1.1 phil
301 1.21 dsl void
302 1.21 dsl msg_display_add(msg msg_no, ...)
303 1.1 phil {
304 1.1 phil va_list ap;
305 1.1 phil
306 1.21 dsl va_start(ap, msg_no);
307 1.16 cgd (void)_msg_vprintf(1, msg_string(msg_no), ap);
308 1.21 dsl va_end(ap);
309 1.1 phil }
310 1.1 phil
311 1.19 dsl static void
312 1.19 dsl _erase_ch(void)
313 1.19 dsl {
314 1.19 dsl int y, x;
315 1.19 dsl
316 1.19 dsl getyx(msg_win, y, x);
317 1.19 dsl x--;
318 1.19 dsl wmove(msg_win, y, x);
319 1.19 dsl waddch(msg_win, ' ');
320 1.19 dsl wmove(msg_win, y, x);
321 1.19 dsl }
322 1.1 phil
323 1.12 cgd static void
324 1.24 dsl _msg_vprompt(const char *fmt, int do_echo, const char *def, char *val,
325 1.18 christos size_t max_chars, va_list ap)
326 1.1 phil {
327 1.1 phil int ch;
328 1.1 phil int count = 0;
329 1.3 cgd char *ibuf = alloca(max_chars);
330 1.1 phil
331 1.24 dsl _msg_vprintf(0, fmt, ap);
332 1.1 phil if (def != NULL && *def) {
333 1.21 dsl waddstr(msg_win, " [");
334 1.21 dsl waddstr(msg_win, def);
335 1.21 dsl waddstr(msg_win, "]");
336 1.1 phil }
337 1.21 dsl waddstr(msg_win, ": ");
338 1.21 dsl wrefresh(msg_win);
339 1.1 phil
340 1.1 phil while ((ch = wgetch(msg_win)) != '\n') {
341 1.1 phil if (ch == 0x08 || ch == 0x7f) { /* bs or del */
342 1.1 phil if (count > 0) {
343 1.1 phil count--;
344 1.19 dsl if (do_echo)
345 1.19 dsl _erase_ch();
346 1.1 phil } else
347 1.14 cgd _msg_beep();
348 1.4 cgd } else if (ch == 0x15) { /* ^U; line kill */
349 1.4 cgd while (count > 0) {
350 1.9 cgd count--;
351 1.19 dsl if (do_echo)
352 1.19 dsl _erase_ch();
353 1.9 cgd }
354 1.9 cgd } else if (ch == 0x17) { /* ^W; word kill */
355 1.9 cgd /*
356 1.9 cgd * word kill kills the spaces and the 'word'
357 1.9 cgd * (non-spaces) last typed. the spaces before
358 1.9 cgd * the 'word' aren't killed.
359 1.9 cgd */
360 1.9 cgd while (count > 0 && isspace(ibuf[count - 1])) {
361 1.9 cgd count--;
362 1.19 dsl if (do_echo)
363 1.19 dsl _erase_ch();
364 1.9 cgd }
365 1.9 cgd while (count > 0 && !isspace(ibuf[count - 1])) {
366 1.4 cgd count--;
367 1.19 dsl if (do_echo)
368 1.19 dsl _erase_ch();
369 1.4 cgd }
370 1.4 cgd } else if (count < (max_chars - 1) && isprint(ch)) {
371 1.1 phil if (do_echo)
372 1.21 dsl waddch(msg_win, ch);
373 1.3 cgd ibuf[count++] = ch;
374 1.1 phil } else
375 1.14 cgd _msg_beep();
376 1.1 phil if (do_echo)
377 1.1 phil wrefresh(msg_win);
378 1.1 phil }
379 1.8 cgd if (do_echo) {
380 1.1 phil waddch(msg_win, '\n');
381 1.8 cgd last_o_was_punct = 0;
382 1.8 cgd last_o_was_space = 1;
383 1.8 cgd }
384 1.1 phil
385 1.3 cgd /* copy the appropriate string to the output */
386 1.3 cgd if (count != 0) {
387 1.3 cgd ibuf[count] = '\0';
388 1.23 itojun strlcpy(val, ibuf, max_chars);
389 1.5 cgd } else if (def != NULL && val != def) {
390 1.19 dsl strlcpy(val, def, max_chars);
391 1.3 cgd }
392 1.1 phil }
393 1.1 phil
394 1.13 cgd void
395 1.18 christos msg_prompt(msg msg_no, const char *def, char *val, size_t max_chars, ...)
396 1.1 phil {
397 1.1 phil va_list ap;
398 1.1 phil
399 1.13 cgd msg_clear();
400 1.13 cgd
401 1.21 dsl va_start(ap, max_chars);
402 1.16 cgd _msg_vprompt(msg_string(msg_no), 1, def, val, max_chars, ap);
403 1.21 dsl va_end(ap);
404 1.19 dsl }
405 1.19 dsl
406 1.19 dsl void
407 1.20 dsl msg_prompt_win(msg msg_no, int x, int y, int w, int h,
408 1.19 dsl const char *def, char *val, size_t max_chars, ...)
409 1.19 dsl {
410 1.19 dsl va_list ap;
411 1.20 dsl WINDOW *win, *svwin;
412 1.20 dsl int maxx, maxy;
413 1.19 dsl
414 1.21 dsl maxx = getmaxx(msg_win);
415 1.21 dsl maxy = getmaxy(msg_win);
416 1.20 dsl if (w == 0) {
417 1.20 dsl va_start(ap, max_chars);
418 1.20 dsl w = vsnprintf(NULL, 0, msg_string(msg_no), ap);
419 1.20 dsl va_end(ap);
420 1.20 dsl if (def != NULL && *def != 0)
421 1.22 dsl w += 2 + strlen(def) + 1;
422 1.20 dsl w += 1 + 2 + max_chars + 1;
423 1.20 dsl if (w > maxx)
424 1.20 dsl w = maxx;
425 1.20 dsl }
426 1.19 dsl
427 1.20 dsl if (x == -1)
428 1.20 dsl x = (maxx - w) / 2;
429 1.20 dsl if (h < 3)
430 1.20 dsl h = 3;
431 1.20 dsl if (y < 3)
432 1.20 dsl y = (maxy - h) / 2;
433 1.20 dsl if (y + h > maxy)
434 1.20 dsl y = maxy - h;
435 1.20 dsl
436 1.21 dsl win = subwin(msg_win, h, w, y, x);
437 1.20 dsl if (win == NULL)
438 1.20 dsl wprintw(msg_win, "msg_prompt_win: "
439 1.20 dsl "newwin(%d, %d, %d, %d) failed\n",
440 1.20 dsl h, w, y, x);
441 1.20 dsl else {
442 1.20 dsl wbkgd(win, getbkgd(msg_win));
443 1.20 dsl wattrset(win, getattrs(msg_win));
444 1.20 dsl box(win, 0, 0);
445 1.20 dsl wrefresh(win);
446 1.19 dsl
447 1.21 dsl svwin = msg_window(subwin(msg_win, h - 2, w - 2, y + 1, x + 1));
448 1.20 dsl wbkgd(msg_win, getbkgd(win));
449 1.20 dsl wattrset(msg_win, getattrs(win));
450 1.19 dsl
451 1.20 dsl msg_clear();
452 1.19 dsl }
453 1.19 dsl
454 1.20 dsl va_start(ap, max_chars);
455 1.19 dsl _msg_vprompt(msg_string(msg_no), 1, def, val, max_chars, ap);
456 1.20 dsl va_end(ap);
457 1.19 dsl
458 1.20 dsl if (win != NULL) {
459 1.20 dsl wclear(win);
460 1.20 dsl wrefresh(win);
461 1.20 dsl delwin(msg_window(svwin));
462 1.20 dsl delwin(win);
463 1.20 dsl }
464 1.13 cgd }
465 1.13 cgd
466 1.13 cgd void
467 1.18 christos msg_prompt_add(msg msg_no, const char *def, char *val, size_t max_chars, ...)
468 1.13 cgd {
469 1.13 cgd va_list ap;
470 1.13 cgd
471 1.21 dsl va_start(ap, max_chars);
472 1.16 cgd _msg_vprompt(msg_string(msg_no), 1, def, val, max_chars, ap);
473 1.1 phil va_end(ap);
474 1.1 phil }
475 1.1 phil
476 1.13 cgd void
477 1.18 christos msg_prompt_noecho(msg msg_no, const char *def, char *val, size_t max_chars, ...)
478 1.1 phil {
479 1.1 phil va_list ap;
480 1.1 phil
481 1.7 cgd msg_clear();
482 1.7 cgd
483 1.21 dsl va_start(ap, max_chars);
484 1.16 cgd _msg_vprompt(msg_string(msg_no), 0, def, val, max_chars, ap);
485 1.21 dsl va_end(ap);
486 1.1 phil }
487 1.10 cgd
488 1.21 dsl void
489 1.21 dsl msg_table_add(msg msg_no, ...)
490 1.10 cgd {
491 1.10 cgd va_list ap;
492 1.10 cgd
493 1.21 dsl va_start(ap, msg_no);
494 1.16 cgd (void)_msg_vprintf(0, msg_string(msg_no), ap);
495 1.21 dsl va_end(ap);
496 1.10 cgd }
497 1.10 cgd
498 1.26 dsl int
499 1.26 dsl msg_row(void)
500 1.26 dsl {
501 1.26 dsl
502 1.26 dsl return getcury(msg_win) + getbegy(msg_win);
503 1.26 dsl }
504