]> andersk Git - moira.git/blobdiff - clients/moira/menu.c
Added support for DEBUG_LIBS so that when this is defined
[moira.git] / clients / moira / menu.c
index 89e81c7be80e972506c58c8728115e63e451e405..89d40ed2c19cd8efe91be00c0147c4d3519747cb 100644 (file)
@@ -22,6 +22,7 @@ static char rcsid_menu_c[] = "$Header$";
 
 #endif lint
 
+#include <mit-copyright.h>
 #include <sys/types.h>
 #include <stdio.h>
 #include <signal.h>
@@ -32,13 +33,22 @@ static char rcsid_menu_c[] = "$Header$";
 #include <com_err.h>
 #include "menu.h"
 
+#ifndef __STDC__
+#define const
+#endif
+
 #define MAX(A,B)       ((A) > (B) ? (A) : (B))
 #define MIN(A,B)       ((A) < (B) ? (A) : (B))
 #define CTL(ch)                ((ch) & 037)
 
 #define MIN_INPUT 2            /* Minimum number of lines for input window */
 
+extern FILE *fdopen();
+extern int getpid();
 extern char *calloc();
+extern char *whoami;
+
+FILE *log_file = (FILE *) NULL;                /* file stream of log file */
 int more_flg = 1;
 
 /* Structure for holding current displayed menu */
@@ -59,11 +69,11 @@ Menu *top_menu;                     /* Root for command search */
  * curses instead of around it.
  */
 
-int
+void
 menu_com_err_hook(who, code, fmt, args)
