menu_sys.def revision 1.18
1/* $NetBSD: menu_sys.def,v 1.18 1999/06/20 01:20:14 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/* menu_sys.defs -- Menu system standard routines. */ 40 41#include <string.h> 42#include <ctype.h> 43 44#define REQ_EXECUTE 1000 45#define REQ_NEXT_ITEM 1001 46#define REQ_PREV_ITEM 1002 47#define REQ_REDISPLAY 1003 48#define REQ_SCROLLDOWN 1004 49#define REQ_SCROLLUP 1005 50#define REQ_HELP 1006 51 52/* Multiple key support */ 53#define KEYSEQ_FIRST 256 54#define KEYSEQ_DOWN_ARROW 256 55#define KEYSEQ_UP_ARROW 257 56#define KEYSEQ_LEFT_ARROW 258 57#define KEYSEQ_RIGHT_ARROW 259 58#define KEYSEQ_PAGE_DOWN 260 59#define KEYSEQ_PAGE_UP 261 60 61struct keyseq { 62 char *termcap_name; 63 char *chars; 64 int numchars; 65 int keyseq_val; 66 struct keyseq *next; 67}; 68 69/* keypad and other definitions */ 70struct keyseq _mc_key_seq[] = { 71 /* Cludge for xterm ... */ 72 { NULL, "\033[B", 0, KEYSEQ_DOWN_ARROW, NULL }, 73 { NULL, "\033[D", 0, KEYSEQ_LEFT_ARROW, NULL }, 74 { NULL, "\033[C", 0, KEYSEQ_RIGHT_ARROW, NULL }, 75 { NULL, "\033[A", 0, KEYSEQ_UP_ARROW, NULL }, 76 /* Termcap defined */ 77 { "kd", NULL, 0, KEYSEQ_DOWN_ARROW, NULL }, 78 { "kl", NULL, 0, KEYSEQ_LEFT_ARROW, NULL }, 79 { "kr", NULL, 0, KEYSEQ_RIGHT_ARROW, NULL }, 80 { "ku", NULL, 0, KEYSEQ_UP_ARROW, NULL }, 81 { "kf", NULL, 0, KEYSEQ_PAGE_DOWN, NULL }, /* scroll forward */ 82 { "kN", NULL, 0, KEYSEQ_PAGE_DOWN, NULL }, /* next page */ 83 { "kP", NULL, 0, KEYSEQ_PAGE_UP, NULL }, /* scroll backward */ 84 { "kR", NULL, 0, KEYSEQ_PAGE_UP, NULL }, /* prev page */ 85 /* other definitions */ 86 { NULL, "\033v", 0, KEYSEQ_PAGE_UP, NULL }, /* ESC-v */ 87 { NULL, "\026", 0, KEYSEQ_PAGE_DOWN, NULL }, /* CTL-v */ 88}; 89 90int _mc_num_key_seq = sizeof(_mc_key_seq) / sizeof(struct keyseq); 91struct keyseq *pad_list = NULL; 92static char str_area [512]; 93static char *str_ptr = str_area; 94 95/* Macros */ 96#define MAX(x,y) ((x)>(y)?(x):(y)) 97#define MIN(x,y) ((x)<(y)?(x):(y)) 98 99/* Initialization state. */ 100static int __menu_init = 0; 101int __m_endwin = 0; 102static int max_lines = 0, max_cols = 0; 103static char *scrolltext = " <: page up, >: page down"; 104 105static menudesc *menus = menu_def; 106 107#ifdef DYNAMIC_MENUS 108static int num_menus = 0; 109static int num_avail = 0; 110#define DYN_INIT_NUM 32 111#endif 112 113/* prototypes for in here! */ 114static void ins_keyseq (struct keyseq **seq, struct keyseq *ins); 115static void init_keyseq (void); 116static void init_menu (struct menudesc *m); 117static char opt_ch (int op_no); 118static void post_menu (struct menudesc *m); 119static void process_help (struct menudesc *m, int num); 120static void process_req (struct menudesc *m, int num, int req); 121static void mbeep (void); 122static int menucmd (WINDOW *w); 123 124#ifndef NULL 125#define NULL (void *)0 126#endif 127 128/* menu system processing routines */ 129 130static void mbeep (void) 131{ 132 fprintf (stderr,"\a"); 133} 134 135static void ins_keyseq (struct keyseq **seq, struct keyseq *ins) 136{ 137 if (*seq == NULL) { 138 ins->next = NULL; 139 *seq = ins; 140 } else if (ins->numchars <= (*seq)->numchars) { 141 ins->next = *seq; 142 *seq = ins; 143 } else 144 ins_keyseq (&(*seq)->next, ins); 145} 146 147static void init_keyseq (void) 148{ 149 int i; 150 for (i=0; i<_mc_num_key_seq; i++) { 151 if (_mc_key_seq[i].termcap_name) 152 _mc_key_seq[i].chars = 153 tgetstr (_mc_key_seq[i].termcap_name, 154 &str_ptr); 155 if (_mc_key_seq[i].chars != NULL && 156 (_mc_key_seq[i].numchars = strlen(_mc_key_seq[i].chars)) 157 > 0) 158 ins_keyseq (&pad_list,&_mc_key_seq[i]); 159 } 160} 161 162static int mgetch(WINDOW *w) 163{ 164 static char buf[20]; 165 static int num = 0; 166 struct keyseq *list = pad_list; 167 int i, ret; 168 169 /* key pad processing */ 170 while (list) { 171 for (i=0; i< list->numchars; i++) { 172 if (i >= num) 173 buf[num++] = wgetch(w); 174 if (buf[i] != list->chars[i]) 175 break; 176 } 177 if (i == list->numchars) { 178 num = 0; 179 return list->keyseq_val; 180 } 181 list = list->next; 182 } 183 184 ret = buf[0]; 185 for (i = 0; i < strlen(buf); i++) 186 buf[i] = buf[i+1]; 187 num--; 188 return ret; 189} 190 191static int menucmd (WINDOW *w) 192{ 193 int ch; 194 195 while (TRUE) { 196 ch = mgetch(w); 197 198 switch (ch) { 199 case '\n': 200 return REQ_EXECUTE; 201 case '\016': /* Contnrol-P */ 202 case KEYSEQ_DOWN_ARROW: 203 return REQ_NEXT_ITEM; 204 case '\020': /* Control-N */ 205 case KEYSEQ_UP_ARROW: 206 return REQ_PREV_ITEM; 207 case '\014': /* Control-L */ 208 return REQ_REDISPLAY; 209 case '<': 210 case '\010': /* Control-H (backspace) */ 211 case KEYSEQ_PAGE_UP: 212 return REQ_SCROLLUP; 213 case '>': 214 case ' ': 215 case KEYSEQ_PAGE_DOWN: 216 return REQ_SCROLLDOWN; 217 case '?': 218 return REQ_HELP; 219 } 220 221 if (isalpha(ch)) 222 return (ch); 223 224 mbeep(); 225 wrefresh(w); 226 } 227} 228 229static void init_menu (struct menudesc *m) 230{ 231 int wmax; 232 int hadd, wadd, exithadd; 233 int i; 234 235 hadd = ((m->mopt & MC_NOBOX) ? 0 : 2); 236 wadd = ((m->mopt & MC_NOBOX) ? 2 : 4); 237 238 hadd += strlen(m->title) != 0 ? 2 : 0; 239 exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1); 240 241 wmax = strlen(m->title); 242 243 /* Calculate h? h == number of visible options. */ 244 if (m->h == 0) { 245 m->h = m->numopts + exithadd; 246 if (m->h + m->y + hadd >= max_lines && (m->mopt & MC_SCROLL)) 247 m->h = max_lines - m->y - hadd ; 248 } 249 250 /* Check window heights and set scrolling */ 251 if (m->h < m->numopts + exithadd) { 252 if (!(m->mopt & MC_SCROLL) || m->h < 3) { 253 endwin(); 254 (void) fprintf (stderr, 255 "Window too short for menu \"%s\"\n", 256 m->title); 257 exit(1); 258 } 259 } else 260 m->mopt &= ~MC_SCROLL; 261 262 /* check for screen fit */ 263 if (m->y + m->h + hadd > max_lines) { 264 endwin(); 265 (void) fprintf (stderr, 266 "Screen too short for menu \"%s\"\n", m->title); 267 exit(1); 268 269 } 270 271 /* Calculate w? */ 272 if (m->w == 0) { 273 if (m->mopt & MC_SCROLL) 274 wmax = MAX(wmax,strlen(scrolltext)); 275 for (i=0; i < m->numopts; i++ ) 276 wmax = MAX(wmax,strlen(m->opts[i].opt_name)+3); 277 m->w = wmax; 278 } 279 280 /* check and adjust for screen fit */ 281 if (m->w + wadd > max_cols) { 282 endwin(); 283 (void) fprintf (stderr, 284 "Screen too narrow for menu \"%s\"\n", m->title); 285 exit(1); 286 287 } 288 if (m->x + m->w + wadd > max_cols) 289 m->x = max_cols - (m->w + wadd); 290 291 /* Get the windows. */ 292 m->mw = newwin(m->h+hadd, m->w+wadd, m->y, m->x); 293 294 if (m->mw == NULL) { 295 endwin(); 296 (void) fprintf (stderr, 297 "Could not create window for menu \"%s\"\n", m->title); 298 exit(1); 299 } 300} 301 302static char opt_ch (int op_no) 303{ 304 char c; 305 if (op_no < 25) { 306 c = 'a' + op_no; 307 if (c >= 'x') c++; 308 } else 309 c = 'A' + op_no - 25; 310 return (char) c; 311} 312 313static void post_menu (struct menudesc *m) 314{ 315 int i; 316 int hasbox, cury, maxy, selrow, lastopt; 317 int tadd; 318 char optstr[5]; 319 320 if (m->mopt & MC_NOBOX) { 321 cury = 0; 322 maxy = m->h; 323 hasbox = 0; 324 } else { 325 cury = 1; 326 maxy = m->h+1; 327 hasbox = 1; 328 } 329 330 /* Clear the window */ 331 wclear (m->mw); 332 333 tadd = strlen(m->title) ? 2 : 0; 334 335 if (tadd) { 336 mvwaddstr(m->mw, cury, cury, " "); 337 mvwaddstr(m->mw, cury, cury + 1, m->title); 338 cury += 2; 339 maxy += 2; 340 } 341 342 /* Set defaults, calculate lastopt. */ 343 selrow = -1; 344 if (m->mopt & MC_SCROLL) { 345 lastopt = MIN(m->numopts, m->topline+m->h-1); 346 maxy -= 1; 347 } else 348 lastopt = m->numopts; 349 350 for (i=m->topline; i<lastopt; i++, cury++) { 351 if (m->cursel == i) { 352 mvwaddstr (m->mw, cury, hasbox, ">"); 353 wstandout(m->mw); 354 selrow = cury; 355 } else 356 mvwaddstr (m->mw, cury, hasbox, " "); 357 (void) sprintf (optstr, "%c: ", opt_ch(i)); 358 waddstr (m->mw, optstr); 359 waddstr (m->mw, m->opts[i].opt_name); 360 if (m->cursel == i) 361 wstandend(m->mw); 362 } 363 364 /* Add the exit option. */ 365 if (!(m->mopt & MC_NOEXITOPT) && cury < maxy) { 366 if (m->cursel >= m->numopts) { 367 mvwaddstr (m->mw, cury, hasbox, ">"); 368 wstandout(m->mw); 369 selrow = cury; 370 } else 371 mvwaddstr (m->mw, cury, hasbox, " "); 372 waddstr (m->mw, "x: Exit"); 373 if (m->cursel >= m->numopts) 374 wstandend(m->mw); 375 cury++; 376 } 377 378 /* Add the scroll line */ 379 if (m->mopt & MC_SCROLL) { 380 mvwaddstr (m->mw, cury, hasbox, scrolltext); 381 if (selrow < 0) 382 selrow = cury; 383 } 384 385 /* Add the box. */ 386 if (!(m->mopt & MC_NOBOX)) 387 box(m->mw, '*', '*'); 388 389 wmove(m->mw, selrow, hasbox); 390} 391 392static void process_help (struct menudesc *m, int num) 393{ 394 char *help = m->helpstr; 395 int lineoff = 0; 396 int curoff = 0; 397 int again; 398 int winin; 399 400 /* Is there help? */ 401 if (!help) { 402 mbeep(); 403 return; 404 } 405 406 /* Display the help information. */ 407 do { 408 if (lineoff < curoff) { 409 help = m->helpstr; 410 curoff = 0; 411 } 412 while (*help && curoff < lineoff) { 413 if (*help == '\n') 414 curoff++; 415 help++; 416 } 417 418 wclear(stdscr); 419 mvwaddstr (stdscr, 0, 0, 420 "Help: exit: x, page up: u <, page down: d >"); 421 mvwaddstr (stdscr, 2, 0, help); 422 wmove (stdscr, 1, 0); 423 wrefresh(stdscr); 424 425 do { 426 winin = mgetch(stdscr); 427 if (winin < KEYSEQ_FIRST) 428 winin = tolower(winin); 429 again = 0; 430 switch (winin) { 431 case '<': 432 case 'u': 433 case KEYSEQ_UP_ARROW: 434 case KEYSEQ_LEFT_ARROW: 435 case KEYSEQ_PAGE_UP: 436 if (lineoff) 437 lineoff -= max_lines - 2; 438 else 439 again = 1; 440 break; 441 case '>': 442 case 'd': 443 case KEYSEQ_DOWN_ARROW: 444 case KEYSEQ_RIGHT_ARROW: 445 case KEYSEQ_PAGE_DOWN: 446 if (*help) 447 lineoff += max_lines - 2; 448 else 449 again = 1; 450 break; 451 case 'q': 452 break; 453 case 'x': 454 winin = 'q'; 455 break; 456 default: 457 again = 1; 458 } 459 if (again) 460 mbeep(); 461 } while (again); 462 } while (winin != 'q'); 463 464 /* Restore current menu */ 465 wclear(stdscr); 466 wrefresh(stdscr); 467 if (m->post_act) 468 (*m->post_act)(); 469} 470 471static void process_req (struct menudesc *m, int num, int req) 472{ 473 int ch; 474 int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1 ); 475 int refresh = 0; 476 int scroll_sel = 0; 477 478 if (req == REQ_EXECUTE) 479 return; 480 481 else if (req == REQ_NEXT_ITEM) { 482 if (m->cursel < m->numopts + hasexit - 1) { 483 m->cursel++; 484 scroll_sel = 1; 485 refresh = 1; 486 if (m->mopt & MC_SCROLL && 487 m->cursel >= m->topline + m->h -1 ) 488 m->topline += 1; 489 } else 490 mbeep(); 491 492 } else if (req == REQ_PREV_ITEM) { 493 if (m->cursel > 0) { 494 m->cursel--; 495 scroll_sel = 1; 496 refresh = 1; 497 if (m->cursel < m->topline ) 498 m->topline -= 1; 499 } else 500 mbeep(); 501 502 } else if (req == REQ_REDISPLAY) { 503 wclear(stdscr); 504 wrefresh(stdscr); 505 if (m->post_act) 506 (*m->post_act)(); 507 refresh = 1; 508 509 } else if (req == REQ_HELP) { 510 process_help (m, num); 511 refresh = 1; 512 513 } else if (req == REQ_SCROLLUP) { 514 if (!(m->mopt & MC_SCROLL)) 515 mbeep(); 516 else if (m->topline == 0) 517 mbeep(); 518 else { 519 m->topline = MAX(0,m->topline-m->h+1); 520 wclear (m->mw); 521 refresh = 1; 522 } 523 524 } else if (req == REQ_SCROLLDOWN) { 525 if (!(m->mopt & MC_SCROLL)) 526 mbeep(); 527 else if (m->topline + m->h - 1 >= m->numopts + hasexit) 528 mbeep(); 529 else { 530 m->topline = MIN(m->topline+m->h-1, 531 m->numopts+hasexit-m->h+1); 532 wclear (m->mw); 533 refresh = 1; 534 } 535 536 } else { 537 ch = req; 538 if (ch == 'x' && hasexit) { 539 m->cursel = m->numopts; 540 scroll_sel = 1; 541 refresh = 1; 542 } else { 543 if (ch > 'z') 544 ch = 255; 545 if (ch >= 'a') { 546 if (ch > 'x') ch--; 547 ch = ch - 'a'; 548 } else 549 ch = 25 + ch - 'A'; 550 if (ch < 0 || ch >= m->numopts) 551 mbeep(); 552 else { 553 m->cursel = ch; 554 scroll_sel = 1; 555 refresh = 1; 556 } 557 } 558 } 559 560 if (m->mopt & MC_SCROLL && scroll_sel) { 561 while (m->cursel >= m->topline + m->h -1 ) 562 m->topline = MIN(m->topline+m->h-1, 563 m->numopts+hasexit-m->h+1); 564 while (m->cursel < m->topline) 565 m->topline = MAX(0,m->topline-m->h+1); 566 } 567 568 if (refresh) { 569 post_menu (m); 570 wrefresh (m->mw); 571 } 572} 573 574int menu_init (void) 575{ 576 577 if (__menu_init) 578 return 0; 579 580 if (initscr() == NULL) 581 return 1; 582 583 cbreak(); 584 noecho(); 585 max_lines = getmaxy(stdscr); 586 max_cols = getmaxx(stdscr); 587 init_keyseq(); 588#ifdef DYNAMIC_MENUS 589 num_menus = DYN_INIT_NUM; 590 while (num_menus < DYN_MENU_START) 591 num_menus *= 2; 592 menus = (menudesc *) malloc(sizeof(menudesc)*num_menus); 593 if (menus == NULL) 594 return 2; 595 (void) memset ((void *)menus, 0, sizeof(menudesc)*num_menus); 596 (void) memcpy ((void *)menus, (void *)menu_def, 597 sizeof(menudesc)*DYN_MENU_START); 598 num_avail = num_menus - DYN_MENU_START; 599#endif 600 601 __menu_init = 1; 602 return (0); 603} 604 605void process_menu (int num) 606{ 607 int sel = 0; 608 int req, done; 609 int last_num; 610 611 struct menudesc *m; 612 613 m = &menus[num]; 614 615 done = FALSE; 616 617 /* Initialize? */ 618 if (menu_init()) { 619 __menu_initerror(); 620 return; 621 } 622 623 if (__m_endwin) { 624 wclear(stdscr); 625 wrefresh(stdscr); 626 __m_endwin = 0; 627 } 628 if (m->mw == NULL) 629 init_menu (m); 630 631 /* Always preselect option 0 and display from 0! */ 632 m->cursel = 0; 633 m->topline = 0; 634 635 while (!done) { 636 last_num = num; 637 if (__m_endwin) { 638 wclear(stdscr); 639 wrefresh(stdscr); 640 __m_endwin = 0; 641 } 642 /* Process the display action */ 643 if (m->post_act) 644 (*m->post_act)(); 645 post_menu (m); 646 wrefresh (m->mw); 647 648 while ((req = menucmd (m->mw)) != REQ_EXECUTE) 649 process_req (m, num, req); 650 651 sel = m->cursel; 652 wclear (m->mw); 653 wrefresh (m->mw); 654 655 /* Process the items */ 656 if (sel < m->numopts) { 657 if (m->opts[sel].opt_flags & OPT_ENDWIN) { 658 endwin(); 659 __m_endwin = 1; 660 } 661 if (m->opts[sel].opt_action) 662 done = (*m->opts[sel].opt_action)(); 663 if (m->opts[sel].opt_menu != -1) { 664 if (m->opts[sel].opt_flags & OPT_SUB) 665 process_menu (m->opts[sel].opt_menu); 666 else 667 num = m->opts[sel].opt_menu; 668 } 669 670 if (m->opts[sel].opt_flags & OPT_EXIT) 671 done = TRUE; 672 673 } else 674 done = TRUE; 675 676 /* Reselect m just in case */ 677 if (num != last_num) { 678 m = &menus[num]; 679 680 /* Initialize? */ 681 if (m->mw == NULL) 682 init_menu (m); 683 if (m->post_act) 684 (*m->post_act)(); 685 } 686 } 687 688 /* Process the exit action */ 689 if (m->exit_act) 690 (*m->exit_act)(); 691} 692 693/* Control L is end of standard routines, remaining only for dynamic. */ 694 695/* Beginning of routines for dynamic menus. */ 696 697/* local prototypes */ 698static int double_menus (void); 699 700static int double_menus (void) 701{ 702 menudesc *temp; 703 704 temp = (menudesc *) malloc(sizeof(menudesc)*num_menus*2); 705 if (temp == NULL) 706 return 0; 707 (void) memset ((void *)temp, 0, 708 sizeof(menudesc)*num_menus*2); 709 (void) memcpy ((void *)temp, (void *)menus, 710 sizeof(menudesc)*num_menus); 711 free (menus); 712 menus = temp; 713 num_avail = num_menus; 714 num_menus *= 2; 715 716 return 1; 717} 718 719int new_menu (char * title, menu_ent * opts, int numopts, 720 int x, int y, int h, int w, int mopt, 721 void (*post_act)(void), void (*exit_act), char * help) 722{ 723 int ix; 724 725 /* Check for free menu entry. */ 726 if (num_avail == 0) 727 if (!double_menus ()) 728 return -1; 729 730 /* Find free menu entry. */ 731 for (ix = DYN_MENU_START; ix < num_menus && menus[ix].mopt & MC_VALID; 732 ix++) /* do nothing */; 733 734 /* if ix == num_menus ... panic */ 735 736 /* Set Entries */ 737 menus[ix].title = title; 738 menus[ix].opts = opts; 739 menus[ix].numopts = numopts; 740 menus[ix].x = x; 741 menus[ix].y = y; 742 menus[ix].h = h; 743 menus[ix].w = w; 744 menus[ix].mopt = mopt | MC_VALID; 745 menus[ix].post_act = post_act; 746 menus[ix].exit_act = exit_act; 747 menus[ix].helpstr = help; 748 749 init_menu (&menus[ix]); 750 751 return ix; 752} 753 754void free_menu (int menu_no) 755{ 756 if (menu_no < num_menus) { 757 menus[menu_no].mopt &= ~MC_VALID; 758 if (menus[menu_no].mw != NULL) 759 delwin (menus[menu_no].mw); 760 } 761} 762