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