2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)cmds.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/usr.bin/tip/tip/cmds.c,v 1.11.2.2 2000/07/01 12:24:23 ps Exp $";
44 #include "pathnames.h"
46 #include <sys/types.h>
56 * miscellaneous commands
59 int quant[] = { 60, 60, 24 };
62 char *sep[] = { "second", "minute", "hour" };
63 static char *argv[10]; /* argument vector for take and put */
65 void timeout(); /* timeout function called on alarm */
66 static void stopsnd(); /* SIGINT handler during file transfers */
67 static void intcopy(); /* interrupt routine for file transfers */
69 void suspend __P((char));
70 void genbrk __P((void));
71 void variable __P((void));
72 void finish __P((void));
73 void tipabort __P((char *));
74 void chdirectory __P((void));
75 void shell __P((void));
76 void cu_put __P((char));
77 void sendfile __P((char));
78 void pipefile __P((void));
79 void cu_take __P((char));
80 void getfl __P((char));
82 static int anyof __P((char *, char *));
83 static void tandem __P((char *));
84 static void prtime __P((char *, time_t));
85 static int args __P((char *, char **, int));
86 static void execute __P((char *));
87 static void send __P((char));
88 static void transmit __P((FILE *, char *, char *));
89 static void transfer __P((char *, int, char *));
90 static void xfer __P((char *, int, char *));
97 struct termios ttermios;
99 for (cnt = 0; cnt < NCCS; cnt++)
100 ttermios.c_cc [cnt] = otermios.c_cc [cnt];
101 tcsetattr (0, TCSANOW, &ttermios);
103 ioctl(0, TIOCSETC, &defchars);
111 tcsetattr (0, TCSANOW, &ctermios);
113 ioctl(0, TIOCSETC, &tchars);
122 ioctl (FD, TIOCFLUSH, &cmd);
125 ioctl (FD, TIOCGETP, &buf); /* this does a */
126 ioctl (FD, TIOCSETP, &buf); /* wflushtty */
131 * FTP - remote ==> local
132 * get a file from the remote host
138 char buf[256], *cp, *expand();
142 * get the UNIX receiving file's name
144 if (prompt("Local file name? ", copyname, sizeof(copyname)))
146 cp = expand(copyname);
147 if ((sfd = creat(cp, 0666)) < 0) {
148 printf("\r\n%s: cannot creat\r\n", copyname);
155 if (prompt("List command for remote system? ", buf, sizeof(buf))) {
159 transfer(buf, sfd, value(EOFREAD));
163 * Cu-like take command
170 char line[BUFSIZ], *expand(), *cp;
172 if (prompt("[take] ", copyname, sizeof(copyname)))
174 if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 || argc > 2) {
175 printf("usage: <take> from [to]\r\n");
180 cp = expand(argv[1]);
181 if ((fd = creat(cp, 0666)) < 0) {
182 printf("\r\n%s: cannot create\r\n", argv[1]);
185 (void)snprintf(line, sizeof(line), "cat %s ; echo \"\" ; echo ___tip_end_of_file_marker___", argv[0]);
186 xfer(line, fd, "\n___tip_end_of_file_marker___\n");
189 static jmp_buf intbuf;
192 xfer(buf, fd, eofchars)
193 char *buf, *eofchars;
203 v = boolean(value(VERBOSE));
205 if ((ff = fdopen (fd, "w")) == NULL) {
209 if ((cnt = number(value(FRAMESIZE))) != BUFSIZ)
210 if (setvbuf(ff, NULL, _IOFBF, cnt) != 0) {
211 warn("file allocation");
216 xpwrite(FD, buf, size(buf));
219 read(repdes[0], (char *)&ccc, 1); /* Wait until read process stops */
228 while ((c&0177) != '\n');
232 (void) setjmp(intbuf);
233 f = signal(SIGINT, intcopy);
236 for (ct = 0; !quit;) {
237 eof = read(FD, &c, 1) <= 0;
244 continue; /* ignore nulls */
247 if (c != *match && match > eofchars) {
248 register char *p = eofchars;
251 (void)printf("\r%d", ++ct);
257 if (*++match == '\0')
261 (void)printf("\r%d", ++ct);
266 prtime(" lines transferred in ", time(0)-start);
268 write(fildes[1], (char *)&ccc, 1);
274 * Bulk transfer routine --
275 * used by getfl(), cu_take(), and pipefile()
278 transfer(buf, fd, eofchars)
279 char *buf, *eofchars;
283 register int cnt, eof, v;
289 v = boolean(value(VERBOSE));
291 if ((ff = fdopen (fd, "w")) == NULL) {
295 if ((cnt = number(value(FRAMESIZE))) != BUFSIZ)
296 if (setvbuf(ff, NULL, _IOFBF, cnt) != 0) {
297 warn("file allocation");
302 xpwrite(FD, buf, size(buf));
305 read(repdes[0], (char *)&ccc, 1); /* Wait until read process stops */
314 while ((c&0177) != '\n');
316 (void) setjmp(intbuf);
317 f = signal(SIGINT, intcopy);
319 for (ct = 0; !quit;) {
320 eof = read(FD, &c, 1) <= 0;
324 if (eof || any(c, eofchars))
327 continue; /* ignore nulls */
331 printf("\r%d", ++ct);
335 prtime(" lines transferred in ", time(0)-start);
337 write(fildes[1], (char *)&ccc, 1);
343 * FTP - remote ==> local process
344 * send remote input to local process via pipe
353 if (prompt("Local command? ", buf, sizeof(buf)))
357 printf("can't establish pipe\r\n");
361 if ((cpid = fork()) < 0) {
362 printf("can't fork!\r\n");
365 if (prompt("List command for remote system? ", buf, sizeof(buf))) {
366 close(pdes[0]), close(pdes[1]);
367 kill (cpid, SIGKILL);
370 signal(SIGPIPE, intcopy);
371 transfer(buf, pdes[1], value(EOFREAD));
372 signal(SIGPIPE, SIG_DFL);
373 while ((p = wait(&status)) > 0 && p != cpid)
381 for (f = 3; f < 20; f++)
384 printf("can't execl!\r\n");
390 * Interrupt service routine for FTP
397 signal(SIGINT, SIG_IGN);
401 * FTP - local ==> remote
402 * send local file to remote host
403 * terminate transmission with pseudo EOF sequence
417 if (prompt("Local file name? ", fname, sizeof(fname)))
423 fnamex = expand(fname);
424 if ((fd = fopen(fnamex, "r")) == NULL) {
425 printf("%s: cannot open\r\n", fname);
428 transmit(fd, value(EOFWRITE), NULL);
429 if (!boolean(value(ECHOCHECK))) {
435 * Bulk transfer routine to remote host --
436 * used by sendfile() and cu_put()
439 transmit(fd, eofchars, command)
441 char *eofchars, *command;
444 int c, ccount, lcount;
445 time_t start_t, stop_t;
448 kill(pid, SIGIOT); /* put TIPOUT into a wait state */
450 f = signal(SIGINT, stopsnd);
452 read(repdes[0], (char *)&ccc, 1);
453 if (command != NULL) {
454 for (pc = command; *pc; pc++)
456 if (boolean(value(ECHOCHECK)))
457 read(FD, (char *)&c, 1); /* trailing \n */
460 sleep(5); /* wait for remote stty to take effect */
474 if (c == 0177 && !boolean(value(RAWFTP)))
479 if (!boolean(value(RAWFTP)))
482 else if (c == '\t') {
483 if (!boolean(value(RAWFTP))) {
484 if (boolean(value(TABEXPAND))) {
486 while ((++ccount % 8) != 0)
492 if (!boolean(value(RAWFTP)))
496 } while (c != '\r' && !boolean(value(RAWFTP)));
497 if (boolean(value(VERBOSE)))
498 printf("\r%d", ++lcount);
499 if (boolean(value(ECHOCHECK))) {
501 alarm(number(value(ETIMEOUT)));
502 do { /* wait for prompt */
503 read(FD, (char *)&c, 1);
504 if (timedout || stop) {
506 printf("\r\ntimed out at eol\r\n");
510 } while ((c&0177) != character(value(PROMPT)));
515 if (lastc != '\n' && !boolean(value(RAWFTP)))
517 for (pc = eofchars; pc && *pc; pc++)
522 if (boolean(value(VERBOSE)))
523 if (boolean(value(RAWFTP)))
524 prtime(" chars transferred in ", stop_t-start_t);
526 prtime(" lines transferred in ", stop_t-start_t);
527 write(fildes[1], (char *)&ccc, 1);
532 * Cu-like put command
544 if (prompt("[put] ", copyname, sizeof(copyname)))
546 if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 || argc > 2) {
547 printf("usage: <put> from [to]\r\n");
552 copynamex = expand(argv[0]);
553 if ((fd = fopen(copynamex, "r")) == NULL) {
554 printf("%s: cannot open\r\n", copynamex);
557 if (boolean(value(ECHOCHECK)))
558 snprintf(line, sizeof(line), "cat>%s\r", argv[1]);
560 snprintf(line, sizeof(line), "stty -echo;cat>%s;stty echo\r", argv[1]);
561 transmit(fd, "\04", line);
567 int msec; /* milliseconds */
569 if (usleep(msec*1000) != 0) {
570 fprintf ( stderr, "warning: ldelay or cdelay interrupted, "
571 "delay time cut short: %s\n",
580 * FTP - send single character
581 * wait for echo & handle timeout
592 if (number(value(CDELAY)) > 0 && c != '\r')
593 nap(number(value(CDELAY)));
594 if (!boolean(value(ECHOCHECK))) {
595 if (number(value(LDELAY)) > 0 && c == '\r')
596 nap(number(value(LDELAY)));
601 alarm(number(value(ETIMEOUT)));
605 printf("\r\ntimeout error (%s)\r\n", ctrl(c));
608 xpwrite(FD, &null, 1); /* poke it */
616 signal(SIGALRM, timeout);
621 * Stolen from consh() -- puts a remote file on the output of a local command.
622 * Identical to consh() except for where stdout goes.
632 if (prompt("Local command? ", buf, sizeof(buf)))
634 kill(pid, SIGIOT); /* put TIPOUT into a wait state */
635 signal(SIGINT, SIG_IGN);
636 signal(SIGQUIT, SIG_IGN);
638 read(repdes[0], (char *)&ccc, 1);
640 * Set up file descriptors in the child and
643 if ((cpid = fork()) < 0)
644 printf("can't fork!\r\n");
647 while ((p = wait(&status)) > 0 && p != cpid)
653 for (i = 3; i < 20; i++)
655 signal(SIGINT, SIG_DFL);
656 signal(SIGQUIT, SIG_DFL);
658 printf("can't find `%s'\r\n", buf);
661 if (boolean(value(VERBOSE)))
662 prtime("away for ", time(0)-start);
663 write(fildes[1], (char *)&ccc, 1);
665 signal(SIGINT, SIG_DFL);
666 signal(SIGQUIT, SIG_DFL);
672 tiplink (char *cmd, unsigned int flags)
677 if (flags & TL_SIGNAL_TIPOUT) {
678 kill(pid, SIGIOT); /* put TIPOUT into a wait state */
679 signal(SIGINT, SIG_IGN);
680 signal(SIGQUIT, SIG_IGN);
682 read(repdes[0], (char *)&ccc, 1);
686 * Set up file descriptors in the child and
689 if ((cpid = fork()) < 0)
690 printf("can't fork!\r\n");
693 while ((p = wait(&status)) > 0 && p != cpid)
700 for (fd = 3; fd < 20; fd++)
702 signal(SIGINT, SIG_DFL);
703 signal(SIGQUIT, SIG_DFL);
705 printf("can't find `%s'\r\n", cmd);
709 if (flags & TL_VERBOSE && boolean(value(VERBOSE)))
710 prtime("away for ", time(0)-start);
712 if (flags & TL_SIGNAL_TIPOUT) {
713 write(fildes[1], (char *)&ccc, 1);
715 signal(SIGINT, SIG_DFL);
716 signal(SIGQUIT, SIG_DFL);
723 * Fork a program with:
724 * 0 <-> remote tty in
725 * 1 <-> remote tty out
726 * 2 <-> local tty out
733 if (prompt("Local command? ", buf, sizeof(buf)))
735 tiplink (buf, TL_SIGNAL_TIPOUT | TL_VERBOSE);
741 * Escape to local shell
750 signal(SIGINT, SIG_IGN);
751 signal(SIGQUIT, SIG_IGN);
753 if ((shpid = fork())) {
754 while (shpid != wait(&status));
757 signal(SIGINT, SIG_DFL);
758 signal(SIGQUIT, SIG_DFL);
761 signal(SIGQUIT, SIG_DFL);
762 signal(SIGINT, SIG_DFL);
763 if ((cp = rindex(value(SHELL), '/')) == NULL)
768 execl(value(SHELL), cp, 0);
769 printf("\r\ncan't execl!\r\n");
775 * TIPIN portion of scripting
776 * initiate the conversation with TIPOUT
783 * enable TIPOUT side for dialogue
786 if (boolean(value(SCRIPT)))
787 write(fildes[1], value(RECORD), size(value(RECORD)));
788 write(fildes[1], "\n", 1);
790 * wait for TIPOUT to finish
792 read(repdes[0], &c, 1);
794 printf("can't create %s\r\n", value(RECORD));
798 * Change current working directory of
799 * local portion of tip
804 char dirname[PATH_MAX];
805 register char *cp = dirname;
807 if (prompt("[cd] ", dirname, sizeof(dirname))) {
813 printf("%s: bad directory\r\n", cp);
825 printf("\r\n%s", msg);
826 printf("\r\n[EOT]\r\n");
828 (void)uu_unlock(uucplock);
836 char *abortmsg = NOSTR, *dismsg;
838 if (LO != NOSTR && tiplink (LO, TL_SIGNAL_TIPOUT) != 0) {
839 abortmsg = "logout failed";
842 if ((dismsg = value(DISCONNECT)) != NOSTR) {
843 write(FD, dismsg, strlen(dismsg));
863 if ((cp = rindex(value(SHELL), '/')) == NULL)
868 execl(value(SHELL), cp, "-c", s, 0);
876 register char *p = buf, *start;
877 register char **parg = a;
880 while (*p && n < num) {
881 while (*p && (*p == ' ' || *p == '\t'))
886 while (*p && (*p != ' ' && *p != '\t'))
904 for (i = 0; i < 3; i++) {
905 nums[i] = (int)(a % quant[i]);
910 if (nums[i] || (i == 0 && nums[1] == 0 && nums[2] == 0))
911 printf("%d %s%c ", nums[i], sep[i],
912 nums[i] == 1 ? '\0' : 's');
921 if (prompt("[set] ", buf, sizeof(buf)))
924 if (vtable[BEAUTIFY].v_access&CHANGED) {
925 vtable[BEAUTIFY].v_access &= ~CHANGED;
928 if (vtable[SCRIPT].v_access&CHANGED) {
929 vtable[SCRIPT].v_access &= ~CHANGED;
932 * So that "set record=blah script" doesn't
933 * cause two transactions to occur.
935 if (vtable[RECORD].v_access&CHANGED)
936 vtable[RECORD].v_access &= ~CHANGED;
938 if (vtable[RECORD].v_access&CHANGED) {
939 vtable[RECORD].v_access &= ~CHANGED;
940 if (boolean(value(SCRIPT)))
943 if (vtable[TAND].v_access&CHANGED) {
944 vtable[TAND].v_access &= ~CHANGED;
945 if (boolean(value(TAND)))
950 if (vtable[LECHO].v_access&CHANGED) {
951 vtable[LECHO].v_access &= ~CHANGED;
952 HD = boolean(value(LECHO));
954 if (vtable[PARITY].v_access&CHANGED) {
955 vtable[PARITY].v_access &= ~CHANGED;
956 setparity(value(PARITY));
961 * Turn tandem mode on or off for remote tty.
968 struct termios ttermios;
969 tcgetattr (FD, &ttermios);
970 if (strcmp(option,"on") == 0) {
971 ttermios.c_iflag |= IXOFF;
972 ctermios.c_iflag |= IXOFF;
975 ttermios.c_iflag &= ~IXOFF;
976 ctermios.c_iflag &= ~IXOFF;
978 tcsetattr (FD, TCSANOW, &ttermios);
979 tcsetattr (0, TCSANOW, &ctermios);
980 #else /* HAVE_TERMIOS */
983 ioctl(FD, TIOCGETP, &rmtty);
984 if (strcmp(option,"on") == 0) {
985 rmtty.sg_flags |= TANDEM;
986 arg.sg_flags |= TANDEM;
988 rmtty.sg_flags &= ~TANDEM;
989 arg.sg_flags &= ~TANDEM;
991 ioctl(FD, TIOCSETP, &rmtty);
992 ioctl(0, TIOCSETP, &arg);
993 #endif /* HAVE_TERMIOS */
1003 ioctl(FD, TIOCSBRK, NULL);
1005 ioctl(FD, TIOCCBRK, NULL);
1017 kill(c == CTRL('y') ? getpid() : 0, SIGTSTP);
1022 * expand a file name if it includes shell meta characters
1029 static char xname[BUFSIZ];
1030 char cmdbuf[BUFSIZ];
1031 register int pid, l;
1032 register char *cp, *Shell;
1033 int s, pivec[2] /*, (*sigint)()*/;
1035 if (!anyof(name, "~{[*?$`'\"\\"))
1037 /* sigint = signal(SIGINT, SIG_IGN); */
1038 if (pipe(pivec) < 0) {
1040 /* signal(SIGINT, sigint) */
1043 snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
1044 if ((pid = vfork()) == 0) {
1045 Shell = value(SHELL);
1047 Shell = _PATH_BSHELL;
1054 execl(Shell, Shell, "-c", cmdbuf, 0);
1064 l = read(pivec[0], xname, BUFSIZ);
1066 while (wait(&s) != pid);
1069 if (s != 0 && s != SIGPIPE) {
1070 fprintf(stderr, "\"Echo\" failed\n");
1078 fprintf(stderr, "\"%s\": No match\n", name);
1082 fprintf(stderr, "Buffer overflow expanding \"%s\"\n", name);
1086 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
1093 * Are any of the characters in the two strings the same?
1098 register char *s1, *s2;