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