Home | History | Annotate | Line # | Download | only in menuc
menu_sys.def revision 1.10
      1  1.10  phil /*	$NetBSD: menu_sys.def,v 1.10 1998/06/30 06:57:57 phil 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.9  phil static char *str_ptr = str_area;
     94   1.1  phil 
     95   1.9  phil /* Macros */
     96   1.1  phil #define MAX(x,y) ((x)>(y)?(x):(y))
     97   1.7  phil #define MIN(x,y) ((x)<(y)?(x):(y))
     98   1.1  phil 
     99   1.1  phil /* Initialization state. */
    100   1.1  phil static int __menu_init = 0;
    101   1.1  phil int __m_endwin = 0;
    102   1.5  phil static int max_lines = 0;
    103   1.7  phil static char *scrolltext = " <: page up, >: page down";
    104   1.1  phil 
    105   1.1  phil /* prototypes for in here! */
    106   1.9  phil static void ins_keyseq (struct keyseq **seq, struct keyseq *ins);
    107   1.9  phil static void init_keyseq (void);
    108   1.1  phil static void init_menu (struct menudesc *m);
    109   1.1  phil static void post_menu (struct menudesc *m);
    110   1.5  phil static void process_help (struct menudesc *m, int num);
    111   1.4  phil static void process_req (struct menudesc *m, int num, int req);
    112   1.1  phil static void mbeep (void);
    113   1.1  phil static int menucmd (WINDOW *w);
    114   1.1  phil 
    115   1.1  phil #ifndef NULL
    116   1.1  phil #define NULL (void *)0
    117   1.1  phil #endif
    118   1.1  phil 
    119   1.1  phil /* menu system processing routines */
    120   1.1  phil 
    121   1.1  phil static void mbeep (void)
    122   1.1  phil {
    123   1.1  phil 	fprintf (stderr,"\a");
    124   1.1  phil }
    125   1.1  phil 
    126   1.9  phil static void ins_keyseq (struct keyseq **seq, struct keyseq *ins)
    127   1.9  phil {
    128   1.9  phil 	if (*seq == NULL) {
    129   1.9  phil 		ins->next = NULL;
    130   1.9  phil 		*seq = ins;
    131   1.9  phil 	} else if (ins->numchars <= (*seq)->numchars) {
    132   1.9  phil 		ins->next = *seq;
    133   1.9  phil 		*seq = ins;
    134   1.9  phil 	} else
    135   1.9  phil 		ins_keyseq (&(*seq)->next, ins);
    136   1.9  phil }
    137   1.9  phil 
    138   1.9  phil static void init_keyseq (void)
    139   1.9  phil {
    140   1.9  phil 	int i;
    141   1.9  phil 	for (i=0; i<_mc_num_key_seq; i++) {
    142   1.9  phil 		if (_mc_key_seq[i].termcap_name)
    143   1.9  phil 			_mc_key_seq[i].chars =
    144   1.9  phil 				tgetstr (_mc_key_seq[i].termcap_name,
    145   1.9  phil 					 &str_ptr);
    146   1.9  phil 		if (_mc_key_seq[i].chars != NULL &&
    147   1.9  phil 		    (_mc_key_seq[i].numchars = strlen(_mc_key_seq[i].chars))
    148   1.9  phil 		    > 0)
    149   1.9  phil 			ins_keyseq (&pad_list,&_mc_key_seq[i]);
    150   1.9  phil 	}
    151   1.9  phil }
    152   1.9  phil 
    153   1.1  phil static int mgetch(WINDOW *w)
    154   1.1  phil {
    155   1.1  phil 	static char buf[20];
    156   1.1  phil 	static int  num = 0;
    157   1.9  phil 	struct keyseq *list = pad_list;
    158   1.1  phil 	int i, ret;
    159   1.1  phil 
    160   1.9  phil 	/* key pad processing */
    161   1.9  phil 	while (list) {
    162   1.9  phil 		for (i=0; i< list->numchars; i++) {
    163   1.9  phil 			if (i >= num)
    164   1.9  phil 				buf[num++] = wgetch(w);
    165   1.9  phil 			if (buf[i] != list->chars[i])
    166   1.9  phil 				break;
    167   1.9  phil 		}
    168   1.9  phil 		if (i == list->numchars) {
    169   1.9  phil 			num = 0;
    170   1.9  phil 			return list->keyseq_val;
    171   1.9  phil 		}
    172   1.9  phil 		list = list->next;
    173   1.9  phil 	}
    174   1.1  phil 
    175   1.1  phil 	ret = buf[0];
    176   1.1  phil 	for (i = 0; i < strlen(buf); i++)
    177   1.1  phil 		buf[i] = buf[i+1];
    178   1.1  phil 	num--;
    179   1.1  phil 	return ret;
    180   1.1  phil }
    181   1.1  phil 
    182   1.1  phil static int menucmd (WINDOW *w)
    183   1.1  phil {
    184   1.1  phil 	int ch;
    185   1.1  phil 
    186   1.1  phil 	while (TRUE) {
    187   1.1  phil 		ch = mgetch(w);
    188   1.1  phil 
    189   1.1  phil 		switch (ch) {
    190   1.1  phil 		case '\n':
    191   1.1  phil 			return REQ_EXECUTE;
    192   1.1  phil 		case '\016':
    193   1.9  phil 		case KEYSEQ_DOWN_ARROW:
    194   1.1  phil 			return REQ_NEXT_ITEM;
    195   1.1  phil 		case '\020':
    196   1.9  phil 		case KEYSEQ_UP_ARROW:
    197   1.1  phil 			return REQ_PREV_ITEM;
    198   1.3  phil 		case '\014':
    199   1.3  phil 		        return REQ_REDISPLAY;
    200   1.9  phil 		case '<':
    201   1.9  phil 		case KEYSEQ_PAGE_UP:
    202   1.9  phil 			return REQ_SCROLLUP;
    203   1.9  phil 		case '>':
    204   1.9  phil 		case KEYSEQ_PAGE_DOWN:
    205   1.9  phil 			return REQ_SCROLLDOWN;
    206   1.9  phil 		case '?':
    207   1.9  phil 			return REQ_HELP;
    208   1.1  phil 		}
    209   1.1  phil 
    210   1.9  phil 		if (isalpha(ch))
    211   1.1  phil 			return (ch);
    212   1.1  phil 
    213   1.1  phil 		mbeep();
    214   1.1  phil 		wrefresh(w);
    215   1.1  phil 	}
    216   1.1  phil }
    217   1.1  phil 
    218   1.1  phil static void init_menu (struct menudesc *m)
    219   1.1  phil {
    220   1.1  phil 	int max;
    221   1.1  phil 	char **p;
    222   1.7  phil 	int add, exitadd;
    223   1.1  phil 
    224   1.7  phil 	add = ((m->mopt & NOBOX) ? 2 : 4);
    225   1.7  phil 	exitadd = ((m->mopt & NOEXITOPT) ? 0 : 1);
    226   1.1  phil 	max = strlen(m->title);
    227   1.1  phil 
    228   1.7  phil 	/* Calculate h? h == number of visible options. */
    229   1.1  phil 	if (m->h == 0) {
    230   1.7  phil 		m->h = m->numopts + exitadd;
    231   1.7  phil 		if (m->h + m->y + add >= max_lines && (m->mopt & SCROLL))
    232   1.9  phil 			m->h = max_lines - m->y - add ;
    233   1.1  phil 	}
    234   1.7  phil 
    235   1.7  phil 	/* Check window heights and set scrolling */
    236   1.7  phil 	if (m->h < m->numopts + exitadd) {
    237   1.9  phil 		if (!(m->mopt & SCROLL) || m->h < 3) {
    238   1.7  phil 			endwin();
    239   1.7  phil 			(void) fprintf (stderr,
    240   1.9  phil 				"Window too small for menu \"%s\"\n",
    241   1.9  phil 				m->title);
    242   1.7  phil 			exit(1);
    243   1.7  phil 		}
    244   1.7  phil 	} else
    245   1.7  phil 		m->mopt &= ~SCROLL;
    246   1.1  phil 
    247   1.9  phil 	/* check for screen fit */
    248   1.9  phil 	if (m->y + m->h + add > max_lines) {
    249   1.9  phil 		endwin();
    250   1.9  phil 		(void) fprintf (stderr,
    251   1.9  phil 			"Screen too small for menu \"%s\"\n", m->title);
    252   1.9  phil 		exit(1);
    253   1.9  phil 
    254   1.9  phil 	}
    255   1.9  phil 
    256   1.1  phil 	/* Calculate w? */
    257   1.1  phil 	if (m->w == 0) {
    258   1.1  phil 		p = m->opts;
    259   1.7  phil 		if (m->mopt & SCROLL)
    260   1.7  phil 			max = strlen(scrolltext);
    261   1.1  phil 		while (*p) {
    262   1.1  phil 			max = MAX(max,strlen(*p));
    263   1.1  phil 			p++;
    264   1.1  phil 		}
    265   1.1  phil 		m->w = max;
    266   1.1  phil 	}
    267   1.1  phil 
    268   1.1  phil 	/* Get the windows. */
    269   1.1  phil 	m->mw = newwin(m->h+add, m->w+add, m->y, m->x);
    270   1.1  phil 
    271   1.1  phil 	if (m->mw == NULL) {
    272   1.1  phil 		endwin();
    273   1.1  phil 		(void) fprintf (stderr,
    274   1.1  phil 			"Could not create window for window with title "
    275   1.1  phil 			" \"%s\"\n", m->title);
    276   1.1  phil 		exit(1);
    277   1.1  phil 	}
    278   1.1  phil }
    279   1.1  phil 
    280   1.1  phil static void post_menu (struct menudesc *m)
    281   1.1  phil {
    282   1.1  phil 	int i;
    283   1.7  phil 	int hasbox, cury, maxy, selrow, lastopt;
    284   1.1  phil 	int tadd;
    285   1.1  phil 
    286   1.1  phil 	if (m->mopt & NOBOX) {
    287   1.1  phil 		cury = 0;
    288   1.7  phil 		maxy = m->h;
    289   1.1  phil 		hasbox = 0;
    290   1.1  phil 	} else {
    291   1.1  phil 		cury = 1;
    292   1.7  phil 		maxy = m->h+1;
    293   1.1  phil 		hasbox = 1;
    294   1.1  phil 	}
    295   1.1  phil 
    296   1.9  phil 	/* Clear the window */
    297   1.9  phil 	wclear (m->mw);
    298   1.9  phil 
    299   1.1  phil 	tadd = strlen(m->title) ? 2 : 0;
    300   1.1  phil 
    301   1.1  phil 	if (tadd) {
    302   1.1  phil 		mvwaddstr(m->mw, cury, cury, m->title);
    303   1.1  phil 		cury += 2;
    304   1.7  phil 		maxy += 2;
    305   1.1  phil 	}
    306   1.1  phil 
    307   1.7  phil 	/* Set defaults, calculate lastopt. */
    308   1.7  phil 	selrow = -1;
    309   1.7  phil 	if (m->mopt & SCROLL) {
    310   1.7  phil 		lastopt = MIN(m->numopts, m->topline+m->h-1);
    311   1.7  phil 		maxy -= 1;
    312   1.7  phil 	} else
    313   1.7  phil 		lastopt = m->numopts;
    314   1.7  phil 
    315   1.7  phil 	for (i=m->topline; i<lastopt; i++, cury++) {
    316   1.1  phil 		if (m->cursel == i) {
    317   1.1  phil 			mvwaddstr (m->mw, cury, hasbox, ">");
    318   1.1  phil 			wstandout(m->mw);
    319   1.7  phil 			selrow = cury;
    320   1.1  phil 		} else
    321   1.1  phil 			mvwaddstr (m->mw, cury, hasbox, " ");
    322   1.1  phil 		waddstr (m->mw, m->opts[i]);
    323   1.1  phil 		if (m->cursel == i)
    324   1.1  phil 			wstandend(m->mw);
    325   1.1  phil 	}
    326   1.7  phil 
    327   1.7  phil 	/* Add the exit option. */
    328   1.7  phil 	if (!(m->mopt & NOEXITOPT) && cury < maxy) {
    329   1.7  phil 		if (m->cursel >= m->numopts) {
    330   1.7  phil 			mvwaddstr (m->mw, cury, hasbox, ">");
    331   1.7  phil 			wstandout(m->mw);
    332   1.7  phil 			selrow = cury;
    333   1.7  phil 		} else
    334   1.7  phil 			mvwaddstr (m->mw, cury, hasbox, " ");
    335   1.7  phil 		waddstr (m->mw, "x: Exit");
    336   1.7  phil 		if (m->cursel >= m->numopts)
    337   1.7  phil 			wstandend(m->mw);
    338   1.7  phil 		cury++;
    339   1.7  phil 	}
    340   1.7  phil 
    341   1.7  phil 	/* Add the scroll line */
    342   1.7  phil 	if (m->mopt & SCROLL) {
    343   1.7  phil 		mvwaddstr (m->mw, cury, hasbox, scrolltext);
    344   1.7  phil 		if (selrow < 0)
    345   1.7  phil 			selrow = cury;
    346   1.7  phil 	}
    347   1.7  phil 
    348   1.7  phil 	/* Add the box. */
    349   1.1  phil 	if (!(m->mopt & NOBOX))
    350   1.1  phil 		box(m->mw, '*', '*');
    351   1.1  phil 
    352   1.7  phil 	wmove(m->mw, selrow, hasbox);
    353   1.1  phil }
    354   1.1  phil 
    355   1.5  phil static void process_help (struct menudesc *m, int num)
    356   1.5  phil {
    357   1.5  phil 	char *help = m->helpstr;
    358   1.7  phil 	int lineoff = 0;
    359   1.5  phil 	int curoff = 0;
    360   1.5  phil 	int again;
    361   1.7  phil 	int winin;
    362   1.5  phil 
    363   1.6  phil 	/* Is there help? */
    364   1.6  phil 	if (!help) {
    365   1.6  phil 		mbeep();
    366   1.6  phil 		return;
    367   1.6  phil 	}
    368   1.6  phil 
    369   1.6  phil 	/* Display the help information. */
    370   1.7  phil 	do {
    371   1.5  phil 		if (lineoff < curoff) {
    372   1.5  phil 			help = m->helpstr;
    373   1.5  phil 			curoff = 0;
    374   1.5  phil 		}
    375   1.5  phil 		while (*help && curoff < lineoff) {
    376   1.5  phil 			if (*help == '\n')
    377   1.5  phil 				curoff++;
    378   1.5  phil 			help++;
    379   1.5  phil 		}
    380   1.5  phil 
    381   1.5  phil 		wclear(stdscr);
    382   1.5  phil 		mvwaddstr (stdscr, 0, 0,
    383   1.9  phil 			"Help: exit: x,  page up: u <, page down: d >");
    384   1.5  phil 		mvwaddstr (stdscr, 2, 0, help);
    385   1.5  phil 		wmove (stdscr, 1, 0);
    386   1.5  phil 	  	wrefresh(stdscr);
    387   1.5  phil 
    388   1.5  phil 		do {
    389   1.5  phil 			winin = mgetch(stdscr);
    390   1.9  phil 			if (winin < KEYSEQ_FIRST)
    391   1.9  phil 				winin = tolower(winin);
    392   1.5  phil 			again = 0;
    393   1.5  phil 			switch (winin) {
    394   1.5  phil 				case '<':
    395   1.5  phil 				case 'u':
    396   1.9  phil 				case KEYSEQ_UP_ARROW:
    397   1.9  phil 				case KEYSEQ_LEFT_ARROW:
    398   1.9  phil 				case KEYSEQ_PAGE_UP:
    399   1.5  phil 					if (lineoff)
    400   1.5  phil 						lineoff -= max_lines - 2;
    401   1.7  phil 					else
    402   1.7  phil 						again = 1;
    403   1.5  phil 					break;
    404   1.5  phil 				case '>':
    405   1.5  phil 				case 'd':
    406   1.9  phil 				case KEYSEQ_DOWN_ARROW:
    407   1.9  phil 				case KEYSEQ_RIGHT_ARROW:
    408   1.9  phil 				case KEYSEQ_PAGE_DOWN:
    409   1.5  phil 					if (*help)
    410   1.5  phil 						lineoff += max_lines - 2;
    411   1.7  phil 					else
    412   1.7  phil 						again = 1;
    413   1.5  phil 					break;
    414  1.10  phil 				case 'q':
    415  1.10  phil 					break;
    416   1.9  phil 				case 'x':
    417  1.10  phil 					winin = 'q';
    418   1.5  phil 					break;
    419   1.5  phil 				default:
    420   1.5  phil 					again = 1;
    421   1.5  phil 			}
    422   1.9  phil 			if (again)
    423   1.9  phil 				mbeep();
    424   1.5  phil 		} while (again);
    425   1.5  phil 	} while (winin != 'q');
    426   1.5  phil 
    427   1.5  phil 	/* Restore current menu */
    428   1.5  phil 	wclear(stdscr);
    429   1.5  phil 	wrefresh(stdscr);
    430   1.5  phil 	process_item (&num, -2);
    431   1.5  phil }
    432   1.5  phil 
    433   1.4  phil static void process_req (struct menudesc *m, int num, int req)
    434   1.1  phil {
    435   1.1  phil 	int ch;
    436   1.1  phil 	int hasexit = (m->mopt & NOEXITOPT ? 0 : 1 );
    437   1.7  phil 	int refresh = 0;
    438   1.9  phil 	int scroll_sel = 0;
    439   1.1  phil 
    440   1.1  phil 	if (req == REQ_EXECUTE)
    441   1.1  phil 		return;
    442   1.7  phil 
    443   1.1  phil 	else if (req == REQ_NEXT_ITEM) {
    444   1.7  phil 		if (m->cursel < m->numopts + hasexit - 1) {
    445   1.1  phil 			m->cursel++;
    446   1.9  phil 			scroll_sel = 1;
    447   1.7  phil 			refresh = 1;
    448   1.7  phil 		} else
    449   1.1  phil 			mbeep();
    450   1.7  phil 
    451   1.1  phil 	} else if (req == REQ_PREV_ITEM) {
    452   1.7  phil 		if (m->cursel > 0) {
    453   1.1  phil 			m->cursel--;
    454   1.9  phil 			scroll_sel = 1;
    455   1.7  phil 			refresh = 1;
    456   1.7  phil 		} else
    457   1.1  phil 			mbeep();
    458   1.3  phil 
    459   1.3  phil 	} else if (req == REQ_REDISPLAY) {
    460   1.3  phil 		wclear(stdscr);
    461   1.3  phil 		wrefresh(stdscr);
    462   1.3  phil 		process_item (&num, -2);
    463   1.7  phil 		refresh = 1;
    464   1.3  phil 
    465   1.9  phil 	} else if (req == REQ_HELP) {
    466   1.5  phil 		process_help (m, num);
    467   1.7  phil 		refresh = 1;
    468   1.7  phil 
    469   1.9  phil 	} else if (req == REQ_SCROLLUP) {
    470   1.9  phil 		if (!(m->mopt & SCROLL))
    471   1.9  phil 			mbeep();
    472   1.9  phil 		else if (m->topline == 0)
    473   1.7  phil 			mbeep();
    474   1.7  phil 		else {
    475   1.7  phil 			m->topline -= m->h-1;
    476   1.7  phil 			wclear (m->mw);
    477   1.7  phil 			refresh = 1;
    478   1.7  phil 		}
    479   1.7  phil 
    480   1.9  phil 	} else if (req == REQ_SCROLLDOWN) {
    481   1.9  phil 		if (!(m->mopt & SCROLL))
    482   1.9  phil 			mbeep();
    483   1.9  phil 		else if (m->topline + m->h - 1 > m->numopts + hasexit)
    484   1.7  phil 			mbeep();
    485   1.7  phil 		else {
    486   1.7  phil 			m->topline += m->h-1;
    487   1.7  phil 			wclear (m->mw);
    488   1.7  phil 			refresh = 1;
    489   1.7  phil 		}
    490   1.7  phil 
    491   1.1  phil 	} else {
    492   1.9  phil 		ch = req;
    493   1.7  phil 		if (ch == 'x' && hasexit) {
    494   1.1  phil 			m->cursel = m->numopts;
    495   1.9  phil 			scroll_sel = 1;
    496   1.7  phil 			refresh = 1;
    497   1.7  phil 		} else {
    498   1.8  phil 			if (ch > 'z')
    499   1.8  phil 				ch = 255;
    500   1.8  phil 			if (ch >= 'a') {
    501   1.8  phil 				if (ch > 'x') ch--;
    502   1.8  phil 				ch = ch - 'a';
    503   1.8  phil 			} else
    504   1.8  phil 				ch = 25 + ch - 'A';
    505   1.1  phil 			if (ch < 0 || ch >= m->numopts)
    506   1.1  phil 				mbeep();
    507   1.7  phil 			else {
    508   1.1  phil 				m->cursel = ch;
    509   1.9  phil 				scroll_sel = 1;
    510   1.7  phil 				refresh = 1;
    511   1.7  phil 			}
    512   1.1  phil 		}
    513   1.1  phil 	}
    514   1.9  phil 
    515   1.9  phil 	if (m->mopt & SCROLL && scroll_sel) {
    516   1.9  phil 		while (m->cursel >= m->topline + m->h -1 )
    517   1.9  phil 			m->topline += m->h -1;
    518   1.9  phil 		while (m->cursel < m->topline)
    519   1.9  phil 			m->topline -= m->h -1;
    520   1.9  phil 	}
    521   1.9  phil 
    522   1.7  phil 	if (refresh) {
    523   1.7  phil 		post_menu (m);
    524   1.7  phil 		wrefresh (m->mw);
    525   1.1  phil 	}
    526   1.1  phil }
    527   1.1  phil 
    528   1.1  phil void process_menu (int num)
    529   1.1  phil {
    530   1.1  phil 	int sel = 0;
    531   1.1  phil 	int req, done;
    532   1.1  phil 	int last_num;
    533   1.1  phil 
    534   1.1  phil 	struct menudesc *m = &menus[num];
    535   1.1  phil 
    536   1.1  phil 	done = FALSE;
    537   1.1  phil 
    538   1.1  phil 	/* Initialize? */
    539   1.1  phil 	if (!__menu_init) {
    540   1.2  phil 		if (initscr() == NULL) {
    541   1.2  phil 			__menu_initerror();
    542   1.2  phil 			return;
    543   1.2  phil 		}
    544   1.1  phil 		cbreak();
    545   1.1  phil 		noecho();
    546   1.5  phil 		max_lines = stdscr->maxy;
    547   1.9  phil 		init_keyseq();
    548   1.1  phil 		__menu_init = 1;
    549   1.1  phil 	}
    550   1.1  phil 	if (__m_endwin) {
    551   1.7  phil      		wclear(stdscr);
    552   1.1  phil 		wrefresh(stdscr);
    553   1.1  phil 		__m_endwin = 0;
    554   1.1  phil 	}
    555   1.1  phil 	if (m->mw == NULL)
    556   1.1  phil 		init_menu (m);
    557   1.1  phil 
    558   1.7  phil 	/* Always preselect option 0 and display from 0! */
    559   1.1  phil 	m->cursel = 0;
    560   1.7  phil 	m->topline = 0;
    561   1.1  phil 
    562   1.1  phil 	while (!done) {
    563   1.1  phil 		last_num = num;
    564   1.1  phil 		if (__m_endwin) {
    565   1.1  phil 			wclear(stdscr);
    566   1.1  phil 			wrefresh(stdscr);
    567   1.1  phil 			__m_endwin = 0;
    568   1.1  phil 		}
    569   1.1  phil 		/* Process the display action */
    570   1.1  phil 		process_item (&num, -2);
    571   1.1  phil 		post_menu (m);
    572   1.1  phil 		wrefresh (m->mw);
    573   1.1  phil 
    574   1.1  phil 		while ((req = menucmd (m->mw)) != REQ_EXECUTE)
    575   1.4  phil 			process_req (m, num, req);
    576   1.1  phil 
    577   1.1  phil 		sel = m->cursel;
    578   1.1  phil 		wclear (m->mw);
    579   1.1  phil 		wrefresh (m->mw);
    580   1.1  phil 
    581   1.1  phil 		/* Process the items */
    582   1.1  phil 		if (sel < m->numopts)
    583   1.1  phil 			done = process_item (&num, sel);
    584   1.1  phil 		else
    585   1.1  phil 			done = TRUE;
    586   1.1  phil 
    587   1.1  phil 		/* Reselect m just in case */
    588   1.1  phil 		if (num != last_num) {
    589   1.1  phil 			m = &menus[num];
    590   1.1  phil 			/* Initialize? */
    591   1.1  phil 			if (m->mw == NULL)
    592   1.1  phil 				init_menu (m);
    593   1.1  phil 			process_item (&num, -2);
    594   1.1  phil 		}
    595   1.1  phil 	}
    596   1.1  phil 
    597   1.1  phil 	/* Process the exit action */
    598   1.1  phil 	process_item (&num, -1);
    599   1.1  phil }
    600   1.7  phil 
    601