add -a option, minor cleanups
[unix-history] / usr / src / usr.bin / telnet / commands.c
index dbb4be7..a46e81e 100644 (file)
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)commands.c 1.9 (Berkeley) %G%";
+static char sccsid[] = "@(#)commands.c 1.15 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
+#if    defined(unix)
+#include <sys/file.h>
+#endif /* defined(unix) */
 #include <sys/socket.h>
 #include <netinet/in.h>
 
 #include <sys/socket.h>
 #include <netinet/in.h>
 
@@ -40,7 +43,7 @@ static char sccsid[] = "@(#)commands.c        1.9 (Berkeley) %G%";
 
 char   *hostname;
 
 
 char   *hostname;
 
-#define Ambiguous(s)   ((char *)s == ambiguous)
+#define Ambiguous(s)   ((char **)s == &ambiguous)
 static char *ambiguous;                /* special return value for command routines */
 
 typedef struct {
 static char *ambiguous;                /* special return value for command routines */
 
 typedef struct {
@@ -59,6 +62,41 @@ static char *margv[20];
  * Various utility routines.
  */
 
  * Various utility routines.
  */
 
+#if    !defined(BSD) || (BSD <= 43)
+
+char   *h_errlist[] = {
+       "Error 0",
+       "Unknown host",                         /* 1 HOST_NOT_FOUND */
+       "Host name lookup failure",             /* 2 TRY_AGAIN */
+       "Unknown server error",                 /* 3 NO_RECOVERY */
+       "No address associated with name",      /* 4 NO_ADDRESS */
+};
+int    h_nerr = { sizeof(h_errlist)/sizeof(h_errlist[0]) };
+
+int h_errno;           /* In some version of SunOS this is necessary */
+
+/*
+ * herror --
+ *     print the error indicated by the h_errno value.
+ */
+herror(s)
+       char *s;
+{
+       if (s && *s) {
+               fprintf(stderr, "%s: ", s);
+       }
+       if ((h_errno < 0) || (h_errno >= h_nerr)) {
+               fprintf(stderr, "Unknown error\n");
+       } else if (h_errno == 0) {
+#if    defined(sun)
+               fprintf(stderr, "Host unknown\n");
+#endif /* defined(sun) */
+       } else {
+               fprintf(stderr, "%s\n", h_errlist[h_errno]);
+       }
+}
+#endif /* !define(BSD) || (BSD <= 43) */
+
 static void
 makeargv()
 {
 static void
 makeargv()
 {
@@ -120,7 +158,7 @@ char        **(*next)();    /* routine to return next entry in table */
                }
        }
        if (nmatches > 1)
                }
        }
        if (nmatches > 1)
-               return (char **)ambiguous;
+               return &ambiguous;
        return (found);
 }
 
        return (found);
 }
 
@@ -561,8 +599,6 @@ char        *argv[];
                    printf("%s %s.\n", *c->variable? "Will" : "Won't",
                                                        c->actionexplanation);
                }
                    printf("%s %s.\n", *c->variable? "Will" : "Won't",
                                                        c->actionexplanation);
                }
-               printf("%s %s.\n", *c->variable? "Will" : "Won't",
-                                                       c->actionexplanation);
            }
            if (c->handler) {
                retval &= (*c->handler)(c);
            }
            if (c->handler) {
                retval &= (*c->handler)(c);
@@ -885,6 +921,97 @@ quit()
        return 1;                       /* just to keep lint happy */
 }
 
        return 1;                       /* just to keep lint happy */
 }
 
