1/* $NetBSD: msg_sys.def,v 1.41 2012/03/06 16:26:01 mbalmer 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. The name of Piermont Information Systems Inc. may not be used to endorse 18 * or promote products derived from this software without specific prior 19 * written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS'' 22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE 25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 31 * THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 */ 34 35static WINDOW *msg_win = NULL; 36static char *cbuffer; 37static size_t cbuffersize; 38 39static int last_i_was_nl, last_i_was_space; 40static int last_o_was_punct, last_o_was_space; 41 42static void _msg_beep(void); 43static int _msg_vprintf(int, const char *, va_list); 44#define MSG_PROMPT_ECHO 1 45#define MSG_PROMPT_HIDE_DFLT 2 46static void _msg_vprompt(const char *, int, const char *, char *, 47 size_t, va_list); 48 49static char *msgmap = MAP_FAILED; 50static size_t msgmapsz; 51static unsigned int msgmapcount; 52 53/* Routines */ 54 55static void 56_msg_beep(void) 57{ 58 59 fprintf(stderr, "\a"); 60} 61 62WINDOW * 63msg_window(WINDOW *window) 64{ 65 size_t ncbuffersize; 66 char *ncbuffer; 67 WINDOW *old; 68 69 old = msg_win; 70 if (!window) 71 return old; 72 msg_win = window; 73 74 ncbuffersize = getmaxx(window) * getmaxy(window) + 1; 75 while (ncbuffersize > cbuffersize) { 76 ncbuffer = malloc(ncbuffersize); 77 if (ncbuffer == NULL) { 78 /* we might get truncated messages... */ 79 ncbuffersize <<= 1; 80 continue; 81 } 82 if (cbuffer != NULL) 83 free(cbuffer); 84 cbuffer = ncbuffer; 85 cbuffersize = ncbuffersize; 86 break; 87 } 88 last_o_was_punct = 0; 89 last_o_was_space = 1; 90 return old; 91} 92 93int 94msg_file(const char *file) 95{ 96 int fd; 97 98 if (msgmap != MAP_FAILED) 99 munmap(msgmap, msgmapsz); 100 msgmap = MAP_FAILED; 101 if (!file) 102 return 0; 103 fd = open(file, O_RDONLY, 0); 104 if (fd == -1) 105 return -1; 106 msgmapsz = lseek(fd, 0, SEEK_END); 107 msgmap = mmap(0, msgmapsz, PROT_READ, MAP_SHARED, fd, 0); 108 close(fd); 109 if (msgmap == MAP_FAILED) 110 return -1; 111 /* check_magic */ 112 if (strcmp(msgmap, "MSGTXTS") != 0) { 113 msg_file(NULL); 114 return -1; 115 } 116 msgmapcount = atoi(msgmap + 8); 117 return 0; 118} 119 120const char * 121msg_string(msg msg_no) 122{ 123 uintptr_t m = (uintptr_t)msg_no; 124 125 if (m > sizeof msg_list / sizeof msg_list[0]) 126 /* guess that we were passed a text string */ 127 return msg_no; 128 129 if (msgmap != MAP_FAILED && m != 0 && m <= msgmapcount) { 130 unsigned int offset = atoi(msgmap + 8 + 8 * m); 131 if (offset != 0 && offset < msgmapsz) 132 return msgmap + offset; 133 } 134 135 return msg_list[m]; 136} 137 138void 139msg_clear(void) 140{ 141 142 wclear(msg_win); 143 last_o_was_punct = 0; 144 last_o_was_space = 1; 145} 146 147void 148msg_standout(void) 149{ 150 151 wstandout(msg_win); 152} 153 154void 155msg_standend(void) 156{ 157 158 wstandend(msg_win); 159} 160 161static int 162_msg_vprintf(int auto_fill, const char *fmt, va_list ap) 163{ 164 const char *wstart, *afterw; 165 int wordlen, nspaces; 166 int ret; 167 168 ret = vsnprintf(cbuffer, cbuffersize, fmt, ap); 169 170 if (!auto_fill) { 171 waddstr(msg_win, cbuffer); 172 173 /* 174 * nothing is perfect if they flow text after a table, 175 * but this may be decent. 176 */ 177 last_i_was_nl = last_i_was_space = 1; 178 last_o_was_punct = 0; 179 last_o_was_space = 1; 180 goto out; 181 } 182 183 for (wstart = afterw = cbuffer; *wstart; wstart = afterw) { 184 185 /* eat one space, or a whole word of non-spaces */ 186 if (isspace((unsigned char)*afterw)) 187 afterw++; 188 else 189 while (*afterw && !isspace((unsigned char)*afterw)) 190 afterw++; 191 192 /* this is an nl: special formatting necessary */ 193 if (*wstart == '\n') { 194 if (last_i_was_nl || last_i_was_space) { 195 196 if (getcurx(msg_win) != 0) 197 waddch(msg_win, '\n'); 198 if (last_i_was_nl) { 199 /* last was an nl: paragraph break */ 200 waddch(msg_win, '\n'); 201 } else { 202 /* last was space: line break */ 203 } 204 last_o_was_punct = 0; 205 last_o_was_space = 1; 206 } else { 207 /* last_o_was_punct unchanged */ 208 /* last_o_was_space unchanged */ 209 } 210 last_i_was_space = 1; 211 last_i_was_nl = 1; 212 continue; 213 } 214 215 /* this is a tab: special formatting necessary. */ 216 if (*wstart == '\t') { 217 if (last_i_was_nl) { 218 /* last was an nl: list indent */ 219 if (getcurx(msg_win) != 0) 220 waddch(msg_win, '\n'); 221 } else { 222 /* last was not an nl: columns */ 223 } 224 waddch(msg_win, '\t'); 225 last_i_was_nl = 0; 226 last_i_was_space = 1; 227 last_o_was_punct = 0; 228 last_o_was_space = 1; 229 continue; 230 } 231 232 /* this is a space: ignore it but set flags */ 233 last_i_was_nl = 0; /* all newlines handled above */ 234 last_i_was_space = isspace((unsigned char)*wstart); 235 if (last_i_was_space) 236 continue; 237 238 /* 239 * we have a real "word," i.e. a sequence of non-space 240 * characters. wstart is now the start of the word, 241 * afterw is the next character after the end. 242 */ 243 wordlen = afterw - wstart; 244 nspaces = last_o_was_space ? 0 : (last_o_was_punct ? 2 : 1); 245 if ((getcurx(msg_win) + nspaces + wordlen) >= 246 getmaxx(msg_win) && 247 wordlen < (getmaxx(msg_win) / 3)) { 248 /* wrap the line */ 249 waddch(msg_win, '\n'); 250 nspaces = 0; 251 } 252 253 /* output the word, preceded by spaces if necessary */ 254 while (nspaces-- > 0) 255 waddch(msg_win, ' '); 256 waddbytes(msg_win, wstart, wordlen); 257 258 /* set up the 'last' state for the next time around */ 259 switch (afterw[-1]) { 260 case '.': 261 case '?': 262 case '!': 263 last_o_was_punct = 1; 264 break; 265 default: 266 last_o_was_punct = 0; 267 break; 268 } 269 last_o_was_space = 0; 270 271 /* ... and do it all again! */ 272 } 273 274 /* String ended with a newline. They really want a line break. */ 275 if (last_i_was_nl) { 276 if (getcurx(msg_win) != 0) 277 waddch(msg_win, '\n'); 278 last_o_was_punct = 0; 279 last_o_was_space = 1; 280 } 281 282out: 283 wrefresh(msg_win); 284 return ret; 285} 286 287void 288msg_display(msg msg_no, ...) 289{ 290 va_list ap; 291 292 msg_clear(); 293 294 va_start(ap, msg_no); 295 (void)_msg_vprintf(1, msg_string(msg_no), ap); 296 va_end(ap); 297} 298 299void 300msg_display_add(msg msg_no, ...) 301{ 302 va_list ap; 303 304 va_start(ap, msg_no); 305 (void)_msg_vprintf(1, msg_string(msg_no), ap); 306 va_end(ap); 307} 308 309void 310msg_printf(const char *fmt, ...) 311{ 312 va_list ap; 313 314 va_start(ap, fmt); 315 (void)_msg_vprintf(1, fmt, ap); 316 va_end(ap); 317} 318 319static void 320_msg_vprompt(const char *fmt, int flags, const char *def, char *val, 321 size_t val_buf_len, va_list ap) 322{ 323 int ch; 324 int len, pos, npos, off; 325 int first; 326 int txt_y, txt_x; 327 char *ibuf; 328 int maxx; 329 330 if (val == NULL || val_buf_len == 0) { 331 /* No answer wanted */ 332 val = NULL; 333 val_buf_len = 1; 334 } 335 336 ibuf = malloc(val_buf_len); 337 338 keypad(msg_win, TRUE); 339 _msg_vprintf(0, fmt, ap); 340 ibuf[0] = 0; 341 if (def != NULL && *def) { 342 if (flags & MSG_PROMPT_HIDE_DFLT) 343 strlcpy(ibuf, def, val_buf_len); 344 else { 345 waddstr(msg_win, " ["); 346 waddstr(msg_win, def); 347 waddstr(msg_win, "]"); 348 } 349 } 350 waddstr(msg_win, ": "); 351 len = strlen(ibuf); 352 pos = len; 353 getyx(msg_win, txt_y, txt_x); 354 maxx = getmaxx(msg_win) - txt_x - 1; 355 off = 0; 356 357 for (first = 1; ; first = 0) { 358 359 if (flags & MSG_PROMPT_ECHO) { 360 /* shift text right as we near the buffer start */ 361 if (pos - off < 4) 362 off = pos - 4; 363 /* keep offset to a minimum if we are at the end */ 364 if (pos == len) 365 off = pos - maxx; 366 if (off < 0 || len <= maxx) 367 off = 0; 368 /* shift text left as we near the buffer end */ 369 npos = pos + 4; 370 if (npos > len) 371 npos = len; 372 if (npos - off > maxx) 373 off = npos - maxx; 374 /* calc. length to display */ 375 npos = len - off; 376 if (npos > maxx) 377 npos = maxx; 378 mvwaddnstr(msg_win, txt_y, txt_x, ibuf + off, npos); 379 wclrtoeol(msg_win); 380 if (off != 0) 381 mvwaddstr(msg_win, txt_y, txt_x, "+"); 382 wmove(msg_win, txt_y, txt_x + pos - off); 383 wrefresh(msg_win); 384 } 385 386 ch = wgetch(msg_win); 387 if (ch == '\n') 388 break; 389 390 switch (ch) { 391 case KEY_BACKSPACE: 392 case 'h' & 0x1f: case 0x7f: /* bs or del - delete left */ 393 if (first) { 394 /* delete all of default string */ 395 len = pos = 0; 396 break; 397 } 398 if (pos > 0) { 399 memmove(ibuf + pos - 1, ibuf + pos, len - pos); 400 len--; 401 pos--; 402 } else 403 _msg_beep(); 404 break; 405 case 'u' & 0x1f: /* ^U; line kill */ 406 /* kill line */ 407 len = pos = 0; 408 break; 409 case 'w' & 0x1f: /* ^W; word kill */ 410 /* 411 * word kill kills the spaces and the 'word' 412 * (non-spaces) last typed. the spaces before 413 * the 'word' aren't killed. 414 */ 415 npos = pos; 416 while (npos > 0 && isspace((unsigned char)ibuf[npos - 1])) 417 npos--; 418 while (npos > 0 && !isspace((unsigned char)ibuf[npos - 1])) 419 npos--; 420 memmove(ibuf + npos, ibuf + pos, len - pos); 421 len -= pos - npos; 422 pos = npos; 423 break; 424 case KEY_LEFT: 425 if (pos > 0) 426 pos--; 427 break; 428 case KEY_RIGHT: 429 if (len == 0 && pos == 0 && def != NULL) { 430 /* restore default! */ 431 strlcpy(ibuf, def, val_buf_len); 432 len = pos = strlen(ibuf); 433 break; 434 } 435 if (pos < len) 436 pos++; 437 break; 438 default: 439 if (len < (int)(val_buf_len - 1) && isprint(ch)) { 440 memmove(ibuf + pos + 1, ibuf + pos, len - pos); 441 ibuf[pos++] = ch; 442 len++; 443 } else 444 _msg_beep(); 445 break; 446 } 447 } 448 449 if (flags & MSG_PROMPT_ECHO) { 450 mvwaddch(msg_win, txt_y, txt_x + len - off, '\n'); 451 last_o_was_punct = 0; 452 last_o_was_space = 1; 453 } 454 455 if (val != NULL) { 456 /* copy the appropriate string to the output */ 457 if (len != 0 || flags & MSG_PROMPT_HIDE_DFLT) { 458 ibuf[len] = '\0'; 459 strlcpy(val, ibuf, val_buf_len); 460 } else if (def != NULL && val != def) { 461 strlcpy(val, def, val_buf_len); 462 } 463 } 464 free(ibuf); 465} 466 467void 468msg_prompt(msg msg_no, const char *def, char *val, size_t val_buf_len, ...) 469{ 470 va_list ap; 471 472 msg_clear(); 473 474 va_start(ap, val_buf_len); 475 _msg_vprompt(msg_string(msg_no), MSG_PROMPT_ECHO, 476 def, val, val_buf_len, ap); 477 va_end(ap); 478} 479 480void 481msg_prompt_win(msg msg_no, int x, int y, int w, int h, 482 const char *def, char *val, size_t val_buf_len, ...) 483{ 484 va_list ap; 485 WINDOW *win; 486 WINDOW *svmsg = NULL, *sv_win = NULL; /* XXX -Wuninitialized [many] */ 487 int maxx, maxy; 488 int msg_flags = MSG_PROMPT_ECHO | MSG_PROMPT_HIDE_DFLT; 489 490 maxx = getmaxx(msg_win); 491 maxy = getmaxy(msg_win); 492 if (w == 0) { 493 va_start(ap, val_buf_len); 494 w = vsnprintf(NULL, 0, msg_string(msg_no), ap); 495 va_end(ap); 496 if (def != NULL && *def != 0 && w + (int)val_buf_len * 2 < maxx) { 497 w += 2 + strlen(def) + 1; 498 msg_flags &= ~MSG_PROMPT_HIDE_DFLT; 499 } 500 w += 1 + 2 + val_buf_len + 1; 501 if (w > maxx) { 502 if (!(msg_flags & MSG_PROMPT_HIDE_DFLT)) { 503 w -= 2 + strlen(def) + 1; 504 msg_flags |= MSG_PROMPT_HIDE_DFLT; 505 } 506 w = maxx; 507 } 508 } 509 510 if (x == -1) 511 x = (maxx - w) / 2 + 1; 512 if (h < 3) 513 h = 3; 514 if (y < 3) 515 y = (maxy - h) / 2; 516 if (y + h > maxy) 517 y = maxy - h; 518 519 win = subwin(msg_win, h, w, y, x); 520 if (win == NULL) 521 wprintw(msg_win, "msg_prompt_win: " 522 "newwin(%d, %d, %d, %d) failed\n", 523 h, w, y, x); 524 else { 525 /* 526 * Save screen contents from under our window 527 * Due to a mis-feature of NetBSD curses, curscr contains 528 * the data processed by doupdate() not that by wnoutrefresh(). 529 * We must call doupdate() here to ensure we save the correct 530 * data. See PR 26660 531 */ 532 doupdate(); 533 sv_win = dupwin(win); 534 if (sv_win) 535 overwrite(curscr, sv_win); 536 wbkgd(win, getbkgd(msg_win)); 537 wattrset(win, getattrs(msg_win)); 538 box(win, 0, 0); 539 wrefresh(win); 540 541 /* Change message window to be our little box */ 542 svmsg = msg_window(subwin(msg_win, h - 2, w - 2, y + 1, x + 1)); 543 wbkgd(msg_win, getbkgd(win)); 544 wattrset(msg_win, getattrs(win)); 545 546 msg_clear(); 547 } 548 549 va_start(ap, val_buf_len); 550 _msg_vprompt(msg_string(msg_no), msg_flags, def, val, val_buf_len, ap); 551 va_end(ap); 552 553 if (win != NULL) { 554 wclear(win); 555 if (sv_win) { 556 /* Restore original screen contents */ 557 overwrite(sv_win, win); 558 delwin(sv_win); 559 } 560 wnoutrefresh(win); 561 /* Restore normal message window */ 562 delwin(msg_window(svmsg)); 563 delwin(win); 564 } 565} 566 567void 568msg_prompt_add(msg msg_no, const char *def, char *val, size_t val_buf_len, ...) 569{ 570 va_list ap; 571 572 va_start(ap, val_buf_len); 573 _msg_vprompt(msg_string(msg_no), MSG_PROMPT_ECHO, def, val, val_buf_len, ap); 574 va_end(ap); 575} 576 577void 578msg_prompt_noecho(msg msg_no, const char *def, char *val, size_t val_buf_len, ...) 579{ 580 va_list ap; 581 582 msg_clear(); 583 584 va_start(ap, val_buf_len); 585 _msg_vprompt(msg_string(msg_no), 0, def, val, val_buf_len, ap); 586 va_end(ap); 587} 588 589void 590msg_table_add(msg msg_no, ...) 591{ 592 va_list ap; 593 594 va_start(ap, msg_no); 595 (void)_msg_vprintf(0, msg_string(msg_no), ap); 596 va_end(ap); 597} 598 599int 600msg_row(void) 601{ 602 603 return getcury(msg_win) + getbegy(msg_win); 604} 605