summaryrefslogtreecommitdiff
path: root/usr.bin/tip/cmds.c
blob: c7c962888624e3dae3572d46630e22605bf04373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/*	$OpenBSD: cmds.c,v 1.42 2010/07/02 07:32:16 nicm Exp $	*/
/*	$NetBSD: cmds.c,v 1.7 1997/02/11 09:24:03 mrg Exp $	*/

/*
 * Copyright (c) 1983, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "tip.h"

#include <paths.h>
#include <util.h>
#include <vis.h>

/*
 * tip
 *
 * miscellaneous commands
 */

int	quant[] = { 60, 60, 24 };

char	null = '\0';
char	*sep[] = { "second", "minute", "hour" };
static char *argv[10];		/* argument vector for take and put */

static void	transfer(char *, int, char *);
static void	stopsnd(int);	/* SIGINT handler during file transfers */
static void	intcopy(int);	/* interrupt routine for file transfers */
static void	transmit(FILE *, char *, char *);
static void	send(int);
static void	execute(char *);
static int	args(char *, char **, int);
static void	prtime(char *, time_t);
static void	tandem(char *);
static void	hardwareflow(char *);
void		linedisc(char *);
static int	anyof(char *, char *);

/*
 * FTP - remote ==> local
 *  get a file from the remote host
 */
void
getfl(int c)
{
	char buf[256], *cp;

	putchar(c);
	/*
	 * get the UNIX receiving file's name
	 */
	if (prompt("Local file name? ", copyname, sizeof(copyname)))
		return;
	cp = expand(copyname);
	if ((sfd = creat(cp, 0666)) < 0) {
		printf("\r\n%s: cannot creat\r\n", copyname);
		return;
	}

	/*
	 * collect parameters
	 */
	if (prompt("List command for remote system? ", buf, sizeof(buf))) {
		unlink(copyname);
		return;
	}
	transfer(buf, sfd, vgetstr(EOFREAD));
}

/*
 * Cu-like take command
 */
void
cu_take(int c)
{
	int fd, argc;
	char line[BUFSIZ], *cp;

	if (prompt("[take] ", copyname, sizeof(copyname)))
		return;
	if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 ||
	    argc > 2) {
		printf("usage: <take> from [to]\r\n");
		return;
	}
	if (argc == 1)
		argv[1] = argv[0];
	cp = expand(argv[1]);
	if ((fd = creat(cp, 0666)) < 0) {
		printf("\r\n%s: cannot create\r\n", argv[1]);
		return;
	}
	(void)snprintf(line, sizeof(line), "cat %s;echo ''|tr '\\012' '\\01'", argv[0]);
	transfer(line, fd, "\01");
}

static	jmp_buf intbuf;

/*
 * Bulk transfer routine --
 *  used by getfl(), cu_take(), and pipefile()
 */
static void
transfer(char *buf, int fd, char *eofchars)
{
	int ct, eof;
	char c, buffer[BUFSIZ];
	char *p = buffer;
	size_t cnt;
	time_t start;
	sig_t f;
	char r;

	if (vgetnum(FRAMESIZE) > BUFSIZ || vgetnum(FRAMESIZE) < 1) {
		printf("framesize must be >= 1 and <= %d\r\n", BUFSIZ);
		close(fd);
		return;
	}

	parwrite(FD, buf, size(buf));
	quit = 0;
	write(tipout_fd, "W", 1);
	read(tipout_fd, (char *)&ccc, 1);  /* Wait until read process stops */

	/*
	 * finish command
	 */
	r = '\r';
	parwrite(FD, &r, 1);
	do
		read(FD, &c, 1);
	while ((c&STRIP_PAR) != '\n');
	tcsetattr(0, TCSAFLUSH, &defchars);

	(void) setjmp(intbuf);
	f = signal(SIGINT, intcopy);
	start = time(0);
	for (ct = 0; !quit;) {
		eof = read(FD, &c, 1) <= 0;
		c &= STRIP_PAR;
		if (quit)
			continue;
		if (eof || any(c, eofchars))
			break;
		if (c == 0)
			continue;	/* ignore nulls */
		if (c == '\r')
			continue;
		*p++ = c;

		if (c == '\n' && vgetnum(VERBOSE))
			printf("\r%d", ++ct);
		if ((cnt = (p-buffer)) == vgetnum(FRAMESIZE)) {
			if (write(fd, buffer, cnt) != cnt) {
				printf("\r\nwrite error\r\n");
				quit = 1;
			}
			p = buffer;
		}
	}
	if ((cnt = (p-buffer)))
		if (write(fd, buffer, cnt) != cnt)
			printf("\r\nwrite error\r\n");

	if (vgetnum(VERBOSE))
		prtime(" lines transferred in ", time(0)-start);
	tcsetattr(0, TCSAFLUSH, &term);
	write(tipout_fd, (char *)&ccc, 1);
	signal(SIGINT, f);
	close(fd);
}

