xref: /illumos-gate/usr/src/cmd/tip/cmds.c (revision 94e1761e)
17c478bd9Sstevel@tonic-gate /*
2*94e1761eSsn  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
58d489c7aSmuffin 
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 
128d489c7aSmuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #include "tip.h"
15*94e1761eSsn #include <limits.h>
167c478bd9Sstevel@tonic-gate #ifdef USG
177c478bd9Sstevel@tonic-gate #include <unistd.h>
187c478bd9Sstevel@tonic-gate #else
197c478bd9Sstevel@tonic-gate #include <vfork.h>
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * tip
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * miscellaneous commands
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate int	quant[] = { 60, 60, 24 };
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate char	null = '\0';
317c478bd9Sstevel@tonic-gate char	*sep[] = { "second", "minute", "hour" };
327c478bd9Sstevel@tonic-gate static	char *argv[10];		/* argument vector for take and put */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate sigjmp_buf intbuf;		/* for interrupts and timeouts */
357c478bd9Sstevel@tonic-gate 
368d489c7aSmuffin void	timeout(void);		/* timeout function called on alarm */
378d489c7aSmuffin void	intcopy(void);		/* interrupt routine for file transfers */
388d489c7aSmuffin void	transfer(char *, int, char *);
398d489c7aSmuffin void	transmit(FILE *, char *, char *);
408d489c7aSmuffin void	send(char);
418d489c7aSmuffin void	execute(char *);
428d489c7aSmuffin void	prtime(char *, time_t);
438d489c7aSmuffin void	hardwareflow(char *);
448d489c7aSmuffin void	intr(char *);
458d489c7aSmuffin int	args(char *, char *[], size_t);
468d489c7aSmuffin int	anyof(char *, char *);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * FTP - remote ==> local
507c478bd9Sstevel@tonic-gate  *  get a file from the remote host
517c478bd9Sstevel@tonic-gate  */
528d489c7aSmuffin void
538d489c7aSmuffin getfl(int c)
547c478bd9Sstevel@tonic-gate {
558d489c7aSmuffin 	char buf[256], *cp;
567c478bd9Sstevel@tonic-gate 
578d489c7aSmuffin 	(void) putchar(c);
587c478bd9Sstevel@tonic-gate 	/*
597c478bd9Sstevel@tonic-gate 	 * get the UNIX receiving file's name
607c478bd9Sstevel@tonic-gate 	 */
617c478bd9Sstevel@tonic-gate 	if (prompt("Local file name? ", copyname, sizeof (copyname)))
627c478bd9Sstevel@tonic-gate 		return;
637c478bd9Sstevel@tonic-gate 	cp = expand(copyname);
647c478bd9Sstevel@tonic-gate 	if (cp == NOSTR)
657c478bd9Sstevel@tonic-gate 		return;
667c478bd9Sstevel@tonic-gate 	if ((sfd = creat(cp, 0666)) < 0) {
678d489c7aSmuffin 		(void) printf("\r\n%s: cannot creat\r\n", copyname);
687c478bd9Sstevel@tonic-gate 		return;
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/*
727c478bd9Sstevel@tonic-gate 	 * collect parameters
737c478bd9Sstevel@tonic-gate 	 */
747c478bd9Sstevel@tonic-gate 	if (prompt("List command for remote system? ", buf, sizeof (buf))) {
758d489c7aSmuffin 		(void) unlink(copyname);
767c478bd9Sstevel@tonic-gate 		return;
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 	transfer(buf, sfd, value(EOFREAD));
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * Cu-like take command
837c478bd9Sstevel@tonic-gate  */
848d489c7aSmuffin /* ARGSUSED */
858d489c7aSmuffin void
868d489c7aSmuffin cu_take(int cc)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	int fd, argc;
898d489c7aSmuffin 	char line[BUFSIZ], *cp;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (prompt("[take] ", copyname, sizeof (copyname)))
927c478bd9Sstevel@tonic-gate 		return;
937c478bd9Sstevel@tonic-gate 	argc = args(copyname, argv, sizeof (argv)/sizeof (char *));
947c478bd9Sstevel@tonic-gate 	if (argc < 1 || argc > 2) {
958d489c7aSmuffin 		(void) printf("usage: <take> from [to]\r\n");
967c478bd9Sstevel@tonic-gate 		return;
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 	if (argc == 1)
997c478bd9Sstevel@tonic-gate 		argv[1] = argv[0];
1007c478bd9Sstevel@tonic-gate 	cp = expand(argv[1]);
1017c478bd9Sstevel@tonic-gate 	if (cp == NOSTR)
1027c478bd9Sstevel@tonic-gate 		return;
1037c478bd9Sstevel@tonic-gate 	if ((fd = creat(cp, 0666)) < 0) {
1048d489c7aSmuffin 		(void) printf("\r\n%s: cannot create\r\n", argv[1]);
1057c478bd9Sstevel@tonic-gate 		return;
1067c478bd9Sstevel@tonic-gate 	}
1078d489c7aSmuffin 	(void) snprintf(line, sizeof (line), "cat %s; echo \01", argv[0]);
1087c478bd9Sstevel@tonic-gate 	transfer(line, fd, "\01");
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * Bulk transfer routine --
1137c478bd9Sstevel@tonic-gate  *  used by getfl(), cu_take(), and pipefile()
1147c478bd9Sstevel@tonic-gate  */
1158d489c7aSmuffin void
1168d489c7aSmuffin transfer(char *buf, int fd, char *eofchars)
1177c478bd9Sstevel@tonic-gate {
1188d489c7aSmuffin 	int ct;
1197c478bd9Sstevel@tonic-gate 	char c, buffer[BUFSIZ];
1207c478bd9Sstevel@tonic-gate 	char *p = buffer;	/* can't be register because of longjmp */
1218d489c7aSmuffin 	int cnt, eof, bol;
1227c478bd9Sstevel@tonic-gate 	time_t start;
1238d489c7aSmuffin 	sig_handler_t	f;
1247c478bd9Sstevel@tonic-gate 
1258d489c7aSmuffin 	parwrite(FD, (unsigned char *)buf, strlen(buf));
1268d489c7aSmuffin 	(void) kill(pid, SIGIOT);
1278d489c7aSmuffin 	/* Wait until read process stops */
1288d489c7aSmuffin 	(void) read(repdes[0], (char *)&ccc, 1);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * finish command
1327c478bd9Sstevel@tonic-gate 	 */
1338d489c7aSmuffin 	parwrite(FD, (unsigned char *)"\r", 1);
1347c478bd9Sstevel@tonic-gate 	do
1358d489c7aSmuffin 		(void) read(FD, &c, 1);
1368d489c7aSmuffin 	while ((c&0177) != '\n')
1378d489c7aSmuffin 		;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (sigsetjmp(intbuf, 1))
1407c478bd9Sstevel@tonic-gate 		goto out;
1418d489c7aSmuffin 	f = signal(SIGINT, (sig_handler_t)intcopy);
1427c478bd9Sstevel@tonic-gate 	intr("on");
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	start = time(0);
1457c478bd9Sstevel@tonic-gate 	bol = 1;
1467c478bd9Sstevel@tonic-gate 	ct = 0;
1477c478bd9Sstevel@tonic-gate 	for (;;) {
1487c478bd9Sstevel@tonic-gate 		eof = read(FD, &c, 1) <= 0;
1497c478bd9Sstevel@tonic-gate 		if (noparity)
1507c478bd9Sstevel@tonic-gate 			c &= 0377;
1517c478bd9Sstevel@tonic-gate 		else
1527c478bd9Sstevel@tonic-gate 			c &= 0177;
1537c478bd9Sstevel@tonic-gate 		if (eof || (bol && any(c, eofchars)))
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 		if (c == 0)
1567c478bd9Sstevel@tonic-gate 			continue;	/* ignore nulls */
1577c478bd9Sstevel@tonic-gate 		if (c == '\r')
1587c478bd9Sstevel@tonic-gate 			continue;
1597c478bd9Sstevel@tonic-gate 		*p++ = c;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		if (c == '\n') {
1627c478bd9Sstevel@tonic-gate 			bol = 1;
1637c478bd9Sstevel@tonic-gate 			if (boolean(value(VERBOSE)))
1648d489c7aSmuffin 				(void) printf("\r%d", ++ct);
1657c478bd9Sstevel@tonic-gate 		} else
1667c478bd9Sstevel@tonic-gate 			bol = 0;
1677c478bd9Sstevel@tonic-gate 		if ((cnt = (p-buffer)) == number(value(FRAMESIZE))) {
1687c478bd9Sstevel@tonic-gate 			if (write(fd, buffer, cnt) != cnt) {
1698d489c7aSmuffin 				(void) printf("\r\nwrite error\r\n");
1707c478bd9Sstevel@tonic-gate 				goto out;
1717c478bd9Sstevel@tonic-gate 			}
1727c478bd9Sstevel@tonic-gate 			p = buffer;
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate out:
1768d489c7aSmuffin 	if ((cnt = (p-buffer)) != 0)
1777c478bd9Sstevel@tonic-gate 		if (write(fd, buffer, cnt) != cnt)
1788d489c7aSmuffin 			(void) printf("\r\nwrite error\r\n");
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
1817c478bd9Sstevel@tonic-gate 		prtime(" lines transferred in ", time(0)-start);
1827c478bd9Sstevel@tonic-gate 	intr("off");
1838d489c7aSmuffin 	(void) write(fildes[1], (char *)&ccc, 1);
1848d489c7aSmuffin 	(void) signal(SIGINT, f);
1858d489c7aSmuffin 	(void) close(fd);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * FTP - remote ==> local process
1907c478bd9Sstevel@tonic-gate  *   send remote input to local process via pipe
1917c478bd9Sstevel@tonic-gate  */
1928d489c7aSmuffin /* ARGSUSED */
1938d489c7aSmuffin void
1948d489c7aSmuffin pipefile(int cc)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	int cpid, pdes[2];
1977c478bd9Sstevel@tonic-gate 	char buf[256];
1987c478bd9Sstevel@tonic-gate 	int status, p;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (prompt("Local command? ", buf, sizeof (buf)))
2017c478bd9Sstevel@tonic-gate 		return;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (pipe(pdes)) {
2048d489c7aSmuffin 		(void) printf("can't establish pipe\r\n");
2057c478bd9Sstevel@tonic-gate 		return;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if ((cpid = fork()) < 0) {
2098d489c7aSmuffin 		(void) printf("can't fork!\r\n");
2107c478bd9Sstevel@tonic-gate 		return;
2117c478bd9Sstevel@tonic-gate 	} else if (cpid) {
2127c478bd9Sstevel@tonic-gate 		if (prompt("List command for remote system? ", buf,
2138d489c7aSmuffin 		    sizeof (buf))) {
2148d489c7aSmuffin 			(void) close(pdes[0]), (void) close(pdes[1]);
2158d489c7aSmuffin 			(void) kill(cpid, SIGKILL);
2167c478bd9Sstevel@tonic-gate 		} else {
2178d489c7aSmuffin 			(void) close(pdes[0]);
2188d489c7aSmuffin 			(void) signal(SIGPIPE, (sig_handler_t)intcopy);
2197c478bd9Sstevel@tonic-gate 			transfer(buf, pdes[1], value(EOFREAD));
2208d489c7aSmuffin 			(void) signal(SIGPIPE, SIG_DFL);
2217c478bd9Sstevel@tonic-gate 			while ((p = wait(&status)) > 0 && p != cpid)
2227c478bd9Sstevel@tonic-gate 				;
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 	} else {
2258d489c7aSmuffin 		int f;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		userperm();
2288d489c7aSmuffin 		(void) dup2(pdes[0], 0);
2298d489c7aSmuffin 		(void) close(pdes[0]);
2307c478bd9Sstevel@tonic-gate 		for (f = 3; f < 20; f++)
2318d489c7aSmuffin 			(void) close(f);
2327c478bd9Sstevel@tonic-gate 		execute(buf);
2338d489c7aSmuffin 		(void) printf("can't execl!\r\n");
2347c478bd9Sstevel@tonic-gate 		exit(0);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * FTP - local ==> remote
2407c478bd9Sstevel@tonic-gate  *  send local file to remote host
2417c478bd9Sstevel@tonic-gate  *  terminate transmission with pseudo EOF sequence
2427c478bd9Sstevel@tonic-gate  */
2438d489c7aSmuffin void
2448d489c7aSmuffin tip_sendfile(int cc)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	FILE *fd;
2477c478bd9Sstevel@tonic-gate 	char *fnamex;
2487c478bd9Sstevel@tonic-gate 
2498d489c7aSmuffin 	(void) putchar(cc);
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * get file name
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	if (prompt("Local file name? ", fname, sizeof (fname)))
2547c478bd9Sstevel@tonic-gate 		return;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * look up file
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	fnamex = expand(fname);
2607c478bd9Sstevel@tonic-gate 	if (fnamex == NOSTR)
2617c478bd9Sstevel@tonic-gate 		return;
2627c478bd9Sstevel@tonic-gate 	if ((fd = fopen(fnamex, "r")) == NULL) {
2638d489c7aSmuffin 		(void) printf("%s: cannot open\r\n", fname);
2647c478bd9Sstevel@tonic-gate 		return;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	transmit(fd, value(EOFWRITE), NULL);
2677c478bd9Sstevel@tonic-gate 	if (!boolean(value(ECHOCHECK))) {
2687c478bd9Sstevel@tonic-gate 		struct termios buf;
2697c478bd9Sstevel@tonic-gate 
2708d489c7aSmuffin 		(void) ioctl(FD, TCGETS, (char *)&buf);	/* this does a */
2718d489c7aSmuffin 		(void) ioctl(FD, TCSETSF, (char *)&buf);	/* wflushtty */
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Bulk transfer routine to remote host --
2778d489c7aSmuffin  *   used by tip_sendfile() and cu_put()
2787c478bd9Sstevel@tonic-gate  */
2798d489c7aSmuffin void
2808d489c7aSmuffin transmit(FILE *fd, char *eofchars, char *command)
2817c478bd9Sstevel@tonic-gate {
2828d489c7aSmuffin 	sig_handler_t	ointr;
2837c478bd9Sstevel@tonic-gate 	char *pc, lastc, rc;
2847c478bd9Sstevel@tonic-gate 	int c, ccount, lcount;
2857c478bd9Sstevel@tonic-gate 	time_t start_t, stop_t;
2867c478bd9Sstevel@tonic-gate 
2878d489c7aSmuffin 	(void) kill(pid, SIGIOT);	/* put TIPOUT into a wait state */
2887c478bd9Sstevel@tonic-gate 	timedout = 0;
2897c478bd9Sstevel@tonic-gate 	if (sigsetjmp(intbuf, 1)) {
2907c478bd9Sstevel@tonic-gate 		if (timedout)
2918d489c7aSmuffin 			(void) printf("\r\ntimed out at eol\r\n");
2928d489c7aSmuffin 		(void) alarm(0);
2937c478bd9Sstevel@tonic-gate 		goto out;
2947c478bd9Sstevel@tonic-gate 	}
2958d489c7aSmuffin 	ointr = signal(SIGINT, (sig_handler_t)intcopy);
2967c478bd9Sstevel@tonic-gate 	intr("on");
2978d489c7aSmuffin 	(void) read(repdes[0], (char *)&ccc, 1);
2987c478bd9Sstevel@tonic-gate 	if (command != NULL) {
2997c478bd9Sstevel@tonic-gate 		for (pc = command; *pc; pc++)
3007c478bd9Sstevel@tonic-gate 			send(*pc);
3017c478bd9Sstevel@tonic-gate 		if (boolean(value(ECHOCHECK)))
3028d489c7aSmuffin 			(void) read(FD, (char *)&c, 1);	/* trailing \n */
3037c478bd9Sstevel@tonic-gate 		else {
3047c478bd9Sstevel@tonic-gate 			struct termios buf;
3058d489c7aSmuffin 			/* wait for remote stty to take effect */
3068d489c7aSmuffin 			(void) sleep(5);
3078d489c7aSmuffin 			/* this does a */
3088d489c7aSmuffin 			(void) ioctl(FD, TCGETS, (char *)&buf);
3098d489c7aSmuffin 			/* wflushtty */
3108d489c7aSmuffin 			(void) ioctl(FD, TCSETSF, (char *)&buf);
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	lcount = 0;
3147c478bd9Sstevel@tonic-gate 	lastc = '\0';
3157c478bd9Sstevel@tonic-gate 	start_t = time(0);
3167c478bd9Sstevel@tonic-gate 	if (boolean(value(RAWFTP))) {
3177c478bd9Sstevel@tonic-gate 		while ((c = getc(fd)) != EOF) {
3187c478bd9Sstevel@tonic-gate 			lcount++;
3197c478bd9Sstevel@tonic-gate 			send(c);
3207c478bd9Sstevel@tonic-gate 			if (boolean(value(VERBOSE)) && lcount%100 == 0)
3218d489c7aSmuffin 				(void) printf("\r%d", lcount);
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 		if (boolean(value(VERBOSE)))
3248d489c7aSmuffin 			(void) printf("\r%d", lcount);
3257c478bd9Sstevel@tonic-gate 		goto out;
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 	for (;;) {
3287c478bd9Sstevel@tonic-gate 		ccount = 0;
3297c478bd9Sstevel@tonic-gate 		do {
3307c478bd9Sstevel@tonic-gate 			c = getc(fd);
3317c478bd9Sstevel@tonic-gate 			if (c == EOF)
3327c478bd9Sstevel@tonic-gate 				goto out;
3337c478bd9Sstevel@tonic-gate 			if (c == 0177)
3347c478bd9Sstevel@tonic-gate 				continue;
3357c478bd9Sstevel@tonic-gate 			lastc = c;
3367c478bd9Sstevel@tonic-gate 			if (c < 040) {
3377c478bd9Sstevel@tonic-gate 				if (c == '\n') {
3387c478bd9Sstevel@tonic-gate 					c = '\r';
3397c478bd9Sstevel@tonic-gate 				} else if (c == '\t') {
3407c478bd9Sstevel@tonic-gate 					if (boolean(value(TABEXPAND))) {
3417c478bd9Sstevel@tonic-gate 						send(' ');
3427c478bd9Sstevel@tonic-gate 						while ((++ccount % 8) != 0)
3437c478bd9Sstevel@tonic-gate 							send(' ');
3447c478bd9Sstevel@tonic-gate 						continue;
3457c478bd9Sstevel@tonic-gate 					}
3467c478bd9Sstevel@tonic-gate 				} else
3477c478bd9Sstevel@tonic-gate 					continue;
3487c478bd9Sstevel@tonic-gate 			}
3497c478bd9Sstevel@tonic-gate 			send(c);
3507c478bd9Sstevel@tonic-gate 		} while (c != '\r');
3517c478bd9Sstevel@tonic-gate 		if (boolean(value(VERBOSE)))
3528d489c7aSmuffin 			(void) printf("\r%d", ++lcount);
3537c478bd9Sstevel@tonic-gate 		if (boolean(value(ECHOCHECK))) {
3548d489c7aSmuffin 			(void) alarm(number(value(ETIMEOUT)));
3557c478bd9Sstevel@tonic-gate 			do {	/* wait for prompt */
3568d489c7aSmuffin 				(void) read(FD, &rc, 1);
3577c478bd9Sstevel@tonic-gate 			} while ((rc&0177) != character(value(PROMPT)));
3588d489c7aSmuffin 			(void) alarm(0);
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate out:
3627c478bd9Sstevel@tonic-gate 	if (lastc != '\n' && !boolean(value(RAWFTP)))
3637c478bd9Sstevel@tonic-gate 		send('\r');
3647c478bd9Sstevel@tonic-gate 	if (eofchars)
3657c478bd9Sstevel@tonic-gate 		for (pc = eofchars; *pc; pc++)
3667c478bd9Sstevel@tonic-gate 			send(*pc);
3677c478bd9Sstevel@tonic-gate 	stop_t = time(0);
3688d489c7aSmuffin 	(void) fclose(fd);
3697c478bd9Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
3707c478bd9Sstevel@tonic-gate 		if (boolean(value(RAWFTP)))
3717c478bd9Sstevel@tonic-gate 			prtime(" chars transferred in ", stop_t-start_t);
3727c478bd9Sstevel@tonic-gate 		else
3737c478bd9Sstevel@tonic-gate 			prtime(" lines transferred in ", stop_t-start_t);
3748d489c7aSmuffin 	(void) write(fildes[1], (char *)&ccc, 1);
3757c478bd9Sstevel@tonic-gate 	intr("off");
3768d489c7aSmuffin 	(void) signal(SIGINT, ointr);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate  * Cu-like put command
3817c478bd9Sstevel@tonic-gate  */
3828d489c7aSmuffin /* ARGSUSED */
3838d489c7aSmuffin void
3848d489c7aSmuffin cu_put(int cc)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	FILE *fd;
3877c478bd9Sstevel@tonic-gate 	char line[BUFSIZ];
3887c478bd9Sstevel@tonic-gate 	int argc;
3897c478bd9Sstevel@tonic-gate 	char *copynamex;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (prompt("[put] ", copyname, sizeof (copyname)))
3927c478bd9Sstevel@tonic-gate 		return;
3937c478bd9Sstevel@tonic-gate 	argc = args(copyname, argv, sizeof (argv)/sizeof (char *));
3947c478bd9Sstevel@tonic-gate 	if (argc < 1 || argc > 2) {
3958d489c7aSmuffin 		(void) printf("usage: <put> from [to]\r\n");
3967c478bd9Sstevel@tonic-gate 		return;
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 	if (argc == 1)
3997c478bd9Sstevel@tonic-gate 		argv[1] = argv[0];
4007c478bd9Sstevel@tonic-gate 	copynamex = expand(argv[0]);
4017c478bd9Sstevel@tonic-gate 	if (copynamex == NOSTR)
4027c478bd9Sstevel@tonic-gate 		return;
4037c478bd9Sstevel@tonic-gate 	if ((fd = fopen(copynamex, "r")) == NULL) {
4048d489c7aSmuffin 		(void) printf("%s: cannot open\r\n", copynamex);
4057c478bd9Sstevel@tonic-gate 		return;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	if (boolean(value(ECHOCHECK)))
4088d489c7aSmuffin 		(void) snprintf(line, sizeof (line), "cat>%s\r", argv[1]);
4097c478bd9Sstevel@tonic-gate 	else
4108d489c7aSmuffin 		(void) snprintf(line, sizeof (line),
4118d489c7aSmuffin 		    "stty -echo; cat>%s; stty echo\r", argv[1]);
4127c478bd9Sstevel@tonic-gate 	transmit(fd, "\04", line);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * FTP - send single character
4177c478bd9Sstevel@tonic-gate  *  wait for echo & handle timeout
4187c478bd9Sstevel@tonic-gate  */
4198d489c7aSmuffin void
4208d489c7aSmuffin send(char c)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	char cc;
4237c478bd9Sstevel@tonic-gate 	int retry = 0;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	cc = c;
4268d489c7aSmuffin 	parwrite(FD, (unsigned char *)&cc, 1);
4277c478bd9Sstevel@tonic-gate #ifdef notdef
4287c478bd9Sstevel@tonic-gate 	if (number(value(CDELAY)) > 0 && c != '\r')
4297c478bd9Sstevel@tonic-gate 		nap(number(value(CDELAY)));
4307c478bd9Sstevel@tonic-gate #endif
4317c478bd9Sstevel@tonic-gate 	if (!boolean(value(ECHOCHECK))) {
4327c478bd9Sstevel@tonic-gate #ifdef notdef
4337c478bd9Sstevel@tonic-gate 		if (number(value(LDELAY)) > 0 && c == '\r')
4347c478bd9Sstevel@tonic-gate 			nap(number(value(LDELAY)));
4357c478bd9Sstevel@tonic-gate #endif
4367c478bd9Sstevel@tonic-gate 		return;
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate tryagain:
4397c478bd9Sstevel@tonic-gate 	timedout = 0;
4407c478bd9Sstevel@tonic-gate 	if (sigsetjmp(intbuf, 1) && timedout) {
4418d489c7aSmuffin 		(void) printf("\r\ntimeout error (%s)\r\n", ctrl(c));
4427c478bd9Sstevel@tonic-gate 		if (retry++ > 3)
4437c478bd9Sstevel@tonic-gate 			return;
4448d489c7aSmuffin 		parwrite(FD, (unsigned char *)&null, 1); /* poke it */
4457c478bd9Sstevel@tonic-gate 		goto tryagain;
4467c478bd9Sstevel@tonic-gate 	}
4478d489c7aSmuffin 	(void) alarm(number(value(ETIMEOUT)));
4488d489c7aSmuffin 	(void) read(FD, &cc, 1);
4498d489c7aSmuffin 	(void) alarm(0);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate void
4538d489c7aSmuffin timeout(void)
4547c478bd9Sstevel@tonic-gate {
4558d489c7aSmuffin 	(void) signal(SIGALRM, (sig_handler_t)timeout);
4567c478bd9Sstevel@tonic-gate 	timedout = 1;
4577c478bd9Sstevel@tonic-gate 	siglongjmp(intbuf, 1);
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate /*
4617c478bd9Sstevel@tonic-gate  * Stolen from consh() -- puts a remote file on the output of a local command.
4627c478bd9Sstevel@tonic-gate  *	Identical to consh() except for where stdout goes.
4637c478bd9Sstevel@tonic-gate  */
4648d489c7aSmuffin void
4658d489c7aSmuffin pipeout(int c)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	char buf[256];
4687c478bd9Sstevel@tonic-gate 	int cpid, status, p;
4697c478bd9Sstevel@tonic-gate 	time_t start;
4707c478bd9Sstevel@tonic-gate 
4718d489c7aSmuffin 	(void) putchar(c);
4727c478bd9Sstevel@tonic-gate 	if (prompt("Local command? ", buf, sizeof (buf)))
4737c478bd9Sstevel@tonic-gate 		return;
4748d489c7aSmuffin 	(void) kill(pid, SIGIOT);	/* put TIPOUT into a wait state */
4758d489c7aSmuffin 	(void) signal(SIGINT, SIG_IGN);
4768d489c7aSmuffin 	(void) signal(SIGQUIT, SIG_IGN);
4777c478bd9Sstevel@tonic-gate 	intr("on");
4788d489c7aSmuffin 	(void) read(repdes[0], (char *)&ccc, 1);
4797c478bd9Sstevel@tonic-gate 	/*
4807c478bd9Sstevel@tonic-gate 	 * Set up file descriptors in the child and
4817c478bd9Sstevel@tonic-gate 	 *  let it go...
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	if ((cpid = fork()) < 0)
4848d489c7aSmuffin 		(void) printf("can't fork!\r\n");
4857c478bd9Sstevel@tonic-gate 	else if (cpid) {
4867c478bd9Sstevel@tonic-gate 		start = time(0);
4877c478bd9Sstevel@tonic-gate 		while ((p = wait(&status)) > 0 && p != cpid)
4887c478bd9Sstevel@tonic-gate 			;
4897c478bd9Sstevel@tonic-gate 	} else {
4908d489c7aSmuffin 		int i;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		userperm();
4938d489c7aSmuffin 		(void) dup2(FD, 1);
4947c478bd9Sstevel@tonic-gate 		for (i = 3; i < 20; i++)
4958d489c7aSmuffin 			(void) close(i);
4968d489c7aSmuffin 		(void) signal(SIGINT, SIG_DFL);
4978d489c7aSmuffin 		(void) signal(SIGQUIT, SIG_DFL);
4987c478bd9Sstevel@tonic-gate 		execute(buf);
4998d489c7aSmuffin 		(void) printf("can't find `%s'\r\n", buf);
5007c478bd9Sstevel@tonic-gate 		exit(0);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
5037c478bd9Sstevel@tonic-gate 		prtime("away for ", time(0)-start);
5048d489c7aSmuffin 	(void) write(fildes[1], (char *)&ccc, 1);
5057c478bd9Sstevel@tonic-gate 	intr("off");
5068d489c7aSmuffin 	(void) signal(SIGINT, SIG_DFL);
5078d489c7aSmuffin 	(void) signal(SIGQUIT, SIG_DFL);
5087c478bd9Sstevel@tonic-gate }
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate /*
5117c478bd9Sstevel@tonic-gate  * Fork a program with:
5127c478bd9Sstevel@tonic-gate  *  0 <-> remote tty in
5137c478bd9Sstevel@tonic-gate  *  1 <-> remote tty out
5147c478bd9Sstevel@tonic-gate  *  2 <-> local tty stderr out
5157c478bd9Sstevel@tonic-gate  */
5168d489c7aSmuffin void
5178d489c7aSmuffin consh(int c)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	char buf[256];
5207c478bd9Sstevel@tonic-gate 	int cpid, status, p;
5218d489c7aSmuffin 	sig_handler_t	ointr, oquit;
5227c478bd9Sstevel@tonic-gate 	time_t start;
5237c478bd9Sstevel@tonic-gate 
5248d489c7aSmuffin 	(void) putchar(c);
5257c478bd9Sstevel@tonic-gate 	if (prompt("Local command? ", buf, sizeof (buf)))
5267c478bd9Sstevel@tonic-gate 		return;
5278d489c7aSmuffin 	(void) kill(pid, SIGIOT);	/* put TIPOUT into a wait state */
5288d489c7aSmuffin 	(void) read(repdes[0], (char *)&ccc, 1);
5297c478bd9Sstevel@tonic-gate 	ointr = signal(SIGINT, SIG_IGN);
5307c478bd9Sstevel@tonic-gate 	oquit = signal(SIGQUIT, SIG_IGN);
5317c478bd9Sstevel@tonic-gate 	unraw();
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Set up file descriptors in the child and
5347c478bd9Sstevel@tonic-gate 	 *  let it go...
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	if ((cpid = fork()) < 0)
5378d489c7aSmuffin 		(void) printf("can't fork!\r\n");
5387c478bd9Sstevel@tonic-gate 	else if (cpid) {
5397c478bd9Sstevel@tonic-gate 		start = time(0);
5407c478bd9Sstevel@tonic-gate 		while ((p = wait(&status)) > 0 && p != cpid)
5417c478bd9Sstevel@tonic-gate 			;
5427c478bd9Sstevel@tonic-gate 		raw();
5438d489c7aSmuffin 		(void) signal(SIGINT, ointr);
5448d489c7aSmuffin 		(void) signal(SIGQUIT, oquit);
5457c478bd9Sstevel@tonic-gate 	} else {
5468d489c7aSmuffin 		int i;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		userperm();
5498d489c7aSmuffin 		(void) dup2(FD, 0);
5508d489c7aSmuffin 		(void) dup2(0, 1);
5517c478bd9Sstevel@tonic-gate 		for (i = 3; i < 20; i++)
5528d489c7aSmuffin 			(void) close(i);
5538d489c7aSmuffin 		(void) signal(SIGINT, SIG_DFL);
5548d489c7aSmuffin 		(void) signal(SIGQUIT, SIG_DFL);
5557c478bd9Sstevel@tonic-gate 		execute(buf);
5568d489c7aSmuffin 		(void) printf("can't find `%s'\r\n", buf);
5577c478bd9Sstevel@tonic-gate 		exit(0);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
5607c478bd9Sstevel@tonic-gate 		prtime("\r\naway for ", time(0)-start);
5618d489c7aSmuffin 	(void) write(fildes[1], (char *)&ccc, 1);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * Escape to local shell
5667c478bd9Sstevel@tonic-gate  */
5678d489c7aSmuffin /* ARGSUSED */
5688d489c7aSmuffin void
5698d489c7aSmuffin shell(int cc)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	int shpid, status;
5728d489c7aSmuffin 	sig_handler_t	ointr, oquit;
5737c478bd9Sstevel@tonic-gate 	char *cp;
5747c478bd9Sstevel@tonic-gate 
5758d489c7aSmuffin 	(void) printf("[sh]\r\n");
5767c478bd9Sstevel@tonic-gate 	ointr = signal(SIGINT, SIG_IGN);
5777c478bd9Sstevel@tonic-gate 	oquit = signal(SIGQUIT, SIG_IGN);
5787c478bd9Sstevel@tonic-gate 	unraw();
5797c478bd9Sstevel@tonic-gate 	if (shpid = fork()) {
5807c478bd9Sstevel@tonic-gate 		while (shpid != wait(&status))
5817c478bd9Sstevel@tonic-gate 			;
5827c478bd9Sstevel@tonic-gate 		raw();
5838d489c7aSmuffin 		(void) printf("\r\n!\r\n");
5848d489c7aSmuffin 		(void) signal(SIGINT, ointr);
5858d489c7aSmuffin 		(void) signal(SIGQUIT, oquit);
5867c478bd9Sstevel@tonic-gate 	} else {
5877c478bd9Sstevel@tonic-gate 		userperm();
5888d489c7aSmuffin 		(void) signal(SIGQUIT, SIG_DFL);
5898d489c7aSmuffin 		(void) signal(SIGINT, SIG_DFL);
5907c478bd9Sstevel@tonic-gate 		if ((cp = strrchr(value(SHELL), '/')) == NULL)
5917c478bd9Sstevel@tonic-gate 			cp = value(SHELL);
5927c478bd9Sstevel@tonic-gate 		else
5937c478bd9Sstevel@tonic-gate 			cp++;
5948d489c7aSmuffin 		(void) execl(value(SHELL), cp, 0);
5958d489c7aSmuffin 		(void) printf("\r\ncan't execl!\r\n");
5967c478bd9Sstevel@tonic-gate 		exit(1);
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate /*
6017c478bd9Sstevel@tonic-gate  * TIPIN portion of scripting
6027c478bd9Sstevel@tonic-gate  *   initiate the conversation with TIPOUT
6037c478bd9Sstevel@tonic-gate  */
6048d489c7aSmuffin void
6058d489c7aSmuffin setscript(void)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	char c;
608*94e1761eSsn 
609*94e1761eSsn 	if (strlen(value(RECORD)) >= PATH_MAX-1) {
610*94e1761eSsn 		(void) fprintf(stderr, "tip: record file name too long\r\n");
611*94e1761eSsn 		return;
612*94e1761eSsn 	}
6137c478bd9Sstevel@tonic-gate 	/*
6147c478bd9Sstevel@tonic-gate 	 * enable TIPOUT side for dialogue
6157c478bd9Sstevel@tonic-gate 	 */
6168d489c7aSmuffin 	(void) kill(pid, SIGEMT);
6177c478bd9Sstevel@tonic-gate 	if (boolean(value(SCRIPT)))
6188d489c7aSmuffin 		(void) write(fildes[1], value(RECORD), strlen(value(RECORD)));
6198d489c7aSmuffin 	(void) write(fildes[1], "\n", 1);
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * wait for TIPOUT to finish
6227c478bd9Sstevel@tonic-gate 	 */
6238d489c7aSmuffin 	(void) read(repdes[0], &c, 1);
6247c478bd9Sstevel@tonic-gate 	if (c == 'n')
6258d489c7aSmuffin 		(void) fprintf(stderr, "tip: can't create record file %s\r\n",
6267c478bd9Sstevel@tonic-gate 		    value(RECORD));
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate /*
6307c478bd9Sstevel@tonic-gate  * Change current working directory of
6317c478bd9Sstevel@tonic-gate  *   local portion of tip
6327c478bd9Sstevel@tonic-gate  */
6338d489c7aSmuffin /* ARGSUSED */
6348d489c7aSmuffin void
6358d489c7aSmuffin chdirectory(int cc)
6367c478bd9Sstevel@tonic-gate {
6377c478bd9Sstevel@tonic-gate 	char dirname[80];
6388d489c7aSmuffin 	char *cp = dirname;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	if (prompt("[cd] ", dirname, sizeof (dirname))) {
6417c478bd9Sstevel@tonic-gate 		if (stoprompt)
6427c478bd9Sstevel@tonic-gate 			return;
6437c478bd9Sstevel@tonic-gate 		cp = value(HOME);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	if (chdir(cp) < 0)
6468d489c7aSmuffin 		(void) printf("%s: bad directory\r\n", cp);
6478d489c7aSmuffin 	(void) printf("!\r\n");
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6508d489c7aSmuffin void
6518d489c7aSmuffin tip_abort(char *msg)
6527c478bd9Sstevel@tonic-gate {
6538d489c7aSmuffin 	/* don't want to hear about our child */
6548d489c7aSmuffin 	(void) signal(SIGCHLD, SIG_DFL);
6558d489c7aSmuffin 	(void) kill(pid, SIGTERM);
6567c478bd9Sstevel@tonic-gate 	myperm();
6577c478bd9Sstevel@tonic-gate 	disconnect(msg);
6587c478bd9Sstevel@tonic-gate 	if (msg != NOSTR)
6598d489c7aSmuffin 		(void) printf("\r\n%s", msg);
6608d489c7aSmuffin 	(void) printf("\r\n[EOT]\r\n");
6617c478bd9Sstevel@tonic-gate 	delock(uucplock);
6627c478bd9Sstevel@tonic-gate 	unraw();
6637c478bd9Sstevel@tonic-gate 	exit(0);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6668d489c7aSmuffin /* ARGSUSED */
6678d489c7aSmuffin void
6688d489c7aSmuffin finish(int cc)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	char *dismsg;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if ((dismsg = value(DISCONNECT)) != NOSTR) {
6738d489c7aSmuffin 		(void) write(FD, dismsg, strlen(dismsg));
6748d489c7aSmuffin 		(void) sleep(5);
6757c478bd9Sstevel@tonic-gate 	}
6768d489c7aSmuffin 	tip_abort(NOSTR);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate void
6808d489c7aSmuffin intcopy(void)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 
6838d489c7aSmuffin 	(void) signal(SIGINT, SIG_IGN);
6847c478bd9Sstevel@tonic-gate 	siglongjmp(intbuf, 1);
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6878d489c7aSmuffin void
6888d489c7aSmuffin execute(char *s)
6897c478bd9Sstevel@tonic-gate {
6908d489c7aSmuffin 	char *cp;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if ((cp = strrchr(value(SHELL), '/')) == NULL)
6937c478bd9Sstevel@tonic-gate 		cp = value(SHELL);
6947c478bd9Sstevel@tonic-gate 	else
6957c478bd9Sstevel@tonic-gate 		cp++;
6968d489c7aSmuffin 	(void) execl(value(SHELL), cp, "-c", s, 0);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6998d489c7aSmuffin int
7008d489c7aSmuffin args(char *buf, char *a[], size_t na)
7017c478bd9Sstevel@tonic-gate {
7028d489c7aSmuffin 	char *p = buf, *start;
7038d489c7aSmuffin 	char **parg = a;
7048d489c7aSmuffin 	int n = 0;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	do {
7077c478bd9Sstevel@tonic-gate 		while (*p && (*p == ' ' || *p == '\t'))
7087c478bd9Sstevel@tonic-gate 			p++;
7097c478bd9Sstevel@tonic-gate 		start = p;
7107c478bd9Sstevel@tonic-gate 		if (*p)
7117c478bd9Sstevel@tonic-gate 			*parg = p;
7127c478bd9Sstevel@tonic-gate 		while (*p && (*p != ' ' && *p != '\t'))
7137c478bd9Sstevel@tonic-gate 			p++;
7147c478bd9Sstevel@tonic-gate 		if (p != start)
7157c478bd9Sstevel@tonic-gate 			parg++, n++;
7167c478bd9Sstevel@tonic-gate 		if (*p)
7177c478bd9Sstevel@tonic-gate 			*p++ = '\0';
7187c478bd9Sstevel@tonic-gate 	} while (*p && n < na);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	return (n);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate 
7238d489c7aSmuffin void
7248d489c7aSmuffin prtime(char *s, time_t a)
7257c478bd9Sstevel@tonic-gate {
7268d489c7aSmuffin 	int i;
7277c478bd9Sstevel@tonic-gate 	int nums[3];
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
7307c478bd9Sstevel@tonic-gate 		nums[i] = (int)(a % quant[i]);
7317c478bd9Sstevel@tonic-gate 		a /= quant[i];
7327c478bd9Sstevel@tonic-gate 	}
7338d489c7aSmuffin 	(void) printf("%s", s);
7347c478bd9Sstevel@tonic-gate 	while (--i >= 0)
7357c478bd9Sstevel@tonic-gate 		if (nums[i] || i == 0 && nums[1] == 0 && nums[2] == 0)
7368d489c7aSmuffin 			(void) printf("%d %s%c ", nums[i], sep[i],
7378d489c7aSmuffin 			    nums[i] == 1 ? '\0' : 's');
7388d489c7aSmuffin 	(void) printf("\r\n!\r\n");
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7418d489c7aSmuffin /* ARGSUSED */
7428d489c7aSmuffin void
7438d489c7aSmuffin variable(int cc)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	char	buf[256];
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if (prompt("[set] ", buf, sizeof (buf)))
7487c478bd9Sstevel@tonic-gate 		return;
7497c478bd9Sstevel@tonic-gate 	vlex(buf);
7507c478bd9Sstevel@tonic-gate 	if (vtable[BEAUTIFY].v_access&CHANGED) {
7517c478bd9Sstevel@tonic-gate 		vtable[BEAUTIFY].v_access &= ~CHANGED;
7528d489c7aSmuffin 		(void) kill(pid, SIGSYS);
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 	if (vtable[SCRIPT].v_access&CHANGED) {
7557c478bd9Sstevel@tonic-gate 		vtable[SCRIPT].v_access &= ~CHANGED;
7567c478bd9Sstevel@tonic-gate 		setscript();
7577c478bd9Sstevel@tonic-gate 		/*
7587c478bd9Sstevel@tonic-gate 		 * So that "set record=blah script" doesn't
7597c478bd9Sstevel@tonic-gate 		 *  cause two transactions to occur.
7607c478bd9Sstevel@tonic-gate 		 */
7617c478bd9Sstevel@tonic-gate 		if (vtable[RECORD].v_access&CHANGED)
7627c478bd9Sstevel@tonic-gate 			vtable[RECORD].v_access &= ~CHANGED;
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	if (vtable[RECORD].v_access&CHANGED) {
7657c478bd9Sstevel@tonic-gate 		vtable[RECORD].v_access &= ~CHANGED;
7667c478bd9Sstevel@tonic-gate 		if (boolean(value(SCRIPT)))
7677c478bd9Sstevel@tonic-gate 			setscript();
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 	if (vtable[TAND].v_access&CHANGED) {
7707c478bd9Sstevel@tonic-gate 		vtable[TAND].v_access &= ~CHANGED;
7717c478bd9Sstevel@tonic-gate 		if (boolean(value(TAND)))
7727c478bd9Sstevel@tonic-gate 			tandem("on");
7737c478bd9Sstevel@tonic-gate 		else
7747c478bd9Sstevel@tonic-gate 			tandem("off");
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	if (vtable[LECHO].v_access&CHANGED) {
7777c478bd9Sstevel@tonic-gate 		vtable[LECHO].v_access &= ~CHANGED;
7787c478bd9Sstevel@tonic-gate 		boolean(value(HALFDUPLEX)) = boolean(value(LECHO));
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 	if (vtable[PARITY].v_access&CHANGED) {
7817c478bd9Sstevel@tonic-gate 		vtable[PARITY].v_access &= ~CHANGED;
7827c478bd9Sstevel@tonic-gate 		setparity(NULL);
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	if (vtable[BAUDRATE].v_access&CHANGED) {
7857c478bd9Sstevel@tonic-gate 		vtable[BAUDRATE].v_access &= ~CHANGED;
7867c478bd9Sstevel@tonic-gate 		ttysetup(speed(number(value(BAUDRATE))));
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 	if (vtable[HARDWAREFLOW].v_access & CHANGED) {
7897c478bd9Sstevel@tonic-gate 		vtable[HARDWAREFLOW].v_access &= ~CHANGED;
7907c478bd9Sstevel@tonic-gate 		if (boolean(value(HARDWAREFLOW)))
7917c478bd9Sstevel@tonic-gate 			hardwareflow("on");
7927c478bd9Sstevel@tonic-gate 		else
7937c478bd9Sstevel@tonic-gate 			hardwareflow("off");
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate /*
7987c478bd9Sstevel@tonic-gate  * Turn tandem mode on or off for remote tty.
7997c478bd9Sstevel@tonic-gate  */
8008d489c7aSmuffin void
8018d489c7aSmuffin tandem(char *option)
8027c478bd9Sstevel@tonic-gate {
8037c478bd9Sstevel@tonic-gate 	struct termios rmtty;
8047c478bd9Sstevel@tonic-gate 
8058d489c7aSmuffin 	(void) ioctl(FD, TCGETS, (char *)&rmtty);
8067c478bd9Sstevel@tonic-gate 	if (equal(option, "on")) {
8077c478bd9Sstevel@tonic-gate 		rmtty.c_iflag |= IXOFF|IXON;
8087c478bd9Sstevel@tonic-gate 		arg.c_iflag |= IXOFF|IXON;
8097c478bd9Sstevel@tonic-gate 		rmtty.c_cc[VSTART] = defarg.c_cc[VSTART];
8107c478bd9Sstevel@tonic-gate 		rmtty.c_cc[VSTOP] = defarg.c_cc[VSTOP];
8117c478bd9Sstevel@tonic-gate 	} else {
8127c478bd9Sstevel@tonic-gate 		rmtty.c_iflag &= ~(IXOFF|IXON);
8137c478bd9Sstevel@tonic-gate 		arg.c_iflag &= ~(IXOFF|IXON);
8147c478bd9Sstevel@tonic-gate 	}
8158d489c7aSmuffin 	(void) ioctl(FD, TCSETSF, (char *)&rmtty);
8168d489c7aSmuffin 	(void) ioctl(0, TCSETSF, (char *)&arg);
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate /*
8207c478bd9Sstevel@tonic-gate  * Turn hardwareflow mode on or off for remote tty.
8217c478bd9Sstevel@tonic-gate  */
8228d489c7aSmuffin void
8238d489c7aSmuffin hardwareflow(char *option)
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 	struct termios rmtty;
8267c478bd9Sstevel@tonic-gate 
8278d489c7aSmuffin 	(void) ioctl(FD, TCGETS, (char *)&rmtty);
8287c478bd9Sstevel@tonic-gate 	if (equal(option, "on")) {
8297c478bd9Sstevel@tonic-gate 		rmtty.c_cflag |= (CRTSCTS|CRTSXOFF);
8307c478bd9Sstevel@tonic-gate 	} else {
8317c478bd9Sstevel@tonic-gate 		rmtty.c_cflag &= ~(CRTSCTS|CRTSXOFF);
8327c478bd9Sstevel@tonic-gate 	}
8338d489c7aSmuffin 	(void) ioctl(FD, TCSETSF, (char *)&rmtty);
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate /*
8377c478bd9Sstevel@tonic-gate  * Turn interrupts from local tty on or off.
8387c478bd9Sstevel@tonic-gate  */
8398d489c7aSmuffin void
8408d489c7aSmuffin intr(char *option)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	if (equal(option, "on"))
8447c478bd9Sstevel@tonic-gate 		arg.c_lflag |= ISIG;
8457c478bd9Sstevel@tonic-gate 	else
8467c478bd9Sstevel@tonic-gate 		arg.c_lflag &= ~ISIG;
8478d489c7aSmuffin 	(void) ioctl(0, TCSETSF, (char *)&arg);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
8517c478bd9Sstevel@tonic-gate  * Send a break.
8527c478bd9Sstevel@tonic-gate  */
8538d489c7aSmuffin /* ARGSUSED */
8548d489c7aSmuffin void
8558d489c7aSmuffin genbrk(int cc)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 
8588d489c7aSmuffin 	(void) ioctl(FD, TCSBRK, 0);
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate /*
8627c478bd9Sstevel@tonic-gate  * Suspend tip
8637c478bd9Sstevel@tonic-gate  */
8648d489c7aSmuffin void
8658d489c7aSmuffin suspend(int c)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	unraw();
8698d489c7aSmuffin 	(void) kill(c == _CTRL('y') ? getpid() : 0, SIGTSTP);
8707c478bd9Sstevel@tonic-gate 	raw();
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate /*
8747c478bd9Sstevel@tonic-gate  *	expand a file name if it includes shell meta characters
8757c478bd9Sstevel@tonic-gate  */
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate char *
8788d489c7aSmuffin expand(char name[])
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	static char xname[BUFSIZ];
8817c478bd9Sstevel@tonic-gate 	char cmdbuf[BUFSIZ];
8828d489c7aSmuffin 	int pid, l;
8838d489c7aSmuffin 	char *cp, *Shell;
8847c478bd9Sstevel@tonic-gate 	int s, pivec[2];
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	if (!anyof(name, "~{[*?$`'\"\\"))
8877c478bd9Sstevel@tonic-gate 		return (name);
8887c478bd9Sstevel@tonic-gate 	if (pipe(pivec) < 0) {
8897c478bd9Sstevel@tonic-gate 		perror("pipe");
8907c478bd9Sstevel@tonic-gate 		return (name);
8917c478bd9Sstevel@tonic-gate 	}
8928d489c7aSmuffin 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "echo %s", name);
8937c478bd9Sstevel@tonic-gate 	if ((pid = vfork()) == 0) {
8947c478bd9Sstevel@tonic-gate 		userperm();
8957c478bd9Sstevel@tonic-gate 		Shell = value(SHELL);
8967c478bd9Sstevel@tonic-gate 		if (Shell == NOSTR)
8977c478bd9Sstevel@tonic-gate 			Shell = "/bin/sh";
8988d489c7aSmuffin 		(void) close(pivec[0]);
8998d489c7aSmuffin 		(void) close(1);
9008d489c7aSmuffin 		(void) dup(pivec[1]);
9018d489c7aSmuffin 		(void) close(pivec[1]);
9028d489c7aSmuffin 		(void) close(2);
9038d489c7aSmuffin 		(void) execl(Shell, Shell, "-c", cmdbuf, 0);
9047c478bd9Sstevel@tonic-gate 		_exit(1);
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 	if (pid == -1) {
9077c478bd9Sstevel@tonic-gate 		perror("fork");
9088d489c7aSmuffin 		(void) close(pivec[0]);
9098d489c7aSmuffin 		(void) close(pivec[1]);
9107c478bd9Sstevel@tonic-gate 		return (NOSTR);
9117c478bd9Sstevel@tonic-gate 	}
9128d489c7aSmuffin 	(void) close(pivec[1]);
9137c478bd9Sstevel@tonic-gate 	l = read(pivec[0], xname, BUFSIZ);
9148d489c7aSmuffin 	(void) close(pivec[0]);
9158d489c7aSmuffin 	while (wait(&s) != pid)
9167c478bd9Sstevel@tonic-gate 		;
9177c478bd9Sstevel@tonic-gate 	s &= 0377;
9187c478bd9Sstevel@tonic-gate 	if (s != 0 && s != SIGPIPE) {
9198d489c7aSmuffin 		(void) fprintf(stderr, "\"Echo\" failed\n");
9207c478bd9Sstevel@tonic-gate 		return (NOSTR);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 	if (l < 0) {
9237c478bd9Sstevel@tonic-gate 		perror("read");
9247c478bd9Sstevel@tonic-gate 		return (NOSTR);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 	if (l == 0) {
9278d489c7aSmuffin 		(void) fprintf(stderr, "\"%s\": No match\n", name);
9287c478bd9Sstevel@tonic-gate 		return (NOSTR);
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 	if (l == BUFSIZ) {
9318d489c7aSmuffin 		(void) fprintf(stderr, "Buffer overflow expanding \"%s\"\n",
9328d489c7aSmuffin 		    name);
9337c478bd9Sstevel@tonic-gate 		return (NOSTR);
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 	xname[l] = 0;
9367c478bd9Sstevel@tonic-gate 	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
9377c478bd9Sstevel@tonic-gate 		;
9387c478bd9Sstevel@tonic-gate 	*++cp = '\0';
9397c478bd9Sstevel@tonic-gate 	return (xname);
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate /*
9437c478bd9Sstevel@tonic-gate  * Are any of the characters in the two strings the same?
9447c478bd9Sstevel@tonic-gate  */
9457c478bd9Sstevel@tonic-gate 
9468d489c7aSmuffin int
9478d489c7aSmuffin anyof(char *s1, char *s2)
9487c478bd9Sstevel@tonic-gate {
9498d489c7aSmuffin 	int c;
9507c478bd9Sstevel@tonic-gate 
9518d489c7aSmuffin 	while ((c = *s1++) != 0)
9527c478bd9Sstevel@tonic-gate 		if (any(c, s2))
9537c478bd9Sstevel@tonic-gate 			return (1);
9547c478bd9Sstevel@tonic-gate 	return (0);
9557c478bd9Sstevel@tonic-gate }
956