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 $
39 #include "pathnames.h"
41 #include <sys/types.h>
51 * miscellaneous commands
54 int quant
[] = { 60, 60, 24 };
57 char *sep
[] = { "second", "minute", "hour" };
58 static char *argv
[10]; /* argument vector for take and put */
60 static void stopsnd(int); /* SIGINT handler during file transfers */
61 static void intcopy(int); /* interrupt routine for file transfers */
63 static int anyof(char *, char *);
64 static void tandem(char *);
65 static void prtime(char *, time_t);
66 static int args(char *, char **, int);
67 static void execute(char *);
68 static void send(char);
69 static void transmit(FILE *, char *, char *);
70 static void transfer(char *, int, char *);
71 static void xfer(char *, int, char *);
78 struct termios ttermios
;
80 for (cnt
= 0; cnt
< NCCS
; cnt
++)
81 ttermios
.c_cc
[cnt
] = otermios
.c_cc
[cnt
];
82 tcsetattr (0, TCSANOW
, &ttermios
);
84 ioctl(0, TIOCSETC
, &defchars
);
92 tcsetattr (0, TCSANOW
, &ctermios
);
94 ioctl(0, TIOCSETC
, &tchars
);
103 ioctl (FD
, TIOCFLUSH
, &cmd
);
106 ioctl (FD
, TIOCGETP
, &buf
); /* this does a */
107 ioctl (FD
, TIOCSETP
, &buf
); /* wflushtty */
112 * FTP - remote ==> local
113 * get a file from the remote host
122 * get the UNIX receiving file's name
124 if (prompt("Local file name? ", copyname
, sizeof(copyname
)))
126 cp
= expand(copyname
);
127 if ((sfd
= creat(cp
, 0666)) < 0) {
128 printf("\r\n%s: cannot creat\r\n", copyname
);
135 if (prompt("List command for remote system? ", buf
, sizeof(buf
))) {
139 transfer(buf
, sfd
, value(EOFREAD
));
143 * Cu-like take command
149 char line
[BUFSIZ
], *cp
;
151 if (prompt("[take] ", copyname
, sizeof(copyname
)))
153 if ((argc
= args(copyname
, argv
, sizeof(argv
)/sizeof(argv
[0]))) < 1 || argc
> 2) {
154 printf("usage: <take> from [to]\r\n");
159 cp
= expand(argv
[1]);
160 if ((fd
= creat(cp
, 0666)) < 0) {
161 printf("\r\n%s: cannot create\r\n", argv
[1]);
164 (void)snprintf(line
, sizeof(line
), "cat %s ; echo \"\" ; echo ___tip_end_of_file_marker___", argv
[0]);
165 xfer(line
, fd
, "\n___tip_end_of_file_marker___\n");
168 static jmp_buf intbuf
;
171 xfer(char *buf
, int fd
, char *eofchars
)
181 v
= boolean(value(VERBOSE
));
183 if ((ff
= fdopen (fd
, "w")) == NULL
) {
187 if ((cnt
= number(value(FRAMESIZE
))) != BUFSIZ
)
188 if (setvbuf(ff
, NULL
, _IOFBF
, cnt
) != 0) {
189 warn("file allocation");
194 xpwrite(FD
, buf
, size(buf
));
197 read(repdes
[0], (char *)&ccc
, 1); /* Wait until read process stops */
206 while ((c
&0177) != '\n');
210 (void) setjmp(intbuf
);
211 f
= signal(SIGINT
, intcopy
);
214 for (ct
= 0; !quit
;) {
215 eof
= read(FD
, &c
, 1) <= 0;
222 continue; /* ignore nulls */
225 if (c
!= *match
&& match
> eofchars
) {
229 (void)printf("\r%d", ++ct
);
235 if (*++match
== '\0')
239 (void)printf("\r%d", ++ct
);
244 prtime(" lines transferred in ", time(0)-start
);
246 write(fildes
[1], (char *)&ccc
, 1);
252 * Bulk transfer routine --
253 * used by getfl(), cu_take(), and pipefile()
256 transfer(char *buf
, int fd
, char *eofchars
)
266 v
= boolean(value(VERBOSE
));
268 if ((ff
= fdopen (fd
, "w")) == NULL
) {
272 if ((cnt
= number(value(FRAMESIZE
))) != BUFSIZ
)
273 if (setvbuf(ff
, NULL
, _IOFBF
, cnt
) != 0) {
274 warn("file allocation");
279 xpwrite(FD
, buf
, size(buf
));
282 read(repdes
[0], (char *)&ccc
, 1); /* Wait until read process stops */
291 while ((c
&0177) != '\n');
293 (void) setjmp(intbuf
);
294 f
= signal(SIGINT
, intcopy
);
296 for (ct
= 0; !quit
;) {
297 eof
= read(FD
, &c
, 1) <= 0;
301 if (eof
|| any(c
, eofchars
))
304 continue; /* ignore nulls */
308 printf("\r%d", ++ct
);
312 prtime(" lines transferred in ", time(0)-start
);
314 write(fildes
[1], (char *)&ccc
, 1);
320 * FTP - remote ==> local process
321 * send remote input to local process via pipe
330 if (prompt("Local command? ", buf
, sizeof(buf
)))
334 printf("can't establish pipe\r\n");
338 if ((cpid
= fork()) < 0) {
339 printf("can't fork!\r\n");
342 if (prompt("List command for remote system? ", buf
, sizeof(buf
))) {
343 close(pdes
[0]), close(pdes
[1]);
344 kill (cpid
, SIGKILL
);
347 signal(SIGPIPE
, intcopy
);
348 transfer(buf
, pdes
[1], value(EOFREAD
));
349 signal(SIGPIPE
, SIG_DFL
);
350 while ((p
= wait(&status
)) > 0 && p
!= cpid
)
358 for (f
= 3; f
< 20; f
++)
361 printf("can't execl!\r\n");
367 * Interrupt service routine for FTP
374 signal(SIGINT
, SIG_IGN
);
378 * FTP - local ==> remote
379 * send local file to remote host
380 * terminate transmission with pseudo EOF sequence
392 if (prompt("Local file name? ", fname
, sizeof(fname
)))
398 fnamex
= expand(fname
);
399 if ((fd
= fopen(fnamex
, "r")) == NULL
) {
400 printf("%s: cannot open\r\n", fname
);
403 transmit(fd
, value(EOFWRITE
), NULL
);
404 if (!boolean(value(ECHOCHECK
))) {
410 * Bulk transfer routine to remote host --
411 * used by sendfile() and cu_put()
414 transmit(FILE *fd
, char *eofchars
, char *command
)
417 int c
, ccount
, lcount
;
418 time_t start_t
, stop_t
;
421 kill(pid
, SIGIOT
); /* put TIPOUT into a wait state */
423 f
= signal(SIGINT
, stopsnd
);
425 read(repdes
[0], (char *)&ccc
, 1);
426 if (command
!= NULL
) {
427 for (pc
= command
; *pc
; pc
++)
429 if (boolean(value(ECHOCHECK
)))
430 read(FD
, (char *)&c
, 1); /* trailing \n */
433 sleep(5); /* wait for remote stty to take effect */
447 if (c
== 0177 && !boolean(value(RAWFTP
)))
452 if (!boolean(value(RAWFTP
)))
455 else if (c
== '\t') {
456 if (!boolean(value(RAWFTP
))) {
457 if (boolean(value(TABEXPAND
))) {
459 while ((++ccount
% 8) != 0)
465 if (!boolean(value(RAWFTP
)))
469 } while (c
!= '\r' && !boolean(value(RAWFTP
)));
470 if (boolean(value(VERBOSE
)))
471 printf("\r%d", ++lcount
);
472 if (boolean(value(ECHOCHECK
))) {
474 alarm(number(value(ETIMEOUT
)));
475 do { /* wait for prompt */
476 read(FD
, (char *)&c
, 1);
477 if (timedout
|| stop
) {
479 printf("\r\ntimed out at eol\r\n");
483 } while ((c
&0177) != character(value(PROMPT
)));
488 if (lastc
!= '\n' && !boolean(value(RAWFTP
)))
490 for (pc
= eofchars
; pc
&& *pc
; pc
++)
495 if (boolean(value(VERBOSE
))) {
496 if (boolean(value(RAWFTP
)))
497 prtime(" chars transferred in ", stop_t
-start_t
);
499 prtime(" lines transferred in ", stop_t
-start_t
);
501 write(fildes
[1], (char *)&ccc
, 1);
506 * Cu-like put command
516 if (prompt("[put] ", copyname
, sizeof(copyname
)))
518 if ((argc
= args(copyname
, argv
, sizeof(argv
)/sizeof(argv
[0]))) < 1 || argc
> 2) {
519 printf("usage: <put> from [to]\r\n");
524 copynamex
= expand(argv
[0]);
525 if ((fd
= fopen(copynamex
, "r")) == NULL
) {
526 printf("%s: cannot open\r\n", copynamex
);
529 if (boolean(value(ECHOCHECK
)))
530 snprintf(line
, sizeof(line
), "cat>%s\r", argv
[1]);
532 snprintf(line
, sizeof(line
), "stty -echo;cat>%s;stty echo\r", argv
[1]);
533 transmit(fd
, "\04", line
);
540 if (usleep(msec
*1000) != 0) {
541 fprintf ( stderr
, "warning: ldelay or cdelay interrupted, "
542 "delay time cut short: %s\n",
551 * FTP - send single character
552 * wait for echo & handle timeout
562 if (number(value(CDELAY
)) > 0 && c
!= '\r')
563 nap(number(value(CDELAY
)));
564 if (!boolean(value(ECHOCHECK
))) {
565 if (number(value(LDELAY
)) > 0 && c
== '\r')
566 nap(number(value(LDELAY
)));
571 alarm(number(value(ETIMEOUT
)));
575 printf("\r\ntimeout error (%s)\r\n", ctrl(c
));
578 xpwrite(FD
, &null
, 1); /* poke it */
584 timeoutfunc(int __dummy
)
586 signal(SIGALRM
, timeoutfunc
);
591 * Stolen from consh() -- puts a remote file on the output of a local command.
592 * Identical to consh() except for where stdout goes.
602 if (prompt("Local command? ", buf
, sizeof(buf
)))
604 kill(pid
, SIGIOT
); /* put TIPOUT into a wait state */
605 signal(SIGINT
, SIG_IGN
);
606 signal(SIGQUIT
, SIG_IGN
);
608 read(repdes
[0], (char *)&ccc
, 1);
610 * Set up file descriptors in the child and
613 if ((cpid
= fork()) < 0)
614 printf("can't fork!\r\n");
617 while ((p
= wait(&status
)) > 0 && p
!= cpid
)
623 for (i
= 3; i
< 20; i
++)
625 signal(SIGINT
, SIG_DFL
);
626 signal(SIGQUIT
, SIG_DFL
);
628 printf("can't find `%s'\r\n", buf
);
631 if (boolean(value(VERBOSE
)))
632 prtime("away for ", time(0)-start
);
633 write(fildes
[1], (char *)&ccc
, 1);
635 signal(SIGINT
, SIG_DFL
);
636 signal(SIGQUIT
, SIG_DFL
);
642 tiplink (char *cmd
, unsigned int flags
)
647 if (flags
& TL_SIGNAL_TIPOUT
) {
648 kill(pid
, SIGIOT
); /* put TIPOUT into a wait state */
649 signal(SIGINT
, SIG_IGN
);
650 signal(SIGQUIT
, SIG_IGN
);
652 read(repdes
[0], (char *)&ccc
, 1);
656 * Set up file descriptors in the child and
659 if ((cpid
= fork()) < 0)
660 printf("can't fork!\r\n");
663 while ((p
= wait(&status
)) > 0 && p
!= cpid
)
670 for (fd
= 3; fd
< 20; fd
++)
672 signal(SIGINT
, SIG_DFL
);
673 signal(SIGQUIT
, SIG_DFL
);
675 printf("can't find `%s'\r\n", cmd
);
679 if (flags
& TL_VERBOSE
&& boolean(value(VERBOSE
)))
680 prtime("away for ", time(0)-start
);
682 if (flags
& TL_SIGNAL_TIPOUT
) {
683 write(fildes
[1], (char *)&ccc
, 1);
685 signal(SIGINT
, SIG_DFL
);
686 signal(SIGQUIT
, SIG_DFL
);
693 * Fork a program with:
694 * 0 <-> remote tty in
695 * 1 <-> remote tty out
696 * 2 <-> local tty out
703 if (prompt("Local command? ", buf
, sizeof(buf
)))
705 tiplink (buf
, TL_SIGNAL_TIPOUT
| TL_VERBOSE
);
710 * Escape to local shell
719 signal(SIGINT
, SIG_IGN
);
720 signal(SIGQUIT
, SIG_IGN
);
722 if ((shpid
= fork())) {
723 while (shpid
!= wait(&status
));
726 signal(SIGINT
, SIG_DFL
);
727 signal(SIGQUIT
, SIG_DFL
);
730 signal(SIGQUIT
, SIG_DFL
);
731 signal(SIGINT
, SIG_DFL
);
732 if ((cp
= rindex(value(SHELL
), '/')) == NULL
)
737 execl(value(SHELL
), cp
, NULL
);
738 printf("\r\ncan't execl!\r\n");
744 * TIPIN portion of scripting
745 * initiate the conversation with TIPOUT
752 * enable TIPOUT side for dialogue
755 if (boolean(value(SCRIPT
)))
756 write(fildes
[1], value(RECORD
), size(value(RECORD
)));
757 write(fildes
[1], "\n", 1);
759 * wait for TIPOUT to finish
761 read(repdes
[0], &c
, 1);
763 printf("can't create %s\r\n", value(RECORD
));
767 * Change current working directory of
768 * local portion of tip
773 char dirname
[PATH_MAX
];
776 if (prompt("[cd] ", dirname
, sizeof(dirname
))) {
782 printf("%s: bad directory\r\n", cp
);
793 printf("\r\n%s", msg
);
794 printf("\r\n[EOT]\r\n");
796 (void)uu_unlock(uucplock
);
804 char *abortmsg
= NULL
, *dismsg
;
806 if (LO
!= NULL
&& tiplink (LO
, TL_SIGNAL_TIPOUT
) != 0) {
807 abortmsg
= "logout failed";
810 if ((dismsg
= value(DISCONNECT
)) != NULL
) {
811 write(FD
, dismsg
, strlen(dismsg
));
830 if ((cp
= rindex(value(SHELL
), '/')) == NULL
)
835 execl(value(SHELL
), cp
, "-c", s
, NULL
);
839 args(char *buf
, char **a
, int num
)
841 char *p
= buf
, *start
;
845 while (*p
&& n
< num
) {
846 while (*p
&& (*p
== ' ' || *p
== '\t'))
851 while (*p
&& (*p
!= ' ' && *p
!= '\t'))
862 prtime(char *s
, time_t a
)
867 for (i
= 0; i
< 3; i
++) {
868 nums
[i
] = (int)(a
% quant
[i
]);
873 if (nums
[i
] || (i
== 0 && nums
[1] == 0 && nums
[2] == 0))
874 printf("%d %s%c ", nums
[i
], sep
[i
],
875 nums
[i
] == 1 ? '\0' : 's');
884 if (prompt("[set] ", buf
, sizeof(buf
)))
887 if (vtable
[BEAUTIFY
].v_access
&CHANGED
) {
888 vtable
[BEAUTIFY
].v_access
&= ~CHANGED
;
891 if (vtable
[SCRIPT
].v_access
&CHANGED
) {
892 vtable
[SCRIPT
].v_access
&= ~CHANGED
;
895 * So that "set record=blah script" doesn't
896 * cause two transactions to occur.
898 if (vtable
[RECORD
].v_access
&CHANGED
)
899 vtable
[RECORD
].v_access
&= ~CHANGED
;
901 if (vtable
[RECORD
].v_access
&CHANGED
) {
902 vtable
[RECORD
].v_access
&= ~CHANGED
;
903 if (boolean(value(SCRIPT
)))
906 if (vtable
[TAND
].v_access
&CHANGED
) {
907 vtable
[TAND
].v_access
&= ~CHANGED
;
908 if (boolean(value(TAND
)))
913 if (vtable
[LECHO
].v_access
&CHANGED
) {
914 vtable
[LECHO
].v_access
&= ~CHANGED
;
915 HD
= boolean(value(LECHO
));
917 if (vtable
[PARITY
].v_access
&CHANGED
) {
918 vtable
[PARITY
].v_access
&= ~CHANGED
;
919 setparity(value(PARITY
));
924 * Turn tandem mode on or off for remote tty.
930 struct termios ttermios
;
931 tcgetattr (FD
, &ttermios
);
932 if (strcmp(option
,"on") == 0) {
933 ttermios
.c_iflag
|= IXOFF
;
934 ctermios
.c_iflag
|= IXOFF
;
937 ttermios
.c_iflag
&= ~IXOFF
;
938 ctermios
.c_iflag
&= ~IXOFF
;
940 tcsetattr (FD
, TCSANOW
, &ttermios
);
941 tcsetattr (0, TCSANOW
, &ctermios
);
942 #else /* HAVE_TERMIOS */
945 ioctl(FD
, TIOCGETP
, &rmtty
);
946 if (strcmp(option
,"on") == 0) {
947 rmtty
.sg_flags
|= TANDEM
;
948 arg
.sg_flags
|= TANDEM
;
950 rmtty
.sg_flags
&= ~TANDEM
;
951 arg
.sg_flags
&= ~TANDEM
;
953 ioctl(FD
, TIOCSETP
, &rmtty
);
954 ioctl(0, TIOCSETP
, &arg
);
955 #endif /* HAVE_TERMIOS */
965 ioctl(FD
, TIOCSBRK
, NULL
);
967 ioctl(FD
, TIOCCBRK
, NULL
);
978 kill(c
== CTRL('y') ? getpid() : 0, SIGTSTP
);
983 * expand a file name if it includes shell meta characters
989 static char xname
[BUFSIZ
];
993 int s
, pivec
[2] /*, (*sigint)()*/;
995 if (!anyof(name
, "~{[*?$`'\"\\"))
997 /* sigint = signal(SIGINT, SIG_IGN); */
998 if (pipe(pivec
) < 0) {
1000 /* signal(SIGINT, sigint) */
1003 snprintf(cmdbuf
, sizeof(cmdbuf
), "echo %s", name
);
1004 if ((pid
= vfork()) == 0) {
1005 Shell
= value(SHELL
);
1007 Shell
= _PATH_BSHELL
;
1014 execl(Shell
, Shell
, "-c", cmdbuf
, NULL
);
1024 l
= read(pivec
[0], xname
, BUFSIZ
);
1026 while (wait(&s
) != pid
)
1029 if (s
!= 0 && s
!= SIGPIPE
) {
1030 fprintf(stderr
, "\"Echo\" failed\n");
1038 fprintf(stderr
, "\"%s\": No match\n", name
);
1042 fprintf(stderr
, "Buffer overflow expanding \"%s\"\n", name
);
1046 for (cp
= &xname
[l
-1]; *cp
== '\n' && cp
> xname
; cp
--)
1053 * Are any of the characters in the two strings the same?
1057 anyof(char *s1
, char *s2
)