/*
 * FTP - remote ==> local process
 *   send remote input to local process via pipe
 */
/*ARGSUSED*/
void
pipefile(int c)
{
	int pdes[2];
	char buf[256];
	int status, p;
	pid_t cpid;

	if (prompt("Local command? ", buf, sizeof(buf)))
		return;

	if (pipe(pdes)) {
		printf("can't establish pipe\r\n");
		return;
	}

	if ((cpid = fork()) < 0) {
		printf("can't fork!\r\n");
		return;
	} else if (cpid) {
		if (prompt("List command for remote system? ", buf, sizeof(buf))) {
			close(pdes[0]), close(pdes[1]);
			kill (cpid, SIGKILL);
		} else {
			close(pdes[0]);
			signal(SIGPIPE, intcopy);
			transfer(buf, pdes[1], vgetstr(EOFREAD));
			signal(SIGPIPE, SIG_DFL);
			while ((p = wait(&status)) > 0 && p != cpid)
				;
		}
	} else {
		int f;

		dup2(pdes[0], 0);
		close(pdes[0]);
		for (f = 3; f < 20; f++)
			close(f);
		execute(buf);
		printf("can't execl!\r\n");
		exit(0);
	}
}

/*
 * Interrupt service routine for FTP
 */
/*ARGSUSED*/
static void
stopsnd(int signo)
{
	stop = 1;
	signal(SIGINT, SIG_IGN);
}

/*
 * FTP - local ==> remote
 *  send local file to remote host
 *  terminate transmission with pseudo EOF sequence
 */
void
sendfile(int c)
{
	FILE *fp;
	char *fnamex;

	putchar(c);
	/*
	 * get file name
	 */
	if (prompt("Local file name? ", fname, sizeof(fname)))
		return;

	/*
	 * look up file
	 */
	fnamex = expand(fname);
	if ((fp = fopen(fnamex, "r")) == NULL) {
		printf("%s: cannot open\r\n", fname);
		return;
	}
	transmit(fp, vgetstr(EOFWRITE), NULL);
	if (!vgetnum(ECHOCHECK))
		tcdrain(FD);
}

/*
 * Bulk transfer routine to remote host --
 *   used by sendfile() and cu_put()
 */
static void
transmit(FILE *fp, char *eofchars, char *command)
{
	char *pc, lastc;
	int c, ccount, lcount;
	time_t start_t, stop_t;
	sig_t f;

	write(tipout_fd, "W", 1);	/* put TIPOUT into a wait state */
	stop = 0;
	f = signal(SIGINT, stopsnd);
	tcsetattr(0, TCSAFLUSH, &defchars);
	read(tipout_fd, (char *)&ccc, 1);
	if (command != NULL) {
		for (pc = command; *pc; pc++)
			send(*pc);
		if (vgetnum(ECHOCHECK))
			read(FD, (char *)&c, 1);	/* trailing \n */
		else {
			tcdrain(FD);
			sleep(5); /* wait for remote stty to take effect */
		}
	}
	lcount = 0;
	lastc = '\0';
	start_t = time(0);
	while (1) {
		ccount = 0;
		do {
			c = getc(fp);
			if (stop)
				goto out;
			if (c == EOF)
				goto out;
			if (c == 0177 && !vgetnum(RAWFTP))
				continue;
			lastc = c;
			if (c < 040) {
				if (c == '\n') {
					if (!vgetnum(RAWFTP))
						c = '\r';
				} else if (c == '\t') {
					if (!vgetnum(RAWFTP)) {
						if (vgetnum(TABEXPAND)) {
							send(' ');
							while ((++ccount % 8) != 0)
								send(' ');
							continue;
						}
					}
				} else
					if (!vgetnum(RAWFTP))
						continue;
			}
			send(c);
		} while (c != '\r' && !vgetnum(RAWFTP));
		if (vgetnum(VERBOSE))
			printf("\r%d", ++lcount);
		if (vgetnum(ECHOCHECK)) {
			timedout = 0;
			alarm((unsigned int)vgetnum(ETIMEOUT));
			do {	/* wait for prompt */
				read(FD, (char *)&c, 1);
				if (timedout || stop) {
					if (timedout)
						printf("\r\ntimed out at eol\r\n");
					alarm(0);
					goto out;
				}
			} while ((c&STRIP_PAR) != vgetnum(PROMPT));
			alarm(0);
		}
	}
out:
	if (lastc != '\n' && !vgetnum(RAWFTP))
		send('\r');
	if (eofchars) {
		for (pc = eofchars; *pc; pc++)
			send(*pc);
	}
	stop_t = time(0);
	fclose(fp);
	signal(SIGINT, f);
	if (vgetnum(VERBOSE)) {
		if (vgetnum(RAWFTP))
			prtime(" chars transferred in ", stop_t-start_t);
		else
			prtime(" lines transferred in ", stop_t-start_t);
	}
	write(tipout_fd, (char *)&ccc, 1);
	tcsetattr(0, TCSAFLUSH, &term);
}

