2 * Copyright 1987 by the Massachusetts Institute of Technology.
3 * For copying and distribution information, see the file
10 * Generic menu system module.
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.
16 * By making the menu descriptions so general, we can ease porting to just
21 static char rcsid_menu_c[] = "$Header$";
25 #include <mit-copyright.h>
26 #include <sys/types.h>
47 #define MAX(A,B) ((A) > (B) ? (A) : (B))
48 #define MIN(A,B) ((A) < (B) ? (A) : (B))
49 #define CTL(ch) ((ch) & 037)
51 #define MIN_INPUT 2 /* Minimum number of lines for input window */
53 extern int interrupt; /* will be set if ^C is received */
54 extern FILE *fdopen();
56 extern char *calloc();
59 FILE *log_file = (FILE *) NULL; /* file stream of log file */
63 extern char *malloc();
66 /* Structure for holding current displayed menu */
68 WINDOW *ms_screen; /* Window for this menu */
69 WINDOW *ms_title; /* Title subwindow */
70 WINDOW *ms_menu; /* Menu subwindow */
71 WINDOW *ms_input; /* Input subwindow */
72 int ms_input_y; /* Input subwindow reference coordinate */
75 #define NULLMS ((struct menu_screen *) 0)
77 Menu *top_menu; /* Root for command search */
78 int parsed_argc; /* used by extern routines to get additional */
79 char **parsed_argv; /* comand line input */
82 * Hook function to cause error messages to be printed through
83 * curses instead of around it. Takes at most 5 args to the
84 * printf string (crock...)
88 menu_com_err_hook(who, code, fmt, arg1, arg2, arg3, arg4, arg5)
92 caddr_t arg1, arg2, arg3, arg4, arg5;
94 char buf[BUFSIZ], *cp;
96 (void) strcpy(buf, who);
97 for (cp = buf; *cp; cp++);
101 (void) strcpy(cp, error_message(code));
105 sprintf(cp, fmt, arg1, arg2, arg3, arg4, arg5);
110 * Start_menu takes a menu as an argument. It initializes curses u.s.w.,
111 * and a quit in any submenu should unwind back to here. (it might not,
112 * if user functions which run their own menus don't cooperate.)
113 * Start_menu should only be called once, at the start of the program.
118 struct menu_screen *make_ms();
120 register void (*old_hook)(const char *, long, const char *, va_list) = set_com_err_hook(menu_com_err_hook);
122 register void (*old_hook)() = set_com_err_hook(menu_com_err_hook);
125 if (initscr() == (WINDOW *)ERR) {
126 fputs("Can't initialize curses!\n", stderr);
129 (void) raw(); /* We parse & print everything ourselves */
131 cur_ms = make_ms(0); /* So we always have some current */
134 (void) Do_menu(m, -1, (char **) NULL);
136 (void) set_com_err_hook(old_hook);
143 (void) wclear(cur_ms->ms_screen);
144 (void) wrefresh(cur_ms->ms_screen);
150 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
157 (void) Do_menu(m, -1, (char **) NULL);
161 * Create a new menu screen template with the specified menu length
168 struct menu_screen *ms;
170 if (MAX_TITLE + length + MIN_INPUT > LINES) {
171 fputs("Menu too big!\n", stderr);
175 ms = (struct menu_screen *) malloc(sizeof(struct menu_screen));
177 ms->ms_screen = newwin(0, 0, 0, 0);
178 ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
179 ms->ms_menu = subwin(ms->ms_screen,
180 length, 0, MAX_TITLE, 0);
181 ms->ms_input = subwin(ms->ms_screen, 0, 0,
182 ms->ms_input_y = MAX_TITLE + length,
185 scrollok(ms->ms_input, TRUE);
186 (void) wmove(ms->ms_input, 0, 0);
187 (void) wclear(ms->ms_screen);
193 * This routine destroys a menu_screen.
196 struct menu_screen *ms;
198 delwin(ms->ms_title);
200 delwin(ms->ms_input);
201 delwin(ms->ms_screen);
206 * This guy actually puts up the menu
207 * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
211 Do_menu(m, margc, margv)
216 struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
217 char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
218 char buf[MAX_ARGC * MAX_ARGLEN];
219 char *argv[MAX_ARGC];
222 struct menu_line *command, *Find_command();
224 int quitflag, is_topmenu = (margc < 0);
225 int toggle_logging();
227 /* Entry function gets called with old menu_screen still current */
228 if (m->m_entry != NULLFUNC) {
229 if (m->m_entry(m, margc, margv) == DM_QUIT)
231 if (parsed_argc > 0) {
232 margc = parsed_argc + 1;
233 margv = --parsed_argv;
242 /* The following get run only in curses mode */
243 if (cur_ms != NULLMS) {
244 /* Get a menu_screen */
246 /* 2 is for the 2 obligatory lines; quit and toggle */
247 cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
249 /* Now print the title and the menu */
250 (void) wclear(my_ms->ms_menu);
251 (void) wmove(my_ms->ms_title, 0, MAX(0, (COLS -
252 strlen(m->m_title)) >> 1));
253 (void) wstandout(my_ms->ms_title);
254 (void) waddstr(my_ms->ms_title, m->m_title);
255 (void) wstandend(my_ms->ms_title);
257 for (line = 0; line < m->m_length; line++) {
258 int len = strlen(m->m_lines[line].ml_command);
259 if (len > 12) len=12;
261 (void) wmove(my_ms->ms_menu, line, 0);
263 (void) wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
264 m->m_lines[line].ml_command,
266 m->m_lines[line].ml_doc);
268 (void) wmove(my_ms->ms_menu, line++, 0);
270 (void) waddstr(my_ms->ms_menu,
271 " r. (return) Return to previous menu.");
272 (void) wmove(my_ms->ms_menu, line++, 0);
274 (void) waddstr(my_ms->ms_menu,
275 " t. (toggle) Toggle logging on and off.");
276 (void) wmove(my_ms->ms_menu, line, 0);
277 (void) waddstr(my_ms->ms_menu, " q. (quit) Quit.");
279 Put_message(m->m_title);
280 for (line = 0; line < m->m_length; line++) {
281 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
282 m->m_lines[line].ml_command,
283 12 - strlen(m->m_lines[line].ml_command), "",
284 m->m_lines[line].ml_doc);
288 Put_message(" r. (return) Return to previous menu.");
289 Put_message(" t. (toggle) Toggle logging on and off.");
290 Put_message(" q. (quit) Quit.");
291 Put_message(" ?. Print this information.");
295 /* This will be set by a return val from func or submenu */
296 quitflag = DM_NORMAL;
297 /* This is here because we may be coming from another menu */
299 touchwin(my_ms->ms_screen);
301 /* Initialize argv */
302 for (argc = 0; argc < MAX_ARGC; argc++)
303 argv[argc] = argvals[argc];
305 for (i = 1; i < margc; i++)
306 strcpy(argvals[i - 1], margv[i]);
310 if (!Prompt_input("Command: ", buf, sizeof(buf))) {
311 if (cur_ms == NULLMS &&
313 sprintf(buf, "quit");
317 /* Parse it into the argument list */
318 /* If there's nothing there, try again */
319 /* Initialize argv */
320 for (argc = 0; argc < MAX_ARGC; argc++)
321 argv[argc] = argvals[argc];
323 if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
326 if ((line = atoi(argv[0])) > 0 && line <= m->m_length) {
327 command = &m->m_lines[line - 1];
329 else if ((!is_topmenu &&
330 (!strcmp(argv[0], "r") || !strcmp(argv[0], "return")))
331 || !strcmp(argv[0], "q") || !strcmp(argv[0], "quit")) {
333 /* here if it's either return or quit */
334 if (cur_ms != NULLMS) {
338 if (m->m_exit != NULLFUNC)
340 return (*argv[0] == 'r' ? DM_NORMAL : DM_QUIT);
342 else if (argv[0][0] == '?') {
343 for (line = 0; line < m->m_length; line++) {
344 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
345 m->m_lines[line].ml_command,
346 12 - strlen(m->m_lines[line].ml_command), "",
347 m->m_lines[line].ml_doc);
351 Put_message(" r. (return) Return to previous menu.");
352 Put_message(" t. (toggle) Toggle logging on and off.");
353 Put_message(" q. (quit) Quit.");
356 else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle")) {
357 toggle_logging(argc, argv);
360 /* finally, try to find it using Find_command */
361 else if ((command = Find_command(m, argvals[0])) ==
362 (struct menu_line *) 0) {
363 sprintf(buf, "Command not recognized: %s\n", argvals[0]);
367 /* If we got to here, command is a valid menu_line */
368 /* Send the offical command name into the argv */
369 (void) strcpy(argvals[0], command->ml_command);
370 /* Show that we're working on it */
371 Put_message(command->ml_doc);
372 /* Print args that we've already got */
373 for (i = 1; i < argc; i++) {
374 if (command->ml_args[i].ma_prompt == NULL)
376 (void) sprintf(buf, "%s%s", command->ml_args[i].ma_prompt,
380 /* Get remaining arguments, if any */
381 for (; argc < command->ml_argc; argc++) {
382 if (!Prompt_input(command->ml_args[argc].ma_prompt,
383 argvals[argc], sizeof(argvals[argc])))
386 parsed_argc = argc - command->ml_argc;
387 parsed_argv = &(argv[command->ml_argc]);
388 if (command->ml_function != NULLFUNC) {
389 /* If it's got a function, call it */
390 quitflag = command->ml_function(argc, argv);
392 else if (command->ml_submenu != NULLMENU) {
393 /* Else see if it is a submenu */
394 quitflag = Do_menu(command->ml_submenu, argc, argv);
397 /* If it's got neither, something is wrong */
398 Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
400 if (quitflag == DM_QUIT) {
401 if (cur_ms != NULLMS) {
405 if (m->m_exit != NULLFUNC)
417 if (cur_ms != NULLMS) {
418 touchwin(cur_ms->ms_screen);
424 /* Prompt the user for input in the input window of cur_ms */
425 int Prompt_input(prompt, buf, buflen)
432 int y, x, oldx, oldy;
434 if (cur_ms != NULLMS) {
436 getyx(cur_ms->ms_input, y, x);
437 (void) wmove(cur_ms->ms_input, y, 0);
440 (void) waddstr(cur_ms->ms_input, prompt);
441 getyx(cur_ms->ms_input, y, x);
447 (void) wmove(cur_ms->ms_input, y, x);
448 (void) touchwin(cur_ms->ms_screen);
449 (void) wclrtoeol(cur_ms->ms_input);
450 (void) wrefresh(cur_ms->ms_input);
451 c = getchar() & 0x7f;
457 (void) kill(getpid(), SIGTSTP);
461 (void) wclear(cur_ms->ms_input);
462 (void) waddstr(cur_ms->ms_input, prompt);
463 (void) touchwin(cur_ms->ms_input);
464 (void) move(LINES - 1, 0);
465 (void) wrefresh(curscr);
466 getyx(cur_ms->ms_input, y, x);
475 /* these should be obtained by doing ioctl() on tty */
482 (void) wmove(cur_ms->ms_input, y, 0);
483 (void) wclrtoeol(cur_ms->ms_input);
485 x = cur_ms->ms_input->_maxx-1;
497 /* (buflen - 1) leaves room for the \0 */
498 if (isprint(c) && (p - buf < buflen - 1)) {
499 (void) waddch(cur_ms->ms_input, c);
502 if (x >= cur_ms->ms_input->_maxx) {
512 (void) waddch(cur_ms->ms_input, '\n');
514 (void) wclrtoeol(cur_ms->ms_input);
520 printf("%s", prompt);
521 if (gets(buf) == NULL)
531 strcpy(buf, strtrim(buf));
535 /* Prompt the user for input in the input window of cur_ms, but don't echo
536 and allow some control characters */
537 int Password_input(prompt, buf, buflen)
546 if (cur_ms != NULLMS) {
548 getyx(cur_ms->ms_input, y, x);
549 (void) wmove(cur_ms->ms_input, y, 0);
551 touchwin(cur_ms->ms_screen);
553 (void) waddstr(cur_ms->ms_input, prompt);
554 getyx(cur_ms->ms_input, y, x);
557 for (p = buf; p - buf < buflen;) {
558 (void) wmove(cur_ms->ms_input, y, x);
559 (void) wclrtoeol(cur_ms->ms_input);
561 c = getchar() & 0x7f;
566 (void) kill(getpid(), SIGTSTP);
570 (void) wclear(cur_ms->ms_input);
571 (void) waddstr(cur_ms->ms_input, prompt);
573 (void) move(LINES - 1, 0);
574 (void) wrefresh(curscr);
575 getyx(cur_ms->ms_input, y, x);
579 (void) waddch(cur_ms->ms_input, '\n');
581 (void) wclrtoeol(cur_ms->ms_input);
605 struct termios ttybuf, nttybuf;
607 struct sgttyb ttybuf, nttybuf;
609 printf("%s", prompt);
610 /* turn off echoing */
612 tcgetattr(0, &ttybuf);
614 nttybuf.c_lflag &= ~ECHO;
615 tcsetattr(0, TCSANOW, &nttybuf);
616 if (gets(buf) == NULL) {
617 tcsetattr(0, TCSANOW, &ttybuf);
622 (void) ioctl(0, TCSETA, (char *)&ttybuf);
624 (void) ioctl(0, TIOCGETP, (char *)&ttybuf);
626 nttybuf.sg_flags &= ~ECHO;
627 (void)ioctl(0, TIOCSETP, (char *)&nttybuf);
628 if (gets(buf) == NULL) {
629 (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
634 (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
645 /* This routine will cause the most recently put message to be the
646 one at the top of the screen when a ---More--- prompt is displayed */
649 if (cur_ms != NULLMS) {
650 lines_left = LINES - cur_ms->ms_input_y - 1;
657 /* Turn off paging */
663 /* Print a message in the input window of cur_ms. */
667 char *copy, *line, *s;
669 copy = (char *) malloc((u_int)COLS);
671 if (log_file) { /* if we're doing logging; we assume that the */
672 /* file has already been opened. */
673 (void) fprintf(log_file, "%s\n", msg);
678 if (s - line >= COLS-1) {
679 (void) strncpy(copy, line, COLS-1);
681 } else if (*s == '\n') {
683 (void) strcpy(copy, line);
693 /* Will be truncated to COLS characters. */
703 if (lines_left >= 0) {
704 if (--lines_left == 0) {
705 /* Give the user a more prompt */
706 if (cur_ms != NULLMS) {
707 (void) wstandout(cur_ms->ms_input);
708 (void) wprintw(cur_ms->ms_input, "---More---");
709 (void) wstandend(cur_ms->ms_input);
711 chr = getchar() & 0x7f;/* We do care what it is */
712 if (chr == 'q' || chr == 'Q' || chr == 3 /* ^C */) {
716 getyx(cur_ms->ms_input, y, x);
717 /* x is a bitbucket; avoid lint problems */
719 (void) wmove(cur_ms->ms_input, y, 0);
720 (void) wclrtoeol(cur_ms->ms_input);
723 printf("---More (hit return)---");
726 Start_paging(); /* Reset lines_left */
730 if (cur_ms != NULLMS) {
731 msg1 = (char *) calloc((u_int)COLS, 1);
732 (void) strncpy(msg1, msg, COLS-1);
733 for (i = strlen(msg1); i < COLS - 1; i++)
735 (void) wprintw(cur_ms->ms_input, "%s\n", msg1);
742 /* Refresh a menu_screen onto the real screen */
744 struct menu_screen *ms;
746 (void) wrefresh(ms->ms_title);
747 (void) wrefresh(ms->ms_menu);
748 (void) wrefresh(ms->ms_input);
751 /* Parse buf into a list of words, which will be placed in strings specified by
752 argv. Space for these strings must have already been allocated.
753 Only the first n characters of each word will be copied */
754 Parse_words(buf, argv, n)
760 char *start, *end; /* For sausage machine */
764 for (argc = 0; argc < MAX_ARGC; argc++) {
765 while (isspace(*start))
766 start++; /* Kill whitespace */
768 break; /* Nothing left */
769 /* Now find the end of the word */
770 for (end = start; *end != '\0' && !isspace(*end); end++);
771 (void) strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
772 argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */
778 /* This is the internal form of Find_command, which recursively searches
779 for a menu_line with command command in the specified menu */
780 /* It will search to a maximum depth of d */
782 find_command_from(c, m, d)
788 struct menu_line *maybe;
791 return ((struct menu_line *) 0); /* Too deep! */
792 for (line = 0; line < m->m_length; line++) {
793 if (!strcmp(c, m->m_lines[line].ml_command)) {
794 return (&m->m_lines[line]);
797 for (line = 0; line < m->m_length; line++) {
798 if (m->m_lines[line].ml_submenu != NULLMENU &&
799 (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1))
800 != (struct menu_line *) 0) {
804 /* If we got to here, nothing works */
805 return ((struct menu_line *) 0);
808 /* Find_command searches down the current menu tree */
809 /* And returns a pointer to a menu_line with the specified command name */
810 /* It returns (struct menu_line *) 0 if none is found */
812 Find_command(m, command)
817 return ((struct menu_line *) 0);
820 return (find_command_from(command, m, MAX_MENU_DEPTH));
826 toggle_logging(argc, argv)
833 if (log_file == (FILE *) NULL) {
836 Put_message("I've lost my SENSE of DIRECTION! I have no IDEA who I AM!");
837 Put_message("My God... You've turned him into a DEMOCRAT!!");
838 Put_message(" -- Doonesbury");
840 Put_message("translation: your log file can be found in \"/usr/tmp/a.out.pid\".");
842 (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
845 (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
848 log_file = fopen(buf,"a");
850 if (log_file == (FILE *) NULL)
851 Put_message("Open of log file failed. Logging is not on.");
853 Put_message("Log file successfully opened.");
855 else { /* log_file is a valid pointer; turn off logging. */
856 (void) fflush(log_file);
857 (void) fclose(log_file);
858 log_file = (FILE *) NULL;
859 Put_message("Logging off.");