Home | History | Annotate | Line # | Download | only in menuc
menu_sys.def revision 1.55
      1 /*	$NetBSD: menu_sys.def,v 1.55 2004/08/14 15:51:08 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 /* 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 /* Macros */
     53 #define MAX(x,y) ((x)>(y)?(x):(y))
     54 #define MIN(x,y) ((x)<(y)?(x):(y))
     55 
     56 /* Initialization state. */
     57 static int __menu_init = 0;
     58 static int max_lines = 0, max_cols = 0;
     59 #ifndef scrolltext
     60 static const char *scrolltext = " <: page up, >: page down";
     61 #endif
     62 
     63 #ifdef DYNAMIC_MENUS
     64 static int num_menus  = 0;
     65 #define DYN_INIT_NUM 32
     66 static menudesc **menu_list;
     67 #define MENUS(n) (*(menu_list[n]))
     68 #else
     69 #define MENUS(n) (menu_def[n])
     70 #endif
     71 
     72 /* prototypes for in here! */
     73 static void init_menu(menudesc *m);
     74 static char opt_ch(menudesc *m, int op_no);
     75 static void draw_menu(menudesc *m, void *arg);
     76 static void process_help(menudesc *m);
     77 static void process_req(menudesc *m, void *arg, int req);
     78 static int menucmd(WINDOW *w);
     79 
     80 #ifndef NULL
     81 #define NULL 0
     82 #endif
     83 
     84 /* menu system processing routines */
     85 #define mbeep() (void)fputc('\a', stderr)
     86 
     87 static int
     88 menucmd(WINDOW *w)
     89 {
     90 	int ch;
     91 
     92 	while (TRUE) {
     93 		ch = wgetch(w);
     94 
     95 		switch (ch) {
     96 		case '\n':
     97 			return REQ_EXECUTE;
     98 		case '\016':  /* Control-P */
     99 		case KEY_DOWN:
    100 			return REQ_NEXT_ITEM;
    101 		case '\020':  /* Control-N */
    102 		case KEY_UP:
    103 			return REQ_PREV_ITEM;
    104 		case '\014':  /* Control-L */
    105 			return REQ_REDISPLAY;
    106 		case '<':
    107 		case '\010':  /* Control-H (backspace) */
    108 		case KEY_PPAGE:
    109 		case KEY_LEFT:
    110 			return REQ_SCROLLUP;
    111 		case '\026':  /* Control-V */
    112 		case '>':
    113 		case ' ':
    114 		case KEY_NPAGE:
    115 		case KEY_RIGHT:
    116 			return REQ_SCROLLDOWN;
    117 		case '?':
    118 			return REQ_HELP;
    119 		case '\033': /* esc-v is scroll down */
    120 			ch = wgetch(w);
    121 			if (ch == 'v')
    122 				return REQ_SCROLLUP;
    123 			else
    124 				ch = 0; /* zap char so we beep */
    125 		}
    126 
    127 		if (isalpha(ch))
    128 			return ch;
    129 
    130 		mbeep();
    131 		wrefresh(w);
    132 	}
    133 }
    134 
    135 static void
    136 init_menu(menudesc *m)
    137 {
    138 	int wmax;
    139 	int hadd, wadd, exithadd;
    140 	int i;
    141 	int x, y, w;
    142 	const char *title, *tp, *ep;
    143 
    144 	x = m->x;
    145 	y = m->y;
    146 	w = m->w;
    147 	wmax = 0;
    148 	hadd = ((m->mopt & MC_NOBOX) ? 0 : 2);
    149 	wadd = ((m->mopt & MC_NOBOX) ? 2 : 4);
    150 
    151 	if (m->title && *(title = MSG_XLAT(m->title)) != 0) {
    152 		/* Allow multiple line titles */
    153 		for (tp = title; ep = strchr(tp, '\n'); tp = ep + 1) {
    154 			i = ep - tp;
    155 			wmax = MAX(wmax, i);
    156 			hadd++;
    157 		}
    158 		hadd++;
    159 		i = strlen(tp);
    160 		wmax = MAX(wmax, i);
    161 		if (i != 0)
    162 			hadd++;
    163 	} else {
    164 		m->title = NULL;
    165 		title = "untitled";
    166 	}
    167 	exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
    168 
    169 #ifdef MSG_DEFS_H
    170 	if (y < 0) {
    171 		/* put menu box below message text */
    172 		y = -y;
    173 		i = msg_row();
    174 		if (i > y)
    175 		    y = i;
    176 	}
    177 #endif
    178 
    179 	/* Calculate h? h == number of visible options. */
    180 	if (m->h == 0)
    181 		m->h = m->numopts + exithadd;
    182 
    183 	if (m->h > max_lines - y - hadd) {
    184 		/* Not enough space for all the options */
    185 		if (m->h <= 4 || !(m->mopt & (MC_SCROLL | MC_ALWAYS_SCROLL))) {
    186 			/* move menu up screen */
    187 			y = max_lines - hadd - m->h;
    188 			if (y < 0)
    189 				y = 0;
    190 		}
    191 		m->h = max_lines - y - hadd;
    192 	}
    193 
    194 	if (m->h < m->numopts + exithadd || m->mopt & MC_ALWAYS_SCROLL) {
    195 		/* We need to add the scroll text...
    196 		 * The used to be a check for MC_SCROLL here, but it is
    197 		 * fairly pointless - you just don't want the program
    198 		 * to exit on this sort of error.
    199 		 */
    200 		if (m->h < 3) {
    201 			endwin();
    202 			(void)fprintf(stderr,
    203 				"Window too short (m->h %d, m->numopts %d, exithadd %d, y %d, max_lines %d, hadd %d) for menu \"%.30s\"\n",
    204 				m->h, m->numopts, exithadd, y, max_lines, hadd,
    205 				title);
    206 			exit(1);
    207 		}
    208 		hadd++;
    209 		m->h = MIN(m->h, max_lines - y - hadd);
    210 		i = strlen(scrolltext);
    211 		wmax = MAX(wmax, i);
    212 	}
    213 
    214 	/* Calculate w? */
    215 	if (w == 0) {
    216 		int l;
    217 		for (i = 0; i < m->numopts; i++) {
    218 			l = strlen(MSG_XLAT(m->opts[i].opt_name));
    219 			if (!(m->mopt & MC_NOSHORTCUT))
    220 				l += 3;
    221 			wmax = MAX(wmax, l);
    222 		}
    223 		w = wmax;
    224 	}
    225 
    226 	/* check and adjust for screen fit */
    227 	if (w + wadd > max_cols) {
    228 		endwin();
    229 		(void)fprintf(stderr,
    230 			"Screen too narrow (%d + %d > %d) for menu \"%s\"\n",
    231 				w, wadd, max_cols, title);
    232 		exit(1);
    233 
    234 	}
    235 
    236 	if (x == -1)
    237 		x = (max_cols - (w + wadd)) / 2;	/* center */
    238 	else if (x + w + wadd > max_cols)
    239 		x = max_cols - (w + wadd);	/* right align */
    240 
    241 	if (y == 0) {
    242 		/* Center - rather than top */
    243 		y = (max_lines - hadd - m->h) / 2;
    244 	}
    245 
    246 	/* Get the windows. */
    247 	m->mw = newwin(m->h + hadd, w + wadd, y, x);
    248 
    249 	if (m->mw == NULL) {
    250 		endwin();
    251 		(void)fprintf(stderr,
    252 			"Could not create window (%d + %d, %d + %d, %d, %d) for menu \"%s\"\n",
    253 			m->h, hadd, w, wadd, y, x, title);
    254 		exit(1);
    255 	}
    256 	keypad(m->mw, TRUE); /* enable multi-key assembling for win */
    257 
    258 	/* XXX is it even worth doing this right? */
    259 	if (has_colors()) {
    260 		wbkgd(m->mw, COLOR_PAIR(1));
    261 		wattrset(m->mw, COLOR_PAIR(1));
    262 	}
    263 
    264 	if (m->mopt & MC_SUBMENU) {
    265 		/* Keep a copy of what is on the screen under the window */
    266 		m->sv_mw = newwin(m->h + hadd, w + wadd, y, x);
    267 		/*
    268 		 * cursrc contains post-doupdate() data, not post-refresh()
    269 		 * data so we must call doupdate to ensure we save the
    270 		 * correct data.  Avoids PR 26660.
    271 		 */
    272 		doupdate();
    273 		if (m->sv_mw)
    274 			overwrite(curscr, m->sv_mw);
    275 	}
    276 }
    277 
    278 static char
    279 opt_ch(menudesc *m, int op_no)
    280 {
    281 	char c;
    282 
    283 	if (op_no == m->numopts)
    284 		return 'x';
    285 
    286 	if (op_no < 25) {
    287 		c = 'a' + op_no;
    288 		if (c >= 'x')
    289 			c++;
    290 	} else
    291 		c = 'A' + op_no - 25;
    292 	return c;
    293 }
    294 
    295 static void
    296 draw_menu_line(menudesc *m, int opt, int cury, void *arg, const char *text)
    297 {
    298 	int hasbox = m->mopt & MC_NOBOX ? 0 : 1;
    299 
    300 	if (m->cursel == opt) {
    301 		mvwaddstr(m->mw, cury, hasbox, ">");
    302 		wstandout(m->mw);
    303 	} else
    304 		mvwaddstr(m->mw, cury, hasbox, " ");
    305 	if (!(m->mopt & MC_NOSHORTCUT))
    306 		wprintw(m->mw, "%c: ", opt_ch(m, opt));
    307 
    308 	if (!text && m->draw_line)
    309 		m->draw_line(m, opt, arg);
    310 	else
    311 		waddstr(m->mw, MSG_XLAT(text));
    312 	if (m->cursel == opt)
    313 		wstandend(m->mw);
    314 }
    315 
    316 static void
    317 draw_menu(menudesc *m, void *arg)
    318 {
    319 	int opt;
    320 	int hasbox, cury, maxy;
    321 	int tadd;
    322 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
    323 	const char *tp, *ep;
    324 
    325 	hasbox = (m->mopt & MC_NOBOX ? 0 : 1);
    326 
    327 	/* Clear the window */
    328 	wclear(m->mw);
    329 
    330 	tadd = hasbox;
    331 	if (m->title) {
    332 		for (tp = MSG_XLAT(m->title); ; tp = ep + 1) {
    333 			ep = strchr(tp , '\n');
    334 			mvwaddnstr(m->mw, tadd++, hasbox + 1, tp,
    335 			    ep ? ep - tp : -1);
    336 			if (ep == NULL || *ep == 0)
    337 				break;
    338 		}
    339 		tadd++;
    340 	}
    341 
    342 	cury = tadd;
    343 	maxy = getmaxy(m->mw) - hasbox;
    344 	if (m->numopts + hasexit > m->h)
    345 		/* allow for scroll text */
    346 		maxy--;
    347 
    348 	if (m->cursel == -1) {
    349 		m->cursel = m->numopts;
    350 		if (m->h <= m->numopts)
    351 			m->topline = m->numopts + 1 - m->h;
    352 	}
    353 
    354 	while (m->cursel >= m->topline + m->h)
    355 		m->topline = MIN(m->topline + m->h,
    356 				 m->numopts + hasexit - m->h);
    357 	while (m->cursel < m->topline)
    358 		m->topline = MAX(0, m->topline - m->h);
    359 
    360 	for (opt = m->topline; opt < m->numopts; opt++) {
    361 		if (cury >= maxy)
    362 			break;
    363 		draw_menu_line(m, opt, cury++, arg, m->opts[opt].opt_name);
    364 	}
    365 
    366 	/* Add the exit option. */
    367 	if (!(m->mopt & MC_NOEXITOPT)) {
    368 		if (cury < maxy)
    369 			draw_menu_line(m, m->numopts, cury++, arg, m->exitstr);
    370 		else
    371 			opt = 0;
    372 	}
    373 
    374 	/* Add the scroll line */
    375 	if (opt != m->numopts || m->topline != 0)
    376 		mvwaddstr(m->mw, cury, hasbox, scrolltext);
    377 
    378 	/* Add the box. */
    379 	if (!(m->mopt & MC_NOBOX))
    380 		box(m->mw, 0, 0);
    381 
    382 	wmove(m->mw, tadd + m->cursel - m->topline, hasbox);
    383 	wrefresh(m->mw);
    384 }
    385 
    386 
    387 static void
    388 /*ARGSUSED*/
    389 process_help(menudesc *m)
    390 {
    391 	const char *help = m->helpstr;
    392 	WINDOW *sv_curscr;
    393 	int lineoff = 0;
    394 	int curoff = 0;
    395 	int again;
    396 	int winin;
    397 
    398 	/* Is there help? */
    399 	if (!help) {
    400 		mbeep();
    401 		return;
    402 	}
    403 	sv_curscr = newwin(getmaxy(curscr), getmaxx(curscr), 0, 0);
    404 	if (!sv_curscr) {
    405 		mbeep();
    406 		return;
    407 	}
    408 	/*
    409 	 * Save screen contents so we can restore before returning.
    410 	 * cursrc contains post-doupdate() data, not post-refresh()
    411 	 * data so we must call doupdate to ensure we save the
    412 	 * correct data.  Avoids PR 26660.
    413 	 */
    414 	doupdate();
    415 	overwrite(curscr, sv_curscr);
    416 	touchwin(stdscr);
    417 
    418 	help = MSG_XLAT(help);
    419 	/* Display the help information. */
    420 	do {
    421 		if (lineoff < curoff) {
    422 			help = MSG_XLAT(m->helpstr);
    423 			curoff = 0;
    424 		}
    425 		while (*help && curoff < lineoff) {
    426 			if (*help == '\n')
    427 				curoff++;
    428 			help++;
    429 		}
    430 
    431 		wclear(stdscr);
    432 		mvwaddstr(stdscr, 0, 0,
    433 			"Help: exit: x,  page up: u <, page down: d >");
    434 		mvwaddstr(stdscr, 2, 0, help);
    435 		wmove(stdscr, 1, 0);
    436 		wrefresh(stdscr);
    437 
    438 		do {
    439 			winin = wgetch(stdscr);
    440 			if (winin < KEY_MIN)
    441 				winin = tolower(winin);
    442 			again = 0;
    443 			switch (winin) {
    444 				case '<':
    445 				case 'u':
    446 				case KEY_UP:
    447 				case KEY_LEFT:
    448 				case KEY_PPAGE:
    449 					if (lineoff)
    450 						lineoff -= max_lines - 2;
    451 					else
    452 						again = 1;
    453 					break;
    454 				case '>':
    455 				case 'd':
    456 				case KEY_DOWN:
    457 				case KEY_RIGHT:
    458 				case KEY_NPAGE:
    459 					if (*help)
    460 						lineoff += max_lines - 2;
    461 					else
    462 						again = 1;
    463 					break;
    464 				case 'q':
    465 					break;
    466 				case 'x':
    467 					winin = 'q';
    468 					break;
    469 				default:
    470 					again = 1;
    471 			}
    472 			if (again)
    473 				mbeep();
    474 		} while (again);
    475 	} while (winin != 'q');
    476 
    477 	/* Restore the original screen contents */
    478 	touchwin(sv_curscr);
    479 	wnoutrefresh(sv_curscr);
    480 	delwin(sv_curscr);
    481 
    482 	/* Some code thinks that wrefresh(stdout) is a good idea... */
    483 	wclear(stdscr);
    484 }
    485 
    486 static void
    487 process_req(menudesc *m, void *arg, int req)
    488 {
    489 	int ch;
    490 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
    491 
    492 	switch(req) {
    493 
    494 	case REQ_EXECUTE:
    495 		return;
    496 
    497 	case REQ_NEXT_ITEM:
    498 		ch = m->cursel;
    499 		for (;;) {
    500 			ch++;
    501 			if (ch >= m->numopts + hasexit) {
    502 				mbeep();
    503 				return;
    504 			}
    505 			if (hasexit && ch == m->numopts)
    506 				break;
    507 			if (!(m->opts[ch].opt_flags & OPT_IGNORE))
    508 				break;
    509 		}
    510 		m->cursel = ch;
    511 		if (m->cursel >= m->topline + m->h)
    512 			m->topline = m->cursel - m->h + 1;
    513 		break;
    514 
    515 	case REQ_PREV_ITEM:
    516 		ch = m->cursel;
    517 		for (;;) {
    518 			if (ch <= 0) {
    519 				mbeep();
    520 				return;
    521 			}
    522 			ch--;
    523 			if (!(m->opts[ch].opt_flags & OPT_IGNORE))
    524 				break;
    525 		}
    526 		m->cursel = ch;
    527 		if (m->cursel < m->topline)
    528 			m->topline = m->cursel;
    529 		break;
    530 
    531 	case REQ_HELP:
    532 		process_help(m);
    533 		break;
    534 
    535 	case REQ_REDISPLAY:
    536 		endwin();
    537 		doupdate();
    538 		break;
    539 
    540 	case REQ_SCROLLUP:
    541 		if (m->cursel == 0) {
    542 			mbeep();
    543 			return;
    544 		}
    545 		m->topline = MAX(0, m->topline - m->h);
    546 		m->cursel = MAX(0, m->cursel - m->h);
    547 		wclear(m->mw);
    548 		break;
    549 
    550 	case REQ_SCROLLDOWN:
    551 		if (m->cursel >= m->numopts + hasexit - 1) {
    552 			mbeep();
    553 			return;
    554 		}
    555 		m->topline = MIN(m->topline + m->h,
    556 				 MAX(m->numopts + hasexit - m->h, 0));
    557 		m->cursel = MIN(m->numopts + hasexit - 1, m->cursel + m->h);
    558 		wclear(m->mw);
    559 		break;
    560 
    561 	default:
    562 		ch = req;
    563 		if (ch == 'x' && hasexit) {
    564 			m->cursel = m->numopts;
    565 			break;
    566 		}
    567 		if (m->mopt & MC_NOSHORTCUT) {
    568 			mbeep();
    569 			return;
    570 		}
    571 		if (ch > 'z')
    572 			ch = 255;
    573 		if (ch >= 'a') {
    574 			if (ch > 'x')
    575 				ch--;
    576 			ch = ch - 'a';
    577 		} else
    578 			ch = 25 + ch - 'A';
    579 		if (ch < 0 || ch >= m->numopts) {
    580 			mbeep();
    581 			return;
    582 		}
    583 		if (m->opts[ch].opt_flags & OPT_IGNORE) {
    584 			mbeep();
    585 			return;
    586 		}
    587 		m->cursel = ch;
    588 	}
    589 
    590 	draw_menu(m, arg);
    591 }
    592 
    593 int
    594 menu_init(void)
    595 {
    596 	int i;
    597 
    598 	if (__menu_init)
    599 		return 0;
    600 
    601 #ifdef	USER_MENU_INIT
    602 	if (USER_MENU_INIT)
    603 		return 1;
    604 #endif
    605 
    606 	if (initscr() == NULL)
    607 		return 1;
    608 
    609 	cbreak();
    610 	noecho();
    611 
    612 	/* XXX Should be configurable but it almost isn't worth it. */
    613 	if (has_colors()) {
    614 		start_color();
    615 		init_pair(1, COLOR_WHITE, COLOR_BLUE);
    616 		bkgd(COLOR_PAIR(1));
    617 		attrset(COLOR_PAIR(1));
    618 	}
    619 
    620 	max_lines = getmaxy(stdscr);
    621 	max_cols = getmaxx(stdscr);
    622 	keypad(stdscr, TRUE);
    623 #ifdef DYNAMIC_MENUS
    624 	num_menus = DYN_INIT_NUM;
    625 	while (num_menus < DYN_MENU_START)
    626 		num_menus *= 2;
    627 	menu_list = malloc(sizeof *menu_list * num_menus);
    628 	if (menu_list == NULL)
    629 		return 2;
    630 	(void)memset(menu_list, 0, sizeof *menu_list * num_menus);
    631 	for (i = 0; i < DYN_MENU_START; i++)
    632 		menu_list[i] = &menu_def[i];
    633 #endif
    634 
    635 	__menu_init = 1;
    636 	return 0;
    637 }
    638 
    639 void
    640 process_menu(int num, void *arg)
    641 {
    642 	int sel = 0;
    643 	int req;
    644 	menu_ent *opt;
    645 
    646 	menudesc *m;
    647 
    648 	m = &MENUS(num);
    649 
    650 	/* Initialize? */
    651 	if (menu_init()) {
    652 		__menu_initerror();
    653 		return;
    654 	}
    655 
    656 	/* Default to select option 0 and display from 0 */
    657 	m->topline = 0;
    658 	if ((m->mopt & (MC_DFLTEXIT | MC_NOEXITOPT)) == MC_DFLTEXIT)
    659 		m->cursel = -1;
    660 	else
    661 		m->cursel = 0;
    662 
    663 	for (;;) {
    664 		if (isendwin())
    665 			/* I'm not sure this is needed with netbsd's curses */
    666 			doupdate();
    667 		/* Process the display action */
    668 		if (m->post_act)
    669 			(*m->post_act)(m, arg);
    670 		if (m->mw == NULL)
    671 			init_menu(m);
    672 		draw_menu(m, arg);
    673 
    674 		while ((req = menucmd(m->mw)) != REQ_EXECUTE)
    675 			process_req(m, arg, req);
    676 
    677 		sel = m->cursel;
    678 		if (!(m->mopt & MC_NOCLEAR)) {
    679 			wclear(m->mw);
    680 			if (m->sv_mw)
    681 				overwrite(m->sv_mw, m->mw);
    682 			wnoutrefresh(m->mw);
    683 		}
    684 
    685 		/* Process the items */
    686 		if (sel >= m->numopts)
    687 			/* exit option */
    688 			break;
    689 
    690 		opt = &m->opts[sel];
    691 		if (opt->opt_flags & OPT_IGNORE)
    692 			continue;
    693 		if (opt->opt_flags & OPT_ENDWIN)
    694 			endwin();
    695 		if (opt->opt_action && (*opt->opt_action)(m, arg))
    696 			break;
    697 
    698 		if (opt->opt_menu != -1) {
    699 			if (!(opt->opt_flags & OPT_SUB)) {
    700 				num = opt->opt_menu;
    701 				wclear(m->mw);
    702 				if (m->sv_mw) {
    703 					overwrite(m->sv_mw, m->mw);
    704 					delwin(m->sv_mw);
    705 					m->sv_mw = NULL;
    706 				}
    707 				wnoutrefresh(m->mw);
    708 				delwin(m->mw);
    709 				m->mw = NULL;
    710 				m = &MENUS(num);
    711 				continue;
    712 			}
    713 			process_menu(opt->opt_menu, arg);
    714 		}
    715 		if (opt->opt_flags & OPT_EXIT)
    716 			break;
    717 	}
    718 
    719 	if (m->mopt & MC_NOCLEAR) {
    720 		wclear(m->mw);
    721 		if (m->sv_mw)
    722 			overwrite(m->sv_mw, m->mw);
    723 		wnoutrefresh(m->mw);
    724 	}
    725 
    726 	/* Process the exit action */
    727 	if (m->exit_act)
    728 		(*m->exit_act)(m, arg);
    729 	delwin(m->mw);
    730 	m->mw = NULL;
    731 	if (m->sv_mw) {
    732 		delwin(m->sv_mw);
    733 		m->sv_mw = NULL;
    734 	}
    735 }
    736 
    737 
    738 void
    739 set_menu_numopts(int menu, int numopts)
    740 {
    741 
    742 	MENUS(menu).numopts = numopts;
    743 }
    744 
    745 /* Control L is end of standard routines, remaining only for dynamic. */
    747 
    748 /* Beginning of routines for dynamic menus. */
    749 
    750 static int
    751 double_menus(void)
    752 {
    753 	menudesc **temp;
    754 	int sz = sizeof *menu_list * num_menus;
    755 
    756 	temp  = realloc(menu_list, sz * 2);
    757 	if (temp == NULL)
    758 		return 0;
    759 	(void)memset(temp + num_menus, 0, sz);
    760 	menu_list = temp;
    761 	num_menus *= 2;
    762 
    763 	return 1;
    764 }
    765 
    766 int
    767 new_menu(const char *title, menu_ent *opts, int numopts,
    768 	int x, int y, int h, int w, int mopt,
    769 	void (*post_act)(menudesc *, void *),
    770 	void (*draw_line)(menudesc *, int, void *),
    771 	void (*exit_act)(menudesc *, void *),
    772 	const char *help, const char *exit_str)
    773 {
    774 	int ix;
    775 	menudesc *m;
    776 
    777 	/* Find free menu entry. */
    778 	for (ix = DYN_MENU_START; ; ix++) {
    779 		if (ix >= num_menus && !double_menus())
    780 			return -1;
    781 		m = menu_list[ix];
    782 		if (m == NULL) {
    783 			m = calloc(sizeof *m, 1);
    784 			if (m == NULL)
    785 				return -1;
    786 			menu_list[ix] = m;
    787 			break;
    788 		}
    789 		if (!(m->mopt & MC_VALID))
    790 			break;
    791 	}
    792 
    793 	/* Set Entries */
    794 	m->title = title;
    795 	m->opts = opts;
    796 	m->numopts = numopts;
    797 	m->x = x;
    798 	m->y = y;
    799 	m->h = h;
    800 	m->w = w;
    801 	m->mopt = mopt | MC_VALID;
    802 	m->post_act = post_act;
    803 	m->draw_line = draw_line;
    804 	m->exit_act = exit_act;
    805 	m->helpstr  = help;
    806 	m->exitstr  = exit_str ? exit_str : "Exit";
    807 
    808 	return ix;
    809 }
    810 
    811 void
    812 free_menu(int menu_no)
    813 {
    814 	menudesc *m;
    815 
    816 	if (menu_no < 0 || menu_no >= num_menus)
    817 		return;
    818 
    819 	m = menu_list[menu_no];
    820 	if (!(m->mopt & MC_VALID))
    821 		return;
    822 	if (m->mw != NULL)
    823 		delwin(m->mw);
    824 	memset(m, 0, sizeof *m);
    825 }
    826