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