tip.c 16.7 KB
Newer Older
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
/*
 * 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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. 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.
 */

#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1983, 1993\n\
	The Regents of the University of California.  All rights reserved.\n";
#endif /* not lint */

#ifndef lint
#if 0
static char sccsid[] = "@(#)tip.c	8.1 (Berkeley) 6/6/93";
#endif
static const char rcsid[] =
45
	"$Id: tip.c,v 1.4 2001-07-31 21:58:35 stoller Exp $";
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#endif /* not lint */

/*
	Forward declarations
*/
void ttysetup (int speed);

/*
 * tip - UNIX link to other systems
 *  tip [-v] [-speed] system-name
 * or
 *  cu phone-number [-s speed] [-l line] [-a acu]
 */

60
61
62
#include "tip.h"
#include "pathnames.h"

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
#include <err.h>
#include <errno.h>
#include <sys/types.h>
#ifndef LINUX
#include <libutil.h>
#endif

/*
 * Baud rate mapping table
 */
#ifndef LINUX
#if !HAVE_TERMIOS
CONST int bauds[] = {
	0, 50, 75, 110, 134, 150, 200, 300, 600,
	1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, -1
};
#endif
#else
struct baudmap {
	CONST int BR;
	CONST int bval;
} baudmap[] = {
	{ 0,		B0 },
	{ 50,		B50 },
	{ 75,		B75 },
	{ 110,		B110 },
	{ 134,		B134 },
	{ 150,		B150 },
	{ 200,		B200 },
	{ 300,		B300 },
	{ 600,		B600 },
	{ 1200,		B1200 },
	{ 1800,		B1800 },
	{ 2400,		B2400 },
	{ 4800,		B4800 },
	{ 9600,		B9600 },
	{ 19200,	B19200 },
	{ 38400,	B38400 },
	{ 57600,	B57600 },
	{ 115200,	B115200 },
	{ -1,		0 }
};
#endif

#if !HAVE_TERMIOS
int	disc = OTTYDISC;		/* tip normally runs this way */
#endif

void	intprompt();
void	timeout();
void	tipdone();
char	*sname();
char	PNbuf[256];			/* This limits the size of a number */

117
118
int	rflag = 1;

119
120
121
static void usage __P((void));
void setparity __P((char *));
void xpwrite __P((int, char *, int));
122
123
124
void tipio __P((void));
void tipinfunc __P((char *, int));
void tipoutfunc __P((char *, int));
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
int prompt __P((char *, char *, size_t));
void unraw __P((void));
void shell_uid __P((void));
void daemon_uid __P((void));
void user_uid __P((void));
int speed __P((int));

