Home | History | Annotate | Line # | Download | only in menuc
menu_sys.def revision 1.21
      1 /*	$NetBSD: menu_sys.def,v 1.21 1999/06/20 20:21:03 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 
     61 struct 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 */
     70 struct 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 
     90 int _mc_num_key_seq = sizeof(_mc_key_seq) / sizeof(struct keyseq);
     91 struct keyseq *pad_list = NULL;
     92 static char str_area [512];
     93 static 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. */
    100 static int __menu_init = 0;
    101 int __m_endwin = 0;
    102 static int max_lines = 0, max_cols = 0;
    103 static char *scrolltext = " <: page up, >: page down";
    104 
    105 static menudesc *menus = menu_def;
    106 
    107 #ifdef DYNAMIC_MENUS
    108 static int num_menus  = 0;
    109 static int num_avail  = 0;
    110 #define DYN_INIT_NUM 32
    111 #endif
    112 
    113 /* prototypes for in here! */
    114 static void ins_keyseq (struct keyseq **seq, struct keyseq *ins);
    115 static void init_keyseq (void);
    116 static void init_menu (struct menudesc *m);
    117 static char opt_ch (int op_no);
    118 static void post_menu (struct menudesc *m);
    119 static void process_help (struct menudesc *m, int num);
    120 static void process_req (struct menudesc *m, int num, int req);
    121 static void mbeep (void);
    122 static int menucmd (WINDOW *w);
    123 
    124 #ifndef NULL
    125 #define NULL (void *)0
    126 #endif
    127 
    128 /* menu system processing routines */
    129 
    130 static void mbeep (void)
    131 {
    132 	fprintf (stderr,"\a");
    133 }
    134 
    135 static 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 
    147 static 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 
    162 static 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 
    191 static 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 
    229 static 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 == -1)
    289 		m->x = (max_cols - (m->w + wadd)) / 2;	/* center */
    290 	else if (m->x + m->w + wadd > max_cols)
    291 		m->x = max_cols - (m->w + wadd);
    292 
    293 	/* Get the windows. */
    294 	m->mw = newwin(m->h+hadd, m->w+wadd, m->y, m->x);
    295 
    296 	if (m->mw == NULL) {
    297 		endwin();
    298 		(void) fprintf (stderr,
    299 			"Could not create window for menu \"%s\"\n", m->title);
    300 		exit(1);
    301 	}
    302 }
    303 
    304 static char opt_ch (int op_no)
    305 {
    306 	char c;
    307 	if (op_no < 25) {
    308 		c = 'a' + op_no;
    309 		if (c >= 'x') c++;
    310 	} else
    311 		c = 'A' + op_no - 25;
    312 	return (char) c;
    313 }
    314 
    315 static void post_menu (struct menudesc *m)
    316 {
    317 	int i;
    318 	int hasbox, cury, maxy, selrow, lastopt;
    319 	int tadd;
    320 	char optstr[5];
    321 
    322 	if (m->mopt & MC_NOBOX) {
    323 		cury = 0;
    324 		maxy = m->h;
    325 		hasbox = 0;
    326 	} else {
    327 		cury = 1;
    328 		maxy = m->h+1;
    329 		hasbox = 1;
    330 	}
    331 
    332 	/* Clear the window */
    333 	wclear (m->mw);
    334 
    335 	tadd = strlen(m->title) ? 2 : 0;
    336 
    337 	if (tadd) {
    338 		mvwaddstr(m->mw, cury, cury, " ");
    339 		mvwaddstr(m->mw, cury, cury + 1, m->title);
    340 		cury += 2;
    341 		maxy += 2;
    342 	}
    343 
    344 	/* Set defaults, calculate lastopt. */
    345 	selrow = -1;
    346 	if (m->mopt & MC_SCROLL) {
    347 		lastopt = MIN(m->numopts, m->topline+m->h-1);
    348 		maxy -= 1;
    349 	} else
    350 		lastopt = m->numopts;
    351 
    352 	for (i=m->topline; i<lastopt; i++, cury++) {
    353 		if (m->cursel == i) {
    354 			mvwaddstr (m->mw, cury, hasbox, ">");
    355 			wstandout(m->mw);
    356 			selrow = cury;
    357 		} else
    358 			mvwaddstr (m->mw, cury, hasbox, " ");
    359 		(void) sprintf (optstr, "%c: ", opt_ch(i));
    360 		waddstr (m->mw, optstr);
    361 		waddstr (m->mw, m->opts[i].opt_name);
    362 		if (m->cursel == i)
    363 			wstandend(m->mw);
    364 	}
    365 
    366 	/* Add the exit option. */
    367 	if (!(m->mopt & MC_NOEXITOPT) && cury < maxy) {
    368 		if (m->cursel >= m->numopts) {
    369 			mvwaddstr (m->mw, cury, hasbox, ">");
    370 			wstandout(m->mw);
    371 			selrow = cury;
    372 		} else
    373 			mvwaddstr (m->mw, cury, hasbox, " ");
    374 		waddstr (m->mw, "x: ");
    375 		waddstr (m->mw, m->exitstr);
    376 		if (m->cursel >= m->numopts)
    377 			wstandend(m->mw);
    378 		cury++;
    379 	}
    380 
    381 	/* Add the scroll line */
    382 	if (m->mopt & MC_SCROLL) {
    383 		mvwaddstr (m->mw, cury, hasbox, scrolltext);
    384 		if (selrow < 0)
    385 			selrow = cury;
    386 	}
    387 
    388 	/* Add the box. */
    389 	if (!(m->mopt & MC_NOBOX))
    390 		box(m->mw, '*', '*');
    391 
    392 	wmove(m->mw, selrow, hasbox);
    393 }
    394 
    395 static void process_help (struct menudesc *m, int num)
    396 {
    397 	char *help = m->helpstr;
    398 	int lineoff = 0;
    399 	int curoff = 0;
    400 	int again;
    401 	int winin;
    402 
    403 	/* Is there help? */
    404 	if (!help) {
    405 		mbeep();
    406 		return;
    407 	}
    408 
    409 	/* Display the help information. */
    410 	do {
    411 		if (lineoff < curoff) {
    412 			help = m->helpstr;
    413 			curoff = 0;
    414 		}
    415 		while (*help && curoff < lineoff) {
    416 			if (*help == '\n')
    417 				curoff++;
    418 			help++;
    419 		}
    420 
    421 		wclear(stdscr);
    422 		mvwaddstr (stdscr, 0, 0,
    423 			"Help: exit: x,  page up: u <, page down: d >");
    424 		mvwaddstr (stdscr, 2, 0, help);
    425 		wmove (stdscr, 1, 0);
    426 	  	wrefresh(stdscr);
    427 
    428 		do {
    429 			winin = mgetch(stdscr);
    430 			if (winin < KEYSEQ_FIRST)
    431 				winin = tolower(winin);
    432 			again = 0;
    433 			switch (winin) {
    434 				case '<':
    435 				case 'u':
    436 				case KEYSEQ_UP_ARROW:
    437 				case KEYSEQ_LEFT_ARROW:
    438 				case KEYSEQ_PAGE_UP:
    439 					if (lineoff)
    440 						lineoff -= max_lines - 2;
    441 					else
    442 						again = 1;
    443 					break;
    444 				case '>':
    445 				case 'd':
    446 				case KEYSEQ_DOWN_ARROW:
    447 				case KEYSEQ_RIGHT_ARROW:
    448 				case KEYSEQ_PAGE_DOWN:
    449 					if (*help)
    450 						lineoff += max_lines - 2;
    451 					else
    452 						again = 1;
    453 					break;
    454 				case 'q':
    455 					break;
    456 				case 'x':
    457 					winin = 'q';
    458 					break;
    459 				default:
    460 					again = 1;
    461 			}
    462 			if (again)
    463 				mbeep();
    464 		} while (again);
    465 	} while (winin != 'q');
    466 
    467 	/* Restore current menu */
    468 	wclear(stdscr);
    469 	wrefresh(stdscr);
    470 	if (m->post_act)
    471 		(*m->post_act)();
    472 }
    473 
    474 static void process_req (struct menudesc *m, int num, int req)
    475 {
    476 	int ch;
    477 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1 );
    478 	int refresh = 0;
    479 	int scroll_sel = 0;
    480 
    481 	if (req == REQ_EXECUTE)
    482 		return;
    483 
    484 	else if (req == REQ_NEXT_ITEM) {
    485 		if (m->cursel < m->numopts + hasexit - 1) {
    486 			m->cursel++;
    487 			scroll_sel = 1;
    488 			refresh = 1;
    489 			if (m->mopt & MC_SCROLL &&
    490 			    m->cursel >= m->topline + m->h -1 )
    491 				m->topline += 1;
    492 		} else
    493 			mbeep();
    494 
    495 	} else if (req == REQ_PREV_ITEM) {
    496 		if (m->cursel > 0) {
    497 			m->cursel--;
    498 			scroll_sel = 1;
    499 			refresh = 1;
    500 			if (m->cursel < m->topline )
    501 				m->topline -= 1;
    502 		} else
    503 			mbeep();
    504 
    505 	} else if (req == REQ_REDISPLAY) {
    506 		wclear(stdscr);
    507 		wrefresh(stdscr);
    508 		if (m->post_act)
    509 			(*m->post_act)();
    510 		refresh = 1;
    511 
    512 	} else if (req == REQ_HELP) {
    513 		process_help (m, num);
    514 		refresh = 1;
    515 
    516 	} else if (req == REQ_SCROLLUP) {
    517 		if (!(m->mopt & MC_SCROLL))
    518 			mbeep();
    519 		else if (m->topline == 0)
    520 			mbeep();
    521 		else {
    522 			m->topline = MAX(0,m->topline-m->h+1);
    523 			wclear (m->mw);
    524 			refresh = 1;
    525 		}
    526 
    527 	} else if (req == REQ_SCROLLDOWN) {
    528 		if (!(m->mopt & MC_SCROLL))
    529 			mbeep();
    530 		else if (m->topline + m->h - 1 >= m->numopts + hasexit)
    531 			mbeep();
    532 		else {
    533 			m->topline = MIN(m->topline+m->h-1,
    534 					 m->numopts+hasexit-m->h+1);
    535 			wclear (m->mw);
    536 			refresh = 1;
    537 		}
    538 
    539 	} else {
    540 		ch = req;
    541 		if (ch == 'x' && hasexit) {
    542 			m->cursel = m->numopts;
    543 			scroll_sel = 1;
    544 			refresh = 1;
    545 		} else {
    546 			if (ch > 'z')
    547 				ch = 255;
    548 			if (ch >= 'a') {
    549 				if (ch > 'x') ch--;
    550 				ch = ch - 'a';
    551 			} else
    552 				ch = 25 + ch - 'A';
    553 			if (ch < 0 || ch >= m->numopts)
    554 				mbeep();
    555 			else {
    556 				m->cursel = ch;
    557 				scroll_sel = 1;
    558 				refresh = 1;
    559 			}
    560 		}
    561 	}
    562 
    563 	if (m->mopt & MC_SCROLL && scroll_sel) {
    564 		while (m->cursel >= m->topline + m->h -1 )
    565 			m->topline = MIN(m->topline+m->h-1,
    566 					 m->numopts+hasexit-m->h+1);
    567 		while (m->cursel < m->topline)
    568 			m->topline = MAX(0,m->topline-m->h+1);
    569 	}
    570 
    571 	if (refresh) {
    572 		post_menu (m);
    573 		wrefresh (m->mw);
    574 	}
    575 }
    576 
    577 int menu_init (void)
    578 {
    579 
    580 	if (__menu_init)
    581 		return 0;
    582 
    583 	if (initscr() == NULL)
    584 		return 1;
    585 
    586 	cbreak();
    587 	noecho();
    588 	max_lines = getmaxy(stdscr);
    589 	max_cols = getmaxx(stdscr);
    590 	init_keyseq();
    591 #ifdef DYNAMIC_MENUS
    592 	num_menus = DYN_INIT_NUM;
    593 	while (num_menus < DYN_MENU_START)
    594 		num_menus *= 2;
    595 	menus = (menudesc *) malloc(sizeof(menudesc)*num_menus);
    596 	if (menus == NULL)
    597 		return 2;
    598 	(void) memset ((void *)menus, 0, sizeof(menudesc)*num_menus);
    599 	(void) memcpy ((void *)menus, (void *)menu_def,
    600 		sizeof(menudesc)*DYN_MENU_START);
    601 	 num_avail = num_menus - DYN_MENU_START;
    602 #endif
    603 
    604 	__menu_init = 1;
    605 	return (0);
    606 }
    607 
    608 void process_menu (int num)
    609 {
    610 	int sel = 0;
    611 	int req, done;
    612 	int last_num;
    613 
    614 	struct menudesc *m;
    615 
    616 	m = &menus[num];
    617 
    618 	done = FALSE;
    619 
    620 	/* Initialize? */
    621 	if (menu_init()) {
    622 		__menu_initerror();
    623 		return;
    624 	}
    625 
    626 	if (__m_endwin) {
    627      		wclear(stdscr);
    628 		wrefresh(stdscr);
    629 		__m_endwin = 0;
    630 	}
    631 	if (m->mw == NULL)
    632 		init_menu (m);
    633 
    634 	/* Always preselect option 0 and display from 0! */
    635 	m->cursel = 0;
    636 	m->topline = 0;
    637 
    638 	while (!done) {
    639 		last_num = num;
    640 		if (__m_endwin) {
    641 			wclear(stdscr);
    642 			wrefresh(stdscr);
    643 			__m_endwin = 0;
    644 		}
    645 		/* Process the display action */
    646 		if (m->post_act)
    647 			(*m->post_act)();
    648 		post_menu (m);
    649 		wrefresh (m->mw);
    650 
    651 		while ((req = menucmd (m->mw)) != REQ_EXECUTE)
    652 			process_req (m, num, req);
    653 
    654 		sel = m->cursel;
    655 		wclear (m->mw);
    656 		wrefresh (m->mw);
    657 
    658 		/* Process the items */
    659 		if (sel < m->numopts) {
    660 			if (m->opts[sel].opt_flags & OPT_ENDWIN) {
    661 				endwin();
    662 				__m_endwin = 1;
    663 			}
    664 			if (m->opts[sel].opt_action)
    665 				done = (*m->opts[sel].opt_action)();
    666 			if (m->opts[sel].opt_menu != -1) {
    667 				if (m->opts[sel].opt_flags & OPT_SUB)
    668 					process_menu (m->opts[sel].opt_menu);
    669 				else
    670 					num = m->opts[sel].opt_menu;
    671 			}
    672 
    673                         if (m->opts[sel].opt_flags & OPT_EXIT)
    674                                 done = TRUE;
    675 
    676 		} else
    677 			done = TRUE;
    678 
    679 		/* Reselect m just in case */
    680 		if (num != last_num) {
    681 			m = &menus[num];
    682 
    683 			/* Initialize? */
    684 			if (m->mw == NULL)
    685 				init_menu (m);
    686 			if (m->post_act)
    687 				(*m->post_act)();
    688 		}
    689 	}
    690 
    691 	/* Process the exit action */
    692 	if (m->exit_act)
    693 		(*m->exit_act)();
    694 }
    695 
    696 /* Control L is end of standard routines, remaining only for dynamic. */
    698 
    699 /* Beginning of routines for dynamic menus. */
    700 
    701 /* local prototypes */
    702 static int double_menus (void);
    703 
    704 static int double_menus (void)
    705 {
    706 	menudesc *temp;
    707 
    708 	temp  = (menudesc *) malloc(sizeof(menudesc)*num_menus*2);
    709 	if (temp == NULL)
    710 		return 0;
    711 	(void) memset ((void *)temp, 0,
    712 		sizeof(menudesc)*num_menus*2);
    713 	(void) memcpy ((void *)temp, (void *)menus,
    714 		sizeof(menudesc)*num_menus);
    715 	free (menus);
    716 	menus = temp;
    717 	num_avail = num_menus;
    718 	num_menus *= 2;
    719 
    720 	return 1;
    721 }
    722 
    723 int new_menu (char * title, menu_ent * opts, int numopts,
    724         int x, int y, int h, int w, int mopt,
    725         void (*post_act)(void), void (*exit_act), char * help)
    726 {
    727 	int ix;
    728 
    729 	/* Check for free menu entry. */
    730 	if (num_avail == 0)
    731 		if (!double_menus ())
    732 			return -1;
    733 
    734 	/* Find free menu entry. */
    735 	for (ix = DYN_MENU_START; ix < num_menus && menus[ix].mopt & MC_VALID;
    736 		ix++) /* do  nothing */;
    737 
    738 	/* if ix == num_menus ... panic */
    739 
    740 	/* Set Entries */
    741 	menus[ix].title = title;
    742 	menus[ix].opts = opts;
    743 	menus[ix].numopts = numopts;
    744 	menus[ix].x = x;
    745 	menus[ix].y = y;
    746 	menus[ix].h = h;
    747 	menus[ix].w = w;
    748 	menus[ix].mopt = mopt | MC_VALID;
    749 	menus[ix].post_act = post_act;
    750 	menus[ix].exit_act = exit_act;
    751 	menus[ix].helpstr  = help;
    752 	menus[ix].exitstr  = "Exit";
    753 
    754 	init_menu (&menus[ix]);
    755 
    756 	return ix;
    757 }
    758 
    759 void free_menu (int menu_no)
    760 {
    761 	if (menu_no < num_menus) {
    762 		menus[menu_no].mopt &= ~MC_VALID;
    763 		if (menus[menu_no].mw != NULL)
    764 			delwin (menus[menu_no].mw);
    765 	}
    766 }
    767