]> andersk Git - moira.git/blob - clients/moira/menu.c
update for new com_err library
[moira.git] / clients / moira / menu.c
1 /*
2  * Copyright 1987 by the Massachusetts Institute of Technology.
3  * For copying and distribution information, see the file
4  * "mit-copyright.h".
5  *
6  * $Source$
7  * $Author$
8  * $Header$
9  *
10  * Generic menu system module.
11  *
12  * Basically, we define an enormous tree structure which represents the
13  * menu.  Some extra pieces (ml_command, ma_doc) get thrown in so we can
14  * also use the structure for a command-based system.
15  *
16  * By making the menu descriptions so general, we can ease porting to just
17  * about anything.
18  */
19
20 #ifndef lint
21 static char rcsid_menu_c[] = "$Header$";
22
23 #endif lint
24
25 #include <mit-copyright.h>
26 #include <sys/types.h>
27 #include <stdio.h>
28 #include <signal.h>
29 #include <curses.h>
30 #include <ctype.h>
31 #include <strings.h>
32 #include <varargs.h>
33 #include <com_err.h>
34 #include "menu.h"
35
36 #ifndef __STDC__
37 #define const
38 #endif
39
40 #define MAX(A,B)        ((A) > (B) ? (A) : (B))
41 #define MIN(A,B)        ((A) < (B) ? (A) : (B))
42 #define CTL(ch)         ((ch) & 037)
43
44 #define MIN_INPUT 2             /* Minimum number of lines for input window */
45
46 extern FILE *fdopen();
47 extern int getpid();
48 extern char *calloc();
49 extern char *whoami;
50
51 FILE *log_file = (FILE *) NULL;         /* file stream of log file */
52 int more_flg = 1;
53
54 /* Structure for holding current displayed menu */
55 struct menu_screen {
56     WINDOW *ms_screen;          /* Window for this menu */
57     WINDOW *ms_title;           /* Title subwindow */
58     WINDOW *ms_menu;            /* Menu subwindow */
59     WINDOW *ms_input;           /* Input subwindow */
60     int ms_input_y;             /* Input subwindow reference coordinate */
61 }          *cur_ms;
62
63 #define NULLMS ((struct menu_screen *) 0)
64
65 Menu *top_menu;                 /* Root for command search */
66
67 /*
68  * Hook function to cause error messages to be printed through
69  * curses instead of around it.
70  */
71
72 void
73 menu_com_err_hook(who, code, fmt, args)
74     const char *who;
75     long code;
76     const char *fmt;
77     va_list args;
78 {
79     char buf[BUFSIZ], *cp;
80
81     FILE _strbuf;
82
83     (void) strcpy(buf, who);
84     for (cp = buf; *cp; cp++);
85     *cp++ = ':';
86     *cp++ = ' ';
87     if (code) {
88         (void) strcpy(cp, error_message(code));
89         while (*cp)
90             cp++;
91     }
92     _strbuf._flag = _IOWRT + _IOSTRG;
93     _strbuf._ptr = cp;
94     _strbuf._cnt = BUFSIZ - (cp - buf);
95     _doprnt(fmt, args, &_strbuf);
96     (void) putc('\0', &_strbuf);
97     Put_message(buf);
98 }
99
100 /*
101  * Start_menu takes a menu as an argument.  It initializes curses u.s.w.,
102  * and a quit in any submenu should unwind back to here.  (it might not,
103  * if user functions which run their own menus don't cooperate.)
104  * Start_menu should only be called once, at the start of the program.
105  */
106 Start_menu(m)
107     Menu *m;
108 {
109     struct menu_screen *make_ms();
110 #ifdef __STDC__
111     register void (*old_hook)(const char *, long, const char *, va_list) = set_com_err_hook(menu_com_err_hook);
112 #else
113     register void (*old_hook)() = set_com_err_hook(menu_com_err_hook);
114 #endif
115     
116     if (initscr() == ERR) {
117         fputs("Can't initialize curses!\n", stderr);
118         Start_no_menu(m);
119     } else {
120         (void) raw();           /* We parse & print everything ourselves */
121         (void) noecho();
122         cur_ms = make_ms(0);    /* So we always have some current */
123                                 /* menu_screen */ 
124         /* Run the menu */
125         (void) Do_menu(m, -1, (char **) NULL);
126     }
127     (void) set_com_err_hook(old_hook);
128     Cleanup_menu();
129 }
130
131 Cleanup_menu()
132 {
133     if (cur_ms) {
134         (void) wclear(cur_ms->ms_screen);
135         (void) wrefresh(cur_ms->ms_screen);
136     }
137     endwin();
138 }
139     
140
141 /* Like Start_menu, except it doesn't print menus and doesn't use curses */
142 Start_no_menu(m)
143     Menu *m;
144 {
145     cur_ms = NULLMS;
146     COLS = 80;
147     /* Run the menu */
148     (void) Do_menu(m, -1, (char **) NULL);
149 }
150
151 /*
152  * Create a new menu screen template with the specified menu length
153  * and return it.
154  */
155 struct menu_screen *
156 make_ms(length)
157     int length;
158 {
159     struct menu_screen *ms;
160     char *malloc();
161
162     if (MAX_TITLE + length + MIN_INPUT > LINES) {
163         fputs("Menu too big!\n", stderr);
164         exit(2);
165     }
166
167     ms = (struct menu_screen *) malloc(sizeof(struct menu_screen));
168
169     ms->ms_screen = newwin(0, 0, 0, 0);
170     ms->ms_title = subwin(ms->ms_screen, MAX_TITLE, 0, 0, 0);
171     ms->ms_menu = subwin(ms->ms_screen,
172                          length, 0, MAX_TITLE, 0);
173     ms->ms_input = subwin(ms->ms_screen, 0, 0,
174                           ms->ms_input_y = MAX_TITLE + length,
175                           0);
176
177     scrollok(ms->ms_input, TRUE);
178     (void) wmove(ms->ms_input, 0, 0);
179     (void) wclear(ms->ms_screen);
180
181     return (ms);
182 }
183
184 /*
185  * This routine destroys a menu_screen.
186  */
187 destroy_ms(ms)
188     struct menu_screen *ms;
189 {
190     delwin(ms->ms_title);
191     delwin(ms->ms_menu);
192     delwin(ms->ms_input);
193     delwin(ms->ms_screen);
194     free((char *) ms);
195 }
196
197 /*
198  * This guy actually puts up the menu
199  * Note: if margc < 0, no 'r' option will be displayed (i.e., on the
200  * top level menu)
201  */
202 int 
203 Do_menu(m, margc, margv)
204     Menu *m;
205     int margc;
206     char *margv[];
207 {
208     struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
209     char argvals[MAX_ARGC][MAX_ARGLEN]; /* This is where args are stored */
210     char buf[MAX_ARGC * MAX_ARGLEN];
211     char *argv[MAX_ARGC];
212     int line;
213     int i;
214     struct menu_line *command, *Find_command();
215     int argc;
216     int quitflag, is_topmenu = (margc < 0);
217     int toggle_logging();
218     
219     /* Entry function gets called with old menu_screen still current */
220     if (m->m_entry != NULLFUNC)
221         if (m->m_entry(m, margc, margv) == DM_QUIT)
222             return DM_NORMAL;
223
224     /* The following get run only in curses mode */
225     if (cur_ms != NULLMS) {
226         /* Get a menu_screen */
227         old_cur_ms = cur_ms;
228         /* 2 is for the 2 obligatory lines; quit and toggle */
229         cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
230
231         /* Now print the title and the menu */
232         (void) wclear(my_ms->ms_menu);
233         (void) wmove(my_ms->ms_title, 0, MAX(0, (COLS -
234                                                  strlen(m->m_title)) >> 1));
235         (void) wstandout(my_ms->ms_title);
236         (void) waddstr(my_ms->ms_title, m->m_title);
237         (void) wstandend(my_ms->ms_title);
238
239         for (line = 0; line < m->m_length; line++) {
240             int len = strlen(m->m_lines[line].ml_command);
241             if (len > 12) len=12;
242             
243             (void) wmove(my_ms->ms_menu, line, 0);
244             
245             (void) wprintw(my_ms->ms_menu, "%2d. (%s)%*s %s.", line + 1,
246                            m->m_lines[line].ml_command,
247                            12-len, "",
248                            m->m_lines[line].ml_doc);
249         }
250         (void) wmove(my_ms->ms_menu, line++, 0);
251         if (!is_topmenu) {
252             (void) waddstr(my_ms->ms_menu,
253                            " r. (return)       Return to previous menu.");
254             (void) wmove(my_ms->ms_menu, line++, 0);
255         }
256         (void) waddstr(my_ms->ms_menu,
257                            " t. (toggle)       Toggle logging on and off.");
258         (void) wmove(my_ms->ms_menu, line, 0);
259         (void) waddstr(my_ms->ms_menu, " q. (quit)         Quit.");
260     } else {
261         Put_message(m->m_title);
262         for (line = 0; line < m->m_length; line++) {
263             sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
264                     m->m_lines[line].ml_command,
265                     12 - strlen(m->m_lines[line].ml_command), "",
266                     m->m_lines[line].ml_doc);
267             Put_message(buf);
268         }
269         if (!is_topmenu)
270             Put_message(" r. (return)       Return to previous menu.");
271         Put_message(" t. (toggle)       Toggle logging on and off.");
272         Put_message(" q. (quit)         Quit.");
273         Put_message(" ?.                Print this information.");
274     }
275
276     for (;;) {
277         /* This will be set by a return val from func or submenu */
278         quitflag = DM_NORMAL;
279         /* This is here because we may be coming from another menu */
280         if (cur_ms != NULL)
281             touchwin(my_ms->ms_screen);
282         /* Get a command */
283         if (!Prompt_input("Command: ", buf, sizeof(buf)))
284             continue;
285         /* Parse it into the argument list */
286         /* If there's nothing there, try again */
287         /* Initialize argv */
288         for (argc = 0; argc < MAX_ARGC; argc++)
289             argv[argc] = argvals[argc];
290
291         if ((argc = Parse_words(buf, argv, MAX_ARGLEN)) == 0)
292             continue;
293         if ((line = atoi(argv[0])) > 0 && line <= m->m_length) {
294             command = &m->m_lines[line - 1];
295         }
296         else if ((!is_topmenu &&
297                   (!strcmp(argv[0], "r") || !strcmp(argv[0], "return")))
298                  || !strcmp(argv[0], "q") || !strcmp(argv[0], "quit")) {
299                  
300             /* here if it's either return or quit */
301             if (cur_ms != NULLMS) {
302                 cur_ms = old_cur_ms;
303                 destroy_ms(my_ms);
304             }
305             if (m->m_exit != NULLFUNC)
306                 m->m_exit(m);
307             return (*argv[0] == 'r' ? DM_NORMAL : DM_QUIT);
308         }
309         else if (argv[0][0] == '?') {
310             for (line = 0; line < m->m_length; line++) {
311                 sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
312                         m->m_lines[line].ml_command,
313                         12 - strlen(m->m_lines[line].ml_command), "",
314                         m->m_lines[line].ml_doc);
315                 Put_message(buf);
316             }
317             if (!is_topmenu)
318                 Put_message(" r. (return)       Return to previous menu.");
319             Put_message(" t. (toggle)       Toggle logging on and off.");
320             Put_message(" q. (quit)         Quit.");
321             continue;
322         }
323         else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle")) {
324             toggle_logging(argc, argv);
325             continue;
326         } 
327         /* finally, try to find it using Find_command */
328         else if ((command = Find_command(m, argvals[0])) ==
329                  (struct menu_line *) 0) {
330             Put_message("Command not recognized");
331             continue;
332         }
333         /* If we got to here, command is a valid menu_line */
334         /* Send the offical command name into the argv */
335         (void) strcpy(argvals[0], command->ml_command);
336         /* Show that we're working on it */
337         Put_message(command->ml_doc);
338         /* Print args that we've already got */
339         for (i = 1; i < argc; i++) {
340             if (command->ml_args[i].ma_prompt == NULL)
341                 break;
342             (void) sprintf(buf, "%s%s", command->ml_args[i].ma_prompt,
343                            argv[i]);
344             Put_message(buf);
345         }
346         /* Get remaining arguments, if any */
347         for (; argc < command->ml_argc; argc++) {
348             if (!Prompt_input(command->ml_args[argc].ma_prompt,
349                               argvals[argc], sizeof(argvals[argc])))
350                 goto punt_command;
351         }
352         if (command->ml_function != NULLFUNC) {
353             /* If it's got a function, call it */
354             quitflag = command->ml_function(argc, argv);
355         }
356         else if (command->ml_submenu != NULLMENU) {
357             /* Else see if it is a submenu */
358             quitflag = Do_menu(command->ml_submenu, argc, argv);
359         }
360         else {
361             /* If it's got neither, something is wrong */
362             Put_message("*INTERNAL ERROR: NO FUNCTION OR MENU FOR LINE*");
363         }
364         if (quitflag == DM_QUIT) {
365             if (cur_ms != NULLMS) {
366                 cur_ms = old_cur_ms;
367                 destroy_ms(my_ms);
368             }
369             if (m->m_exit != NULLFUNC)
370                 m->m_exit(m);
371             return (DM_QUIT);
372         }
373     punt_command:
374         ;
375     }
376 }
377
378 /* Prompt the user for input in the input window of cur_ms */
379 int Prompt_input(prompt, buf, buflen)
380     char *prompt;
381     char *buf;
382     int buflen;
383 {
384     int c;
385     char *p;
386     int y, x, oldx, oldy;
387
388     if (cur_ms != NULLMS) {
389         more_flg = 1;
390         getyx(cur_ms->ms_input, y, x);
391         (void) wmove(cur_ms->ms_input, y, 0);
392
393         touchwin(cur_ms->ms_screen);
394         refresh_ms(cur_ms);
395         (void) waddstr(cur_ms->ms_input, prompt);
396         getyx(cur_ms->ms_input, y, x);
397
398         oldx = x;
399         oldy = y;
400         p = buf;
401         while(1) {
402             (void) wmove(cur_ms->ms_input, y, x);
403                 (void) touchwin(cur_ms->ms_screen);
404             (void) wclrtoeol(cur_ms->ms_input);
405             refresh_ms(cur_ms);
406             c = getchar() & 0x7f;
407             switch (c) {
408             case CTL('C'):
409                 *p = '\0';
410                 return 0;
411             case CTL('Z'):
412                 (void) kill(getpid(), SIGTSTP);
413                 touchwin(curscr);
414                 break;
415             case CTL('L'):
416                 (void) wclear(cur_ms->ms_input);
417                 (void) waddstr(cur_ms->ms_input, prompt);
418                 (void) touchwin(cur_ms->ms_screen);
419 #ifdef notdef
420                 refresh_ms(cur_ms);
421 #endif notdef
422                 getyx(cur_ms->ms_input, y, x);
423                 oldy = y;
424                 oldx = x;
425                 p = buf;
426                 break;
427
428             case '\n':
429             case '\r':
430                 goto end_input;
431                 /* these should be obtained by doing ioctl() on tty */
432             case '\b':
433             case '\177':
434                 if (p > buf) {
435                     p--;
436                     x--;
437                     if (x < 0) {
438                         (void) wmove(cur_ms->ms_input, y, 0);
439                         (void) wclrtoeol(cur_ms->ms_input);
440                         y--;
441                         x = cur_ms->ms_input->_maxx-1;
442                     }
443                 }
444                 break;
445             case CTL('U'):
446             case CTL('G'):
447             case CTL('['):
448                 x = oldx;
449                 y = oldy;
450                 p = buf;
451                 break;
452             default:
453                 /* (buflen - 1) leaves room for the \0 */
454                 if (isprint(c) && (p - buf < buflen - 1)) {
455                     (void) waddch(cur_ms->ms_input, c);
456                     *p++ = c;
457                     x++;
458                     if (x >= cur_ms->ms_input->_maxx) {
459                         x = 0;
460                         y++;
461                     }
462                 } else
463                     putchar(CTL('G'));
464                 break;
465             }
466         }
467     end_input:
468         (void) waddch(cur_ms->ms_input, '\n');
469         (void) waddch(cur_ms->ms_input, '\r');
470         
471         (void) wclrtoeol(cur_ms->ms_input);
472         refresh_ms(cur_ms);
473         *p = '\0';
474         Start_paging();
475         goto gotit;
476     } else {
477         printf("%s", prompt);
478         if (gets(buf) == NULL)
479             return 0;
480         Start_paging();
481         goto gotit;
482     }
483 gotit:
484     strcpy(buf, strtrim(buf));
485     return 1;
486 }
487
488 /* Prompt the user for input in the input window of cur_ms, but don't echo
489    and allow some control characters */
490 int Password_input(prompt, buf, buflen)
491     char *prompt;
492     char *buf;
493     int buflen;
494 {
495     int c;
496     char *p;
497     int y, x, oldx;
498
499     if (cur_ms != NULLMS) {
500         more_flg = 1;
501         getyx(cur_ms->ms_input, y, x);
502         (void) wmove(cur_ms->ms_input, y, 0);
503
504         touchwin(cur_ms->ms_screen);
505         refresh_ms(cur_ms);
506         (void) waddstr(cur_ms->ms_input, prompt);
507         getyx(cur_ms->ms_input, y, x);
508
509         oldx = x;
510         for (p = buf; p - buf < buflen;) {
511             (void) wmove(cur_ms->ms_input, y, x);
512             (void) wclrtoeol(cur_ms->ms_input);
513             refresh_ms(cur_ms);
514             c = getchar() & 0x7f;
515             switch (c) {
516             case CTL('C'):
517                 return 0;
518             case CTL('Z'):
519                 (void) kill(getpid(), SIGTSTP);
520                 touchwin(curscr);
521                 break;
522             case CTL('L'):
523                 (void) wclear(cur_ms->ms_input);
524                 (void) waddstr(cur_ms->ms_input, prompt);
525                 refresh_ms(cur_ms);
526                 getyx(cur_ms->ms_input, y, x);
527                 break;
528             case '\n':
529             case '\r':
530                 (void) waddch(cur_ms->ms_input, '\n');
531                 (void) waddch(cur_ms->ms_input, '\r');
532
533                 (void) wclrtoeol(cur_ms->ms_input);
534                 refresh_ms(cur_ms);
535                 *p = '\0';
536                 Start_paging();
537                 return 1;
538             case '\b':
539             case '\177':
540                 if (p > buf) {
541                     p--;
542                     x--;
543                 }
544                 break;
545             case CTL('U'):
546                 x = oldx;
547                 p = buf;
548                 break;
549             default:
550                 *p++ = c;
551                 break;
552             }
553         }
554     }
555     else {
556         struct sgttyb ttybuf, nttybuf;
557         printf("%s", prompt);
558         /* turn off echoing */
559         (void) ioctl(0, TIOCGETP, (char *)&ttybuf);
560         nttybuf = ttybuf;
561         nttybuf.sg_flags &= ~ECHO;
562         (void)ioctl(0, TIOCSETP, (char *)&nttybuf);
563         if (gets(buf) == NULL) {
564             (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
565             putchar('\n');
566             return 0;
567         }
568         putchar('\n');
569         (void) ioctl(0, TIOCSETP, (char *)&ttybuf);
570         Start_paging();
571         return 1;
572     }
573     return 0;
574 }
575
576 int lines_left;
577
578 /* Start paging */
579 /* This routine will cause the most recently put message to be the
580    one at the top of the screen when a ---More--- prompt is displayed */
581 Start_paging()
582 {
583     if (cur_ms != NULLMS) {
584         lines_left = LINES - cur_ms->ms_input_y - 1;
585     }
586     else {
587         lines_left = 23;
588     }
589 }
590
591 /* Turn off paging */
592 Stop_paging()
593 {
594     lines_left = -1;
595 }
596
597 /* Print a message in the input window of cur_ms.  */
598 Put_message(msg)
599 char *msg;
600 {
601     char *copy, *line, *s;
602
603     copy = (char *) malloc((u_int)COLS);
604     s = line = msg;
605     if (log_file) {     /* if we're doing logging; we assume that the */
606                         /* file has already been opened. */
607         (void) fprintf(log_file, "%s\n", msg);
608         fflush(log_file);
609     } 
610
611     while(*s++) {
612         if (s - line >= COLS-1) {
613             (void) strncpy(copy, line, COLS-1);
614             line += COLS-1;
615         } else if (*s == '\n') {
616             *s = '\0';
617             (void) strcpy(copy, line);
618             line = ++s;
619         } else
620             continue;
621         Put_line(copy);
622     }
623     Put_line(line);
624     free(copy);
625 }
626
627 /* Will be truncated to COLS characters.  */
628 Put_line(msg)
629 char *msg;
630 {
631     int y, x, i;
632     char *msg1, chr;
633
634     if (!more_flg)
635         return;
636
637     if (lines_left >= 0) {
638         if (--lines_left == 0) {
639             /* Give the user a more prompt */
640             if (cur_ms != NULLMS) {
641                 (void) wstandout(cur_ms->ms_input);
642                 (void) wprintw(cur_ms->ms_input, "---More---");
643                 (void) wstandend(cur_ms->ms_input);
644                 refresh_ms(cur_ms);
645                 chr = getchar() & 0x7f;/* We do care what it is */
646                 if (chr == 'q' || chr == 'Q') {
647                     more_flg = 0;
648                     return;
649                 }
650                 getyx(cur_ms->ms_input, y, x);
651                 /* x is a bitbucket; avoid lint problems */
652                 x=x;
653                 (void) wmove(cur_ms->ms_input, y, 0);
654                 (void) wclrtoeol(cur_ms->ms_input);
655             }
656             else {
657                 printf("---More (hit return)---");
658                 getchar();
659             }
660             Start_paging();     /* Reset lines_left */
661         }
662     }
663
664     if (cur_ms != NULLMS) {
665         msg1 = (char *) calloc((u_int)COLS, 1);
666         (void) strncpy(msg1, msg, COLS-1);
667         for (i = strlen(msg1); i < COLS - 1; i++)
668             msg1[i] = ' ';
669         (void) wprintw(cur_ms->ms_input, "%s\n", msg1);
670 /*      refresh_ms(cur_ms); */
671     }
672     else {
673         puts(msg);
674     }
675 }
676
677 /* Refresh a menu_screen onto the real screen */
678 refresh_ms(ms)
679     struct menu_screen *ms;
680 {
681     int y, x;
682
683     getyx(ms->ms_input, y, x);
684     (void) wmove(ms->ms_screen, y + ms->ms_input_y, x);
685     (void) wrefresh(ms->ms_screen);
686 }
687
688 /* Parse buf into a list of words, which will be placed in strings specified by
689    argv.  Space for these strings must have already been allocated.
690    Only the first n characters of each word will be copied */
691 Parse_words(buf, argv, n)
692     char *buf;
693     char *argv[];
694 int n;
695
696 {
697     char *start, *end;          /* For sausage machine */
698     int argc;
699
700     start = buf;
701     for (argc = 0; argc < MAX_ARGC; argc++) {
702         while (isspace(*start))
703             start++;            /* Kill whitespace */
704         if (*start == '\0')
705             break;              /* Nothing left */
706         /* Now find the end of the word */
707         for (end = start; *end != '\0' && !isspace(*end); end++);
708         (void) strncpy(argv[argc], start, MIN(end - start, n)); /* Copy it */
709         argv[argc][MIN(end - start, n - 1)] = '\0';     /* Terminate */
710         start = end;
711     }
712     return (argc);
713 }
714
715 /* This is the internal form of Find_command, which recursively searches
716    for a menu_line with command command in the specified menu */
717 /* It will search to a maximum depth of d */
718 struct menu_line *
719 find_command_from(c, m, d)
720     char *c;
721     struct menu *m;
722     int d;
723 {
724     int line;
725     struct menu_line *maybe;
726
727     if (d < 0)
728         return ((struct menu_line *) 0);        /* Too deep! */
729     for (line = 0; line < m->m_length; line++) {
730         if (!strcmp(c, m->m_lines[line].ml_command)) {
731             return (&m->m_lines[line]);
732         }
733     }
734     for (line = 0; line < m->m_length; line++) {
735         if (m->m_lines[line].ml_submenu != NULLMENU  &&
736             (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1))
737                 != (struct menu_line *) 0) {
738             return (maybe);
739         }
740     }
741     /* If we got to here, nothing works */
742     return ((struct menu_line *) 0);
743 }
744
745 /* Find_command searches down the current menu tree */
746 /* And returns a pointer to a menu_line with the specified command name */
747 /* It returns (struct menu_line *) 0 if none is found */
748 struct menu_line *
749 Find_command(m, command)
750     Menu *m;
751     char *command;
752 {
753     if (m == NULLMENU) {
754         return ((struct menu_line *) 0);
755     }
756     else {
757         return (find_command_from(command, m, MAX_MENU_DEPTH));
758     }
759 }
760
761 /*ARGSUSED*/
762 int
763 toggle_logging(argc, argv)
764     int argc;
765     char *argv[];
766 {
767     int pid;
768     char buf[BUFSIZ];
769     
770     if (log_file == (FILE *) NULL) {
771         pid = getpid();
772         if (!whoami) {
773             Put_message("I've lost my SENSE of DIRECTION!  I have no IDEA who I AM!");
774             Put_message("My God... You've turned him into a DEMOCRAT!!");
775             Put_message("               -- Doonesbury");
776             Put_message("");
777             Put_message("translation:  your log file can be found in \"/usr/tmp/a.out.pid\".");
778             whoami = "a.out";
779             (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
780         }
781         else
782             (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
783
784         /* open the file */
785         log_file = fopen(buf,"a");
786
787         if (log_file == (FILE *) NULL)
788             Put_message("Open of log file failed.  Logging is not on.");
789         else
790             Put_message("Log file successfully opened.");
791     } 
792     else { /* log_file is a valid pointer; turn off logging. */
793         (void) fflush(log_file);
794         (void) fclose(log_file);
795         log_file = (FILE *) NULL;
796         Put_message("Logging off.");
797     }
798     return(DM_NORMAL);
799
This page took 0.560065 seconds and 5 git commands to generate.