int
main(argc, argv)
	char *argv[];
{
	char *system = NOSTR;
	register int i;
	register char *p;
	char sbuf[12];

	gid = getgid();
	egid = getegid();
	uid = getuid();
	euid = geteuid();

	if (argc > 4)
		usage();
	if (!isatty(0))
		errx(1, "must be interactive");

151
152
	STDIN = 0;

153
154
155
156
157
158
159
160
161
	for (; argc > 1; argv++, argc--) {
		if (argv[1][0] != '-')
			system = argv[1];
		else switch (argv[1][1]) {

		case 'v':
			vflag++;
			break;

162
163
164
165
166
167
168
		case 'c':
			rflag = 0;
			break;
		case 'r':
			rflag = 1;
			break;

169
170
171
172
173
174
175
176
177
178
179
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			BR = atoi(&argv[1][1]);
			break;

		default:
			warnx("%s, unknown option", argv[1]);
			break;
		}
	}

180
181
182
183
	(void)signal(SIGINT, tipdone);
	(void)signal(SIGQUIT, tipdone);
	(void)signal(SIGHUP, tipdone);
	(void)signal(SIGTERM, tipdone);
184
185
	(void)signal(SIGUSR1, tipdone);

186
187
188
189
	/* reset the tty on screw ups */
	(void)signal(SIGBUS, tipdone);
	(void)signal(SIGSEGV, tipdone);

190
191
192
193
194
	if ((i = hunt(system)) == 0) {
		printf("all ports busy\n");
		exit(3);
	}
	if (i == -1) {
195
196
		printf("%s: busy\n", system);
#if HAVE_UUCPLOCK
197
		(void)uu_unlock(uucplock);
198
#endif
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
		exit(3);
	}
	setbuf(stdout, NULL);
	loginit();

	/*
	 * Kludge, their's no easy way to get the initialization
	 *   in the right order, so force it here
	 */
	if ((PH = getenv("PHONES")) == NOSTR)
		PH = _PATH_PHONES;
	vinit();				/* init variables */
	setparity("even");			/* set the parity table */
	if ((i = speed(number(value(BAUDRATE)))) == 0) {
		printf("tip: bad baud rate %d\n", number(value(BAUDRATE)));
214
#if HAVE_UUCPLOCK
215
		(void)uu_unlock(uucplock);
216
#endif
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
		exit(3);
	}

	/*
	 * Now that we have the logfile and the ACU open
	 *  return to the real uid and gid.  These things will
	 *  be closed on exit.  Swap real and effective uid's
	 *  so we can get the original permissions back
	 *  for removing the uucp lock.
	 */
	user_uid();

	/*
	 * Hardwired connections require the
	 *  line speed set before they make any transmissions
	 *  (this is particularly true of things like a DF03-AC)
	 */
	if (HW)
		ttysetup(i);
236

237
238
239
240
241
242
243
244
	/*
	 * From here down the code is shared with
	 * the "cu" version of tip.
	 */

#if HAVE_TERMIOS
	tcgetattr (0, &otermios);
	ctermios = otermios;
245
246
247
248
249
250
251
252
	if (rflag) {
		ctermios.c_iflag = (IGNBRK|IGNPAR);
		ctermios.c_lflag = 0;
		ctermios.c_cflag = (CLOCAL|CREAD|CS8);
		ctermios.c_cc[VMIN] = 1;
		ctermios.c_cc[VTIME] = 5;
		ctermios.c_oflag = 0;
	} else {
253
#ifndef _POSIX_SOURCE
254
255
		ctermios.c_iflag = (IMAXBEL|IXANY|ISTRIP|IXON|BRKINT);
		ctermios.c_lflag = (PENDIN|IEXTEN|ISIG|ECHOCTL|ECHOE|ECHOKE);
256
#else
257
258
		ctermios.c_iflag = (ISTRIP|IXON|BRKINT);
		ctermios.c_lflag = (PENDIN|IEXTEN|ISIG|ECHOE);
259
#endif
260
261
262
263
		ctermios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8);
		ctermios.c_cc[VINTR] = 	ctermios.c_cc[VQUIT] = _POSIX_VDISABLE;
		ctermios.c_cc[VSUSP] = ctermios.c_cc[VDISCARD] =
			ctermios.c_cc[VLNEXT] = _POSIX_VDISABLE;
264
#ifdef VDSUSP
265
		ctermios.c_cc[VDSUSP] = _POSIX_VDISABLE;
266
#endif
267
	}
268
269
270
271
272
273
#else /* HAVE_TERMIOS */
	ioctl(0, TIOCGETP, (char *)&defarg);
	ioctl(0, TIOCGETC, (char *)&defchars);
	ioctl(0, TIOCGLTC, (char *)&deflchars);
	ioctl(0, TIOCGETD, (char *)&odisc);
	arg = defarg;
274
275
276
277
278
	if (rflag)
		arg.sg_flags = RAW;
	else
		arg.sg_flags = CBREAK;
	arg.sg_flags |= PASS8 | ANYP;
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
	tchars = defchars;
	tchars.t_intrc = tchars.t_quitc = -1;
	ltchars = deflchars;
	ltchars.t_suspc = ltchars.t_dsuspc = ltchars.t_flushc
		= ltchars.t_lnextc = -1;
#endif /* HAVE_TERMIOS */
	raw();

	(void)signal(SIGALRM, timeout);

	/*
	 * Everything's set up now:
	 *	connection established (hardwired or dialup)
	 *	line conditioned (baud rate, mode, etc.)
	 *	internal data structures (variables)
294
	 * so, fire up!
295
	 */
296
297
	if (rflag)
		printf("\07connected (raw mode)\r\n");
298
	else
299
300
301
		printf("\07connected\r\n");

	tipio();
302
303
304
305
306
307
308
309
310
311
312
	/*NOTREACHED*/
}

static void
usage()
{
	fprintf(stderr, "usage: tip [-v] [-speed] [system-name]\n");
	exit(1);
}

void
313
314
tipdone(sig)
	int sig;