/*
 * Cu-like put command
 */
/*ARGSUSED*/
void
cu_put(int c)
{
	FILE *fp;
	char line[BUFSIZ];
	int argc;
	char *copynamex;

	if (prompt("[put] ", copyname, sizeof(copyname)))
		return;
	if ((argc = args(copyname, argv, sizeof(argv)/sizeof(argv[0]))) < 1 ||
	    argc > 2) {
		printf("usage: <put> from [to]\r\n");
		return;
	}
	if (argc == 1)
		argv[1] = argv[0];
	copynamex = expand(argv[0]);
	if ((fp = fopen(copynamex, "r")) == NULL) {
		printf("%s: cannot open\r\n", copynamex);
		return;
	}
	if (vgetnum(ECHOCHECK))
		(void)snprintf(line, sizeof(line), "cat>%s\r", argv[1]);
	else
		(void)snprintf(line, sizeof(line),
		    "stty -echo;cat>%s;stty echo\r", argv[1]);
	transmit(fp, "\04", line);
}

/*
 * FTP - send single character
 *  wait for echo & handle timeout
 */
static void
send(int c)
{
	char cc;
	int retry = 0;

	cc = c;
	parwrite(FD, &cc, 1);
	if (vgetnum(CDELAY) > 0 && c != '\r')
		usleep(vgetnum(CDELAY));
	if (!vgetnum(ECHOCHECK)) {
		if (vgetnum(LDELAY) > 0 && c == '\r')
			usleep(vgetnum(LDELAY));
		return;
	}
tryagain:
	timedout = 0;
	alarm((unsigned int)vgetnum(ETIMEOUT));
	read(FD, &cc, 1);
	alarm(0);
	if (timedout) {
		printf("\r\ntimeout error (%s)\r\n", ctrl(c));
		if (retry++ > 3)
			return;
		parwrite(FD, &null, 1); /* poke it */
		goto tryagain;
	}
}

/*ARGSUSED*/
void
timeout(int signo)
{
	int saved_errno = errno;

	signal(SIGALRM, timeout);
	timedout = 1;

	errno = saved_errno;
}

/*
 * Stolen from consh() -- puts a remote file on the output of a local command.
 *	Identical to consh() except for where stdout goes.
 */
void
pipeout(int c)
{
	char buf[256];
	int status, p;
	pid_t cpid;
	time_t start = time(NULL);

	putchar(c);
	if (prompt("Local command? ", buf, sizeof(buf)))
		return;
	write(tipout_fd, "W", 1);	/* put TIPOUT into a wait state */
	signal(SIGINT, SIG_IGN);
	signal(SIGQUIT, SIG_IGN);
	tcsetattr(0, TCSAFLUSH, &defchars);
	read(tipout_fd, (char *)&ccc, 1);
	/*
	 * Set up file descriptors in the child and
	 *  let it go...
	 */
	if ((cpid = fork()) < 0)
		printf("can't fork!\r\n");
	else if (cpid) {
		start = time(NULL);
		while ((p = wait(&status)) > 0 && p != cpid)
			;
	} else {
		int i;

		dup2(FD, 1);
		for (i = 3; i < 20; i++)
			close(i);
		signal(SIGINT, SIG_DFL);
		signal(SIGQUIT, SIG_DFL);
		execute(buf);
		printf("can't find `%s'\r\n", buf);
		exit(0);
	}
	if (vgetnum(VERBOSE))
		prtime("away for ", time(0)-start);
	write(tipout_fd, (char *)&ccc, 1);
	tcsetattr(0, TCSAFLUSH, &term);
	signal(SIGINT, SIG_DFL);
	signal(SIGQUIT, SIG_DFL);
}

