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. Takes at most 5 args to the
70 * printf string (crock...)
74 menu_com_err_hook(who, code, fmt, arg1, arg2, arg3, arg4, arg5)
78 caddr_t arg1, arg2, arg3, arg4, arg5;
80 char buf[BUFSIZ], *cp;
82 (void) strcpy(buf, who);
83 for (cp = buf; *cp; cp++);
87 (void) strcpy(cp, error_message(code));
91 sprintf(cp, fmt, arg1, arg2, arg3, arg4, arg5);
96 * Start_menu takes a menu as an argument. It initializes curses u.s.w.,
97 * and a quit in any submenu should unwind back to here. (it might not,
98 * if user functions which run their own menus don't cooperate.)
99 * Start_menu should only be called once, at the start of the program.
104 struct menu_screen *make_ms();
106 register void (*old_hook)(const char *, long, const char *, va_list) = set_com_err_hook(menu_com_err_hook);
108 register void (*old_hook)() = set_com_err_hook(menu_com_err_hook);
111 if (initscr() == ERR) {
112 fputs("Can't initialize curses!\n", stderr);
115 (void) raw(); /* We parse & print everything ourselves */
117 cur_ms = make_ms(0); /* So we always have some current */
120 (void) Do_menu(m, -1, (char **) NULL);
122 (void) set_com_err_hook(old_hook);
129 (void) wclear(cur_ms->ms_screen);
130 (void) wrefresh(cur_ms->ms_screen);
136 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
143 (void) Do_menu(m, -1, (char **) NULL);
147 * Create a new menu screen template with the specified menu length
154 struct menu_screen *ms;
157 if (MAX_TITLE + length + MIN_INPUT > LINES) {
158 fputs("Menu too big!\n", stderr);
162 ms = (struct menu_screen *) malloc(sizeof(struct menu_screen));
164 ms->ms_screen = newwin(0, 0, 0, 0);
165 ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
166 ms->ms_menu = subwin(ms->ms_screen,
167 length, 0, MAX_TITLE, 0);
168 ms->ms_input = subwin(ms->ms_screen, 0, 0,
169 ms->ms_input_y = MAX_TITLE + length,
172 scrollok(ms->ms_input, TRUE);
173 (void) wmove(ms->ms_input, 0, 0);
174 (void) wclear(ms->ms_screen);
180 * This routine destroys a menu_screen.
183 struct menu_screen *ms;
185 delwin(ms->ms_title);
187 delwin(ms->ms_input);
188 delwin(ms->ms_screen);
193 * This guy actually puts up the menu
194 * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
198 Do_menu(m, margc, margv)
203 struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
204 char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
205 char buf[MAX_ARGC * MAX_ARGLEN];
206 char *argv[MAX_ARGC];
209 struct menu_line *command, *Find_command();
211 int quitflag, is_topmenu = (margc < 0);
212 int toggle_logging();
214 /* Entry function gets called with old menu_screen still current */
215 if (m->m_entry != NULLFUNC)
216 if (m->m_entry(m, margc, margv) == DM_QUIT)
219 /* The following get run only in curses mode */
220 if (cur_ms != NULLMS) {
221 /* Get a menu_screen */
223 /* 2 is for the 2 obligatory lines; quit and toggle */
224 cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
226 /* Now print the title and the menu */
227 (void) wclear(my_ms->ms_menu);
228 (void) wmove(my_ms->ms_title, 0, MAX(0, (COLS -
229 strlen(m->m_title)) >> 1));
230 (void) wstandout(my_ms->ms_title);
231 (void) waddstr(my_ms->ms_title, m->m_title);
232 (void) wstandend(my_ms->ms_title);
234 for (line = 0; line < m->m_length; line++) {
235 int len = strlen(m->m_lines[line].ml_command);
236 if (len > 12) len=12;
238 (void) wmove(my_ms->ms_menu, line, 0);
240 (void) wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
241 m->m_lines[line].ml_command,
243 m->m_lines[line].ml_doc);
245 (void) wmove(my_ms->ms_menu, line++, 0);
247 (void) waddstr(my_ms->ms_menu,
248 " r. (return) Return to previous menu.");
249 (void) wmove(my_ms->ms_menu, line++, 0);
251 (void) waddstr(my_ms->ms_menu,
252 " t. (toggle) Toggle logging on and off.");
253 (void) wmove(my_ms->ms_menu, line, 0);
254 (void) waddstr(my_ms->ms_menu, " q. (quit) Quit.");
256 Put_message(m->m_title);
257 for (line = 0; line < m->m_length; line++) {
258 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
259 m->m_lines[line].ml_command,
260 12 - strlen(m->m_lines[line].ml_command), "",
261 m->m_lines[line].ml_doc);
265 Put_message(" r. (return) Return to previous menu.");
266 Put_message(" t. (toggle) Toggle logging on and off.");
267 Put_message(" q. (quit) Quit.");
268 Put_message(" ?. Print this information.");
272 /* This will be set by a return val from func or submenu */
273 quitflag = DM_NORMAL;
274 /* This is here because we may be coming from another menu */
276 touchwin(my_ms->ms_screen);
278 if (!Prompt_input("Command: ", buf, sizeof(buf))) {
279 if (cur_ms == NULLMS &&
281 sprintf(buf, "quit");
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_input);
419 (void) mvcur(0, COLS - 1, LINES - 1, 0);
420 (void) wrefresh(curscr);
421 getyx(cur_ms->ms_input, y, x);
430 /* these should be obtained by doing ioctl() on tty */
437 (void) wmove(cur_ms->ms_input, y, 0);
438 (void) wclrtoeol(cur_ms->ms_input);
440 x = cur_ms->ms_input->_maxx-1;
452 /* (buflen - 1) leaves room for the \0 */
453 if (isprint(c) && (p - buf < buflen - 1)) {
454 (void) waddch(cur_ms->ms_input, c);
457 if (x >= cur_ms->ms_input->_maxx) {
467 (void) waddch(cur_ms->ms_input, '\n');
468 (void) waddch(cur_ms->ms_input, '\r');
470 (void) wclrtoeol(cur_ms->ms_input);
476 printf("%s", prompt);
477 if (gets(buf) == NULL)
483 strcpy(buf, strtrim(buf));
487 /* Prompt the user for input in the input window of cur_ms, but don't echo
488 and allow some control characters */
489 int Password_input(prompt, buf, buflen)
498 if (cur_ms != NULLMS) {
500 getyx(cur_ms->ms_input, y, x);
501 (void) wmove(cur_ms->ms_input, y, 0);
503 touchwin(cur_ms->ms_screen);
505 (void) waddstr(cur_ms->ms_input, prompt);
506 getyx(cur_ms->ms_input, y, x);
509 for (p = buf; p - buf < buflen;) {
510 (void) wmove(cur_ms->ms_input, y, x);
511 (void) wclrtoeol(cur_ms->ms_input);
513 c = getchar() & 0x7f;
518 (void) kill(getpid(), SIGTSTP);
522 (void) wclear(cur_ms->ms_input);
523 (void) waddstr(cur_ms->ms_input, prompt);
525 (void) mvcur(0, COLS - 1, LINES - 1, 0);
526 (void) wrefresh(curscr);
527 getyx(cur_ms->ms_input, y, x);
531 (void) waddch(cur_ms->ms_input, '\n');
532 (void) waddch(cur_ms->ms_input, '\r');
534 (void) wclrtoeol(cur_ms->ms_input);
557 struct sgttyb ttybuf, nttybuf;
558 printf("%s", prompt);
559 /* turn off echoing */
560 (void) ioctl(0, TIOCGETP, (char *)&ttybuf);
562 nttybuf.sg_flags &= ~ECHO;
563 (void)ioctl(0, TIOCSETP, (char *)&nttybuf);
564 if (gets(buf) == NULL) {
565 (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
570 (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
580 /* This routine will cause the most recently put message to be the
581 one at the top of the screen when a ---More--- prompt is displayed */
584 if (cur_ms != NULLMS) {
585 lines_left = LINES - cur_ms->ms_input_y - 1;
592 /* Turn off paging */
598 /* Print a message in the input window of cur_ms. */
602 char *copy, *line, *s;
604 copy = (char *) malloc((u_int)COLS);
606 if (log_file) { /* if we're doing logging; we assume that the */
607 /* file has already been opened. */
608 (void) fprintf(log_file, "%s\n", msg);
613 if (s - line >= COLS-1) {
614 (void) strncpy(copy, line, COLS-1);
616 } else if (*s == '\n') {
618 (void) strcpy(copy, line);
628 /* Will be truncated to COLS characters. */
638 if (lines_left >= 0) {
639 if (--lines_left == 0) {
640 /* Give the user a more prompt */
641 if (cur_ms != NULLMS) {
642 (void) wstandout(cur_ms->ms_input);
643 (void) wprintw(cur_ms->ms_input, "---More---");
644 (void) wstandend(cur_ms->ms_input);
646 chr = getchar() & 0x7f;/* We do care what it is */
647 if (chr == 'q' || chr == 'Q') {
651 getyx(cur_ms->ms_input, y, x);
652 /* x is a bitbucket; avoid lint problems */
654 (void) wmove(cur_ms->ms_input, y, 0);
655 (void) wclrtoeol(cur_ms->ms_input);
658 printf("---More (hit return)---");
661 Start_paging(); /* Reset lines_left */
665 if (cur_ms != NULLMS) {
666 msg1 = (char *) calloc((u_int)COLS, 1);
667 (void) strncpy(msg1, msg, COLS-1);
668 for (i = strlen(msg1); i < COLS - 1; i++)
670 (void) wprintw(cur_ms->ms_input, "%s\n", msg1);
671 /* refresh_ms(cur_ms); */
678 /* Refresh a menu_screen onto the real screen */
680 struct menu_screen *ms;
684 getyx(ms->ms_input, y, x);
685 (void) wmove(ms->ms_screen, y + ms->ms_input_y, x);
686 (void) wrefresh(ms->ms_screen);
689 /* Parse buf into a list of words, which will be placed in strings specified by
690 argv. Space for these strings must have already been allocated.
691 Only the first n characters of each word will be copied */
692 Parse_words(buf, argv, n)
698 char *start, *end; /* For sausage machine */
702 for (argc = 0; argc < MAX_ARGC; argc++) {
703 while (isspace(*start))
704 start++; /* Kill whitespace */
706 break; /* Nothing left */
707 /* Now find the end of the word */
708 for (end = start; *end != '\0' && !isspace(*end); end++);
709 (void) strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
710 argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */
716 /* This is the internal form of Find_command, which recursively searches
717 for a menu_line with command command in the specified menu */
718 /* It will search to a maximum depth of d */
720 find_command_from(c, m, d)
726 struct menu_line *maybe;
729 return ((struct menu_line *) 0); /* Too deep! */
730 for (line = 0; line < m->m_length; line++) {
731 if (!strcmp(c, m->m_lines[line].ml_command)) {
732 return (&m->m_lines[line]);
735 for (line = 0; line < m->m_length; line++) {
736 if (m->m_lines[line].ml_submenu != NULLMENU &&
737 (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1))
738 != (struct menu_line *) 0) {
742 /* If we got to here, nothing works */
743 return ((struct menu_line *) 0);
746 /* Find_command searches down the current menu tree */
747 /* And returns a pointer to a menu_line with the specified command name */
748 /* It returns (struct menu_line *) 0 if none is found */
750 Find_command(m, command)
755 return ((struct menu_line *) 0);
758 return (find_command_from(command, m, MAX_MENU_DEPTH));
764 toggle_logging(argc, argv)
771 if (log_file == (FILE *) NULL) {
774 Put_message("I've lost my SENSE of DIRECTION! I have no IDEA who I AM!");
775 Put_message("My God... You've turned him into a DEMOCRAT!!");
776 Put_message(" -- Doonesbury");
778 Put_message("translation: your log file can be found in \"/usr/tmp/a.out.pid\".");
780 (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
783 (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
786 log_file = fopen(buf,"a");
788 if (log_file == (FILE *) NULL)
789 Put_message("Open of log file failed. Logging is not on.");
791 Put_message("Log file successfully opened.");
793 else { /* log_file is a valid pointer; turn off logging. */
794 (void) fflush(log_file);
795 (void) fclose(log_file);
796 log_file = (FILE *) NULL;
797 Put_message("Logging off.");