Home | History | Annotate | Line # | Download | only in menuc
menu_sys.def revision 1.26
      1 /*	$NetBSD: menu_sys.def,v 1.26 2001/11/17 01:12:47 perry 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 	/* XXX is it even worth doing this right? */
    318 	if (has_colors()) {
    319 		wbkgd(m->mw, COLOR_PAIR(1));
    320 		wattrset(m->mw, COLOR_PAIR(1));
    321 	}
    322 }
    323 
    324 static char
    325 opt_ch (int op_no)
    326 {
    327 	char c;
    328 	if (op_no < 25) {
    329 		c = 'a' + op_no;
    330 		if (c >= 'x') c++;
    331 	} else
    332 		c = 'A' + op_no - 25;
    333 	return (char) c;
    334 }
    335 
    336 static void
    337 post_menu (struct menudesc *m)
    338 {
    339 	int i;
    340 	int hasbox, cury, maxy, selrow, lastopt;
    341 	int tadd;
    342 	char optstr[5];
    343 
    344 	if (m->mopt & MC_NOBOX) {
    345 		cury = 0;
    346 		maxy = m->h;
    347 		hasbox = 0;
    348 	} else {
    349 		cury = 1;
    350 		maxy = m->h+1;
    351 		hasbox = 1;
    352 	}
    353 
    354 	/* Clear the window */
    355 	wclear (m->mw);
    356 
    357 	tadd = strlen(m->title) ? 2 : 0;
    358 
    359 	if (tadd) {
    360 		mvwaddstr(m->mw, cury, cury, " ");
    361 		mvwaddstr(m->mw, cury, cury + 1, m->title);
    362 		cury += 2;
    363 		maxy += 2;
    364 	}
    365 
    366 	/* Set defaults, calculate lastopt. */
    367 	selrow = -1;
    368 	if (m->mopt & MC_SCROLL) {
    369 		lastopt = MIN(m->numopts, m->topline+m->h-1);
    370 		maxy -= 1;
    371 	} else
    372 		lastopt = m->numopts;
    373 
    374 	for (i=m->topline; i<lastopt; i++, cury++) {
    375 		if (m->cursel == i) {
    376 			mvwaddstr (m->mw, cury, hasbox, ">");
    377 			wstandout(m->mw);
    378 			selrow = cury;
    379 		} else
    380 			mvwaddstr (m->mw, cury, hasbox, " ");
    381 		if (!(m->mopt & MC_NOSHORTCUT)) {
    382 			(void) sprintf (optstr, "%c: ", opt_ch(i));
    383 		    	waddstr (m->mw, optstr);
    384 		}
    385 		waddstr (m->mw, m->opts[i].opt_name);
    386 		if (m->cursel == i)
    387 			wstandend(m->mw);
    388 	}
    389 
    390 	/* Add the exit option. */
    391 	if (!(m->mopt & MC_NOEXITOPT) && cury < maxy) {
    392 		if (m->cursel >= m->numopts) {
    393 			mvwaddstr (m->mw, cury, hasbox, ">");
    394 			wstandout(m->mw);
    395 			selrow = cury;
    396 		} else
    397 			mvwaddstr (m->mw, cury, hasbox, " ");
    398 		if (!(m->mopt & MC_NOSHORTCUT))
    399 			waddstr (m->mw, "x: ");
    400 		waddstr (m->mw, m->exitstr);
    401 		if (m->cursel >= m->numopts)
    402 			wstandend(m->mw);
    403 		cury++;
    404 	}
    405 
    406 	/* Add the scroll line */
    407 	if (m->mopt & MC_SCROLL) {
    408 		mvwaddstr (m->mw, cury, hasbox, scrolltext);
    409 		if (selrow < 0)
    410 			selrow = cury;
    411 	}
    412 
    413 	/* Add the box. */
    414 	if (!(m->mopt & MC_NOBOX))
    415 		box(m->mw, 0, 0);
    416 
    417 	wmove(m->mw, selrow, hasbox);
    418 }
    419 
    420 static void
    421 process_help (struct menudesc *m, int num)
    422 {
    423 	char *help = m->helpstr;
    424 	int lineoff = 0;
    425 	int curoff = 0;
    426 	int again;
    427 	int winin;
    428 
    429 	/* Is there help? */
    430 	if (!help) {
    431 		mbeep();
    432 		return;
    433 	}
    434 
    435 	/* Display the help information. */
    436 	do {
    437 		if (lineoff < curoff) {
    438 			help = m->helpstr;
    439 			curoff = 0;
    440 		}
    441 		while (*help && curoff < lineoff) {
    442 			if (*help == '\n')
    443 				curoff++;
    444 			help++;
    445 		}
    446 
    447 		wclear(stdscr);
    448 		mvwaddstr (stdscr, 0, 0,
    449 			"Help: exit: x,  page up: u <, page down: d >");
    450 		mvwaddstr (stdscr, 2, 0, help);
    451 		wmove (stdscr, 1, 0);
    452 	  	wrefresh(stdscr);
    453 
    454 		do {
    455 			winin = mgetch(stdscr);
    456 			if (winin < KEYSEQ_FIRST)
    457 				winin = tolower(winin);
    458 			again = 0;
    459 			switch (winin) {
    460 				case '<':
    461 				case 'u':
    462 				case KEYSEQ_UP_ARROW:
    463 				case KEYSEQ_LEFT_ARROW:
    464 				case KEYSEQ_PAGE_UP:
    465 					if (lineoff)
    466 						lineoff -= max_lines - 2;
    467 					else
    468 						again = 1;
    469 					break;
    470 				case '>':
    471 				case 'd':
    472 				case KEYSEQ_DOWN_ARROW:
    473 				case KEYSEQ_RIGHT_ARROW:
    474 				case KEYSEQ_PAGE_DOWN:
    475 					if (*help)
    476 						lineoff += max_lines - 2;
    477 					else
    478 						again = 1;
    479 					break;
    480 				case 'q':
    481 					break;
    482 				case 'x':
    483 					winin = 'q';
    484 					break;
    485 				default:
    486 					again = 1;
    487 			}
    488 			if (again)
    489 				mbeep();
    490 		} while (again);
    491 	} while (winin != 'q');
    492 
    493 	/* Restore current menu */
    494 	wclear(stdscr);
    495 	wrefresh(stdscr);
    496 	if (m->post_act)
    497 		(*m->post_act)();
    498 }
    499 
    500 static void
    501 process_req (struct menudesc *m, int num, int req)
    502 {
    503 	int ch;
    504 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1 );
    505 	int refresh = 0;
    506 	int scroll_sel = 0;
    507 
    508 	if (req == REQ_EXECUTE)
    509 		return;
    510 
    511 	else if (req == REQ_NEXT_ITEM) {
    512 		if (m->cursel < m->numopts + hasexit - 1) {
    513 			m->cursel++;
    514 			scroll_sel = 1;
    515 			refresh = 1;
    516 			if (m->mopt & MC_SCROLL &&
    517 			    m->cursel >= m->topline + m->h -1 )
    518 				m->topline += 1;
    519 		} else
    520 			mbeep();
    521 
    522 	} else if (req == REQ_PREV_ITEM) {
    523 		if (m->cursel > 0) {
    524 			m->cursel--;
    525 			scroll_sel = 1;
    526 			refresh = 1;
    527 			if (m->cursel < m->topline )
    528 				m->topline -= 1;
    529 		} else
    530 			mbeep();
    531 
    532 	} else if (req == REQ_REDISPLAY) {
    533 		wclear(stdscr);
    534 		wrefresh(stdscr);
    535 		if (m->post_act)
    536 			(*m->post_act)();
    537 		refresh = 1;
    538 
    539 	} else if (req == REQ_HELP) {
    540 		process_help (m, num);
    541 		refresh = 1;
    542 
    543 	} else if (req == REQ_SCROLLUP) {
    544 		if (!(m->mopt & MC_SCROLL))
    545 			mbeep();
    546 		else if (m->topline == 0)
    547 			mbeep();
    548 		else {
    549 			m->topline = MAX(0,m->topline-m->h+1);
    550 			m->cursel = MAX(0, m->cursel-m->h+1);
    551 			wclear (m->mw);
    552 			refresh = 1;
    553 		}
    554 
    555 	} else if (req == REQ_SCROLLDOWN) {
    556 		if (!(m->mopt & MC_SCROLL))
    557 			mbeep();
    558 		else if (m->topline + m->h - 1 >= m->numopts + hasexit)
    559 			mbeep();
    560 		else {
    561 			m->topline = MIN(m->topline+m->h-1,
    562 					 m->numopts+hasexit-m->h+1);
    563 			m->cursel = MIN(m->numopts-1, m->cursel+m->h-1);
    564 			wclear (m->mw);
    565 			refresh = 1;
    566 		}
    567 
    568 	} else {
    569 		ch = req;
    570 		if (ch == 'x' && hasexit) {
    571 			m->cursel = m->numopts;
    572 			scroll_sel = 1;
    573 			refresh = 1;
    574 		} else
    575 			if (!(m->mopt & MC_NOSHORTCUT)) {
    576 				if (ch > 'z')
    577 					ch = 255;
    578 				if (ch >= 'a') {
    579 					if (ch > 'x') ch--;
    580 				   		ch = ch - 'a';
    581 				} else
    582 					ch = 25 + ch - 'A';
    583 				if (ch < 0 || ch >= m->numopts)
    584 					mbeep();
    585 				else {
    586 					m->cursel = ch;
    587 					scroll_sel = 1;
    588 					refresh = 1;
    589 				}
    590 			} else
    591 				mbeep();
    592 	}
    593 
    594 	if (m->mopt & MC_SCROLL && scroll_sel) {
    595 		while (m->cursel >= m->topline + m->h -1 )
    596 			m->topline = MIN(m->topline+m->h-1,
    597 					 m->numopts+hasexit-m->h+1);
    598 		while (m->cursel < m->topline)
    599 			m->topline = MAX(0,m->topline-m->h+1);
    600 	}
    601 
    602 	if (refresh) {
    603 		post_menu (m);
    604 		wrefresh (m->mw);
    605 	}
    606 }
    607 
    608 int
    609 menu_init (void)
    610 {
    611 
    612 	if (__menu_init)
    613 		return 0;
    614 
    615 	if (initscr() == NULL)
    616 		return 1;
    617 
    618 	cbreak();
    619 	noecho();
    620 
    621 	/* XXX Should be configurable but it almost isn't worth it. */
    622 	if (has_colors()) {
    623 		start_color();
    624 		init_pair(1, COLOR_WHITE, COLOR_BLUE);
    625 		bkgd(COLOR_PAIR(1));
    626 		attrset(COLOR_PAIR(1));
    627 	}
    628 
    629 	max_lines = getmaxy(stdscr);
    630 	max_cols = getmaxx(stdscr);
    631 	init_keyseq();
    632 #ifdef DYNAMIC_MENUS
    633 	num_menus = DYN_INIT_NUM;
    634 	while (num_menus < DYN_MENU_START)
    635 		num_menus *= 2;
    636 	menus = (menudesc *) malloc(sizeof(menudesc)*num_menus);
    637 	if (menus == NULL)
    638 		return 2;
    639 	(void) memset ((void *)menus, 0, sizeof(menudesc)*num_menus);
    640 	(void) memcpy ((void *)menus, (void *)menu_def,
    641 		sizeof(menudesc)*DYN_MENU_START);
    642 	 num_avail = num_menus - DYN_MENU_START;
    643 #endif
    644 
    645 	__menu_init = 1;
    646 	return (0);
    647 }
    648 
    649 void
    650 process_menu (int num)
    651 {
    652 	int sel = 0;
    653 	int req, done;
    654 	int last_num;
    655 
    656 	struct menudesc *m;
    657 
    658 	m = &menus[num];
    659 
    660 	done = FALSE;
    661 
    662 	/* Initialize? */
    663 	if (menu_init()) {
    664 		__menu_initerror();
    665 		return;
    666 	}
    667 
    668 	if (__m_endwin) {
    669      		wclear(stdscr);
    670 		wrefresh(stdscr);
    671 		__m_endwin = 0;
    672 	}
    673 	if (m->mw == NULL)
    674 		init_menu (m);
    675 
    676 	/* Always preselect option 0 and display from 0! */
    677 	m->cursel = 0;
    678 	m->topline = 0;
    679 
    680 	while (!done) {
    681 		last_num = num;
    682 		if (__m_endwin) {
    683 			wclear(stdscr);
    684 			wrefresh(stdscr);
    685 			__m_endwin = 0;
    686 		}
    687 		/* Process the display action */
    688 		if (m->post_act)
    689 			(*m->post_act)();
    690 		post_menu (m);
    691 		wrefresh (m->mw);
    692 
    693 		while ((req = menucmd (m->mw)) != REQ_EXECUTE)
    694 			process_req (m, num, req);
    695 
    696 		sel = m->cursel;
    697 		wclear (m->mw);
    698 		wrefresh (m->mw);
    699 
    700 		/* Process the items */
    701 		if (sel < m->numopts) {
    702 			if (m->opts[sel].opt_flags & OPT_ENDWIN) {
    703 				endwin();
    704 				__m_endwin = 1;
    705 			}
    706 			if (m->opts[sel].opt_action)
    707 				done = (*m->opts[sel].opt_action)(m);
    708 			if (m->opts[sel].opt_menu != -1) {
    709 				if (m->opts[sel].opt_flags & OPT_SUB)
    710 					process_menu (m->opts[sel].opt_menu);
    711 				else
    712 					num = m->opts[sel].opt_menu;
    713 			}
    714 
    715                         if (m->opts[sel].opt_flags & OPT_EXIT)
    716                                 done = TRUE;
    717 
    718 		} else
    719 			done = TRUE;
    720 
    721 		/* Reselect m just in case */
    722 		if (num != last_num) {
    723 			m = &menus[num];
    724 
    725 			/* Initialize? */
    726 			if (m->mw == NULL)
    727 				init_menu (m);
    728 			if (m->post_act)
    729 				(*m->post_act)();
    730 		}
    731 	}
    732 
    733 	/* Process the exit action */
    734 	if (m->exit_act)
    735 		(*m->exit_act)();
    736 }
    737 
    738 /* Control L is end of standard routines, remaining only for dynamic. */
    740 
    741 /* Beginning of routines for dynamic menus. */
    742 
    743 /* local prototypes */
    744 static int double_menus (void);
    745 
    746 static int
    747 double_menus (void)
    748 {
    749 	menudesc *temp;
    750 
    751 	temp  = (menudesc *) malloc(sizeof(menudesc)*num_menus*2);
    752 	if (temp == NULL)
    753 		return 0;
    754 	(void) memset ((void *)temp, 0,
    755 		sizeof(menudesc)*num_menus*2);
    756 	(void) memcpy ((void *)temp, (void *)menus,
    757 		sizeof(menudesc)*num_menus);
    758 	free (menus);
    759 	menus = temp;
    760 	num_avail = num_menus;
    761 	num_menus *= 2;
    762 
    763 	return 1;
    764 }
    765 
    766 int
    767 new_menu (char * title, menu_ent * opts, int numopts,
    768 	int x, int y, int h, int w, int mopt,
    769 	void (*post_act)(void), void (*exit_act)(void), char * help)
    770 {
    771 	int ix;
    772 
    773 	/* Check for free menu entry. */
    774 	if (num_avail == 0)
    775 		if (!double_menus ())
    776 			return -1;
    777 
    778 	/* Find free menu entry. */
    779 	for (ix = DYN_MENU_START; ix < num_menus && menus[ix].mopt & MC_VALID;
    780 		ix++) /* do  nothing */;
    781 
    782 	/* if ix == num_menus ... panic */
    783 
    784 	/* Set Entries */
    785 	menus[ix].title = title ? title : "";
    786 	menus[ix].opts = opts;
    787 	menus[ix].numopts = numopts;
    788 	menus[ix].x = x;
    789 	menus[ix].y = y;
    790 	menus[ix].h = h;
    791 	menus[ix].w = w;
    792 	menus[ix].mopt = mopt | MC_VALID;
    793 	menus[ix].post_act = post_act;
    794 	menus[ix].exit_act = exit_act;
    795 	menus[ix].helpstr  = help;
    796 	menus[ix].exitstr  = "Exit";
    797 
    798 	init_menu (&menus[ix]);
    799 
    800 	return ix;
    801 }
    802 
    803 void
    804 free_menu (int menu_no)
    805 {
    806 	if (menu_no < num_menus) {
    807 		menus[menu_no].mopt &= ~MC_VALID;
    808 		if (menus[menu_no].mw != NULL)
    809 			delwin (menus[menu_no].mw);
    810 	}
    811 }
    812