Home | History | Annotate | Line # | Download | only in menuc
menu_sys.def revision 1.14
      1 /*	$NetBSD: menu_sys.def,v 1.14 1998/07/16 07:08:26 phil 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;
    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 max;
    232 	int add, exitadd;
    233 	int i;
    234 
    235 	add = ((m->mopt & MC_NOBOX) ? 2 : 4);
    236 	exitadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
    237 	max = strlen(m->title);
    238 
    239 	/* Calculate h? h == number of visible options. */
    240 	if (m->h == 0) {
    241 		m->h = m->numopts + exitadd;
    242 		if (m->h + m->y + add >= max_lines && (m->mopt & MC_SCROLL))
    243 			m->h = max_lines - m->y - add ;
    244 	}
    245 
    246 	/* Check window heights and set scrolling */
    247 	if (m->h < m->numopts + exitadd) {
    248 		if (!(m->mopt & MC_SCROLL) || m->h < 3) {
    249 			endwin();
    250 			(void) fprintf (stderr,
    251 				"Window too small for menu \"%s\"\n",
    252 				m->title);
    253 			exit(1);
    254 		}
    255 	} else
    256 		m->mopt &= ~MC_SCROLL;
    257 
    258 	/* check for screen fit */
    259 	if (m->y + m->h + add > max_lines) {
    260 		endwin();
    261 		(void) fprintf (stderr,
    262 			"Screen too small for menu \"%s\"\n", m->title);
    263 		exit(1);
    264 
    265 	}
    266 
    267 	/* Calculate w? */
    268 	if (m->w == 0) {
    269 		if (m->mopt & MC_SCROLL)
    270 			max = strlen(scrolltext);
    271 		for (i=0; i < m->numopts; i++ )
    272 			max = MAX(max,strlen(m->opts[i].opt_name)+3);
    273 		m->w = max;
    274 	}
    275 
    276 	/* Get the windows. */
    277 	m->mw = newwin(m->h+add, m->w+add, m->y, m->x);
    278 
    279 	if (m->mw == NULL) {
    280 		endwin();
    281 		(void) fprintf (stderr,
    282 			"Could not create window for window with title "
    283 			" \"%s\"\n", m->title);
    284 		exit(1);
    285 	}
    286 }
    287 
    288 static char opt_ch (int op_no)
    289 {
    290 	char c;
    291 	if (op_no < 25) {
    292 		c = 'a' + op_no;
    293 		if (c >= 'x') c++;
    294 	} else
    295 		c = 'A' + op_no - 25;
    296 	return (char) c;
    297 }
    298 
    299 static void post_menu (struct menudesc *m)
    300 {
    301 	int i;
    302 	int hasbox, cury, maxy, selrow, lastopt;
    303 	int tadd;
    304 	char optstr[5];
    305 
    306 	if (m->mopt & MC_NOBOX) {
    307 		cury = 0;
    308 		maxy = m->h;
    309 		hasbox = 0;
    310 	} else {
    311 		cury = 1;
    312 		maxy = m->h+1;
    313 		hasbox = 1;
    314 	}
    315 
    316 	/* Clear the window */
    317 	wclear (m->mw);
    318 
    319 	tadd = strlen(m->title) ? 2 : 0;
    320 
    321 	if (tadd) {
    322 		mvwaddstr(m->mw, cury, cury, m->title);
    323 		cury += 2;
    324 		maxy += 2;
    325 	}
    326 
    327 	/* Set defaults, calculate lastopt. */
    328 	selrow = -1;
    329 	if (m->mopt & MC_SCROLL) {
    330 		lastopt = MIN(m->numopts, m->topline+m->h-1);
    331 		maxy -= 1;
    332 	} else
    333 		lastopt = m->numopts;
    334 
    335 	for (i=m->topline; i<lastopt; i++, cury++) {
    336 		if (m->cursel == i) {
    337 			mvwaddstr (m->mw, cury, hasbox, ">");
    338 			wstandout(m->mw);
    339 			selrow = cury;
    340 		} else
    341 			mvwaddstr (m->mw, cury, hasbox, " ");
    342 		(void) sprintf (optstr, "%c: ", opt_ch(i));
    343 		waddstr (m->mw, optstr);
    344 		waddstr (m->mw, m->opts[i].opt_name);
    345 		if (m->cursel == i)
    346 			wstandend(m->mw);
    347 	}
    348 
    349 	/* Add the exit option. */
    350 	if (!(m->mopt & MC_NOEXITOPT) && cury < maxy) {
    351 		if (m->cursel >= m->numopts) {
    352 			mvwaddstr (m->mw, cury, hasbox, ">");
    353 			wstandout(m->mw);
    354 			selrow = cury;
    355 		} else
    356 			mvwaddstr (m->mw, cury, hasbox, " ");
    357 		waddstr (m->mw, "x: Exit");
    358 		if (m->cursel >= m->numopts)
    359 			wstandend(m->mw);
    360 		cury++;
    361 	}
    362 
    363 	/* Add the scroll line */
    364 	if (m->mopt & MC_SCROLL) {
    365 		mvwaddstr (m->mw, cury, hasbox, scrolltext);
    366 		if (selrow < 0)
    367 			selrow = cury;
    368 	}
    369 
    370 	/* Add the box. */
    371 	if (!(m->mopt & MC_NOBOX))
    372 		box(m->mw, '*', '*');
    373 
    374 	wmove(m->mw, selrow, hasbox);
    375 }
    376 
    377 static void process_help (struct menudesc *m, int num)
    378 {
    379 	char *help = m->helpstr;
    380 	int lineoff = 0;
    381 	int curoff = 0;
    382 	int again;
    383 	int winin;
    384 
    385 	/* Is there help? */
    386 	if (!help) {
    387 		mbeep();
    388 		return;
    389 	}
    390 
    391 	/* Display the help information. */
    392 	do {
    393 		if (lineoff < curoff) {
    394 			help = m->helpstr;
    395 			curoff = 0;
    396 		}
    397 		while (*help && curoff < lineoff) {
    398 			if (*help == '\n')
    399 				curoff++;
    400 			help++;
    401 		}
    402 
    403 		wclear(stdscr);
    404 		mvwaddstr (stdscr, 0, 0,
    405 			"Help: exit: x,  page up: u <, page down: d >");
    406 		mvwaddstr (stdscr, 2, 0, help);
    407 		wmove (stdscr, 1, 0);
    408 	  	wrefresh(stdscr);
    409 
    410 		do {
    411 			winin = mgetch(stdscr);
    412 			if (winin < KEYSEQ_FIRST)
    413 				winin = tolower(winin);
    414 			again = 0;
    415 			switch (winin) {
    416 				case '<':
    417 				case 'u':
    418 				case KEYSEQ_UP_ARROW:
    419 				case KEYSEQ_LEFT_ARROW:
    420 				case KEYSEQ_PAGE_UP:
    421 					if (lineoff)
    422 						lineoff -= max_lines - 2;
    423 					else
    424 						again = 1;
    425 					break;
    426 				case '>':
    427 				case 'd':
    428 				case KEYSEQ_DOWN_ARROW:
    429 				case KEYSEQ_RIGHT_ARROW:
    430 				case KEYSEQ_PAGE_DOWN:
    431 					if (*help)
    432 						lineoff += max_lines - 2;
    433 					else
    434 						again = 1;
    435 					break;
    436 				case 'q':
    437 					break;
    438 				case 'x':
    439 					winin = 'q';
    440 					break;
    441 				default:
    442 					again = 1;
    443 			}
    444 			if (again)
    445 				mbeep();
    446 		} while (again);
    447 	} while (winin != 'q');
    448 
    449 	/* Restore current menu */
    450 	wclear(stdscr);
    451 	wrefresh(stdscr);
    452 	if (m->post_act)
    453 		(*m->post_act)();
    454 }
    455 
    456 static void process_req (struct menudesc *m, int num, int req)
    457 {
    458 	int ch;
    459 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1 );
    460 	int refresh = 0;
    461 	int scroll_sel = 0;
    462 
    463 	if (req == REQ_EXECUTE)
    464 		return;
    465 
    466 	else if (req == REQ_NEXT_ITEM) {
    467 		if (m->cursel < m->numopts + hasexit - 1) {
    468 			m->cursel++;
    469 			scroll_sel = 1;
    470 			refresh = 1;
    471 			if (m->mopt & MC_SCROLL &&
    472 			    m->cursel >= m->topline + m->h -1 )
    473 				m->topline += 1;
    474 		} else
    475 			mbeep();
    476 
    477 	} else if (req == REQ_PREV_ITEM) {
    478 		if (m->cursel > 0) {
    479 			m->cursel--;
    480 			scroll_sel = 1;
    481 			refresh = 1;
    482 			if (m->cursel < m->topline )
    483 				m->topline -= 1;
    484 		} else
    485 			mbeep();
    486 
    487 	} else if (req == REQ_REDISPLAY) {
    488 		wclear(stdscr);
    489 		wrefresh(stdscr);
    490 		if (m->post_act)
    491 			(*m->post_act)();
    492 		refresh = 1;
    493 
    494 	} else if (req == REQ_HELP) {
    495 		process_help (m, num);
    496 		refresh = 1;
    497 
    498 	} else if (req == REQ_SCROLLUP) {
    499 		if (!(m->mopt & MC_SCROLL))
    500 			mbeep();
    501 		else if (m->topline == 0)
    502 			mbeep();
    503 		else {
    504 			m->topline = MAX(0,m->topline-m->h+1);
    505 			wclear (m->mw);
    506 			refresh = 1;
    507 		}
    508 
    509 	} else if (req == REQ_SCROLLDOWN) {
    510 		if (!(m->mopt & MC_SCROLL))
    511 			mbeep();
    512 		else if (m->topline + m->h - 1 >= m->numopts + hasexit)
    513 			mbeep();
    514 		else {
    515 			m->topline = MIN(m->topline+m->h-1,
    516 					 m->numopts+hasexit-m->h+1);
    517 			wclear (m->mw);
    518 			refresh = 1;
    519 		}
    520 
    521 	} else {
    522 		ch = req;
    523 		if (ch == 'x' && hasexit) {
    524 			m->cursel = m->numopts;
    525 			scroll_sel = 1;
    526 			refresh = 1;
    527 		} else {
    528 			if (ch > 'z')
    529 				ch = 255;
    530 			if (ch >= 'a') {
    531 				if (ch > 'x') ch--;
    532 				ch = ch - 'a';
    533 			} else
    534 				ch = 25 + ch - 'A';
    535 			if (ch < 0 || ch >= m->numopts)
    536 				mbeep();
    537 			else {
    538 				m->cursel = ch;
    539 				scroll_sel = 1;
    540 				refresh = 1;
    541 			}
    542 		}
    543 	}
    544 
    545 	if (m->mopt & MC_SCROLL && scroll_sel) {
    546 		while (m->cursel >= m->topline + m->h -1 )
    547 			m->topline = MIN(m->topline+m->h-1,
    548 					 m->numopts+hasexit-m->h+1);
    549 		while (m->cursel < m->topline)
    550 			m->topline = MAX(0,m->topline-m->h+1);
    551 	}
    552 
    553 	if (refresh) {
    554 		post_menu (m);
    555 		wrefresh (m->mw);
    556 	}
    557 }
    558 
    559 void process_menu (int num)
    560 {
    561 	int sel = 0;
    562 	int req, done;
    563 	int last_num;
    564 
    565 	struct menudesc *m;
    566 
    567 	m = &menus[num];
    568 
    569 	done = FALSE;
    570 
    571 	/* Initialize? */
    572 	if (!__menu_init) {
    573 		if (initscr() == NULL) {
    574 			__menu_initerror();
    575 			return;
    576 		}
    577 		cbreak();
    578 		noecho();
    579 		max_lines = stdscr->maxy;
    580 		init_keyseq();
    581 #ifdef DYNAMIC_MENUS
    582 		num_menus = DYN_INIT_NUM;
    583 		while (num_menus < DYN_MENU_START)
    584 			num_menus *= 2;
    585 		menus = (menudesc *) malloc(sizeof(menudesc)*num_menus);
    586 		if (menus == NULL) {
    587 			__menu_initerror();
    588 			return ;
    589 		}
    590 		(void) memset ((void *)menus, 0, sizeof(menudesc)*num_menus);
    591 		(void) memcpy ((void *)menus, (void *)menu_def,
    592 			sizeof(menudesc)*DYN_MENU_START);
    593 		 num_avail = num_menus - DYN_MENU_START;
    594 #endif
    595 		__menu_init = 1;
    596 	}
    597 	if (__m_endwin) {
    598      		wclear(stdscr);
    599 		wrefresh(stdscr);
    600 		__m_endwin = 0;
    601 	}
    602 	if (m->mw == NULL)
    603 		init_menu (m);
    604 
    605 	/* Always preselect option 0 and display from 0! */
    606 	m->cursel = 0;
    607 	m->topline = 0;
    608 
    609 	while (!done) {
    610 		last_num = num;
    611 		if (__m_endwin) {
    612 			wclear(stdscr);
    613 			wrefresh(stdscr);
    614 			__m_endwin = 0;
    615 		}
    616 		/* Process the display action */
    617 		if (m->post_act)
    618 			(*m->post_act)();
    619 		post_menu (m);
    620 		wrefresh (m->mw);
    621 
    622 		while ((req = menucmd (m->mw)) != REQ_EXECUTE)
    623 			process_req (m, num, req);
    624 
    625 		sel = m->cursel;
    626 		wclear (m->mw);
    627 		wrefresh (m->mw);
    628 
    629 		/* Process the items */
    630 		if (sel < m->numopts) {
    631 			if (m->opts[sel].opt_flags & OPT_ENDWIN) {
    632 				endwin();
    633 				__m_endwin = 1;
    634 			}
    635 			if (m->opts[sel].opt_action)
    636 				(*m->opts[sel].opt_action)();
    637 			if (m->opts[sel].opt_menu != -1) {
    638 				if (m->opts[sel].opt_flags & OPT_SUB)
    639 					process_menu (m->opts[sel].opt_menu);
    640 				else
    641 					num = m->opts[sel].opt_menu;
    642 			}
    643 
    644 			if (m->opts[sel].opt_flags & OPT_EXIT)
    645 				done = TRUE;
    646 		} else
    647 			done = TRUE;
    648 
    649 		/* Reselect m just in case */
    650 		if (num != last_num) {
    651 			m = &menus[num];
    652 
    653 			/* Initialize? */
    654 			if (m->mw == NULL)
    655 				init_menu (m);
    656 			if (m->post_act)
    657 				(*m->post_act)();
    658 		}
    659 	}
    660 
    661 	/* Process the exit action */
    662 	if (m->exit_act)
    663 		(*m->exit_act)();
    664 }
    665 
    666 /* Control L is end of standard routines, remaining only for dynamic. */
    668 
    669 /* Beginning of routines for dynamic menus. */
    670 
    671 /* local prototypes */
    672 static int double_menus (void);
    673 
    674 static int double_menus (void)
    675 {
    676 	menudesc *temp;
    677 
    678 	temp  = (menudesc *) malloc(sizeof(menudesc)*num_menus*2);
    679 	if (temp == NULL)
    680 		return 0;
    681 	(void) memset ((void *)temp, 0,
    682 		sizeof(menudesc)*num_menus*2);
    683 	(void) memcpy ((void *)temp, (void *)menus,
    684 		sizeof(menudesc)*num_menus);
    685 	free (menus);
    686 	menus = temp;
    687 	num_avail = num_menus;
    688 	num_menus *= 2;
    689 
    690 	return 1;
    691 }
    692 
    693 int new_menu (char * title, menu_ent * opts, int numopts,
    694         int x, int y, int h, int w, int mopt,
    695         void (*post_act)(void), void (*exit_act), char * help)
    696 {
    697 	int ix;
    698 
    699 	/* Check for free menu entry. */
    700 	if (num_avail == 0)
    701 		if (!double_menus ())
    702 			return -1;
    703 
    704 	/* Find free menu entry. */
    705 	for (ix = DYN_MENU_START; ix < num_menus && menus[ix].mopt & MC_VALID;
    706 		ix++) /* do  nothing */;
    707 
    708 	/* if ix == num_menus ... panic */
    709 
    710 	/* Set Entries */
    711 	menus[ix].title = title;
    712 	menus[ix].opts = opts;
    713 	menus[ix].numopts = numopts;
    714 	menus[ix].x = x;
    715 	menus[ix].y = y;
    716 	menus[ix].h = h;
    717 	menus[ix].w = w;
    718 	menus[ix].mopt = mopt | MC_VALID;
    719 	menus[ix].post_act = post_act;
    720 	menus[ix].exit_act = exit_act;
    721 	menus[ix].helpstr  = help;
    722 
    723 	init_menu (&menus[ix]);
    724 
    725 	return ix;
    726 }
    727 
    728 void free_menu (int menu_no)
    729 {
    730 	if (menu_no < num_menus) {
    731 		menus[menu_no].mopt &= ~MC_VALID;
    732 		if (menus[menu_no].mw != NULL)
    733 			delwin (menus[menu_no].mw);
    734 	}
    735 }
    736