+#if    defined(unix)
+/*
+ * Some information about our file descriptors.
+ */
+
+char *
+decodeflags(mask)
+int mask;
+{
+    static char buffer[100];
+#define do(m,s) \
+       if (mask&(m)) { \
+           strcat(buffer, (s)); \
+       }
+
+    buffer[0] = 0;                     /* Terminate it */
+
+#ifdef FREAD
+    do(FREAD, " FREAD");
+#endif
+#ifdef FWRITE
+    do(FWRITE, " FWRITE");
+#endif
+#ifdef F_DUPFP
+    do(F_DUPFD, " F_DUPFD");
+#endif
+#ifdef FNDELAY
+    do(FNDELAY, " FNDELAY");
+#endif
+#ifdef FAPPEND
+    do(FAPPEND, " FAPPEND");
+#endif
+#ifdef FMARK
+    do(FMARK, " FMARK");
+#endif
+#ifdef FDEFER
+    do(FDEFER, " FDEFER");
+#endif
+#ifdef FASYNC
+    do(FASYNC, " FASYNC");
+#endif
+#ifdef FSHLOCK
+    do(FSHLOCK, " FSHLOCK");
+#endif
+#ifdef FEXLOCK
+    do(FEXLOCK, " FEXLOCK");
+#endif
+#ifdef FCREAT
+    do(FCREAT, " FCREAT");
+#endif
+#ifdef FTRUNC
+    do(FTRUNC, " FTRUNC");
+#endif
+#ifdef FEXCL
+    do(FEXCL, " FEXCL");
+#endif
+
+    return buffer;
+}
+#undef do
+
+static void
+filestuff(fd)
+int fd;
+{
+    int res;
+
+    setconnmode();
+    res = fcntl(fd, F_GETOWN, 0);
+    setcommandmode();
+
+    if (res == -1) {
+       perror("fcntl");
+       return;
+    }
+    printf("\tOwner is %d.\n", res);
+
+    setconnmode();
+    res = fcntl(fd, F_GETFL, 0);
+    setcommandmode();
+
+    if (res == -1) {
+       perror("fcntl");
+       return;
+    }
+    printf("\tFlags are 0x%x: %s\n", res, decodeflags(res));
+}
+
+
+#endif /* defined(unix) */
+
 /*
  * Print status about the connection.
  */
 /*
  * Print status about the connection.
  */
@@ -896,7 +1023,7 @@ char       *argv[];
 {
     if (connected) {
        printf("Connected to %s.\n", hostname);
 {
     if (connected) {
        printf("Connected to %s.\n", hostname);
-       if (argc < 2) {
+       if ((argc < 2) || strcmp(argv[1], "notmuch")) {
            printf("Operating in %s.\n",
                                modelist[getconnmode()].modedescriptions);
            if (localchars) {
            printf("Operating in %s.\n",
                                modelist[getconnmode()].modedescriptions);
            if (localchars) {
@@ -914,6 +1041,20 @@ char      *argv[];
        printf("Escape character is '%s'.\n", control(escape));
     }
 #   if defined(unix)
        printf("Escape character is '%s'.\n", control(escape));
     }
 #   if defined(unix)
+    if ((argc >= 2) && !strcmp(argv[1], "everything")) {
+       printf("SIGIO received %d time%s.\n",
+                               sigiocount, (sigiocount == 1)? "":"s");
+       if (In3270) {
+           printf("Process ID %d, process group %d.\n",
+                                           getpid(), getpgrp(getpid()));
+           printf("Terminal input:\n");
+           filestuff(tin);
+           printf("Terminal output:\n");
+           filestuff(tout);
+           printf("Network socket:\n");
+           filestuff(net);
+       }
+    }
     if (In3270 && transcom) {
        printf("Transparent mode command is '%s'.\n", transcom);
     }
     if (In3270 && transcom) {
        printf("Transparent mode command is '%s'.\n", transcom);
     }
@@ -968,7 +1109,7 @@ tn(argc, argv)
     }
 #endif /* defined(MSDOS) */
     sin.sin_addr.s_addr = inet_addr(argv[1]);
     }
 #endif /* defined(MSDOS) */
     sin.sin_addr.s_addr = inet_addr(argv[1]);
-    if (sin.sin_addr.s_addr != -1) {
+    if (sin.sin_addr.s_addr != (unsigned long) -1) {
        sin.sin_family = AF_INET;
        (void) strcpy(hnamebuf, argv[1]);
        hostname = hnamebuf;
        sin.sin_family = AF_INET;
        (void) strcpy(hnamebuf, argv[1]);
        hostname = hnamebuf;
@@ -984,7 +1125,7 @@ tn(argc, argv)
 #endif /* defined(h_addr) */
            hostname = host->h_name;
        } else {
 #endif /* defined(h_addr) */
            hostname = host->h_name;
        } else {
-           printf("%s: unknown host\n", argv[1]);
+           herror(argv[1]);
            return 0;
        }
     }
            return 0;
        }
     }
@@ -1134,15 +1275,15 @@ va_dcl
     typedef int (*intrtn_t)();
     intrtn_t routine;
     char *args[100];
     typedef int (*intrtn_t)();
     intrtn_t routine;
     char *args[100];
-
     int argno = 0;
 
     va_start(ap);
     routine = (va_arg(ap, intrtn_t));
     int argno = 0;
 
     va_start(ap);
     routine = (va_arg(ap, intrtn_t));
-    while (args[argno++] = va_arg(ap, char *))
+    while ((args[argno++] = va_arg(ap, char *)) != 0) {
        ;
        ;
+    }
     va_end(ap);
     va_end(ap);
-    return (*routine)(argno, args);
+    return (*routine)(argno-1, args);
 }
 
 
 }