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
33 * @(#)cmds.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.bin/tip/tip/cmds.c,v 1.11.2.2 2000/07/01 12:24:23 ps Exp $
35 * $DragonFly: src/usr.bin/tip/tip/cmds.c,v 1.5 2005/04/19 05:32:02 cpressey Exp $
40 #include "pathnames.h"
42 #include <sys/types.h>
52 * miscellaneous commands
55 int quant[] = { 60, 60, 24 };
58 char *sep[] = { "second", "minute", "hour" };
59 static char *argv[10]; /* argument vector for take and put */
61 void timeout(int); /* timeout function called on alarm */
62 static void stopsnd(int); /* SIGINT handler during file transfers */
63 static void intcopy(); /* interrupt routine for file transfers */
69 void tipabort(char *);
70 void chdirectory(void);
78 static int anyof(char *, char *);
79 static void tandem(char *);
80 static void prtime(char *, time_t);
81 static int args(char *, char **, int);
82 static void execute(char *);
83 static void send(char);
84 static void transmit(FILE *, char *, char *);
85 static void transfer(char *, int, char *);
86 static void xfer(char *, int, char *);
93 struct termios ttermios;
95 for (cnt = 0; cnt < NCCS; cnt++)
96 ttermios.c_cc [cnt] = otermios.c_cc [cnt];
97 tcsetattr (0, TCSANOW, &ttermios);
99 ioctl(0, TIOCSETC, &defchars);
107 tcsetattr (0, TCSANOW, &ctermios);
109 ioctl(0, TIOCSETC, &tchars);
118 ioctl (FD, TIOCFLUSH, &cmd);
121 ioctl (FD, TIOCGETP, &buf); /* this does a */
122 ioctl (FD, TIOCSETP, &buf); /* wflushtty */
127 * FTP - remote ==> local
128 * get a file from the remote host
133 char buf[256], *cp, *expand();
137 * get the UNIX receiving file's name
139 if (prompt("Local file name? ", copyname, sizeof(copyname)))
141 cp = expand(copyname);
142 if ((sfd = creat(cp, 0666)) < 0) {
143 printf("\r\n%s: cannot creat\r\n", copyname);
150 if (prompt("List command for remote system? ", buf, sizeof(buf))) {
154 transfer(buf, sfd, value(EOFREAD));
158 * Cu-like take command
164 char line[BUFSIZ], *expand(), *cp;
166 if (prompt("[take] ", copyname, sizeof(copyname)))
168 if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 || argc > 2) {
169 printf("usage: <take> from [to]\r\n");
174 cp = expand(argv[1]);
175 if ((fd = creat(cp, 0666)) < 0) {
176 printf("\r\n%s: cannot create\r\n", argv[1]);
179 (void)snprintf(line, sizeof(line), "cat %s ; echo \"\" ; echo ___tip_end_of_file_marker___", argv[0]);
180 xfer(line, fd, "\n___tip_end_of_file_marker___\n");
183 static jmp_buf intbuf;
186 xfer(char *buf, int fd, char *eofchars)
196 v = boolean(value(VERBOSE));
198 if ((ff = fdopen (fd, "w")) == NULL) {
202 if ((cnt = number(value(FRAMESIZE))) != BUFSIZ)
203 if (setvbuf(ff, NULL, _IOFBF, cnt) != 0) {
204 warn("file allocation");
209 xpwrite(FD, buf, size(buf));
212 read(repdes[0], (char *)&ccc, 1); /* Wait until read process stops */
221 while ((c&0177) != '\n');
225 (void) setjmp(intbuf);
226 f = signal(SIGINT, intcopy);
229 for (ct = 0; !quit;) {
230 eof = read(FD, &c, 1) <= 0;
237 continue; /* ignore nulls */
240 if (c != *match && match > eofchars) {
241 register char *p = eofchars;
244 (void)printf("\r%d", ++ct);
250 if (*++match == '\0')
254 (void)printf("\r%d", ++ct);
259 prtime(" lines transferred in ", time(0)-start);
261 write(fildes[1], (char *)&ccc, 1);
267 * Bulk transfer routine --
268 * used by getfl(), cu_take(), and pipefile()
271 transfer(char *buf, int fd, char *eofchars)
275 register int cnt, eof, v;
281 v = boolean(value(VERBOSE));
283 if ((ff = fdopen (fd, "w")) == NULL) {
287 if ((cnt = number(value(FRAMESIZE))) != BUFSIZ)
288 if (setvbuf(ff, NULL, _IOFBF, cnt) != 0) {
289 warn("file allocation");
294 xpwrite(FD, buf, size(buf));
297 read(repdes[0], (char *)&ccc, 1); /* Wait until read process stops */
306 while ((c&0177) != '\n');
308 (void) setjmp(intbuf);
309 f = signal(SIGINT, intcopy);
311 for (ct = 0; !quit;) {
312 eof = read(FD, &c, 1) <= 0;
316 if (eof || any(c, eofchars))
319 continue; /* ignore nulls */
323 printf("\r%d", ++ct);
327 prtime(" lines transferred in ", time(0)-start);
329 write(fildes[1], (char *)&ccc, 1);
335 * FTP - remote ==> local process
336 * send remote input to local process via pipe
345 if (prompt("Local command? ", buf, sizeof(buf)))
349 printf("can't establish pipe\r\n");
353 if ((cpid = fork()) < 0) {
354 printf("can't fork!\r\n");
357 if (prompt("List command for remote system? ", buf, sizeof(buf))) {
358 close(pdes[0]), close(pdes[1]);
359 kill (cpid, SIGKILL);
362 signal(SIGPIPE, intcopy);
363 transfer(buf, pdes[1], value(EOFREAD));
364 signal(SIGPIPE, SIG_DFL);
365 while ((p = wait(&status)) > 0 && p != cpid)
373 for (f = 3; f < 20; f++)
376 printf("can't execl!\r\n");
382 * Interrupt service routine for FTP
389 signal(SIGINT, SIG_IGN);
393 * FTP - local ==> remote
394 * send local file to remote host
395 * terminate transmission with pseudo EOF sequence
408 if (prompt("Local file name? ", fname, sizeof(fname)))
414 fnamex = expand(fname);
415 if ((fd = fopen(fnamex, "r")) == NULL) {
416 printf("%s: cannot open\r\n", fname);
419 transmit(fd, value(EOFWRITE), NULL);
420 if (!boolean(value(ECHOCHECK))) {
426 * Bulk transfer routine to remote host --
427 * used by sendfile() and cu_put()
430 transmit(FILE *fd, char *eofchars, char *command)
433 int c, ccount, lcount;
434 time_t start_t, stop_t;
437 kill(pid, SIGIOT); /* put TIPOUT into a wait state */
439 f = signal(SIGINT, stopsnd);
441 read(repdes[0], (char *)&ccc, 1);
442 if (command != NULL) {
443 for (pc = command; *pc; pc++)
445 if (boolean(value(ECHOCHECK)))
446 read(FD, (char *)&c, 1); /* trailing \n */
449 sleep(5); /* wait for remote stty to take effect */
463 if (c == 0177 && !boolean(value(RAWFTP)))
468 if (!boolean(value(RAWFTP)))
471 else if (c == '\t') {
472 if (!boolean(value(RAWFTP))) {
473 if (boolean(value(TABEXPAND))) {
475 while ((++ccount % 8) != 0)
481 if (!boolean(value(RAWFTP)))
485 } while (c != '\r' && !boolean(value(RAWFTP)));
486 if (boolean(value(VERBOSE)))
487 printf("\r%d", ++lcount);
488 if (boolean(value(ECHOCHECK))) {
490 alarm(number(value(ETIMEOUT)));
491 do { /* wait for prompt */
492 read(FD, (char *)&c, 1);
493 if (timedout || stop) {
495 printf("\r\ntimed out at eol\r\n");
499 } while ((c&0177) != character(value(PROMPT)));
504 if (lastc != '\n' && !boolean(value(RAWFTP)))
506 for (pc = eofchars; pc && *pc; pc++)
511 if (boolean(value(VERBOSE)))
512 if (boolean(value(RAWFTP)))
513 prtime(" chars transferred in ", stop_t-start_t);
515 prtime(" lines transferred in ", stop_t-start_t);
516 write(fildes[1], (char *)&ccc, 1);
521 * Cu-like put command
532 if (prompt("[put] ", copyname, sizeof(copyname)))
534 if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 || argc > 2) {
535 printf("usage: <put> from [to]\r\n");
540 copynamex = expand(argv[0]);
541 if ((fd = fopen(copynamex, "r")) == NULL) {
542 printf("%s: cannot open\r\n", copynamex);
545 if (boolean(value(ECHOCHECK)))
546 snprintf(line, sizeof(line), "cat>%s\r", argv[1]);
548 snprintf(line, sizeof(line), "stty -echo;cat>%s;stty echo\r", argv[1]);
549 transmit(fd, "\04", line);
556 if (usleep(msec*1000) != 0) {
557 fprintf ( stderr, "warning: ldelay or cdelay interrupted, "
558 "delay time cut short: %s\n",
567 * FTP - send single character
568 * wait for echo & handle timeout
578 if (number(value(CDELAY)) > 0 && c != '\r')
579 nap(number(value(CDELAY)));
580 if (!boolean(value(ECHOCHECK))) {
581 if (number(value(LDELAY)) > 0 && c == '\r')
582 nap(number(value(LDELAY)));
587 alarm(number(value(ETIMEOUT)));
591 printf("\r\ntimeout error (%s)\r\n", ctrl(c));
594 xpwrite(FD, &null, 1); /* poke it */
602 signal(SIGALRM, timeout);
607 * Stolen from consh() -- puts a remote file on the output of a local command.
608 * Identical to consh() except for where stdout goes.
618 if (prompt("Local command? ", buf, sizeof(buf)))
620 kill(pid, SIGIOT); /* put TIPOUT into a wait state */
621 signal(SIGINT, SIG_IGN);
622 signal(SIGQUIT, SIG_IGN);
624 read(repdes[0], (char *)&ccc, 1);
626 * Set up file descriptors in the child and
629 if ((cpid = fork()) < 0)
630 printf("can't fork!\r\n");
633 while ((p = wait(&status)) > 0 && p != cpid)
639 for (i = 3; i < 20; i++)
641 signal(SIGINT, SIG_DFL);
642 signal(SIGQUIT, SIG_DFL);
644 printf("can't find `%s'\r\n", buf);
647 if (boolean(value(VERBOSE)))
648 prtime("away for ", time(0)-start);
649 write(fildes[1], (char *)&ccc, 1);
651 signal(SIGINT, SIG_DFL);
652 signal(SIGQUIT, SIG_DFL);
658 tiplink (char *cmd, unsigned int flags)
663 if (flags & TL_SIGNAL_TIPOUT) {
664 kill(pid, SIGIOT); /* put TIPOUT into a wait state */
665 signal(SIGINT, SIG_IGN);
666 signal(SIGQUIT, SIG_IGN);
668 read(repdes[0], (char *)&ccc, 1);
672 * Set up file descriptors in the child and
675 if ((cpid = fork()) < 0)
676 printf("can't fork!\r\n");
679 while ((p = wait(&status)) > 0 && p != cpid)
686 for (fd = 3; fd < 20; fd++)
688 signal(SIGINT, SIG_DFL);
689 signal(SIGQUIT, SIG_DFL);
691 printf("can't find `%s'\r\n", cmd);
695 if (flags & TL_VERBOSE && boolean(value(VERBOSE)))
696 prtime("away for ", time(0)-start);
698 if (flags & TL_SIGNAL_TIPOUT) {
699 write(fildes[1], (char *)&ccc, 1);
701 signal(SIGINT, SIG_DFL);
702 signal(SIGQUIT, SIG_DFL);
709 * Fork a program with:
710 * 0 <-> remote tty in
711 * 1 <-> remote tty out
712 * 2 <-> local tty out
719 if (prompt("Local command? ", buf, sizeof(buf)))
721 tiplink (buf, TL_SIGNAL_TIPOUT | TL_VERBOSE);
727 * Escape to local shell
736 signal(SIGINT, SIG_IGN);
737 signal(SIGQUIT, SIG_IGN);
739 if ((shpid = fork())) {
740 while (shpid != wait(&status));
743 signal(SIGINT, SIG_DFL);
744 signal(SIGQUIT, SIG_DFL);
747 signal(SIGQUIT, SIG_DFL);
748 signal(SIGINT, SIG_DFL);
749 if ((cp = rindex(value(SHELL), '/')) == NULL)
754 execl(value(SHELL), cp, 0);
755 printf("\r\ncan't execl!\r\n");
761 * TIPIN portion of scripting
762 * initiate the conversation with TIPOUT
769 * enable TIPOUT side for dialogue
772 if (boolean(value(SCRIPT)))
773 write(fildes[1], value(RECORD), size(value(RECORD)));
774 write(fildes[1], "\n", 1);
776 * wait for TIPOUT to finish
778 read(repdes[0], &c, 1);
780 printf("can't create %s\r\n", value(RECORD));
784 * Change current working directory of
785 * local portion of tip
790 char dirname[PATH_MAX];
791 register char *cp = dirname;
793 if (prompt("[cd] ", dirname, sizeof(dirname))) {
799 printf("%s: bad directory\r\n", cp);
810 printf("\r\n%s", msg);
811 printf("\r\n[EOT]\r\n");
813 (void)uu_unlock(uucplock);
821 char *abortmsg = NULL, *dismsg;
823 if (LO != NULL && tiplink (LO, TL_SIGNAL_TIPOUT) != 0) {
824 abortmsg = "logout failed";
827 if ((dismsg = value(DISCONNECT)) != NULL) {
828 write(FD, dismsg, strlen(dismsg));
847 if ((cp = rindex(value(SHELL), '/')) == NULL)
852 execl(value(SHELL), cp, "-c", s, 0);
856 args(char *buf, char **a, int num)
858 register char *p = buf, *start;
859 register char **parg = a;
862 while (*p && n < num) {
863 while (*p && (*p == ' ' || *p == '\t'))
868 while (*p && (*p != ' ' && *p != '\t'))
879 prtime(char *s, time_t a)
884 for (i = 0; i < 3; i++) {
885 nums[i] = (int)(a % quant[i]);
890 if (nums[i] || (i == 0 && nums[1] == 0 && nums[2] == 0))
891 printf("%d %s%c ", nums[i], sep[i],
892 nums[i] == 1 ? '\0' : 's');
901 if (prompt("[set] ", buf, sizeof(buf)))
904 if (vtable[BEAUTIFY].v_access&CHANGED) {
905 vtable[BEAUTIFY].v_access &= ~CHANGED;
908 if (vtable[SCRIPT].v_access&CHANGED) {
909 vtable[SCRIPT].v_access &= ~CHANGED;
912 * So that "set record=blah script" doesn't
913 * cause two transactions to occur.
915 if (vtable[RECORD].v_access&CHANGED)
916 vtable[RECORD].v_access &= ~CHANGED;
918 if (vtable[RECORD].v_access&CHANGED) {
919 vtable[RECORD].v_access &= ~CHANGED;
920 if (boolean(value(SCRIPT)))
923 if (vtable[TAND].v_access&CHANGED) {
924 vtable[TAND].v_access &= ~CHANGED;
925 if (boolean(value(TAND)))
930 if (vtable[LECHO].v_access&CHANGED) {
931 vtable[LECHO].v_access &= ~CHANGED;
932 HD = boolean(value(LECHO));
934 if (vtable[PARITY].v_access&CHANGED) {
935 vtable[PARITY].v_access &= ~CHANGED;
936 setparity(value(PARITY));
941 * Turn tandem mode on or off for remote tty.
947 struct termios ttermios;
948 tcgetattr (FD, &ttermios);
949 if (strcmp(option,"on") == 0) {
950 ttermios.c_iflag |= IXOFF;
951 ctermios.c_iflag |= IXOFF;
954 ttermios.c_iflag &= ~IXOFF;
955 ctermios.c_iflag &= ~IXOFF;
957 tcsetattr (FD, TCSANOW, &ttermios);
958 tcsetattr (0, TCSANOW, &ctermios);
959 #else /* HAVE_TERMIOS */
962 ioctl(FD, TIOCGETP, &rmtty);
963 if (strcmp(option,"on") == 0) {
964 rmtty.sg_flags |= TANDEM;
965 arg.sg_flags |= TANDEM;
967 rmtty.sg_flags &= ~TANDEM;
968 arg.sg_flags &= ~TANDEM;
970 ioctl(FD, TIOCSETP, &rmtty);
971 ioctl(0, TIOCSETP, &arg);
972 #endif /* HAVE_TERMIOS */
982 ioctl(FD, TIOCSBRK, NULL);
984 ioctl(FD, TIOCCBRK, NULL);
995 kill(c == CTRL('y') ? getpid() : 0, SIGTSTP);
1000 * expand a file name if it includes shell meta characters
1006 static char xname[BUFSIZ];
1007 char cmdbuf[BUFSIZ];
1008 register int pid, l;
1009 register char *cp, *Shell;
1010 int s, pivec[2] /*, (*sigint)()*/;
1012 if (!anyof(name, "~{[*?$`'\"\\"))
1014 /* sigint = signal(SIGINT, SIG_IGN); */
1015 if (pipe(pivec) < 0) {
1017 /* signal(SIGINT, sigint) */
1020 snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
1021 if ((pid = vfork()) == 0) {
1022 Shell = value(SHELL);
1024 Shell = _PATH_BSHELL;
1031 execl(Shell, Shell, "-c", cmdbuf, 0);
1041 l = read(pivec[0], xname, BUFSIZ);
1043 while (wait(&s) != pid);
1046 if (s != 0 && s != SIGPIPE) {
1047 fprintf(stderr, "\"Echo\" failed\n");
1055 fprintf(stderr, "\"%s\": No match\n", name);
1059 fprintf(stderr, "Buffer overflow expanding \"%s\"\n", name);
1063 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
1070 * Are any of the characters in the two strings the same?
1074 anyof(register char *s1, register char *s2)