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 $
38 #include "pathnames.h"
40 #include <sys/types.h>
50 * miscellaneous commands
53 int quant[] = { 60, 60, 24 };
56 char *sep[] = { "second", "minute", "hour" };
57 static char *argv[10]; /* argument vector for take and put */
59 static void stopsnd(int); /* SIGINT handler during file transfers */
60 static void intcopy(int); /* interrupt routine for file transfers */
62 static int anyof(char *, char *);
63 static void tandem(char *);
64 static void prtime(char *, time_t);
65 static int args(char *, char **, int);
66 static void execute(char *);
67 static void send(char);
68 static void transmit(FILE *, char *, char *);
69 static void transfer(char *, int, char *);
70 static void xfer(char *, int, char *);
76 struct termios ttermios;
78 for (cnt = 0; cnt < NCCS; cnt++)
79 ttermios.c_cc [cnt] = otermios.c_cc [cnt];
80 tcsetattr (0, TCSANOW, &ttermios);
86 tcsetattr (0, TCSANOW, &ctermios);
93 ioctl (FD, TIOCFLUSH, &cmd);
97 * FTP - remote ==> local
98 * get a file from the remote host
107 * get the UNIX receiving file's name
109 if (prompt("Local file name? ", copyname, sizeof(copyname)))
111 cp = expand(copyname);
112 if ((sfd = creat(cp, 0666)) < 0) {
113 printf("\r\n%s: cannot creat\r\n", copyname);
120 if (prompt("List command for remote system? ", buf, sizeof(buf))) {
124 transfer(buf, sfd, value(EOFREAD));
128 * Cu-like take command
134 char line[BUFSIZ], *cp;
136 if (prompt("[take] ", copyname, sizeof(copyname)))
138 if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 || argc > 2) {
139 printf("usage: <take> from [to]\r\n");
144 cp = expand(argv[1]);
145 if ((fd = creat(cp, 0666)) < 0) {
146 printf("\r\n%s: cannot create\r\n", argv[1]);
149 (void)snprintf(line, sizeof(line), "cat %s ; echo \"\" ; echo ___tip_end_of_file_marker___", argv[0]);
150 xfer(line, fd, "\n___tip_end_of_file_marker___\n");
153 static jmp_buf intbuf;
156 xfer(char *buf, int fd, char *eofchars)
166 v = boolean(value(VERBOSE));
168 if ((ff = fdopen (fd, "w")) == NULL) {
172 if ((cnt = number(value(FRAMESIZE))) != BUFSIZ)
173 if (setvbuf(ff, NULL, _IOFBF, cnt) != 0) {
174 warn("file allocation");
179 xpwrite(FD, buf, size(buf));
182 read(repdes[0], (char *)&ccc, 1); /* Wait until read process stops */
191 while ((c&0177) != '\n');
195 (void) setjmp(intbuf);
196 f = signal(SIGINT, intcopy);
199 for (ct = 0; !quit;) {
200 eof = read(FD, &c, 1) <= 0;
207 continue; /* ignore nulls */
210 if (c != *match && match > eofchars) {
214 (void)printf("\r%d", ++ct);
220 if (*++match == '\0')
224 (void)printf("\r%d", ++ct);
229 prtime(" lines transferred in ", time(0)-start);
231 write(fildes[1], (char *)&ccc, 1);
237 * Bulk transfer routine --
238 * used by getfl(), cu_take(), and pipefile()
241 transfer(char *buf, int fd, char *eofchars)
251 v = boolean(value(VERBOSE));
253 if ((ff = fdopen (fd, "w")) == NULL) {
257 if ((cnt = number(value(FRAMESIZE))) != BUFSIZ)
258 if (setvbuf(ff, NULL, _IOFBF, cnt) != 0) {
259 warn("file allocation");
264 xpwrite(FD, buf, size(buf));
267 read(repdes[0], (char *)&ccc, 1); /* Wait until read process stops */
276 while ((c&0177) != '\n');
278 (void) setjmp(intbuf);
279 f = signal(SIGINT, intcopy);
281 for (ct = 0; !quit;) {
282 eof = read(FD, &c, 1) <= 0;
286 if (eof || any(c, eofchars))
289 continue; /* ignore nulls */
293 printf("\r%d", ++ct);
297 prtime(" lines transferred in ", time(0)-start);
299 write(fildes[1], (char *)&ccc, 1);
305 * FTP - remote ==> local process
306 * send remote input to local process via pipe
315 if (prompt("Local command? ", buf, sizeof(buf)))
319 printf("can't establish pipe\r\n");
323 if ((cpid = fork()) < 0) {
324 printf("can't fork!\r\n");
327 if (prompt("List command for remote system? ", buf, sizeof(buf))) {
328 close(pdes[0]), close(pdes[1]);
329 kill (cpid, SIGKILL);
332 signal(SIGPIPE, intcopy);
333 transfer(buf, pdes[1], value(EOFREAD));
334 signal(SIGPIPE, SIG_DFL);
335 while ((p = wait(&status)) > 0 && p != cpid)
343 for (f = 3; f < 20; f++)
346 printf("can't execl!\r\n");
352 * Interrupt service routine for FTP
359 signal(SIGINT, SIG_IGN);
363 * FTP - local ==> remote
364 * send local file to remote host
365 * terminate transmission with pseudo EOF sequence
377 if (prompt("Local file name? ", fname, sizeof(fname)))
383 fnamex = expand(fname);
384 if ((fd = fopen(fnamex, "r")) == NULL) {
385 printf("%s: cannot open\r\n", fname);
388 transmit(fd, value(EOFWRITE), NULL);
389 if (!boolean(value(ECHOCHECK))) {
395 * Bulk transfer routine to remote host --
396 * used by sendfile() and cu_put()
399 transmit(FILE *fd, char *eofchars, char *command)
402 int c, ccount, lcount;
403 time_t start_t, stop_t;
406 kill(pid, SIGIOT); /* put TIPOUT into a wait state */
408 f = signal(SIGINT, stopsnd);
410 read(repdes[0], (char *)&ccc, 1);
411 if (command != NULL) {
412 for (pc = command; *pc; pc++)
414 if (boolean(value(ECHOCHECK)))
415 read(FD, (char *)&c, 1); /* trailing \n */
418 sleep(5); /* wait for remote stty to take effect */
432 if (c == 0177 && !boolean(value(RAWFTP)))
437 if (!boolean(value(RAWFTP)))
440 else if (c == '\t') {
441 if (!boolean(value(RAWFTP))) {
442 if (boolean(value(TABEXPAND))) {
444 while ((++ccount % 8) != 0)
450 if (!boolean(value(RAWFTP)))
454 } while (c != '\r' && !boolean(value(RAWFTP)));
455 if (boolean(value(VERBOSE)))
456 printf("\r%d", ++lcount);
457 if (boolean(value(ECHOCHECK))) {
459 alarm(number(value(ETIMEOUT)));
460 do { /* wait for prompt */
461 read(FD, (char *)&c, 1);
462 if (timedout || stop) {
464 printf("\r\ntimed out at eol\r\n");
468 } while ((c&0177) != character(value(PROMPT)));
473 if (lastc != '\n' && !boolean(value(RAWFTP)))
475 for (pc = eofchars; pc && *pc; pc++)
480 if (boolean(value(VERBOSE))) {
481 if (boolean(value(RAWFTP)))
482 prtime(" chars transferred in ", stop_t-start_t);
484 prtime(" lines transferred in ", stop_t-start_t);
486 write(fildes[1], (char *)&ccc, 1);
491 * Cu-like put command
501 if (prompt("[put] ", copyname, sizeof(copyname)))
503 if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 || argc > 2) {
504 printf("usage: <put> from [to]\r\n");
509 copynamex = expand(argv[0]);
510 if ((fd = fopen(copynamex, "r")) == NULL) {
511 printf("%s: cannot open\r\n", copynamex);
514 if (boolean(value(ECHOCHECK)))
515 snprintf(line, sizeof(line), "cat>%s\r", argv[1]);
517 snprintf(line, sizeof(line), "stty -echo;cat>%s;stty echo\r", argv[1]);
518 transmit(fd, "\04", line);
525 if (usleep(msec*1000) != 0) {
526 fprintf ( stderr, "warning: ldelay or cdelay interrupted, "
527 "delay time cut short: %s\n",
536 * FTP - send single character
537 * wait for echo & handle timeout
547 if (number(value(CDELAY)) > 0 && c != '\r')
548 nap(number(value(CDELAY)));
549 if (!boolean(value(ECHOCHECK))) {
550 if (number(value(LDELAY)) > 0 && c == '\r')
551 nap(number(value(LDELAY)));
556 alarm(number(value(ETIMEOUT)));
560 printf("\r\ntimeout error (%s)\r\n", ctrl(c));
563 xpwrite(FD, &null, 1); /* poke it */
569 timeoutfunc(int __dummy)
571 signal(SIGALRM, timeoutfunc);
576 * Stolen from consh() -- puts a remote file on the output of a local command.
577 * Identical to consh() except for where stdout goes.
587 if (prompt("Local command? ", buf, sizeof(buf)))
589 kill(pid, SIGIOT); /* put TIPOUT into a wait state */
590 signal(SIGINT, SIG_IGN);
591 signal(SIGQUIT, SIG_IGN);
593 read(repdes[0], (char *)&ccc, 1);
595 * Set up file descriptors in the child and
598 if ((cpid = fork()) < 0)
599 printf("can't fork!\r\n");
602 while ((p = wait(&status)) > 0 && p != cpid)
608 for (i = 3; i < 20; i++)
610 signal(SIGINT, SIG_DFL);
611 signal(SIGQUIT, SIG_DFL);
613 printf("can't find `%s'\r\n", buf);
616 if (boolean(value(VERBOSE)))
617 prtime("away for ", time(0)-start);
618 write(fildes[1], (char *)&ccc, 1);
620 signal(SIGINT, SIG_DFL);
621 signal(SIGQUIT, SIG_DFL);
625 tiplink (char *cmd, unsigned int flags)
630 if (flags & TL_SIGNAL_TIPOUT) {
631 kill(pid, SIGIOT); /* put TIPOUT into a wait state */
632 signal(SIGINT, SIG_IGN);
633 signal(SIGQUIT, SIG_IGN);
635 read(repdes[0], (char *)&ccc, 1);
639 * Set up file descriptors in the child and
642 if ((cpid = fork()) < 0)
643 printf("can't fork!\r\n");
646 while ((p = wait(&status)) > 0 && p != cpid)
653 for (fd = 3; fd < 20; fd++)
655 signal(SIGINT, SIG_DFL);
656 signal(SIGQUIT, SIG_DFL);
658 printf("can't find `%s'\r\n", cmd);
662 if (flags & TL_VERBOSE && boolean(value(VERBOSE)))
663 prtime("away for ", time(0)-start);
665 if (flags & TL_SIGNAL_TIPOUT) {
666 write(fildes[1], (char *)&ccc, 1);
668 signal(SIGINT, SIG_DFL);
669 signal(SIGQUIT, SIG_DFL);
676 * Fork a program with:
677 * 0 <-> remote tty in
678 * 1 <-> remote tty out
679 * 2 <-> local tty out
686 if (prompt("Local command? ", buf, sizeof(buf)))
688 tiplink (buf, TL_SIGNAL_TIPOUT | TL_VERBOSE);
692 * Escape to local shell
701 signal(SIGINT, SIG_IGN);
702 signal(SIGQUIT, SIG_IGN);
704 if ((shpid = fork())) {
705 while (shpid != wait(&status));
708 signal(SIGINT, SIG_DFL);
709 signal(SIGQUIT, SIG_DFL);
712 signal(SIGQUIT, SIG_DFL);
713 signal(SIGINT, SIG_DFL);
714 if ((cp = rindex(value(SHELL), '/')) == NULL)
719 execl(value(SHELL), cp, NULL);
720 printf("\r\ncan't execl!\r\n");
726 * TIPIN portion of scripting
727 * initiate the conversation with TIPOUT
734 * enable TIPOUT side for dialogue
737 if (boolean(value(SCRIPT)))
738 write(fildes[1], value(RECORD), size(value(RECORD)));
739 write(fildes[1], "\n", 1);
741 * wait for TIPOUT to finish
743 read(repdes[0], &c, 1);
745 printf("can't create %s\r\n", value(RECORD));
749 * Change current working directory of
750 * local portion of tip
755 char dirname[PATH_MAX];
758 if (prompt("[cd] ", dirname, sizeof(dirname))) {
764 printf("%s: bad directory\r\n", cp);
775 printf("\r\n%s", msg);
776 printf("\r\n[EOT]\r\n");
778 (void)uu_unlock(uucplock);
786 char *abortmsg = NULL, *dismsg;
788 if (LO != NULL && tiplink (LO, TL_SIGNAL_TIPOUT) != 0) {
789 abortmsg = "logout failed";
792 if ((dismsg = value(DISCONNECT)) != NULL) {
793 write(FD, dismsg, strlen(dismsg));
812 if ((cp = rindex(value(SHELL), '/')) == NULL)
817 execl(value(SHELL), cp, "-c", s, NULL);
821 args(char *buf, char **a, int num)
823 char *p = buf, *start;
827 while (*p && n < num) {
828 while (*p && (*p == ' ' || *p == '\t'))
833 while (*p && (*p != ' ' && *p != '\t'))
844 prtime(char *s, time_t a)
849 for (i = 0; i < 3; i++) {
850 nums[i] = (int)(a % quant[i]);
855 if (nums[i] || (i == 0 && nums[1] == 0 && nums[2] == 0))
856 printf("%d %s%c ", nums[i], sep[i],
857 nums[i] == 1 ? '\0' : 's');
866 if (prompt("[set] ", buf, sizeof(buf)))
869 if (vtable[BEAUTIFY].v_access&CHANGED) {
870 vtable[BEAUTIFY].v_access &= ~CHANGED;
873 if (vtable[SCRIPT].v_access&CHANGED) {
874 vtable[SCRIPT].v_access &= ~CHANGED;
877 * So that "set record=blah script" doesn't
878 * cause two transactions to occur.
880 if (vtable[RECORD].v_access&CHANGED)
881 vtable[RECORD].v_access &= ~CHANGED;
883 if (vtable[RECORD].v_access&CHANGED) {
884 vtable[RECORD].v_access &= ~CHANGED;
885 if (boolean(value(SCRIPT)))
888 if (vtable[TAND].v_access&CHANGED) {
889 vtable[TAND].v_access &= ~CHANGED;
890 if (boolean(value(TAND)))
895 if (vtable[LECHO].v_access&CHANGED) {
896 vtable[LECHO].v_access &= ~CHANGED;
897 HD = boolean(value(LECHO));
899 if (vtable[PARITY].v_access&CHANGED) {
900 vtable[PARITY].v_access &= ~CHANGED;
901 setparity(value(PARITY));
906 * Turn tandem mode on or off for remote tty.
911 struct termios ttermios;
912 tcgetattr (FD, &ttermios);
913 if (strcmp(option,"on") == 0) {
914 ttermios.c_iflag |= IXOFF;
915 ctermios.c_iflag |= IXOFF;
918 ttermios.c_iflag &= ~IXOFF;
919 ctermios.c_iflag &= ~IXOFF;
921 tcsetattr (FD, TCSANOW, &ttermios);
922 tcsetattr (0, TCSANOW, &ctermios);
932 ioctl(FD, TIOCSBRK, NULL);
934 ioctl(FD, TIOCCBRK, NULL);
945 kill(c == CTRL('y') ? getpid() : 0, SIGTSTP);
950 * expand a file name if it includes shell meta characters
956 static char xname[BUFSIZ];
960 int s, pivec[2] /*, (*sigint)()*/;
962 if (!anyof(name, "~{[*?$`'\"\\"))
964 /* sigint = signal(SIGINT, SIG_IGN); */
965 if (pipe(pivec) < 0) {
967 /* signal(SIGINT, sigint) */
970 snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
971 if ((pid = vfork()) == 0) {
972 Shell = value(SHELL);
974 Shell = _PATH_BSHELL;
981 execl(Shell, Shell, "-c", cmdbuf, NULL);
991 l = read(pivec[0], xname, BUFSIZ);
993 while (wait(&s) != pid)
996 if (s != 0 && s != SIGPIPE) {
997 fprintf(stderr, "\"Echo\" failed\n");
1005 fprintf(stderr, "\"%s\": No match\n", name);
1009 fprintf(stderr, "Buffer overflow expanding \"%s\"\n", name);
1013 for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
1020 * Are any of the characters in the two strings the same?
1024 anyof(char *s1, char *s2)