3 * Generic menu system module.
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, see the file
10 #include <mit-copyright.h>
30 #define MAX(A, B) ((A) > (B) ? (A) : (B))
31 #define MIN(A, B) ((A) < (B) ? (A) : (B))
32 #define CTL(ch) ((ch) & 037)
34 #define MIN_INPUT 2 /* Minimum number of lines for input window */
36 extern int interrupt; /* will be set if ^C is received */
39 FILE *log_file = (FILE *) NULL; /* file stream of log file */
42 /* Structure for holding current displayed menu */
45 WINDOW *ms_screen; /* Window for this menu */
46 WINDOW *ms_title; /* Title subwindow */
47 WINDOW *ms_menu; /* Menu subwindow */
48 WINDOW *ms_input; /* Input subwindow */
49 #endif /* HAVE_CURSES */
50 int ms_input_y; /* Input subwindow reference coordinate */
57 #define NULLMS ((struct menu_screen *) 0)
59 Menu *top_menu; /* Root for command search */
60 int parsed_argc; /* used by extern routines to get additional */
61 char **parsed_argv; /* comand line input */
63 int Parse_words(char *buf, char *argv[], int n);
64 void refresh_ms(struct menu_screen *ms);
65 void Put_line(char *msg);
66 void menu_com_err_hook(const char *who, long code,
67 const char *fmt, va_list ap);
68 struct menu_screen *make_ms(int length);
69 void destroy_ms(struct menu_screen *ms);
70 struct menu_line *find_command_from(char *c, struct menu *m, int d);
71 struct menu_line *Find_command(Menu *m, char *command);
72 int toggle_logging(int argc, char *argv[]);
75 * Hook function to cause error messages to be printed through
76 * curses instead of around it.
79 void menu_com_err_hook(const char *who, long code, const char *fmt, va_list ap)
81 char buf[BUFSIZ], *cp;
86 for (cp = buf; *cp; cp++)
98 strcpy(cp, error_message(code));
102 vsprintf(cp, fmt, ap);
108 * Start_menu takes a menu as an argument. It initializes curses u.s.w.,
109 * and a quit in any submenu should unwind back to here. (it might not,
110 * if user functions which run their own menus don't cooperate.)
111 * Start_menu should only be called once, at the start of the program.
113 void Start_menu(Menu *m)
115 void (*old_hook)(const char *, long, const char *, va_list) =
116 set_com_err_hook(menu_com_err_hook);
117 #ifdef CURSES_HAS_NEWTERM
118 SCREEN *scrn = newterm(NULL, stdout, stdin);
120 WINDOW *scrn = initscr();
124 fputs("Can't initialize curses!\nReverting to -nomenu mode\n\n", stderr);
129 #ifdef CURSES_HAS_NEWTERM
134 raw(); /* We parse & print everything ourselves */
136 cur_ms = make_ms(0); /* So we always have some current */
139 Do_menu(m, -1, NULL);
142 set_com_err_hook(old_hook);
145 void Cleanup_menu(void)
149 wclear(cur_ms->ms_screen);
150 wrefresh(cur_ms->ms_screen);
156 * Create a new menu screen template with the specified menu length
159 struct menu_screen *make_ms(int length)
161 struct menu_screen *ms;
163 if (MAX_TITLE + length + MIN_INPUT > LINES)
165 fputs("Menu too big!\n", stderr);
169 ms = malloc(sizeof(struct menu_screen));
171 ms->ms_screen = newwin(0, 0, 0, 0);
172 ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
173 ms->ms_menu = subwin(ms->ms_screen, length, 0, MAX_TITLE, 0);
174 ms->ms_input = subwin(ms->ms_screen, 0, 0,
175 ms->ms_input_y = MAX_TITLE + length,
178 scrollok(ms->ms_input, TRUE);
179 wmove(ms->ms_input, 0, 0);
180 wclear(ms->ms_screen);
186 * This routine destroys a menu_screen.
188 void destroy_ms(struct menu_screen *ms)
190 delwin(ms->ms_title);
192 delwin(ms->ms_input);
193 delwin(ms->ms_screen);
196 #endif /* HAVE_CURSES */
198 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
199 void Start_no_menu(Menu *m)
204 Do_menu(m, -1, NULL);
208 * This guy actually puts up the menu
209 * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
212 int Do_menu(Menu *m, int margc, char *margv[])
214 struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
215 char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
216 char buf[MAX_ARGC * MAX_ARGLEN];
217 char *argv[MAX_ARGC];
220 struct menu_line *command;
222 int quitflag, is_topmenu = (margc < 0);
224 /* Entry function gets called with old menu_screen still current */
225 if (m->m_entry != NULLFUNC)
227 if (m->m_entry(m, margc, margv) == DM_QUIT)
231 margc = parsed_argc + 1;
232 margv = --parsed_argv;
244 /* The following get run only in curses mode */
245 if (cur_ms != NULLMS)
247 /* Get a menu_screen */
249 /* 2 is for the 2 obligatory lines; quit and toggle */
250 cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
252 /* Now print the title and the menu */
253 wclear(my_ms->ms_screen);
254 wrefresh(my_ms->ms_screen);
255 wmove(my_ms->ms_title, 0, MAX(0, (COLS - (int)strlen(m->m_title)) >> 1));
256 wstandout(my_ms->ms_title);
257 waddstr(my_ms->ms_title, m->m_title);
258 wstandend(my_ms->ms_title);
260 for (line = 0; line < m->m_length; line++)
262 int len = strlen(m->m_lines[line].ml_command);
266 wmove(my_ms->ms_menu, line, 0);
268 wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
269 m->m_lines[line].ml_command, 12 - len, "",
270 m->m_lines[line].ml_doc);
272 wmove(my_ms->ms_menu, line++, 0);
275 waddstr(my_ms->ms_menu,
276 " r. (return) Return to previous menu.");
277 wmove(my_ms->ms_menu, line++, 0);
279 waddstr(my_ms->ms_menu, " t. (toggle) Toggle logging on and off.");
280 wmove(my_ms->ms_menu, line, 0);
281 waddstr(my_ms->ms_menu, " q. (quit) Quit.");
284 #endif /* HAVE_CURSES */
286 Put_message(m->m_title);
287 for (line = 0; line < m->m_length; line++)
289 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
290 m->m_lines[line].ml_command,
291 12 - strlen(m->m_lines[line].ml_command), "",
292 m->m_lines[line].ml_doc);
296 Put_message(" r. (return) Return to previous menu.");
297 Put_message(" t. (toggle) Toggle logging on and off.");
298 Put_message(" q. (quit) Quit.");
299 Put_message(" ?. Print this information.");
304 /* This will be set by a return val from func or submenu */
305 quitflag = DM_NORMAL;
307 /* This is here because we may be coming from another menu */
310 touchwin(my_ms->ms_screen);
311 wrefresh(my_ms->ms_screen);
313 #endif /* HAVE_CURSES */
316 /* Initialize argv */
317 for (argc = 0; argc < MAX_ARGC; argc++)
318 argv[argc] = argvals[argc];
320 for (i = 1; i < margc; i++)
321 strcpy(argvals[i - 1], margv[i]);
327 if (!Prompt_input("Command: ", buf, sizeof(buf)))
329 if (cur_ms == NULLMS && feof(stdin))
330 sprintf(buf, "quit");
334 /* Parse it into the argument list */
335 /* If there's nothing there, try again */
336 /* Initialize argv */
337 for (argc = 0; argc < MAX_ARGC; argc++)
338 argv[argc] = argvals[argc];
340 if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
343 if ((line = atoi(argv[0])) > 0 && line <= m->m_length)
344 command = &m->m_lines[line - 1];
345 else if ((!is_topmenu &&
346 (!strcmp(argv[0], "r") || !strcmp(argv[0], "return"))) ||
347 !strcmp(argv[0], "q") || !strcmp(argv[0], "quit"))
349 /* here if it's either return or quit */
351 if (cur_ms != NULLMS)
356 #endif /* HAVE_CURSES */
357 if (m->m_exit != NULLFUNC)
359 return *argv[0] == 'r' ? DM_NORMAL : DM_QUIT;
361 else if (argv[0][0] == '?')
363 for (line = 0; line < m->m_length; line++)
365 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
366 m->m_lines[line].ml_command,
367 12 - strlen(m->m_lines[line].ml_command), "",
368 m->m_lines[line].ml_doc);
372 Put_message(" r. (return) Return to previous menu.");
373 Put_message(" t. (toggle) Toggle logging on and off.");
374 Put_message(" q. (quit) Quit.");
377 else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle"))
379 toggle_logging(argc, argv);
382 /* finally, try to find it using Find_command */
383 else if (!(command = Find_command(m, argvals[0])))
385 sprintf(buf, "Command not recognized: %s\n", argvals[0]);
389 /* If we got to here, command is a valid menu_line */
390 /* Send the offical command name into the argv */
391 strcpy(argvals[0], command->ml_command);
392 /* Show that we're working on it */
393 Put_message(command->ml_doc);
394 /* Print args that we've already got */
395 for (i = 1; i < argc; i++)
397 if (!command->ml_args[i].ma_prompt)
399 sprintf(buf, "%s%s", command->ml_args[i].ma_prompt, argv[i]);
402 /* Get remaining arguments, if any */
403 for (; argc < command->ml_argc; argc++)
405 if (!Prompt_input(command->ml_args[argc].ma_prompt,
406 argvals[argc], sizeof(argvals[argc])))
409 parsed_argc = argc - command->ml_argc;
410 parsed_argv = &(argv[command->ml_argc]);
411 if (command->ml_function != NULLFUNC)
413 /* If it's got a function, call it */
414 quitflag = command->ml_function(argc, argv);
416 else if (command->ml_submenu != NULLMENU)
418 /* Else see if it is a submenu */
419 quitflag = Do_menu(command->ml_submenu, argc, argv);
423 /* If it's got neither, something is wrong */
424 Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
426 if (quitflag == DM_QUIT)
429 if (cur_ms != NULLMS)
434 #endif /* HAVE_CURSES */
435 if (m->m_exit != NULLFUNC)
445 void refresh_screen(void)
448 if (cur_ms != NULLMS)
450 touchwin(cur_ms->ms_screen);
453 #endif /* HAVE_CURSES */
457 /* Prompt the user for input in the input window of cur_ms */
458 int Prompt_input(char *prompt, char *buf, int buflen)
462 int y, x, oldx, oldy;
465 if (cur_ms != NULLMS)
468 getyx(cur_ms->ms_input, y, x);
469 wmove(cur_ms->ms_input, y, 0);
472 waddstr(cur_ms->ms_input, prompt);
473 getyx(cur_ms->ms_input, y, x);
480 wmove(cur_ms->ms_input, y, x);
481 touchwin(cur_ms->ms_screen);
482 wclrtoeol(cur_ms->ms_input);
483 wrefresh(cur_ms->ms_input);
484 c = getchar() & 0x7f;
491 kill(getpid(), SIGTSTP);
492 touchwin(cur_ms->ms_screen);
495 wclear(cur_ms->ms_input);
496 wmove(cur_ms->ms_input, 0, 0);
497 waddstr(cur_ms->ms_input, prompt);
498 touchwin(cur_ms->ms_input);
499 wrefresh(cur_ms->ms_screen);
500 getyx(cur_ms->ms_input, y, x);
509 /* these should be obtained by doing ioctl() on tty */
518 wmove(cur_ms->ms_input, y, 0);
519 wclrtoeol(cur_ms->ms_input);
521 x = getmaxx(cur_ms->ms_input) - 1;
533 /* (buflen - 1) leaves room for the \0 */
534 if (isprint(c) && (p - buf < buflen - 1))
536 waddch(cur_ms->ms_input, c);
539 if (x >= getmaxx(cur_ms->ms_input))
551 waddch(cur_ms->ms_input, '\n');
553 wclrtoeol(cur_ms->ms_input);
557 strcpy(buf, strtrim(buf));
561 #endif /* HAVE_CURSES */
565 printf("%s", prompt);
566 if (!fgets(bigbuf, BUFSIZ, stdin))
574 strncpy(buf, strtrim(bigbuf), buflen);
575 if (strchr(buf, '\n'))
576 *strchr(buf, '\n') = '\0';
578 buf[buflen - 1] = '\0';
586 /* This routine will cause the most recently put message to be the
587 one at the top of the screen when a ---More--- prompt is displayed */
588 void Start_paging(void)
591 if (cur_ms != NULLMS)
592 lines_left = LINES - cur_ms->ms_input_y - 1;
594 #endif /* HAVE_CURSES */
598 /* Turn off paging */
599 void Stop_paging(void)
604 /* Print a message in the input window of cur_ms. */
605 void Put_message(char *msg)
607 char *copy, *line, *s;
613 /* if we're doing logging; we assume that the file has already
615 fprintf(log_file, "%s\n", msg);
621 if (s - line >= COLS - 1)
623 strncpy(copy, line, COLS - 1);
624 copy[COLS - 1] = '\0';
641 /* Will be truncated to COLS characters. */
642 void Put_line(char *msg)
652 if (--lines_left == 0)
654 /* Give the user a more prompt */
656 if (cur_ms != NULLMS)
658 wstandout(cur_ms->ms_input);
659 wprintw(cur_ms->ms_input, "---More---");
660 wstandend(cur_ms->ms_input);
662 chr = getchar() & 0x7f;/* We do care what it is */
663 if (chr == 'q' || chr == 'Q' || chr == 3 /* ^C */)
668 getyx(cur_ms->ms_input, y, x);
669 /* x is a bitbucket; avoid lint problems */
671 wmove(cur_ms->ms_input, y, 0);
672 wclrtoeol(cur_ms->ms_input);
675 #endif /* HAVE_CURSES */
677 printf("---More (hit return)---");
680 Start_paging(); /* Reset lines_left */
685 if (cur_ms != NULLMS)
687 msg1 = calloc(COLS, 1);
688 strncpy(msg1, msg, COLS - 1);
689 for (i = strlen(msg1); i < COLS - 1; i++)
691 wprintw(cur_ms->ms_input, "%s\n", msg1);
694 #endif /* HAVE_CURSES */
699 /* Refresh a menu_screen onto the real screen */
700 void refresh_ms(struct menu_screen *ms)
702 wrefresh(ms->ms_title);
703 wrefresh(ms->ms_menu);
704 wrefresh(ms->ms_input);
706 #endif /* HAVE_CURSES */
708 /* Parse buf into a list of words, which will be placed in strings specified by
709 argv. Space for these strings must have already been allocated.
710 Only the first n characters of each word will be copied */
711 int Parse_words(char *buf, char *argv[], int n)
713 char *start, *end; /* For sausage machine */
717 for (argc = 0; argc < MAX_ARGC; argc++)
719 while (isspace(*start))
720 start++; /* Kill whitespace */
722 break; /* Nothing left */
723 /* Now find the end of the word */
724 for (end = start; *end != '\0' && !isspace(*end); end++)
726 strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
727 argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */
733 /* This is the internal form of Find_command, which recursively searches
734 for a menu_line with command command in the specified menu */
735 /* It will search to a maximum depth of d */
736 struct menu_line *find_command_from(char *c, struct menu *m, int d)
739 struct menu_line *maybe;
742 return NULL; /* Too deep! */
743 for (line = 0; line < m->m_length; line++)
745 if (!strcmp(c, m->m_lines[line].ml_command))
746 return &m->m_lines[line];
748 for (line = 0; line < m->m_length; line++)
750 if (m->m_lines[line].ml_submenu != NULLMENU &&
751 (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1)))
754 /* If we got to here, nothing works */
758 /* Find_command searches down the current menu tree */
759 /* And returns a pointer to a menu_line with the specified command name */
760 /* It returns (struct menu_line *) 0 if none is found */
761 struct menu_line *Find_command(Menu *m, char *command)
766 return find_command_from(command, m, MAX_MENU_DEPTH);
769 int toggle_logging(int argc, char *argv[])
775 sprintf(buf, "/var/tmp/%s-log.%ld", whoami, (long)getpid());
778 log_file = fopen(buf, "a");
781 Put_message("Open of log file failed. Logging is not on.");
783 Put_message("Log file successfully opened.");
786 { /* log_file is a valid pointer; turn off logging. */
790 Put_message("Logging off.");