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$";
24 #include <mit-copyright.h>
25 #include <sys/types.h>
39 #define MAX(A, B) ((A) > (B) ? (A) : (B))
40 #define MIN(A, B) ((A) < (B) ? (A) : (B))
41 #define CTL(ch) ((ch) & 037)
43 #define MIN_INPUT 2 /* Minimum number of lines for input window */
45 extern int interrupt; /* will be set if ^C is received */
48 FILE *log_file = (FILE *) NULL; /* file stream of log file */
51 /* Structure for holding current displayed menu */
53 WINDOW *ms_screen; /* Window for this menu */
54 WINDOW *ms_title; /* Title subwindow */
55 WINDOW *ms_menu; /* Menu subwindow */
56 WINDOW *ms_input; /* Input subwindow */
57 int ms_input_y; /* Input subwindow reference coordinate */
60 #define NULLMS ((struct menu_screen *) 0)
62 Menu *top_menu; /* Root for command search */
63 int parsed_argc; /* used by extern routines to get additional */
64 char **parsed_argv; /* comand line input */
67 * Hook function to cause error messages to be printed through
68 * curses instead of around it. Takes at most 5 args to the
69 * printf string (crock...)
72 void menu_com_err_hook(const char *who, long code, const char *fmt,
73 caddr_t arg1, caddr_t arg2, caddr_t arg3,
74 caddr_t arg4, caddr_t arg5)
76 char buf[BUFSIZ], *cp;
79 for (cp = buf; *cp; cp++)
85 strcpy(cp, error_message(code));
89 sprintf(cp, fmt, arg1, arg2, arg3, arg4, arg5);
94 * Start_menu takes a menu as an argument. It initializes curses u.s.w.,
95 * and a quit in any submenu should unwind back to here. (it might not,
96 * if user functions which run their own menus don't cooperate.)
97 * Start_menu should only be called once, at the start of the program.
99 int Start_menu(Menu *m)
101 struct menu_screen *make_ms();
102 void (*old_hook)(const char *, long, const char *, va_list) =
103 set_com_err_hook((void (*) (const char *, long, const char *, va_list))menu_com_err_hook);
105 if (initscr() == (WINDOW *)ERR)
107 fputs("Can't initialize curses!\n", stderr);
112 raw(); /* We parse & print everything ourselves */
114 cur_ms = make_ms(0); /* So we always have some current */
117 Do_menu(m, -1, NULL);
119 set_com_err_hook(old_hook);
123 int Cleanup_menu(void)
127 wclear(cur_ms->ms_screen);
128 wrefresh(cur_ms->ms_screen);
134 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
135 int Start_no_menu(Menu *m)
140 Do_menu(m, -1, NULL);
144 * Create a new menu screen template with the specified menu length
147 struct menu_screen *make_ms(int length)
149 struct menu_screen *ms;
151 if (MAX_TITLE + length + MIN_INPUT > LINES)
153 fputs("Menu too big!\n", stderr);
157 ms = malloc(sizeof(struct menu_screen));
159 ms->ms_screen = newwin(0, 0, 0, 0);
160 ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
161 ms->ms_menu = subwin(ms->ms_screen, length, 0, MAX_TITLE, 0);
162 ms->ms_input = subwin(ms->ms_screen, 0, 0,
163 ms->ms_input_y = MAX_TITLE + length,
166 scrollok(ms->ms_input, TRUE);
167 wmove(ms->ms_input, 0, 0);
168 wclear(ms->ms_screen);
174 * This routine destroys a menu_screen.
176 int destroy_ms(struct menu_screen *ms)
178 delwin(ms->ms_title);
180 delwin(ms->ms_input);
181 delwin(ms->ms_screen);
186 * This guy actually puts up the menu
187 * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
190 int Do_menu(Menu *m, int margc, char *margv[])
192 struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
193 char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
194 char buf[MAX_ARGC * MAX_ARGLEN];
195 char *argv[MAX_ARGC];
198 struct menu_line *command, *Find_command();
200 int quitflag, is_topmenu = (margc < 0);
201 int toggle_logging();
203 /* Entry function gets called with old menu_screen still current */
204 if (m->m_entry != NULLFUNC)
206 if (m->m_entry(m, margc, margv) == DM_QUIT)
210 margc = parsed_argc + 1;
211 margv = --parsed_argv;
222 /* The following get run only in curses mode */
223 if (cur_ms != NULLMS)
225 /* Get a menu_screen */
227 /* 2 is for the 2 obligatory lines; quit and toggle */
228 cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
230 /* Now print the title and the menu */
231 wclear(my_ms->ms_screen);
232 wrefresh(my_ms->ms_screen);
233 wmove(my_ms->ms_title, 0, MAX(0, (COLS - strlen(m->m_title)) >> 1));
234 wstandout(my_ms->ms_title);
235 waddstr(my_ms->ms_title, m->m_title);
236 wstandend(my_ms->ms_title);
238 for (line = 0; line < m->m_length; line++)
240 int len = strlen(m->m_lines[line].ml_command);
244 wmove(my_ms->ms_menu, line, 0);
246 wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
247 m->m_lines[line].ml_command, 12 - len, "",
248 m->m_lines[line].ml_doc);
250 wmove(my_ms->ms_menu, line++, 0);
253 waddstr(my_ms->ms_menu,
254 " r. (return) Return to previous menu.");
255 wmove(my_ms->ms_menu, line++, 0);
257 waddstr(my_ms->ms_menu, " t. (toggle) Toggle logging on and off.");
258 wmove(my_ms->ms_menu, line, 0);
259 waddstr(my_ms->ms_menu, " q. (quit) Quit.");
263 Put_message(m->m_title);
264 for (line = 0; line < m->m_length; line++)
266 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
267 m->m_lines[line].ml_command,
268 12 - strlen(m->m_lines[line].ml_command), "",
269 m->m_lines[line].ml_doc);
273 Put_message(" r. (return) Return to previous menu.");
274 Put_message(" t. (toggle) Toggle logging on and off.");
275 Put_message(" q. (quit) Quit.");
276 Put_message(" ?. Print this information.");
281 /* This will be set by a return val from func or submenu */
282 quitflag = DM_NORMAL;
283 /* This is here because we may be coming from another menu */
286 touchwin(my_ms->ms_screen);
287 wrefresh(my_ms->ms_screen);
291 /* Initialize argv */
292 for (argc = 0; argc < MAX_ARGC; argc++)
293 argv[argc] = argvals[argc];
295 for (i = 1; i < margc; i++)
296 strcpy(argvals[i - 1], margv[i]);
302 if (!Prompt_input("Command: ", buf, sizeof(buf)))
304 if (cur_ms == NULLMS && feof(stdin))
305 sprintf(buf, "quit");
309 /* Parse it into the argument list */
310 /* If there's nothing there, try again */
311 /* Initialize argv */
312 for (argc = 0; argc < MAX_ARGC; argc++)
313 argv[argc] = argvals[argc];
315 if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
318 if ((line = atoi(argv[0])) > 0 && line <= m->m_length)
319 command = &m->m_lines[line - 1];
320 else if ((!is_topmenu &&
321 (!strcmp(argv[0], "r") || !strcmp(argv[0], "return"))) ||
322 !strcmp(argv[0], "q") || !strcmp(argv[0], "quit"))
324 /* here if it's either return or quit */
325 if (cur_ms != NULLMS)
330 if (m->m_exit != NULLFUNC)
332 return *argv[0] == 'r' ? DM_NORMAL : DM_QUIT;
334 else if (argv[0][0] == '?')
336 for (line = 0; line < m->m_length; line++)
338 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
339 m->m_lines[line].ml_command,
340 12 - strlen(m->m_lines[line].ml_command), "",
341 m->m_lines[line].ml_doc);
345 Put_message(" r. (return) Return to previous menu.");
346 Put_message(" t. (toggle) Toggle logging on and off.");
347 Put_message(" q. (quit) Quit.");
350 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])))
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 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++)
370 if (!command->ml_args[i].ma_prompt)
372 sprintf(buf, "%s%s", command->ml_args[i].ma_prompt, argv[i]);
375 /* Get remaining arguments, if any */
376 for (; argc < command->ml_argc; argc++)
378 if (!Prompt_input(command->ml_args[argc].ma_prompt,
379 argvals[argc], sizeof(argvals[argc])))
382 parsed_argc = argc - command->ml_argc;
383 parsed_argv = &(argv[command->ml_argc]);
384 if (command->ml_function != NULLFUNC)
386 /* If it's got a function, call it */
387 quitflag = command->ml_function(argc, argv);
389 else if (command->ml_submenu != NULLMENU)
391 /* Else see if it is a submenu */
392 quitflag = Do_menu(command->ml_submenu, argc, argv);
396 /* If it's got neither, something is wrong */
397 Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
399 if (quitflag == DM_QUIT)
401 if (cur_ms != NULLMS)
406 if (m->m_exit != NULLFUNC)
416 int refresh_screen(void)
418 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(char *prompt, char *buf, int buflen)
431 int y, x, oldx, oldy;
433 if (cur_ms != NULLMS)
436 getyx(cur_ms->ms_input, y, x);
437 wmove(cur_ms->ms_input, y, 0);
440 waddstr(cur_ms->ms_input, prompt);
441 getyx(cur_ms->ms_input, y, x);
448 wmove(cur_ms->ms_input, y, x);
449 touchwin(cur_ms->ms_screen);
450 wclrtoeol(cur_ms->ms_input);
451 wrefresh(cur_ms->ms_input);
452 c = getchar() & 0x7f;
459 kill(getpid(), SIGTSTP);
460 touchwin(cur_ms->ms_screen);
463 wclear(cur_ms->ms_input);
464 wmove(cur_ms->ms_input, 0, 0);
465 waddstr(cur_ms->ms_input, prompt);
466 touchwin(cur_ms->ms_input);
467 wrefresh(cur_ms->ms_screen);
468 getyx(cur_ms->ms_input, y, x);
477 /* these should be obtained by doing ioctl() on tty */
486 wmove(cur_ms->ms_input, y, 0);
487 wclrtoeol(cur_ms->ms_input);
490 x = cur_ms->ms_input->maxx - 1;
492 x = cur_ms->ms_input->_maxx - 1;
505 /* (buflen - 1) leaves room for the \0 */
506 if (isprint(c) && (p - buf < buflen - 1))
508 waddch(cur_ms->ms_input, c);
512 if (x >= cur_ms->ms_input->maxx)
514 if (x >= cur_ms->ms_input->_maxx)
527 waddch(cur_ms->ms_input, '\n');
529 wclrtoeol(cur_ms->ms_input);
537 printf("%s", prompt);
538 if (!fgets(buf, buflen, stdin))
549 strcpy(buf, strtrim(buf));
556 /* This routine will cause the most recently put message to be the
557 one at the top of the screen when a ---More--- prompt is displayed */
558 int Start_paging(void)
560 if (cur_ms != NULLMS)
561 lines_left = LINES - cur_ms->ms_input_y - 1;
566 /* Turn off paging */
567 int Stop_paging(void)
572 /* Print a message in the input window of cur_ms. */
573 int Put_message(char *msg)
575 char *copy, *line, *s;
581 /* if we're doing logging; we assume that the file has already
583 fprintf(log_file, "%s\n", msg);
589 if (s - line >= COLS - 1)
591 strncpy(copy, line, COLS - 1);
592 copy[COLS - 1] = '\0';
609 /* Will be truncated to COLS characters. */
610 int Put_line(char *msg)
620 if (--lines_left == 0)
622 /* Give the user a more prompt */
623 if (cur_ms != NULLMS)
625 wstandout(cur_ms->ms_input);
626 wprintw(cur_ms->ms_input, "---More---");
627 wstandend(cur_ms->ms_input);
629 chr = getchar() & 0x7f;/* We do care what it is */
630 if (chr == 'q' || chr == 'Q' || chr == 3 /* ^C */)
635 getyx(cur_ms->ms_input, y, x);
636 /* x is a bitbucket; avoid lint problems */
638 wmove(cur_ms->ms_input, y, 0);
639 wclrtoeol(cur_ms->ms_input);
643 printf("---More (hit return)---");
646 Start_paging(); /* Reset lines_left */
650 if (cur_ms != NULLMS)
652 msg1 = calloc(COLS, 1);
653 strncpy(msg1, msg, COLS - 1);
654 for (i = strlen(msg1); i < COLS - 1; i++)
656 wprintw(cur_ms->ms_input, "%s\n", msg1);
662 /* Refresh a menu_screen onto the real screen */
663 int refresh_ms(struct menu_screen *ms)
665 wrefresh(ms->ms_title);
666 wrefresh(ms->ms_menu);
667 wrefresh(ms->ms_input);
670 /* Parse buf into a list of words, which will be placed in strings specified by
671 argv. Space for these strings must have already been allocated.
672 Only the first n characters of each word will be copied */
673 int Parse_words(char *buf, char *argv[], int n)
675 char *start, *end; /* For sausage machine */
679 for (argc = 0; argc < MAX_ARGC; argc++)
681 while (isspace(*start))
682 start++; /* Kill whitespace */
684 break; /* Nothing left */
685 /* Now find the end of the word */
686 for (end = start; *end != '\0' && !isspace(*end); end++)
688 strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
689 argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */
695 /* This is the internal form of Find_command, which recursively searches
696 for a menu_line with command command in the specified menu */
697 /* It will search to a maximum depth of d */
698 struct menu_line *find_command_from(char *c, struct menu *m, int d)
701 struct menu_line *maybe;
704 return NULL; /* Too deep! */
705 for (line = 0; line < m->m_length; line++)
707 if (!strcmp(c, m->m_lines[line].ml_command))
708 return &m->m_lines[line];
710 for (line = 0; line < m->m_length; line++)
712 if (m->m_lines[line].ml_submenu != NULLMENU &&
713 (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1)))
716 /* If we got to here, nothing works */
720 /* Find_command searches down the current menu tree */
721 /* And returns a pointer to a menu_line with the specified command name */
722 /* It returns (struct menu_line *) 0 if none is found */
723 struct menu_line *Find_command(Menu *m, char *command)
728 return find_command_from(command, m, MAX_MENU_DEPTH);
731 int toggle_logging(int argc, char *argv[])
741 Put_message("I've lost my SENSE of DIRECTION! I have no IDEA who I AM!");
742 Put_message("My God... You've turned him into a DEMOCRAT!!");
743 Put_message(" -- Doonesbury");
745 Put_message("translation: your log file can be found in \"/usr/tmp/a.out.pid\".");
747 sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
750 sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
753 log_file = fopen(buf, "a");
756 Put_message("Open of log file failed. Logging is not on.");
758 Put_message("Log file successfully opened.");
761 { /* log_file is a valid pointer; turn off logging. */
765 Put_message("Logging off.");