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>
40 #define MAX(A,B) ((A) > (B) ? (A) : (B))
41 #define MIN(A,B) ((A) < (B) ? (A) : (B))
42 #define CTL(ch) ((ch) & 037)
44 #define MIN_INPUT 2 /* Minimum number of lines for input window */
46 extern FILE *fdopen();
48 extern char *calloc();
51 FILE *log_file = (FILE *) NULL; /* file stream of log file */
54 /* Structure for holding current displayed menu */
56 WINDOW *ms_screen; /* Window for this menu */
57 WINDOW *ms_title; /* Title subwindow */
58 WINDOW *ms_menu; /* Menu subwindow */
59 WINDOW *ms_input; /* Input subwindow */
60 int ms_input_y; /* Input subwindow reference coordinate */
63 #define NULLMS ((struct menu_screen *) 0)
65 Menu *top_menu; /* Root for command search */
68 * Hook function to cause error messages to be printed through
69 * curses instead of around it.
73 menu_com_err_hook(who, code, fmt, args)
79 char buf[BUFSIZ], *cp;
83 (void) strcpy(buf, who);
84 for (cp = buf; *cp; cp++);
88 (void) strcpy(cp, error_message(code));
92 _strbuf._flag = _IOWRT + _IOSTRG;
94 _strbuf._cnt = BUFSIZ - (cp - buf);
95 _doprnt(fmt, args, &_strbuf);
96 (void) putc('\0', &_strbuf);
101 * Start_menu takes a menu as an argument. It initializes curses u.s.w.,
102 * and a quit in any submenu should unwind back to here. (it might not,
103 * if user functions which run their own menus don't cooperate.)
104 * Start_menu should only be called once, at the start of the program.
109 struct menu_screen *make_ms();
111 register void (*old_hook)(const char *, long, const char *, va_list) = set_com_err_hook(menu_com_err_hook);
113 register void (*old_hook)() = set_com_err_hook(menu_com_err_hook);
116 if (initscr() == ERR) {
117 fputs("Can't initialize curses!\n", stderr);
120 (void) raw(); /* We parse & print everything ourselves */
122 cur_ms = make_ms(0); /* So we always have some current */
125 (void) Do_menu(m, -1, (char **) NULL);
127 (void) set_com_err_hook(old_hook);
134 (void) wclear(cur_ms->ms_screen);
135 (void) wrefresh(cur_ms->ms_screen);
141 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
148 (void) Do_menu(m, -1, (char **) NULL);
152 * Create a new menu screen template with the specified menu length
159 struct menu_screen *ms;
162 if (MAX_TITLE + length + MIN_INPUT > LINES) {
163 fputs("Menu too big!\n", stderr);
167 ms = (struct menu_screen *) malloc(sizeof(struct menu_screen));
169 ms->ms_screen = newwin(0, 0, 0, 0);
170 ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
171 ms->ms_menu = subwin(ms->ms_screen,
172 length, 0, MAX_TITLE, 0);
173 ms->ms_input = subwin(ms->ms_screen, 0, 0,
174 ms->ms_input_y = MAX_TITLE + length,
177 scrollok(ms->ms_input, TRUE);
178 (void) wmove(ms->ms_input, 0, 0);
179 (void) wclear(ms->ms_screen);
185 * This routine destroys a menu_screen.
188 struct menu_screen *ms;
190 delwin(ms->ms_title);
192 delwin(ms->ms_input);
193 delwin(ms->ms_screen);
198 * This guy actually puts up the menu
199 * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
203 Do_menu(m, margc, margv)
208 struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
209 char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
210 char buf[MAX_ARGC * MAX_ARGLEN];
211 char *argv[MAX_ARGC];
214 struct menu_line *command, *Find_command();
216 int quitflag, is_topmenu = (margc < 0);
217 int toggle_logging();
219 /* Entry function gets called with old menu_screen still current */
220 if (m->m_entry != NULLFUNC)
221 if (m->m_entry(m, margc, margv) == DM_QUIT)
224 /* The following get run only in curses mode */
225 if (cur_ms != NULLMS) {
226 /* Get a menu_screen */
228 /* 2 is for the 2 obligatory lines; quit and toggle */
229 cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
231 /* Now print the title and the menu */
232 (void) wclear(my_ms->ms_menu);
233 (void) wmove(my_ms->ms_title, 0, MAX(0, (COLS -
234 strlen(m->m_title)) >> 1));
235 (void) wstandout(my_ms->ms_title);
236 (void) waddstr(my_ms->ms_title, m->m_title);
237 (void) wstandend(my_ms->ms_title);
239 for (line = 0; line < m->m_length; line++) {
240 int len = strlen(m->m_lines[line].ml_command);
241 if (len > 12) len=12;
243 (void) wmove(my_ms->ms_menu, line, 0);
245 (void) wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
246 m->m_lines[line].ml_command,
248 m->m_lines[line].ml_doc);
250 (void) wmove(my_ms->ms_menu, line++, 0);
252 (void) waddstr(my_ms->ms_menu,
253 " r. (return) Return to previous menu.");
254 (void) wmove(my_ms->ms_menu, line++, 0);
256 (void) waddstr(my_ms->ms_menu,
257 " t. (toggle) Toggle logging on and off.");
258 (void) wmove(my_ms->ms_menu, line, 0);
259 (void) waddstr(my_ms->ms_menu, " q. (quit) Quit.");
261 Put_message(m->m_title);
262 for (line = 0; line < m->m_length; line++) {
263 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
264 m->m_lines[line].ml_command,
265 12 - strlen(m->m_lines[line].ml_command), "",
266 m->m_lines[line].ml_doc);
270 Put_message(" r. (return) Return to previous menu.");
271 Put_message(" t. (toggle) Toggle logging on and off.");
272 Put_message(" q. (quit) Quit.");
273 Put_message(" ?. Print this information.");
277 /* This will be set by a return val from func or submenu */
278 quitflag = DM_NORMAL;
279 /* This is here because we may be coming from another menu */
281 touchwin(my_ms->ms_screen);
283 if (!Prompt_input("Command: ", buf, sizeof(buf)))
285 /* Parse it into the argument list */
286 /* If there's nothing there, try again */
287 /* Initialize argv */
288 for (argc = 0; argc < MAX_ARGC; argc++)
289 argv[argc] = argvals[argc];
291 if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
293 if ((line = atoi(argv[0])) > 0 && line <= m->m_length) {
294 command = &m->m_lines[line - 1];
296 else if ((!is_topmenu &&
297 (!strcmp(argv[0], "r") || !strcmp(argv[0], "return")))
298 || !strcmp(argv[0], "q") || !strcmp(argv[0], "quit")) {
300 /* here if it's either return or quit */
301 if (cur_ms != NULLMS) {
305 if (m->m_exit != NULLFUNC)
307 return (*argv[0] == 'r' ? DM_NORMAL : DM_QUIT);
309 else if (argv[0][0] == '?') {
310 for (line = 0; line < m->m_length; line++) {
311 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
312 m->m_lines[line].ml_command,
313 12 - strlen(m->m_lines[line].ml_command), "",
314 m->m_lines[line].ml_doc);
318 Put_message(" r. (return) Return to previous menu.");
319 Put_message(" t. (toggle) Toggle logging on and off.");
320 Put_message(" q. (quit) Quit.");
323 else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle")) {
324 toggle_logging(argc, argv);
327 /* finally, try to find it using Find_command */
328 else if ((command = Find_command(m, argvals[0])) ==
329 (struct menu_line *) 0) {
330 Put_message("Command not recognized");
333 /* If we got to here, command is a valid menu_line */
334 /* Send the offical command name into the argv */
335 (void) strcpy(argvals[0], command->ml_command);
336 /* Show that we're working on it */
337 Put_message(command->ml_doc);
338 /* Print args that we've already got */
339 for (i = 1; i < argc; i++) {
340 if (command->ml_args[i].ma_prompt == NULL)
342 (void) sprintf(buf, "%s%s", command->ml_args[i].ma_prompt,
346 /* Get remaining arguments, if any */
347 for (; argc < command->ml_argc; argc++) {
348 if (!Prompt_input(command->ml_args[argc].ma_prompt,
349 argvals[argc], sizeof(argvals[argc])))
352 if (command->ml_function != NULLFUNC) {
353 /* If it's got a function, call it */
354 quitflag = command->ml_function(argc, argv);
356 else if (command->ml_submenu != NULLMENU) {
357 /* Else see if it is a submenu */
358 quitflag = Do_menu(command->ml_submenu, argc, argv);
361 /* If it's got neither, something is wrong */
362 Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
364 if (quitflag == DM_QUIT) {
365 if (cur_ms != NULLMS) {
369 if (m->m_exit != NULLFUNC)
378 /* Prompt the user for input in the input window of cur_ms */
379 int Prompt_input(prompt, buf, buflen)
386 int y, x, oldx, oldy;
388 if (cur_ms != NULLMS) {
390 getyx(cur_ms->ms_input, y, x);
391 (void) wmove(cur_ms->ms_input, y, 0);
393 touchwin(cur_ms->ms_screen);
395 (void) waddstr(cur_ms->ms_input, prompt);
396 getyx(cur_ms->ms_input, y, x);
402 (void) wmove(cur_ms->ms_input, y, x);
403 (void) touchwin(cur_ms->ms_screen);
404 (void) wclrtoeol(cur_ms->ms_input);
406 c = getchar() & 0x7f;
412 (void) kill(getpid(), SIGTSTP);
416 (void) wclear(cur_ms->ms_input);
417 (void) waddstr(cur_ms->ms_input, prompt);
418 (void) touchwin(cur_ms->ms_screen);
422 getyx(cur_ms->ms_input, y, x);
431 /* these should be obtained by doing ioctl() on tty */
438 (void) wmove(cur_ms->ms_input, y, 0);
439 (void) wclrtoeol(cur_ms->ms_input);
441 x = cur_ms->ms_input->_maxx-1;
453 /* (buflen - 1) leaves room for the \0 */
454 if (isprint(c) && (p - buf < buflen - 1)) {
455 (void) waddch(cur_ms->ms_input, c);
458 if (x >= cur_ms->ms_input->_maxx) {
468 (void) waddch(cur_ms->ms_input, '\n');
469 (void) waddch(cur_ms->ms_input, '\r');
471 (void) wclrtoeol(cur_ms->ms_input);
477 printf("%s", prompt);
478 if (gets(buf) == NULL)
484 strcpy(buf, strtrim(buf));
488 /* Prompt the user for input in the input window of cur_ms, but don't echo
489 and allow some control characters */
490 int Password_input(prompt, buf, buflen)
499 if (cur_ms != NULLMS) {
501 getyx(cur_ms->ms_input, y, x);
502 (void) wmove(cur_ms->ms_input, y, 0);
504 touchwin(cur_ms->ms_screen);
506 (void) waddstr(cur_ms->ms_input, prompt);
507 getyx(cur_ms->ms_input, y, x);
510 for (p = buf; p - buf < buflen;) {
511 (void) wmove(cur_ms->ms_input, y, x);
512 (void) wclrtoeol(cur_ms->ms_input);
514 c = getchar() & 0x7f;
519 (void) kill(getpid(), SIGTSTP);
523 (void) wclear(cur_ms->ms_input);
524 (void) waddstr(cur_ms->ms_input, prompt);
526 getyx(cur_ms->ms_input, y, x);
530 (void) waddch(cur_ms->ms_input, '\n');
531 (void) waddch(cur_ms->ms_input, '\r');
533 (void) wclrtoeol(cur_ms->ms_input);
556 struct sgttyb ttybuf, nttybuf;
557 printf("%s", prompt);
558 /* turn off echoing */
559 (void) ioctl(0, TIOCGETP, (char *)&ttybuf);
561 nttybuf.sg_flags &= ~ECHO;
562 (void)ioctl(0, TIOCSETP, (char *)&nttybuf);
563 if (gets(buf) == NULL) {
564 (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
569 (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
579 /* This routine will cause the most recently put message to be the
580 one at the top of the screen when a ---More--- prompt is displayed */
583 if (cur_ms != NULLMS) {
584 lines_left = LINES - cur_ms->ms_input_y - 1;
591 /* Turn off paging */
597 /* Print a message in the input window of cur_ms. */
601 char *copy, *line, *s;
603 copy = (char *) malloc((u_int)COLS);
605 if (log_file) { /* if we're doing logging; we assume that the */
606 /* file has already been opened. */
607 (void) fprintf(log_file, "%s\n", msg);
612 if (s - line >= COLS-1) {
613 (void) strncpy(copy, line, COLS-1);
615 } else if (*s == '\n') {
617 (void) strcpy(copy, line);
627 /* Will be truncated to COLS characters. */
637 if (lines_left >= 0) {
638 if (--lines_left == 0) {
639 /* Give the user a more prompt */
640 if (cur_ms != NULLMS) {
641 (void) wstandout(cur_ms->ms_input);
642 (void) wprintw(cur_ms->ms_input, "---More---");
643 (void) wstandend(cur_ms->ms_input);
645 chr = getchar() & 0x7f;/* We do care what it is */
646 if (chr == 'q' || chr == 'Q') {
650 getyx(cur_ms->ms_input, y, x);
651 /* x is a bitbucket; avoid lint problems */
653 (void) wmove(cur_ms->ms_input, y, 0);
654 (void) wclrtoeol(cur_ms->ms_input);
657 printf("---More (hit return)---");
660 Start_paging(); /* Reset lines_left */
664 if (cur_ms != NULLMS) {
665 msg1 = (char *) calloc((u_int)COLS, 1);
666 (void) strncpy(msg1, msg, COLS-1);
667 for (i = strlen(msg1); i < COLS - 1; i++)
669 (void) wprintw(cur_ms->ms_input, "%s\n", msg1);
670 /* refresh_ms(cur_ms); */
677 /* Refresh a menu_screen onto the real screen */
679 struct menu_screen *ms;
683 getyx(ms->ms_input, y, x);
684 (void) wmove(ms->ms_screen, y + ms->ms_input_y, x);
685 (void) wrefresh(ms->ms_screen);
688 /* Parse buf into a list of words, which will be placed in strings specified by
689 argv. Space for these strings must have already been allocated.
690 Only the first n characters of each word will be copied */
691 Parse_words(buf, argv, n)
697 char *start, *end; /* For sausage machine */
701 for (argc = 0; argc < MAX_ARGC; argc++) {
702 while (isspace(*start))
703 start++; /* Kill whitespace */
705 break; /* Nothing left */
706 /* Now find the end of the word */
707 for (end = start; *end != '\0' && !isspace(*end); end++);
708 (void) strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
709 argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */
715 /* This is the internal form of Find_command, which recursively searches
716 for a menu_line with command command in the specified menu */
717 /* It will search to a maximum depth of d */
719 find_command_from(c, m, d)
725 struct menu_line *maybe;
728 return ((struct menu_line *) 0); /* Too deep! */
729 for (line = 0; line < m->m_length; line++) {
730 if (!strcmp(c, m->m_lines[line].ml_command)) {
731 return (&m->m_lines[line]);
734 for (line = 0; line < m->m_length; line++) {
735 if (m->m_lines[line].ml_submenu != NULLMENU &&
736 (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1))
737 != (struct menu_line *) 0) {
741 /* If we got to here, nothing works */
742 return ((struct menu_line *) 0);
745 /* Find_command searches down the current menu tree */
746 /* And returns a pointer to a menu_line with the specified command name */
747 /* It returns (struct menu_line *) 0 if none is found */
749 Find_command(m, command)
754 return ((struct menu_line *) 0);
757 return (find_command_from(command, m, MAX_MENU_DEPTH));
763 toggle_logging(argc, argv)
770 if (log_file == (FILE *) NULL) {
773 Put_message("I've lost my SENSE of DIRECTION! I have no IDEA who I AM!");
774 Put_message("My God... You've turned him into a DEMOCRAT!!");
775 Put_message(" -- Doonesbury");
777 Put_message("translation: your log file can be found in \"/usr/tmp/a.out.pid\".");
779 (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
782 (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
785 log_file = fopen(buf,"a");
787 if (log_file == (FILE *) NULL)
788 Put_message("Open of log file failed. Logging is not on.");
790 Put_message("Log file successfully opened.");
792 else { /* log_file is a valid pointer; turn off logging. */
793 (void) fflush(log_file);
794 (void) fclose(log_file);
795 log_file = (FILE *) NULL;
796 Put_message("Logging off.");