-    char *who;
-    int code;
-    char *fmt;
+    const char *who;
+    long code;
+    const char *fmt;
     va_list args;
 {
     char buf[BUFSIZ], *cp;
@@ -97,7 +107,11 @@ Start_menu(m)
     Menu *m;
 {
     struct menu_screen *make_ms();
-    register int (*old_hook)() = set_com_err_hook(menu_com_err_hook);
+#ifdef __STDC__
+    register void (*old_hook)(const char *, long, const char *, va_list) = set_com_err_hook(menu_com_err_hook);
+#else
+    register void (*old_hook)() = set_com_err_hook(menu_com_err_hook);
+#endif
     
     if (initscr() == ERR) {
        fputs("Can't initialize curses!\n", stderr);
@@ -106,8 +120,7 @@ Start_menu(m)
        (void) raw();           /* We parse & print everything ourselves */
        (void) noecho();
        cur_ms = make_ms(0);    /* So we always have some current */
-       /* menu_screen */ 
-       top_menu = m;
+                               /* menu_screen */ 
        /* Run the menu */
        (void) Do_menu(m, -1, (char **) NULL);
     }
@@ -130,7 +143,7 @@ Start_no_menu(m)
     Menu *m;
 {
     cur_ms = NULLMS;
-    top_menu = m;
+    COLS = 80;
     /* Run the menu */
     (void) Do_menu(m, -1, (char **) NULL);
 }
@@ -192,7 +205,7 @@ Do_menu(m, margc, margv)
     int margc;
     char *margv[];
 {
-    struct menu_screen *my_ms, *old_cur_ms;
+    struct menu_screen *my_ms = NULLMS, *old_cur_ms = NULLMS;
     char argvals[MAX_ARGC][MAX_ARGLEN];        /* This is where args are stored */
     char buf[MAX_ARGC * MAX_ARGLEN];
     char *argv[MAX_ARGC];
@@ -201,6 +214,7 @@ Do_menu(m, margc, margv)
     struct menu_line *command, *Find_command();
     int argc;
     int quitflag, is_topmenu = (margc < 0);
+    int toggle_logging();
     
     /* Entry function gets called with old menu_screen still current */
     if (m->m_entry != NULLFUNC)
@@ -211,7 +225,8 @@ Do_menu(m, margc, margv)
     if (cur_ms != NULLMS) {
        /* Get a menu_screen */
        old_cur_ms = cur_ms;
-       cur_ms = my_ms = make_ms(m->m_length + 1 + (is_topmenu?0:1));
+       /* 2 is for the 2 obligatory lines; quit and toggle */
+       cur_ms = my_ms = make_ms(m->m_length + 2 + (is_topmenu ? 0 : 1));
 
        /* Now print the title and the menu */
        (void) wclear(my_ms->ms_menu);
@@ -236,9 +251,26 @@ Do_menu(m, margc, margv)
        if (!is_topmenu) {
            (void) waddstr(my_ms->ms_menu,
                           " r. (return)       Return to previous menu.");
-           (void) wmove(my_ms->ms_menu, line, 0);
+           (void) wmove(my_ms->ms_menu, line++, 0);
        }
+       (void) waddstr(my_ms->ms_menu,
+                          " t. (toggle)       Toggle logging on and off.");
+       (void) wmove(my_ms->ms_menu, line, 0);
        (void) waddstr(my_ms->ms_menu, " q. (quit)         Quit.");
+    } else {
+       Put_message(m->m_title);
+       for (line = 0; line < m->m_length; line++) {
+           sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
+                   m->m_lines[line].ml_command,
+                   12 - strlen(m->m_lines[line].ml_command), "",
+                   m->m_lines[line].ml_doc);
+           Put_message(buf);
+       }
+       if (!is_topmenu)
+           Put_message(" r. (return)       Return to previous menu.");
+       Put_message(" t. (toggle)       Toggle logging on and off.");
+       Put_message(" q. (quit)         Quit.");
+       Put_message(" ?.                Print this information.");
     }
 
     for (;;) {
@@ -262,10 +294,9 @@ Do_menu(m, margc, margv)
            command = &m->m_lines[line - 1];
        }
        else if ((!is_topmenu &&
-                 (!strcmp(argv[0], "r")
-                  || !strcmp(argv[0], "return")))
-                || !strcmp(argv[0], "q")
-                || !strcmp(argv[0], "quit")) {
+                 (!strcmp(argv[0], "r") || !strcmp(argv[0], "return")))
+                || !strcmp(argv[0], "q") || !strcmp(argv[0], "quit")) {
+                
            /* here if it's either return or quit */
            if (cur_ms != NULLMS) {
                cur_ms = old_cur_ms;
@@ -274,9 +305,27 @@ Do_menu(m, margc, margv)
            if (m->m_exit != NULLFUNC)
                m->m_exit(m);
            return (*argv[0] == 'r' ? DM_NORMAL : DM_QUIT);
-           /* finally, try to find it using Find_command */
        }
-       else if ((command = Find_command(argvals[0])) ==
+       else if (argv[0][0] == '?') {
+           for (line = 0; line < m->m_length; line++) {
+               sprintf(buf, "%2d. (%s)%*s %s.", line + 1,
+                       m->m_lines[line].ml_command,
+                       12 - strlen(m->m_lines[line].ml_command), "",
+                       m->m_lines[line].ml_doc);
+               Put_message(buf);
+           }
+           if (!is_topmenu)
+               Put_message(" r. (return)       Return to previous menu.");
+           Put_message(" t. (toggle)       Toggle logging on and off.");
+           Put_message(" q. (quit)         Quit.");
+           continue;
+       }
+       else if (!strcmp(argv[0], "t") || !strcmp(argv[0], "toggle")) {
+           toggle_logging(argc, argv);
+           continue;
+       } 
+       /* finally, try to find it using Find_command */
+       else if ((command = Find_command(m, argvals[0])) ==
                 (struct menu_line *) 0) {
            Put_message("Command not recognized");
            continue;
@@ -334,7 +383,7 @@ int Prompt_input(prompt, buf, buflen)
 {
     int c;
     char *p;
-    int y, x, oldx;
+    int y, x, oldx, oldy;
 
     if (cur_ms != NULLMS) {
        more_flg = 1;
@@ -347,14 +396,17 @@ int Prompt_input(prompt, buf, buflen)
        getyx(cur_ms->ms_input, y, x);
 
        oldx = x;
+       oldy = y;
        p = buf;
        while(1) {
            (void) wmove(cur_ms->ms_input, y, x);
+               (void) touchwin(cur_ms->ms_screen);
            (void) wclrtoeol(cur_ms->ms_input);
            refresh_ms(cur_ms);
-           c = getchar();
+           c = getchar() & 0x7f;
            switch (c) {
            case CTL('C'):
+               *p = '\0';
                return 0;
            case CTL('Z'):
                (void) kill(getpid(), SIGTSTP);
@@ -365,9 +417,12 @@ int Prompt_input(prompt, buf, buflen)
                (void) waddstr(cur_ms->ms_input, prompt);
                (void) touchwin(cur_ms->ms_screen);
 #ifdef notdef
-               (void) wrefresh(curscr);
+               refresh_ms(cur_ms);
 #endif notdef
                getyx(cur_ms->ms_input, y, x);
+               oldy = y;
+               oldx = x;
+               p = buf;
                break;
 
            case '\n':
@@ -379,19 +434,31 @@ int Prompt_input(prompt, buf, buflen)
                if (p > buf) {
                    p--;
                    x--;
+                   if (x < 0) {
+                       (void) wmove(cur_ms->ms_input, y, 0);
+                       (void) wclrtoeol(cur_ms->ms_input);
+                       y--;
+                       x = cur_ms->ms_input->_maxx-1;
+                   }
                }
                break;
            case CTL('U'):
            case CTL('G'):
            case CTL('['):
                x = oldx;
+               y = oldy;
                p = buf;
                break;
            default:
-               if (isprint(c) && (p - buf < buflen)) {
+               /* (buflen - 1) leaves room for the \0 */
+               if (isprint(c) && (p - buf < buflen - 1)) {
                    (void) waddch(cur_ms->ms_input, c);
                    *p++ = c;
                    x++;
+                   if (x >= cur_ms->ms_input->_maxx) {
+                       x = 0;
+                       y++;
+                   }
                } else
                    putchar(CTL('G'));
                break;
@@ -444,7 +511,7 @@ int Password_input(prompt, buf, buflen)
            (void) wmove(cur_ms->ms_input, y, x);
            (void) wclrtoeol(cur_ms->ms_input);
            refresh_ms(cur_ms);
-           c = getchar();
+           c = getchar() & 0x7f;
            switch (c) {
            case CTL('C'):
                return 0;
@@ -535,6 +602,12 @@ char *msg;
 
     copy = (char *) malloc((u_int)COLS);
     s = line = msg;
+    if (log_file) {    /* if we're doing logging; we assume that the */
+                       /* file has already been opened. */
+       (void) fprintf(log_file, "%s\n", msg);
+       fflush(log_file);
+    } 
+
     while(*s++) {
        if (s - line >= COLS-1) {
            (void) strncpy(copy, line, COLS-1);
@@ -569,7 +642,7 @@ char *msg;
                (void) wprintw(cur_ms->ms_input, "---More---");
                (void) wstandend(cur_ms->ms_input);
                refresh_ms(cur_ms);
-               chr = getchar();/* We do care what it is */
+               chr = getchar() & 0x7f;/* We do care what it is */
                if (chr == 'q' || chr == 'Q') {
                    more_flg = 0;
                    return;
@@ -582,7 +655,7 @@ char *msg;
            }
            else {
                printf("---More (hit return)---");
-               (void) getchar();
+               getchar();
            }
            Start_paging();     /* Reset lines_left */
        }
@@ -657,10 +730,11 @@ find_command_from(c, m, d)
        if (!strcmp(c, m->m_lines[line].ml_command)) {
            return (&m->m_lines[line]);
        }
-       else if (m->m_lines[line].ml_submenu != NULLMENU
-                && (maybe =
-                  find_command_from(c, m->m_lines[line].ml_submenu, d - 1))
-                != (struct menu_line *) 0) {
+    }
+    for (line = 0; line < m->m_length; line++) {
+       if (m->m_lines[line].ml_submenu != NULLMENU  &&
+           (maybe = find_command_from(c, m->m_lines[line].ml_submenu, d - 1))
+               != (struct menu_line *) 0) {
            return (maybe);
        }
     }
@@ -672,24 +746,54 @@ find_command_from(c, m, d)
 /* And returns a pointer to a menu_line with the specified command name */
 /* It returns (struct menu_line *) 0 if none is found */
 struct menu_line *
-Find_command(command)
+Find_command(m, command)
+    Menu *m;
     char *command;
 {
-    if (top_menu == NULLMENU) {
+    if (m == NULLMENU) {
        return ((struct menu_line *) 0);
     }
     else {
-       return (find_command_from(command, top_menu, MAX_MENU_DEPTH));
+       return (find_command_from(command, m, MAX_MENU_DEPTH));
     }
 }
 
-/*
- * Local Variables:
- * mode: c
- * c-indent-level: 4
- * c-continued-statement-offset: 4
- * c-brace-offset: -4
- * c-argdecl-indent: 4
- * c-label-offset: -4
- * End:
- */
+/*ARGSUSED*/
+int
+toggle_logging(argc, argv)
+    int argc;
+    char *argv[];
+{
+    int pid;
+    char buf[BUFSIZ];
+    
+    if (log_file == (FILE *) NULL) {
+       pid = getpid();
+       if (!whoami) {
+           Put_message("I've lost my SENSE of DIRECTION!  I have no IDEA who I AM!");
+           Put_message("My God... You've turned him into a DEMOCRAT!!");
+           Put_message("               -- Doonesbury");
+           Put_message("");
+           Put_message("translation:  your log file can be found in \"/usr/tmp/a.out.pid\".");
+           whoami = "a.out";
+           (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
+       }
+       else
+           (void) sprintf(buf, "/usr/tmp/%s-log.%d", whoami, pid);
+
+       /* open the file */
+       log_file = fopen(buf,"a");
+
+       if (log_file == (FILE *) NULL)
+           Put_message("Open of log file failed.  Logging is not on.");
+       else
+           Put_message("Log file successfully opened.");
+    } 
+    else { /* log_file is a valid pointer; turn off logging. */
+       (void) fflush(log_file);
+       (void) fclose(log_file);
+       log_file = (FILE *) NULL;
+       Put_message("Logging off.");
+    }
+    return(DM_NORMAL);
+} 
This page took 0.045197 seconds and 4 git commands to generate.