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, const char *fmt, ...);
67 struct menu_screen *make_ms(int length);
68 void destroy_ms(struct menu_screen *ms);
69 struct menu_line *find_command_from(char *c, struct menu *m, int d);
70 struct menu_line *Find_command(Menu *m, char *command);
71 int toggle_logging(int argc, char *argv[]);
74 * Hook function to cause error messages to be printed through
75 * curses instead of around it. Takes at most 5 args to the
76 * printf string (crock...)
79 void menu_com_err_hook(const char *who, long code, const char *fmt, ...)
81 char buf[BUFSIZ], *cp;
87 for (cp = buf; *cp; cp++)
99 strcpy(cp, error_message(code));
104 vsprintf(cp, fmt, ap);
111 * Start_menu takes a menu as an argument. It initializes curses u.s.w.,
112 * and a quit in any submenu should unwind back to here. (it might not,
113 * if user functions which run their own menus don't cooperate.)
114 * Start_menu should only be called once, at the start of the program.
116 void Start_menu(Menu *m)
118 void (*old_hook)(const char *, long, const char *, va_list) =
119 set_com_err_hook((void (*) (const char *, long, const char *, va_list))menu_com_err_hook);
120 #ifdef CURSES_HAS_NEWTERM
121 SCREEN *scrn = newterm(NULL, stdout, stdin);
123 WINDOW *scrn = initscr();
127 fputs("Can't initialize curses!\nReverting to -nomenu mode\n\n", stderr);
132 #ifdef CURSES_HAS_NEWTERM
137 raw(); /* We parse & print everything ourselves */
139 cur_ms = make_ms(0); /* So we always have some current */
142 Do_menu(m, -1, NULL);
145 set_com_err_hook(old_hook);
148 void Cleanup_menu(void)
152 wclear(cur_ms->ms_screen);
153 wrefresh(cur_ms->ms_screen);
159 * Create a new menu screen template with the specified menu length
162 struct menu_screen *make_ms(int length)
164 struct menu_screen *ms;
166 if (MAX_TITLE + length + MIN_INPUT > LINES)
168 fputs("Menu too big!\n", stderr);
172 ms = malloc(sizeof(struct menu_screen));
174 ms->ms_screen = newwin(0, 0, 0, 0);
175 ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
176 ms->ms_menu = subwin(ms->ms_screen, length, 0, MAX_TITLE, 0);
177 ms->ms_input = subwin(ms->ms_screen, 0, 0,
178 ms->ms_input_y = MAX_TITLE + length,
181 scrollok(ms->ms_input, TRUE);
182 wmove(ms->ms_input, 0, 0);
183 wclear(ms->ms_screen);
189 * This routine destroys a menu_screen.
191 void destroy_ms(struct menu_screen *ms)
193 delwin(ms->ms_title);
195 delwin(ms->ms_input);
196 delwin(ms->ms_screen);
199 #endif /* HAVE_CURSES */
201 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
202 void Start_no_menu(Menu *m)
207 Do_menu(m, -1, NULL);
211 * This guy actually puts up the menu
212 * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
215 int Do_menu(Menu *m, int margc, char *margv[])
217 struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
218 char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
219 char buf[MAX_ARGC * MAX_ARGLEN];
220 char *argv[MAX_ARGC];
223 struct menu_line *command;
225 int quitflag, is_topmenu = (margc < 0);
227 /* Entry function gets called with old menu_screen still current */
228 if (m->m_entry != NULLFUNC)
230 if (m->m_entry(m, margc, margv) == DM_QUIT)
234 margc = parsed_argc + 1;
235 margv = --parsed_argv;
247 /* The following get run only in curses mode */
248 if (cur_ms != NULLMS)
250 /* Get a menu_screen */
252 /* 2 is for the 2 obligatory lines; quit and toggle */
253 cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
255 /* Now print the title and the menu */
256 wclear(my_ms->ms_screen);
257 wrefresh(my_ms->ms_screen);
258 wmove(my_ms->ms_title, 0, MAX(0, (COLS - (int)strlen(m->m_title)) >> 1));
259 wstandout(my_ms->ms_title);
260 waddstr(my_ms->ms_title, m->m_title);
261 wstandend(my_ms->ms_title);
263 for (line = 0; line < m->m_length; line++)
265 int len = strlen(m->m_lines[line].ml_command);
269 wmove(my_ms->ms_menu, line, 0);
271 wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
272 m->m_lines[line].ml_command, 12 - len, "",
273 m->m_lines[line].ml_doc);
275 wmove(my_ms->ms_menu, line++, 0);
278 waddstr(my_ms->ms_menu,
279 " r. (return) Return to previous menu.");
280 wmove(my_ms->ms_menu, line++, 0);
282 waddstr(my_ms->ms_menu, " t. (toggle) Toggle logging on and off.");
283 wmove(my_ms->ms_menu, line, 0);
284 waddstr(my_ms->ms_menu, " q. (quit) Quit.");
287 #endif /* HAVE_CURSES */
289 Put_message(m->m_title);
290 for (line = 0; line < m->m_length; line++)
292 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
293 m->m_lines[line].ml_command,
294 12 - strlen(m->m_lines[line].ml_command), "",
295 m->m_lines[line].ml_doc);
299 Put_message(" r. (return) Return to previous menu.");
300 Put_message(" t. (toggle) Toggle logging on and off.");
301 Put_message(" q. (quit) Quit.");
302 Put_message(" ?. Print this information.");
307 /* This will be set by a return val from func or submenu */
308 quitflag = DM_NORMAL;
310 /* This is here because we may be coming from another menu */
313 touchwin(my_ms->ms_screen);
314 wrefresh(my_ms->ms_screen);
316 #endif /* HAVE_CURSES */
319 /* Initialize argv */
320 for (argc = 0; argc < MAX_ARGC; argc++)
321 argv[argc] = argvals[argc];
323 for (i = 1; i < margc; i++)
324 strcpy(argvals[i - 1], margv[i]);
330 if (!Prompt_input("Command: ", buf, sizeof(buf)))
332 if (cur_ms == NULLMS && feof(stdin))
333 sprintf(buf, "quit");
337 /* Parse it into the argument list */
338 /* If there's nothing there, try again */
339 /* Initialize argv */
340 for (argc = 0; argc < MAX_ARGC; argc++)
341 argv[argc] = argvals[argc];
343 if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
346 if ((line = atoi(argv[0])) > 0 && line <= m->m_length)
347 command = &m->m_lines[line - 1];
348 else if ((!is_topmenu &&
349 (!strcmp(argv[0], "r") || !strcmp(argv[0], "return"))) ||
350 !strcmp(argv[0], "q") || !strcmp(argv[0], "quit"))
352 /* here if it's either return or quit */
354 if (cur_ms != NULLMS)
359 #endif /* HAVE_CURSES */
360 if (m->m_exit != NULLFUNC)
362 return *argv[0] == 'r' ? DM_NORMAL : DM_QUIT;
364 else if (argv[0][0] == '?')
366 for (line = 0; line < m->m_length; line++)
368 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
369 m->m_lines[line].ml_command,
370 12 - strlen(m->m_lines[line].ml_command), "",
371 m->m_lines[line].ml_doc);
375 Put_message(" r. (return) Return to previous menu.");
376 Put_message(" t. (toggle) Toggle logging on and off.");
377 Put_message(" q. (quit) Quit.");
380 else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle"))
382 toggle_logging(argc, argv);
385 /* finally, try to find it using Find_command */
386 else if (!(command = Find_command(m, argvals[0])))
388 sprintf(buf, "Command not recognized: %s\n", argvals[0]);
392 /* If we got to here, command is a valid menu_line */
393 /* Send the offical command name into the argv */
394 strcpy(argvals[0], command->ml_command);
395 /* Show that we're working on it */
396 Put_message(command->ml_doc);
397 /* Print args that we've already got */
398 for (i = 1; i < argc; i++)
400 if (!command->ml_args[i].ma_prompt)
402 sprintf(buf, "%s%s", command->ml_args[i].ma_prompt, argv[i]);
405 /* Get remaining arguments, if any */
406 for (; argc < command->ml_argc; argc++)
408 if (!Prompt_input(command->ml_args[argc].ma_prompt,
409 argvals[argc], sizeof(argvals[argc])))
412 parsed_argc = argc - command->ml_argc;
413 parsed_argv = &(argv[command->ml_argc]);
414 if (command->ml_function != NULLFUNC)
416 /* If it's got a function, call it */
417 quitflag = command->ml_function(argc, argv);
419 else if (command->ml_submenu != NULLMENU)
421 /* Else see if it is a submenu */
422 quitflag = Do_menu(command->ml_submenu, argc, argv);
426 /* If it's got neither, something is wrong */
427 Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
429 if (quitflag == DM_QUIT)
432 if (cur_ms != NULLMS)
437 #endif /* HAVE_CURSES */
438 if (m->m_exit != NULLFUNC)
448 void refresh_screen(void)
451 if (cur_ms != NULLMS)
453 touchwin(cur_ms->ms_screen);
456 #endif /* HAVE_CURSES */
460 /* Prompt the user for input in the input window of cur_ms */
461 int Prompt_input(char *prompt, char *buf, int buflen)
465 int y, x, oldx, oldy;
468 if (cur_ms != NULLMS)
471 getyx(cur_ms->ms_input, y, x);
472 wmove(cur_ms->ms_input, y, 0);
475 waddstr(cur_ms->ms_input, prompt);
476 getyx(cur_ms->ms_input, y, x);
483 wmove(cur_ms->ms_input, y, x);
484 touchwin(cur_ms->ms_screen);
485 wclrtoeol(cur_ms->ms_input);
486 wrefresh(cur_ms->ms_input);
487 c = getchar() & 0x7f;
494 kill(getpid(), SIGTSTP);
495 touchwin(cur_ms->ms_screen);
498 wclear(cur_ms->ms_input);
499 wmove(cur_ms->ms_input, 0, 0);
500 waddstr(cur_ms->ms_input, prompt);
501 touchwin(cur_ms->ms_input);
502 wrefresh(cur_ms->ms_screen);
503 getyx(cur_ms->ms_input, y, x);
512 /* these should be obtained by doing ioctl() on tty */
521 wmove(cur_ms->ms_input, y, 0);
522 wclrtoeol(cur_ms->ms_input);
524 x = getmaxx(cur_ms->ms_input) - 1;
536 /* (buflen - 1) leaves room for the \0 */
537 if (isprint(c) && (p - buf < buflen - 1))
539 waddch(cur_ms->ms_input, c);
542 if (x >= getmaxx(cur_ms->ms_input))
554 waddch(cur_ms->ms_input, '\n');
556 wclrtoeol(cur_ms->ms_input);
560 strcpy(buf, strtrim(buf));
564 #endif /* HAVE_CURSES */
568 printf("%s", prompt);
569 if (!fgets(bigbuf, BUFSIZ, stdin))
577 strncpy(buf, strtrim(bigbuf), buflen);
578 if (strchr(buf, '\n'))
579 *strchr(buf, '\n') = '\0';
581 buf[buflen - 1] = '\0';
589 /* This routine will cause the most recently put message to be the
590 one at the top of the screen when a ---More--- prompt is displayed */
591 void Start_paging(void)
594 if (cur_ms != NULLMS)
595 lines_left = LINES - cur_ms->ms_input_y - 1;
597 #endif /* HAVE_CURSES */
601 /* Turn off paging */
602 void Stop_paging(void)
607 /* Print a message in the input window of cur_ms. */
608 void Put_message(char *msg)
610 char *copy, *line, *s;
616 /* if we're doing logging; we assume that the file has already
618 fprintf(log_file, "%s\n", msg);
624 if (s - line >= COLS - 1)
626 strncpy(copy, line, COLS - 1);
627 copy[COLS - 1] = '\0';
644 /* Will be truncated to COLS characters. */
645 void Put_line(char *msg)
655 if (--lines_left == 0)
657 /* Give the user a more prompt */
659 if (cur_ms != NULLMS)
661 wstandout(cur_ms->ms_input);
662 wprintw(cur_ms->ms_input, "---More---");
663 wstandend(cur_ms->ms_input);
665 chr = getchar() & 0x7f;/* We do care what it is */
666 if (chr == 'q' || chr == 'Q' || chr == 3 /* ^C */)
671 getyx(cur_ms->ms_input, y, x);
672 /* x is a bitbucket; avoid lint problems */
674 wmove(cur_ms->ms_input, y, 0);
675 wclrtoeol(cur_ms->ms_input);
678 #endif /* HAVE_CURSES */
680 printf("---More (hit return)---");
683 Start_paging(); /* Reset lines_left */
688 if (cur_ms != NULLMS)
690 msg1 = calloc(COLS, 1);
691 strncpy(msg1, msg, COLS - 1);
692 for (i = strlen(msg1); i < COLS - 1; i++)
694 wprintw(cur_ms->ms_input, "%s\n", msg1);
697 #endif /* HAVE_CURSES */
702 /* Refresh a menu_screen onto the real screen */
703 void refresh_ms(struct menu_screen *ms)
705 wrefresh(ms->ms_title);
706 wrefresh(ms->ms_menu);
707 wrefresh(ms->ms_input);
709 #endif /* HAVE_CURSES */
711 /* Parse buf into a list of words, which will be placed in strings specified by
712 argv. Space for these strings must have already been allocated.
713 Only the first n characters of each word will be copied */
714 int Parse_words(char *buf, char *argv[], int n)
716 char *start, *end; /* For sausage machine */
720 for (argc = 0; argc < MAX_ARGC; argc++)
722 while (isspace(*start))
723 start++; /* Kill whitespace */
725 break; /* Nothing left */
726 /* Now find the end of the word */
727 for (end = start; *end != '\0' && !isspace(*end); end++)
729 strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
730 argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */
736 /* This is the internal form of Find_command, which recursively searches
737 for a menu_line with command command in the specified menu */
738 /* It will search to a maximum depth of d */
739 struct menu_line *find_command_from(char *c, struct menu *m, int d)
742 struct menu_line *maybe;
745 return NULL; /* Too deep! */
746 for (line = 0; line < m->m_length; line++)
748 if (!strcmp(c, m->m_lines[line].ml_command))
749 return &m->m_lines[line];
751 for (line = 0; line < m->m_length; line++)
753 if (m->m_lines[line].ml_submenu != NULLMENU &&
754 (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1)))
757 /* If we got to here, nothing works */
761 /* Find_command searches down the current menu tree */
762 /* And returns a pointer to a menu_line with the specified command name */
763 /* It returns (struct menu_line *) 0 if none is found */
764 struct menu_line *Find_command(Menu *m, char *command)
769 return find_command_from(command, m, MAX_MENU_DEPTH);
772 int toggle_logging(int argc, char *argv[])
778 sprintf(buf, "/var/tmp/%s-log.%ld", whoami, (long)getpid());
781 log_file = fopen(buf, "a");
784 Put_message("Open of log file failed. Logging is not on.");
786 Put_message("Log file successfully opened.");
789 { /* log_file is a valid pointer; turn off logging. */
793 Put_message("Logging off.");