315
{
316
317
318
319
320
321
322
323
324
325
326
	switch (sig) {
	case SIGHUP:
		tipabort("Hangup.");
		break;
	case SIGTERM:
		tipabort("Killed.");
		break;
	default:
		printf("\r\nSignal %d", sig);
		tipabort(NOSTR);
	}
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
}

/*
 * Muck with user ID's.  We are setuid to the owner of the lock
 * directory when we start.  user_uid() reverses real and effective
 * ID's after startup, to run with the user's permissions.
 * daemon_uid() switches back to the privileged uid for unlocking.
 * Finally, to avoid running a shell with the wrong real uid,
 * shell_uid() sets real and effective uid's to the user's real ID.
 */
static int uidswapped;

void
user_uid()
{
	if (uidswapped == 0) {
		seteuid(uid);
		uidswapped = 1;
	}
}

void
daemon_uid()
{
	if (uidswapped) {
		seteuid(euid);
		uidswapped = 0;
	}
}

void
shell_uid()
{
	setegid(gid);
	seteuid(uid);
}

364
365
static int inrawmode;

366
367
368
369
/*
 * put the controlling keyboard into raw mode
 */
void
370
raw()
371
{
372
373
374
	if (inrawmode)
		return;

375
376
377
378
379
380
381
382
#if HAVE_TERMIOS
	tcsetattr (0, TCSANOW, &ctermios);
#else /* HAVE_TERMIOS */
	ioctl(0, TIOCSETP, &arg);
	ioctl(0, TIOCSETC, &tchars);
	ioctl(0, TIOCSLTC, &ltchars);
	ioctl(0, TIOCSETD, (char *)&disc);
#endif /* HAVE_TERMIOS */
383
384

	inrawmode = 1;
385
386
387
388
389
390
391
392
393
}


/*
 * return keyboard to normal mode
 */
void
unraw()
{
394
395
396
	if (!inrawmode)
		return;

397
398
399
400
401
402
403
404
#if HAVE_TERMIOS
	tcsetattr (0, TCSANOW, &otermios);
#else /* HAVE_TERMIOS */
	ioctl(0, TIOCSETD, (char *)&odisc);
	ioctl(0, TIOCSETP, (char *)&defarg);
	ioctl(0, TIOCSETC, (char *)&defchars);
	ioctl(0, TIOCSLTC, (char *)&deflchars);
#endif /* HAVE_TERMIOS */
405
406

	inrawmode = 0;
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
}

static	jmp_buf promptbuf;

/*
 * Print string ``s'', then read a string
 *  in from the terminal.  Handles signals & allows use of
 *  normal erase and kill characters.
 */
int
prompt(s, p, sz)
	char *s;
	register char *p;
	size_t sz;
{
	register char *b = p;
	sig_t oint, oquit;

	stoprompt = 0;
	oint = signal(SIGINT, intprompt);
	oquit = signal(SIGQUIT, SIG_IGN);
	unraw();
	printf("%s", s);
	if (setjmp(promptbuf) == 0)
431
		while ((*p = tipgetchar()) != '\n' && --sz > 0)
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
			p++;
	*p = '\0';

	raw();
	(void)signal(SIGINT, oint);
	(void)signal(SIGQUIT, oquit);
	return (stoprompt || p == b);
}

/*
 * Interrupt service routine during prompting
 */
void
intprompt()
{

	(void)signal(SIGINT, SIG_IGN);
	stoprompt = 1;
	printf("\r\n");
	longjmp(promptbuf, 1);
}

/*
455
456
457
458
 * ****TIPIO   TIPIO****
 *
 * Replace two process reader/writer with select-based single process
 * reader and writer.
459
460
 */
