Home | History | Annotate | Line # | Download | only in menuc
menu_sys.def revision 1.54
      1  1.54       dsl /*	$NetBSD: menu_sys.def,v 1.54 2004/08/02 21:29:07 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.17       cgd static int max_lines = 0, max_cols = 0;
     59  1.48       dsl #ifndef scrolltext
     60  1.44       dsl static const char *scrolltext = " <: page up, >: page down";
     61  1.48       dsl #endif
     62   1.1      phil 
     63  1.14      phil #ifdef DYNAMIC_MENUS
     64  1.14      phil static int num_menus  = 0;
     65  1.14      phil #define DYN_INIT_NUM 32
     66  1.52       dsl static menudesc **menu_list;
     67  1.52       dsl #define MENUS(n) (*(menu_list[n]))
     68  1.52       dsl #else
     69  1.52       dsl #define MENUS(n) (menu_def[n])
     70  1.14      phil #endif
     71  1.14      phil 
     72   1.1      phil /* prototypes for in here! */
     73  1.39       dsl static void init_menu(menudesc *m);
     74  1.43       dsl static char opt_ch(menudesc *m, int op_no);
     75  1.43       dsl static void draw_menu(menudesc *m, void *arg);
     76  1.52       dsl static void process_help(menudesc *m);
     77  1.52       dsl static void process_req(menudesc *m, void *arg, int req);
     78  1.36       dsl static int menucmd(WINDOW *w);
     79   1.1      phil 
     80   1.1      phil #ifndef NULL
     81  1.36       dsl #define NULL 0
     82   1.1      phil #endif
     83   1.1      phil 
     84   1.1      phil /* menu system processing routines */
     85  1.27  christos #define mbeep() (void)fputc('\a', stderr)
     86   1.1      phil 
     87  1.23   hubertf static int
     88  1.35       dsl menucmd(WINDOW *w)
     89   1.1      phil {
     90   1.1      phil 	int ch;
     91   1.1      phil 
     92   1.1      phil 	while (TRUE) {
     93  1.29     blymn 		ch = wgetch(w);
     94   1.1      phil 
     95   1.1      phil 		switch (ch) {
     96   1.1      phil 		case '\n':
     97   1.1      phil 			return REQ_EXECUTE;
     98  1.31       dsl 		case '\016':  /* Control-P */
     99  1.29     blymn 		case KEY_DOWN:
    100   1.1      phil 			return REQ_NEXT_ITEM;
    101  1.11      phil 		case '\020':  /* Control-N */
    102  1.29     blymn 		case KEY_UP:
    103   1.1      phil 			return REQ_PREV_ITEM;
    104  1.11      phil 		case '\014':  /* Control-L */
    105  1.36       dsl 			return REQ_REDISPLAY;
    106   1.9      phil 		case '<':
    107  1.11      phil 		case '\010':  /* Control-H (backspace) */
    108  1.29     blymn 		case KEY_PPAGE:
    109  1.31       dsl 		case KEY_LEFT:
    110   1.9      phil 			return REQ_SCROLLUP;
    111  1.31       dsl 		case '\026':  /* Control-V */
    112   1.9      phil 		case '>':
    113  1.11      phil 		case ' ':
    114  1.29     blymn 		case KEY_NPAGE:
    115  1.31       dsl 		case KEY_RIGHT:
    116   1.9      phil 			return REQ_SCROLLDOWN;
    117   1.9      phil 		case '?':
    118   1.9      phil 			return REQ_HELP;
    119  1.29     blymn 		case '\033': /* esc-v is scroll down */
    120  1.29     blymn 			ch = wgetch(w);
    121  1.29     blymn 			if (ch == 'v')
    122  1.29     blymn 				return REQ_SCROLLUP;
    123  1.29     blymn 			else
    124  1.29     blymn 				ch = 0; /* zap char so we beep */
    125   1.1      phil 		}
    126   1.1      phil 
    127   1.9      phil 		if (isalpha(ch))
    128  1.36       dsl 			return ch;
    129   1.1      phil 
    130   1.1      phil 		mbeep();
    131   1.1      phil 		wrefresh(w);
    132   1.1      phil 	}
    133   1.1      phil }
    134   1.1      phil 
    135  1.54       dsl WINDOW *sv_mw;
    136  1.23   hubertf static void
    137  1.39       dsl init_menu(menudesc *m)
    138   1.1      phil {
    139  1.18       cgd 	int wmax;
    140  1.18       cgd 	int hadd, wadd, exithadd;
    141  1.14      phil 	int i;
    142  1.50       dsl 	int x, y, w;
    143  1.50       dsl 	const char *title, *tp, *ep;
    144   1.1      phil 
    145  1.50       dsl 	x = m->x;
    146  1.50       dsl 	y = m->y;
    147  1.50       dsl 	w = m->w;
    148  1.50       dsl 	wmax = 0;
    149  1.18       cgd 	hadd = ((m->mopt & MC_NOBOX) ? 0 : 2);
    150  1.18       cgd 	wadd = ((m->mopt & MC_NOBOX) ? 2 : 4);
    151  1.18       cgd 
    152  1.40       dsl 	if (m->title && *(title = MSG_XLAT(m->title)) != 0) {
    153  1.50       dsl 		/* Allow multiple line titles */
    154  1.50       dsl 		for (tp = title; ep = strchr(tp, '\n'); tp = ep + 1) {
    155  1.50       dsl 			i = ep - tp;
    156  1.50       dsl 			wmax = MAX(wmax, i);
    157  1.50       dsl 			hadd++;
    158  1.50       dsl 		}
    159  1.50       dsl 		hadd++;
    160  1.50       dsl 		i = strlen(tp);
    161  1.51       dsl 		wmax = MAX(wmax, i);
    162  1.50       dsl 		if (i != 0)
    163  1.50       dsl 			hadd++;
    164  1.40       dsl 	} else {
    165  1.40       dsl 		m->title = NULL;
    166  1.40       dsl 		title = "untitled";
    167  1.40       dsl 	}
    168  1.18       cgd 	exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
    169  1.18       cgd 
    170  1.47       dsl #ifdef MSG_DEFS_H
    171  1.53       dsl 	if (y < 0) {
    172  1.53       dsl 		/* put menu box below message text */
    173  1.53       dsl 		y = -y;
    174  1.53       dsl 		i = msg_row();
    175  1.53       dsl 		if (i > y)
    176  1.53       dsl 		    y = i;
    177  1.53       dsl 	}
    178  1.47       dsl #endif
    179   1.1      phil 
    180   1.7      phil 	/* Calculate h? h == number of visible options. */
    181  1.38       dsl 	if (m->h == 0)
    182  1.18       cgd 		m->h = m->numopts + exithadd;
    183  1.54       dsl 
    184  1.54       dsl 	if (m->h > max_lines - y - hadd) {
    185  1.54       dsl 		/* Not enough space for all the options */
    186  1.54       dsl 		if (m->h <= 4 || !(m->mopt & (MC_SCROLL | MC_ALWAYS_SCROLL))) {
    187  1.54       dsl 			/* move menu up screen */
    188  1.54       dsl 			y = max_lines - hadd - m->h;
    189  1.54       dsl 			if (y < 0)
    190  1.54       dsl 				y = 0;
    191  1.54       dsl 		}
    192  1.54       dsl 		m->h = max_lines - y - hadd;
    193  1.54       dsl 	}
    194   1.7      phil 
    195  1.48       dsl 	if (m->h < m->numopts + exithadd || m->mopt & MC_ALWAYS_SCROLL) {
    196  1.54       dsl 		/* We need to add the scroll text...
    197  1.54       dsl 		 * The used to be a check for MC_SCROLL here, but it is
    198  1.54       dsl 		 * fairly pointless - you just don't want the program
    199  1.54       dsl 		 * to exit on this sort of error.
    200  1.54       dsl 		 */
    201  1.54       dsl 		if (m->h < 3) {
    202   1.7      phil 			endwin();
    203  1.35       dsl 			(void)fprintf(stderr,
    204  1.54       dsl 				"Window too short (m->h %d, m->numopts %d, exithadd %d, y %d, max_lines %d, hadd %d) for menu \"%.30s\"\n",
    205  1.54       dsl 				m->h, m->numopts, exithadd, y, max_lines, hadd,
    206  1.40       dsl 				title);
    207   1.7      phil 			exit(1);
    208   1.7      phil 		}
    209  1.38       dsl 		hadd++;
    210  1.51       dsl 		m->h = MIN(m->h, max_lines - y - hadd);
    211  1.51       dsl 		i = strlen(scrolltext);
    212  1.51       dsl 		wmax = MAX(wmax, i);
    213  1.51       dsl 	}
    214   1.9      phil 
    215   1.1      phil 	/* Calculate w? */
    216  1.50       dsl 	if (w == 0) {
    217  1.49       dsl 		int l;
    218  1.49       dsl 		for (i = 0; i < m->numopts; i++) {
    219  1.49       dsl 			l = strlen(MSG_XLAT(m->opts[i].opt_name));
    220  1.49       dsl 			if (!(m->mopt & MC_NOSHORTCUT))
    221  1.49       dsl 				l += 3;
    222  1.49       dsl 			wmax = MAX(wmax, l);
    223  1.49       dsl 		}
    224  1.50       dsl 		w = wmax;
    225   1.1      phil 	}
    226   1.1      phil 
    227  1.17       cgd 	/* check and adjust for screen fit */
    228  1.50       dsl 	if (w + wadd > max_cols) {
    229  1.17       cgd 		endwin();
    230  1.35       dsl 		(void)fprintf(stderr,
    231  1.54       dsl 			"Screen too narrow (%d + %d > %d) for menu \"%s\"\n",
    232  1.54       dsl 				w, wadd, max_cols, title);
    233  1.17       cgd 		exit(1);
    234  1.17       cgd 
    235  1.17       cgd 	}
    236  1.54       dsl 
    237  1.50       dsl 	if (x == -1)
    238  1.50       dsl 		x = (max_cols - (w + wadd)) / 2;	/* center */
    239  1.50       dsl 	else if (x + w + wadd > max_cols)
    240  1.50       dsl 		x = max_cols - (w + wadd);	/* right align */
    241  1.17       cgd 
    242  1.54       dsl 	if (y == 0) {
    243  1.54       dsl 		/* Center - rather than top */
    244  1.54       dsl 		y = (max_lines - hadd - m->h) / 2;
    245  1.54       dsl 	}
    246  1.54       dsl 
    247   1.1      phil 	/* Get the windows. */
    248  1.50       dsl 	m->mw = newwin(m->h + hadd, w + wadd, y, x);
    249   1.1      phil 
    250   1.1      phil 	if (m->mw == NULL) {
    251   1.1      phil 		endwin();
    252  1.35       dsl 		(void)fprintf(stderr,
    253  1.42       dsl 			"Could not create window (%d + %d, %d + %d, %d, %d) for menu \"%s\"\n",
    254  1.50       dsl 			m->h, hadd, w, wadd, y, x, title);
    255   1.1      phil 		exit(1);
    256  1.26     perry 	}
    257  1.38       dsl 	keypad(m->mw, TRUE); /* enable multi-key assembling for win */
    258  1.26     perry 
    259  1.26     perry 	/* XXX is it even worth doing this right? */
    260  1.26     perry 	if (has_colors()) {
    261  1.26     perry 		wbkgd(m->mw, COLOR_PAIR(1));
    262  1.26     perry 		wattrset(m->mw, COLOR_PAIR(1));
    263  1.26     perry 	}
    264  1.54       dsl 
    265  1.54       dsl 	if (m->mopt & MC_SUBMENU) {
    266  1.54       dsl 		/* Keep a copy of what is on the screen under the window */
    267  1.54       dsl 		sv_mw = m->sv_mw = newwin(m->h + hadd, w + wadd, y, x);
    268  1.54       dsl 		if (m->sv_mw)
    269  1.54       dsl 			overwrite(curscr, m->sv_mw);
    270  1.54       dsl 	}
    271   1.1      phil }
    272   1.1      phil 
    273  1.23   hubertf static char
    274  1.43       dsl opt_ch(menudesc *m, int op_no)
    275  1.14      phil {
    276  1.14      phil 	char c;
    277  1.43       dsl 
    278  1.43       dsl 	if (op_no == m->numopts)
    279  1.43       dsl 		return 'x';
    280  1.43       dsl 
    281  1.14      phil 	if (op_no < 25) {
    282  1.14      phil 		c = 'a' + op_no;
    283  1.35       dsl 		if (c >= 'x')
    284  1.35       dsl 			c++;
    285  1.14      phil 	} else
    286  1.14      phil 		c = 'A' + op_no - 25;
    287  1.35       dsl 	return c;
    288  1.14      phil }
    289  1.14      phil 
    290  1.23   hubertf static void
    291  1.43       dsl draw_menu_line(menudesc *m, int opt, int cury, void *arg, const char *text)
    292  1.36       dsl {
    293  1.36       dsl 	int hasbox = m->mopt & MC_NOBOX ? 0 : 1;
    294  1.36       dsl 
    295  1.43       dsl 	if (m->cursel == opt) {
    296  1.36       dsl 		mvwaddstr(m->mw, cury, hasbox, ">");
    297  1.36       dsl 		wstandout(m->mw);
    298  1.36       dsl 	} else
    299  1.36       dsl 		mvwaddstr(m->mw, cury, hasbox, " ");
    300  1.36       dsl 	if (!(m->mopt & MC_NOSHORTCUT))
    301  1.43       dsl 		wprintw(m->mw, "%c: ", opt_ch(m, opt));
    302  1.43       dsl 
    303  1.43       dsl 	if (!text && m->draw_line)
    304  1.43       dsl 		m->draw_line(m, opt, arg);
    305  1.43       dsl 	else
    306  1.43       dsl 		waddstr(m->mw, MSG_XLAT(text));
    307  1.43       dsl 	if (m->cursel == opt)
    308  1.36       dsl 		wstandend(m->mw);
    309  1.36       dsl }
    310  1.36       dsl 
    311  1.36       dsl static void
    312  1.43       dsl draw_menu(menudesc *m, void *arg)
    313   1.1      phil {
    314  1.38       dsl 	int opt;
    315  1.36       dsl 	int hasbox, cury, maxy;
    316   1.1      phil 	int tadd;
    317  1.45       dsl 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
    318  1.50       dsl 	const char *tp, *ep;
    319   1.1      phil 
    320  1.50       dsl 	hasbox = (m->mopt & MC_NOBOX ? 0 : 1);
    321   1.1      phil 
    322   1.9      phil 	/* Clear the window */
    323  1.35       dsl 	wclear(m->mw);
    324   1.9      phil 
    325  1.50       dsl 	tadd = hasbox;
    326  1.40       dsl 	if (m->title) {
    327  1.50       dsl 		for (tp = MSG_XLAT(m->title); ; tp = ep + 1) {
    328  1.50       dsl 			ep = strchr(tp , '\n');
    329  1.50       dsl 			mvwaddnstr(m->mw, tadd++, hasbox + 1, tp,
    330  1.50       dsl 			    ep ? ep - tp : -1);
    331  1.50       dsl 			if (ep == NULL || *ep == 0)
    332  1.50       dsl 				break;
    333  1.50       dsl 		}
    334  1.50       dsl 		tadd++;
    335  1.50       dsl 	}
    336  1.50       dsl 
    337  1.50       dsl 	cury = tadd;
    338  1.50       dsl 	maxy = getmaxy(m->mw) - hasbox;
    339  1.51       dsl 	if (m->numopts + hasexit > m->h)
    340  1.51       dsl 		/* allow for scroll text */
    341  1.51       dsl 		maxy--;
    342   1.1      phil 
    343  1.39       dsl 	if (m->cursel == -1) {
    344  1.39       dsl 		m->cursel = m->numopts;
    345  1.39       dsl 		if (m->h <= m->numopts)
    346  1.39       dsl 			m->topline = m->numopts + 1 - m->h;
    347  1.39       dsl 	}
    348  1.39       dsl 
    349  1.45       dsl 	while (m->cursel >= m->topline + m->h)
    350  1.45       dsl 		m->topline = MIN(m->topline + m->h,
    351  1.45       dsl 				 m->numopts + hasexit - m->h);
    352  1.45       dsl 	while (m->cursel < m->topline)
    353  1.45       dsl 		m->topline = MAX(0, m->topline - m->h);
    354  1.45       dsl 
    355  1.38       dsl 	for (opt = m->topline; opt < m->numopts; opt++) {
    356  1.36       dsl 		if (cury >= maxy)
    357  1.36       dsl 			break;
    358  1.43       dsl 		draw_menu_line(m, opt, cury++, arg, m->opts[opt].opt_name);
    359   1.1      phil 	}
    360   1.7      phil 
    361   1.7      phil 	/* Add the exit option. */
    362  1.38       dsl 	if (!(m->mopt & MC_NOEXITOPT)) {
    363  1.38       dsl 		if (cury < maxy)
    364  1.43       dsl 			draw_menu_line(m, m->numopts, cury++, arg, m->exitstr);
    365  1.38       dsl 		else
    366  1.38       dsl 			opt = 0;
    367  1.38       dsl 	}
    368   1.7      phil 
    369   1.7      phil 	/* Add the scroll line */
    370  1.38       dsl 	if (opt != m->numopts || m->topline != 0)
    371  1.35       dsl 		mvwaddstr(m->mw, cury, hasbox, scrolltext);
    372   1.7      phil 
    373   1.7      phil 	/* Add the box. */
    374  1.14      phil 	if (!(m->mopt & MC_NOBOX))
    375  1.26     perry 		box(m->mw, 0, 0);
    376   1.1      phil 
    377  1.50       dsl 	wmove(m->mw, tadd + m->cursel - m->topline, hasbox);
    378  1.36       dsl 	wrefresh(m->mw);
    379   1.1      phil }
    380   1.1      phil 
    381  1.54       dsl 
    382  1.23   hubertf static void
    383  1.30  christos /*ARGSUSED*/
    384  1.52       dsl process_help(menudesc *m)
    385   1.5      phil {
    386  1.34       dsl 	const char *help = m->helpstr;
    387  1.54       dsl 	WINDOW *sv_curscr;
    388   1.7      phil 	int lineoff = 0;
    389   1.5      phil 	int curoff = 0;
    390   1.5      phil 	int again;
    391   1.7      phil 	int winin;
    392   1.5      phil 
    393   1.6      phil 	/* Is there help? */
    394   1.6      phil 	if (!help) {
    395   1.6      phil 		mbeep();
    396   1.6      phil 		return;
    397   1.6      phil 	}
    398  1.54       dsl 	sv_curscr = newwin(getmaxy(curscr), getmaxx(curscr), 0, 0);
    399  1.54       dsl 	if (!sv_curscr) {
    400  1.54       dsl 		mbeep();
    401  1.54       dsl 		return;
    402  1.54       dsl 	}
    403  1.54       dsl 	/* Save screen contents so we can restore before returning */
    404  1.54       dsl 	overwrite(curscr, sv_curscr);
    405  1.54       dsl 	touchwin(stdscr);
    406  1.54       dsl 
    407  1.40       dsl 	help = MSG_XLAT(help);
    408   1.6      phil 	/* Display the help information. */
    409   1.7      phil 	do {
    410   1.5      phil 		if (lineoff < curoff) {
    411  1.40       dsl 			help = MSG_XLAT(m->helpstr);
    412   1.5      phil 			curoff = 0;
    413   1.5      phil 		}
    414   1.5      phil 		while (*help && curoff < lineoff) {
    415   1.5      phil 			if (*help == '\n')
    416   1.5      phil 				curoff++;
    417   1.5      phil 			help++;
    418   1.5      phil 		}
    419   1.5      phil 
    420   1.5      phil 		wclear(stdscr);
    421  1.35       dsl 		mvwaddstr(stdscr, 0, 0,
    422   1.9      phil 			"Help: exit: x,  page up: u <, page down: d >");
    423  1.35       dsl 		mvwaddstr(stdscr, 2, 0, help);
    424  1.35       dsl 		wmove(stdscr, 1, 0);
    425  1.36       dsl 		wrefresh(stdscr);
    426   1.5      phil 
    427   1.5      phil 		do {
    428  1.29     blymn 			winin = wgetch(stdscr);
    429  1.29     blymn 			if (winin < KEY_MIN)
    430   1.9      phil 				winin = tolower(winin);
    431   1.5      phil 			again = 0;
    432   1.5      phil 			switch (winin) {
    433   1.5      phil 				case '<':
    434   1.5      phil 				case 'u':
    435  1.29     blymn 				case KEY_UP:
    436  1.29     blymn 				case KEY_LEFT:
    437  1.29     blymn 				case KEY_PPAGE:
    438   1.5      phil 					if (lineoff)
    439   1.5      phil 						lineoff -= max_lines - 2;
    440   1.7      phil 					else
    441   1.7      phil 						again = 1;
    442   1.5      phil 					break;
    443   1.5      phil 				case '>':
    444   1.5      phil 				case 'd':
    445  1.29     blymn 				case KEY_DOWN:
    446  1.29     blymn 				case KEY_RIGHT:
    447  1.29     blymn 				case KEY_NPAGE:
    448   1.5      phil 					if (*help)
    449   1.5      phil 						lineoff += max_lines - 2;
    450   1.7      phil 					else
    451   1.7      phil 						again = 1;
    452   1.5      phil 					break;
    453  1.10      phil 				case 'q':
    454  1.10      phil 					break;
    455   1.9      phil 				case 'x':
    456  1.10      phil 					winin = 'q';
    457   1.5      phil 					break;
    458   1.5      phil 				default:
    459   1.5      phil 					again = 1;
    460   1.5      phil 			}
    461   1.9      phil 			if (again)
    462   1.9      phil 				mbeep();
    463   1.5      phil 		} while (again);
    464   1.5      phil 	} while (winin != 'q');
    465  1.54       dsl 
    466  1.54       dsl 	/* Restore the original screen contents */
    467  1.54       dsl 	touchwin(sv_curscr);
    468  1.54       dsl 	wnoutrefresh(sv_curscr);
    469  1.54       dsl 	delwin(sv_curscr);
    470  1.54       dsl 
    471  1.54       dsl 	/* Some code thinks that wrefresh(stdout) is a good idea... */
    472  1.54       dsl 	wclear(stdscr);
    473   1.5      phil }
    474   1.5      phil 
    475  1.23   hubertf static void
    476  1.52       dsl process_req(menudesc *m, void *arg, int req)
    477   1.1      phil {
    478   1.1      phil 	int ch;
    479  1.35       dsl 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1);
    480   1.1      phil 
    481  1.35       dsl 	switch(req) {
    482  1.35       dsl 
    483  1.35       dsl 	case REQ_EXECUTE:
    484   1.1      phil 		return;
    485   1.7      phil 
    486  1.35       dsl 	case REQ_NEXT_ITEM:
    487  1.41       dsl 		ch = m->cursel;
    488  1.41       dsl 		for (;;) {
    489  1.41       dsl 			ch++;
    490  1.41       dsl 			if (ch >= m->numopts + hasexit) {
    491  1.41       dsl 				mbeep();
    492  1.41       dsl 				return;
    493  1.41       dsl 			}
    494  1.41       dsl 			if (hasexit && ch == m->numopts)
    495  1.41       dsl 				break;
    496  1.43       dsl 			if (!(m->opts[ch].opt_flags & OPT_IGNORE))
    497  1.41       dsl 				break;
    498  1.35       dsl 		}
    499  1.41       dsl 		m->cursel = ch;
    500  1.48       dsl 		if (m->cursel >= m->topline + m->h)
    501  1.41       dsl 			m->topline = m->cursel - m->h + 1;
    502  1.35       dsl 		break;
    503   1.7      phil 
    504  1.35       dsl 	case REQ_PREV_ITEM:
    505  1.41       dsl 		ch = m->cursel;
    506  1.41       dsl 		for (;;) {
    507  1.41       dsl 			if (ch <= 0) {
    508  1.41       dsl 				mbeep();
    509  1.41       dsl 				return;
    510  1.41       dsl 			}
    511  1.41       dsl 			ch--;
    512  1.43       dsl 			if (!(m->opts[ch].opt_flags & OPT_IGNORE))
    513  1.41       dsl 				break;
    514  1.35       dsl 		}
    515  1.41       dsl 		m->cursel = ch;
    516  1.35       dsl 		if (m->cursel < m->topline)
    517  1.41       dsl 			m->topline = m->cursel;
    518  1.35       dsl 		break;
    519   1.3      phil 
    520  1.39       dsl 	case REQ_HELP:
    521  1.52       dsl 		process_help(m);
    522  1.54       dsl 		break;
    523  1.39       dsl 
    524  1.35       dsl 	case REQ_REDISPLAY:
    525  1.54       dsl 		endwin();
    526  1.54       dsl 		doupdate();
    527  1.35       dsl 		break;
    528   1.7      phil 
    529  1.35       dsl 	case REQ_SCROLLUP:
    530  1.35       dsl 		if (m->cursel == 0) {
    531   1.7      phil 			mbeep();
    532  1.35       dsl 			return;
    533   1.7      phil 		}
    534  1.36       dsl 		m->topline = MAX(0, m->topline - m->h);
    535  1.36       dsl 		m->cursel = MAX(0, m->cursel - m->h);
    536  1.35       dsl 		wclear(m->mw);
    537  1.35       dsl 		break;
    538   1.7      phil 
    539  1.35       dsl 	case REQ_SCROLLDOWN:
    540  1.35       dsl 		if (m->cursel >= m->numopts + hasexit - 1) {
    541   1.7      phil 			mbeep();
    542  1.35       dsl 			return;
    543   1.7      phil 		}
    544  1.36       dsl 		m->topline = MIN(m->topline + m->h,
    545  1.38       dsl 				 MAX(m->numopts + hasexit - m->h, 0));
    546  1.36       dsl 		m->cursel = MIN(m->numopts + hasexit - 1, m->cursel + m->h);
    547  1.35       dsl 		wclear(m->mw);
    548  1.35       dsl 		break;
    549   1.7      phil 
    550  1.35       dsl 	default:
    551   1.9      phil 		ch = req;
    552   1.7      phil 		if (ch == 'x' && hasexit) {
    553   1.1      phil 			m->cursel = m->numopts;
    554  1.35       dsl 			break;
    555  1.35       dsl 		}
    556  1.35       dsl 		if (m->mopt & MC_NOSHORTCUT) {
    557  1.35       dsl 			mbeep();
    558  1.35       dsl 			return;
    559  1.35       dsl 		}
    560  1.35       dsl 		if (ch > 'z')
    561  1.35       dsl 			ch = 255;
    562  1.35       dsl 		if (ch >= 'a') {
    563  1.35       dsl 			if (ch > 'x')
    564  1.35       dsl 				ch--;
    565  1.35       dsl 			ch = ch - 'a';
    566  1.35       dsl 		} else
    567  1.35       dsl 			ch = 25 + ch - 'A';
    568  1.35       dsl 		if (ch < 0 || ch >= m->numopts) {
    569  1.41       dsl 			mbeep();
    570  1.41       dsl 			return;
    571  1.41       dsl 		}
    572  1.43       dsl 		if (m->opts[ch].opt_flags & OPT_IGNORE) {
    573  1.35       dsl 			mbeep();
    574  1.35       dsl 			return;
    575  1.35       dsl 		}
    576  1.35       dsl 		m->cursel = ch;
    577   1.1      phil 	}
    578   1.9      phil 
    579  1.43       dsl 	draw_menu(m, arg);
    580   1.1      phil }
    581   1.1      phil 
    582  1.23   hubertf int
    583  1.35       dsl menu_init(void)
    584  1.17       cgd {
    585  1.52       dsl 	int i;
    586  1.17       cgd 
    587  1.17       cgd 	if (__menu_init)
    588  1.17       cgd 		return 0;
    589  1.33       dsl 
    590  1.33       dsl #ifdef	USER_MENU_INIT
    591  1.33       dsl 	if (USER_MENU_INIT)
    592  1.33       dsl 		return 1;
    593  1.33       dsl #endif
    594  1.17       cgd 
    595  1.17       cgd 	if (initscr() == NULL)
    596  1.17       cgd 		return 1;
    597  1.17       cgd 
    598  1.17       cgd 	cbreak();
    599  1.17       cgd 	noecho();
    600  1.26     perry 
    601  1.26     perry 	/* XXX Should be configurable but it almost isn't worth it. */
    602  1.26     perry 	if (has_colors()) {
    603  1.26     perry 		start_color();
    604  1.26     perry 		init_pair(1, COLOR_WHITE, COLOR_BLUE);
    605  1.26     perry 		bkgd(COLOR_PAIR(1));
    606  1.26     perry 		attrset(COLOR_PAIR(1));
    607  1.26     perry 	}
    608  1.26     perry 
    609  1.17       cgd 	max_lines = getmaxy(stdscr);
    610  1.17       cgd 	max_cols = getmaxx(stdscr);
    611  1.29     blymn 	keypad(stdscr, TRUE);
    612  1.17       cgd #ifdef DYNAMIC_MENUS
    613  1.17       cgd 	num_menus = DYN_INIT_NUM;
    614  1.17       cgd 	while (num_menus < DYN_MENU_START)
    615  1.17       cgd 		num_menus *= 2;
    616  1.52       dsl 	menu_list = malloc(sizeof *menu_list * num_menus);
    617  1.52       dsl 	if (menu_list == NULL)
    618  1.17       cgd 		return 2;
    619  1.52       dsl 	(void)memset(menu_list, 0, sizeof *menu_list * num_menus);
    620  1.52       dsl 	for (i = 0; i < DYN_MENU_START; i++)
    621  1.52       dsl 		menu_list[i] = &menu_def[i];
    622  1.17       cgd #endif
    623  1.17       cgd 
    624  1.17       cgd 	__menu_init = 1;
    625  1.36       dsl 	return 0;
    626  1.17       cgd }
    627  1.17       cgd 
    628  1.23   hubertf void
    629  1.37       dsl process_menu(int num, void *arg)
    630   1.1      phil {
    631   1.1      phil 	int sel = 0;
    632  1.50       dsl 	int req;
    633  1.37       dsl 	menu_ent *opt;
    634   1.1      phil 
    635  1.39       dsl 	menudesc *m;
    636  1.14      phil 
    637  1.52       dsl 	m = &MENUS(num);
    638   1.1      phil 
    639   1.1      phil 	/* Initialize? */
    640  1.17       cgd 	if (menu_init()) {
    641  1.17       cgd 		__menu_initerror();
    642  1.17       cgd 		return;
    643   1.1      phil 	}
    644  1.17       cgd 
    645  1.39       dsl 	/* Default to select option 0 and display from 0 */
    646   1.7      phil 	m->topline = 0;
    647  1.39       dsl 	if ((m->mopt & (MC_DFLTEXIT | MC_NOEXITOPT)) == MC_DFLTEXIT)
    648  1.39       dsl 		m->cursel = -1;
    649  1.39       dsl 	else
    650  1.39       dsl 		m->cursel = 0;
    651   1.1      phil 
    652  1.50       dsl 	for (;;) {
    653  1.54       dsl 		if (isendwin())
    654  1.54       dsl 			/* I'm not sure this is needed with netbsd's curses */
    655  1.54       dsl 			doupdate();
    656   1.1      phil 		/* Process the display action */
    657  1.14      phil 		if (m->post_act)
    658  1.39       dsl 			(*m->post_act)(m, arg);
    659  1.47       dsl 		if (m->mw == NULL)
    660  1.47       dsl 			init_menu(m);
    661  1.43       dsl 		draw_menu(m, arg);
    662   1.1      phil 
    663  1.36       dsl 		while ((req = menucmd(m->mw)) != REQ_EXECUTE)
    664  1.52       dsl 			process_req(m, arg, req);
    665   1.1      phil 
    666   1.1      phil 		sel = m->cursel;
    667  1.38       dsl 		if (!(m->mopt & MC_NOCLEAR)) {
    668  1.38       dsl 			wclear(m->mw);
    669  1.54       dsl 			if (m->sv_mw)
    670  1.54       dsl 				overwrite(m->sv_mw, m->mw);
    671  1.54       dsl 			wnoutrefresh(m->mw);
    672  1.38       dsl 		}
    673   1.1      phil 
    674   1.1      phil 		/* Process the items */
    675  1.50       dsl 		if (sel >= m->numopts)
    676  1.50       dsl 			/* exit option */
    677  1.50       dsl 			break;
    678  1.50       dsl 
    679  1.50       dsl 		opt = &m->opts[sel];
    680  1.50       dsl 		if (opt->opt_flags & OPT_IGNORE)
    681  1.50       dsl 			continue;
    682  1.54       dsl 		if (opt->opt_flags & OPT_ENDWIN)
    683  1.50       dsl 			endwin();
    684  1.50       dsl 		if (opt->opt_action && (*opt->opt_action)(m, arg))
    685  1.50       dsl 			break;
    686  1.50       dsl 
    687  1.50       dsl 		if (opt->opt_menu != -1) {
    688  1.50       dsl 			if (!(opt->opt_flags & OPT_SUB)) {
    689  1.50       dsl 				num = opt->opt_menu;
    690  1.54       dsl 				wclear(m->mw);
    691  1.54       dsl 				if (m->sv_mw) {
    692  1.54       dsl 					overwrite(m->sv_mw, m->mw);
    693  1.54       dsl 					delwin(m->sv_mw);
    694  1.54       dsl 					m->sv_mw = NULL;
    695  1.54       dsl 				}
    696  1.54       dsl 				wnoutrefresh(m->mw);
    697  1.50       dsl 				delwin(m->mw);
    698  1.50       dsl 				m->mw = NULL;
    699  1.52       dsl 				m = &MENUS(num);
    700  1.43       dsl 				continue;
    701  1.14      phil 			}
    702  1.50       dsl 			process_menu(opt->opt_menu, arg);
    703  1.50       dsl 		}
    704  1.50       dsl 		if (opt->opt_flags & OPT_EXIT)
    705  1.50       dsl 			break;
    706   1.1      phil 	}
    707   1.1      phil 
    708  1.38       dsl 	if (m->mopt & MC_NOCLEAR) {
    709  1.38       dsl 		wclear(m->mw);
    710  1.54       dsl 		if (m->sv_mw)
    711  1.54       dsl 			overwrite(m->sv_mw, m->mw);
    712  1.54       dsl 		wnoutrefresh(m->mw);
    713  1.38       dsl 	}
    714  1.38       dsl 
    715   1.1      phil 	/* Process the exit action */
    716  1.14      phil 	if (m->exit_act)
    717  1.39       dsl 		(*m->exit_act)(m, arg);
    718  1.50       dsl 	delwin(m->mw);
    719  1.50       dsl 	m->mw = NULL;
    720  1.54       dsl 	if (m->sv_mw) {
    721  1.54       dsl 		delwin(m->sv_mw);
    722  1.54       dsl 		m->sv_mw = NULL;
    723  1.54       dsl 	}
    724   1.1      phil }
    725  1.13      phil 
    726  1.52       dsl 
    727  1.52       dsl void
    728  1.52       dsl set_menu_numopts(int menu, int numopts)
    729  1.52       dsl {
    730  1.52       dsl 
    731  1.52       dsl 	MENUS(menu).numopts = numopts;
    732  1.52       dsl }
    733  1.52       dsl 
    734  1.13      phil /* Control L is end of standard routines, remaining only for dynamic. */
    736  1.13      phil 
    737  1.13      phil /* Beginning of routines for dynamic menus. */
    738  1.24      phil 
    739  1.35       dsl static int
    740  1.14      phil double_menus(void)
    741  1.52       dsl {
    742  1.52       dsl 	menudesc **temp;
    743  1.14      phil 	int sz = sizeof *menu_list * num_menus;
    744  1.52       dsl 
    745  1.14      phil 	temp  = realloc(menu_list, sz * 2);
    746  1.14      phil 	if (temp == NULL)
    747  1.39       dsl 		return 0;
    748  1.52       dsl 	(void)memset(temp + num_menus, 0, sz);
    749  1.14      phil 	menu_list = temp;
    750  1.14      phil 	num_menus *= 2;
    751  1.14      phil 
    752  1.14      phil 	return 1;
    753  1.14      phil }
    754  1.23   hubertf 
    755  1.38       dsl int
    756  1.24      phil new_menu(const char *title, menu_ent *opts, int numopts,
    757  1.39       dsl 	int x, int y, int h, int w, int mopt,
    758  1.43       dsl 	void (*post_act)(menudesc *, void *),
    759  1.39       dsl 	void (*draw_line)(menudesc *, int, void *),
    760  1.44       dsl 	void (*exit_act)(menudesc *, void *),
    761  1.14      phil 	const char *help, const char *exit_str)
    762  1.14      phil {
    763  1.39       dsl 	int ix;
    764   1.7      phil 	menudesc *m;
    765  1.39       dsl 
    766  1.39       dsl 	/* Find free menu entry. */
    767  1.46  takemura 	for (ix = DYN_MENU_START; ; ix++) {
    768  1.14      phil 		if (ix >= num_menus && !double_menus())
    769  1.52       dsl 			return -1;
    770  1.52       dsl 		m = menu_list[ix];
    771  1.52       dsl 		if (m == NULL) {
    772  1.52       dsl 			m = calloc(sizeof *m, 1);
    773  1.52       dsl 			if (m == NULL)
    774  1.52       dsl 				return -1;
    775  1.52       dsl 			menu_list[ix] = m;
    776  1.52       dsl 			break;
    777  1.52       dsl 		}
    778  1.39       dsl 		if (!(m->mopt & MC_VALID))
    779  1.39       dsl 			break;
    780  1.14      phil 	}
    781  1.14      phil 
    782  1.40       dsl 	/* Set Entries */
    783  1.39       dsl 	m->title = title;
    784  1.39       dsl 	m->opts = opts;
    785  1.39       dsl 	m->numopts = numopts;
    786  1.39       dsl 	m->x = x;
    787  1.39       dsl 	m->y = y;
    788  1.39       dsl 	m->h = h;
    789  1.39       dsl 	m->w = w;
    790  1.39       dsl 	m->mopt = mopt | MC_VALID;
    791  1.43       dsl 	m->post_act = post_act;
    792  1.39       dsl 	m->draw_line = draw_line;
    793  1.39       dsl 	m->exit_act = exit_act;
    794  1.44       dsl 	m->helpstr  = help;
    795  1.14      phil 	m->exitstr  = exit_str ? exit_str : "Exit";
    796  1.14      phil 
    797  1.14      phil 	return ix;
    798  1.14      phil }
    799  1.23   hubertf 
    800  1.35       dsl void
    801  1.14      phil free_menu(int menu_no)
    802  1.39       dsl {
    803  1.39       dsl 	menudesc *m;
    804  1.39       dsl 
    805  1.39       dsl 	if (menu_no < 0 || menu_no >= num_menus)
    806  1.39       dsl 		return;
    807  1.52       dsl 
    808  1.50       dsl 	m = menu_list[menu_no];
    809  1.39       dsl 	if (!(m->mopt & MC_VALID))
    810  1.39       dsl 		return;
    811  1.39       dsl 	if (m->mw != NULL)
    812  1.39       dsl 		delwin(m->mw);
    813  1.39       dsl 	memset(m, 0, sizeof *m);
    814                 }
    815