Home | History | Annotate | Line # | Download | only in menuc
menu_sys.def revision 1.27
      1  1.27  christos /*	$NetBSD: menu_sys.def,v 1.27 2001/12/06 16:38:30 christos 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 /* Multiple key support */
     53   1.9      phil #define KEYSEQ_FIRST       256
     54   1.9      phil #define KEYSEQ_DOWN_ARROW  256
     55   1.9      phil #define KEYSEQ_UP_ARROW    257
     56   1.9      phil #define KEYSEQ_LEFT_ARROW  258
     57   1.9      phil #define KEYSEQ_RIGHT_ARROW 259
     58   1.9      phil #define KEYSEQ_PAGE_DOWN   260
     59   1.9      phil #define KEYSEQ_PAGE_UP     261
     60   1.9      phil 
     61   1.9      phil struct keyseq {
     62   1.9      phil 	char *termcap_name;
     63   1.9      phil 	char *chars;
     64   1.9      phil 	int  numchars;
     65   1.9      phil 	int  keyseq_val;
     66   1.9      phil 	struct keyseq *next;
     67   1.9      phil };
     68   1.9      phil 
     69  1.10      phil /* keypad and other definitions */
     70   1.9      phil struct keyseq _mc_key_seq[] = {
     71  1.10      phil 	/* Cludge for xterm ... */
     72  1.10      phil 	{  NULL, "\033[B", 0, KEYSEQ_DOWN_ARROW, NULL },
     73  1.10      phil 	{  NULL, "\033[D", 0, KEYSEQ_LEFT_ARROW, NULL },
     74  1.10      phil 	{  NULL, "\033[C", 0, KEYSEQ_RIGHT_ARROW, NULL },
     75  1.10      phil 	{  NULL, "\033[A", 0, KEYSEQ_UP_ARROW, NULL },
     76  1.10      phil 	/* Termcap defined */
     77   1.9      phil 	{ "kd", NULL, 0, KEYSEQ_DOWN_ARROW, NULL },
     78   1.9      phil 	{ "kl", NULL, 0, KEYSEQ_LEFT_ARROW, NULL },
     79   1.9      phil 	{ "kr", NULL, 0, KEYSEQ_RIGHT_ARROW, NULL },
     80   1.9      phil 	{ "ku", NULL, 0, KEYSEQ_UP_ARROW, NULL },
     81   1.9      phil 	{ "kf", NULL, 0, KEYSEQ_PAGE_DOWN, NULL },  /* scroll forward */
     82   1.9      phil 	{ "kN", NULL, 0, KEYSEQ_PAGE_DOWN, NULL },  /* next page */
     83   1.9      phil 	{ "kP", NULL, 0, KEYSEQ_PAGE_UP, NULL },    /* scroll backward */
     84   1.9      phil 	{ "kR", NULL, 0, KEYSEQ_PAGE_UP, NULL },    /* prev page */
     85   1.9      phil 	/* other definitions */
     86   1.9      phil 	{ NULL, "\033v", 0, KEYSEQ_PAGE_UP, NULL },   /* ESC-v */
     87   1.9      phil 	{ NULL, "\026", 0, KEYSEQ_PAGE_DOWN, NULL },  /* CTL-v */
     88   1.9      phil };
     89   1.9      phil 
     90   1.9      phil int _mc_num_key_seq = sizeof(_mc_key_seq) / sizeof(struct keyseq);
     91   1.9      phil struct keyseq *pad_list = NULL;
     92   1.9      phil static char str_area [512];
     93  1.27  christos static int str_size = sizeof(str_area);
     94   1.9      phil static char *str_ptr = str_area;
     95   1.1      phil 
     96   1.9      phil /* Macros */
     97   1.1      phil #define MAX(x,y) ((x)>(y)?(x):(y))
     98   1.7      phil #define MIN(x,y) ((x)<(y)?(x):(y))
     99   1.1      phil 
    100   1.1      phil /* Initialization state. */
    101   1.1      phil static int __menu_init = 0;
    102   1.1      phil int __m_endwin = 0;
    103  1.17       cgd static int max_lines = 0, max_cols = 0;
    104   1.7      phil static char *scrolltext = " <: page up, >: page down";
    105   1.1      phil 
    106  1.14      phil static menudesc *menus = menu_def;
    107  1.14      phil 
    108  1.14      phil #ifdef DYNAMIC_MENUS
    109  1.14      phil static int num_menus  = 0;
    110  1.14      phil static int num_avail  = 0;
    111  1.14      phil #define DYN_INIT_NUM 32
    112  1.14      phil #endif
    113  1.14      phil 
    114   1.1      phil /* prototypes for in here! */
    115   1.9      phil static void ins_keyseq (struct keyseq **seq, struct keyseq *ins);
    116   1.9      phil static void init_keyseq (void);
    117   1.1      phil static void init_menu (struct menudesc *m);
    118  1.14      phil static char opt_ch (int op_no);
    119   1.1      phil static void post_menu (struct menudesc *m);
    120   1.5      phil static void process_help (struct menudesc *m, int num);
    121   1.4      phil static void process_req (struct menudesc *m, int num, int req);
    122   1.1      phil static int menucmd (WINDOW *w);
    123   1.1      phil 
    124   1.1      phil #ifndef NULL
    125   1.1      phil #define NULL (void *)0
    126   1.1      phil #endif
    127   1.1      phil 
    128   1.1      phil /* menu system processing routines */
    129  1.27  christos #define mbeep() (void)fputc('\a', stderr)
    130   1.1      phil 
    131  1.23   hubertf static void
    132  1.23   hubertf ins_keyseq (struct keyseq **seq, struct keyseq *ins)
    133   1.9      phil {
    134   1.9      phil 	if (*seq == NULL) {
    135   1.9      phil 		ins->next = NULL;
    136   1.9      phil 		*seq = ins;
    137   1.9      phil 	} else if (ins->numchars <= (*seq)->numchars) {
    138   1.9      phil 		ins->next = *seq;
    139   1.9      phil 		*seq = ins;
    140   1.9      phil 	} else
    141   1.9      phil 		ins_keyseq (&(*seq)->next, ins);
    142   1.9      phil }
    143   1.9      phil 
    144  1.23   hubertf static void
    145  1.27  christos init_keyseq(void)
    146   1.9      phil {
    147  1.22   thorpej 	/*
    148  1.22   thorpej 	 * XXX XXX XXX THIS SHOULD BE NUKED FROM ORBIT!  DO THIS
    149  1.22   thorpej 	 * XXX XXX XXX WITH NORMAL CURSES FACILITIES!
    150  1.22   thorpej 	 */
    151  1.27  christos 	struct tinfo *ti;
    152  1.27  christos 	char *term = getenv("TERM");
    153  1.27  christos 	int i;
    154  1.22   thorpej 
    155  1.27  christos 	if (term == NULL || term[0] == '\0')
    156  1.27  christos 		return;
    157  1.27  christos 
    158  1.27  christos 	if (t_getent(&ti, term) < 0)
    159  1.27  christos 		return;
    160  1.27  christos 
    161  1.27  christos 	for (i = 0; i < _mc_num_key_seq; i++) {
    162  1.27  christos 		if (_mc_key_seq[i].termcap_name == NULL)
    163  1.27  christos 			continue;
    164  1.27  christos 
    165  1.27  christos 		_mc_key_seq[i].chars = t_getstr(ti, _mc_key_seq[i].termcap_name,
    166  1.27  christos 		     &str_ptr, &str_size);
    167  1.22   thorpej 
    168   1.9      phil 		if (_mc_key_seq[i].chars != NULL &&
    169   1.9      phil 		    (_mc_key_seq[i].numchars = strlen(_mc_key_seq[i].chars))
    170   1.9      phil 		    > 0)
    171  1.27  christos 			ins_keyseq(&pad_list, &_mc_key_seq[i]);
    172   1.9      phil 	}
    173  1.27  christos 	t_freent(ti);
    174   1.9      phil }
    175   1.9      phil 
    176  1.23   hubertf static int
    177  1.23   hubertf mgetch(WINDOW *w)
    178   1.1      phil {
    179   1.1      phil 	static char buf[20];
    180   1.1      phil 	static int  num = 0;
    181   1.9      phil 	struct keyseq *list = pad_list;
    182   1.1      phil 	int i, ret;
    183   1.1      phil 
    184   1.9      phil 	/* key pad processing */
    185   1.9      phil 	while (list) {
    186   1.9      phil 		for (i=0; i< list->numchars; i++) {
    187   1.9      phil 			if (i >= num)
    188   1.9      phil 				buf[num++] = wgetch(w);
    189   1.9      phil 			if (buf[i] != list->chars[i])
    190   1.9      phil 				break;
    191   1.9      phil 		}
    192   1.9      phil 		if (i == list->numchars) {
    193   1.9      phil 			num = 0;
    194   1.9      phil 			return list->keyseq_val;
    195   1.9      phil 		}
    196   1.9      phil 		list = list->next;
    197   1.9      phil 	}
    198   1.1      phil 
    199   1.1      phil 	ret = buf[0];
    200   1.1      phil 	for (i = 0; i < strlen(buf); i++)
    201   1.1      phil 		buf[i] = buf[i+1];
    202   1.1      phil 	num--;
    203   1.1      phil 	return ret;
    204   1.1      phil }
    205   1.1      phil 
    206  1.23   hubertf static int
    207  1.23   hubertf menucmd (WINDOW *w)
    208   1.1      phil {
    209   1.1      phil 	int ch;
    210   1.1      phil 
    211   1.1      phil 	while (TRUE) {
    212   1.1      phil 		ch = mgetch(w);
    213   1.1      phil 
    214   1.1      phil 		switch (ch) {
    215   1.1      phil 		case '\n':
    216   1.1      phil 			return REQ_EXECUTE;
    217  1.11      phil 		case '\016':  /* Contnrol-P */
    218   1.9      phil 		case KEYSEQ_DOWN_ARROW:
    219   1.1      phil 			return REQ_NEXT_ITEM;
    220  1.11      phil 		case '\020':  /* Control-N */
    221   1.9      phil 		case KEYSEQ_UP_ARROW:
    222   1.1      phil 			return REQ_PREV_ITEM;
    223  1.11      phil 		case '\014':  /* Control-L */
    224   1.3      phil 		        return REQ_REDISPLAY;
    225   1.9      phil 		case '<':
    226  1.11      phil 		case '\010':  /* Control-H (backspace) */
    227   1.9      phil 		case KEYSEQ_PAGE_UP:
    228   1.9      phil 			return REQ_SCROLLUP;
    229   1.9      phil 		case '>':
    230  1.11      phil 		case ' ':
    231   1.9      phil 		case KEYSEQ_PAGE_DOWN:
    232   1.9      phil 			return REQ_SCROLLDOWN;
    233   1.9      phil 		case '?':
    234   1.9      phil 			return REQ_HELP;
    235   1.1      phil 		}
    236   1.1      phil 
    237   1.9      phil 		if (isalpha(ch))
    238   1.1      phil 			return (ch);
    239   1.1      phil 
    240   1.1      phil 		mbeep();
    241   1.1      phil 		wrefresh(w);
    242   1.1      phil 	}
    243   1.1      phil }
    244   1.1      phil 
    245  1.23   hubertf static void
    246  1.23   hubertf init_menu (struct menudesc *m)
    247   1.1      phil {
    248  1.18       cgd 	int wmax;
    249  1.18       cgd 	int hadd, wadd, exithadd;
    250  1.14      phil 	int i;
    251   1.1      phil 
    252  1.18       cgd 	hadd = ((m->mopt & MC_NOBOX) ? 0 : 2);
    253  1.18       cgd 	wadd = ((m->mopt & MC_NOBOX) ? 2 : 4);
    254  1.18       cgd 
    255  1.18       cgd 	hadd += strlen(m->title) != 0 ? 2 : 0;
    256  1.18       cgd 	exithadd = ((m->mopt & MC_NOEXITOPT) ? 0 : 1);
    257  1.18       cgd 
    258  1.18       cgd 	wmax = strlen(m->title);
    259   1.1      phil 
    260   1.7      phil 	/* Calculate h? h == number of visible options. */
    261   1.1      phil 	if (m->h == 0) {
    262  1.18       cgd 		m->h = m->numopts + exithadd;
    263  1.18       cgd 		if (m->h + m->y + hadd >= max_lines && (m->mopt & MC_SCROLL))
    264  1.18       cgd 			m->h = max_lines - m->y - hadd ;
    265   1.1      phil 	}
    266   1.7      phil 
    267   1.7      phil 	/* Check window heights and set scrolling */
    268  1.18       cgd 	if (m->h < m->numopts + exithadd) {
    269  1.14      phil 		if (!(m->mopt & MC_SCROLL) || m->h < 3) {
    270   1.7      phil 			endwin();
    271   1.7      phil 			(void) fprintf (stderr,
    272  1.17       cgd 				"Window too short for menu \"%s\"\n",
    273   1.9      phil 				m->title);
    274   1.7      phil 			exit(1);
    275   1.7      phil 		}
    276   1.7      phil 	} else
    277  1.14      phil 		m->mopt &= ~MC_SCROLL;
    278   1.1      phil 
    279   1.9      phil 	/* check for screen fit */
    280  1.18       cgd 	if (m->y + m->h + hadd > max_lines) {
    281   1.9      phil 		endwin();
    282   1.9      phil 		(void) fprintf (stderr,
    283  1.17       cgd 			"Screen too short for menu \"%s\"\n", m->title);
    284   1.9      phil 		exit(1);
    285   1.9      phil 
    286   1.9      phil 	}
    287   1.9      phil 
    288   1.1      phil 	/* Calculate w? */
    289   1.1      phil 	if (m->w == 0) {
    290  1.14      phil 		if (m->mopt & MC_SCROLL)
    291  1.18       cgd 			wmax = MAX(wmax,strlen(scrolltext));
    292  1.14      phil 		for (i=0; i < m->numopts; i++ )
    293  1.18       cgd 			wmax = MAX(wmax,strlen(m->opts[i].opt_name)+3);
    294  1.18       cgd 		m->w = wmax;
    295   1.1      phil 	}
    296   1.1      phil 
    297  1.17       cgd 	/* check and adjust for screen fit */
    298  1.18       cgd 	if (m->w + wadd > max_cols) {
    299  1.17       cgd 		endwin();
    300  1.17       cgd 		(void) fprintf (stderr,
    301  1.17       cgd 			"Screen too narrow for menu \"%s\"\n", m->title);
    302  1.17       cgd 		exit(1);
    303  1.17       cgd 
    304  1.17       cgd 	}
    305  1.20       cgd 	if (m->x == -1)
    306  1.20       cgd 		m->x = (max_cols - (m->w + wadd)) / 2;	/* center */
    307  1.20       cgd 	else if (m->x + m->w + wadd > max_cols)
    308  1.18       cgd 		m->x = max_cols - (m->w + wadd);
    309  1.17       cgd 
    310   1.1      phil 	/* Get the windows. */
    311  1.18       cgd 	m->mw = newwin(m->h+hadd, m->w+wadd, m->y, m->x);
    312   1.1      phil 
    313   1.1      phil 	if (m->mw == NULL) {
    314   1.1      phil 		endwin();
    315   1.1      phil 		(void) fprintf (stderr,
    316  1.18       cgd 			"Could not create window for menu \"%s\"\n", m->title);
    317   1.1      phil 		exit(1);
    318  1.26     perry 	}
    319  1.26     perry 
    320  1.26     perry 	/* XXX is it even worth doing this right? */
    321  1.26     perry 	if (has_colors()) {
    322  1.26     perry 		wbkgd(m->mw, COLOR_PAIR(1));
    323  1.26     perry 		wattrset(m->mw, COLOR_PAIR(1));
    324  1.26     perry 	}
    325   1.1      phil }
    326   1.1      phil 
    327  1.23   hubertf static char
    328  1.23   hubertf opt_ch (int op_no)
    329  1.14      phil {
    330  1.14      phil 	char c;
    331  1.14      phil 	if (op_no < 25) {
    332  1.14      phil 		c = 'a' + op_no;
    333  1.14      phil 		if (c >= 'x') c++;
    334  1.14      phil 	} else
    335  1.14      phil 		c = 'A' + op_no - 25;
    336  1.14      phil 	return (char) c;
    337  1.14      phil }
    338  1.14      phil 
    339  1.23   hubertf static void
    340  1.23   hubertf post_menu (struct menudesc *m)
    341   1.1      phil {
    342   1.1      phil 	int i;
    343   1.7      phil 	int hasbox, cury, maxy, selrow, lastopt;
    344   1.1      phil 	int tadd;
    345  1.14      phil 	char optstr[5];
    346   1.1      phil 
    347  1.14      phil 	if (m->mopt & MC_NOBOX) {
    348   1.1      phil 		cury = 0;
    349   1.7      phil 		maxy = m->h;
    350   1.1      phil 		hasbox = 0;
    351   1.1      phil 	} else {
    352   1.1      phil 		cury = 1;
    353   1.7      phil 		maxy = m->h+1;
    354   1.1      phil 		hasbox = 1;
    355   1.1      phil 	}
    356   1.1      phil 
    357   1.9      phil 	/* Clear the window */
    358   1.9      phil 	wclear (m->mw);
    359   1.9      phil 
    360   1.1      phil 	tadd = strlen(m->title) ? 2 : 0;
    361   1.1      phil 
    362   1.1      phil 	if (tadd) {
    363  1.18       cgd 		mvwaddstr(m->mw, cury, cury, " ");
    364  1.18       cgd 		mvwaddstr(m->mw, cury, cury + 1, m->title);
    365   1.1      phil 		cury += 2;
    366   1.7      phil 		maxy += 2;
    367   1.1      phil 	}
    368   1.1      phil 
    369   1.7      phil 	/* Set defaults, calculate lastopt. */
    370   1.7      phil 	selrow = -1;
    371  1.14      phil 	if (m->mopt & MC_SCROLL) {
    372   1.7      phil 		lastopt = MIN(m->numopts, m->topline+m->h-1);
    373   1.7      phil 		maxy -= 1;
    374   1.7      phil 	} else
    375   1.7      phil 		lastopt = m->numopts;
    376   1.7      phil 
    377   1.7      phil 	for (i=m->topline; i<lastopt; i++, cury++) {
    378   1.1      phil 		if (m->cursel == i) {
    379   1.1      phil 			mvwaddstr (m->mw, cury, hasbox, ">");
    380   1.1      phil 			wstandout(m->mw);
    381   1.7      phil 			selrow = cury;
    382   1.1      phil 		} else
    383   1.1      phil 			mvwaddstr (m->mw, cury, hasbox, " ");
    384  1.24      phil 		if (!(m->mopt & MC_NOSHORTCUT)) {
    385  1.23   hubertf 			(void) sprintf (optstr, "%c: ", opt_ch(i));
    386  1.24      phil 		    	waddstr (m->mw, optstr);
    387  1.23   hubertf 		}
    388  1.14      phil 		waddstr (m->mw, m->opts[i].opt_name);
    389   1.1      phil 		if (m->cursel == i)
    390   1.1      phil 			wstandend(m->mw);
    391   1.1      phil 	}
    392   1.7      phil 
    393   1.7      phil 	/* Add the exit option. */
    394  1.14      phil 	if (!(m->mopt & MC_NOEXITOPT) && cury < maxy) {
    395   1.7      phil 		if (m->cursel >= m->numopts) {
    396   1.7      phil 			mvwaddstr (m->mw, cury, hasbox, ">");
    397   1.7      phil 			wstandout(m->mw);
    398   1.7      phil 			selrow = cury;
    399   1.7      phil 		} else
    400   1.7      phil 			mvwaddstr (m->mw, cury, hasbox, " ");
    401  1.24      phil 		if (!(m->mopt & MC_NOSHORTCUT))
    402  1.23   hubertf 			waddstr (m->mw, "x: ");
    403  1.19       cgd 		waddstr (m->mw, m->exitstr);
    404   1.7      phil 		if (m->cursel >= m->numopts)
    405   1.7      phil 			wstandend(m->mw);
    406   1.7      phil 		cury++;
    407   1.7      phil 	}
    408   1.7      phil 
    409   1.7      phil 	/* Add the scroll line */
    410  1.14      phil 	if (m->mopt & MC_SCROLL) {
    411   1.7      phil 		mvwaddstr (m->mw, cury, hasbox, scrolltext);
    412   1.7      phil 		if (selrow < 0)
    413   1.7      phil 			selrow = cury;
    414   1.7      phil 	}
    415   1.7      phil 
    416   1.7      phil 	/* Add the box. */
    417  1.14      phil 	if (!(m->mopt & MC_NOBOX))
    418  1.26     perry 		box(m->mw, 0, 0);
    419   1.1      phil 
    420   1.7      phil 	wmove(m->mw, selrow, hasbox);
    421   1.1      phil }
    422   1.1      phil 
    423  1.23   hubertf static void
    424  1.23   hubertf process_help (struct menudesc *m, int num)
    425   1.5      phil {
    426   1.5      phil 	char *help = m->helpstr;
    427   1.7      phil 	int lineoff = 0;
    428   1.5      phil 	int curoff = 0;
    429   1.5      phil 	int again;
    430   1.7      phil 	int winin;
    431   1.5      phil 
    432   1.6      phil 	/* Is there help? */
    433   1.6      phil 	if (!help) {
    434   1.6      phil 		mbeep();
    435   1.6      phil 		return;
    436   1.6      phil 	}
    437   1.6      phil 
    438   1.6      phil 	/* Display the help information. */
    439   1.7      phil 	do {
    440   1.5      phil 		if (lineoff < curoff) {
    441   1.5      phil 			help = m->helpstr;
    442   1.5      phil 			curoff = 0;
    443   1.5      phil 		}
    444   1.5      phil 		while (*help && curoff < lineoff) {
    445   1.5      phil 			if (*help == '\n')
    446   1.5      phil 				curoff++;
    447   1.5      phil 			help++;
    448   1.5      phil 		}
    449   1.5      phil 
    450   1.5      phil 		wclear(stdscr);
    451   1.5      phil 		mvwaddstr (stdscr, 0, 0,
    452   1.9      phil 			"Help: exit: x,  page up: u <, page down: d >");
    453   1.5      phil 		mvwaddstr (stdscr, 2, 0, help);
    454   1.5      phil 		wmove (stdscr, 1, 0);
    455   1.5      phil 	  	wrefresh(stdscr);
    456   1.5      phil 
    457   1.5      phil 		do {
    458   1.5      phil 			winin = mgetch(stdscr);
    459   1.9      phil 			if (winin < KEYSEQ_FIRST)
    460   1.9      phil 				winin = tolower(winin);
    461   1.5      phil 			again = 0;
    462   1.5      phil 			switch (winin) {
    463   1.5      phil 				case '<':
    464   1.5      phil 				case 'u':
    465   1.9      phil 				case KEYSEQ_UP_ARROW:
    466   1.9      phil 				case KEYSEQ_LEFT_ARROW:
    467   1.9      phil 				case KEYSEQ_PAGE_UP:
    468   1.5      phil 					if (lineoff)
    469   1.5      phil 						lineoff -= max_lines - 2;
    470   1.7      phil 					else
    471   1.7      phil 						again = 1;
    472   1.5      phil 					break;
    473   1.5      phil 				case '>':
    474   1.5      phil 				case 'd':
    475   1.9      phil 				case KEYSEQ_DOWN_ARROW:
    476   1.9      phil 				case KEYSEQ_RIGHT_ARROW:
    477   1.9      phil 				case KEYSEQ_PAGE_DOWN:
    478   1.5      phil 					if (*help)
    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.10      phil 				case 'q':
    484  1.10      phil 					break;
    485   1.9      phil 				case 'x':
    486  1.10      phil 					winin = 'q';
    487   1.5      phil 					break;
    488   1.5      phil 				default:
    489   1.5      phil 					again = 1;
    490   1.5      phil 			}
    491   1.9      phil 			if (again)
    492   1.9      phil 				mbeep();
    493   1.5      phil 		} while (again);
    494   1.5      phil 	} while (winin != 'q');
    495   1.5      phil 
    496   1.5      phil 	/* Restore current menu */
    497   1.5      phil 	wclear(stdscr);
    498   1.5      phil 	wrefresh(stdscr);
    499  1.14      phil 	if (m->post_act)
    500  1.14      phil 		(*m->post_act)();
    501   1.5      phil }
    502   1.5      phil 
    503  1.23   hubertf static void
    504  1.23   hubertf process_req (struct menudesc *m, int num, int req)
    505   1.1      phil {
    506   1.1      phil 	int ch;
    507  1.14      phil 	int hasexit = (m->mopt & MC_NOEXITOPT ? 0 : 1 );
    508   1.7      phil 	int refresh = 0;
    509   1.9      phil 	int scroll_sel = 0;
    510   1.1      phil 
    511   1.1      phil 	if (req == REQ_EXECUTE)
    512   1.1      phil 		return;
    513   1.7      phil 
    514   1.1      phil 	else if (req == REQ_NEXT_ITEM) {
    515   1.7      phil 		if (m->cursel < m->numopts + hasexit - 1) {
    516   1.1      phil 			m->cursel++;
    517   1.9      phil 			scroll_sel = 1;
    518   1.7      phil 			refresh = 1;
    519  1.14      phil 			if (m->mopt & MC_SCROLL &&
    520  1.12      phil 			    m->cursel >= m->topline + m->h -1 )
    521  1.11      phil 				m->topline += 1;
    522   1.7      phil 		} else
    523   1.1      phil 			mbeep();
    524   1.7      phil 
    525   1.1      phil 	} else if (req == REQ_PREV_ITEM) {
    526   1.7      phil 		if (m->cursel > 0) {
    527   1.1      phil 			m->cursel--;
    528   1.9      phil 			scroll_sel = 1;
    529   1.7      phil 			refresh = 1;
    530  1.11      phil 			if (m->cursel < m->topline )
    531  1.11      phil 				m->topline -= 1;
    532   1.7      phil 		} else
    533   1.1      phil 			mbeep();
    534   1.3      phil 
    535   1.3      phil 	} else if (req == REQ_REDISPLAY) {
    536   1.3      phil 		wclear(stdscr);
    537   1.3      phil 		wrefresh(stdscr);
    538  1.14      phil 		if (m->post_act)
    539  1.14      phil 			(*m->post_act)();
    540   1.7      phil 		refresh = 1;
    541   1.3      phil 
    542   1.9      phil 	} else if (req == REQ_HELP) {
    543   1.5      phil 		process_help (m, num);
    544   1.7      phil 		refresh = 1;
    545   1.7      phil 
    546   1.9      phil 	} else if (req == REQ_SCROLLUP) {
    547  1.14      phil 		if (!(m->mopt & MC_SCROLL))
    548   1.9      phil 			mbeep();
    549   1.9      phil 		else if (m->topline == 0)
    550   1.7      phil 			mbeep();
    551   1.7      phil 		else {
    552  1.14      phil 			m->topline = MAX(0,m->topline-m->h+1);
    553  1.23   hubertf 			m->cursel = MAX(0, m->cursel-m->h+1);
    554   1.7      phil 			wclear (m->mw);
    555   1.7      phil 			refresh = 1;
    556   1.7      phil 		}
    557   1.7      phil 
    558   1.9      phil 	} else if (req == REQ_SCROLLDOWN) {
    559  1.14      phil 		if (!(m->mopt & MC_SCROLL))
    560   1.9      phil 			mbeep();
    561  1.13      phil 		else if (m->topline + m->h - 1 >= m->numopts + hasexit)
    562   1.7      phil 			mbeep();
    563   1.7      phil 		else {
    564  1.13      phil 			m->topline = MIN(m->topline+m->h-1,
    565  1.13      phil 					 m->numopts+hasexit-m->h+1);
    566  1.23   hubertf 			m->cursel = MIN(m->numopts-1, m->cursel+m->h-1);
    567   1.7      phil 			wclear (m->mw);
    568   1.7      phil 			refresh = 1;
    569   1.7      phil 		}
    570   1.7      phil 
    571   1.1      phil 	} else {
    572   1.9      phil 		ch = req;
    573   1.7      phil 		if (ch == 'x' && hasexit) {
    574   1.1      phil 			m->cursel = m->numopts;
    575   1.9      phil 			scroll_sel = 1;
    576   1.7      phil 			refresh = 1;
    577  1.24      phil 		} else
    578  1.24      phil 			if (!(m->mopt & MC_NOSHORTCUT)) {
    579  1.23   hubertf 				if (ch > 'z')
    580  1.23   hubertf 					ch = 255;
    581  1.23   hubertf 				if (ch >= 'a') {
    582  1.23   hubertf 					if (ch > 'x') ch--;
    583  1.24      phil 				   		ch = ch - 'a';
    584  1.23   hubertf 				} else
    585  1.23   hubertf 					ch = 25 + ch - 'A';
    586  1.23   hubertf 				if (ch < 0 || ch >= m->numopts)
    587  1.23   hubertf 					mbeep();
    588  1.23   hubertf 				else {
    589  1.23   hubertf 					m->cursel = ch;
    590  1.23   hubertf 					scroll_sel = 1;
    591  1.23   hubertf 					refresh = 1;
    592  1.23   hubertf 				}
    593  1.24      phil 			} else
    594  1.24      phil 				mbeep();
    595   1.1      phil 	}
    596   1.9      phil 
    597  1.14      phil 	if (m->mopt & MC_SCROLL && scroll_sel) {
    598   1.9      phil 		while (m->cursel >= m->topline + m->h -1 )
    599  1.13      phil 			m->topline = MIN(m->topline+m->h-1,
    600  1.13      phil 					 m->numopts+hasexit-m->h+1);
    601   1.9      phil 		while (m->cursel < m->topline)
    602  1.13      phil 			m->topline = MAX(0,m->topline-m->h+1);
    603   1.9      phil 	}
    604   1.9      phil 
    605   1.7      phil 	if (refresh) {
    606   1.7      phil 		post_menu (m);
    607   1.7      phil 		wrefresh (m->mw);
    608   1.1      phil 	}
    609   1.1      phil }
    610   1.1      phil 
    611  1.23   hubertf int
    612  1.23   hubertf menu_init (void)
    613  1.17       cgd {
    614  1.17       cgd 
    615  1.17       cgd 	if (__menu_init)
    616  1.17       cgd 		return 0;
    617  1.17       cgd 
    618  1.17       cgd 	if (initscr() == NULL)
    619  1.17       cgd 		return 1;
    620  1.17       cgd 
    621  1.17       cgd 	cbreak();
    622  1.17       cgd 	noecho();
    623  1.26     perry 
    624  1.26     perry 	/* XXX Should be configurable but it almost isn't worth it. */
    625  1.26     perry 	if (has_colors()) {
    626  1.26     perry 		start_color();
    627  1.26     perry 		init_pair(1, COLOR_WHITE, COLOR_BLUE);
    628  1.26     perry 		bkgd(COLOR_PAIR(1));
    629  1.26     perry 		attrset(COLOR_PAIR(1));
    630  1.26     perry 	}
    631  1.26     perry 
    632  1.17       cgd 	max_lines = getmaxy(stdscr);
    633  1.17       cgd 	max_cols = getmaxx(stdscr);
    634  1.17       cgd 	init_keyseq();
    635  1.17       cgd #ifdef DYNAMIC_MENUS
    636  1.17       cgd 	num_menus = DYN_INIT_NUM;
    637  1.17       cgd 	while (num_menus < DYN_MENU_START)
    638  1.17       cgd 		num_menus *= 2;
    639  1.17       cgd 	menus = (menudesc *) malloc(sizeof(menudesc)*num_menus);
    640  1.17       cgd 	if (menus == NULL)
    641  1.17       cgd 		return 2;
    642  1.17       cgd 	(void) memset ((void *)menus, 0, sizeof(menudesc)*num_menus);
    643  1.17       cgd 	(void) memcpy ((void *)menus, (void *)menu_def,
    644  1.17       cgd 		sizeof(menudesc)*DYN_MENU_START);
    645  1.17       cgd 	 num_avail = num_menus - DYN_MENU_START;
    646  1.17       cgd #endif
    647  1.17       cgd 
    648  1.17       cgd 	__menu_init = 1;
    649  1.17       cgd 	return (0);
    650  1.17       cgd }
    651  1.17       cgd 
    652  1.23   hubertf void
    653  1.23   hubertf process_menu (int num)
    654   1.1      phil {
    655   1.1      phil 	int sel = 0;
    656   1.1      phil 	int req, done;
    657   1.1      phil 	int last_num;
    658   1.1      phil 
    659  1.14      phil 	struct menudesc *m;
    660  1.14      phil 
    661  1.14      phil 	m = &menus[num];
    662   1.1      phil 
    663   1.1      phil 	done = FALSE;
    664   1.1      phil 
    665   1.1      phil 	/* Initialize? */
    666  1.17       cgd 	if (menu_init()) {
    667  1.17       cgd 		__menu_initerror();
    668  1.17       cgd 		return;
    669   1.1      phil 	}
    670  1.17       cgd 
    671   1.1      phil 	if (__m_endwin) {
    672   1.7      phil      		wclear(stdscr);
    673   1.1      phil 		wrefresh(stdscr);
    674   1.1      phil 		__m_endwin = 0;
    675   1.1      phil 	}
    676   1.1      phil 	if (m->mw == NULL)
    677   1.1      phil 		init_menu (m);
    678   1.1      phil 
    679   1.7      phil 	/* Always preselect option 0 and display from 0! */
    680   1.1      phil 	m->cursel = 0;
    681   1.7      phil 	m->topline = 0;
    682   1.1      phil 
    683   1.1      phil 	while (!done) {
    684   1.1      phil 		last_num = num;
    685   1.1      phil 		if (__m_endwin) {
    686   1.1      phil 			wclear(stdscr);
    687   1.1      phil 			wrefresh(stdscr);
    688   1.1      phil 			__m_endwin = 0;
    689   1.1      phil 		}
    690   1.1      phil 		/* Process the display action */
    691  1.14      phil 		if (m->post_act)
    692  1.14      phil 			(*m->post_act)();
    693   1.1      phil 		post_menu (m);
    694   1.1      phil 		wrefresh (m->mw);
    695   1.1      phil 
    696   1.1      phil 		while ((req = menucmd (m->mw)) != REQ_EXECUTE)
    697   1.4      phil 			process_req (m, num, req);
    698   1.1      phil 
    699   1.1      phil 		sel = m->cursel;
    700   1.1      phil 		wclear (m->mw);
    701   1.1      phil 		wrefresh (m->mw);
    702   1.1      phil 
    703   1.1      phil 		/* Process the items */
    704  1.14      phil 		if (sel < m->numopts) {
    705  1.14      phil 			if (m->opts[sel].opt_flags & OPT_ENDWIN) {
    706  1.14      phil 				endwin();
    707  1.14      phil 				__m_endwin = 1;
    708  1.14      phil 			}
    709  1.14      phil 			if (m->opts[sel].opt_action)
    710  1.23   hubertf 				done = (*m->opts[sel].opt_action)(m);
    711  1.14      phil 			if (m->opts[sel].opt_menu != -1) {
    712  1.14      phil 				if (m->opts[sel].opt_flags & OPT_SUB)
    713  1.14      phil 					process_menu (m->opts[sel].opt_menu);
    714  1.14      phil 				else
    715  1.14      phil 					num = m->opts[sel].opt_menu;
    716  1.14      phil 			}
    717  1.15      phil 
    718  1.15      phil                         if (m->opts[sel].opt_flags & OPT_EXIT)
    719  1.15      phil                                 done = TRUE;
    720  1.15      phil 
    721  1.14      phil 		} else
    722   1.1      phil 			done = TRUE;
    723   1.1      phil 
    724   1.1      phil 		/* Reselect m just in case */
    725   1.1      phil 		if (num != last_num) {
    726   1.1      phil 			m = &menus[num];
    727  1.14      phil 
    728   1.1      phil 			/* Initialize? */
    729   1.1      phil 			if (m->mw == NULL)
    730   1.1      phil 				init_menu (m);
    731  1.14      phil 			if (m->post_act)
    732  1.14      phil 				(*m->post_act)();
    733   1.1      phil 		}
    734   1.1      phil 	}
    735   1.1      phil 
    736   1.1      phil 	/* Process the exit action */
    737  1.14      phil 	if (m->exit_act)
    738  1.14      phil 		(*m->exit_act)();
    739   1.1      phil }
    740  1.13      phil 
    741  1.13      phil /* Control L is end of standard routines, remaining only for dynamic. */
    743  1.13      phil 
    744  1.13      phil /* Beginning of routines for dynamic menus. */
    745  1.14      phil 
    746  1.14      phil /* local prototypes */
    747  1.14      phil static int double_menus (void);
    748  1.24      phil 
    749  1.23   hubertf static int
    750  1.14      phil double_menus (void)
    751  1.14      phil {
    752  1.14      phil 	menudesc *temp;
    753  1.14      phil 
    754  1.14      phil 	temp  = (menudesc *) malloc(sizeof(menudesc)*num_menus*2);
    755  1.14      phil 	if (temp == NULL)
    756  1.14      phil 		return 0;
    757  1.14      phil 	(void) memset ((void *)temp, 0,
    758  1.14      phil 		sizeof(menudesc)*num_menus*2);
    759  1.14      phil 	(void) memcpy ((void *)temp, (void *)menus,
    760  1.14      phil 		sizeof(menudesc)*num_menus);
    761  1.14      phil 	free (menus);
    762  1.14      phil 	menus = temp;
    763  1.14      phil 	num_avail = num_menus;
    764  1.14      phil 	num_menus *= 2;
    765  1.14      phil 
    766  1.14      phil 	return 1;
    767  1.14      phil }
    768  1.23   hubertf 
    769  1.23   hubertf int
    770  1.24      phil new_menu (char * title, menu_ent * opts, int numopts,
    771  1.25      phil 	int x, int y, int h, int w, int mopt,
    772  1.14      phil 	void (*post_act)(void), void (*exit_act)(void), char * help)
    773  1.14      phil {
    774   1.7      phil 	int ix;
    775  1.14      phil 
    776  1.14      phil 	/* Check for free menu entry. */
    777  1.14      phil 	if (num_avail == 0)
    778  1.14      phil 		if (!double_menus ())
    779  1.14      phil 			return -1;
    780  1.14      phil 
    781  1.14      phil 	/* Find free menu entry. */
    782  1.14      phil 	for (ix = DYN_MENU_START; ix < num_menus && menus[ix].mopt & MC_VALID;
    783  1.14      phil 		ix++) /* do  nothing */;
    784  1.14      phil 
    785  1.14      phil 	/* if ix == num_menus ... panic */
    786  1.14      phil 
    787  1.24      phil 	/* Set Entries */
    788  1.14      phil 	menus[ix].title = title ? title : "";
    789  1.14      phil 	menus[ix].opts = opts;
    790  1.14      phil 	menus[ix].numopts = numopts;
    791  1.14      phil 	menus[ix].x = x;
    792  1.14      phil 	menus[ix].y = y;
    793  1.14      phil 	menus[ix].h = h;
    794  1.14      phil 	menus[ix].w = w;
    795  1.14      phil 	menus[ix].mopt = mopt | MC_VALID;
    796  1.14      phil 	menus[ix].post_act = post_act;
    797  1.14      phil 	menus[ix].exit_act = exit_act;
    798  1.21       cgd 	menus[ix].helpstr  = help;
    799  1.14      phil 	menus[ix].exitstr  = "Exit";
    800  1.14      phil 
    801  1.14      phil 	init_menu (&menus[ix]);
    802  1.14      phil 
    803  1.14      phil 	return ix;
    804  1.14      phil }
    805  1.23   hubertf 
    806  1.23   hubertf void
    807  1.14      phil free_menu (int menu_no)
    808  1.14      phil {
    809  1.14      phil 	if (menu_no < num_menus) {
    810  1.14      phil 		menus[menu_no].mopt &= ~MC_VALID;
    811  1.14      phil 		if (menus[menu_no].mw != NULL)
    812  1.14      phil 			delwin (menus[menu_no].mw);
    813  1.14      phil 	}
    814                 }
    815