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