/*
 * Fork a program with:
 *  0 <-> remote tty in
 *  1 <-> remote tty out
 *  2 <-> local tty stderr
 */
void
consh(int c)
{
	char buf[256];
	int status, p;
	pid_t cpid;
	time_t start = time(NULL);

	putchar(c);
	if (prompt("Local command? ", buf, sizeof(buf)))
		return;
	write(tipout_fd, "W", 1);	/* put TIPOUT into a wait state */
	signal(SIGINT, SIG_IGN);
	signal(SIGQUIT, SIG_IGN);
	tcsetattr(0, TCSAFLUSH, &defchars);
	read(tipout_fd, (char *)&ccc, 1);
	/*
	 * Set up file descriptors in the child and
	 *  let it go...
	 */
	if ((cpid = fork()) < 0)
		printf("can't fork!\r\n");
	else if (cpid) {
		start = time(0);
		while ((p = wait(&status)) > 0 && p != cpid)
			;
	} else {
		dup2(FD, 0);
		dup2(3, 1);
		closefrom(3);
		signal(SIGINT, SIG_DFL);
		signal(SIGQUIT, SIG_DFL);
		execute(buf);
		printf("can't find `%s'\r\n", buf);
		exit(0);
	}
	if (vgetnum(VERBOSE))
		prtime("away for ", time(0)-start);
	write(tipout_fd, (char *)&ccc, 1);
	tcsetattr(0, TCSAFLUSH, &term);
	signal(SIGINT, SIG_DFL);
	signal(SIGQUIT, SIG_DFL);
}

/*
 * Escape to local shell
 */
/*ARGSUSED*/
void
shell(int c)
{
	int status;
	char *cp;
	pid_t shpid;

	printf("[sh]\r\n");
	signal(SIGINT, SIG_IGN);
	signal(SIGQUIT, SIG_IGN);
	unraw();
	if ((shpid = fork())) {
		while (shpid != wait(&status));
		raw();
		printf("\r\n!\r\n");
		signal(SIGINT, SIG_DFL);
		signal(SIGQUIT, SIG_DFL);
		return;
	} else {
		signal(SIGQUIT, SIG_DFL);
		signal(SIGINT, SIG_DFL);
		if ((cp = strrchr(vgetstr(SHELL), '/')) == NULL)
			cp = vgetstr(SHELL);
		else
			cp++;
		execl(vgetstr(SHELL), cp, (char *)NULL);
		printf("\r\ncan't execl!\r\n");
		exit(1);
	}
}

/*
 * TIPIN portion of scripting
 *   initiate the conversation with TIPOUT
 */
void
setscript(void)
{
	char c;

	/*
	 * enable TIPOUT side for dialogue
	 */
	write(tipout_fd, "S", 1);
	if (vgetnum(SCRIPT))
		write(tipout_fd, vgetstr(RECORD), size(vgetstr(RECORD)));
	write(tipout_fd, "\n", 1);
	/*
	 * wait for TIPOUT to finish
	 */
	read(tipout_fd, &c, 1);
	if (c == 'n')
		printf("can't create %s\r\n", vgetstr(RECORD));
}

/*
 * Change current working directory of
 *   local portion of tip
 */
/*ARGSUSED*/
void
chdirectory(int c)
{
	char dirname[PATH_MAX];
	char *cp = dirname;

	if (prompt("[cd] ", dirname, sizeof(dirname))) {
		if (stoprompt)
			return;
		cp = vgetstr(HOME);
	}
	if (chdir(cp) < 0)
		printf("%s: bad directory\r\n", cp);
	printf("!\r\n");
}

