]> andersk Git - moira.git/blob - clients/moira/menu.c
7b1cd3b5dde2d03ae0441fd758ca107a0938152c
[moira.git] / clients / moira / menu.c
1 /*
2  * Copyright 1987 by the Massachusetts Institute of Technology.
3  * For copying and distribution information, see the file
4  * "mit-copyright.h".
5  *
6  * $Source$
7  * $Author$
8  * $Header$
9  *
10  * Generic menu system module.
11  *
12  * Basically, we define an enormous tree structure which represents the
13  * menu.  Some extra pieces (ml_command, ma_doc) get thrown in so we can
14  * also use the structure for a command-based system.
15  *
16  * By making the menu descriptions so general, we can ease porting to just
17  * about anything.
18  */
19
20 #ifndef lint
21 static char rcsid_menu_c[] = "$Header$";
22
23 #endif lint
24
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include <signal.h>
28 #include <curses.h>
29 #include <ctype.h>
30 #include <strings.h>
31 #include <varargs.h>
32 #include <com_err.h>
33 #include "menu.h"
34
35 #define MAX(A,B)        ((A) > (B) ? (A) : (B))
36 #define MIN(A,B)        ((A) < (B) ? (A) : (B))
37 #define CTL(ch)         ((ch) & 037)
38
39 #define MIN_INPUT 2             /* Minimum number of lines for input window */
40
41 extern char *calloc();
42 int more_flg = 1;
43
44 /* Structure for holding current displayed menu */
45 struct menu_screen {
46     WINDOW *ms_screen;          /* Window for this menu */
47     WINDOW *ms_title;           /* Title subwindow */
48     WINDOW *ms_menu;            /* Menu subwindow */
49     WINDOW *ms_input;           /* Input subwindow */
50     int ms_input_y;             /* Input subwindow reference coordinate */
51 }          *cur_ms;
52
53 #define NULLMS ((struct menu_screen *) 0)
54
55 Menu *top_menu;                 /* Root for command search */
56
57 /*
58  * Hook function to cause error messages to be printed through
59  * curses instead of around it.
60  */
61
62 int
63 menu_com_err_hook(who, code, fmt, args)
64     char *who;
65     int code;
66     char *fmt;
67     va_list args;
68 {
69     char buf[BUFSIZ], *cp;
70
71     FILE _strbuf;
72
73     (void) strcpy(buf, who);
74     for (cp = buf; *cp; cp++);
75     *cp++ = ':';
76     *cp++ = ' ';
77     if (code) {
78         (void) strcpy(cp, error_message(code));
79         while (*cp)
80             cp++;
81     }
82     _strbuf._flag = _IOWRT + _IOSTRG;
83     _strbuf._ptr = cp;
84     _strbuf._cnt = BUFSIZ - (cp - buf);
85     _doprnt(fmt, args, &_strbuf);
86     (void) putc('\0', &_strbuf);
87     Put_message(buf);
88 }
89
90 /*
91  * Start_menu takes a menu as an argument.  It initializes curses u.s.w.,
92  * and a quit in any submenu should unwind back to here.  (it might not,
93  * if user functions which run their own menus don't cooperate.)
94  * Start_menu should only be called once, at the start of the program.
95  */
96 Start_menu(m)
97     Menu *m;
98 {
99     struct menu_screen *make_ms();
100     register int (*old_hook)() = set_com_err_hook(menu_com_err_hook);
101     
102     if (initscr() == ERR) {
103         fputs("Can't initialize curses!\n", stderr);
104         Start_no_menu(m);
105     } else {
106         (void) raw();           /* We parse & print everything ourselves */
107         (void) noecho();
108         cur_ms = make_ms(0);    /* So we always have some current */
109         /* menu_screen */ 
110         top_menu = m;
111         /* Run the menu */
112         (void) Do_menu(m, -1, (char **) NULL);
113     }
114     (void) set_com_err_hook(old_hook);
115     Cleanup_menu();
116 }
117
118 Cleanup_menu()
119 {
120     if (cur_ms) {
121         (void) wclear(cur_ms->ms_screen);
122         (void) wrefresh(cur_ms->ms_screen);
123     }
124     endwin();
125 }
126     
127
128 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
129 Start_no_menu(m)
130     Menu *m;
131 {
132     cur_ms = NULLMS;
133     top_menu = m;
134     /* Run the menu */
135     (void) Do_menu(m, -1, (char **) NULL);
136 }
137
138 /*
139  * Create a new menu screen template with the specified menu length
140  * and return it.
141  */
142 struct menu_screen *
143 make_ms(length)
144     int length;
145 {
146     struct menu_screen *ms;
147     char *malloc();
148
149     if (MAX_TITLE + length + MIN_INPUT > LINES) {
150         fputs("Menu too big!\n", stderr);
151         exit(2);
152     }
153
154     ms = (struct menu_screen *) malloc(sizeof(struct menu_screen));
155
156     ms->ms_screen = newwin(0, 0, 0, 0);
157     ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
158     ms->ms_menu = subwin(ms->ms_screen,
159                          length, 0, MAX_TITLE, 0);
160     ms->ms_input = subwin(ms->ms_screen, 0, 0,
161                           ms->ms_input_y = MAX_TITLE + length,
162                           0);
163
164     scrollok(ms->ms_input, TRUE);
165     (void) wmove(ms->ms_input, 0, 0);
166     (void) wclear(ms->ms_screen);
167
168     return (ms);
169 }
170
171 /*
172  * This routine destroys a menu_screen.
173  */
174 destroy_ms(ms)
175     struct menu_screen *ms;
176 {
177     delwin(ms->ms_title);
178     delwin(ms->ms_menu);
179     delwin(ms->ms_input);
180     delwin(ms->ms_screen);
181     free((char *) ms);
182 }
183
184 /*
185  * This guy actually puts up the menu
186  * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
187  * top level menu)
188  */
189 int 
190 Do_menu(m, margc, margv)
191     Menu *m;
192     int margc;
193     char *margv[];
194 {
195     struct menu_screen *my_ms, *old_cur_ms;
196     char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
197     char buf[MAX_ARGC * MAX_ARGLEN];
198     char *argv[MAX_ARGC];
199     int line;
200     int i;
201     struct menu_line *command, *Find_command();
202     int argc;
203     int quitflag, is_topmenu = (margc < 0);
204     
205     /* Entry function gets called with old menu_screen still current */
206     if (m->m_entry != NULLFUNC)
207         if (m->m_entry(m, margc, margv) == DM_QUIT)
208             return DM_NORMAL;
209
210     /* The following get run only in curses mode */
211     if (cur_ms != NULLMS) {
212         /* Get a menu_screen */
213         old_cur_ms = cur_ms;
214         cur_ms = my_ms = make_ms(m->m_length + 1 + (is_topmenu?0:1));
215
216         /* Now print the title and the menu */
217         (void) wclear(my_ms->ms_menu);
218         (void) wmove(my_ms->ms_title, 0, MAX(0, (COLS -
219                                                  strlen(m->m_title)) >> 1));
220         (void) wstandout(my_ms->ms_title);
221         (void) waddstr(my_ms->ms_title, m->m_title);
222         (void) wstandend(my_ms->ms_title);
223
224         for (line = 0; line < m->m_length; line++) {
225             int len = strlen(m->m_lines[line].ml_command);
226             if (len > 12) len=12;
227             
228             (void) wmove(my_ms->ms_menu, line, 0);
229             
230             (void) wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
231                            m->m_lines[line].ml_command,
232                            12-len, "",
233                            m->m_lines[line].ml_doc);
234         }
235         (void) wmove(my_ms->ms_menu, line++, 0);
236         if (!is_topmenu) {
237             (void) waddstr(my_ms->ms_menu,
238                            " r. (return)       Return to previous menu.");
239             (void) wmove(my_ms->ms_menu, line, 0);
240         }
241         (void) waddstr(my_ms->ms_menu, " q. (quit)         Quit.");
242     }
243
244     for (;;) {
245         /* This will be set by a return val from func or submenu */
246         quitflag = DM_NORMAL;
247         /* This is here because we may be coming from another menu */
248         if (cur_ms != NULL)
249             touchwin(my_ms->ms_screen);
250         /* Get a command */
251         if (!Prompt_input("Command: ", buf, sizeof(buf)))
252             continue;
253         /* Parse it into the argument list */
254         /* If there's nothing there, try again */
255         /* Initialize argv */
256         for (argc = 0; argc < MAX_ARGC; argc++)
257             argv[argc] = argvals[argc];
258
259         if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
260             continue;
261         if ((line = atoi(argv[0])) > 0 && line <= m->m_length) {
262             command = &m->m_lines[line - 1];
263         }
264         else if ((!is_topmenu &&
265                   (!strcmp(argv[0], "r")
266                    || !strcmp(argv[0], "return")))
267                  || !strcmp(argv[0], "q")
268                  || !strcmp(argv[0], "quit")) {
269             /* here if it's either return or quit */
270             if (cur_ms != NULLMS) {
271                 cur_ms = old_cur_ms;
272                 destroy_ms(my_ms);
273             }
274             if (m->m_exit != NULLFUNC)
275                 m->m_exit(m);
276             return (*argv[0] == 'r' ? DM_NORMAL : DM_QUIT);
277             /* finally, try to find it using Find_command */
278         }
279         else if ((command = Find_command(argvals[0])) ==
280                  (struct menu_line *) 0) {
281             Put_message("Command not recognized");
282             continue;
283         }
284         /* If we got to here, command is a valid menu_line */
285         /* Send the offical command name into the argv */
286         (void) strcpy(argvals[0], command->ml_command);
287         /* Show that we're working on it */
288         Put_message(command->ml_doc);
289         /* Print args that we've already got */
290         for (i = 1; i < argc; i++) {
291             if (command->ml_args[i].ma_prompt == NULL)
292                 break;
293             (void) sprintf(buf, "%s%s", command->ml_args[i].ma_prompt,
294                            argv[i]);
295             Put_message(buf);
296         }
297         /* Get remaining arguments, if any */
298         for (; argc < command->ml_argc; argc++) {
299             if (!Prompt_input(command->ml_args[argc].ma_prompt,
300                               argvals[argc], sizeof(argvals[argc])))
301                 goto punt_command;
302         }
303         if (command->ml_function != NULLFUNC) {
304             /* If it's got a function, call it */
305             quitflag = command->ml_function(argc, argv);
306         }
307         else if (command->ml_submenu != NULLMENU) {
308             /* Else see if it is a submenu */
309             quitflag = Do_menu(command->ml_submenu, argc, argv);
310         }
311         else {
312             /* If it's got neither, something is wrong */
313             Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
314         }
315         if (quitflag == DM_QUIT) {
316             if (cur_ms != NULLMS) {
317                 cur_ms = old_cur_ms;
318                 destroy_ms(my_ms);
319             }
320             if (m->m_exit != NULLFUNC)
321                 m->m_exit(m);
322             return (DM_QUIT);
323         }
324     punt_command:
325         ;
326     }
327 }
328
329 /* Prompt the user for input in the input window of cur_ms */
330 int Prompt_input(prompt, buf, buflen)
331     char *prompt;
332     char *buf;
333     int buflen;
334 {
335     int c;
336     char *p;
337     int y, x, oldx;
338
339     if (cur_ms != NULLMS) {
340         more_flg = 1;
341         getyx(cur_ms->ms_input, y, x);
342         (void) wmove(cur_ms->ms_input, y, 0);
343
344         touchwin(cur_ms->ms_screen);
345         refresh_ms(cur_ms);
346         (void) waddstr(cur_ms->ms_input, prompt);
347         getyx(cur_ms->ms_input, y, x);
348
349         oldx = x;
350         p = buf;
351         while(1) {
352             (void) wmove(cur_ms->ms_input, y, x);
353             (void) wclrtoeol(cur_ms->ms_input);
354             refresh_ms(cur_ms);
355             c = getchar();
356             switch (c) {
357             case CTL('C'):
358                 return 0;
359             case CTL('Z'):
360                 (void) kill(getpid(), SIGTSTP);
361                 touchwin(curscr);
362                 break;
363             case CTL('L'):
364                 (void) wclear(cur_ms->ms_input);
365                 (void) waddstr(cur_ms->ms_input, prompt);
366                 (void) wrefresh(curscr);
367                 getyx(cur_ms->ms_input, y, x);
368                 break;
369
370             case '\n':
371             case '\r':
372                 goto end_input;
373                 /* these should be obtained by doing ioctl() on tty */
374             case '\b':
375             case '\177':
376                 if (p > buf) {
377                     p--;
378                     x--;
379                 }
380                 break;
381             case CTL('U'):
382             case CTL('G'):
383             case CTL('['):
384                 x = oldx;
385                 p = buf;
386                 break;
387             default:
388                 if (isprint(c) && (p - buf < buflen)) {
389                     (void) waddch(cur_ms->ms_input, c);
390                     *p++ = c;
391                     x++;
392                 } else
393                     putchar(CTL('G'));
394                 break;
395             }
396         }
397     end_input:
398         (void) waddch(cur_ms->ms_input, '\n');
399         (void) waddch(cur_ms->ms_input, '\r');
400         
401         (void) wclrtoeol(cur_ms->ms_input);
402         refresh_ms(cur_ms);
403         *p = '\0';
404         Start_paging();
405         goto gotit;
406     } else {
407         printf("%s", prompt);
408         if (gets(buf) == NULL)
409             return 0;
410         Start_paging();
411         goto gotit;
412     }
413 gotit:
414     strcpy(buf, strtrim(buf));
415     return 1;
416 }
417
418 /* Prompt the user for input in the input window of cur_ms, but don't echo
419    and allow some control characters */
420 int Password_input(prompt, buf, buflen)
421     char *prompt;
422     char *buf;
423     int buflen;
424 {
425     int c;
426     char *p;
427     int y, x, oldx;
428
429     if (cur_ms != NULLMS) {
430         more_flg = 1;
431         getyx(cur_ms->ms_input, y, x);
432         (void) wmove(cur_ms->ms_input, y, 0);
433
434         touchwin(cur_ms->ms_screen);
435         refresh_ms(cur_ms);
436         (void) waddstr(cur_ms->ms_input, prompt);
437         getyx(cur_ms->ms_input, y, x);
438
439         oldx = x;
440         for (p = buf; p - buf < buflen;) {
441             (void) wmove(cur_ms->ms_input, y, x);
442             (void) wclrtoeol(cur_ms->ms_input);
443             refresh_ms(cur_ms);
444             c = getchar();
445             switch (c) {
446             case CTL('C'):
447                 return 0;
448             case CTL('Z'):
449                 (void) kill(getpid(), SIGTSTP);
450                 touchwin(curscr);
451                 break;
452             case CTL('L'):
453                 (void) wclear(cur_ms->ms_input);
454                 (void) waddstr(cur_ms->ms_input, prompt);
455                 (void) wrefresh(curscr);
456                 getyx(cur_ms->ms_input, y, x);
457                 break;
458             case '\n':
459             case '\r':
460                 (void) waddch(cur_ms->ms_input, '\n');
461                 (void) waddch(cur_ms->ms_input, '\r');
462
463                 (void) wclrtoeol(cur_ms->ms_input);
464                 refresh_ms(cur_ms);
465                 *p = '\0';
466                 Start_paging();
467                 return 1;
468             case '\b':
469             case '\177':
470                 if (p > buf) {
471                     p--;
472                     x--;
473                 }
474                 break;
475             case CTL('U'):
476                 x = oldx;
477                 p = buf;
478                 break;
479             default:
480                 *p++ = c;
481                 break;
482             }
483         }
484     }
485     else {
486         struct sgttyb ttybuf, nttybuf;
487         printf("%s", prompt);
488         /* turn off echoing */
489         (void) ioctl(0, TIOCGETP, (char *)&ttybuf);
490         nttybuf = ttybuf;
491         nttybuf.sg_flags &= ~ECHO;
492         (void)ioctl(0, TIOCSETP, (char *)&nttybuf);
493         if (gets(buf) == NULL) {
494             (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
495             putchar('\n');
496             return 0;
497         }
498         putchar('\n');
499         (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
500         Start_paging();
501         return 1;
502     }
503     return 0;
504 }
505
506 int lines_left;
507
508 /* Start paging */
509 /* This routine will cause the most recently put message to be the
510    one at the top of the screen when a ---More--- prompt is displayed */
511 Start_paging()
512 {
513     if (cur_ms != NULLMS) {
514         lines_left = LINES - cur_ms->ms_input_y - 1;
515     }
516     else {
517         lines_left = 23;
518     }
519 }
520
521 /* Turn off paging */
522 Stop_paging()
523 {
524     lines_left = -1;
525 }
526
527 /* Print a message in the input window of cur_ms.  */
528 Put_message(msg)
529 char *msg;
530 {
531     char *copy, *line, *s;
532
533     copy = (char *) malloc((u_int)COLS);
534     s = line = msg;
535     while(*s++) {
536         if (s - line >= COLS-1) {
537             (void) strncpy(copy, line, COLS-1);
538             line += COLS-1;
539         } else if (*s == '\n') {
540             *s = '\0';
541             (void) strcpy(copy, line);
542             line = ++s;
543         } else
544             continue;
545         Put_line(copy);
546     }
547     Put_line(line);
548     free(copy);
549 }
550
551 /* Will be truncated to COLS characters.  */
552 Put_line(msg)
553 char *msg;
554 {
555     int y, x, i;
556     char *msg1, chr;
557
558     if (!more_flg)
559         return;
560
561     if (lines_left >= 0) {
562         if (--lines_left == 0) {
563             /* Give the user a more prompt */
564             if (cur_ms != NULLMS) {
565                 (void) wstandout(cur_ms->ms_input);
566                 (void) wprintw(cur_ms->ms_input, "---More---");
567                 (void) wstandend(cur_ms->ms_input);
568                 refresh_ms(cur_ms);
569                 chr = getchar();/* We do care what it is */
570                 if (chr == 'q' || chr == 'Q') {
571                     more_flg = 0;
572                     return;
573                 }
574                 getyx(cur_ms->ms_input, y, x);
575                 /* x is a bitbucket; avoid lint problems */
576                 x=x;
577                 (void) wmove(cur_ms->ms_input, y, 0);
578                 (void) wclrtoeol(cur_ms->ms_input);
579             }
580             else {
581                 printf("---More (hit return)---");
582                 (void) getchar();
583             }
584             Start_paging();     /* Reset lines_left */
585         }
586     }
587
588     if (cur_ms != NULLMS) {
589         msg1 = (char *) calloc((u_int)COLS, 1);
590         (void) strncpy(msg1, msg, COLS-1);
591         for (i = strlen(msg1); i < COLS - 1; i++)
592             msg1[i] = ' ';
593         (void) wprintw(cur_ms->ms_input, "%s\n", msg1);
594 /*      refresh_ms(cur_ms); */
595     }
596     else {
597         puts(msg);
598     }
599 }
600
601 /* Refresh a menu_screen onto the real screen */
602 refresh_ms(ms)
603     struct menu_screen *ms;
604 {
605     int y, x;
606
607     getyx(ms->ms_input, y, x);
608     (void) wmove(ms->ms_screen, y + ms->ms_input_y, x);
609     (void) wrefresh(ms->ms_screen);
610 }
611
612 /* Parse buf into a list of words, which will be placed in strings specified by
613    argv.  Space for these strings must have already been allocated.
614    Only the first n characters of each word will be copied */
615 Parse_words(buf, argv, n)
616     char *buf;
617     char *argv[];
618 int n;
619
620 {
621     char *start, *end;          /* For sausage machine */
622     int argc;
623
624     start = buf;
625     for (argc = 0; argc < MAX_ARGC; argc++) {
626         while (isspace(*start))
627             start++;            /* Kill whitespace */
628         if (*start == '\0')
629             break;              /* Nothing left */
630         /* Now find the end of the word */
631         for (end = start; *end != '\0' && !isspace(*end); end++);
632         (void) strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
633         argv[argc][MIN(end - start, n - 1)] = '\0';     /* Terminate */
634         start = end;
635     }
636     return (argc);
637 }
638
639 /* This is the internal form of Find_command, which recursively searches
640    for a menu_line with command command in the specified menu */
641 /* It will search to a maximum depth of d */
642 struct menu_line *
643 find_command_from(c, m, d)
644     char *c;
645     struct menu *m;
646     int d;
647 {
648     int line;
649     struct menu_line *maybe;
650
651     if (d < 0)
652         return ((struct menu_line *) 0);        /* Too deep! */
653     for (line = 0; line < m->m_length; line++) {
654         if (!strcmp(c, m->m_lines[line].ml_command)) {
655             return (&m->m_lines[line]);
656         }
657         else if (m->m_lines[line].ml_submenu != NULLMENU
658                  && (maybe =
659                    find_command_from(c, m->m_lines[line].ml_submenu, d - 1))
660                  != (struct menu_line *) 0) {
661             return (maybe);
662         }
663     }
664     /* If we got to here, nothing works */
665     return ((struct menu_line *) 0);
666 }
667
668 /* Find_command searches down the current menu tree */
669 /* And returns a pointer to a menu_line with the specified command name */
670 /* It returns (struct menu_line *) 0 if none is found */
671 struct menu_line *
672 Find_command(command)
673     char *command;
674 {
675     if (top_menu == NULLMENU) {
676         return ((struct menu_line *) 0);
677     }
678     else {
679         return (find_command_from(command, top_menu, MAX_MENU_DEPTH));
680     }
681 }
682
683 /*
684  * Local Variables:
685  * mode: c
686  * c-indent-level: 4
687  * c-continued-statement-offset: 4
688  * c-brace-offset: -4
689  * c-argdecl-indent: 4
690  * c-label-offset: -4
691  * End:
692  */
This page took 0.078327 seconds and 3 git commands to generate.