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>
45 #define MAX(A,B) ((A) > (B) ? (A) : (B))
46 #define MIN(A,B) ((A) < (B) ? (A) : (B))
47 #define CTL(ch) ((ch) & 037)
49 #define MIN_INPUT 2 /* Minimum number of lines for input window */
51 extern int interrupt; /* will be set if ^C is received */
52 extern FILE *fdopen();
54 extern char *calloc();
57 FILE *log_file = (FILE *) NULL; /* file stream of log file */
60 /* Structure for holding current displayed menu */
62 WINDOW *ms_screen; /* Window for this menu */
63 WINDOW *ms_title; /* Title subwindow */
64 WINDOW *ms_menu; /* Menu subwindow */
65 WINDOW *ms_input; /* Input subwindow */
66 int ms_input_y; /* Input subwindow reference coordinate */
69 #define NULLMS ((struct menu_screen *) 0)
71 Menu *top_menu; /* Root for command search */
72 int parsed_argc; /* used by extern routines to get additional */
73 char **parsed_argv; /* comand line input */
76 * Hook function to cause error messages to be printed through
77 * curses instead of around it. Takes at most 5 args to the
78 * printf string (crock...)
82 menu_com_err_hook(who, code, fmt, arg1, arg2, arg3, arg4, arg5)
86 caddr_t arg1, arg2, arg3, arg4, arg5;
88 char buf[BUFSIZ], *cp;
90 (void) strcpy(buf, who);
91 for (cp = buf; *cp; cp++);
95 (void) strcpy(cp, error_message(code));
99 sprintf(cp, fmt, arg1, arg2, arg3, arg4, arg5);
104 * Start_menu takes a menu as an argument. It initializes curses u.s.w.,
105 * and a quit in any submenu should unwind back to here. (it might not,
106 * if user functions which run their own menus don't cooperate.)
107 * Start_menu should only be called once, at the start of the program.
112 struct menu_screen *make_ms();
114 register void (*old_hook)(const char *, long, const char *, va_list) = set_com_err_hook(menu_com_err_hook);
116 register void (*old_hook)() = set_com_err_hook(menu_com_err_hook);
119 if (initscr() == (WINDOW *)ERR) {
120 fputs("Can't initialize curses!\n", stderr);
123 (void) raw(); /* We parse & print everything ourselves */
125 cur_ms = make_ms(0); /* So we always have some current */
128 (void) Do_menu(m, -1, (char **) NULL);
130 (void) set_com_err_hook(old_hook);
137 (void) wclear(cur_ms->ms_screen);
138 (void) wrefresh(cur_ms->ms_screen);
144 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
151 (void) Do_menu(m, -1, (char **) NULL);
155 * Create a new menu screen template with the specified menu length
162 struct menu_screen *ms;
165 if (MAX_TITLE + length + MIN_INPUT > LINES) {
166 fputs("Menu too big!\n", stderr);
170 ms = (struct menu_screen *) malloc(sizeof(struct menu_screen));
172 ms->ms_screen = newwin(0, 0, 0, 0);
173 ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
174 ms->ms_menu = subwin(ms->ms_screen,
175 length, 0, MAX_TITLE, 0);
176 ms->ms_input = subwin(ms->ms_screen, 0, 0,
177 ms->ms_input_y = MAX_TITLE + length,
180 scrollok(ms->ms_input, TRUE);
181 (void) wmove(ms->ms_input, 0, 0);
182 (void) wclear(ms->ms_screen);
188 * This routine destroys a menu_screen.
191 struct menu_screen *ms;
193 delwin(ms->ms_title);
195 delwin(ms->ms_input);
196 delwin(ms->ms_screen);
201 * This guy actually puts up the menu
202 * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
206 Do_menu(m, margc, margv)
211 struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
212 char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
213 char buf[MAX_ARGC * MAX_ARGLEN];
214 char *argv[MAX_ARGC];
217 struct menu_line *command, *Find_command();
219 int quitflag, is_topmenu = (margc < 0);
220 int toggle_logging();
222 /* Entry function gets called with old menu_screen still current */
223 if (m->m_entry != NULLFUNC) {
224 if (m->m_entry(m, margc, margv) == DM_QUIT)
226 if (parsed_argc > 0) {
227 margc = parsed_argc + 1;
228 margv = --parsed_argv;
237 /* The following get run only in curses mode */
238 if (cur_ms != NULLMS) {
239 /* Get a menu_screen */
241 /* 2 is for the 2 obligatory lines; quit and toggle */
242 cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
244 /* Now print the title and the menu */
245 (void) wclear(my_ms->ms_menu);
246 (void) wmove(my_ms->ms_title, 0, MAX(0, (COLS -
247 strlen(m->m_title)) >> 1));
248 (void) wstandout(my_ms->ms_title);
249 (void) waddstr(my_ms->ms_title, m->m_title);
250 (void) wstandend(my_ms->ms_title);
252 for (line = 0; line < m->m_length; line++) {
253 int len = strlen(m->m_lines[line].ml_command);
254 if (len > 12) len=12;
256 (void) wmove(my_ms->ms_menu, line, 0);
258 (void) wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
259 m->m_lines[line].ml_command,
261 m->m_lines[line].ml_doc);
263 (void) wmove(my_ms->ms_menu, line++, 0);
265 (void) waddstr(my_ms->ms_menu,
266 " r. (return) Return to previous menu.");
267 (void) wmove(my_ms->ms_menu, line++, 0);
269 (void) waddstr(my_ms->ms_menu,
270 " t. (toggle) Toggle logging on and off.");
271 (void) wmove(my_ms->ms_menu, line, 0);
272 (void) waddstr(my_ms->ms_menu, " q. (quit) Quit.");
274 Put_message(m->m_title);
275 for (line = 0; line < m->m_length; line++) {
276 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
277 m->m_lines[line].ml_command,
278 12 - strlen(m->m_lines[line].ml_command), "",
279 m->m_lines[line].ml_doc);
283 Put_message(" r. (return) Return to previous menu.");
284 Put_message(" t. (toggle) Toggle logging on and off.");
285 Put_message(" q. (quit) Quit.");
286 Put_message(" ?. Print this information.");
290 /* This will be set by a return val from func or submenu */
291 quitflag = DM_NORMAL;
292 /* This is here because we may be coming from another menu */
294 touchwin(my_ms->ms_screen);
296 /* Initialize argv */
297 for (argc = 0; argc < MAX_ARGC; argc++)
298 argv[argc] = argvals[argc];
300 for (i = 1; i < margc; i++)
301 strcpy(argvals[i - 1], margv[i]);
305 if (!Prompt_input("Command: ", buf, sizeof(buf))) {
306 if (cur_ms == NULLMS &&
308 sprintf(buf, "quit");
312 /* Parse it into the argument list */
313 /* If there's nothing there, try again */
314 /* Initialize argv */
315 for (argc = 0; argc < MAX_ARGC; argc++)
316 argv[argc] = argvals[argc];
318 if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
321 if ((line = atoi(argv[0])) > 0 && line <= m->m_length) {
322 command = &m->m_lines[line - 1];
324 else if ((!is_topmenu &&
325 (!strcmp(argv[0], "r") || !strcmp(argv[0], "return")))
326 || !strcmp(argv[0], "q") || !strcmp(argv[0], "quit")) {
328 /* here if it's either return or quit */
329 if (cur_ms != NULLMS) {
333 if (m->m_exit != NULLFUNC)
335 return (*argv[0] == 'r' ? DM_NORMAL : DM_QUIT);
337 else if (argv[0][0] == '?') {
338 for (line = 0; line < m->m_length; line++) {
339 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
340 m->m_lines[line].ml_command,
341 12 - strlen(m->m_lines[line].ml_command), "",
342 m->m_lines[line].ml_doc);
346 Put_message(" r. (return) Return to previous menu.");
347 Put_message(" t. (toggle) Toggle logging on and off.");
348 Put_message(" q. (quit) Quit.");
351 else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle")) {
352 toggle_logging(argc, argv);
355 /* finally, try to find it using Find_command */
356 else if ((command = Find_command(m, argvals[0])) ==
357 (struct menu_line *) 0) {
358 sprintf(buf, "Command not recognized: %s\n", argvals[0]);
362 /* If we got to here, command is a valid menu_line */
363 /* Send the offical command name into the argv */
364 (void) strcpy(argvals[0], command->ml_command);
365 /* Show that we're working on it */
366 Put_message(command->ml_doc);
367 /* Print args that we've already got */
368 for (i = 1; i < argc; i++) {
369 if (command->ml_args[i].ma_prompt == NULL)
371 (void) sprintf(buf, "%s%s", command->ml_args[i].ma_prompt,
375 /* Get remaining arguments, if any */
376 for (; argc < command->ml_argc; argc++) {
377 if (!Prompt_input(command->ml_args[argc].ma_prompt,
378 argvals[argc], sizeof(argvals[argc])))
381 parsed_argc = argc - command->ml_argc;
382 parsed_argv = &(argv[command->ml_argc]);
383 if (command->ml_function != NULLFUNC) {
384 /* If it's got a function, call it */
385 quitflag = command->ml_function(argc, argv);
387 else if (command->ml_submenu != NULLMENU) {
388 /* Else see if it is a submenu */
389 quitflag = Do_menu(command->ml_submenu, argc, argv);
392 /* If it's got neither, something is wrong */
393 Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
395 if (quitflag == DM_QUIT) {
396 if (cur_ms != NULLMS) {
400 if (m->m_exit != NULLFUNC)
412 if (cur_ms != NULLMS) {
413 touchwin(cur_ms->ms_screen);
419 /* Prompt the user for input in the input window of cur_ms */
420 int Prompt_input(prompt, buf, buflen)
427 int y, x, oldx, oldy;
429 if (cur_ms != NULLMS) {
431 getyx(cur_ms->ms_input, y, x);
432 (void) wmove(cur_ms->ms_input, y, 0);
435 (void) waddstr(cur_ms->ms_input, prompt);
436 getyx(cur_ms->ms_input, y, x);
442 (void) wmove(cur_ms->ms_input, y, x);
443 (void) touchwin(cur_ms->ms_screen);
444 (void) wclrtoeol(cur_ms->ms_input);
445 (void) wrefresh(cur_ms->ms_input);
446 c = getchar() & 0x7f;
452 (void) kill(getpid(), SIGTSTP);
456 (void) wclear(cur_ms->ms_input);
457 (void) waddstr(cur_ms->ms_input, prompt);
458 (void) touchwin(cur_ms->ms_input);
459 (void) move(LINES - 1, 0);
460 (void) wrefresh(curscr);
461 getyx(cur_ms->ms_input, y, x);
470 /* these should be obtained by doing ioctl() on tty */
477 (void) wmove(cur_ms->ms_input, y, 0);
478 (void) wclrtoeol(cur_ms->ms_input);
480 x = cur_ms->ms_input->_maxx-1;
492 /* (buflen - 1) leaves room for the \0 */
493 if (isprint(c) && (p - buf < buflen - 1)) {
494 (void) waddch(cur_ms->ms_input, c);
497 if (x >= cur_ms->ms_input->_maxx) {
507 (void) waddch(cur_ms->ms_input, '\n');
509 (void) wclrtoeol(cur_ms->ms_input);
515 printf("%s", prompt);
516 if (gets(buf) == NULL)
526 strcpy(buf, strtrim(buf));
530 /* Prompt the user for input in the input window of cur_ms, but don't echo
531 and allow some control characters */
532 int Password_input(prompt, buf, buflen)
541 if (cur_ms != NULLMS) {
543 getyx(cur_ms->ms_input, y, x);
544 (void) wmove(cur_ms->ms_input, y, 0);
546 touchwin(cur_ms->ms_screen);
548 (void) waddstr(cur_ms->ms_input, prompt);
549 getyx(cur_ms->ms_input, y, x);
552 for (p = buf; p - buf < buflen;) {
553 (void) wmove(cur_ms->ms_input, y, x);
554 (void) wclrtoeol(cur_ms->ms_input);
556 c = getchar() & 0x7f;
561 (void) kill(getpid(), SIGTSTP);
565 (void) wclear(cur_ms->ms_input);
566 (void) waddstr(cur_ms->ms_input, prompt);
568 (void) move(LINES - 1, 0);
569 (void) wrefresh(curscr);
570 getyx(cur_ms->ms_input, y, x);
574 (void) waddch(cur_ms->ms_input, '\n');
576 (void) wclrtoeol(cur_ms->ms_input);
600 struct termios ttybuf, nttybuf;
602 struct sgttyb ttybuf, nttybuf;
604 printf("%s", prompt);
605 /* turn off echoing */
607 tcgetattr(0, &ttybuf);
609 nttybuf.c_lflag &= ~ECHO;
610 tcsetattr(0, TCSANOW, &nttybuf);
611 if (gets(buf) == NULL) {
612 tcsetattr(0, TCSANOW, &ttybuf);
617 (void) ioctl(0, TCSETA, (char *)&ttybuf);
619 (void) ioctl(0, TIOCGETP, (char *)&ttybuf);
621 nttybuf.sg_flags &= ~ECHO;
622 (void)ioctl(0, TIOCSETP, (char *)&nttybuf);
623 if (gets(buf) == NULL) {
624 (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
629 (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
640 /* This routine will cause the most recently put message to be the
641 one at the top of the screen when a ---More--- prompt is displayed */
644 if (cur_ms != NULLMS) {
645 lines_left = LINES - cur_ms->ms_input_y - 1;
652 /* Turn off paging */
658 /* Print a message in the input window of cur_ms. */
662 char *copy, *line, *s;
664 copy = (char *) malloc((u_int)COLS);
666 if (log_file) { /* if we're doing logging; we assume that the */
667 /* file has already been opened. */
668 (void) fprintf(log_file, "%s\n", msg);
673 if (s - line >= COLS-1) {
674 (void) strncpy(copy, line, COLS-1);
676 } else if (*s == '\n') {
678 (void) strcpy(copy, line);
688 /* Will be truncated to COLS characters. */
698 if (lines_left >= 0) {
699 if (--lines_left == 0) {
700 /* Give the user a more prompt */
701 if (cur_ms != NULLMS) {
702 (void) wstandout(cur_ms->ms_input);
703 (void) wprintw(cur_ms->ms_input, "---More---");
704 (void) wstandend(cur_ms->ms_input);
706 chr = getchar() & 0x7f;/* We do care what it is */
707 if (chr == 'q' || chr == 'Q' || chr == 3 /* ^C */) {
711 getyx(cur_ms->ms_input, y, x);
712 /* x is a bitbucket; avoid lint problems */
714 (void) wmove(cur_ms->ms_input, y, 0);
715 (void) wclrtoeol(cur_ms->ms_input);
718 printf("---More (hit return)---");
721 Start_paging(); /* Reset lines_left */
725 if (cur_ms != NULLMS) {
726 msg1 = (char *) calloc((u_int)COLS, 1);
727 (void) strncpy(msg1, msg, COLS-1);
728 for (i = strlen(msg1); i < COLS - 1; i++)
730 (void) wprintw(cur_ms->ms_input, "%s\n", msg1);
737 /* Refresh a menu_screen onto the real screen */
739 struct menu_screen *ms;
741 (void) wrefresh(ms->ms_title);
742 (void) wrefresh(ms->ms_menu);
743 (void) wrefresh(ms->ms_input);
746 /* Parse buf into a list of words, which will be placed in strings specified by
747 argv. Space for these strings must have already been allocated.
748 Only the first n characters of each word will be copied */
749 Parse_words(buf, argv, n)
755 char *start, *end; /* For sausage machine */
759 for (argc = 0; argc < MAX_ARGC; argc++) {
760 while (isspace(*start))
761 start++; /* Kill whitespace */
763 break; /* Nothing left */
764 /* Now find the end of the word */
765 for (end = start; *end != '\0' && !isspace(*end); end++);
766 (void) strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
767 argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */
773 /* This is the internal form of Find_command, which recursively searches
774 for a menu_line with command command in the specified menu */
775 /* It will search to a maximum depth of d */
777 find_command_from(c, m, d)
783 struct menu_line *maybe;
786 return ((struct menu_line *) 0); /* Too deep! */
787 for (line = 0; line < m->m_length; line++) {
788 if (!strcmp(c, m->m_lines[line].ml_command)) {
789 return (&m->m_lines[line]);
792 for (line = 0; line < m->m_length; line++) {
793 if (m->m_lines[line].ml_submenu != NULLMENU &&
794 (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1))
795 != (struct menu_line *) 0) {
799 /* If we got to here, nothing works */
800 return ((struct menu_line *) 0);
803 /* Find_command searches down the current menu tree */
804 /* And returns a pointer to a menu_line with the specified command name */
805 /* It returns (struct menu_line *) 0 if none is found */
807 Find_command(m, command)
812 return ((struct menu_line *) 0);
815 return (find_command_from(command, m, MAX_MENU_DEPTH));
821 toggle_logging(argc, argv)
828 if (log_file == (FILE *) NULL) {
831 Put_message("I've lost my SENSE of DIRECTION! I have no IDEA who I AM!");
832 Put_message("My God... You've turned him into a DEMOCRAT!!");
833 Put_message(" -- Doonesbury");
835 Put_message("translation: your log file can be found in \"/usr/tmp/a.out.pid\".");
837 (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
840 (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
843 log_file = fopen(buf,"a");
845 if (log_file == (FILE *) NULL)
846 Put_message("Open of log file failed. Logging is not on.");
848 Put_message("Log file successfully opened.");
850 else { /* log_file is a valid pointer; turn off logging. */
851 (void) fflush(log_file);
852 (void) fclose(log_file);
853 log_file = (FILE *) NULL;
854 Put_message("Logging off.");