void
tipabort(char *msg)
{
	signal(SIGTERM, SIG_IGN);
	kill(tipout_pid, SIGTERM);
	logent(vgetstr(HOST), vgetstr(DEVICE), "call terminated");
	if (msg != NULL)
		printf("\r\n%s", msg);
	printf("\r\n[EOT]\r\n");
	(void)uu_unlock(uucplock);
	unraw();
	exit(0);
}

/*ARGSUSED*/
void
finish(int c)
{
	char *dismsg;

	if ((dismsg = vgetstr(DISCONNECT)) != NULL) {
		write(FD, dismsg, strlen(dismsg));
		sleep(5);
	}
	tipabort(NULL);
}

/*ARGSUSED*/
static void
intcopy(int signo)
{
	raw();
	quit = 1;
	longjmp(intbuf, 1);
}

static void
execute(char *s)
{
	char *cp;

	if ((cp = strrchr(vgetstr(SHELL), '/')) == NULL)
		cp = vgetstr(SHELL);
	else
		cp++;
	execl(vgetstr(SHELL), cp, "-c", s, (char *)NULL);
}

static int
args(char *buf, char *a[], int num)
{
	char *p = buf, *start;
	char **parg = a;
	int n = 0;

	do {
		while (*p && (*p == ' ' || *p == '\t'))
			p++;
		start = p;
		if (*p)
			*parg = p;
		while (*p && (*p != ' ' && *p != '\t'))
			p++;
		if (p != start)
			parg++, n++;
		if (*p)
			*p++ = '\0';
	} while (*p && n < num);

	return(n);
}

static void
prtime(char *s, time_t a)
{
	int i;
	int nums[3];

	for (i = 0; i < 3; i++) {
		nums[i] = (int)(a % quant[i]);
		a /= quant[i];
	}
	printf("%s", s);
	while (--i >= 0)
		if (nums[i] || (i == 0 && nums[1] == 0 && nums[2] == 0))
			printf("%d %s%c ", nums[i], sep[i],
				nums[i] == 1 ? '\0' : 's');
	printf("\r\n!\r\n");
}

/*ARGSUSED*/
void
variable(int c)
{
	char	buf[256];

	if (prompt("[set] ", buf, sizeof(buf)))
		return;
	vlex(buf);
	if (vtable[BEAUTIFY].v_flags & V_CHANGED) {
		vtable[BEAUTIFY].v_flags &= ~V_CHANGED;
		write(tipout_fd, "B", 1);
	}
	if (vtable[SCRIPT].v_flags & V_CHANGED) {
		vtable[SCRIPT].v_flags &= ~V_CHANGED;
		setscript();
		/*
		 * So that "set record=blah script" doesn't
		 *  cause two transactions to occur.
		 */
		if (vtable[RECORD].v_flags & V_CHANGED)
			vtable[RECORD].v_flags &= ~V_CHANGED;
	}
	if (vtable[RECORD].v_flags & V_CHANGED) {
		vtable[RECORD].v_flags &= ~V_CHANGED;
		if (vgetnum(SCRIPT))
			setscript();
	}
	if (vtable[TAND].v_flags & V_CHANGED) {
		vtable[TAND].v_flags &= ~V_CHANGED;
		if (vgetnum(TAND))
			tandem("on");
		else
			tandem("off");
	}
	if (vtable[LECHO].v_flags & V_CHANGED) {
		vtable[LECHO].v_flags &= ~V_CHANGED;
		vsetnum(HALFDUPLEX, vgetnum(LECHO));
	}
	if (vtable[PARITY].v_flags & V_CHANGED) {
		vtable[PARITY].v_flags &= ~V_CHANGED;
		setparity(NULL);
	}
	if (vtable[HARDWAREFLOW].v_flags & V_CHANGED) {
		vtable[HARDWAREFLOW].v_flags &= ~V_CHANGED;
		if (vgetnum(HARDWAREFLOW))
			hardwareflow("on");
		else
			hardwareflow("off");
	}
	if (vtable[LINEDISC].v_flags & V_CHANGED) {
		vtable[LINEDISC].v_flags &= ~V_CHANGED;
		linedisc(NULL);
	}
}

