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>
33 #define getchar() _getch()
34 #define getpid _getpid
43 #define MAX(A, B) ((A) > (B) ? (A) : (B))
44 #define MIN(A, B) ((A) < (B) ? (A) : (B))
45 #define CTL(ch) ((ch) & 037)
47 #define MIN_INPUT 2 /* Minimum number of lines for input window */
49 extern int interrupt; /* will be set if ^C is received */
52 FILE *log_file = (FILE *) NULL; /* file stream of log file */
55 /* Structure for holding current displayed menu */
58 WINDOW *ms_screen; /* Window for this menu */
59 WINDOW *ms_title; /* Title subwindow */
60 WINDOW *ms_menu; /* Menu subwindow */
61 WINDOW *ms_input; /* Input subwindow */
62 #endif /* HAVE_CURSES */
63 int ms_input_y; /* Input subwindow reference coordinate */
70 #define NULLMS ((struct menu_screen *) 0)
72 Menu *top_menu; /* Root for command search */
73 int parsed_argc; /* used by extern routines to get additional */
74 char **parsed_argv; /* comand line input */
76 int Parse_words(char *buf, char *argv[], int n);
77 void refresh_ms(struct menu_screen *ms);
78 void Put_line(char *msg);
79 void menu_com_err_hook(const char *who, long code,
80 const char *fmt, va_list ap);
81 struct menu_screen *make_ms(int length);
82 void destroy_ms(struct menu_screen *ms);
83 struct menu_line *find_command_from(char *c, struct menu *m, int d);
84 struct menu_line *Find_command(Menu *m, char *command);
85 int toggle_logging(int argc, char *argv[]);
88 * Hook function to cause error messages to be printed through
89 * curses instead of around it.
92 void menu_com_err_hook(const char *who, long code, const char *fmt, va_list ap)
94 char buf[BUFSIZ], *cp;
99 for (cp = buf; *cp; cp++)
111 strcpy(cp, error_message(code));
115 vsprintf(cp, fmt, ap);
121 * Start_menu takes a menu as an argument. It initializes curses u.s.w.,
122 * and a quit in any submenu should unwind back to here. (it might not,
123 * if user functions which run their own menus don't cooperate.)
124 * Start_menu should only be called once, at the start of the program.
126 void Start_menu(Menu *m)
128 void (*old_hook)(const char *, long, const char *, va_list) =
129 set_com_err_hook(menu_com_err_hook);
130 #ifdef CURSES_HAS_NEWTERM
131 SCREEN *scrn = newterm(NULL, stdout, stdin);
133 WINDOW *scrn = initscr();
137 fputs("Can't initialize curses!\nReverting to -nomenu mode\n\n", stderr);
142 #ifdef CURSES_HAS_NEWTERM
147 raw(); /* We parse & print everything ourselves */
149 cur_ms = make_ms(0); /* So we always have some current */
152 Do_menu(m, -1, NULL);
155 set_com_err_hook(old_hook);
158 void Cleanup_menu(void)
162 wclear(cur_ms->ms_screen);
163 wrefresh(cur_ms->ms_screen);
169 * Create a new menu screen template with the specified menu length
172 struct menu_screen *make_ms(int length)
174 struct menu_screen *ms;
176 if (MAX_TITLE + length + MIN_INPUT > LINES)
178 fputs("Menu too big!\n", stderr);
182 ms = malloc(sizeof(struct menu_screen));
184 ms->ms_screen = newwin(0, 0, 0, 0);
185 ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
186 ms->ms_menu = subwin(ms->ms_screen, length, 0, MAX_TITLE, 0);
187 ms->ms_input = subwin(ms->ms_screen, 0, 0,
188 ms->ms_input_y = MAX_TITLE + length,
191 scrollok(ms->ms_input, TRUE);
192 wmove(ms->ms_input, 0, 0);
193 wclear(ms->ms_screen);
199 * This routine destroys a menu_screen.
201 void destroy_ms(struct menu_screen *ms)
203 delwin(ms->ms_title);
205 delwin(ms->ms_input);
206 delwin(ms->ms_screen);
209 #endif /* HAVE_CURSES */
211 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
212 void Start_no_menu(Menu *m)
217 Do_menu(m, -1, NULL);
221 * This guy actually puts up the menu
222 * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
225 int Do_menu(Menu *m, int margc, char *margv[])
227 struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
228 char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
229 char buf[MAX_ARGC * MAX_ARGLEN];
230 char *argv[MAX_ARGC];
233 struct menu_line *command;
235 int quitflag, is_topmenu = (margc < 0);
237 /* Entry function gets called with old menu_screen still current */
238 if (m->m_entry != NULLFUNC)
240 if (m->m_entry(m, margc, margv) == DM_QUIT)
244 margc = parsed_argc + 1;
245 margv = --parsed_argv;
257 /* The following get run only in curses mode */
258 if (cur_ms != NULLMS)
260 /* Get a menu_screen */
262 /* 2 is for the 2 obligatory lines; quit and toggle */
263 cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
265 /* Now print the title and the menu */
266 wclear(my_ms->ms_screen);
267 wrefresh(my_ms->ms_screen);
268 wmove(my_ms->ms_title, 0, MAX(0, (COLS - (int)strlen(m->m_title)) >> 1));
269 wstandout(my_ms->ms_title);
270 waddstr(my_ms->ms_title, m->m_title);
271 wstandend(my_ms->ms_title);
273 for (line = 0; line < m->m_length; line++)
275 int len = strlen(m->m_lines[line].ml_command);
279 wmove(my_ms->ms_menu, line, 0);
281 wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
282 m->m_lines[line].ml_command, 12 - len, "",
283 m->m_lines[line].ml_doc);
285 wmove(my_ms->ms_menu, line++, 0);
288 waddstr(my_ms->ms_menu,
289 " r. (return) Return to previous menu.");
290 wmove(my_ms->ms_menu, line++, 0);
292 waddstr(my_ms->ms_menu, " t. (toggle) Toggle logging on and off.");
293 wmove(my_ms->ms_menu, line, 0);
294 waddstr(my_ms->ms_menu, " q. (quit) Quit.");
297 #endif /* HAVE_CURSES */
299 Put_message(m->m_title);
300 for (line = 0; line < m->m_length; line++)
302 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
303 m->m_lines[line].ml_command,
304 12 - strlen(m->m_lines[line].ml_command), "",
305 m->m_lines[line].ml_doc);
309 Put_message(" r. (return) Return to previous menu.");
310 Put_message(" t. (toggle) Toggle logging on and off.");
311 Put_message(" q. (quit) Quit.");
312 Put_message(" ?. Print this information.");
317 /* This will be set by a return val from func or submenu */
318 quitflag = DM_NORMAL;
320 /* This is here because we may be coming from another menu */
323 touchwin(my_ms->ms_screen);
324 wrefresh(my_ms->ms_screen);
326 #endif /* HAVE_CURSES */
329 /* Initialize argv */
330 for (argc = 0; argc < MAX_ARGC; argc++)
331 argv[argc] = argvals[argc];
333 for (i = 1; i < margc; i++)
334 strcpy(argvals[i - 1], margv[i]);
340 if (!Prompt_input("Command: ", buf, sizeof(buf)))
342 if (cur_ms == NULLMS && feof(stdin))
343 sprintf(buf, "quit");
347 /* Parse it into the argument list */
348 /* If there's nothing there, try again */
349 /* Initialize argv */
350 for (argc = 0; argc < MAX_ARGC; argc++)
351 argv[argc] = argvals[argc];
353 if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
356 if ((line = atoi(argv[0])) > 0 && line <= m->m_length)
357 command = &m->m_lines[line - 1];
358 else if ((!is_topmenu &&
359 (!strcmp(argv[0], "r") || !strcmp(argv[0], "return"))) ||
360 !strcmp(argv[0], "q") || !strcmp(argv[0], "quit"))
362 /* here if it's either return or quit */
364 if (cur_ms != NULLMS)
369 #endif /* HAVE_CURSES */
370 if (m->m_exit != NULLFUNC)
372 return *argv[0] == 'r' ? DM_NORMAL : DM_QUIT;
374 else if (argv[0][0] == '?')
376 for (line = 0; line < m->m_length; line++)
378 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
379 m->m_lines[line].ml_command,
380 12 - strlen(m->m_lines[line].ml_command), "",
381 m->m_lines[line].ml_doc);
385 Put_message(" r. (return) Return to previous menu.");
386 Put_message(" t. (toggle) Toggle logging on and off.");
387 Put_message(" q. (quit) Quit.");
390 else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle"))
392 toggle_logging(argc, argv);
395 /* finally, try to find it using Find_command */
396 else if (!(command = Find_command(m, argvals[0])))
398 sprintf(buf, "Command not recognized: %s\n", argvals[0]);
402 /* If we got to here, command is a valid menu_line */
403 /* Send the offical command name into the argv */
404 strcpy(argvals[0], command->ml_command);
405 /* Show that we're working on it */
406 Put_message(command->ml_doc);
407 /* Print args that we've already got */
408 for (i = 1; i < argc; i++)
410 if (!command->ml_args[i].ma_prompt)
412 sprintf(buf, "%s%s", command->ml_args[i].ma_prompt, argv[i]);
415 /* Get remaining arguments, if any */
416 for (; argc < command->ml_argc; argc++)
418 if (!Prompt_input(command->ml_args[argc].ma_prompt,
419 argvals[argc], sizeof(argvals[argc])))
422 parsed_argc = argc - command->ml_argc;
423 parsed_argv = &(argv[command->ml_argc]);
424 if (command->ml_function != NULLFUNC)
426 /* If it's got a function, call it */
427 quitflag = command->ml_function(argc, argv);
429 else if (command->ml_submenu != NULLMENU)
431 /* Else see if it is a submenu */
432 quitflag = Do_menu(command->ml_submenu, argc, argv);
436 /* If it's got neither, something is wrong */
437 Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
439 if (quitflag == DM_QUIT)
442 if (cur_ms != NULLMS)
447 #endif /* HAVE_CURSES */
448 if (m->m_exit != NULLFUNC)
458 void refresh_screen(void)
461 if (cur_ms != NULLMS)
463 touchwin(cur_ms->ms_screen);
466 #endif /* HAVE_CURSES */
470 /* Prompt the user for input in the input window of cur_ms */
471 int Prompt_input(char *prompt, char *buf, int buflen)
476 int y, x, oldx, oldy;
478 if (cur_ms != NULLMS)
481 getyx(cur_ms->ms_input, y, x);
482 wmove(cur_ms->ms_input, y, 0);
485 waddstr(cur_ms->ms_input, prompt);
486 getyx(cur_ms->ms_input, y, x);
493 wmove(cur_ms->ms_input, y, x);
494 touchwin(cur_ms->ms_screen);
495 wclrtoeol(cur_ms->ms_input);
496 wrefresh(cur_ms->ms_input);
497 c = getchar() & 0x7f;
504 kill(getpid(), SIGTSTP);
505 touchwin(cur_ms->ms_screen);
508 wclear(cur_ms->ms_input);
509 wmove(cur_ms->ms_input, 0, 0);
510 waddstr(cur_ms->ms_input, prompt);
511 touchwin(cur_ms->ms_input);
512 wrefresh(cur_ms->ms_screen);
513 getyx(cur_ms->ms_input, y, x);
522 /* these should be obtained by doing ioctl() on tty */
531 wmove(cur_ms->ms_input, y, 0);
532 wclrtoeol(cur_ms->ms_input);
534 x = getmaxx(cur_ms->ms_input) - 1;
546 /* (buflen - 1) leaves room for the \0 */
547 if (isprint(c) && (p - buf < buflen - 1))
549 waddch(cur_ms->ms_input, c);
552 if (x >= getmaxx(cur_ms->ms_input))
564 waddch(cur_ms->ms_input, '\n');
566 wclrtoeol(cur_ms->ms_input);
570 strcpy(buf, strtrim(buf));
574 #endif /* HAVE_CURSES */
578 printf("%s", prompt);
579 if (!fgets(bigbuf, BUFSIZ, stdin))
587 strncpy(buf, strtrim(bigbuf), buflen);
588 if (strchr(buf, '\n'))
589 *strchr(buf, '\n') = '\0';
591 buf[buflen - 1] = '\0';
599 /* This routine will cause the most recently put message to be the
600 one at the top of the screen when a ---More--- prompt is displayed */
601 void Start_paging(void)
604 if (cur_ms != NULLMS)
605 lines_left = LINES - cur_ms->ms_input_y - 1;
607 #endif /* HAVE_CURSES */
611 /* Turn off paging */
612 void Stop_paging(void)
617 /* Print a message in the input window of cur_ms. */
618 void Put_message(char *msg)
620 char *copy, *line, *s;
626 /* if we're doing logging; we assume that the file has already
628 fprintf(log_file, "%s\n", msg);
634 if (s - line >= COLS - 1)
636 strncpy(copy, line, COLS - 1);
637 copy[COLS - 1] = '\0';
654 /* Will be truncated to COLS characters. */
655 void Put_line(char *msg)
662 if (--lines_left == 0)
664 /* Give the user a more prompt */
666 if (cur_ms != NULLMS)
671 wstandout(cur_ms->ms_input);
672 wprintw(cur_ms->ms_input, "---More---");
673 wstandend(cur_ms->ms_input);
675 chr = getchar() & 0x7f;/* We do care what it is */
676 if (chr == 'q' || chr == 'Q' || chr == 3 /* ^C */)
681 getyx(cur_ms->ms_input, y, x);
682 /* x is a bitbucket; avoid lint problems */
684 wmove(cur_ms->ms_input, y, 0);
685 wclrtoeol(cur_ms->ms_input);
688 #endif /* HAVE_CURSES */
690 printf("---More (hit return)---");
693 Start_paging(); /* Reset lines_left */
698 if (cur_ms != NULLMS)
703 msg1 = calloc(COLS, 1);
704 strncpy(msg1, msg, COLS - 1);
705 for (i = strlen(msg1); i < COLS - 1; i++)
707 wprintw(cur_ms->ms_input, "%s\n", msg1);
710 #endif /* HAVE_CURSES */
715 /* Refresh a menu_screen onto the real screen */
716 void refresh_ms(struct menu_screen *ms)
718 wrefresh(ms->ms_title);
719 wrefresh(ms->ms_menu);
720 wrefresh(ms->ms_input);
722 #endif /* HAVE_CURSES */
724 /* Parse buf into a list of words, which will be placed in strings specified by
725 argv. Space for these strings must have already been allocated.
726 Only the first n characters of each word will be copied */
727 int Parse_words(char *buf, char *argv[], int n)
729 char *start, *end; /* For sausage machine */
733 for (argc = 0; argc < MAX_ARGC; argc++)
735 while (isspace(*start))
736 start++; /* Kill whitespace */
738 break; /* Nothing left */
739 /* Now find the end of the word */
740 for (end = start; *end != '\0' && !isspace(*end); end++)
742 strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
743 argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */
749 /* This is the internal form of Find_command, which recursively searches
750 for a menu_line with command command in the specified menu */
751 /* It will search to a maximum depth of d */
752 struct menu_line *find_command_from(char *c, struct menu *m, int d)
755 struct menu_line *maybe;
758 return NULL; /* Too deep! */
759 for (line = 0; line < m->m_length; line++)
761 if (!strcmp(c, m->m_lines[line].ml_command))
762 return &m->m_lines[line];
764 for (line = 0; line < m->m_length; line++)
766 if (m->m_lines[line].ml_submenu != NULLMENU &&
767 (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1)))
770 /* If we got to here, nothing works */
774 /* Find_command searches down the current menu tree */
775 /* And returns a pointer to a menu_line with the specified command name */
776 /* It returns (struct menu_line *) 0 if none is found */
777 struct menu_line *Find_command(Menu *m, char *command)
782 return find_command_from(command, m, MAX_MENU_DEPTH);
785 static char *get_tmp_dir(void)
788 static char tmp[BUFSIZ];
792 len = GetTempPath(sizeof(tmp), tmp);
793 if (!len || (len > sizeof(tmp)))
802 int toggle_logging(int argc, char *argv[])
808 sprintf(buf, "%s/%s-log.%ld", get_tmp_dir(), whoami, (long)getpid());
811 log_file = fopen(buf, "a");
814 Put_message("Open of log file failed. Logging is not on.");
816 Put_message("Log file successfully opened.");
819 { /* log_file is a valid pointer; turn off logging. */
823 Put_message("Logging off.");