]>
Commit | Line | Data |
---|---|---|
7ac48069 | 1 | /* $Id $ |
2aaf83c2 | 2 | * |
07c56447 | 3 | * Generic menu system module. |
4 | * | |
7ac48069 | 5 | * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology. |
6 | * For copying and distribution information, see the file | |
7 | * <mit-copyright.h>. | |
07c56447 | 8 | */ |
9 | ||
babbc197 | 10 | #include <mit-copyright.h> |
7ac48069 | 11 | #include <moira.h> |
12 | #include "menu.h" | |
13 | ||
14 | #include <ctype.h> | |
15 | #include <curses.h> | |
16 | #include <signal.h> | |
07c56447 | 17 | #include <stdio.h> |
7798ebc3 | 18 | #include <stdlib.h> |
cde7e985 | 19 | #include <string.h> |
cde7e985 | 20 | #include <unistd.h> |
7ac48069 | 21 | |
22 | RCSID("$Header$"); | |
07c56447 | 23 | |
5eaef520 | 24 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) |
25 | #define MIN(A, B) ((A) < (B) ? (A) : (B)) | |
967b7c08 | 26 | #define CTL(ch) ((ch) & 037) |
07c56447 | 27 | |
28 | #define MIN_INPUT 2 /* Minimum number of lines for input window */ | |
29 | ||
facd6784 | 30 | extern int interrupt; /* will be set if ^C is received */ |
859891c0 | 31 | extern char *whoami; |
32 | ||
33 | FILE *log_file = (FILE *) NULL; /* file stream of log file */ | |
e9db439c | 34 | int more_flg = 1; |
2aaf83c2 | 35 | |
07c56447 | 36 | /* Structure for holding current displayed menu */ |
37 | struct menu_screen { | |
5eaef520 | 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 */ | |
43 | } *cur_ms; | |
07c56447 | 44 | |
2aaf83c2 | 45 | #define NULLMS ((struct menu_screen *) 0) |
46 | ||
47 | Menu *top_menu; /* Root for command search */ | |
11317e48 | 48 | int parsed_argc; /* used by extern routines to get additional */ |
49 | char **parsed_argv; /* comand line input */ | |
2aaf83c2 | 50 | |
7ac48069 | 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[]); | |
60 | ||
6298c97d | 61 | /* |
62 | * Hook function to cause error messages to be printed through | |
22da5e41 | 63 | * curses instead of around it. Takes at most 5 args to the |
64 | * printf string (crock...) | |
6298c97d | 65 | */ |
66 | ||
7ac48069 | 67 | void menu_com_err_hook(const char *who, long code, const char *fmt, ...) |
6298c97d | 68 | { |
5eaef520 | 69 | char buf[BUFSIZ], *cp; |
7ac48069 | 70 | va_list ap; |
5eaef520 | 71 | |
72 | strcpy(buf, who); | |
73 | for (cp = buf; *cp; cp++) | |
74 | ; | |
75 | *cp++ = ':'; | |
76 | *cp++ = ' '; | |
77 | if (code) | |
78 | { | |
79 | strcpy(cp, error_message(code)); | |
80 | while (*cp) | |
81 | cp++; | |
6298c97d | 82 | } |
7ac48069 | 83 | va_start(ap, fmt); |
84 | vsprintf(cp, fmt, ap); | |
85 | va_end(ap); | |
5eaef520 | 86 | Put_message(buf); |
6298c97d | 87 | } |
88 | ||
07c56447 | 89 | /* |
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. | |
94 | */ | |
7ac48069 | 95 | void Start_menu(Menu *m) |
07c56447 | 96 | { |
44d12d58 | 97 | void (*old_hook)(const char *, long, const char *, va_list) = |
5eaef520 | 98 | set_com_err_hook((void (*) (const char *, long, const char *, va_list))menu_com_err_hook); |
99 | ||
100 | if (initscr() == (WINDOW *)ERR) | |
101 | { | |
102 | fputs("Can't initialize curses!\n", stderr); | |
103 | Start_no_menu(m); | |
07c56447 | 104 | } |
5eaef520 | 105 | else |
106 | { | |
107 | raw(); /* We parse & print everything ourselves */ | |
108 | noecho(); | |
109 | cur_ms = make_ms(0); /* So we always have some current */ | |
110 | /* menu_screen */ | |
111 | /* Run the menu */ | |
112 | Do_menu(m, -1, NULL); | |
113 | } | |
114 | set_com_err_hook(old_hook); | |
115 | Cleanup_menu(); | |
967b7c08 | 116 | } |
117 | ||
7ac48069 | 118 | void Cleanup_menu(void) |
967b7c08 | 119 | { |
5eaef520 | 120 | if (cur_ms) |
121 | { | |
122 | wclear(cur_ms->ms_screen); | |
123 | wrefresh(cur_ms->ms_screen); | |
967b7c08 | 124 | } |
5eaef520 | 125 | endwin(); |
07c56447 | 126 | } |
5eaef520 | 127 | |
07c56447 | 128 | |
2aaf83c2 | 129 | /* Like Start_menu, except it doesn't print menus and doesn't use curses */ |
7ac48069 | 130 | void Start_no_menu(Menu *m) |
2aaf83c2 | 131 | { |
5eaef520 | 132 | cur_ms = NULLMS; |
133 | COLS = 80; | |
134 | /* Run the menu */ | |
135 | Do_menu(m, -1, NULL); | |
2aaf83c2 | 136 | } |
137 | ||
07c56447 | 138 | /* |
139 | * Create a new menu screen template with the specified menu length | |
140 | * and return it. | |
141 | */ | |
5eaef520 | 142 | struct menu_screen *make_ms(int length) |
07c56447 | 143 | { |
5eaef520 | 144 | struct menu_screen *ms; |
07c56447 | 145 | |
5eaef520 | 146 | if (MAX_TITLE + length + MIN_INPUT > LINES) |
147 | { | |
148 | fputs("Menu too big!\n", stderr); | |
149 | exit(2); | |
07c56447 | 150 | } |
151 | ||
5eaef520 | 152 | ms = malloc(sizeof(struct menu_screen)); |
07c56447 | 153 | |
5eaef520 | 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, | |
159 | 0); | |
07c56447 | 160 | |
5eaef520 | 161 | scrollok(ms->ms_input, TRUE); |
162 | wmove(ms->ms_input, 0, 0); | |
163 | wclear(ms->ms_screen); | |
07c56447 | 164 | |
5eaef520 | 165 | return ms; |
07c56447 | 166 | } |
167 | ||
168 | /* | |
169 | * This routine destroys a menu_screen. | |
170 | */ | |
7ac48069 | 171 | void destroy_ms(struct menu_screen *ms) |
07c56447 | 172 | { |
5eaef520 | 173 | delwin(ms->ms_title); |
174 | delwin(ms->ms_menu); | |
175 | delwin(ms->ms_input); | |
176 | delwin(ms->ms_screen); | |
177 | free(ms); | |
07c56447 | 178 | } |
179 | ||
180 | /* | |
181 | * This guy actually puts up the menu | |
967b7c08 | 182 | * Note: if margc < 0, no 'r' option will be displayed (i.e., on the |
183 | * top level menu) | |
07c56447 | 184 | */ |
5eaef520 | 185 | int Do_menu(Menu *m, int margc, char *margv[]) |
07c56447 | 186 | { |
5eaef520 | 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]; | |
191 | int line; | |
192 | int i; | |
7ac48069 | 193 | struct menu_line *command; |
5eaef520 | 194 | int argc; |
195 | int quitflag, is_topmenu = (margc < 0); | |
5eaef520 | 196 | |
197 | /* Entry function gets called with old menu_screen still current */ | |
198 | if (m->m_entry != NULLFUNC) | |
199 | { | |
200 | if (m->m_entry(m, margc, margv) == DM_QUIT) | |
201 | return DM_NORMAL; | |
202 | if (parsed_argc > 0) | |
203 | { | |
204 | margc = parsed_argc + 1; | |
205 | margv = --parsed_argv; | |
206 | } | |
207 | else | |
208 | { | |
209 | margc--; | |
210 | margv++; | |
11317e48 | 211 | } |
212 | } | |
213 | ||
5eaef520 | 214 | parsed_argc = 0; |
215 | ||
216 | /* The following get run only in curses mode */ | |
217 | if (cur_ms != NULLMS) | |
218 | { | |
219 | /* Get a menu_screen */ | |
220 | old_cur_ms = cur_ms; | |
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)); | |
223 | ||
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); | |
231 | ||
232 | for (line = 0; line < m->m_length; line++) | |
233 | { | |
234 | int len = strlen(m->m_lines[line].ml_command); | |
235 | if (len > 12) | |
236 | len = 12; | |
237 | ||
238 | wmove(my_ms->ms_menu, line, 0); | |
239 | ||
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); | |
2aaf83c2 | 243 | } |
5eaef520 | 244 | wmove(my_ms->ms_menu, line++, 0); |
245 | if (!is_topmenu) | |
246 | { | |
247 | waddstr(my_ms->ms_menu, | |
248 | " r. (return) Return to previous menu."); | |
249 | wmove(my_ms->ms_menu, line++, 0); | |
967b7c08 | 250 | } |
5eaef520 | 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."); | |
254 | } | |
255 | else | |
256 | { | |
257 | Put_message(m->m_title); | |
258 | for (line = 0; line < m->m_length; line++) | |
259 | { | |
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); | |
264 | Put_message(buf); | |
33a56ee9 | 265 | } |
5eaef520 | 266 | if (!is_topmenu) |
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."); | |
07c56447 | 271 | } |
07c56447 | 272 | |
5eaef520 | 273 | for (;;) |
274 | { | |
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 */ | |
278 | if (cur_ms != NULL) | |
279 | { | |
280 | touchwin(my_ms->ms_screen); | |
281 | wrefresh(my_ms->ms_screen); | |
c3e65787 | 282 | } |
5eaef520 | 283 | if (margc > 1) |
284 | { | |
285 | /* Initialize argv */ | |
286 | for (argc = 0; argc < MAX_ARGC; argc++) | |
287 | argv[argc] = argvals[argc]; | |
288 | argc = margc - 1; | |
289 | for (i = 1; i < margc; i++) | |
290 | strcpy(argvals[i - 1], margv[i]); | |
291 | margc = 0; | |
e85f474d | 292 | } |
5eaef520 | 293 | else |
294 | { | |
295 | /* Get a command */ | |
296 | if (!Prompt_input("Command: ", buf, sizeof(buf))) | |
297 | { | |
298 | if (cur_ms == NULLMS && feof(stdin)) | |
299 | sprintf(buf, "quit"); | |
300 | else | |
301 | continue; | |
302 | } | |
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]; | |
308 | ||
309 | if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0) | |
310 | continue; | |
e9db439c | 311 | } |
5eaef520 | 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")) | |
317 | { | |
318 | /* here if it's either return or quit */ | |
319 | if (cur_ms != NULLMS) | |
320 | { | |
321 | cur_ms = old_cur_ms; | |
322 | destroy_ms(my_ms); | |
2aaf83c2 | 323 | } |
5eaef520 | 324 | if (m->m_exit != NULLFUNC) |
325 | m->m_exit(m); | |
326 | return *argv[0] == 'r' ? DM_NORMAL : DM_QUIT; | |
e9db439c | 327 | } |
5eaef520 | 328 | else if (argv[0][0] == '?') |
329 | { | |
330 | for (line = 0; line < m->m_length; line++) | |
331 | { | |
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); | |
336 | Put_message(buf); | |
33a56ee9 | 337 | } |
5eaef520 | 338 | if (!is_topmenu) |
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."); | |
342 | continue; | |
33a56ee9 | 343 | } |
5eaef520 | 344 | else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle")) |
345 | { | |
346 | toggle_logging(argc, argv); | |
347 | continue; | |
e9db439c | 348 | } |
5eaef520 | 349 | /* finally, try to find it using Find_command */ |
350 | else if (!(command = Find_command(m, argvals[0]))) | |
351 | { | |
352 | sprintf(buf, "Command not recognized: %s\n", argvals[0]); | |
353 | Put_message(buf); | |
354 | continue; | |
2aaf83c2 | 355 | } |
5eaef520 | 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++) | |
363 | { | |
364 | if (!command->ml_args[i].ma_prompt) | |
365 | break; | |
366 | sprintf(buf, "%s%s", command->ml_args[i].ma_prompt, argv[i]); | |
367 | Put_message(buf); | |
2aaf83c2 | 368 | } |
5eaef520 | 369 | /* Get remaining arguments, if any */ |
370 | for (; argc < command->ml_argc; argc++) | |
371 | { | |
372 | if (!Prompt_input(command->ml_args[argc].ma_prompt, | |
373 | argvals[argc], sizeof(argvals[argc]))) | |
374 | goto punt_command; | |
e9db439c | 375 | } |
5eaef520 | 376 | parsed_argc = argc - command->ml_argc; |
377 | parsed_argv = &(argv[command->ml_argc]); | |
378 | if (command->ml_function != NULLFUNC) | |
379 | { | |
380 | /* If it's got a function, call it */ | |
381 | quitflag = command->ml_function(argc, argv); | |
e9db439c | 382 | } |
5eaef520 | 383 | else if (command->ml_submenu != NULLMENU) |
384 | { | |
385 | /* Else see if it is a submenu */ | |
386 | quitflag = Do_menu(command->ml_submenu, argc, argv); | |
07c56447 | 387 | } |
5eaef520 | 388 | else |
389 | { | |
390 | /* If it's got neither, something is wrong */ | |
391 | Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*"); | |
392 | } | |
393 | if (quitflag == DM_QUIT) | |
394 | { | |
395 | if (cur_ms != NULLMS) | |
396 | { | |
397 | cur_ms = old_cur_ms; | |
398 | destroy_ms(my_ms); | |
2aaf83c2 | 399 | } |
5eaef520 | 400 | if (m->m_exit != NULLFUNC) |
401 | m->m_exit(m); | |
402 | parsed_argc = 0; | |
403 | return DM_QUIT; | |
07c56447 | 404 | } |
967b7c08 | 405 | punt_command: |
5eaef520 | 406 | parsed_argc = 0; |
07c56447 | 407 | } |
408 | } | |
409 | ||
7ac48069 | 410 | void refresh_screen(void) |
142e9b37 | 411 | { |
5eaef520 | 412 | if (cur_ms != NULLMS) |
413 | { | |
414 | touchwin(cur_ms->ms_screen); | |
415 | refresh_ms(cur_ms); | |
142e9b37 | 416 | } |
417 | } | |
418 | ||
419 | ||
07c56447 | 420 | /* Prompt the user for input in the input window of cur_ms */ |
5eaef520 | 421 | int Prompt_input(char *prompt, char *buf, int buflen) |
07c56447 | 422 | { |
5eaef520 | 423 | int c; |
424 | char *p; | |
425 | int y, x, oldx, oldy; | |
426 | ||
427 | if (cur_ms != NULLMS) | |
428 | { | |
429 | more_flg = 1; | |
430 | getyx(cur_ms->ms_input, y, x); | |
431 | wmove(cur_ms->ms_input, y, 0); | |
432 | ||
433 | refresh_screen(); | |
434 | waddstr(cur_ms->ms_input, prompt); | |
435 | getyx(cur_ms->ms_input, y, x); | |
436 | ||
437 | oldx = x; | |
438 | oldy = y; | |
439 | p = buf; | |
440 | while (1) | |
441 | { | |
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; | |
447 | switch (c) | |
448 | { | |
967b7c08 | 449 | case CTL('C'): |
5eaef520 | 450 | *p = '\0'; |
451 | return 0; | |
967b7c08 | 452 | case CTL('Z'): |
5eaef520 | 453 | kill(getpid(), SIGTSTP); |
454 | touchwin(cur_ms->ms_screen); | |
455 | break; | |
967b7c08 | 456 | case CTL('L'): |
5eaef520 | 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); | |
463 | oldy = y; | |
464 | oldx = x; | |
465 | p = buf; | |
466 | break; | |
6298c97d | 467 | |
e9db439c | 468 | case '\n': |
469 | case '\r': | |
5eaef520 | 470 | goto end_input; |
471 | /* these should be obtained by doing ioctl() on tty */ | |
2aaf83c2 | 472 | case '\b': |
473 | case '\177': | |
5eaef520 | 474 | if (p > buf) |
475 | { | |
476 | p--; | |
477 | x--; | |
478 | if (x < 0) | |
479 | { | |
480 | wmove(cur_ms->ms_input, y, 0); | |
481 | wclrtoeol(cur_ms->ms_input); | |
482 | y--; | |
7798ebc3 | 483 | #ifdef __NetBSD__ |
5eaef520 | 484 | x = cur_ms->ms_input->maxx - 1; |
7798ebc3 | 485 | #else |
5eaef520 | 486 | x = cur_ms->ms_input->_maxx - 1; |
7798ebc3 | 487 | #endif |
c13d1215 | 488 | } |
2aaf83c2 | 489 | } |
5eaef520 | 490 | break; |
967b7c08 | 491 | case CTL('U'): |
492 | case CTL('G'): | |
493 | case CTL('['): | |
5eaef520 | 494 | x = oldx; |
495 | y = oldy; | |
496 | p = buf; | |
497 | break; | |
2aaf83c2 | 498 | default: |
5eaef520 | 499 | /* (buflen - 1) leaves room for the \0 */ |
500 | if (isprint(c) && (p - buf < buflen - 1)) | |
501 | { | |
502 | waddch(cur_ms->ms_input, c); | |
503 | *p++ = c; | |
504 | x++; | |
7798ebc3 | 505 | #ifdef __NetBSD__ |
5eaef520 | 506 | if (x >= cur_ms->ms_input->maxx) |
7798ebc3 | 507 | #else |
5eaef520 | 508 | if (x >= cur_ms->ms_input->_maxx) |
7798ebc3 | 509 | #endif |
5eaef520 | 510 | { |
511 | x = 0; | |
512 | y++; | |
c13d1215 | 513 | } |
5eaef520 | 514 | } |
515 | else | |
516 | putchar(CTL('G')); | |
517 | break; | |
07c56447 | 518 | } |
07c56447 | 519 | } |
f659480d | 520 | end_input: |
5eaef520 | 521 | waddch(cur_ms->ms_input, '\n'); |
522 | ||
523 | wclrtoeol(cur_ms->ms_input); | |
524 | refresh_ms(cur_ms); | |
525 | *p = '\0'; | |
526 | Start_paging(); | |
527 | goto gotit; | |
528 | } | |
529 | else | |
530 | { | |
531 | printf("%s", prompt); | |
532 | if (!fgets(buf, buflen, stdin)) | |
533 | return 0; | |
534 | if (interrupt) | |
535 | { | |
536 | interrupt = 0; | |
537 | return 0; | |
facd6784 | 538 | } |
5eaef520 | 539 | Start_paging(); |
540 | goto gotit; | |
07c56447 | 541 | } |
6298c97d | 542 | gotit: |
5eaef520 | 543 | strcpy(buf, strtrim(buf)); |
544 | return 1; | |
07c56447 | 545 | } |
546 | ||
2aaf83c2 | 547 | int lines_left; |
548 | ||
549 | /* Start paging */ | |
550 | /* This routine will cause the most recently put message to be the | |
551 | one at the top of the screen when a ---More--- prompt is displayed */ | |
7ac48069 | 552 | void Start_paging(void) |
2aaf83c2 | 553 | { |
5eaef520 | 554 | if (cur_ms != NULLMS) |
555 | lines_left = LINES - cur_ms->ms_input_y - 1; | |
556 | else | |
557 | lines_left = 23; | |
2aaf83c2 | 558 | } |
559 | ||
560 | /* Turn off paging */ | |
7ac48069 | 561 | void Stop_paging(void) |
2aaf83c2 | 562 | { |
5eaef520 | 563 | lines_left = -1; |
2aaf83c2 | 564 | } |
565 | ||
f659480d | 566 | /* Print a message in the input window of cur_ms. */ |
7ac48069 | 567 | void Put_message(char *msg) |
f659480d | 568 | { |
5eaef520 | 569 | char *copy, *line, *s; |
570 | ||
571 | copy = malloc(COLS); | |
572 | s = line = msg; | |
573 | if (log_file) | |
574 | { | |
575 | /* if we're doing logging; we assume that the file has already | |
576 | been opened. */ | |
577 | fprintf(log_file, "%s\n", msg); | |
578 | fflush(log_file); | |
f659480d | 579 | } |
5eaef520 | 580 | |
581 | while (*s++) | |
582 | { | |
583 | if (s - line >= COLS - 1) | |
584 | { | |
585 | strncpy(copy, line, COLS - 1); | |
586 | copy[COLS - 1] = '\0'; | |
587 | line += COLS - 1; | |
588 | } | |
589 | else if (*s == '\n') | |
590 | { | |
591 | *s = '\0'; | |
592 | strcpy(copy, line); | |
593 | line = ++s; | |
594 | } | |
595 | else | |
596 | continue; | |
597 | Put_line(copy); | |
598 | } | |
599 | Put_line(line); | |
600 | free(copy); | |
f659480d | 601 | } |
602 | ||
603 | /* Will be truncated to COLS characters. */ | |
7ac48069 | 604 | void Put_line(char *msg) |
07c56447 | 605 | { |
5eaef520 | 606 | int y, x, i; |
607 | char *msg1, chr; | |
608 | ||
609 | if (!more_flg) | |
610 | return; | |
611 | ||
612 | if (lines_left >= 0) | |
613 | { | |
614 | if (--lines_left == 0) | |
615 | { | |
616 | /* Give the user a more prompt */ | |
617 | if (cur_ms != NULLMS) | |
618 | { | |
619 | wstandout(cur_ms->ms_input); | |
620 | wprintw(cur_ms->ms_input, "---More---"); | |
621 | wstandend(cur_ms->ms_input); | |
622 | refresh_ms(cur_ms); | |
623 | chr = getchar() & 0x7f;/* We do care what it is */ | |
624 | if (chr == 'q' || chr == 'Q' || chr == 3 /* ^C */) | |
625 | { | |
626 | more_flg = 0; | |
627 | return; | |
e9db439c | 628 | } |
5eaef520 | 629 | getyx(cur_ms->ms_input, y, x); |
630 | /* x is a bitbucket; avoid lint problems */ | |
631 | x = x; | |
632 | wmove(cur_ms->ms_input, y, 0); | |
633 | wclrtoeol(cur_ms->ms_input); | |
e9db439c | 634 | } |
5eaef520 | 635 | else |
636 | { | |
637 | printf("---More (hit return)---"); | |
638 | getchar(); | |
2aaf83c2 | 639 | } |
5eaef520 | 640 | Start_paging(); /* Reset lines_left */ |
2aaf83c2 | 641 | } |
642 | } | |
e9db439c | 643 | |
5eaef520 | 644 | if (cur_ms != NULLMS) |
645 | { | |
646 | msg1 = calloc(COLS, 1); | |
647 | strncpy(msg1, msg, COLS - 1); | |
648 | for (i = strlen(msg1); i < COLS - 1; i++) | |
649 | msg1[i] = ' '; | |
650 | wprintw(cur_ms->ms_input, "%s\n", msg1); | |
2aaf83c2 | 651 | } |
5eaef520 | 652 | else |
653 | puts(msg); | |
07c56447 | 654 | } |
655 | ||
656 | /* Refresh a menu_screen onto the real screen */ | |
7ac48069 | 657 | void refresh_ms(struct menu_screen *ms) |
07c56447 | 658 | { |
5eaef520 | 659 | wrefresh(ms->ms_title); |
660 | wrefresh(ms->ms_menu); | |
661 | wrefresh(ms->ms_input); | |
2aaf83c2 | 662 | } |
663 | ||
664 | /* Parse buf into a list of words, which will be placed in strings specified by | |
665 | argv. Space for these strings must have already been allocated. | |
666 | Only the first n characters of each word will be copied */ | |
5eaef520 | 667 | int Parse_words(char *buf, char *argv[], int n) |
2aaf83c2 | 668 | { |
5eaef520 | 669 | char *start, *end; /* For sausage machine */ |
670 | int argc; | |
671 | ||
672 | start = buf; | |
673 | for (argc = 0; argc < MAX_ARGC; argc++) | |
674 | { | |
675 | while (isspace(*start)) | |
676 | start++; /* Kill whitespace */ | |
677 | if (*start == '\0') | |
678 | break; /* Nothing left */ | |
679 | /* Now find the end of the word */ | |
680 | for (end = start; *end != '\0' && !isspace(*end); end++) | |
681 | ; | |
682 | strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */ | |
683 | argv[argc][MIN(end - start, n - 1)] = '\0'; /* Terminate */ | |
684 | start = end; | |
2aaf83c2 | 685 | } |
5eaef520 | 686 | return argc; |
2aaf83c2 | 687 | } |
688 | ||
689 | /* This is the internal form of Find_command, which recursively searches | |
690 | for a menu_line with command command in the specified menu */ | |
691 | /* It will search to a maximum depth of d */ | |
5eaef520 | 692 | struct menu_line *find_command_from(char *c, struct menu *m, int d) |
2aaf83c2 | 693 | { |
5eaef520 | 694 | int line; |
695 | struct menu_line *maybe; | |
696 | ||
697 | if (d < 0) | |
698 | return NULL; /* Too deep! */ | |
699 | for (line = 0; line < m->m_length; line++) | |
700 | { | |
701 | if (!strcmp(c, m->m_lines[line].ml_command)) | |
702 | return &m->m_lines[line]; | |
73b2adba | 703 | } |
5eaef520 | 704 | for (line = 0; line < m->m_length; line++) |
705 | { | |
706 | if (m->m_lines[line].ml_submenu != NULLMENU && | |
707 | (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1))) | |
708 | return maybe; | |
2aaf83c2 | 709 | } |
5eaef520 | 710 | /* If we got to here, nothing works */ |
711 | return NULL; | |
07c56447 | 712 | } |
2aaf83c2 | 713 | |
714 | /* Find_command searches down the current menu tree */ | |
715 | /* And returns a pointer to a menu_line with the specified command name */ | |
716 | /* It returns (struct menu_line *) 0 if none is found */ | |
5eaef520 | 717 | struct menu_line *Find_command(Menu *m, char *command) |
2aaf83c2 | 718 | { |
5eaef520 | 719 | if (m == NULLMENU) |
720 | return NULL; | |
721 | else | |
722 | return find_command_from(command, m, MAX_MENU_DEPTH); | |
2aaf83c2 | 723 | } |
22705d2b | 724 | |
5eaef520 | 725 | int toggle_logging(int argc, char *argv[]) |
859891c0 | 726 | { |
5eaef520 | 727 | char buf[BUFSIZ]; |
728 | ||
729 | if (!log_file) | |
730 | { | |
087d08b8 | 731 | sprintf(buf, "/var/tmp/%s-log.%ld", whoami, (long)getpid()); |
5eaef520 | 732 | |
733 | /* open the file */ | |
734 | log_file = fopen(buf, "a"); | |
735 | ||
736 | if (!log_file) | |
737 | Put_message("Open of log file failed. Logging is not on."); | |
738 | else | |
739 | Put_message("Log file successfully opened."); | |
859891c0 | 740 | } |
5eaef520 | 741 | else |
742 | { /* log_file is a valid pointer; turn off logging. */ | |
743 | fflush(log_file); | |
744 | fclose(log_file); | |
745 | log_file = NULL; | |
746 | Put_message("Logging off."); | |
747 | } | |
748 | return DM_NORMAL; | |
749 | } |