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();
58 extern char *calloc();
61 FILE *log_file = (FILE *) NULL; /* file stream of log file */
65 extern char *malloc();
68 /* Structure for holding current displayed menu */
70 WINDOW *ms_screen; /* Window for this menu */
71 WINDOW *ms_title; /* Title subwindow */
72 WINDOW *ms_menu; /* Menu subwindow */
73 WINDOW *ms_input; /* Input subwindow */
74 int ms_input_y; /* Input subwindow reference coordinate */
77 #define NULLMS ((struct menu_screen *) 0)
79 Menu *top_menu; /* Root for command search */
80 int parsed_argc; /* used by extern routines to get additional */
81 char **parsed_argv; /* comand line input */
84 * Hook function to cause error messages to be printed through
85 * curses instead of around it. Takes at most 5 args to the
86 * printf string (crock...)
90 menu_com_err_hook(who, code, fmt, arg1, arg2, arg3, arg4, arg5)
94 caddr_t arg1, arg2, arg3, arg4, arg5;
96 char buf[BUFSIZ], *cp;
98 (void) strcpy(buf, who);
99 for (cp = buf; *cp; cp++);
103 (void) strcpy(cp, error_message(code));
107 sprintf(cp, fmt, arg1, arg2, arg3, arg4, arg5);
112 * Start_menu takes a menu as an argument. It initializes curses u.s.w.,
113 * and a quit in any submenu should unwind back to here. (it might not,
114 * if user functions which run their own menus don't cooperate.)
115 * Start_menu should only be called once, at the start of the program.
120 struct menu_screen *make_ms();
122 register void (*old_hook)(const char *, long, const char *, va_list) = set_com_err_hook(menu_com_err_hook);
124 register void (*old_hook)() = set_com_err_hook(menu_com_err_hook);
127 if (initscr() == (WINDOW *)ERR) {
128 fputs("Can't initialize curses!\n", stderr);
131 (void) raw(); /* We parse & print everything ourselves */
133 cur_ms = make_ms(0); /* So we always have some current */
136 (void) Do_menu(m, -1, (char **) NULL);
138 (void) set_com_err_hook(old_hook);
145 (void) wclear(cur_ms->ms_screen);
146 (void) wrefresh(cur_ms->ms_screen);
152 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
159 (void) Do_menu(m, -1, (char **) NULL);
163 * Create a new menu screen template with the specified menu length
170 struct menu_screen *ms;
172 if (MAX_TITLE + length + MIN_INPUT > LINES) {
173 fputs("Menu too big!\n", stderr);
177 ms = (struct menu_screen *) malloc(sizeof(struct menu_screen));
179 ms->ms_screen = newwin(0, 0, 0, 0);
180 ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
181 ms->ms_menu = subwin(ms->ms_screen,
182 length, 0, MAX_TITLE, 0);
183 ms->ms_input = subwin(ms->ms_screen, 0, 0,
184 ms->ms_input_y = MAX_TITLE + length,
187 scrollok(ms->ms_input, TRUE);
188 (void) wmove(ms->ms_input, 0, 0);
189 (void) wclear(ms->ms_screen);
195 * This routine destroys a menu_screen.
198 struct menu_screen *ms;
200 delwin(ms->ms_title);
202 delwin(ms->ms_input);
203 delwin(ms->ms_screen);
208 * This guy actually puts up the menu
209 * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
213 Do_menu(m, margc, margv)
218 struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
219 char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
220 char buf[MAX_ARGC * MAX_ARGLEN];
221 char *argv[MAX_ARGC];
224 struct menu_line *command, *Find_command();
226 int quitflag, is_topmenu = (margc < 0);
227 int toggle_logging();
229 /* Entry function gets called with old menu_screen still current */
230 if (m->m_entry != NULLFUNC) {
231 if (m->m_entry(m, margc, margv) == DM_QUIT)
233 if (parsed_argc > 0) {
234 margc = parsed_argc + 1;
235 margv = --parsed_argv;
244 /* The following get run only in curses mode */
245 if (cur_ms != NULLMS) {
246 /* Get a menu_screen */
248 /* 2 is for the 2 obligatory lines; quit and toggle */
249 cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
251 /* Now print the title and the menu */
252 (void) wclear(my_ms->ms_menu);
253 (void) wmove(my_ms->ms_title, 0, MAX(0, (COLS -
254 strlen(m->m_title)) >> 1));
255 (void) wstandout(my_ms->ms_title);
256 (void) waddstr(my_ms->ms_title, m->m_title);
257 (void) wstandend(my_ms->ms_title);
259 for (line = 0; line < m->m_length; line++) {
260 int len = strlen(m->m_lines[line].ml_command);
261 if (len > 12) len=12;
263 (void) wmove(my_ms->ms_menu, line, 0);
265 (void) wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
266 m->m_lines[line].ml_command,
268 m->m_lines[line].ml_doc);
270 (void) wmove(my_ms->ms_menu, line++, 0);
272 (void) waddstr(my_ms->ms_menu,
273 " r. (return) Return to previous menu.");
274 (void) wmove(my_ms->ms_menu, line++, 0);
276 (void) waddstr(my_ms->ms_menu,
277 " t. (toggle) Toggle logging on and off.");
278 (void) wmove(my_ms->ms_menu, line, 0);
279 (void) waddstr(my_ms->ms_menu, " q. (quit) Quit.");
281 Put_message(m->m_title);
282 for (line = 0; line < m->m_length; line++) {
283 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
284 m->m_lines[line].ml_command,
285 12 - strlen(m->m_lines[line].ml_command), "",
286 m->m_lines[line].ml_doc);
290 Put_message(" r. (return) Return to previous menu.");
291 Put_message(" t. (toggle) Toggle logging on and off.");
292 Put_message(" q. (quit) Quit.");
293 Put_message(" ?. Print this information.");
297 /* This will be set by a return val from func or submenu */
298 quitflag = DM_NORMAL;
299 /* This is here because we may be coming from another menu */
301 touchwin(my_ms->ms_screen);
303 /* Initialize argv */
304 for (argc = 0; argc < MAX_ARGC; argc++)
305 argv[argc] = argvals[argc];
307 for (i = 1; i < margc; i++)
308 strcpy(argvals[i - 1], margv[i]);
312 if (!Prompt_input("Command: ", buf, sizeof(buf))) {
313 if (cur_ms == NULLMS &&
315 sprintf(buf, "quit");
319 /* Parse it into the argument list */
320 /* If there's nothing there, try again */
321 /* Initialize argv */
322 for (argc = 0; argc < MAX_ARGC; argc++)
323 argv[argc] = argvals[argc];
325 if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
328 if ((line = atoi(argv[0])) > 0 && line <= m->m_length) {
329 command = &m->m_lines[line - 1];
331 else if ((!is_topmenu &&
332 (!strcmp(argv[0], "r") || !strcmp(argv[0], "return")))
333 || !strcmp(argv[0], "q") || !strcmp(argv[0], "quit")) {
335 /* here if it's either return or quit */
336 if (cur_ms != NULLMS) {
340 if (m->m_exit != NULLFUNC)
342 return (*argv[0] == 'r' ? DM_NORMAL : DM_QUIT);
344 else if (argv[0][0] == '?') {
345 for (line = 0; line < m->m_length; line++) {
346 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
347 m->m_lines[line].ml_command,
348 12 - strlen(m->m_lines[line].ml_command), "",
349 m->m_lines[line].ml_doc);
353 Put_message(" r. (return) Return to previous menu.");
354 Put_message(" t. (toggle) Toggle logging on and off.");
355 Put_message(" q. (quit) Quit.");
358 else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle")) {
359 toggle_logging(argc, argv);
362 /* finally, try to find it using Find_command */
363 else if ((command = Find_command(m, argvals[0])) ==
364 (struct menu_line *) 0) {
365 sprintf(buf, "Command not recognized: %s\n", argvals[0]);
369 /* If we got to here, command is a valid menu_line */
370 /* Send the offical command name into the argv */
371 (void) strcpy(argvals[0], command->ml_command);
372 /* Show that we're working on it */
373 Put_message(command->ml_doc);
374 /* Print args that we've already got */
375 for (i = 1; i < argc; i++) {
376 if (command->ml_args[i].ma_prompt == NULL)
378 (void) sprintf(buf, "%s%s", command->ml_args[i].ma_prompt,
382 /* Get remaining arguments, if any */
383 for (; argc < command->ml_argc; argc++) {
384 if (!Prompt_input(command->ml_args[argc].ma_prompt,
385 argvals[argc], sizeof(argvals[argc])))
388 parsed_argc = argc - command->ml_argc;
389 parsed_argv = &(argv[command->ml_argc]);
390 if (command->ml_function != NULLFUNC) {
391 /* If it's got a function, call it */
392 quitflag = command->ml_function(argc, argv);
394 else if (command->ml_submenu != NULLMENU) {
395 /* Else see if it is a submenu */
396 quitflag = Do_menu(command->ml_submenu, argc, argv);
399 /* If it's got neither, something is wrong */
400 Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
402 if (quitflag == DM_QUIT) {
403 if (cur_ms != NULLMS) {
407 if (m->m_exit != NULLFUNC)
419 if (cur_ms != NULLMS) {
420 touchwin(cur_ms->ms_screen);
426 /* Prompt the user for input in the input window of cur_ms */
427 int Prompt_input(prompt, buf, buflen)
434 int y, x, oldx, oldy;
436 if (cur_ms != NULLMS) {
438 getyx(cur_ms->ms_input, y, x);
439 (void) wmove(cur_ms->ms_input, y, 0);
442 (void) waddstr(cur_ms->ms_input, prompt);
443 getyx(cur_ms->ms_input, y, x);
449 (void) wmove(cur_ms->ms_input, y, x);
450 (void) touchwin(cur_ms->ms_screen);
451 (void) wclrtoeol(cur_ms->ms_input);
452 (void) wrefresh(cur_ms->ms_input);
453 c = getchar() & 0x7f;
459 (void) kill(getpid(), SIGTSTP);
463 (void) wclear(cur_ms->ms_input);
464 (void) waddstr(cur_ms->ms_input, prompt);
465 (void) touchwin(cur_ms->ms_input);
466 (void) move(LINES - 1, 0);
467 (void) wrefresh(curscr);
468 getyx(cur_ms->ms_input, y, x);
477 /* these should be obtained by doing ioctl() on tty */
484 (void) wmove(cur_ms->ms_input, y, 0);
485 (void) wclrtoeol(cur_ms->ms_input);
487 x = cur_ms->ms_input->_maxx-1;
499 /* (buflen - 1) leaves room for the \0 */
500 if (isprint(c) && (p - buf < buflen - 1)) {
501 (void) waddch(cur_ms->ms_input, c);
504 if (x >= cur_ms->ms_input->_maxx) {
514 (void) waddch(cur_ms->ms_input, '\n');
516 (void) wclrtoeol(cur_ms->ms_input);
522 printf("%s", prompt);
523 if (gets(buf) == NULL)
533 strcpy(buf, strtrim(buf));
537 /* Prompt the user for input in the input window of cur_ms, but don't echo
538 and allow some control characters */
539 int Password_input(prompt, buf, buflen)
548 if (cur_ms != NULLMS) {
550 getyx(cur_ms->ms_input, y, x);
551 (void) wmove(cur_ms->ms_input, y, 0);
553 touchwin(cur_ms->ms_screen);
555 (void) waddstr(cur_ms->ms_input, prompt);
556 getyx(cur_ms->ms_input, y, x);
559 for (p = buf; p - buf < buflen;) {
560 (void) wmove(cur_ms->ms_input, y, x);
561 (void) wclrtoeol(cur_ms->ms_input);
563 c = getchar() & 0x7f;
568 (void) kill(getpid(), SIGTSTP);
572 (void) wclear(cur_ms->ms_input);
573 (void) waddstr(cur_ms->ms_input, prompt);
575 (void) move(LINES - 1, 0);
576 (void) wrefresh(curscr);
577 getyx(cur_ms->ms_input, y, x);
581 (void) waddch(cur_ms->ms_input, '\n');
583 (void) wclrtoeol(cur_ms->ms_input);
607 struct termios ttybuf, nttybuf;
609 struct sgttyb ttybuf, nttybuf;
611 printf("%s", prompt);
612 /* turn off echoing */
614 tcgetattr(0, &ttybuf);
616 nttybuf.c_lflag &= ~ECHO;
617 tcsetattr(0, TCSANOW, &nttybuf);
618 if (gets(buf) == NULL) {
619 tcsetattr(0, TCSANOW, &ttybuf);
624 (void) ioctl(0, TCSETA, (char *)&ttybuf);
626 (void) ioctl(0, TIOCGETP, (char *)&ttybuf);
628 nttybuf.sg_flags &= ~ECHO;
629 (void)ioctl(0, TIOCSETP, (char *)&nttybuf);
630 if (gets(buf) == NULL) {
631 (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
636 (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
647 /* This routine will cause the most recently put message to be the
648 one at the top of the screen when a ---More--- prompt is displayed */
651 if (cur_ms != NULLMS) {
652 lines_left = LINES - cur_ms->ms_input_y - 1;
659 /* Turn off paging */
665 /* Print a message in the input window of cur_ms. */
669 char *copy, *line, *s;
671 copy = (char *) malloc((u_int)COLS);
673 if (log_file) { /* if we're doing logging; we assume that the */
674 /* file has already been opened. */
675 (void) fprintf(log_file, "%s\n", msg);
680 if (s - line >= COLS-1) {
681 (void) strncpy(copy, line, COLS-1);
683 } else if (*s == '\n') {
685 (void) strcpy(copy, line);
695 /* Will be truncated to COLS characters. */
705 if (lines_left >= 0) {
706 if (--lines_left == 0) {
707 /* Give the user a more prompt */
708 if (cur_ms != NULLMS) {
709 (void) wstandout(cur_ms->ms_input);
710 (void) wprintw(cur_ms->ms_input, "---More---");
711 (void) wstandend(cur_ms->ms_input);
713 chr = getchar() & 0x7f;/* We do care what it is */
714 if (chr == 'q' || chr == 'Q' || chr == 3 /* ^C */) {
718 getyx(cur_ms->ms_input, y, x);
719 /* x is a bitbucket; avoid lint problems */
721 (void) wmove(cur_ms->ms_input, y, 0);
722 (void) wclrtoeol(cur_ms->ms_input);
725 printf("---More (hit return)---");
728 Start_paging(); /* Reset lines_left */
732 if (cur_ms != NULLMS) {
733 msg1 = (char *) calloc((u_int)COLS, 1);
734 (void) strncpy(msg1, msg, COLS-1);
735 for (i = strlen(msg1); i < COLS - 1; i++)
737 (void) wprintw(cur_ms->ms_input, "%s\n", msg1);
744 /* Refresh a menu_screen onto the real screen */
746 struct menu_screen *ms;
748 (void) wrefresh(ms->ms_title);
749 (void) wrefresh(ms->ms_menu);
750 (void) wrefresh(ms->ms_input);
753 /* Parse buf into a list of words, which will be placed in strings specified by
754 argv. Space for these strings must have already been allocated.
755 Only the first n characters of each word will be copied */
756 Parse_words(buf, argv, n)
762 char *start, *end; /* For sausage machine */
766 for (argc = 0; argc < MAX_ARGC; argc++) {
767 while (isspace(*start))
768 start++; /* Kill whitespace */
770 break; /* Nothing left */
771 /* Now find the end of the word */
772 for (end = start; *end != '\0' && !isspace(*end); end++);
773 (void) strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
774 argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */
780 /* This is the internal form of Find_command, which recursively searches
781 for a menu_line with command command in the specified menu */
782 /* It will search to a maximum depth of d */
784 find_command_from(c, m, d)
790 struct menu_line *maybe;
793 return ((struct menu_line *) 0); /* Too deep! */
794 for (line = 0; line < m->m_length; line++) {
795 if (!strcmp(c, m->m_lines[line].ml_command)) {
796 return (&m->m_lines[line]);
799 for (line = 0; line < m->m_length; line++) {
800 if (m->m_lines[line].ml_submenu != NULLMENU &&
801 (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1))
802 != (struct menu_line *) 0) {
806 /* If we got to here, nothing works */
807 return ((struct menu_line *) 0);
810 /* Find_command searches down the current menu tree */
811 /* And returns a pointer to a menu_line with the specified command name */
812 /* It returns (struct menu_line *) 0 if none is found */
814 Find_command(m, command)
819 return ((struct menu_line *) 0);
822 return (find_command_from(command, m, MAX_MENU_DEPTH));
828 toggle_logging(argc, argv)
835 if (log_file == (FILE *) NULL) {
838 Put_message("I've lost my SENSE of DIRECTION! I have no IDEA who I AM!");
839 Put_message("My God... You've turned him into a DEMOCRAT!!");
840 Put_message(" -- Doonesbury");
842 Put_message("translation: your log file can be found in \"/usr/tmp/a.out.pid\".");
844 (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
847 (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
850 log_file = fopen(buf,"a");
852 if (log_file == (FILE *) NULL)
853 Put_message("Open of log file failed. Logging is not on.");
855 Put_message("Log file successfully opened.");
857 else { /* log_file is a valid pointer; turn off logging. */
858 (void) fflush(log_file);
859 (void) fclose(log_file);
860 log_file = (FILE *) NULL;
861 Put_message("Logging off.");