void
461
tipio()
462
{
463
464
465
	fd_set sfds, fds;
	int n, i, cc;
	char buf[4096];
466
467

	/*
468
	 * Check for scripting being turned on from the .tiprc file.
469
	 */
470
	if (boolean(value(SCRIPT)))
471
		setscript();
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

	n = STDIN;
	if (n < FD)
		n = FD;
	n++;
	FD_ZERO(&sfds);
	FD_SET(STDIN, &sfds);
	FD_SET(FD, &sfds);
	for (;;) {
		fds = sfds;
		i = select(n, &fds, NULL, NULL, NULL);
		if (i <= 0)
			tipabort("select failed");

		/*
		 * Check for user input first.
		 * It is lower volume and possibly more important (^C)
		 */
		if (FD_ISSET(STDIN, &fds)) {
			cc = read(STDIN, buf, sizeof(buf));
			if (cc < 0)
				tipabort("stdin read failed");
			if (cc == 0)
				finish();
			tipinfunc(buf, cc);
		}

		if (FD_ISSET(FD, &fds)) {
			cc = read(FD, buf, sizeof(buf));
			if (cc < 0)
				tipabort("device read failed");
			if (cc == 0)
				tipabort("device read EOF");

			tipoutfunc(buf, cc);
		}
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
void
tipinfunc(buf, nchar)
	char *buf;
	int nchar;
{
	int i;
	char gch;
	char escc = character(value(ESCAPE));
	char forcec = character(value(FORCE));
	static int bol = 1;
	static int inescape = 0;
	static int inforce = 0;

	for (i = 0; i < nchar; i++) {
		gch = buf[i] & 0177;
		if (inescape || (gch == escc && bol)) {
			esctable_t *p;

			/*
			 * Read the next char if not already in an escape.
			 * If there is no next char note that we are in an
			 * escape and exit.
			 */
			if (!inescape && ++i == nchar) {
				inescape = 1;
				break;
			}
			gch = buf[i] & 0177;
			inescape = 0;

			for (p = etable; p->e_char; p++)
				if (p->e_char == gch)
					break;

			/*
			 * If this is a legit escape command, process it.
			 * Otherwise, for an unrecognized sequence (which
			 * includes ESCAPE ESCAPE), we just send the escaped
			 * char with no further interpretation.
			 */
			if (p->e_char) {
				if ((p->e_flags&PRIV) && uid)
					continue;
				printf("%s", ctrl(escc));
				(*p->e_func)(gch);
556
				continue;
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
			}
		} else if (inforce || gch == forcec) {
			/*
			 * Same story, different character...
			 * Read the next char if not already in a force.
			 * If there is no next char note that we are forcing
			 * and exit.
			 */
			if (!inforce && ++i == nchar) {
				inforce = 1;
				break;
			}
			gch = buf[i] & 0177;
			inforce = 0;
		} else if (gch == character(value(RAISECHAR))) {
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
			boolean(value(RAISE)) = !boolean(value(RAISE));
			continue;
		} else if (gch == '\r') {
			bol = 1;
			xpwrite(FD, &gch, 1);
			if (boolean(value(HALFDUPLEX)))
				printf("\r\n");
			continue;
		}
		bol = any(gch, value(EOL));
		if (boolean(value(RAISE)) && islower(gch))
			gch = toupper(gch);
		xpwrite(FD, &gch, 1);
		if (boolean(value(HALFDUPLEX)))
			printf("%c", gch);
	}
}

590
591
592
593
void
tipoutfunc(buf, nchar)
	char *buf;
	int nchar;
594
{
595
	char *cp;
596

597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
	for (cp = buf; cp < buf + nchar; cp++)
		*cp &= 0177;
	write(1, buf, nchar);

	if (boolean(value(SCRIPT)) && fscript != NULL) {
		if (boolean(value(BEAUTIFY))) {
			char *excepts = value(EXCEPTIONS);

			for (cp = buf; cp < buf + nchar; cp++)
				if ((*cp >= ' ' && *cp <= '~') ||
				    any(*cp, excepts))
					putc(*cp, fscript);
		} else
			fwrite(buf, 1, nchar, fscript);
	}
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
}

int
speed(n)
	int n;
{
#ifndef LINUX
#if HAVE_TERMIOS
	return (n);
#else
	register CONST int *p;

	for (p = bauds; *p != -1;  p++)
		if (*p == n)
			return (p - bauds);
	return (0);
#endif
#else
	struct baudmap *p;

	for (p = baudmap; p->BR != -1;  p++)
		if (p->BR == n)
			return (p->bval);
	return (0);
#endif
}

int
any(c, p)
	register char c, *p;
{
	while (p && *p)
		if (*p++ == c)
			return (1);
	return (0);
}

int
size(s)
	register char	*s;
{
	register int i = 0;

	while (s && *s++)
		i++;
	return (i);
}

char *
interp(s)
	register char *s;
{
	static char buf[256];
	register char *p = buf, c, *q;

	while ((c = *s++)) {
		for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
			if (*q++ == c) {
				*p++ = '\\'; *p++ = *q;
				goto next;
			}
		if (c < 040) {
			*p++ = '^'; *p++ = c + 'A'-1;
		} else if (c == 0177) {
			*p++ = '^'; *p++ = '?';
		} else
			*p++ = c;
	next:
		;
	}
	*p = '\0';
	return (buf);
}

char *
ctrl(c)
	char c;
{
	static char s[3];

	if (c < 040 || c == 0177) {
		s[0] = '^';
		s[1] = c == 0177 ? '?' : c+'A'-1;
		s[2] = '\0';
	} else {
		s[0] = c;
		s[1] = '\0';
	}
	return (s);
}

/*
 * Help command
 */
void
help(c)
	char c;
{
	register esctable_t *p;

	printf("%c\r\n", c);
	for (p = etable; p->e_char; p++) {
		if ((p->e_flags&PRIV) && uid)
			continue;
		printf("%2s", ctrl(character(value(ESCAPE))));
		printf("%-2s %c   %s\r\n", ctrl(p->e_char),
			p->e_flags&EXP ? '*': ' ', p->e_help);
	}
}

/*
 * Set up the "remote" tty's state
 */
void
ttysetup (int speed)
{
#if HAVE_TERMIOS
	struct termios termios;
	tcgetattr (FD, &termios);
731
	termios.c_iflag = (IGNBRK|IGNPAR);
732
	if (boolean(value(TAND)))
733
734
		termios.c_iflag |= IXOFF;
	termios.c_lflag = 0;
735
	termios.c_cflag = (CLOCAL|HUPCL|CREAD|CS8);
736
737
738
	termios.c_cc[VMIN] = 1;
	termios.c_cc[VTIME] = 5;
	termios.c_oflag = 0;
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
	cfsetispeed(&termios, speed);
	cfsetospeed(&termios, speed);
	tcsetattr (FD, TCSANOW, &termios);
#else /* HAVE_TERMIOS */
	unsigned bits = LDECCTQ;

	arg.sg_ispeed = arg.sg_ospeed = speed;
	arg.sg_flags = RAW;
	if (boolean(value(TAND)))
		arg.sg_flags |= TANDEM;
	ioctl(FD, TIOCSETP, (char *)&arg);
	ioctl(FD, TIOCLBIS, (char *)&bits);
#endif /* HAVE_TERMIOS */
}

/*
 * Return "simple" name from a file name,
 * strip leading directories.
 */
char *
sname(s)
	register char *s;
{
	register char *p = s;

	while (*s)
		if (*s++ == '/')
			p = s;
	return (p);
}

static char partab[0200];
static int bits8;

/*
 * Do a write to the remote machine with the correct parity.
 * We are doing 8 bit wide output, so we just generate a character
 * with the right parity and output it.
 */
void
xpwrite(fd, buf, n)
	int fd;
	char *buf;
	register int n;
{
	register int i;
	register char *bp;
786
	extern int errno;
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

	bp = buf;
	if (bits8 == 0)
		for (i = 0; i < n; i++) {
			*bp = partab[(*bp) & 0177];
			bp++;
		}
	if (write(fd, buf, n) < 0) {
		if (errno == EIO)
			tipabort("Lost carrier.");
		if (errno == ENODEV)
			tipabort("tty not available.");
		tipabort("Something wrong...");
	}
}

/*
 * Build a parity table with appropriate high-order bit.
 */
void
setparity(defparity)
	char *defparity;
{
	register int i, flip, clr, set;
	char *parity;
	extern char evenpartab[];

	if (value(PARITY) == NOSTR)
		value(PARITY) = defparity;
	parity = value(PARITY);
	if (equal(parity, "none")) {
		bits8 = 1;
		return;
	}
	bits8 = 0;
	flip = 0;
	clr = 0377;
	set = 0;
	if (equal(parity, "odd"))
		flip = 0200;			/* reverse bit 7 */
	else if (equal(parity, "zero"))
		clr = 0177;			/* turn off bit 7 */
	else if (equal(parity, "one"))
		set = 0200;			/* turn on bit 7 */
	else if (!equal(parity, "even")) {
		(void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
		(void) fflush(stderr);
	}
	for (i = 0; i < 0200; i++)
		partab[i] = (evenpartab[i] ^ flip) | (set & clr);
}


/*
 * getchar with EOF check
 */
tipgetchar()
{
	char gch;

847
	if (read(STDIN, &gch, 1) != 1)
848
		finish();
849

850
851
	return gch;
}