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>
24 #define MAX(A, B) ((A) > (B) ? (A) : (B))
25 #define MIN(A, B) ((A) < (B) ? (A) : (B))
26 #define CTL(ch) ((ch) & 037)
28 #define MIN_INPUT 2 /* Minimum number of lines for input window */
30 extern int interrupt; /* will be set if ^C is received */
33 FILE *log_file = (FILE *) NULL; /* file stream of log file */
36 /* Structure for holding current displayed menu */
38 WINDOW *ms_screen; /* Window for this menu */
39 WINDOW *ms_title; /* Title subwindow */
40 WINDOW *ms_menu; /* Menu subwindow */
41 WINDOW *ms_input; /* Input subwindow */
42 int ms_input_y; /* Input subwindow reference coordinate */
45 #define NULLMS ((struct menu_screen *) 0)
47 Menu *top_menu; /* Root for command search */
48 int parsed_argc; /* used by extern routines to get additional */
49 char **parsed_argv; /* comand line input */
51 int Parse_words(char *buf, char *argv[], int n);
52 void refresh_ms(struct menu_screen *ms);
53 void Put_line(char *msg);
54 void menu_com_err_hook(const char *who, long code, const char *fmt, ...);
55 struct menu_screen *make_ms(int length);
56 void destroy_ms(struct menu_screen *ms);
57 struct menu_line *find_command_from(char *c, struct menu *m, int d);
58 struct menu_line *Find_command(Menu *m, char *command);
59 int toggle_logging(int argc, char *argv[]);
62 * Hook function to cause error messages to be printed through
63 * curses instead of around it. Takes at most 5 args to the
64 * printf string (crock...)
67 void menu_com_err_hook(const char *who, long code, const char *fmt, ...)
69 char buf[BUFSIZ], *cp;
73 for (cp = buf; *cp; cp++)
79 strcpy(cp, error_message(code));
84 vsprintf(cp, fmt, ap);
90 * Start_menu takes a menu as an argument. It initializes curses u.s.w.,
91 * and a quit in any submenu should unwind back to here. (it might not,
92 * if user functions which run their own menus don't cooperate.)
93 * Start_menu should only be called once, at the start of the program.
95 void Start_menu(Menu *m)
97 void (*old_hook)(const char *, long, const char *, va_list) =
98 set_com_err_hook((void (*) (const char *, long, const char *, va_list))menu_com_err_hook);
100 if (initscr() == (WINDOW *)ERR)
102 fputs("Can't initialize curses!\n", stderr);
107 raw(); /* We parse & print everything ourselves */
109 cur_ms = make_ms(0); /* So we always have some current */
112 Do_menu(m, -1, NULL);
114 set_com_err_hook(old_hook);
118 void Cleanup_menu(void)
122 wclear(cur_ms->ms_screen);
123 wrefresh(cur_ms->ms_screen);
129 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
130 void Start_no_menu(Menu *m)
135 Do_menu(m, -1, NULL);
139 * Create a new menu screen template with the specified menu length
142 struct menu_screen *make_ms(int length)
144 struct menu_screen *ms;
146 if (MAX_TITLE + length + MIN_INPUT > LINES)
148 fputs("Menu too big!\n", stderr);
152 ms = malloc(sizeof(struct menu_screen));
154 ms->ms_screen = newwin(0, 0, 0, 0);
155 ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
156 ms->ms_menu = subwin(ms->ms_screen, length, 0, MAX_TITLE, 0);
157 ms->ms_input = subwin(ms->ms_screen, 0, 0,
158 ms->ms_input_y = MAX_TITLE + length,
161 scrollok(ms->ms_input, TRUE);
162 wmove(ms->ms_input, 0, 0);
163 wclear(ms->ms_screen);
169 * This routine destroys a menu_screen.
171 void destroy_ms(struct menu_screen *ms)
173 delwin(ms->ms_title);
175 delwin(ms->ms_input);
176 delwin(ms->ms_screen);
181 * This guy actually puts up the menu
182 * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
185 int Do_menu(Menu *m, int margc, char *margv[])
187 struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
188 char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
189 char buf[MAX_ARGC * MAX_ARGLEN];
190 char *argv[MAX_ARGC];
193 struct menu_line *command;
195 int quitflag, is_topmenu = (margc < 0);
197 /* Entry function gets called with old menu_screen still current */
198 if (m->m_entry != NULLFUNC)
200 if (m->m_entry(m, margc, margv) == DM_QUIT)
204 margc = parsed_argc + 1;
205 margv = --parsed_argv;
216 /* The following get run only in curses mode */
217 if (cur_ms != NULLMS)
219 /* Get a menu_screen */
221 /* 2 is for the 2 obligatory lines; quit and toggle */
222 cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
224 /* Now print the title and the menu */
225 wclear(my_ms->ms_screen);
226 wrefresh(my_ms->ms_screen);
227 wmove(my_ms->ms_title, 0, MAX(0, (COLS - strlen(m->m_title)) >> 1));
228 wstandout(my_ms->ms_title);
229 waddstr(my_ms->ms_title, m->m_title);
230 wstandend(my_ms->ms_title);
232 for (line = 0; line < m->m_length; line++)
234 int len = strlen(m->m_lines[line].ml_command);
238 wmove(my_ms->ms_menu, line, 0);
240 wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
241 m->m_lines[line].ml_command, 12 - len, "",
242 m->m_lines[line].ml_doc);
244 wmove(my_ms->ms_menu, line++, 0);
247 waddstr(my_ms->ms_menu,
248 " r. (return) Return to previous menu.");
249 wmove(my_ms->ms_menu, line++, 0);
251 waddstr(my_ms->ms_menu, " t. (toggle) Toggle logging on and off.");
252 wmove(my_ms->ms_menu, line, 0);
253 waddstr(my_ms->ms_menu, " q. (quit) Quit.");
257 Put_message(m->m_title);
258 for (line = 0; line < m->m_length; line++)
260 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
261 m->m_lines[line].ml_command,
262 12 - strlen(m->m_lines[line].ml_command), "",
263 m->m_lines[line].ml_doc);
267 Put_message(" r. (return) Return to previous menu.");
268 Put_message(" t. (toggle) Toggle logging on and off.");
269 Put_message(" q. (quit) Quit.");
270 Put_message(" ?. Print this information.");
275 /* This will be set by a return val from func or submenu */
276 quitflag = DM_NORMAL;
277 /* This is here because we may be coming from another menu */
280 touchwin(my_ms->ms_screen);
281 wrefresh(my_ms->ms_screen);
285 /* Initialize argv */
286 for (argc = 0; argc < MAX_ARGC; argc++)
287 argv[argc] = argvals[argc];
289 for (i = 1; i < margc; i++)
290 strcpy(argvals[i - 1], margv[i]);
296 if (!Prompt_input("Command: ", buf, sizeof(buf)))
298 if (cur_ms == NULLMS && feof(stdin))
299 sprintf(buf, "quit");
303 /* Parse it into the argument list */
304 /* If there's nothing there, try again */
305 /* Initialize argv */
306 for (argc = 0; argc < MAX_ARGC; argc++)
307 argv[argc] = argvals[argc];
309 if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
312 if ((line = atoi(argv[0])) > 0 && line <= m->m_length)
313 command = &m->m_lines[line - 1];
314 else if ((!is_topmenu &&
315 (!strcmp(argv[0], "r") || !strcmp(argv[0], "return"))) ||
316 !strcmp(argv[0], "q") || !strcmp(argv[0], "quit"))
318 /* here if it's either return or quit */
319 if (cur_ms != NULLMS)
324 if (m->m_exit != NULLFUNC)
326 return *argv[0] == 'r' ? DM_NORMAL : DM_QUIT;
328 else if (argv[0][0] == '?')
330 for (line = 0; line < m->m_length; line++)
332 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
333 m->m_lines[line].ml_command,
334 12 - strlen(m->m_lines[line].ml_command), "",
335 m->m_lines[line].ml_doc);
339 Put_message(" r. (return) Return to previous menu.");
340 Put_message(" t. (toggle) Toggle logging on and off.");
341 Put_message(" q. (quit) Quit.");
344 else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle"))
346 toggle_logging(argc, argv);
349 /* finally, try to find it using Find_command */
350 else if (!(command = Find_command(m, argvals[0])))
352 sprintf(buf, "Command not recognized: %s\n", argvals[0]);
356 /* If we got to here, command is a valid menu_line */
357 /* Send the offical command name into the argv */
358 strcpy(argvals[0], command->ml_command);
359 /* Show that we're working on it */
360 Put_message(command->ml_doc);
361 /* Print args that we've already got */
362 for (i = 1; i < argc; i++)
364 if (!command->ml_args[i].ma_prompt)
366 sprintf(buf, "%s%s", command->ml_args[i].ma_prompt, argv[i]);
369 /* Get remaining arguments, if any */
370 for (; argc < command->ml_argc; argc++)
372 if (!Prompt_input(command->ml_args[argc].ma_prompt,
373 argvals[argc], sizeof(argvals[argc])))
376 parsed_argc = argc - command->ml_argc;
377 parsed_argv = &(argv[command->ml_argc]);
378 if (command->ml_function != NULLFUNC)
380 /* If it's got a function, call it */
381 quitflag = command->ml_function(argc, argv);
383 else if (command->ml_submenu != NULLMENU)
385 /* Else see if it is a submenu */
386 quitflag = Do_menu(command->ml_submenu, argc, argv);
390 /* If it's got neither, something is wrong */
391 Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
393 if (quitflag == DM_QUIT)
395 if (cur_ms != NULLMS)
400 if (m->m_exit != NULLFUNC)
410 void refresh_screen(void)
412 if (cur_ms != NULLMS)
414 touchwin(cur_ms->ms_screen);
420 /* Prompt the user for input in the input window of cur_ms */
421 int Prompt_input(char *prompt, char *buf, int buflen)
425 int y, x, oldx, oldy;
427 if (cur_ms != NULLMS)
430 getyx(cur_ms->ms_input, y, x);
431 wmove(cur_ms->ms_input, y, 0);
434 waddstr(cur_ms->ms_input, prompt);
435 getyx(cur_ms->ms_input, y, x);
442 wmove(cur_ms->ms_input, y, x);
443 touchwin(cur_ms->ms_screen);
444 wclrtoeol(cur_ms->ms_input);
445 wrefresh(cur_ms->ms_input);
446 c = getchar() & 0x7f;
453 kill(getpid(), SIGTSTP);
454 touchwin(cur_ms->ms_screen);
457 wclear(cur_ms->ms_input);
458 wmove(cur_ms->ms_input, 0, 0);
459 waddstr(cur_ms->ms_input, prompt);
460 touchwin(cur_ms->ms_input);
461 wrefresh(cur_ms->ms_screen);
462 getyx(cur_ms->ms_input, y, x);
471 /* these should be obtained by doing ioctl() on tty */
480 wmove(cur_ms->ms_input, y, 0);
481 wclrtoeol(cur_ms->ms_input);
484 x = cur_ms->ms_input->maxx - 1;
486 x = cur_ms->ms_input->_maxx - 1;
499 /* (buflen - 1) leaves room for the \0 */
500 if (isprint(c) && (p - buf < buflen - 1))
502 waddch(cur_ms->ms_input, c);
506 if (x >= cur_ms->ms_input->maxx)
508 if (x >= cur_ms->ms_input->_maxx)
521 waddch(cur_ms->ms_input, '\n');
523 wclrtoeol(cur_ms->ms_input);
527 strcpy(buf, strtrim(buf));
534 printf("%s", prompt);
535 if (!fgets(bigbuf, BUFSIZ, stdin))
543 strncpy(buf, strtrim(bigbuf), buflen);
544 if (strchr(buf, '\n'))
545 *strchr(buf, '\n') = '\0';
547 buf[buflen - 1] = '\0';
555 /* This routine will cause the most recently put message to be the
556 one at the top of the screen when a ---More--- prompt is displayed */
557 void Start_paging(void)
559 if (cur_ms != NULLMS)
560 lines_left = LINES - cur_ms->ms_input_y - 1;
565 /* Turn off paging */
566 void Stop_paging(void)
571 /* Print a message in the input window of cur_ms. */
572 void Put_message(char *msg)
574 char *copy, *line, *s;
580 /* if we're doing logging; we assume that the file has already
582 fprintf(log_file, "%s\n", msg);
588 if (s - line >= COLS - 1)
590 strncpy(copy, line, COLS - 1);
591 copy[COLS - 1] = '\0';
608 /* Will be truncated to COLS characters. */
609 void Put_line(char *msg)
619 if (--lines_left == 0)
621 /* Give the user a more prompt */
622 if (cur_ms != NULLMS)
624 wstandout(cur_ms->ms_input);
625 wprintw(cur_ms->ms_input, "---More---");
626 wstandend(cur_ms->ms_input);
628 chr = getchar() & 0x7f;/* We do care what it is */
629 if (chr == 'q' || chr == 'Q' || chr == 3 /* ^C */)
634 getyx(cur_ms->ms_input, y, x);
635 /* x is a bitbucket; avoid lint problems */
637 wmove(cur_ms->ms_input, y, 0);
638 wclrtoeol(cur_ms->ms_input);
642 printf("---More (hit return)---");
645 Start_paging(); /* Reset lines_left */
649 if (cur_ms != NULLMS)
651 msg1 = calloc(COLS, 1);
652 strncpy(msg1, msg, COLS - 1);
653 for (i = strlen(msg1); i < COLS - 1; i++)
655 wprintw(cur_ms->ms_input, "%s\n", msg1);
661 /* Refresh a menu_screen onto the real screen */
662 void refresh_ms(struct menu_screen *ms)
664 wrefresh(ms->ms_title);
665 wrefresh(ms->ms_menu);
666 wrefresh(ms->ms_input);
669 /* Parse buf into a list of words, which will be placed in strings specified by
670 argv. Space for these strings must have already been allocated.
671 Only the first n characters of each word will be copied */
672 int Parse_words(char *buf, char *argv[], int n)
674 char *start, *end; /* For sausage machine */
678 for (argc = 0; argc < MAX_ARGC; argc++)
680 while (isspace(*start))
681 start++; /* Kill whitespace */
683 break; /* Nothing left */
684 /* Now find the end of the word */
685 for (end = start; *end != '\0' && !isspace(*end); end++)
687 strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
688 argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */
694 /* This is the internal form of Find_command, which recursively searches
695 for a menu_line with command command in the specified menu */
696 /* It will search to a maximum depth of d */
697 struct menu_line *find_command_from(char *c, struct menu *m, int d)
700 struct menu_line *maybe;
703 return NULL; /* Too deep! */
704 for (line = 0; line < m->m_length; line++)
706 if (!strcmp(c, m->m_lines[line].ml_command))
707 return &m->m_lines[line];
709 for (line = 0; line < m->m_length; line++)
711 if (m->m_lines[line].ml_submenu != NULLMENU &&
712 (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1)))
715 /* If we got to here, nothing works */
719 /* Find_command searches down the current menu tree */
720 /* And returns a pointer to a menu_line with the specified command name */
721 /* It returns (struct menu_line *) 0 if none is found */
722 struct menu_line *Find_command(Menu *m, char *command)
727 return find_command_from(command, m, MAX_MENU_DEPTH);
730 int toggle_logging(int argc, char *argv[])
736 sprintf(buf, "/var/tmp/%s-log.%ld", whoami, (long)getpid());
739 log_file = fopen(buf, "a");
742 Put_message("Open of log file failed. Logging is not on.");
744 Put_message("Log file successfully opened.");
747 { /* log_file is a valid pointer; turn off logging. */
751 Put_message("Logging off.");