/*ARGSUSED*/
void
listvariables(int c)
{
	value_t *p;
	char buf[BUFSIZ];

	puts("v\r");
	for (p = vtable; p->v_name; p++) {
		fputs(p->v_name, stdout);
		switch (p->v_flags & V_TYPEMASK) {
		case V_STRING:
			if (p->v_string) {
				strnvis(buf, p->v_string, sizeof(buf),
				    VIS_WHITE|VIS_OCTAL);
				printf(" %s", buf);
			}
			putchar('\r');
			putchar('\n');
			break;
		case V_NUMBER:
			printf(" %d\r\n", p->v_number);
			break;
		case V_BOOL:
			printf(" %s\r\n", p->v_number ? "true" : "false");
			break;
		case V_CHAR:
			vis(buf, p->v_number, VIS_WHITE|VIS_OCTAL, 0);
			printf(" %s\r\n", buf);
			break;
		}
	}
}

/*
 * Turn tandem mode on or off for remote tty.
 */
static void
tandem(char *option)
{
	struct termios	rmtty;

	tcgetattr(FD, &rmtty);
	if (strcmp(option, "on") == 0) {
		rmtty.c_iflag |= IXOFF;
		term.c_iflag |= IXOFF;
	} else {
		rmtty.c_iflag &= ~IXOFF;
		term.c_iflag &= ~IXOFF;
	}
	tcsetattr(FD, TCSADRAIN, &rmtty);
	tcsetattr(0, TCSADRAIN, &term);
}

/*
 * Turn hardware flow control on or off for remote tty.
 */
static void
hardwareflow(char *option)
{
	struct termios	rmtty;

	tcgetattr(FD, &rmtty);
	if (strcmp(option, "on") == 0)
		rmtty.c_iflag |= CRTSCTS;
	else
		rmtty.c_iflag &= ~CRTSCTS;
	tcsetattr(FD, TCSADRAIN, &rmtty);
}

/*
 * Change line discipline to the specified one.
 */
void
linedisc(char *option)
{
	int ld = (int)vgetnum(LINEDISC);

	ioctl(FD, TIOCSETD, &ld);
}

/*
 * Send a break.
 */
/*ARGSUSED*/
void
genbrk(int c)
{
	ioctl(FD, TIOCSBRK, NULL);
	sleep(1);
	ioctl(FD, TIOCCBRK, NULL);
}

/*
 * Suspend tip
 */
void
suspend(int c)
{
	unraw();
	kill(c == CTRL('y') ? getpid() : 0, SIGTSTP);
	raw();
}

/*
 *	expand a file name if it includes shell meta characters
 */
char *
expand(char name[])
{
	static char xname[BUFSIZ];
	char cmdbuf[BUFSIZ];
	int l;
	char *cp, *Shell;
	int s, pivec[2];
	pid_t pid;

	if (!anyof(name, "~{[*?$`'\"\\"))
		return(name);
	/* sigint = signal(SIGINT, SIG_IGN); */
	if (pipe(pivec) < 0) {
		perror("pipe");
		/* signal(SIGINT, sigint) */
		return(name);
	}
	(void)snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
	if ((pid = vfork()) == 0) {
		Shell = vgetstr(SHELL);
		if (Shell == NULL)
			Shell = _PATH_BSHELL;
		close(pivec[0]);
		close(1);
		dup(pivec[1]);
		close(pivec[1]);
		close(2);
		execl(Shell, Shell, "-c", cmdbuf, (char *)NULL);
		_exit(1);
	}
	if (pid == -1) {
		perror("fork");
		close(pivec[0]);
		close(pivec[1]);
		return(NULL);
	}
	close(pivec[1]);
	l = read(pivec[0], xname, BUFSIZ);
	close(pivec[0]);
	while (wait(&s) != pid);
		;
	s &= 0377;
	if (s != 0 && s != SIGPIPE) {
		fprintf(stderr, "\"Echo\" failed\n");
		return(NULL);
	}
	if (l < 0) {
		perror("read");
		return(NULL);
	}
	if (l == 0) {
		fprintf(stderr, "\"%s\": No match\n", name);
		return(NULL);
	}
	if (l == BUFSIZ) {
		fprintf(stderr, "Buffer overflow expanding \"%s\"\n", name);
		return(NULL);
	}
	xname[l] = 0;
	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
		;
	*++cp = '\0';
	return(xname);
}

/*
 * Are any of the characters in the two strings the same?
 */
static int
anyof(char *s1, char *s2)
{
	int c;

	while ((c = *s1++))
		if (any(c, s2))
			return(1);
	return(0);
}