xref: /titanic_51/usr/src/cmd/tip/cmds.c (revision 8d489c7a815fcac696803219572e95aa01532b0f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
5*8d489c7aSmuffin 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate 
12*8d489c7aSmuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include "tip.h"
157c478bd9Sstevel@tonic-gate #ifdef USG
167c478bd9Sstevel@tonic-gate #include <unistd.h>
177c478bd9Sstevel@tonic-gate #else
187c478bd9Sstevel@tonic-gate #include <vfork.h>
197c478bd9Sstevel@tonic-gate #endif
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  * tip
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * miscellaneous commands
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate int	quant[] = { 60, 60, 24 };
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate char	null = '\0';
307c478bd9Sstevel@tonic-gate char	*sep[] = { "second", "minute", "hour" };
317c478bd9Sstevel@tonic-gate static	char *argv[10];		/* argument vector for take and put */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate sigjmp_buf intbuf;		/* for interrupts and timeouts */
347c478bd9Sstevel@tonic-gate 
35*8d489c7aSmuffin void	timeout(void);		/* timeout function called on alarm */
36*8d489c7aSmuffin void	intcopy(void);		/* interrupt routine for file transfers */
37*8d489c7aSmuffin void	transfer(char *, int, char *);
38*8d489c7aSmuffin void	transmit(FILE *, char *, char *);
39*8d489c7aSmuffin void	send(char);
40*8d489c7aSmuffin void	execute(char *);
41*8d489c7aSmuffin void	prtime(char *, time_t);
42*8d489c7aSmuffin void	hardwareflow(char *);
43*8d489c7aSmuffin void	intr(char *);
44*8d489c7aSmuffin int	args(char *, char *[], size_t);
45*8d489c7aSmuffin int	anyof(char *, char *);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * FTP - remote ==> local
497c478bd9Sstevel@tonic-gate  *  get a file from the remote host
507c478bd9Sstevel@tonic-gate  */
51*8d489c7aSmuffin void
52*8d489c7aSmuffin getfl(int c)
537c478bd9Sstevel@tonic-gate {
54*8d489c7aSmuffin 	char buf[256], *cp;
557c478bd9Sstevel@tonic-gate 
56*8d489c7aSmuffin 	(void) putchar(c);
577c478bd9Sstevel@tonic-gate 	/*
587c478bd9Sstevel@tonic-gate 	 * get the UNIX receiving file's name
597c478bd9Sstevel@tonic-gate 	 */
607c478bd9Sstevel@tonic-gate 	if (prompt("Local file name? ", copyname, sizeof (copyname)))
617c478bd9Sstevel@tonic-gate 		return;
627c478bd9Sstevel@tonic-gate 	cp = expand(copyname);
637c478bd9Sstevel@tonic-gate 	if (cp == NOSTR)
647c478bd9Sstevel@tonic-gate 		return;
657c478bd9Sstevel@tonic-gate 	if ((sfd = creat(cp, 0666)) < 0) {
66*8d489c7aSmuffin 		(void) printf("\r\n%s: cannot creat\r\n", copyname);
677c478bd9Sstevel@tonic-gate 		return;
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	/*
717c478bd9Sstevel@tonic-gate 	 * collect parameters
727c478bd9Sstevel@tonic-gate 	 */
737c478bd9Sstevel@tonic-gate 	if (prompt("List command for remote system? ", buf, sizeof (buf))) {
74*8d489c7aSmuffin 		(void) unlink(copyname);
757c478bd9Sstevel@tonic-gate 		return;
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	transfer(buf, sfd, value(EOFREAD));
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Cu-like take command
827c478bd9Sstevel@tonic-gate  */
83*8d489c7aSmuffin /* ARGSUSED */
84*8d489c7aSmuffin void
85*8d489c7aSmuffin cu_take(int cc)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	int fd, argc;
88*8d489c7aSmuffin 	char line[BUFSIZ], *cp;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	if (prompt("[take] ", copyname, sizeof (copyname)))
917c478bd9Sstevel@tonic-gate 		return;
927c478bd9Sstevel@tonic-gate 	argc = args(copyname, argv, sizeof (argv)/sizeof (char *));
937c478bd9Sstevel@tonic-gate 	if (argc < 1 || argc > 2) {
94*8d489c7aSmuffin 		(void) printf("usage: <take> from [to]\r\n");
957c478bd9Sstevel@tonic-gate 		return;
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	if (argc == 1)
987c478bd9Sstevel@tonic-gate 		argv[1] = argv[0];
997c478bd9Sstevel@tonic-gate 	cp = expand(argv[1]);
1007c478bd9Sstevel@tonic-gate 	if (cp == NOSTR)
1017c478bd9Sstevel@tonic-gate 		return;
1027c478bd9Sstevel@tonic-gate 	if ((fd = creat(cp, 0666)) < 0) {
103*8d489c7aSmuffin 		(void) printf("\r\n%s: cannot create\r\n", argv[1]);
1047c478bd9Sstevel@tonic-gate 		return;
1057c478bd9Sstevel@tonic-gate 	}
106*8d489c7aSmuffin 	(void) snprintf(line, sizeof (line), "cat %s; echo \01", argv[0]);
1077c478bd9Sstevel@tonic-gate 	transfer(line, fd, "\01");
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * Bulk transfer routine --
1127c478bd9Sstevel@tonic-gate  *  used by getfl(), cu_take(), and pipefile()
1137c478bd9Sstevel@tonic-gate  */
114*8d489c7aSmuffin void
115*8d489c7aSmuffin transfer(char *buf, int fd, char *eofchars)
1167c478bd9Sstevel@tonic-gate {
117*8d489c7aSmuffin 	int ct;
1187c478bd9Sstevel@tonic-gate 	char c, buffer[BUFSIZ];
1197c478bd9Sstevel@tonic-gate 	char *p = buffer;	/* can't be register because of longjmp */
120*8d489c7aSmuffin 	int cnt, eof, bol;
1217c478bd9Sstevel@tonic-gate 	time_t start;
122*8d489c7aSmuffin 	sig_handler_t	f;
1237c478bd9Sstevel@tonic-gate 
124*8d489c7aSmuffin 	parwrite(FD, (unsigned char *)buf, strlen(buf));
125*8d489c7aSmuffin 	(void) kill(pid, SIGIOT);
126*8d489c7aSmuffin 	/* Wait until read process stops */
127*8d489c7aSmuffin 	(void) read(repdes[0], (char *)&ccc, 1);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * finish command
1317c478bd9Sstevel@tonic-gate 	 */
132*8d489c7aSmuffin 	parwrite(FD, (unsigned char *)"\r", 1);
1337c478bd9Sstevel@tonic-gate 	do
134*8d489c7aSmuffin 		(void) read(FD, &c, 1);
135*8d489c7aSmuffin 	while ((c&0177) != '\n')
136*8d489c7aSmuffin 		;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (sigsetjmp(intbuf, 1))
1397c478bd9Sstevel@tonic-gate 		goto out;
140*8d489c7aSmuffin 	f = signal(SIGINT, (sig_handler_t)intcopy);
1417c478bd9Sstevel@tonic-gate 	intr("on");
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	start = time(0);
1447c478bd9Sstevel@tonic-gate 	bol = 1;
1457c478bd9Sstevel@tonic-gate 	ct = 0;
1467c478bd9Sstevel@tonic-gate 	for (;;) {
1477c478bd9Sstevel@tonic-gate 		eof = read(FD, &c, 1) <= 0;
1487c478bd9Sstevel@tonic-gate 		if (noparity)
1497c478bd9Sstevel@tonic-gate 			c &= 0377;
1507c478bd9Sstevel@tonic-gate 		else
1517c478bd9Sstevel@tonic-gate 			c &= 0177;
1527c478bd9Sstevel@tonic-gate 		if (eof || (bol && any(c, eofchars)))
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		if (c == 0)
1557c478bd9Sstevel@tonic-gate 			continue;	/* ignore nulls */
1567c478bd9Sstevel@tonic-gate 		if (c == '\r')
1577c478bd9Sstevel@tonic-gate 			continue;
1587c478bd9Sstevel@tonic-gate 		*p++ = c;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		if (c == '\n') {
1617c478bd9Sstevel@tonic-gate 			bol = 1;
1627c478bd9Sstevel@tonic-gate 			if (boolean(value(VERBOSE)))
163*8d489c7aSmuffin 				(void) printf("\r%d", ++ct);
1647c478bd9Sstevel@tonic-gate 		} else
1657c478bd9Sstevel@tonic-gate 			bol = 0;
1667c478bd9Sstevel@tonic-gate 		if ((cnt = (p-buffer)) == number(value(FRAMESIZE))) {
1677c478bd9Sstevel@tonic-gate 			if (write(fd, buffer, cnt) != cnt) {
168*8d489c7aSmuffin 				(void) printf("\r\nwrite error\r\n");
1697c478bd9Sstevel@tonic-gate 				goto out;
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 			p = buffer;
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate out:
175*8d489c7aSmuffin 	if ((cnt = (p-buffer)) != 0)
1767c478bd9Sstevel@tonic-gate 		if (write(fd, buffer, cnt) != cnt)
177*8d489c7aSmuffin 			(void) printf("\r\nwrite error\r\n");
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
1807c478bd9Sstevel@tonic-gate 		prtime(" lines transferred in ", time(0)-start);
1817c478bd9Sstevel@tonic-gate 	intr("off");
182*8d489c7aSmuffin 	(void) write(fildes[1], (char *)&ccc, 1);
183*8d489c7aSmuffin 	(void) signal(SIGINT, f);
184*8d489c7aSmuffin 	(void) close(fd);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * FTP - remote ==> local process
1897c478bd9Sstevel@tonic-gate  *   send remote input to local process via pipe
1907c478bd9Sstevel@tonic-gate  */
191*8d489c7aSmuffin /* ARGSUSED */
192*8d489c7aSmuffin void
193*8d489c7aSmuffin pipefile(int cc)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	int cpid, pdes[2];
1967c478bd9Sstevel@tonic-gate 	char buf[256];
1977c478bd9Sstevel@tonic-gate 	int status, p;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	if (prompt("Local command? ", buf, sizeof (buf)))
2007c478bd9Sstevel@tonic-gate 		return;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (pipe(pdes)) {
203*8d489c7aSmuffin 		(void) printf("can't establish pipe\r\n");
2047c478bd9Sstevel@tonic-gate 		return;
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if ((cpid = fork()) < 0) {
208*8d489c7aSmuffin 		(void) printf("can't fork!\r\n");
2097c478bd9Sstevel@tonic-gate 		return;
2107c478bd9Sstevel@tonic-gate 	} else if (cpid) {
2117c478bd9Sstevel@tonic-gate 		if (prompt("List command for remote system? ", buf,
2127c478bd9Sstevel@tonic-gate 		    sizeof (buf))) {
213*8d489c7aSmuffin 			(void) close(pdes[0]), (void) close(pdes[1]);
214*8d489c7aSmuffin 			(void) kill(cpid, SIGKILL);
2157c478bd9Sstevel@tonic-gate 		} else {
216*8d489c7aSmuffin 			(void) close(pdes[0]);
217*8d489c7aSmuffin 			(void) signal(SIGPIPE, (sig_handler_t)intcopy);
2187c478bd9Sstevel@tonic-gate 			transfer(buf, pdes[1], value(EOFREAD));
219*8d489c7aSmuffin 			(void) signal(SIGPIPE, SIG_DFL);
2207c478bd9Sstevel@tonic-gate 			while ((p = wait(&status)) > 0 && p != cpid)
2217c478bd9Sstevel@tonic-gate 				;
2227c478bd9Sstevel@tonic-gate 		}
2237c478bd9Sstevel@tonic-gate 	} else {
224*8d489c7aSmuffin 		int f;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		userperm();
227*8d489c7aSmuffin 		(void) dup2(pdes[0], 0);
228*8d489c7aSmuffin 		(void) close(pdes[0]);
2297c478bd9Sstevel@tonic-gate 		for (f = 3; f < 20; f++)
230*8d489c7aSmuffin 			(void) close(f);
2317c478bd9Sstevel@tonic-gate 		execute(buf);
232*8d489c7aSmuffin 		(void) printf("can't execl!\r\n");
2337c478bd9Sstevel@tonic-gate 		exit(0);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * FTP - local ==> remote
2397c478bd9Sstevel@tonic-gate  *  send local file to remote host
2407c478bd9Sstevel@tonic-gate  *  terminate transmission with pseudo EOF sequence
2417c478bd9Sstevel@tonic-gate  */
242*8d489c7aSmuffin void
243*8d489c7aSmuffin tip_sendfile(int cc)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	FILE *fd;
2467c478bd9Sstevel@tonic-gate 	char *fnamex;
2477c478bd9Sstevel@tonic-gate 
248*8d489c7aSmuffin 	(void) putchar(cc);
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * get file name
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	if (prompt("Local file name? ", fname, sizeof (fname)))
2537c478bd9Sstevel@tonic-gate 		return;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * look up file
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	fnamex = expand(fname);
2597c478bd9Sstevel@tonic-gate 	if (fnamex == NOSTR)
2607c478bd9Sstevel@tonic-gate 		return;
2617c478bd9Sstevel@tonic-gate 	if ((fd = fopen(fnamex, "r")) == NULL) {
262*8d489c7aSmuffin 		(void) printf("%s: cannot open\r\n", fname);
2637c478bd9Sstevel@tonic-gate 		return;
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 	transmit(fd, value(EOFWRITE), NULL);
2667c478bd9Sstevel@tonic-gate 	if (!boolean(value(ECHOCHECK))) {
2677c478bd9Sstevel@tonic-gate 		struct termios buf;
2687c478bd9Sstevel@tonic-gate 
269*8d489c7aSmuffin 		(void) ioctl(FD, TCGETS, (char *)&buf);	/* this does a */
270*8d489c7aSmuffin 		(void) ioctl(FD, TCSETSF, (char *)&buf);	/* wflushtty */
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * Bulk transfer routine to remote host --
276*8d489c7aSmuffin  *   used by tip_sendfile() and cu_put()
2777c478bd9Sstevel@tonic-gate  */
278*8d489c7aSmuffin void
279*8d489c7aSmuffin transmit(FILE *fd, char *eofchars, char *command)
2807c478bd9Sstevel@tonic-gate {
281*8d489c7aSmuffin 	sig_handler_t	ointr;
2827c478bd9Sstevel@tonic-gate 	char *pc, lastc, rc;
2837c478bd9Sstevel@tonic-gate 	int c, ccount, lcount;
2847c478bd9Sstevel@tonic-gate 	time_t start_t, stop_t;
2857c478bd9Sstevel@tonic-gate 
286*8d489c7aSmuffin 	(void) kill(pid, SIGIOT);	/* put TIPOUT into a wait state */
2877c478bd9Sstevel@tonic-gate 	timedout = 0;
2887c478bd9Sstevel@tonic-gate 	if (sigsetjmp(intbuf, 1)) {
2897c478bd9Sstevel@tonic-gate 		if (timedout)
290*8d489c7aSmuffin 			(void) printf("\r\ntimed out at eol\r\n");
291*8d489c7aSmuffin 		(void) alarm(0);
2927c478bd9Sstevel@tonic-gate 		goto out;
2937c478bd9Sstevel@tonic-gate 	}
294*8d489c7aSmuffin 	ointr = signal(SIGINT, (sig_handler_t)intcopy);
2957c478bd9Sstevel@tonic-gate 	intr("on");
296*8d489c7aSmuffin 	(void) read(repdes[0], (char *)&ccc, 1);
2977c478bd9Sstevel@tonic-gate 	if (command != NULL) {
2987c478bd9Sstevel@tonic-gate 		for (pc = command; *pc; pc++)
2997c478bd9Sstevel@tonic-gate 			send(*pc);
3007c478bd9Sstevel@tonic-gate 		if (boolean(value(ECHOCHECK)))
301*8d489c7aSmuffin 			(void) read(FD, (char *)&c, 1);	/* trailing \n */
3027c478bd9Sstevel@tonic-gate 		else {
3037c478bd9Sstevel@tonic-gate 			struct termios buf;
304*8d489c7aSmuffin 			/* wait for remote stty to take effect */
305*8d489c7aSmuffin 			(void) sleep(5);
306*8d489c7aSmuffin 			/* this does a */
307*8d489c7aSmuffin 			(void) ioctl(FD, TCGETS, (char *)&buf);
308*8d489c7aSmuffin 			/* wflushtty */
309*8d489c7aSmuffin 			(void) ioctl(FD, TCSETSF, (char *)&buf);
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	lcount = 0;
3137c478bd9Sstevel@tonic-gate 	lastc = '\0';
3147c478bd9Sstevel@tonic-gate 	start_t = time(0);
3157c478bd9Sstevel@tonic-gate 	if (boolean(value(RAWFTP))) {
3167c478bd9Sstevel@tonic-gate 		while ((c = getc(fd)) != EOF) {
3177c478bd9Sstevel@tonic-gate 			lcount++;
3187c478bd9Sstevel@tonic-gate 			send(c);
3197c478bd9Sstevel@tonic-gate 			if (boolean(value(VERBOSE)) && lcount%100 == 0)
320*8d489c7aSmuffin 				(void) printf("\r%d", lcount);
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 		if (boolean(value(VERBOSE)))
323*8d489c7aSmuffin 			(void) printf("\r%d", lcount);
3247c478bd9Sstevel@tonic-gate 		goto out;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	for (;;) {
3277c478bd9Sstevel@tonic-gate 		ccount = 0;
3287c478bd9Sstevel@tonic-gate 		do {
3297c478bd9Sstevel@tonic-gate 			c = getc(fd);
3307c478bd9Sstevel@tonic-gate 			if (c == EOF)
3317c478bd9Sstevel@tonic-gate 				goto out;
3327c478bd9Sstevel@tonic-gate 			if (c == 0177)
3337c478bd9Sstevel@tonic-gate 				continue;
3347c478bd9Sstevel@tonic-gate 			lastc = c;
3357c478bd9Sstevel@tonic-gate 			if (c < 040) {
3367c478bd9Sstevel@tonic-gate 				if (c == '\n') {
3377c478bd9Sstevel@tonic-gate 					c = '\r';
3387c478bd9Sstevel@tonic-gate 				} else if (c == '\t') {
3397c478bd9Sstevel@tonic-gate 					if (boolean(value(TABEXPAND))) {
3407c478bd9Sstevel@tonic-gate 						send(' ');
3417c478bd9Sstevel@tonic-gate 						while ((++ccount % 8) != 0)
3427c478bd9Sstevel@tonic-gate 							send(' ');
3437c478bd9Sstevel@tonic-gate 						continue;
3447c478bd9Sstevel@tonic-gate 					}
3457c478bd9Sstevel@tonic-gate 				} else
3467c478bd9Sstevel@tonic-gate 					continue;
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 			send(c);
3497c478bd9Sstevel@tonic-gate 		} while (c != '\r');
3507c478bd9Sstevel@tonic-gate 		if (boolean(value(VERBOSE)))
351*8d489c7aSmuffin 			(void) printf("\r%d", ++lcount);
3527c478bd9Sstevel@tonic-gate 		if (boolean(value(ECHOCHECK))) {
353*8d489c7aSmuffin 			(void) alarm(number(value(ETIMEOUT)));
3547c478bd9Sstevel@tonic-gate 			do {	/* wait for prompt */
355*8d489c7aSmuffin 				(void) read(FD, &rc, 1);
3567c478bd9Sstevel@tonic-gate 			} while ((rc&0177) != character(value(PROMPT)));
357*8d489c7aSmuffin 			(void) alarm(0);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate out:
3617c478bd9Sstevel@tonic-gate 	if (lastc != '\n' && !boolean(value(RAWFTP)))
3627c478bd9Sstevel@tonic-gate 		send('\r');
3637c478bd9Sstevel@tonic-gate 	if (eofchars)
3647c478bd9Sstevel@tonic-gate 		for (pc = eofchars; *pc; pc++)
3657c478bd9Sstevel@tonic-gate 			send(*pc);
3667c478bd9Sstevel@tonic-gate 	stop_t = time(0);
367*8d489c7aSmuffin 	(void) fclose(fd);
3687c478bd9Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
3697c478bd9Sstevel@tonic-gate 		if (boolean(value(RAWFTP)))
3707c478bd9Sstevel@tonic-gate 			prtime(" chars transferred in ", stop_t-start_t);
3717c478bd9Sstevel@tonic-gate 		else
3727c478bd9Sstevel@tonic-gate 			prtime(" lines transferred in ", stop_t-start_t);
373*8d489c7aSmuffin 	(void) write(fildes[1], (char *)&ccc, 1);
3747c478bd9Sstevel@tonic-gate 	intr("off");
375*8d489c7aSmuffin 	(void) signal(SIGINT, ointr);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate  * Cu-like put command
3807c478bd9Sstevel@tonic-gate  */
381*8d489c7aSmuffin /* ARGSUSED */
382*8d489c7aSmuffin void
383*8d489c7aSmuffin cu_put(int cc)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate 	FILE *fd;
3867c478bd9Sstevel@tonic-gate 	char line[BUFSIZ];
3877c478bd9Sstevel@tonic-gate 	int argc;
3887c478bd9Sstevel@tonic-gate 	char *copynamex;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (prompt("[put] ", copyname, sizeof (copyname)))
3917c478bd9Sstevel@tonic-gate 		return;
3927c478bd9Sstevel@tonic-gate 	argc = args(copyname, argv, sizeof (argv)/sizeof (char *));
3937c478bd9Sstevel@tonic-gate 	if (argc < 1 || argc > 2) {
394*8d489c7aSmuffin 		(void) printf("usage: <put> from [to]\r\n");
3957c478bd9Sstevel@tonic-gate 		return;
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 	if (argc == 1)
3987c478bd9Sstevel@tonic-gate 		argv[1] = argv[0];
3997c478bd9Sstevel@tonic-gate 	copynamex = expand(argv[0]);
4007c478bd9Sstevel@tonic-gate 	if (copynamex == NOSTR)
4017c478bd9Sstevel@tonic-gate 		return;
4027c478bd9Sstevel@tonic-gate 	if ((fd = fopen(copynamex, "r")) == NULL) {
403*8d489c7aSmuffin 		(void) printf("%s: cannot open\r\n", copynamex);
4047c478bd9Sstevel@tonic-gate 		return;
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 	if (boolean(value(ECHOCHECK)))
407*8d489c7aSmuffin 		(void) snprintf(line, sizeof (line), "cat>%s\r", argv[1]);
4087c478bd9Sstevel@tonic-gate 	else
409*8d489c7aSmuffin 		(void) snprintf(line, sizeof (line),
410*8d489c7aSmuffin 		    "stty -echo; cat>%s; stty echo\r", argv[1]);
4117c478bd9Sstevel@tonic-gate 	transmit(fd, "\04", line);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * FTP - send single character
4167c478bd9Sstevel@tonic-gate  *  wait for echo & handle timeout
4177c478bd9Sstevel@tonic-gate  */
418*8d489c7aSmuffin void
419*8d489c7aSmuffin send(char c)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	char cc;
4227c478bd9Sstevel@tonic-gate 	int retry = 0;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	cc = c;
425*8d489c7aSmuffin 	parwrite(FD, (unsigned char *)&cc, 1);
4267c478bd9Sstevel@tonic-gate #ifdef notdef
4277c478bd9Sstevel@tonic-gate 	if (number(value(CDELAY)) > 0 && c != '\r')
4287c478bd9Sstevel@tonic-gate 		nap(number(value(CDELAY)));
4297c478bd9Sstevel@tonic-gate #endif
4307c478bd9Sstevel@tonic-gate 	if (!boolean(value(ECHOCHECK))) {
4317c478bd9Sstevel@tonic-gate #ifdef notdef
4327c478bd9Sstevel@tonic-gate 		if (number(value(LDELAY)) > 0 && c == '\r')
4337c478bd9Sstevel@tonic-gate 			nap(number(value(LDELAY)));
4347c478bd9Sstevel@tonic-gate #endif
4357c478bd9Sstevel@tonic-gate 		return;
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate tryagain:
4387c478bd9Sstevel@tonic-gate 	timedout = 0;
4397c478bd9Sstevel@tonic-gate 	if (sigsetjmp(intbuf, 1) && timedout) {
440*8d489c7aSmuffin 		(void) printf("\r\ntimeout error (%s)\r\n", ctrl(c));
4417c478bd9Sstevel@tonic-gate 		if (retry++ > 3)
4427c478bd9Sstevel@tonic-gate 			return;
443*8d489c7aSmuffin 		parwrite(FD, (unsigned char *)&null, 1); /* poke it */
4447c478bd9Sstevel@tonic-gate 		goto tryagain;
4457c478bd9Sstevel@tonic-gate 	}
446*8d489c7aSmuffin 	(void) alarm(number(value(ETIMEOUT)));
447*8d489c7aSmuffin 	(void) read(FD, &cc, 1);
448*8d489c7aSmuffin 	(void) alarm(0);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate void
452*8d489c7aSmuffin timeout(void)
4537c478bd9Sstevel@tonic-gate {
454*8d489c7aSmuffin 	(void) signal(SIGALRM, (sig_handler_t)timeout);
4557c478bd9Sstevel@tonic-gate 	timedout = 1;
4567c478bd9Sstevel@tonic-gate 	siglongjmp(intbuf, 1);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate  * Stolen from consh() -- puts a remote file on the output of a local command.
4617c478bd9Sstevel@tonic-gate  *	Identical to consh() except for where stdout goes.
4627c478bd9Sstevel@tonic-gate  */
463*8d489c7aSmuffin void
464*8d489c7aSmuffin pipeout(int c)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	char buf[256];
4677c478bd9Sstevel@tonic-gate 	int cpid, status, p;
4687c478bd9Sstevel@tonic-gate 	time_t start;
4697c478bd9Sstevel@tonic-gate 
470*8d489c7aSmuffin 	(void) putchar(c);
4717c478bd9Sstevel@tonic-gate 	if (prompt("Local command? ", buf, sizeof (buf)))
4727c478bd9Sstevel@tonic-gate 		return;
473*8d489c7aSmuffin 	(void) kill(pid, SIGIOT);	/* put TIPOUT into a wait state */
474*8d489c7aSmuffin 	(void) signal(SIGINT, SIG_IGN);
475*8d489c7aSmuffin 	(void) signal(SIGQUIT, SIG_IGN);
4767c478bd9Sstevel@tonic-gate 	intr("on");
477*8d489c7aSmuffin 	(void) read(repdes[0], (char *)&ccc, 1);
4787c478bd9Sstevel@tonic-gate 	/*
4797c478bd9Sstevel@tonic-gate 	 * Set up file descriptors in the child and
4807c478bd9Sstevel@tonic-gate 	 *  let it go...
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 	if ((cpid = fork()) < 0)
483*8d489c7aSmuffin 		(void) printf("can't fork!\r\n");
4847c478bd9Sstevel@tonic-gate 	else if (cpid) {
4857c478bd9Sstevel@tonic-gate 		start = time(0);
4867c478bd9Sstevel@tonic-gate 		while ((p = wait(&status)) > 0 && p != cpid)
4877c478bd9Sstevel@tonic-gate 			;
4887c478bd9Sstevel@tonic-gate 	} else {
489*8d489c7aSmuffin 		int i;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		userperm();
492*8d489c7aSmuffin 		(void) dup2(FD, 1);
4937c478bd9Sstevel@tonic-gate 		for (i = 3; i < 20; i++)
494*8d489c7aSmuffin 			(void) close(i);
495*8d489c7aSmuffin 		(void) signal(SIGINT, SIG_DFL);
496*8d489c7aSmuffin 		(void) signal(SIGQUIT, SIG_DFL);
4977c478bd9Sstevel@tonic-gate 		execute(buf);
498*8d489c7aSmuffin 		(void) printf("can't find `%s'\r\n", buf);
4997c478bd9Sstevel@tonic-gate 		exit(0);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
5027c478bd9Sstevel@tonic-gate 		prtime("away for ", time(0)-start);
503*8d489c7aSmuffin 	(void) write(fildes[1], (char *)&ccc, 1);
5047c478bd9Sstevel@tonic-gate 	intr("off");
505*8d489c7aSmuffin 	(void) signal(SIGINT, SIG_DFL);
506*8d489c7aSmuffin 	(void) signal(SIGQUIT, SIG_DFL);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate  * Fork a program with:
5117c478bd9Sstevel@tonic-gate  *  0 <-> remote tty in
5127c478bd9Sstevel@tonic-gate  *  1 <-> remote tty out
5137c478bd9Sstevel@tonic-gate  *  2 <-> local tty stderr out
5147c478bd9Sstevel@tonic-gate  */
515*8d489c7aSmuffin void
516*8d489c7aSmuffin consh(int c)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	char buf[256];
5197c478bd9Sstevel@tonic-gate 	int cpid, status, p;
520*8d489c7aSmuffin 	sig_handler_t	ointr, oquit;
5217c478bd9Sstevel@tonic-gate 	time_t start;
5227c478bd9Sstevel@tonic-gate 
523*8d489c7aSmuffin 	(void) putchar(c);
5247c478bd9Sstevel@tonic-gate 	if (prompt("Local command? ", buf, sizeof (buf)))
5257c478bd9Sstevel@tonic-gate 		return;
526*8d489c7aSmuffin 	(void) kill(pid, SIGIOT);	/* put TIPOUT into a wait state */
527*8d489c7aSmuffin 	(void) read(repdes[0], (char *)&ccc, 1);
5287c478bd9Sstevel@tonic-gate 	ointr = signal(SIGINT, SIG_IGN);
5297c478bd9Sstevel@tonic-gate 	oquit = signal(SIGQUIT, SIG_IGN);
5307c478bd9Sstevel@tonic-gate 	unraw();
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * Set up file descriptors in the child and
5337c478bd9Sstevel@tonic-gate 	 *  let it go...
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	if ((cpid = fork()) < 0)
536*8d489c7aSmuffin 		(void) printf("can't fork!\r\n");
5377c478bd9Sstevel@tonic-gate 	else if (cpid) {
5387c478bd9Sstevel@tonic-gate 		start = time(0);
5397c478bd9Sstevel@tonic-gate 		while ((p = wait(&status)) > 0 && p != cpid)
5407c478bd9Sstevel@tonic-gate 			;
5417c478bd9Sstevel@tonic-gate 		raw();
542*8d489c7aSmuffin 		(void) signal(SIGINT, ointr);
543*8d489c7aSmuffin 		(void) signal(SIGQUIT, oquit);
5447c478bd9Sstevel@tonic-gate 	} else {
545*8d489c7aSmuffin 		int i;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		userperm();
548*8d489c7aSmuffin 		(void) dup2(FD, 0);
549*8d489c7aSmuffin 		(void) dup2(0, 1);
5507c478bd9Sstevel@tonic-gate 		for (i = 3; i < 20; i++)
551*8d489c7aSmuffin 			(void) close(i);
552*8d489c7aSmuffin 		(void) signal(SIGINT, SIG_DFL);
553*8d489c7aSmuffin 		(void) signal(SIGQUIT, SIG_DFL);
5547c478bd9Sstevel@tonic-gate 		execute(buf);
555*8d489c7aSmuffin 		(void) printf("can't find `%s'\r\n", buf);
5567c478bd9Sstevel@tonic-gate 		exit(0);
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
5597c478bd9Sstevel@tonic-gate 		prtime("\r\naway for ", time(0)-start);
560*8d489c7aSmuffin 	(void) write(fildes[1], (char *)&ccc, 1);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate  * Escape to local shell
5657c478bd9Sstevel@tonic-gate  */
566*8d489c7aSmuffin /* ARGSUSED */
567*8d489c7aSmuffin void
568*8d489c7aSmuffin shell(int cc)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	int shpid, status;
571*8d489c7aSmuffin 	sig_handler_t	ointr, oquit;
5727c478bd9Sstevel@tonic-gate 	char *cp;
5737c478bd9Sstevel@tonic-gate 
574*8d489c7aSmuffin 	(void) printf("[sh]\r\n");
5757c478bd9Sstevel@tonic-gate 	ointr = signal(SIGINT, SIG_IGN);
5767c478bd9Sstevel@tonic-gate 	oquit = signal(SIGQUIT, SIG_IGN);
5777c478bd9Sstevel@tonic-gate 	unraw();
5787c478bd9Sstevel@tonic-gate 	if (shpid = fork()) {
5797c478bd9Sstevel@tonic-gate 		while (shpid != wait(&status))
5807c478bd9Sstevel@tonic-gate 			;
5817c478bd9Sstevel@tonic-gate 		raw();
582*8d489c7aSmuffin 		(void) printf("\r\n!\r\n");
583*8d489c7aSmuffin 		(void) signal(SIGINT, ointr);
584*8d489c7aSmuffin 		(void) signal(SIGQUIT, oquit);
5857c478bd9Sstevel@tonic-gate 	} else {
5867c478bd9Sstevel@tonic-gate 		userperm();
587*8d489c7aSmuffin 		(void) signal(SIGQUIT, SIG_DFL);
588*8d489c7aSmuffin 		(void) signal(SIGINT, SIG_DFL);
5897c478bd9Sstevel@tonic-gate 		if ((cp = strrchr(value(SHELL), '/')) == NULL)
5907c478bd9Sstevel@tonic-gate 			cp = value(SHELL);
5917c478bd9Sstevel@tonic-gate 		else
5927c478bd9Sstevel@tonic-gate 			cp++;
593*8d489c7aSmuffin 		(void) execl(value(SHELL), cp, 0);
594*8d489c7aSmuffin 		(void) printf("\r\ncan't execl!\r\n");
5957c478bd9Sstevel@tonic-gate 		exit(1);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate /*
6007c478bd9Sstevel@tonic-gate  * TIPIN portion of scripting
6017c478bd9Sstevel@tonic-gate  *   initiate the conversation with TIPOUT
6027c478bd9Sstevel@tonic-gate  */
603*8d489c7aSmuffin void
604*8d489c7aSmuffin setscript(void)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	char c;
6077c478bd9Sstevel@tonic-gate 	/*
6087c478bd9Sstevel@tonic-gate 	 * enable TIPOUT side for dialogue
6097c478bd9Sstevel@tonic-gate 	 */
610*8d489c7aSmuffin 	(void) kill(pid, SIGEMT);
6117c478bd9Sstevel@tonic-gate 	if (boolean(value(SCRIPT)))
612*8d489c7aSmuffin 		(void) write(fildes[1], value(RECORD), strlen(value(RECORD)));
613*8d489c7aSmuffin 	(void) write(fildes[1], "\n", 1);
6147c478bd9Sstevel@tonic-gate 	/*
6157c478bd9Sstevel@tonic-gate 	 * wait for TIPOUT to finish
6167c478bd9Sstevel@tonic-gate 	 */
617*8d489c7aSmuffin 	(void) read(repdes[0], &c, 1);
6187c478bd9Sstevel@tonic-gate 	if (c == 'n')
619*8d489c7aSmuffin 		(void) fprintf(stderr, "tip: can't create record file %s\r\n",
6207c478bd9Sstevel@tonic-gate 		    value(RECORD));
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate  * Change current working directory of
6257c478bd9Sstevel@tonic-gate  *   local portion of tip
6267c478bd9Sstevel@tonic-gate  */
627*8d489c7aSmuffin /* ARGSUSED */
628*8d489c7aSmuffin void
629*8d489c7aSmuffin chdirectory(int cc)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	char dirname[80];
632*8d489c7aSmuffin 	char *cp = dirname;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if (prompt("[cd] ", dirname, sizeof (dirname))) {
6357c478bd9Sstevel@tonic-gate 		if (stoprompt)
6367c478bd9Sstevel@tonic-gate 			return;
6377c478bd9Sstevel@tonic-gate 		cp = value(HOME);
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 	if (chdir(cp) < 0)
640*8d489c7aSmuffin 		(void) printf("%s: bad directory\r\n", cp);
641*8d489c7aSmuffin 	(void) printf("!\r\n");
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
644*8d489c7aSmuffin void
645*8d489c7aSmuffin tip_abort(char *msg)
6467c478bd9Sstevel@tonic-gate {
647*8d489c7aSmuffin 	/* don't want to hear about our child */
648*8d489c7aSmuffin 	(void) signal(SIGCHLD, SIG_DFL);
649*8d489c7aSmuffin 	(void) kill(pid, SIGTERM);
6507c478bd9Sstevel@tonic-gate 	myperm();
6517c478bd9Sstevel@tonic-gate 	disconnect(msg);
6527c478bd9Sstevel@tonic-gate 	if (msg != NOSTR)
653*8d489c7aSmuffin 		(void) printf("\r\n%s", msg);
654*8d489c7aSmuffin 	(void) printf("\r\n[EOT]\r\n");
6557c478bd9Sstevel@tonic-gate 	delock(uucplock);
6567c478bd9Sstevel@tonic-gate 	unraw();
6577c478bd9Sstevel@tonic-gate 	exit(0);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
660*8d489c7aSmuffin /* ARGSUSED */
661*8d489c7aSmuffin void
662*8d489c7aSmuffin finish(int cc)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	char *dismsg;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if ((dismsg = value(DISCONNECT)) != NOSTR) {
667*8d489c7aSmuffin 		(void) write(FD, dismsg, strlen(dismsg));
668*8d489c7aSmuffin 		(void) sleep(5);
6697c478bd9Sstevel@tonic-gate 	}
670*8d489c7aSmuffin 	tip_abort(NOSTR);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate void
674*8d489c7aSmuffin intcopy(void)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 
677*8d489c7aSmuffin 	(void) signal(SIGINT, SIG_IGN);
6787c478bd9Sstevel@tonic-gate 	siglongjmp(intbuf, 1);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
681*8d489c7aSmuffin void
682*8d489c7aSmuffin execute(char *s)
6837c478bd9Sstevel@tonic-gate {
684*8d489c7aSmuffin 	char *cp;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if ((cp = strrchr(value(SHELL), '/')) == NULL)
6877c478bd9Sstevel@tonic-gate 		cp = value(SHELL);
6887c478bd9Sstevel@tonic-gate 	else
6897c478bd9Sstevel@tonic-gate 		cp++;
690*8d489c7aSmuffin 	(void) execl(value(SHELL), cp, "-c", s, 0);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate 
693*8d489c7aSmuffin int
694*8d489c7aSmuffin args(char *buf, char *a[], size_t na)
6957c478bd9Sstevel@tonic-gate {
696*8d489c7aSmuffin 	char *p = buf, *start;
697*8d489c7aSmuffin 	char **parg = a;
698*8d489c7aSmuffin 	int n = 0;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	do {
7017c478bd9Sstevel@tonic-gate 		while (*p && (*p == ' ' || *p == '\t'))
7027c478bd9Sstevel@tonic-gate 			p++;
7037c478bd9Sstevel@tonic-gate 		start = p;
7047c478bd9Sstevel@tonic-gate 		if (*p)
7057c478bd9Sstevel@tonic-gate 			*parg = p;
7067c478bd9Sstevel@tonic-gate 		while (*p && (*p != ' ' && *p != '\t'))
7077c478bd9Sstevel@tonic-gate 			p++;
7087c478bd9Sstevel@tonic-gate 		if (p != start)
7097c478bd9Sstevel@tonic-gate 			parg++, n++;
7107c478bd9Sstevel@tonic-gate 		if (*p)
7117c478bd9Sstevel@tonic-gate 			*p++ = '\0';
7127c478bd9Sstevel@tonic-gate 	} while (*p && n < na);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	return (n);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
717*8d489c7aSmuffin void
718*8d489c7aSmuffin prtime(char *s, time_t a)
7197c478bd9Sstevel@tonic-gate {
720*8d489c7aSmuffin 	int i;
7217c478bd9Sstevel@tonic-gate 	int nums[3];
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
7247c478bd9Sstevel@tonic-gate 		nums[i] = (int)(a % quant[i]);
7257c478bd9Sstevel@tonic-gate 		a /= quant[i];
7267c478bd9Sstevel@tonic-gate 	}
727*8d489c7aSmuffin 	(void) printf("%s", s);
7287c478bd9Sstevel@tonic-gate 	while (--i >= 0)
7297c478bd9Sstevel@tonic-gate 		if (nums[i] || i == 0 && nums[1] == 0 && nums[2] == 0)
730*8d489c7aSmuffin 			(void) printf("%d %s%c ", nums[i], sep[i],
7317c478bd9Sstevel@tonic-gate 			    nums[i] == 1 ? '\0' : 's');
732*8d489c7aSmuffin 	(void) printf("\r\n!\r\n");
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
735*8d489c7aSmuffin /* ARGSUSED */
736*8d489c7aSmuffin void
737*8d489c7aSmuffin variable(int cc)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	char	buf[256];
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (prompt("[set] ", buf, sizeof (buf)))
7427c478bd9Sstevel@tonic-gate 		return;
7437c478bd9Sstevel@tonic-gate 	vlex(buf);
7447c478bd9Sstevel@tonic-gate 	if (vtable[BEAUTIFY].v_access&CHANGED) {
7457c478bd9Sstevel@tonic-gate 		vtable[BEAUTIFY].v_access &= ~CHANGED;
746*8d489c7aSmuffin 		(void) kill(pid, SIGSYS);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	if (vtable[SCRIPT].v_access&CHANGED) {
7497c478bd9Sstevel@tonic-gate 		vtable[SCRIPT].v_access &= ~CHANGED;
7507c478bd9Sstevel@tonic-gate 		setscript();
7517c478bd9Sstevel@tonic-gate 		/*
7527c478bd9Sstevel@tonic-gate 		 * So that "set record=blah script" doesn't
7537c478bd9Sstevel@tonic-gate 		 *  cause two transactions to occur.
7547c478bd9Sstevel@tonic-gate 		 */
7557c478bd9Sstevel@tonic-gate 		if (vtable[RECORD].v_access&CHANGED)
7567c478bd9Sstevel@tonic-gate 			vtable[RECORD].v_access &= ~CHANGED;
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 	if (vtable[RECORD].v_access&CHANGED) {
7597c478bd9Sstevel@tonic-gate 		vtable[RECORD].v_access &= ~CHANGED;
7607c478bd9Sstevel@tonic-gate 		if (boolean(value(SCRIPT)))
7617c478bd9Sstevel@tonic-gate 			setscript();
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 	if (vtable[TAND].v_access&CHANGED) {
7647c478bd9Sstevel@tonic-gate 		vtable[TAND].v_access &= ~CHANGED;
7657c478bd9Sstevel@tonic-gate 		if (boolean(value(TAND)))
7667c478bd9Sstevel@tonic-gate 			tandem("on");
7677c478bd9Sstevel@tonic-gate 		else
7687c478bd9Sstevel@tonic-gate 			tandem("off");
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 	if (vtable[LECHO].v_access&CHANGED) {
7717c478bd9Sstevel@tonic-gate 		vtable[LECHO].v_access &= ~CHANGED;
7727c478bd9Sstevel@tonic-gate 		boolean(value(HALFDUPLEX)) = boolean(value(LECHO));
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 	if (vtable[PARITY].v_access&CHANGED) {
7757c478bd9Sstevel@tonic-gate 		vtable[PARITY].v_access &= ~CHANGED;
7767c478bd9Sstevel@tonic-gate 		setparity(NULL);
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 	if (vtable[BAUDRATE].v_access&CHANGED) {
7797c478bd9Sstevel@tonic-gate 		vtable[BAUDRATE].v_access &= ~CHANGED;
7807c478bd9Sstevel@tonic-gate 		ttysetup(speed(number(value(BAUDRATE))));
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	if (vtable[HARDWAREFLOW].v_access & CHANGED) {
7837c478bd9Sstevel@tonic-gate 		vtable[HARDWAREFLOW].v_access &= ~CHANGED;
7847c478bd9Sstevel@tonic-gate 		if (boolean(value(HARDWAREFLOW)))
7857c478bd9Sstevel@tonic-gate 			hardwareflow("on");
7867c478bd9Sstevel@tonic-gate 		else
7877c478bd9Sstevel@tonic-gate 			hardwareflow("off");
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate /*
7927c478bd9Sstevel@tonic-gate  * Turn tandem mode on or off for remote tty.
7937c478bd9Sstevel@tonic-gate  */
794*8d489c7aSmuffin void
795*8d489c7aSmuffin tandem(char *option)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 	struct termios rmtty;
7987c478bd9Sstevel@tonic-gate 
799*8d489c7aSmuffin 	(void) ioctl(FD, TCGETS, (char *)&rmtty);
8007c478bd9Sstevel@tonic-gate 	if (equal(option, "on")) {
8017c478bd9Sstevel@tonic-gate 		rmtty.c_iflag |= IXOFF|IXON;
8027c478bd9Sstevel@tonic-gate 		arg.c_iflag |= IXOFF|IXON;
8037c478bd9Sstevel@tonic-gate 		rmtty.c_cc[VSTART] = defarg.c_cc[VSTART];
8047c478bd9Sstevel@tonic-gate 		rmtty.c_cc[VSTOP] = defarg.c_cc[VSTOP];
8057c478bd9Sstevel@tonic-gate 	} else {
8067c478bd9Sstevel@tonic-gate 		rmtty.c_iflag &= ~(IXOFF|IXON);
8077c478bd9Sstevel@tonic-gate 		arg.c_iflag &= ~(IXOFF|IXON);
8087c478bd9Sstevel@tonic-gate 	}
809*8d489c7aSmuffin 	(void) ioctl(FD, TCSETSF, (char *)&rmtty);
810*8d489c7aSmuffin 	(void) ioctl(0, TCSETSF, (char *)&arg);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate  * Turn hardwareflow mode on or off for remote tty.
8157c478bd9Sstevel@tonic-gate  */
816*8d489c7aSmuffin void
817*8d489c7aSmuffin hardwareflow(char *option)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	struct termios rmtty;
8207c478bd9Sstevel@tonic-gate 
821*8d489c7aSmuffin 	(void) ioctl(FD, TCGETS, (char *)&rmtty);
8227c478bd9Sstevel@tonic-gate 	if (equal(option, "on")) {
8237c478bd9Sstevel@tonic-gate 		rmtty.c_cflag |= (CRTSCTS|CRTSXOFF);
8247c478bd9Sstevel@tonic-gate 	} else {
8257c478bd9Sstevel@tonic-gate 		rmtty.c_cflag &= ~(CRTSCTS|CRTSXOFF);
8267c478bd9Sstevel@tonic-gate 	}
827*8d489c7aSmuffin 	(void) ioctl(FD, TCSETSF, (char *)&rmtty);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate /*
8317c478bd9Sstevel@tonic-gate  * Turn interrupts from local tty on or off.
8327c478bd9Sstevel@tonic-gate  */
833*8d489c7aSmuffin void
834*8d489c7aSmuffin intr(char *option)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	if (equal(option, "on"))
8387c478bd9Sstevel@tonic-gate 		arg.c_lflag |= ISIG;
8397c478bd9Sstevel@tonic-gate 	else
8407c478bd9Sstevel@tonic-gate 		arg.c_lflag &= ~ISIG;
841*8d489c7aSmuffin 	(void) ioctl(0, TCSETSF, (char *)&arg);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate  * Send a break.
8467c478bd9Sstevel@tonic-gate  */
847*8d489c7aSmuffin /* ARGSUSED */
848*8d489c7aSmuffin void
849*8d489c7aSmuffin genbrk(int cc)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 
852*8d489c7aSmuffin 	(void) ioctl(FD, TCSBRK, 0);
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate /*
8567c478bd9Sstevel@tonic-gate  * Suspend tip
8577c478bd9Sstevel@tonic-gate  */
858*8d489c7aSmuffin void
859*8d489c7aSmuffin suspend(int c)
8607c478bd9Sstevel@tonic-gate {
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	unraw();
863*8d489c7aSmuffin 	(void) kill(c == _CTRL('y') ? getpid() : 0, SIGTSTP);
8647c478bd9Sstevel@tonic-gate 	raw();
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate /*
8687c478bd9Sstevel@tonic-gate  *	expand a file name if it includes shell meta characters
8697c478bd9Sstevel@tonic-gate  */
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate char *
872*8d489c7aSmuffin expand(char name[])
8737c478bd9Sstevel@tonic-gate {
8747c478bd9Sstevel@tonic-gate 	static char xname[BUFSIZ];
8757c478bd9Sstevel@tonic-gate 	char cmdbuf[BUFSIZ];
876*8d489c7aSmuffin 	int pid, l;
877*8d489c7aSmuffin 	char *cp, *Shell;
8787c478bd9Sstevel@tonic-gate 	int s, pivec[2];
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	if (!anyof(name, "~{[*?$`'\"\\"))
8817c478bd9Sstevel@tonic-gate 		return (name);
8827c478bd9Sstevel@tonic-gate 	if (pipe(pivec) < 0) {
8837c478bd9Sstevel@tonic-gate 		perror("pipe");
8847c478bd9Sstevel@tonic-gate 		return (name);
8857c478bd9Sstevel@tonic-gate 	}
886*8d489c7aSmuffin 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "echo %s", name);
8877c478bd9Sstevel@tonic-gate 	if ((pid = vfork()) == 0) {
8887c478bd9Sstevel@tonic-gate 		userperm();
8897c478bd9Sstevel@tonic-gate 		Shell = value(SHELL);
8907c478bd9Sstevel@tonic-gate 		if (Shell == NOSTR)
8917c478bd9Sstevel@tonic-gate 			Shell = "/bin/sh";
892*8d489c7aSmuffin 		(void) close(pivec[0]);
893*8d489c7aSmuffin 		(void) close(1);
894*8d489c7aSmuffin 		(void) dup(pivec[1]);
895*8d489c7aSmuffin 		(void) close(pivec[1]);
896*8d489c7aSmuffin 		(void) close(2);
897*8d489c7aSmuffin 		(void) execl(Shell, Shell, "-c", cmdbuf, 0);
8987c478bd9Sstevel@tonic-gate 		_exit(1);
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 	if (pid == -1) {
9017c478bd9Sstevel@tonic-gate 		perror("fork");
902*8d489c7aSmuffin 		(void) close(pivec[0]);
903*8d489c7aSmuffin 		(void) close(pivec[1]);
9047c478bd9Sstevel@tonic-gate 		return (NOSTR);
9057c478bd9Sstevel@tonic-gate 	}
906*8d489c7aSmuffin 	(void) close(pivec[1]);
9077c478bd9Sstevel@tonic-gate 	l = read(pivec[0], xname, BUFSIZ);
908*8d489c7aSmuffin 	(void) close(pivec[0]);
909*8d489c7aSmuffin 	while (wait(&s) != pid)
9107c478bd9Sstevel@tonic-gate 		;
9117c478bd9Sstevel@tonic-gate 	s &= 0377;
9127c478bd9Sstevel@tonic-gate 	if (s != 0 && s != SIGPIPE) {
913*8d489c7aSmuffin 		(void) fprintf(stderr, "\"Echo\" failed\n");
9147c478bd9Sstevel@tonic-gate 		return (NOSTR);
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 	if (l < 0) {
9177c478bd9Sstevel@tonic-gate 		perror("read");
9187c478bd9Sstevel@tonic-gate 		return (NOSTR);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 	if (l == 0) {
921*8d489c7aSmuffin 		(void) fprintf(stderr, "\"%s\": No match\n", name);
9227c478bd9Sstevel@tonic-gate 		return (NOSTR);
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 	if (l == BUFSIZ) {
925*8d489c7aSmuffin 		(void) fprintf(stderr, "Buffer overflow expanding \"%s\"\n",
926*8d489c7aSmuffin 		    name);
9277c478bd9Sstevel@tonic-gate 		return (NOSTR);
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 	xname[l] = 0;
9307c478bd9Sstevel@tonic-gate 	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
9317c478bd9Sstevel@tonic-gate 		;
9327c478bd9Sstevel@tonic-gate 	*++cp = '\0';
9337c478bd9Sstevel@tonic-gate 	return (xname);
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate /*
9377c478bd9Sstevel@tonic-gate  * Are any of the characters in the two strings the same?
9387c478bd9Sstevel@tonic-gate  */
9397c478bd9Sstevel@tonic-gate 
940*8d489c7aSmuffin int
941*8d489c7aSmuffin anyof(char *s1, char *s2)
9427c478bd9Sstevel@tonic-gate {
943*8d489c7aSmuffin 	int c;
9447c478bd9Sstevel@tonic-gate 
945*8d489c7aSmuffin 	while ((c = *s1++) != 0)
9467c478bd9Sstevel@tonic-gate 		if (any(c, s2))
9477c478bd9Sstevel@tonic-gate 			return (1);
9487c478bd9Sstevel@tonic-gate 	return (0);
9497c478bd9Sstevel@tonic-gate }
950