BSD 4_3 release
[unix-history] / usr / src / usr.bin / tip / cmds.c
index bb509b1..84c72e0 100644 (file)
@@ -1,6 +1,12 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)cmds.c     4.12 (Berkeley) 6/25/83";
-#endif
+static char sccsid[] = "@(#)cmds.c     5.4 (Berkeley) 5/5/86";
+#endif not lint
 
 #include "tip.h"
 /*
 
 #include "tip.h"
 /*
@@ -106,9 +112,9 @@ transfer(buf, fd, eofchars)
        while ((c&0177) != '\n');
        ioctl(0, TIOCSETC, &defchars);
        
        while ((c&0177) != '\n');
        ioctl(0, TIOCSETC, &defchars);
        
+       (void) setjmp(intbuf);
        f = signal(SIGINT, intcopy);
        start = time(0);
        f = signal(SIGINT, intcopy);
        start = time(0);
-       (void) setjmp(intbuf);
        for (ct = 0; !quit;) {
                eof = read(FD, &c, 1) <= 0;
                c &= 0177;
        for (ct = 0; !quit;) {
                eof = read(FD, &c, 1) <= 0;
                c &= 0177;
@@ -140,7 +146,7 @@ transfer(buf, fd, eofchars)
                prtime(" lines transferred in ", time(0)-start);
        ioctl(0, TIOCSETC, &tchars);
        write(fildes[1], (char *)&ccc, 1);
                prtime(" lines transferred in ", time(0)-start);
        ioctl(0, TIOCSETC, &tchars);
        write(fildes[1], (char *)&ccc, 1);
-       signal(SIGINT, SIG_DFL);
+       signal(SIGINT, f);
        close(fd);
 }
 
        close(fd);
 }
 
@@ -248,10 +254,11 @@ transmit(fd, eofchars, command)
        char *pc, lastc;
        int c, ccount, lcount;
        time_t start_t, stop_t;
        char *pc, lastc;
        int c, ccount, lcount;
        time_t start_t, stop_t;
+       int (*f)();
 
        kill(pid, SIGIOT);      /* put TIPOUT into a wait state */
 
        kill(pid, SIGIOT);      /* put TIPOUT into a wait state */
-       signal(SIGINT, stopsnd);
        stop = 0;
        stop = 0;
