xref: /onnv-gate/usr/src/cmd/tip/cmds.c (revision 2590:3709c0dd648e)
10Sstevel@tonic-gate /*
2*2590Ssn199410  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
5549Smuffin 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
80Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
90Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
100Sstevel@tonic-gate  */
110Sstevel@tonic-gate 
12549Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
130Sstevel@tonic-gate 
140Sstevel@tonic-gate #include "tip.h"
15*2590Ssn199410 #include <limits.h>
160Sstevel@tonic-gate #ifdef USG
170Sstevel@tonic-gate #include <unistd.h>
180Sstevel@tonic-gate #else
190Sstevel@tonic-gate #include <vfork.h>
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * tip
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * miscellaneous commands
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate int	quant[] = { 60, 60, 24 };
290Sstevel@tonic-gate 
300Sstevel@tonic-gate char	null = '\0';
310Sstevel@tonic-gate char	*sep[] = { "second", "minute", "hour" };
320Sstevel@tonic-gate static	char *argv[10];		/* argument vector for take and put */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate sigjmp_buf intbuf;		/* for interrupts and timeouts */
350Sstevel@tonic-gate 
36549Smuffin void	timeout(void);		/* timeout function called on alarm */
37549Smuffin void	intcopy(void);		/* interrupt routine for file transfers */
38549Smuffin void	transfer(char *, int, char *);
39549Smuffin void	transmit(FILE *, char *, char *);
40549Smuffin void	send(char);
41549Smuffin void	execute(char *);
42549Smuffin void	prtime(char *, time_t);
43549Smuffin void	hardwareflow(char *);
44549Smuffin void	intr(char *);
45549Smuffin int	args(char *, char *[], size_t);
46549Smuffin int	anyof(char *, char *);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * FTP - remote ==> local
500Sstevel@tonic-gate  *  get a file from the remote host
510Sstevel@tonic-gate  */
52549Smuffin void
getfl(int c)53549Smuffin getfl(int c)
540Sstevel@tonic-gate {
55549Smuffin 	char buf[256], *cp;
560Sstevel@tonic-gate 
57549Smuffin 	(void) putchar(c);
580Sstevel@tonic-gate 	/*
590Sstevel@tonic-gate 	 * get the UNIX receiving file's name
600Sstevel@tonic-gate 	 */
610Sstevel@tonic-gate 	if (prompt("Local file name? ", copyname, sizeof (copyname)))
620Sstevel@tonic-gate 		return;
630Sstevel@tonic-gate 	cp = expand(copyname);
640Sstevel@tonic-gate 	if (cp == NOSTR)
650Sstevel@tonic-gate 		return;
660Sstevel@tonic-gate 	if ((sfd = creat(cp, 0666)) < 0) {
67549Smuffin 		(void) printf("\r\n%s: cannot creat\r\n", copyname);
680Sstevel@tonic-gate 		return;
690Sstevel@tonic-gate 	}
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	/*
720Sstevel@tonic-gate 	 * collect parameters
730Sstevel@tonic-gate 	 */
740Sstevel@tonic-gate 	if (prompt("List command for remote system? ", buf, sizeof (buf))) {
75549Smuffin 		(void) unlink(copyname);
760Sstevel@tonic-gate 		return;
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 	transfer(buf, sfd, value(EOFREAD));
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Cu-like take command
830Sstevel@tonic-gate  */
84549Smuffin /* ARGSUSED */
85549Smuffin void
cu_take(int cc)86549Smuffin cu_take(int cc)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	int fd, argc;
89549Smuffin 	char line[BUFSIZ], *cp;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (prompt("[take] ", copyname, sizeof (copyname)))
920Sstevel@tonic-gate 		return;
930Sstevel@tonic-gate 	argc = args(copyname, argv, sizeof (argv)/sizeof (char *));
940Sstevel@tonic-gate 	if (argc < 1 || argc > 2) {
95549Smuffin 		(void) printf("usage: <take> from [to]\r\n");
960Sstevel@tonic-gate 		return;
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 	if (argc == 1)
990Sstevel@tonic-gate 		argv[1] = argv[0];
1000Sstevel@tonic-gate 	cp = expand(argv[1]);
1010Sstevel@tonic-gate 	if (cp == NOSTR)
1020Sstevel@tonic-gate 		return;
1030Sstevel@tonic-gate 	if ((fd = creat(cp, 0666)) < 0) {
104549Smuffin 		(void) printf("\r\n%s: cannot create\r\n", argv[1]);
1050Sstevel@tonic-gate 		return;
1060Sstevel@tonic-gate 	}
107549Smuffin 	(void) snprintf(line, sizeof (line), "cat %s; echo \01", argv[0]);
1080Sstevel@tonic-gate 	transfer(line, fd, "\01");
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * Bulk transfer routine --
1130Sstevel@tonic-gate  *  used by getfl(), cu_take(), and pipefile()
1140Sstevel@tonic-gate  */
115549Smuffin void
transfer(char * buf,int fd,char * eofchars)116549Smuffin transfer(char *buf, int fd, char *eofchars)
1170Sstevel@tonic-gate {
118549Smuffin 	int ct;
1190Sstevel@tonic-gate 	char c, buffer[BUFSIZ];
1200Sstevel@tonic-gate 	char *p = buffer;	/* can't be register because of longjmp */
121549Smuffin 	int cnt, eof, bol;
1220Sstevel@tonic-gate 	time_t start;
123549Smuffin 	sig_handler_t	f;
1240Sstevel@tonic-gate 
125549Smuffin 	parwrite(FD, (unsigned char *)buf, strlen(buf));
126549Smuffin 	(void) kill(pid, SIGIOT);
127549Smuffin 	/* Wait until read process stops */
128549Smuffin 	(void) read(repdes[0], (char *)&ccc, 1);
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	/*
1310Sstevel@tonic-gate 	 * finish command
1320Sstevel@tonic-gate 	 */
133549Smuffin 	parwrite(FD, (unsigned char *)"\r", 1);
1340Sstevel@tonic-gate 	do
135549Smuffin 		(void) read(FD, &c, 1);
136549Smuffin 	while ((c&0177) != '\n')
137549Smuffin 		;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	if (sigsetjmp(intbuf, 1))
1400Sstevel@tonic-gate 		goto out;
141549Smuffin 	f = signal(SIGINT, (sig_handler_t)intcopy);
1420Sstevel@tonic-gate 	intr("on");
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	start = time(0);
1450Sstevel@tonic-gate 	bol = 1;
1460Sstevel@tonic-gate 	ct = 0;
1470Sstevel@tonic-gate 	for (;;) {
1480Sstevel@tonic-gate 		eof = read(FD, &c, 1) <= 0;
1490Sstevel@tonic-gate 		if (noparity)
1500Sstevel@tonic-gate 			c &= 0377;
1510Sstevel@tonic-gate 		else
1520Sstevel@tonic-gate 			c &= 0177;
1530Sstevel@tonic-gate 		if (eof || (bol && any(c, eofchars)))
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 		if (c == 0)
1560Sstevel@tonic-gate 			continue;	/* ignore nulls */
1570Sstevel@tonic-gate 		if (c == '\r')
1580Sstevel@tonic-gate 			continue;
1590Sstevel@tonic-gate 		*p++ = c;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		if (c == '\n') {
1620Sstevel@tonic-gate 			bol = 1;
1630Sstevel@tonic-gate 			if (boolean(value(VERBOSE)))
164549Smuffin 				(void) printf("\r%d", ++ct);
1650Sstevel@tonic-gate 		} else
1660Sstevel@tonic-gate 			bol = 0;
1670Sstevel@tonic-gate 		if ((cnt = (p-buffer)) == number(value(FRAMESIZE))) {
1680Sstevel@tonic-gate 			if (write(fd, buffer, cnt) != cnt) {
169549Smuffin 				(void) printf("\r\nwrite error\r\n");
1700Sstevel@tonic-gate 				goto out;
1710Sstevel@tonic-gate 			}
1720Sstevel@tonic-gate 			p = buffer;
1730Sstevel@tonic-gate 		}
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate out:
176549Smuffin 	if ((cnt = (p-buffer)) != 0)
1770Sstevel@tonic-gate 		if (write(fd, buffer, cnt) != cnt)
178549Smuffin 			(void) printf("\r\nwrite error\r\n");
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
1810Sstevel@tonic-gate 		prtime(" lines transferred in ", time(0)-start);
1820Sstevel@tonic-gate 	intr("off");
183549Smuffin 	(void) write(fildes[1], (char *)&ccc, 1);
184549Smuffin 	(void) signal(SIGINT, f);
185549Smuffin 	(void) close(fd);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * FTP - remote ==> local process
1900Sstevel@tonic-gate  *   send remote input to local process via pipe
1910Sstevel@tonic-gate  */
192549Smuffin /* ARGSUSED */
193549Smuffin void
pipefile(int cc)194549Smuffin pipefile(int cc)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	int cpid, pdes[2];
1970Sstevel@tonic-gate 	char buf[256];
1980Sstevel@tonic-gate 	int status, p;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	if (prompt("Local command? ", buf, sizeof (buf)))
2010Sstevel@tonic-gate 		return;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (pipe(pdes)) {
204549Smuffin 		(void) printf("can't establish pipe\r\n");
2050Sstevel@tonic-gate 		return;
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	if ((cpid = fork()) < 0) {
209549Smuffin 		(void) printf("can't fork!\r\n");
2100Sstevel@tonic-gate 		return;
2110Sstevel@tonic-gate 	} else if (cpid) {
2120Sstevel@tonic-gate 		if (prompt("List command for remote system? ", buf,
213549Smuffin 		    sizeof (buf))) {
214549Smuffin 			(void) close(pdes[0]), (void) close(pdes[1]);
215549Smuffin 			(void) kill(cpid, SIGKILL);
2160Sstevel@tonic-gate 		} else {
217549Smuffin 			(void) close(pdes[0]);
218549Smuffin 			(void) signal(SIGPIPE, (sig_handler_t)intcopy);
2190Sstevel@tonic-gate 			transfer(buf, pdes[1], value(EOFREAD));
220549Smuffin 			(void) signal(SIGPIPE, SIG_DFL);
2210Sstevel@tonic-gate 			while ((p = wait(&status)) > 0 && p != cpid)
2220Sstevel@tonic-gate 				;
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 	} else {
225549Smuffin 		int f;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		userperm();
228549Smuffin 		(void) dup2(pdes[0], 0);
229549Smuffin 		(void) close(pdes[0]);
2300Sstevel@tonic-gate 		for (f = 3; f < 20; f++)
231549Smuffin 			(void) close(f);
2320Sstevel@tonic-gate 		execute(buf);
233549Smuffin 		(void) printf("can't execl!\r\n");
2340Sstevel@tonic-gate 		exit(0);
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate  * FTP - local ==> remote
2400Sstevel@tonic-gate  *  send local file to remote host
2410Sstevel@tonic-gate  *  terminate transmission with pseudo EOF sequence
2420Sstevel@tonic-gate  */
243549Smuffin void
tip_sendfile(int cc)244549Smuffin tip_sendfile(int cc)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	FILE *fd;
2470Sstevel@tonic-gate 	char *fnamex;
2480Sstevel@tonic-gate 
249549Smuffin 	(void) putchar(cc);
2500Sstevel@tonic-gate 	/*
2510Sstevel@tonic-gate 	 * get file name
2520Sstevel@tonic-gate 	 */
2530Sstevel@tonic-gate 	if (prompt("Local file name? ", fname, sizeof (fname)))
2540Sstevel@tonic-gate 		return;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 * look up file
2580Sstevel@tonic-gate 	 */
2590Sstevel@tonic-gate 	fnamex = expand(fname);
2600Sstevel@tonic-gate 	if (fnamex == NOSTR)
2610Sstevel@tonic-gate 		return;
2620Sstevel@tonic-gate 	if ((fd = fopen(fnamex, "r")) == NULL) {
263549Smuffin 		(void) printf("%s: cannot open\r\n", fname);
2640Sstevel@tonic-gate 		return;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 	transmit(fd, value(EOFWRITE), NULL);
2670Sstevel@tonic-gate 	if (!boolean(value(ECHOCHECK))) {
2680Sstevel@tonic-gate 		struct termios buf;
2690Sstevel@tonic-gate 
270549Smuffin 		(void) ioctl(FD, TCGETS, (char *)&buf);	/* this does a */
271549Smuffin 		(void) ioctl(FD, TCSETSF, (char *)&buf);	/* wflushtty */
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * Bulk transfer routine to remote host --
277549Smuffin  *   used by tip_sendfile() and cu_put()
2780Sstevel@tonic-gate  */
279549Smuffin void
transmit(FILE * fd,char * eofchars,char * command)280549Smuffin transmit(FILE *fd, char *eofchars, char *command)
2810Sstevel@tonic-gate {
282549Smuffin 	sig_handler_t	ointr;
2830Sstevel@tonic-gate 	char *pc, lastc, rc;
2840Sstevel@tonic-gate 	int c, ccount, lcount;
2850Sstevel@tonic-gate 	time_t start_t, stop_t;
2860Sstevel@tonic-gate 
287549Smuffin 	(void) kill(pid, SIGIOT);	/* put TIPOUT into a wait state */
2880Sstevel@tonic-gate 	timedout = 0;
2890Sstevel@tonic-gate 	if (sigsetjmp(intbuf, 1)) {
2900Sstevel@tonic-gate 		if (timedout)
291549Smuffin 			(void) printf("\r\ntimed out at eol\r\n");
292549Smuffin 		(void) alarm(0);
2930Sstevel@tonic-gate 		goto out;
2940Sstevel@tonic-gate 	}
295549Smuffin 	ointr = signal(SIGINT, (sig_handler_t)intcopy);
2960Sstevel@tonic-gate 	intr("on");
297549Smuffin 	(void) read(repdes[0], (char *)&ccc, 1);
2980Sstevel@tonic-gate 	if (command != NULL) {
2990Sstevel@tonic-gate 		for (pc = command; *pc; pc++)
3000Sstevel@tonic-gate 			send(*pc);
3010Sstevel@tonic-gate 		if (boolean(value(ECHOCHECK)))
302549Smuffin 			(void) read(FD, (char *)&c, 1);	/* trailing \n */
3030Sstevel@tonic-gate 		else {
3040Sstevel@tonic-gate 			struct termios buf;
305549Smuffin 			/* wait for remote stty to take effect */
306549Smuffin 			(void) sleep(5);
307549Smuffin 			/* this does a */
308549Smuffin 			(void) ioctl(FD, TCGETS, (char *)&buf);
309549Smuffin 			/* wflushtty */
310549Smuffin 			(void) ioctl(FD, TCSETSF, (char *)&buf);
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 	lcount = 0;
3140Sstevel@tonic-gate 	lastc = '\0';
3150Sstevel@tonic-gate 	start_t = time(0);
3160Sstevel@tonic-gate 	if (boolean(value(RAWFTP))) {
3170Sstevel@tonic-gate 		while ((c = getc(fd)) != EOF) {
3180Sstevel@tonic-gate 			lcount++;
3190Sstevel@tonic-gate 			send(c);
3200Sstevel@tonic-gate 			if (boolean(value(VERBOSE)) && lcount%100 == 0)
321549Smuffin 				(void) printf("\r%d", lcount);
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 		if (boolean(value(VERBOSE)))
324549Smuffin 			(void) printf("\r%d", lcount);
3250Sstevel@tonic-gate 		goto out;
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 	for (;;) {
3280Sstevel@tonic-gate 		ccount = 0;
3290Sstevel@tonic-gate 		do {
3300Sstevel@tonic-gate 			c = getc(fd);
3310Sstevel@tonic-gate 			if (c == EOF)
3320Sstevel@tonic-gate 				goto out;
3330Sstevel@tonic-gate 			if (c == 0177)
3340Sstevel@tonic-gate 				continue;
3350Sstevel@tonic-gate 			lastc = c;
3360Sstevel@tonic-gate 			if (c < 040) {
3370Sstevel@tonic-gate 				if (c == '\n') {
3380Sstevel@tonic-gate 					c = '\r';
3390Sstevel@tonic-gate 				} else if (c == '\t') {
3400Sstevel@tonic-gate 					if (boolean(value(TABEXPAND))) {
3410Sstevel@tonic-gate 						send(' ');
3420Sstevel@tonic-gate 						while ((++ccount % 8) != 0)
3430Sstevel@tonic-gate 							send(' ');
3440Sstevel@tonic-gate 						continue;
3450Sstevel@tonic-gate 					}
3460Sstevel@tonic-gate 				} else
3470Sstevel@tonic-gate 					continue;
3480Sstevel@tonic-gate 			}
3490Sstevel@tonic-gate 			send(c);
3500Sstevel@tonic-gate 		} while (c != '\r');
3510Sstevel@tonic-gate 		if (boolean(value(VERBOSE)))
352549Smuffin 			(void) printf("\r%d", ++lcount);
3530Sstevel@tonic-gate 		if (boolean(value(ECHOCHECK))) {
354549Smuffin 			(void) alarm(number(value(ETIMEOUT)));
3550Sstevel@tonic-gate 			do {	/* wait for prompt */
356549Smuffin 				(void) read(FD, &rc, 1);
3570Sstevel@tonic-gate 			} while ((rc&0177) != character(value(PROMPT)));
358549Smuffin 			(void) alarm(0);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate out:
3620Sstevel@tonic-gate 	if (lastc != '\n' && !boolean(value(RAWFTP)))
3630Sstevel@tonic-gate 		send('\r');
3640Sstevel@tonic-gate 	if (eofchars)
3650Sstevel@tonic-gate 		for (pc = eofchars; *pc; pc++)
3660Sstevel@tonic-gate 			send(*pc);
3670Sstevel@tonic-gate 	stop_t = time(0);
368549Smuffin 	(void) fclose(fd);
3690Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
3700Sstevel@tonic-gate 		if (boolean(value(RAWFTP)))
3710Sstevel@tonic-gate 			prtime(" chars transferred in ", stop_t-start_t);
3720Sstevel@tonic-gate 		else
3730Sstevel@tonic-gate 			prtime(" lines transferred in ", stop_t-start_t);
374549Smuffin 	(void) write(fildes[1], (char *)&ccc, 1);
3750Sstevel@tonic-gate 	intr("off");
376549Smuffin 	(void) signal(SIGINT, ointr);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate  * Cu-like put command
3810Sstevel@tonic-gate  */
382549Smuffin /* ARGSUSED */
383549Smuffin void
cu_put(int cc)384549Smuffin cu_put(int cc)
3850Sstevel@tonic-gate {
3860Sstevel@tonic-gate 	FILE *fd;
3870Sstevel@tonic-gate 	char line[BUFSIZ];
3880Sstevel@tonic-gate 	int argc;
3890Sstevel@tonic-gate 	char *copynamex;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	if (prompt("[put] ", copyname, sizeof (copyname)))
3920Sstevel@tonic-gate 		return;
3930Sstevel@tonic-gate 	argc = args(copyname, argv, sizeof (argv)/sizeof (char *));
3940Sstevel@tonic-gate 	if (argc < 1 || argc > 2) {
395549Smuffin 		(void) printf("usage: <put> from [to]\r\n");
3960Sstevel@tonic-gate 		return;
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 	if (argc == 1)
3990Sstevel@tonic-gate 		argv[1] = argv[0];
4000Sstevel@tonic-gate 	copynamex = expand(argv[0]);
4010Sstevel@tonic-gate 	if (copynamex == NOSTR)
4020Sstevel@tonic-gate 		return;
4030Sstevel@tonic-gate 	if ((fd = fopen(copynamex, "r")) == NULL) {
404549Smuffin 		(void) printf("%s: cannot open\r\n", copynamex);
4050Sstevel@tonic-gate 		return;
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 	if (boolean(value(ECHOCHECK)))
408549Smuffin 		(void) snprintf(line, sizeof (line), "cat>%s\r", argv[1]);
4090Sstevel@tonic-gate 	else
410549Smuffin 		(void) snprintf(line, sizeof (line),
411549Smuffin 		    "stty -echo; cat>%s; stty echo\r", argv[1]);
4120Sstevel@tonic-gate 	transmit(fd, "\04", line);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate  * FTP - send single character
4170Sstevel@tonic-gate  *  wait for echo & handle timeout
4180Sstevel@tonic-gate  */
419549Smuffin void
send(char c)420549Smuffin send(char c)
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	char cc;
4230Sstevel@tonic-gate 	int retry = 0;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	cc = c;
426549Smuffin 	parwrite(FD, (unsigned char *)&cc, 1);
4270Sstevel@tonic-gate #ifdef notdef
4280Sstevel@tonic-gate 	if (number(value(CDELAY)) > 0 && c != '\r')
4290Sstevel@tonic-gate 		nap(number(value(CDELAY)));
4300Sstevel@tonic-gate #endif
4310Sstevel@tonic-gate 	if (!boolean(value(ECHOCHECK))) {
4320Sstevel@tonic-gate #ifdef notdef
4330Sstevel@tonic-gate 		if (number(value(LDELAY)) > 0 && c == '\r')
4340Sstevel@tonic-gate 			nap(number(value(LDELAY)));
4350Sstevel@tonic-gate #endif
4360Sstevel@tonic-gate 		return;
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate tryagain:
4390Sstevel@tonic-gate 	timedout = 0;
4400Sstevel@tonic-gate 	if (sigsetjmp(intbuf, 1) && timedout) {
441549Smuffin 		(void) printf("\r\ntimeout error (%s)\r\n", ctrl(c));
4420Sstevel@tonic-gate 		if (retry++ > 3)
4430Sstevel@tonic-gate 			return;
444549Smuffin 		parwrite(FD, (unsigned char *)&null, 1); /* poke it */
4450Sstevel@tonic-gate 		goto tryagain;
4460Sstevel@tonic-gate 	}
447549Smuffin 	(void) alarm(number(value(ETIMEOUT)));
448549Smuffin 	(void) read(FD, &cc, 1);
449549Smuffin 	(void) alarm(0);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate void
timeout(void)453549Smuffin timeout(void)
4540Sstevel@tonic-gate {
455549Smuffin 	(void) signal(SIGALRM, (sig_handler_t)timeout);
4560Sstevel@tonic-gate 	timedout = 1;
4570Sstevel@tonic-gate 	siglongjmp(intbuf, 1);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * Stolen from consh() -- puts a remote file on the output of a local command.
4620Sstevel@tonic-gate  *	Identical to consh() except for where stdout goes.
4630Sstevel@tonic-gate  */
464549Smuffin void
pipeout(int c)465549Smuffin pipeout(int c)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	char buf[256];
4680Sstevel@tonic-gate 	int cpid, status, p;
4690Sstevel@tonic-gate 	time_t start;
4700Sstevel@tonic-gate 
471549Smuffin 	(void) putchar(c);
4720Sstevel@tonic-gate 	if (prompt("Local command? ", buf, sizeof (buf)))
4730Sstevel@tonic-gate 		return;
474549Smuffin 	(void) kill(pid, SIGIOT);	/* put TIPOUT into a wait state */
475549Smuffin 	(void) signal(SIGINT, SIG_IGN);
476549Smuffin 	(void) signal(SIGQUIT, SIG_IGN);
4770Sstevel@tonic-gate 	intr("on");
478549Smuffin 	(void) read(repdes[0], (char *)&ccc, 1);
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * Set up file descriptors in the child and
4810Sstevel@tonic-gate 	 *  let it go...
4820Sstevel@tonic-gate 	 */
4830Sstevel@tonic-gate 	if ((cpid = fork()) < 0)
484549Smuffin 		(void) printf("can't fork!\r\n");
4850Sstevel@tonic-gate 	else if (cpid) {
4860Sstevel@tonic-gate 		start = time(0);
4870Sstevel@tonic-gate 		while ((p = wait(&status)) > 0 && p != cpid)
4880Sstevel@tonic-gate 			;
4890Sstevel@tonic-gate 	} else {
490549Smuffin 		int i;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		userperm();
493549Smuffin 		(void) dup2(FD, 1);
4940Sstevel@tonic-gate 		for (i = 3; i < 20; i++)
495549Smuffin 			(void) close(i);
496549Smuffin 		(void) signal(SIGINT, SIG_DFL);
497549Smuffin 		(void) signal(SIGQUIT, SIG_DFL);
4980Sstevel@tonic-gate 		execute(buf);
499549Smuffin 		(void) printf("can't find `%s'\r\n", buf);
5000Sstevel@tonic-gate 		exit(0);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
5030Sstevel@tonic-gate 		prtime("away for ", time(0)-start);
504549Smuffin 	(void) write(fildes[1], (char *)&ccc, 1);
5050Sstevel@tonic-gate 	intr("off");
506549Smuffin 	(void) signal(SIGINT, SIG_DFL);
507549Smuffin 	(void) signal(SIGQUIT, SIG_DFL);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate /*
5110Sstevel@tonic-gate  * Fork a program with:
5120Sstevel@tonic-gate  *  0 <-> remote tty in
5130Sstevel@tonic-gate  *  1 <-> remote tty out
5140Sstevel@tonic-gate  *  2 <-> local tty stderr out
5150Sstevel@tonic-gate  */
516549Smuffin void
consh(int c)517549Smuffin consh(int c)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate 	char buf[256];
5200Sstevel@tonic-gate 	int cpid, status, p;
521549Smuffin 	sig_handler_t	ointr, oquit;
5220Sstevel@tonic-gate 	time_t start;
5230Sstevel@tonic-gate 
524549Smuffin 	(void) putchar(c);
5250Sstevel@tonic-gate 	if (prompt("Local command? ", buf, sizeof (buf)))
5260Sstevel@tonic-gate 		return;
527549Smuffin 	(void) kill(pid, SIGIOT);	/* put TIPOUT into a wait state */
528549Smuffin 	(void) read(repdes[0], (char *)&ccc, 1);
5290Sstevel@tonic-gate 	ointr = signal(SIGINT, SIG_IGN);
5300Sstevel@tonic-gate 	oquit = signal(SIGQUIT, SIG_IGN);
5310Sstevel@tonic-gate 	unraw();
5320Sstevel@tonic-gate 	/*
5330Sstevel@tonic-gate 	 * Set up file descriptors in the child and
5340Sstevel@tonic-gate 	 *  let it go...
5350Sstevel@tonic-gate 	 */
5360Sstevel@tonic-gate 	if ((cpid = fork()) < 0)
537549Smuffin 		(void) printf("can't fork!\r\n");
5380Sstevel@tonic-gate 	else if (cpid) {
5390Sstevel@tonic-gate 		start = time(0);
5400Sstevel@tonic-gate 		while ((p = wait(&status)) > 0 && p != cpid)
5410Sstevel@tonic-gate 			;
5420Sstevel@tonic-gate 		raw();
543549Smuffin 		(void) signal(SIGINT, ointr);
544549Smuffin 		(void) signal(SIGQUIT, oquit);
5450Sstevel@tonic-gate 	} else {
546549Smuffin 		int i;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 		userperm();
549549Smuffin 		(void) dup2(FD, 0);
550549Smuffin 		(void) dup2(0, 1);
5510Sstevel@tonic-gate 		for (i = 3; i < 20; i++)
552549Smuffin 			(void) close(i);
553549Smuffin 		(void) signal(SIGINT, SIG_DFL);
554549Smuffin 		(void) signal(SIGQUIT, SIG_DFL);
5550Sstevel@tonic-gate 		execute(buf);
556549Smuffin 		(void) printf("can't find `%s'\r\n", buf);
5570Sstevel@tonic-gate 		exit(0);
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
5600Sstevel@tonic-gate 		prtime("\r\naway for ", time(0)-start);
561549Smuffin 	(void) write(fildes[1], (char *)&ccc, 1);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * Escape to local shell
5660Sstevel@tonic-gate  */
567549Smuffin /* ARGSUSED */
568549Smuffin void
shell(int cc)569549Smuffin shell(int cc)
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate 	int shpid, status;
572549Smuffin 	sig_handler_t	ointr, oquit;
5730Sstevel@tonic-gate 	char *cp;
5740Sstevel@tonic-gate 
575549Smuffin 	(void) printf("[sh]\r\n");
5760Sstevel@tonic-gate 	ointr = signal(SIGINT, SIG_IGN);
5770Sstevel@tonic-gate 	oquit = signal(SIGQUIT, SIG_IGN);
5780Sstevel@tonic-gate 	unraw();
5790Sstevel@tonic-gate 	if (shpid = fork()) {
5800Sstevel@tonic-gate 		while (shpid != wait(&status))
5810Sstevel@tonic-gate 			;
5820Sstevel@tonic-gate 		raw();
583549Smuffin 		(void) printf("\r\n!\r\n");
584549Smuffin 		(void) signal(SIGINT, ointr);
585549Smuffin 		(void) signal(SIGQUIT, oquit);
5860Sstevel@tonic-gate 	} else {
5870Sstevel@tonic-gate 		userperm();
588549Smuffin 		(void) signal(SIGQUIT, SIG_DFL);
589549Smuffin 		(void) signal(SIGINT, SIG_DFL);
5900Sstevel@tonic-gate 		if ((cp = strrchr(value(SHELL), '/')) == NULL)
5910Sstevel@tonic-gate 			cp = value(SHELL);
5920Sstevel@tonic-gate 		else
5930Sstevel@tonic-gate 			cp++;
594549Smuffin 		(void) execl(value(SHELL), cp, 0);
595549Smuffin 		(void) printf("\r\ncan't execl!\r\n");
5960Sstevel@tonic-gate 		exit(1);
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate /*
6010Sstevel@tonic-gate  * TIPIN portion of scripting
6020Sstevel@tonic-gate  *   initiate the conversation with TIPOUT
6030Sstevel@tonic-gate  */
604549Smuffin void
setscript(void)605549Smuffin setscript(void)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	char c;
608*2590Ssn199410 
609*2590Ssn199410 	if (strlen(value(RECORD)) >= PATH_MAX-1) {
610*2590Ssn199410 		(void) fprintf(stderr, "tip: record file name too long\r\n");
611*2590Ssn199410 		return;
612*2590Ssn199410 	}
6130Sstevel@tonic-gate 	/*
6140Sstevel@tonic-gate 	 * enable TIPOUT side for dialogue
6150Sstevel@tonic-gate 	 */
616549Smuffin 	(void) kill(pid, SIGEMT);
6170Sstevel@tonic-gate 	if (boolean(value(SCRIPT)))
618549Smuffin 		(void) write(fildes[1], value(RECORD), strlen(value(RECORD)));
619549Smuffin 	(void) write(fildes[1], "\n", 1);
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * wait for TIPOUT to finish
6220Sstevel@tonic-gate 	 */
623549Smuffin 	(void) read(repdes[0], &c, 1);
6240Sstevel@tonic-gate 	if (c == 'n')
625549Smuffin 		(void) fprintf(stderr, "tip: can't create record file %s\r\n",
6260Sstevel@tonic-gate 		    value(RECORD));
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate  * Change current working directory of
6310Sstevel@tonic-gate  *   local portion of tip
6320Sstevel@tonic-gate  */
633549Smuffin /* ARGSUSED */
634549Smuffin void
chdirectory(int cc)635549Smuffin chdirectory(int cc)
6360Sstevel@tonic-gate {
6370Sstevel@tonic-gate 	char dirname[80];
638549Smuffin 	char *cp = dirname;
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if (prompt("[cd] ", dirname, sizeof (dirname))) {
6410Sstevel@tonic-gate 		if (stoprompt)
6420Sstevel@tonic-gate 			return;
6430Sstevel@tonic-gate 		cp = value(HOME);
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 	if (chdir(cp) < 0)
646549Smuffin 		(void) printf("%s: bad directory\r\n", cp);
647549Smuffin 	(void) printf("!\r\n");
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
650549Smuffin void
tip_abort(char * msg)651549Smuffin tip_abort(char *msg)
6520Sstevel@tonic-gate {
653549Smuffin 	/* don't want to hear about our child */
654549Smuffin 	(void) signal(SIGCHLD, SIG_DFL);
655549Smuffin 	(void) kill(pid, SIGTERM);
6560Sstevel@tonic-gate 	myperm();
6570Sstevel@tonic-gate 	disconnect(msg);
6580Sstevel@tonic-gate 	if (msg != NOSTR)
659549Smuffin 		(void) printf("\r\n%s", msg);
660549Smuffin 	(void) printf("\r\n[EOT]\r\n");
6610Sstevel@tonic-gate 	delock(uucplock);
6620Sstevel@tonic-gate 	unraw();
6630Sstevel@tonic-gate 	exit(0);
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
666549Smuffin /* ARGSUSED */
667549Smuffin void
finish(int cc)668549Smuffin finish(int cc)
6690Sstevel@tonic-gate {
6700Sstevel@tonic-gate 	char *dismsg;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if ((dismsg = value(DISCONNECT)) != NOSTR) {
673549Smuffin 		(void) write(FD, dismsg, strlen(dismsg));
674549Smuffin 		(void) sleep(5);
6750Sstevel@tonic-gate 	}
676549Smuffin 	tip_abort(NOSTR);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate void
intcopy(void)680549Smuffin intcopy(void)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 
683549Smuffin 	(void) signal(SIGINT, SIG_IGN);
6840Sstevel@tonic-gate 	siglongjmp(intbuf, 1);
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
687549Smuffin void
execute(char * s)688549Smuffin execute(char *s)
6890Sstevel@tonic-gate {
690549Smuffin 	char *cp;
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	if ((cp = strrchr(value(SHELL), '/')) == NULL)
6930Sstevel@tonic-gate 		cp = value(SHELL);
6940Sstevel@tonic-gate 	else
6950Sstevel@tonic-gate 		cp++;
696549Smuffin 	(void) execl(value(SHELL), cp, "-c", s, 0);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
699549Smuffin int
args(char * buf,char * a[],size_t na)700549Smuffin args(char *buf, char *a[], size_t na)
7010Sstevel@tonic-gate {
702549Smuffin 	char *p = buf, *start;
703549Smuffin 	char **parg = a;
704549Smuffin 	int n = 0;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	do {
7070Sstevel@tonic-gate 		while (*p && (*p == ' ' || *p == '\t'))
7080Sstevel@tonic-gate 			p++;
7090Sstevel@tonic-gate 		start = p;
7100Sstevel@tonic-gate 		if (*p)
7110Sstevel@tonic-gate 			*parg = p;
7120Sstevel@tonic-gate 		while (*p && (*p != ' ' && *p != '\t'))
7130Sstevel@tonic-gate 			p++;
7140Sstevel@tonic-gate 		if (p != start)
7150Sstevel@tonic-gate 			parg++, n++;
7160Sstevel@tonic-gate 		if (*p)
7170Sstevel@tonic-gate 			*p++ = '\0';
7180Sstevel@tonic-gate 	} while (*p && n < na);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	return (n);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
723549Smuffin void
prtime(char * s,time_t a)724549Smuffin prtime(char *s, time_t a)
7250Sstevel@tonic-gate {
726549Smuffin 	int i;
7270Sstevel@tonic-gate 	int nums[3];
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	for (i = 0; i < 3; i++) {
7300Sstevel@tonic-gate 		nums[i] = (int)(a % quant[i]);
7310Sstevel@tonic-gate 		a /= quant[i];
7320Sstevel@tonic-gate 	}
733549Smuffin 	(void) printf("%s", s);
7340Sstevel@tonic-gate 	while (--i >= 0)
7350Sstevel@tonic-gate 		if (nums[i] || i == 0 && nums[1] == 0 && nums[2] == 0)
736549Smuffin 			(void) printf("%d %s%c ", nums[i], sep[i],
737549Smuffin 			    nums[i] == 1 ? '\0' : 's');
738549Smuffin 	(void) printf("\r\n!\r\n");
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate 
741549Smuffin /* ARGSUSED */
742549Smuffin void
variable(int cc)743549Smuffin variable(int cc)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate 	char	buf[256];
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	if (prompt("[set] ", buf, sizeof (buf)))
7480Sstevel@tonic-gate 		return;
7490Sstevel@tonic-gate 	vlex(buf);
7500Sstevel@tonic-gate 	if (vtable[BEAUTIFY].v_access&CHANGED) {
7510Sstevel@tonic-gate 		vtable[BEAUTIFY].v_access &= ~CHANGED;
752549Smuffin 		(void) kill(pid, SIGSYS);
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 	if (vtable[SCRIPT].v_access&CHANGED) {
7550Sstevel@tonic-gate 		vtable[SCRIPT].v_access &= ~CHANGED;
7560Sstevel@tonic-gate 		setscript();
7570Sstevel@tonic-gate 		/*
7580Sstevel@tonic-gate 		 * So that "set record=blah script" doesn't
7590Sstevel@tonic-gate 		 *  cause two transactions to occur.
7600Sstevel@tonic-gate 		 */
7610Sstevel@tonic-gate 		if (vtable[RECORD].v_access&CHANGED)
7620Sstevel@tonic-gate 			vtable[RECORD].v_access &= ~CHANGED;
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 	if (vtable[RECORD].v_access&CHANGED) {
7650Sstevel@tonic-gate 		vtable[RECORD].v_access &= ~CHANGED;
7660Sstevel@tonic-gate 		if (boolean(value(SCRIPT)))
7670Sstevel@tonic-gate 			setscript();
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 	if (vtable[TAND].v_access&CHANGED) {
7700Sstevel@tonic-gate 		vtable[TAND].v_access &= ~CHANGED;
7710Sstevel@tonic-gate 		if (boolean(value(TAND)))
7720Sstevel@tonic-gate 			tandem("on");
7730Sstevel@tonic-gate 		else
7740Sstevel@tonic-gate 			tandem("off");
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 	if (vtable[LECHO].v_access&CHANGED) {
7770Sstevel@tonic-gate 		vtable[LECHO].v_access &= ~CHANGED;
7780Sstevel@tonic-gate 		boolean(value(HALFDUPLEX)) = boolean(value(LECHO));
7790Sstevel@tonic-gate 	}
7800Sstevel@tonic-gate 	if (vtable[PARITY].v_access&CHANGED) {
7810Sstevel@tonic-gate 		vtable[PARITY].v_access &= ~CHANGED;
7820Sstevel@tonic-gate 		setparity(NULL);
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 	if (vtable[BAUDRATE].v_access&CHANGED) {
7850Sstevel@tonic-gate 		vtable[BAUDRATE].v_access &= ~CHANGED;
7860Sstevel@tonic-gate 		ttysetup(speed(number(value(BAUDRATE))));
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 	if (vtable[HARDWAREFLOW].v_access & CHANGED) {
7890Sstevel@tonic-gate 		vtable[HARDWAREFLOW].v_access &= ~CHANGED;
7900Sstevel@tonic-gate 		if (boolean(value(HARDWAREFLOW)))
7910Sstevel@tonic-gate 			hardwareflow("on");
7920Sstevel@tonic-gate 		else
7930Sstevel@tonic-gate 			hardwareflow("off");
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate /*
7980Sstevel@tonic-gate  * Turn tandem mode on or off for remote tty.
7990Sstevel@tonic-gate  */
800549Smuffin void
tandem(char * option)801549Smuffin tandem(char *option)
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate 	struct termios rmtty;
8040Sstevel@tonic-gate 
805549Smuffin 	(void) ioctl(FD, TCGETS, (char *)&rmtty);
8060Sstevel@tonic-gate 	if (equal(option, "on")) {
8070Sstevel@tonic-gate 		rmtty.c_iflag |= IXOFF|IXON;
8080Sstevel@tonic-gate 		arg.c_iflag |= IXOFF|IXON;
8090Sstevel@tonic-gate 		rmtty.c_cc[VSTART] = defarg.c_cc[VSTART];
8100Sstevel@tonic-gate 		rmtty.c_cc[VSTOP] = defarg.c_cc[VSTOP];
8110Sstevel@tonic-gate 	} else {
8120Sstevel@tonic-gate 		rmtty.c_iflag &= ~(IXOFF|IXON);
8130Sstevel@tonic-gate 		arg.c_iflag &= ~(IXOFF|IXON);
8140Sstevel@tonic-gate 	}
815549Smuffin 	(void) ioctl(FD, TCSETSF, (char *)&rmtty);
816549Smuffin 	(void) ioctl(0, TCSETSF, (char *)&arg);
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate /*
8200Sstevel@tonic-gate  * Turn hardwareflow mode on or off for remote tty.
8210Sstevel@tonic-gate  */
822549Smuffin void
hardwareflow(char * option)823549Smuffin hardwareflow(char *option)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 	struct termios rmtty;
8260Sstevel@tonic-gate 
827549Smuffin 	(void) ioctl(FD, TCGETS, (char *)&rmtty);
8280Sstevel@tonic-gate 	if (equal(option, "on")) {
8290Sstevel@tonic-gate 		rmtty.c_cflag |= (CRTSCTS|CRTSXOFF);
8300Sstevel@tonic-gate 	} else {
8310Sstevel@tonic-gate 		rmtty.c_cflag &= ~(CRTSCTS|CRTSXOFF);
8320Sstevel@tonic-gate 	}
833549Smuffin 	(void) ioctl(FD, TCSETSF, (char *)&rmtty);
8340Sstevel@tonic-gate }
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate /*
8370Sstevel@tonic-gate  * Turn interrupts from local tty on or off.
8380Sstevel@tonic-gate  */
839549Smuffin void
intr(char * option)840549Smuffin intr(char *option)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	if (equal(option, "on"))
8440Sstevel@tonic-gate 		arg.c_lflag |= ISIG;
8450Sstevel@tonic-gate 	else
8460Sstevel@tonic-gate 		arg.c_lflag &= ~ISIG;
847549Smuffin 	(void) ioctl(0, TCSETSF, (char *)&arg);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate /*
8510Sstevel@tonic-gate  * Send a break.
8520Sstevel@tonic-gate  */
853549Smuffin /* ARGSUSED */
854549Smuffin void
genbrk(int cc)855549Smuffin genbrk(int cc)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 
858549Smuffin 	(void) ioctl(FD, TCSBRK, 0);
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate /*
8620Sstevel@tonic-gate  * Suspend tip
8630Sstevel@tonic-gate  */
864549Smuffin void
suspend(int c)865549Smuffin suspend(int c)
8660Sstevel@tonic-gate {
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	unraw();
869549Smuffin 	(void) kill(c == _CTRL('y') ? getpid() : 0, SIGTSTP);
8700Sstevel@tonic-gate 	raw();
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate /*
8740Sstevel@tonic-gate  *	expand a file name if it includes shell meta characters
8750Sstevel@tonic-gate  */
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate char *
expand(char name[])878549Smuffin expand(char name[])
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	static char xname[BUFSIZ];
8810Sstevel@tonic-gate 	char cmdbuf[BUFSIZ];
882549Smuffin 	int pid, l;
883549Smuffin 	char *cp, *Shell;
8840Sstevel@tonic-gate 	int s, pivec[2];
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	if (!anyof(name, "~{[*?$`'\"\\"))
8870Sstevel@tonic-gate 		return (name);
8880Sstevel@tonic-gate 	if (pipe(pivec) < 0) {
8890Sstevel@tonic-gate 		perror("pipe");
8900Sstevel@tonic-gate 		return (name);
8910Sstevel@tonic-gate 	}
892549Smuffin 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "echo %s", name);
8930Sstevel@tonic-gate 	if ((pid = vfork()) == 0) {
8940Sstevel@tonic-gate 		userperm();
8950Sstevel@tonic-gate 		Shell = value(SHELL);
8960Sstevel@tonic-gate 		if (Shell == NOSTR)
8970Sstevel@tonic-gate 			Shell = "/bin/sh";
898549Smuffin 		(void) close(pivec[0]);
899549Smuffin 		(void) close(1);
900549Smuffin 		(void) dup(pivec[1]);
901549Smuffin 		(void) close(pivec[1]);
902549Smuffin 		(void) close(2);
903549Smuffin 		(void) execl(Shell, Shell, "-c", cmdbuf, 0);
9040Sstevel@tonic-gate 		_exit(1);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 	if (pid == -1) {
9070Sstevel@tonic-gate 		perror("fork");
908549Smuffin 		(void) close(pivec[0]);
909549Smuffin 		(void) close(pivec[1]);
9100Sstevel@tonic-gate 		return (NOSTR);
9110Sstevel@tonic-gate 	}
912549Smuffin 	(void) close(pivec[1]);
9130Sstevel@tonic-gate 	l = read(pivec[0], xname, BUFSIZ);
914549Smuffin 	(void) close(pivec[0]);
915549Smuffin 	while (wait(&s) != pid)
9160Sstevel@tonic-gate 		;
9170Sstevel@tonic-gate 	s &= 0377;
9180Sstevel@tonic-gate 	if (s != 0 && s != SIGPIPE) {
919549Smuffin 		(void) fprintf(stderr, "\"Echo\" failed\n");
9200Sstevel@tonic-gate 		return (NOSTR);
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate 	if (l < 0) {
9230Sstevel@tonic-gate 		perror("read");
9240Sstevel@tonic-gate 		return (NOSTR);
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 	if (l == 0) {
927549Smuffin 		(void) fprintf(stderr, "\"%s\": No match\n", name);
9280Sstevel@tonic-gate 		return (NOSTR);
9290Sstevel@tonic-gate 	}
9300Sstevel@tonic-gate 	if (l == BUFSIZ) {
931549Smuffin 		(void) fprintf(stderr, "Buffer overflow expanding \"%s\"\n",
932549Smuffin 		    name);
9330Sstevel@tonic-gate 		return (NOSTR);
9340Sstevel@tonic-gate 	}
9350Sstevel@tonic-gate 	xname[l] = 0;
9360Sstevel@tonic-gate 	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
9370Sstevel@tonic-gate 		;
9380Sstevel@tonic-gate 	*++cp = '\0';
9390Sstevel@tonic-gate 	return (xname);
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate /*
9430Sstevel@tonic-gate  * Are any of the characters in the two strings the same?
9440Sstevel@tonic-gate  */
9450Sstevel@tonic-gate 
946549Smuffin int
anyof(char * s1,char * s2)947549Smuffin anyof(char *s1, char *s2)
9480Sstevel@tonic-gate {
949549Smuffin 	int c;
9500Sstevel@tonic-gate 
951549Smuffin 	while ((c = *s1++) != 0)
9520Sstevel@tonic-gate 		if (any(c, s2))
9530Sstevel@tonic-gate 			return (1);
9540Sstevel@tonic-gate 	return (0);
9550Sstevel@tonic-gate }
956