Home | History | Annotate | Line # | Download | only in menuc
menu_sys.def revision 1.49
      1  1.49       dsl /*	$NetBSD: menu_sys.def,v 1.49 2003/11/20 13:03:44 dsl Exp $	*/
      2   1.1      phil 
      3   1.1      phil /*
      4   1.1      phil  * Copyright 1997 Piermont Information Systems Inc.
      5   1.1      phil  * All rights reserved.
      6   1.1      phil  *
      7   1.1      phil  * Written by Philip A. Nelson for Piermont Information Systems Inc.
      8   1.1      phil  *
      9   1.1      phil  * Redistribution and use in source and binary forms, with or without
     10   1.1      phil  * modification, are permitted provided that the following conditions
     11   1.1      phil  * are met:
     12   1.1      phil  * 1. Redistributions of source code must retain the above copyright
     13   1.1      phil  *    notice, this list of conditions and the following disclaimer.
     14   1.1      phil  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1      phil  *    notice, this list of conditions and the following disclaimer in the
     16   1.1      phil  *    documentation and/or other materials provided with the distribution.
     17   1.1      phil  * 3. All advertising materials mentioning features or use of this software
     18   1.1      phil  *    must display the following acknowledgement:
     19   1.1      phil  *      This product includes software develooped for the NetBSD Project by
     20   1.1      phil  *      Piermont Information Systems Inc.
     21   1.1      phil  * 4. The name of Piermont Information Systems Inc. may not be used to endorse
     22   1.1      phil  *    or promote products derived from this software without specific prior
     23   1.1      phil  *    written permission.
     24   1.1      phil  *
     25   1.1      phil  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     26   1.1      phil  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27   1.1      phil  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28   1.1      phil  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     29   1.1      phil  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1      phil  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1      phil  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1      phil  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1      phil  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1      phil  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     35   1.1      phil  * THE POSSIBILITY OF SUCH DAMAGE.
     36   1.1      phil  *
     37   1.1      phil  */
     38   1.1      phil 
     39   1.1      phil /* menu_sys.defs -- Menu system standard routines. */
     40   1.1      phil 
     41   1.1      phil #include <string.h>
     42   1.1      phil #include <ctype.h>
     43   1.1      phil 
     44   1.3      phil #define REQ_EXECUTE    1000
     45   1.3      phil #define REQ_NEXT_ITEM  1001
     46   1.3      phil #define REQ_PREV_ITEM  1002
     47   1.3      phil #define REQ_REDISPLAY  1003
     48   1.9      phil #define REQ_SCROLLDOWN 1004
     49   1.9      phil #define REQ_SCROLLUP   1005
     50   1.9      phil #define REQ_HELP       1006
     51   1.9      phil 
     52   1.9      phil /* Macros */
     53   1.1      phil #define MAX(x,y) ((x)>(y)?(x):(y))
     54   1.7      phil #define MIN(x,y) ((x)<(y)?(x):(y))
     55   1.1      phil 
     56   1.1      phil /* Initialization state. */
     57   1.1      phil static int __menu_init = 0;
     58   1.1      phil int __m_endwin = 0;
     59  1.17       cgd static int max_lines = 0, max_cols = 0;
     60  1.48       dsl #ifndef scrolltext
     61  1.44       dsl static const char *scrolltext = " <: page up, >: page down";
     62  1.48       dsl #endif
     63   1.1      phil 
     64  1.14      phil static menudesc *menus = menu_def;
     65  1.14      phil 
     66  1.14      phil #ifdef DYNAMIC_MENUS
     67  1.14      phil static int num_menus  = 0;
     68  1.14      phil #define DYN_INIT_NUM 32
     69  1.14      phil #endif
     70  1.14      phil 
     71   1.1      phil /* prototypes for in here! */
     72  1.39       dsl static void init_menu(menudesc *m);
     73  1.43       dsl static char opt_ch(menudesc *m, int op_no);
     74  1.43       dsl static void draw_menu(menudesc *m, void *arg);
     75  1.39       dsl static void process_help(menudesc *m, int num);
     76  1.39       dsl static void process_req(menudesc *m, void *arg, int num, int req);
     77  1.36       dsl static int menucmd(WINDOW *w);
     78   1.1      phil 
     79   1.1      phil #ifndef NULL
     80  1.36       dsl #define NULL 0
     81   1.1      phil #endif
     82   1.1      phil 
     83   1.1      phil /* menu system processing routines */
     84  1.27  christos #define mbeep() (void)fputc('\a', stderr)
     85   1.1      phil 
     86  1.23   hubertf static int
     87  1.35       dsl menucmd(WINDOW *w)
     88   1.1      phil {
     89   1.1      phil 	int ch;
     90   1.1      phil 
     91   1.1      phil 	while (TRUE) {
     92  1.29     blymn 		ch = wgetch(w);
     93   1.1      phil 
     94   1.1      phil 		switch (ch) {
     95   1.1      phil 		case '\n':
     96   1.1      phil 			return REQ_EXECUTE;
     97  1.31       dsl 		case '\016':  /* Control-P */
     98  1.29     blymn 		case KEY_DOWN:
     99   1.1      phil 			return REQ_NEXT_ITEM;
    100  1.11      phil 		case '\020':  /* Control-N */
    101  1.29     blymn 		case KEY_UP:
    102   1.1      phil 			return REQ_PREV_ITEM;
    103  1.11      phil 		case '\014':  /* Control-L */
    104  1.36       dsl 			return REQ_REDISPLAY;
    105   1.9      phil 		case '<':
    106  1.11      phil 		case '\010':  /* Control-H (backspace) */
    107  1.29     blymn 		case KEY_PPAGE:
    108  1.31       dsl 		case KEY_LEFT:
    109   1.9      phil 			return REQ_SCROLLUP;
    110  1.31       dsl 		case '\026':  /* Control-V */
    111   1.9      phil 		case '>':
    112  1.11      phil 		case ' ':
    113  1.29     blymn 		case KEY_NPAGE:
    114  1.31       dsl 		case KEY_RIGHT:
    115   1.9      phil 			return REQ_SCROLLDOWN;
    116   1.9      phil 		case '?':
    117   1.9      phil 			return REQ_HELP;
    118  1.29     blymn 		case '\033': /* esc-v is scroll down */
    119  1.29     blymn 			ch = wgetch(w);
    120  1.29     blymn 			if (ch == 'v')
    121  1.29     blymn 				return REQ_SCROLLUP;
    122  1.29     blymn 			else
    123  1.29     blymn 				ch = 0; /* zap char so we beep */
    124   1.1      phil 		}
    125   1.1      phil 
    126   1.9      phil 		if (isalpha(ch))
    127  1.36       dsl 			return ch;
    128   1.1      phil 
    129   1.1      phil 		mbeep();
    130   1.1      phil 		wrefresh(w);
    131   1.1      phil 	}
    132   1.1      phil }
    133   1.1      phil 
    134  1.23   hubertf static void
    135  1.39       dsl init_menu(menudesc *m)
    136   1.1      phil {
    137  1.18       cgd 	int wmax;
    138  1.18       cgd 	int hadd, wadd, exithadd;
    139  1.14      phil 	int i;
    140  1.40       dsl 	const char *title;
    141   1.1      phil 
    142  1.18       cgd 	hadd = ((m->mopt & MC_NOBOX) ? 0 : 2);
    143  1.18       cgd 	wadd = ((m->mopt & MC_NOBOX) ? 2 : 4);
    144  1.18       cgd 
    145  1.40       dsl 	if (m->title && *(title = MSG_XLAT(m->title)) != 0) {
    146  1.40       dsl 		wmax = strlen(title);
    147  1.40       dsl 		hadd += 2;
    148  1.40       dsl 	} else {
    149  1.40       dsl 		m->title = NULL;
    150  1.40       dsl 		title = "untitled";
    151  1.40       dsl 		wmax = 0;
    152  1.40       dsl 	}
    153  1.18       cgd 	exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
    154  1.18       cgd 
    155  1.47       dsl #ifdef MSG_DEFS_H
    156  1.47       dsl 	if (m->y == -1)
    157  1.47       dsl 		m->y = msg_row();
    158  1.47       dsl #endif
    159   1.1      phil 
    160   1.7      phil 	/* Calculate h? h == number of visible options. */
    161  1.38       dsl 	if (m->h == 0)
    162  1.18       cgd 		m->h = m->numopts + exithadd;
    163  1.38       dsl 	m->h = MIN(m->h, max_lines - m->y - hadd);
    164   1.7      phil 
    165  1.48       dsl 	if (m->h < m->numopts + exithadd || m->mopt & MC_ALWAYS_SCROLL) {
    166  1.48       dsl 		if (!(m->mopt & (MC_SCROLL | MC_ALWAYS_SCROLL)) || m->h < 3) {
    167   1.7      phil 			endwin();
    168  1.35       dsl 			(void)fprintf(stderr,
    169  1.17       cgd 				"Window too short for menu \"%s\"\n",
    170  1.40       dsl 				title);
    171   1.7      phil 			exit(1);
    172   1.7      phil 		}
    173  1.38       dsl 		hadd++;
    174  1.38       dsl 		m->h--;
    175   1.7      phil 	} else
    176  1.14      phil 		m->mopt &= ~MC_SCROLL;
    177  1.38       dsl #if 0
    178   1.9      phil 	/* check for screen fit */
    179  1.18       cgd 	if (m->y + m->h + hadd > max_lines) {
    180   1.9      phil 		endwin();
    181  1.35       dsl 		(void)fprintf(stderr,
    182  1.38       dsl 		    "Screen too short (%d + %d + %d > %d) for menu \"%s\"\n",
    183  1.40       dsl 			m->y, m->h, hadd, max_lines, title);
    184   1.9      phil 		exit(1);
    185   1.9      phil 
    186   1.9      phil 	}
    187  1.38       dsl #endif
    188   1.9      phil 
    189   1.1      phil 	/* Calculate w? */
    190   1.1      phil 	if (m->w == 0) {
    191  1.49       dsl 		int l;
    192  1.49       dsl 		if (m->mopt & (MC_SCROLL | MC_ALWAYS_SCROLL)) {
    193  1.49       dsl 			l = strlen(scrolltext);
    194  1.49       dsl 			wmax = MAX(wmax, l);
    195  1.49       dsl 		}
    196  1.49       dsl 		for (i = 0; i < m->numopts; i++) {
    197  1.49       dsl 			l = strlen(MSG_XLAT(m->opts[i].opt_name));
    198  1.49       dsl 			if (!(m->mopt & MC_NOSHORTCUT))
    199  1.49       dsl 				l += 3;
    200  1.49       dsl 			wmax = MAX(wmax, l);
    201  1.49       dsl 		}
    202  1.18       cgd 		m->w = wmax;
    203   1.1      phil 	}
    204   1.1      phil 
    205  1.17       cgd 	/* check and adjust for screen fit */
    206  1.18       cgd 	if (m->w + wadd > max_cols) {
    207  1.17       cgd 		endwin();
    208  1.35       dsl 		(void)fprintf(stderr,
    209  1.40       dsl 			"Screen too narrow for menu \"%s\"\n", title);
    210  1.17       cgd 		exit(1);
    211  1.17       cgd 
    212  1.17       cgd 	}
    213  1.20       cgd 	if (m->x == -1)
    214  1.20       cgd 		m->x = (max_cols - (m->w + wadd)) / 2;	/* center */
    215  1.20       cgd 	else if (m->x + m->w + wadd > max_cols)
    216  1.49       dsl 		m->x = max_cols - (m->w + wadd);	/* right align */
    217  1.17       cgd 
    218   1.1      phil 	/* Get the windows. */
    219  1.36       dsl 	m->mw = newwin(m->h + hadd, m->w + wadd, m->y, m->x);
    220   1.1      phil 
    221   1.1      phil 	if (m->mw == NULL) {
    222   1.1      phil 		endwin();
    223  1.35       dsl 		(void)fprintf(stderr,
    224  1.42       dsl 			"Could not create window (%d + %d, %d + %d, %d, %d) for menu \"%s\"\n",
    225  1.42       dsl 			m->h, hadd, m->w, wadd, m->y, m->x, title);
    226   1.1      phil 		exit(1);
    227  1.26     perry 	}
    228  1.38       dsl 	keypad(m->mw, TRUE); /* enable multi-key assembling for win */
    229  1.26     perry 
    230  1.26     perry 	/* XXX is it even worth doing this right? */
    231  1.26     perry 	if (has_colors()) {
    232  1.26     perry 		wbkgd(m->mw, COLOR_PAIR(1));
    233  1.26     perry 		wattrset(m->mw, COLOR_PAIR(1));
    234  1.26     perry 	}
    235   1.1      phil }
    236   1.1      phil 
    237  1.23   hubertf static char
    238  1.43       dsl opt_ch(menudesc *m, int op_no)
    239  1.14      phil {
    240  1.14      phil 	char c;
    241  1.43       dsl 
    242  1.43       dsl 	if (op_no == m->numopts)
    243  1.43       dsl 		return 'x';
    244  1.43       dsl 
    245  1.14      phil 	if (op_no < 25) {
    246  1.14      phil 		c = 'a' + op_no;
    247  1.35       dsl 		if (c >= 'x')
    248  1.35       dsl 			c++;
    249  1.14      phil 	} else
    250  1.14      phil 		c = 'A' + op_no - 25;
    251  1.35       dsl 	return c;
    252  1.14      phil }
    253  1.14      phil 
    254  1.23   hubertf static void
    255  1.43       dsl draw_menu_line(menudesc *m, int opt, int cury, void *arg, const char *text)
    256  1.36       dsl {
    257  1.36       dsl 	int hasbox = m->mopt & MC_NOBOX ? 0 : 1;
    258  1.36       dsl 
    259  1.43       dsl 	if (m->cursel == opt) {
    260  1.36       dsl 		mvwaddstr(m->mw, cury, hasbox, ">");
    261  1.36       dsl 		wstandout(m->mw);
    262  1.36       dsl 	} else
    263  1.36       dsl 		mvwaddstr(m->mw, cury, hasbox, " ");
    264  1.36       dsl 	if (!(m->mopt & MC_NOSHORTCUT))
    265  1.43       dsl 		wprintw(m->mw, "%c: ", opt_ch(m, opt));
    266  1.43       dsl 
    267  1.43       dsl 	if (!text && m->draw_line)
    268  1.43       dsl 		m->draw_line(m, opt, arg);
    269  1.43       dsl 	else
    270  1.43       dsl 		waddstr(m->mw, MSG_XLAT(text));
    271  1.43       dsl 	if (m->cursel == opt)
    272  1.36       dsl 		wstandend(m->mw);
    273  1.36       dsl }
    274  1.36       dsl 
    275  1.36       dsl static void
    276  1.43       dsl draw_menu(menudesc *m, void *arg)
    277   1.1      phil {
    278  1.38       dsl 	int opt;
    279  1.36       dsl 	int hasbox, cury, maxy;
    280   1.1      phil 	int tadd;
    281  1.45       dsl 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
    282   1.1      phil 
    283  1.14      phil 	if (m->mopt & MC_NOBOX) {
    284   1.1      phil 		cury = 0;
    285   1.7      phil 		maxy = m->h;
    286   1.1      phil 		hasbox = 0;
    287   1.1      phil 	} else {
    288   1.1      phil 		cury = 1;
    289  1.35       dsl 		maxy = m->h + 1;
    290   1.1      phil 		hasbox = 1;
    291   1.1      phil 	}
    292   1.1      phil 
    293   1.9      phil 	/* Clear the window */
    294  1.35       dsl 	wclear(m->mw);
    295   1.9      phil 
    296  1.40       dsl 	if (m->title) {
    297  1.36       dsl 		mvwaddstr(m->mw, hasbox, hasbox, " ");
    298  1.43       dsl 		waddstr(m->mw, MSG_XLAT(m->title));
    299  1.40       dsl 		tadd = 2;
    300  1.36       dsl 		cury += tadd;
    301  1.36       dsl 		maxy += tadd;
    302  1.40       dsl 	} else
    303  1.40       dsl 		tadd = 0;
    304   1.1      phil 
    305  1.39       dsl 	if (m->cursel == -1) {
    306  1.39       dsl 		m->cursel = m->numopts;
    307  1.39       dsl 		if (m->h <= m->numopts)
    308  1.39       dsl 			m->topline = m->numopts + 1 - m->h;
    309  1.39       dsl 	}
    310  1.39       dsl 
    311  1.45       dsl 	while (m->cursel >= m->topline + m->h)
    312  1.45       dsl 		m->topline = MIN(m->topline + m->h,
    313  1.45       dsl 				 m->numopts + hasexit - m->h);
    314  1.45       dsl 	while (m->cursel < m->topline)
    315  1.45       dsl 		m->topline = MAX(0, m->topline - m->h);
    316  1.45       dsl 
    317  1.38       dsl 	for (opt = m->topline; opt < m->numopts; opt++) {
    318  1.36       dsl 		if (cury >= maxy)
    319  1.36       dsl 			break;
    320  1.43       dsl 		draw_menu_line(m, opt, cury++, arg, m->opts[opt].opt_name);
    321   1.1      phil 	}
    322   1.7      phil 
    323   1.7      phil 	/* Add the exit option. */
    324  1.38       dsl 	if (!(m->mopt & MC_NOEXITOPT)) {
    325  1.38       dsl 		if (cury < maxy)
    326  1.43       dsl 			draw_menu_line(m, m->numopts, cury++, arg, m->exitstr);
    327  1.38       dsl 		else
    328  1.38       dsl 			opt = 0;
    329  1.38       dsl 	}
    330   1.7      phil 
    331   1.7      phil 	/* Add the scroll line */
    332  1.38       dsl 	if (opt != m->numopts || m->topline != 0)
    333  1.35       dsl 		mvwaddstr(m->mw, cury, hasbox, scrolltext);
    334   1.7      phil 
    335   1.7      phil 	/* Add the box. */
    336  1.14      phil 	if (!(m->mopt & MC_NOBOX))
    337  1.26     perry 		box(m->mw, 0, 0);
    338   1.1      phil 
    339  1.36       dsl 	wmove(m->mw, tadd + hasbox + m->cursel - m->topline, hasbox);
    340  1.36       dsl 	wrefresh(m->mw);
    341   1.1      phil }
    342   1.1      phil 
    343  1.23   hubertf static void
    344  1.30  christos /*ARGSUSED*/
    345  1.39       dsl process_help(menudesc *m, int num)
    346   1.5      phil {
    347  1.34       dsl 	const char *help = m->helpstr;
    348   1.7      phil 	int lineoff = 0;
    349   1.5      phil 	int curoff = 0;
    350   1.5      phil 	int again;
    351   1.7      phil 	int winin;
    352   1.5      phil 
    353   1.6      phil 	/* Is there help? */
    354   1.6      phil 	if (!help) {
    355   1.6      phil 		mbeep();
    356   1.6      phil 		return;
    357   1.6      phil 	}
    358  1.40       dsl 	help = MSG_XLAT(help);
    359   1.6      phil 
    360   1.6      phil 	/* Display the help information. */
    361   1.7      phil 	do {
    362   1.5      phil 		if (lineoff < curoff) {
    363  1.40       dsl 			help = MSG_XLAT(m->helpstr);
    364   1.5      phil 			curoff = 0;
    365   1.5      phil 		}
    366   1.5      phil 		while (*help && curoff < lineoff) {
    367   1.5      phil 			if (*help == '\n')
    368   1.5      phil 				curoff++;
    369   1.5      phil 			help++;
    370   1.5      phil 		}
    371   1.5      phil 
    372   1.5      phil 		wclear(stdscr);
    373  1.35       dsl 		mvwaddstr(stdscr, 0, 0,
    374   1.9      phil 			"Help: exit: x,  page up: u <, page down: d >");
    375  1.35       dsl 		mvwaddstr(stdscr, 2, 0, help);
    376  1.35       dsl 		wmove(stdscr, 1, 0);
    377  1.36       dsl 		wrefresh(stdscr);
    378   1.5      phil 
    379   1.5      phil 		do {
    380  1.29     blymn 			winin = wgetch(stdscr);
    381  1.29     blymn 			if (winin < KEY_MIN)
    382   1.9      phil 				winin = tolower(winin);
    383   1.5      phil 			again = 0;
    384   1.5      phil 			switch (winin) {
    385   1.5      phil 				case '<':
    386   1.5      phil 				case 'u':
    387  1.29     blymn 				case KEY_UP:
    388  1.29     blymn 				case KEY_LEFT:
    389  1.29     blymn 				case KEY_PPAGE:
    390   1.5      phil 					if (lineoff)
    391   1.5      phil 						lineoff -= max_lines - 2;
    392   1.7      phil 					else
    393   1.7      phil 						again = 1;
    394   1.5      phil 					break;
    395   1.5      phil 				case '>':
    396   1.5      phil 				case 'd':
    397  1.29     blymn 				case KEY_DOWN:
    398  1.29     blymn 				case KEY_RIGHT:
    399  1.29     blymn 				case KEY_NPAGE:
    400   1.5      phil 					if (*help)
    401   1.5      phil 						lineoff += max_lines - 2;
    402   1.7      phil 					else
    403   1.7      phil 						again = 1;
    404   1.5      phil 					break;
    405  1.10      phil 				case 'q':
    406  1.10      phil 					break;
    407   1.9      phil 				case 'x':
    408  1.10      phil 					winin = 'q';
    409   1.5      phil 					break;
    410   1.5      phil 				default:
    411   1.5      phil 					again = 1;
    412   1.5      phil 			}
    413   1.9      phil 			if (again)
    414   1.9      phil 				mbeep();
    415   1.5      phil 		} while (again);
    416   1.5      phil 	} while (winin != 'q');
    417   1.5      phil }
    418   1.5      phil 
    419  1.23   hubertf static void
    420  1.39       dsl process_req(menudesc *m, void *arg, int num, int req)
    421   1.1      phil {
    422   1.1      phil 	int ch;
    423  1.35       dsl 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
    424   1.1      phil 
    425  1.35       dsl 	switch(req) {
    426  1.35       dsl 
    427  1.35       dsl 	case REQ_EXECUTE:
    428   1.1      phil 		return;
    429   1.7      phil 
    430  1.35       dsl 	case REQ_NEXT_ITEM:
    431  1.41       dsl 		ch = m->cursel;
    432  1.41       dsl 		for (;;) {
    433  1.41       dsl 			ch++;
    434  1.41       dsl 			if (ch >= m->numopts + hasexit) {
    435  1.41       dsl 				mbeep();
    436  1.41       dsl 				return;
    437  1.41       dsl 			}
    438  1.41       dsl 			if (hasexit && ch == m->numopts)
    439  1.41       dsl 				break;
    440  1.43       dsl 			if (!(m->opts[ch].opt_flags & OPT_IGNORE))
    441  1.41       dsl 				break;
    442  1.35       dsl 		}
    443  1.41       dsl 		m->cursel = ch;
    444  1.48       dsl 		if (m->cursel >= m->topline + m->h)
    445  1.41       dsl 			m->topline = m->cursel - m->h + 1;
    446  1.35       dsl 		break;
    447   1.7      phil 
    448  1.35       dsl 	case REQ_PREV_ITEM:
    449  1.41       dsl 		ch = m->cursel;
    450  1.41       dsl 		for (;;) {
    451  1.41       dsl 			if (ch <= 0) {
    452  1.41       dsl 				mbeep();
    453  1.41       dsl 				return;
    454  1.41       dsl 			}
    455  1.41       dsl 			ch--;
    456  1.43       dsl 			if (!(m->opts[ch].opt_flags & OPT_IGNORE))
    457  1.41       dsl 				break;
    458  1.35       dsl 		}
    459  1.41       dsl 		m->cursel = ch;
    460  1.35       dsl 		if (m->cursel < m->topline)
    461  1.41       dsl 			m->topline = m->cursel;
    462  1.35       dsl 		break;
    463   1.3      phil 
    464  1.39       dsl 	case REQ_HELP:
    465  1.39       dsl 		process_help(m, num);
    466  1.39       dsl 		/* FALLTHROUGH */
    467  1.39       dsl 
    468  1.35       dsl 	case REQ_REDISPLAY:
    469   1.3      phil 		wclear(stdscr);
    470   1.3      phil 		wrefresh(stdscr);
    471  1.14      phil 		if (m->post_act)
    472  1.39       dsl 			(*m->post_act)(m, arg);
    473  1.35       dsl 		break;
    474   1.7      phil 
    475  1.35       dsl 	case REQ_SCROLLUP:
    476  1.35       dsl 		if (m->cursel == 0) {
    477   1.7      phil 			mbeep();
    478  1.35       dsl 			return;
    479   1.7      phil 		}
    480  1.36       dsl 		m->topline = MAX(0, m->topline - m->h);
    481  1.36       dsl 		m->cursel = MAX(0, m->cursel - m->h);
    482  1.35       dsl 		wclear(m->mw);
    483  1.35       dsl 		break;
    484   1.7      phil 
    485  1.35       dsl 	case REQ_SCROLLDOWN:
    486  1.35       dsl 		if (m->cursel >= m->numopts + hasexit - 1) {
    487   1.7      phil 			mbeep();
    488  1.35       dsl 			return;
    489   1.7      phil 		}
    490  1.36       dsl 		m->topline = MIN(m->topline + m->h,
    491  1.38       dsl 				 MAX(m->numopts + hasexit - m->h, 0));
    492  1.36       dsl 		m->cursel = MIN(m->numopts + hasexit - 1, m->cursel + m->h);
    493  1.35       dsl 		wclear(m->mw);
    494  1.35       dsl 		break;
    495   1.7      phil 
    496  1.35       dsl 	default:
    497   1.9      phil 		ch = req;
    498   1.7      phil 		if (ch == 'x' && hasexit) {
    499   1.1      phil 			m->cursel = m->numopts;
    500  1.35       dsl 			break;
    501  1.35       dsl 		}
    502  1.35       dsl 		if (m->mopt & MC_NOSHORTCUT) {
    503  1.35       dsl 			mbeep();
    504  1.35       dsl 			return;
    505  1.35       dsl 		}
    506  1.35       dsl 		if (ch > 'z')
    507  1.35       dsl 			ch = 255;
    508  1.35       dsl 		if (ch >= 'a') {
    509  1.35       dsl 			if (ch > 'x')
    510  1.35       dsl 				ch--;
    511  1.35       dsl 			ch = ch - 'a';
    512  1.35       dsl 		} else
    513  1.35       dsl 			ch = 25 + ch - 'A';
    514  1.35       dsl 		if (ch < 0 || ch >= m->numopts) {
    515  1.41       dsl 			mbeep();
    516  1.41       dsl 			return;
    517  1.41       dsl 		}
    518  1.43       dsl 		if (m->opts[ch].opt_flags & OPT_IGNORE) {
    519  1.35       dsl 			mbeep();
    520  1.35       dsl 			return;
    521  1.35       dsl 		}
    522  1.35       dsl 		m->cursel = ch;
    523   1.1      phil 	}
    524   1.9      phil 
    525  1.43       dsl 	draw_menu(m, arg);
    526   1.1      phil }
    527   1.1      phil 
    528  1.23   hubertf int
    529  1.35       dsl menu_init(void)
    530  1.17       cgd {
    531  1.17       cgd 
    532  1.17       cgd 	if (__menu_init)
    533  1.17       cgd 		return 0;
    534  1.33       dsl 
    535  1.33       dsl #ifdef	USER_MENU_INIT
    536  1.33       dsl 	if (USER_MENU_INIT)
    537  1.33       dsl 		return 1;
    538  1.33       dsl #endif
    539  1.17       cgd 
    540  1.17       cgd 	if (initscr() == NULL)
    541  1.17       cgd 		return 1;
    542  1.17       cgd 
    543  1.17       cgd 	cbreak();
    544  1.17       cgd 	noecho();
    545  1.26     perry 
    546  1.26     perry 	/* XXX Should be configurable but it almost isn't worth it. */
    547  1.26     perry 	if (has_colors()) {
    548  1.26     perry 		start_color();
    549  1.26     perry 		init_pair(1, COLOR_WHITE, COLOR_BLUE);
    550  1.26     perry 		bkgd(COLOR_PAIR(1));
    551  1.26     perry 		attrset(COLOR_PAIR(1));
    552  1.26     perry 	}
    553  1.26     perry 
    554  1.17       cgd 	max_lines = getmaxy(stdscr);
    555  1.17       cgd 	max_cols = getmaxx(stdscr);
    556  1.29     blymn 	keypad(stdscr, TRUE);
    557  1.17       cgd #ifdef DYNAMIC_MENUS
    558  1.17       cgd 	num_menus = DYN_INIT_NUM;
    559  1.17       cgd 	while (num_menus < DYN_MENU_START)
    560  1.17       cgd 		num_menus *= 2;
    561  1.35       dsl 	menus = malloc(sizeof(menudesc) * num_menus);
    562  1.17       cgd 	if (menus == NULL)
    563  1.17       cgd 		return 2;
    564  1.35       dsl 	(void)memset(menus, 0, sizeof(menudesc) * num_menus);
    565  1.36       dsl 	(void)memcpy(menus, menu_def, sizeof(menudesc) * DYN_MENU_START);
    566  1.17       cgd #endif
    567  1.17       cgd 
    568  1.17       cgd 	__menu_init = 1;
    569  1.36       dsl 	return 0;
    570  1.17       cgd }
    571  1.17       cgd 
    572  1.23   hubertf void
    573  1.37       dsl process_menu(int num, void *arg)
    574   1.1      phil {
    575   1.1      phil 	int sel = 0;
    576   1.1      phil 	int req, done;
    577   1.1      phil 	int last_num;
    578  1.37       dsl 	menu_ent *opt;
    579   1.1      phil 
    580  1.39       dsl 	menudesc *m;
    581  1.14      phil 
    582  1.14      phil 	m = &menus[num];
    583   1.1      phil 
    584   1.1      phil 	done = FALSE;
    585   1.1      phil 
    586   1.1      phil 	/* Initialize? */
    587  1.17       cgd 	if (menu_init()) {
    588  1.17       cgd 		__menu_initerror();
    589  1.17       cgd 		return;
    590   1.1      phil 	}
    591  1.17       cgd 
    592   1.1      phil 	if (__m_endwin) {
    593   1.7      phil      		wclear(stdscr);
    594   1.1      phil 		wrefresh(stdscr);
    595   1.1      phil 		__m_endwin = 0;
    596   1.1      phil 	}
    597   1.1      phil 
    598  1.39       dsl 	/* Default to select option 0 and display from 0 */
    599   1.7      phil 	m->topline = 0;
    600  1.39       dsl 	if ((m->mopt & (MC_DFLTEXIT | MC_NOEXITOPT)) == MC_DFLTEXIT)
    601  1.39       dsl 		m->cursel = -1;
    602  1.39       dsl 	else
    603  1.39       dsl 		m->cursel = 0;
    604   1.1      phil 
    605   1.1      phil 	while (!done) {
    606   1.1      phil 		last_num = num;
    607   1.1      phil 		if (__m_endwin) {
    608   1.1      phil 			wclear(stdscr);
    609   1.1      phil 			wrefresh(stdscr);
    610   1.1      phil 			__m_endwin = 0;
    611   1.1      phil 		}
    612   1.1      phil 		/* Process the display action */
    613  1.14      phil 		if (m->post_act)
    614  1.39       dsl 			(*m->post_act)(m, arg);
    615  1.47       dsl 		if (m->mw == NULL)
    616  1.47       dsl 			init_menu(m);
    617  1.43       dsl 		draw_menu(m, arg);
    618   1.1      phil 
    619  1.36       dsl 		while ((req = menucmd(m->mw)) != REQ_EXECUTE)
    620  1.39       dsl 			process_req(m, arg, num, req);
    621   1.1      phil 
    622   1.1      phil 		sel = m->cursel;
    623  1.38       dsl 		if (!(m->mopt & MC_NOCLEAR)) {
    624  1.38       dsl 			wclear(m->mw);
    625  1.38       dsl 			wrefresh(m->mw);
    626  1.38       dsl 		}
    627   1.1      phil 
    628   1.1      phil 		/* Process the items */
    629  1.14      phil 		if (sel < m->numopts) {
    630  1.37       dsl 			opt = &m->opts[sel];
    631  1.43       dsl 			if (opt->opt_flags & OPT_IGNORE)
    632  1.43       dsl 				continue;
    633  1.37       dsl 			if (opt->opt_flags & OPT_ENDWIN) {
    634  1.14      phil 				endwin();
    635  1.14      phil 				__m_endwin = 1;
    636  1.14      phil 			}
    637  1.37       dsl 			if (opt->opt_action)
    638  1.45       dsl 				done = (*opt->opt_action)(m, arg);
    639  1.37       dsl 			if (opt->opt_menu != -1) {
    640  1.37       dsl 				if (opt->opt_flags & OPT_SUB)
    641  1.37       dsl 					process_menu(opt->opt_menu, arg);
    642  1.14      phil 				else
    643  1.37       dsl 					num = opt->opt_menu;
    644  1.14      phil 			}
    645  1.15      phil 
    646  1.37       dsl                         if (opt->opt_flags & OPT_EXIT)
    647  1.15      phil                                 done = TRUE;
    648  1.15      phil 
    649  1.14      phil 		} else
    650   1.1      phil 			done = TRUE;
    651   1.1      phil 
    652   1.1      phil 		/* Reselect m just in case */
    653  1.47       dsl 		if (num != last_num)
    654   1.1      phil 			m = &menus[num];
    655   1.1      phil 	}
    656   1.1      phil 
    657  1.38       dsl 	if (m->mopt & MC_NOCLEAR) {
    658  1.38       dsl 		wclear(m->mw);
    659  1.38       dsl 		wrefresh(m->mw);
    660  1.38       dsl 	}
    661  1.38       dsl 
    662   1.1      phil 	/* Process the exit action */
    663  1.14      phil 	if (m->exit_act)
    664  1.39       dsl 		(*m->exit_act)(m, arg);
    665   1.1      phil }
    666  1.13      phil 
    667  1.13      phil /* Control L is end of standard routines, remaining only for dynamic. */
    669  1.13      phil 
    670  1.13      phil /* Beginning of routines for dynamic menus. */
    671  1.24      phil 
    672  1.35       dsl static int
    673  1.14      phil double_menus(void)
    674  1.14      phil {
    675  1.39       dsl 	menudesc *temp;
    676  1.14      phil 	int sz = sizeof(menudesc) * num_menus;
    677  1.39       dsl 
    678  1.14      phil 	temp  = realloc(menus, sz * 2);
    679  1.14      phil 	if (temp == NULL)
    680  1.39       dsl 		return 0;
    681  1.14      phil 	(void)memset(temp + num_menus, 0, sz);
    682  1.14      phil 	menus = temp;
    683  1.14      phil 	num_menus *= 2;
    684  1.14      phil 
    685  1.14      phil 	return 1;
    686  1.14      phil }
    687  1.23   hubertf 
    688  1.38       dsl int
    689  1.24      phil new_menu(const char *title, menu_ent *opts, int numopts,
    690  1.39       dsl 	int x, int y, int h, int w, int mopt,
    691  1.43       dsl 	void (*post_act)(menudesc *, void *),
    692  1.39       dsl 	void (*draw_line)(menudesc *, int, void *),
    693  1.44       dsl 	void (*exit_act)(menudesc *, void *),
    694  1.14      phil 	const char *help, const char *exit_str)
    695  1.14      phil {
    696  1.39       dsl 	int ix;
    697   1.7      phil 	menudesc *m;
    698  1.39       dsl 
    699  1.39       dsl 	/* Find free menu entry. */
    700  1.46  takemura 	for (ix = DYN_MENU_START; ; ix++) {
    701  1.14      phil 		if (ix >= num_menus && !double_menus())
    702  1.39       dsl 			return -1;
    703  1.39       dsl 		if (!(menus[ix].mopt & MC_VALID))
    704  1.39       dsl 			break;
    705  1.14      phil 	}
    706  1.14      phil 
    707  1.39       dsl 	/* Set Entries */
    708  1.40       dsl 	m = menus + ix;
    709  1.39       dsl 	m->title = title;
    710  1.39       dsl 	m->opts = opts;
    711  1.39       dsl 	m->numopts = numopts;
    712  1.39       dsl 	m->x = x;
    713  1.39       dsl 	m->y = y;
    714  1.39       dsl 	m->h = h;
    715  1.39       dsl 	m->w = w;
    716  1.39       dsl 	m->mopt = mopt | MC_VALID;
    717  1.43       dsl 	m->post_act = post_act;
    718  1.39       dsl 	m->draw_line = draw_line;
    719  1.39       dsl 	m->exit_act = exit_act;
    720  1.44       dsl 	m->helpstr  = help;
    721  1.14      phil 	m->exitstr  = exit_str ? exit_str : "Exit";
    722  1.14      phil 
    723  1.14      phil 	return ix;
    724  1.14      phil }
    725  1.23   hubertf 
    726  1.35       dsl void
    727  1.14      phil free_menu(int menu_no)
    728  1.39       dsl {
    729  1.39       dsl 	menudesc *m;
    730  1.39       dsl 
    731  1.39       dsl 	if (menu_no < 0 || menu_no >= num_menus)
    732  1.39       dsl 		return;
    733  1.39       dsl 
    734  1.39       dsl 	m = menus + menu_no;
    735  1.39       dsl 	if ((m->mopt & MC_VALID))
    736  1.39       dsl 		return;
    737  1.39       dsl 	if (m->mw != NULL)
    738  1.39       dsl 		delwin(m->mw);
    739  1.39       dsl 	memset(m, 0, sizeof *m);
    740  1.39       dsl }
    741  1.39       dsl 
    742  1.39       dsl void
    743  1.39       dsl set_menu_numopts(int menu, int numopts)
    744  1.39       dsl {
    745  1.39       dsl 
    746  1.14      phil 	menus[menu].numopts = numopts;
    747                 }
    748