+       f = signal(SIGINT, stopsnd);
        ioctl(0, TIOCSETC, &defchars);
        read(repdes[0], (char *)&ccc, 1);
        if (command != NULL) {
        ioctl(0, TIOCSETC, &defchars);
        read(repdes[0], (char *)&ccc, 1);
        if (command != NULL) {
@@ -304,8 +311,8 @@ transmit(fd, eofchars, command)
                if (boolean(value(VERBOSE)))
                        printf("\r%d", ++lcount);
                if (boolean(value(ECHOCHECK))) {
                if (boolean(value(VERBOSE)))
                        printf("\r%d", ++lcount);
                if (boolean(value(ECHOCHECK))) {
-                       alarm(value(ETIMEOUT));
                        timedout = 0;
                        timedout = 0;
+                       alarm(value(ETIMEOUT));
                        do {    /* wait for prompt */
                                read(FD, (char *)&c, 1);
                                if (timedout || stop) {
                        do {    /* wait for prompt */
                                read(FD, (char *)&c, 1);
                                if (timedout || stop) {
@@ -325,7 +332,7 @@ out:
                send(*pc);
        stop_t = time(0);
        fclose(fd);
                send(*pc);
        stop_t = time(0);
        fclose(fd);
-       signal(SIGINT, SIG_DFL);
+       signal(SIGINT, f);
        if (boolean(value(VERBOSE)))
                if (boolean(value(RAWFTP)))
                        prtime(" chars transferred in ", stop_t-start_t);
        if (boolean(value(VERBOSE)))
                if (boolean(value(RAWFTP)))
                        prtime(" chars transferred in ", stop_t-start_t);
@@ -410,6 +417,54 @@ timeout()
        timedout = 1;
 }
 
        timedout = 1;
 }
 
+/*
+ * Stolen from consh() -- puts a remote file on the output of a local command.
+ *     Identical to consh() except for where stdout goes.
+ */
+pipeout(c)
+{
+       char buf[256];
+       int cpid, status, p;
+       time_t start;
+
+       putchar(c);
+       if (prompt("Local command? ", buf))
+               return;
+       kill(pid, SIGIOT);      /* put TIPOUT into a wait state */
+       signal(SIGINT, SIG_IGN);
+       signal(SIGQUIT, SIG_IGN);
+       ioctl(0, TIOCSETC, &defchars);
+       read(repdes[0], (char *)&ccc, 1);
+       /*
+        * Set up file descriptors in the child and
+        *  let it go...
+        */
+       if ((cpid = fork()) < 0)
+               printf("can't fork!\r\n");
+       else if (cpid) {
+               start = time(0);
+               while ((p = wait(&status)) > 0 && p != cpid)
+                       ;
+       } else {
+               register int i;
+
+               dup2(FD, 1);
+               for (i = 3; i < 20; i++)
+                       close(i);
+               signal(SIGINT, SIG_DFL);
+               signal(SIGQUIT, SIG_DFL);
+               execute(buf);
+               printf("can't find `%s'\r\n", buf);
+               exit(0);
+       }
+       if (boolean(value(VERBOSE)))
+               prtime("away for ", time(0)-start);
+       write(fildes[1], (char *)&ccc, 1);
+       ioctl(0, TIOCSETC, &tchars);
+       signal(SIGINT, SIG_DFL);
+       signal(SIGQUIT, SIG_DFL);
+}
+
 #ifdef CONNECT
 /*
  * Fork a program with:
 #ifdef CONNECT
 /*
  * Fork a program with:
@@ -539,22 +594,33 @@ chdirectory()
        printf("!\r\n");
 }
 
        printf("!\r\n");
 }
 
-finish()
+abort(msg)
+       char *msg;
 {
 {
-       char *dismsg;
 
 
-       if ((dismsg = value(DISCONNECT)) != NOSTR) {
-               write(FD,dismsg,strlen(dismsg));
-               sleep(5);
-       }
        kill(pid, SIGTERM);
        kill(pid, SIGTERM);
-       disconnect();
+       setreuid(euid, euid);
+       setregid(egid, egid);
+       disconnect(msg);
+       if (msg != NOSTR)
+               printf("\r\n%s", msg);
        printf("\r\n[EOT]\r\n");
        delock(uucplock);
        unraw();
        exit(0);
 }
 
        printf("\r\n[EOT]\r\n");
        delock(uucplock);
        unraw();
        exit(0);
 }
 
+finish()
+{
+       char *dismsg;
+
+       if ((dismsg = value(DISCONNECT)) != NOSTR) {
+               write(FD, dismsg, strlen(dismsg));
+               sleep(5);
+       }
+       abort(NOSTR);
+}
+
 intcopy()
 {
 
 intcopy()
 {
 
@@ -612,7 +678,7 @@ prtime(s, a)
        }
        printf("%s", s);
        while (--i >= 0)
        }
        printf("%s", s);
        while (--i >= 0)
-               if (nums[i])
+               if (nums[i] || i == 0 && nums[1] == 0 && nums[2] == 0)
                        printf("%d %s%c ", nums[i], sep[i],
                                nums[i] == 1 ? '\0' : 's');
        printf("\r\n!\r\n");
                        printf("%d %s%c ", nums[i], sep[i],
                                nums[i] == 1 ? '\0' : 's');
        printf("\r\n!\r\n");
@@ -695,11 +761,12 @@ genbrk()
 /*
  * Suspend tip
  */
 /*
  * Suspend tip
  */
-suspend()
+suspend(c)
+       char c;
 {
 
        unraw();
 {
 
        unraw();
-       kill(0, SIGTSTP);
+       kill(c == CTRL(y) ? getpid() : 0, SIGTSTP);
        raw();
 }
 
        raw();
 }