tip.c revision 1.35 1 1.35 perry /* $NetBSD: tip.c,v 1.35 2006/04/03 00:51:13 perry Exp $ */
2 1.6 jtc
3 1.1 cgd /*
4 1.6 jtc * Copyright (c) 1983, 1993
5 1.6 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.24 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.15 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.15 lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
35 1.15 lukem The Regents of the University of California. All rights reserved.\n");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.6 jtc #if 0
40 1.6 jtc static char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93";
41 1.6 jtc #endif
42 1.35 perry __RCSID("$NetBSD: tip.c,v 1.35 2006/04/03 00:51:13 perry Exp $");
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd /*
46 1.1 cgd * tip - UNIX link to other systems
47 1.1 cgd * tip [-v] [-speed] system-name
48 1.1 cgd * or
49 1.1 cgd * cu phone-number [-s speed] [-l line] [-a acu]
50 1.1 cgd */
51 1.1 cgd #include "tip.h"
52 1.1 cgd #include "pathnames.h"
53 1.1 cgd
54 1.35 perry int escape(void);
55 1.35 perry int main(int, char **);
56 1.35 perry void intprompt(int);
57 1.35 perry void tipin(void);
58 1.15 lukem
59 1.1 cgd char PNbuf[256]; /* This limits the size of a number */
60 1.1 cgd
61 1.26 christos static char path_phones[] = _PATH_PHONES;
62 1.26 christos
63 1.15 lukem int
64 1.35 perry main(int argc, char *argv[])
65 1.1 cgd {
66 1.25 christos char *System = NULL;
67 1.15 lukem int i;
68 1.15 lukem char *p;
69 1.25 christos const char *q;
70 1.1 cgd char sbuf[12];
71 1.18 mellon int fcarg;
72 1.1 cgd
73 1.1 cgd gid = getgid();
74 1.1 cgd egid = getegid();
75 1.1 cgd uid = getuid();
76 1.1 cgd euid = geteuid();
77 1.15 lukem if (equal(basename(argv[0]), "cu")) {
78 1.1 cgd cumode = 1;
79 1.1 cgd cumain(argc, argv);
80 1.1 cgd goto cucommon;
81 1.1 cgd }
82 1.1 cgd
83 1.1 cgd if (argc > 4) {
84 1.32 tls fprintf(stderr, "usage: %s [-v] [-speed] [system-name]\n",
85 1.32 tls getprogname());
86 1.1 cgd exit(1);
87 1.1 cgd }
88 1.1 cgd if (!isatty(0)) {
89 1.32 tls fprintf(stderr, "%s: must be interactive\n", getprogname());
90 1.1 cgd exit(1);
91 1.1 cgd }
92 1.1 cgd
93 1.1 cgd for (; argc > 1; argv++, argc--) {
94 1.1 cgd if (argv[1][0] != '-')
95 1.25 christos System = argv[1];
96 1.1 cgd else switch (argv[1][1]) {
97 1.1 cgd
98 1.1 cgd case 'v':
99 1.1 cgd vflag++;
100 1.1 cgd break;
101 1.1 cgd
102 1.1 cgd case '0': case '1': case '2': case '3': case '4':
103 1.1 cgd case '5': case '6': case '7': case '8': case '9':
104 1.1 cgd BR = atoi(&argv[1][1]);
105 1.1 cgd break;
106 1.1 cgd
107 1.1 cgd default:
108 1.32 tls warnx("%s, unknown option", argv[1]);
109 1.1 cgd break;
110 1.1 cgd }
111 1.1 cgd }
112 1.1 cgd
113 1.25 christos if (System == NULL)
114 1.1 cgd goto notnumber;
115 1.25 christos if (isalpha((unsigned char)*System))
116 1.1 cgd goto notnumber;
117 1.1 cgd /*
118 1.1 cgd * System name is really a phone number...
119 1.1 cgd * Copy the number then stomp on the original (in case the number
120 1.1 cgd * is private, we don't want 'ps' or 'w' to find it).
121 1.1 cgd */
122 1.25 christos if (strlen(System) > sizeof PNbuf - 1) {
123 1.32 tls errx(1, "phone number too long (max = %d bytes)",
124 1.17 mrg (int)sizeof(PNbuf) - 1);
125 1.1 cgd }
126 1.27 jrf (void)strlcpy(PNbuf, System, sizeof(PNbuf));
127 1.25 christos for (p = System; *p; p++)
128 1.1 cgd *p = '\0';
129 1.1 cgd PN = PNbuf;
130 1.15 lukem (void)snprintf(sbuf, sizeof sbuf, "tip%d", (int)BR);
131 1.25 christos System = sbuf;
132 1.1 cgd
133 1.1 cgd notnumber:
134 1.1 cgd (void)signal(SIGINT, cleanup);
135 1.1 cgd (void)signal(SIGQUIT, cleanup);
136 1.1 cgd (void)signal(SIGHUP, cleanup);
137 1.1 cgd (void)signal(SIGTERM, cleanup);
138 1.1 cgd
139 1.25 christos if ((i = hunt(System)) == 0) {
140 1.1 cgd printf("all ports busy\n");
141 1.1 cgd exit(3);
142 1.1 cgd }
143 1.1 cgd if (i == -1) {
144 1.1 cgd printf("link down\n");
145 1.1 cgd (void)uu_unlock(uucplock);
146 1.1 cgd exit(3);
147 1.1 cgd }
148 1.1 cgd setbuf(stdout, NULL);
149 1.34 tls #ifdef ACULOG
150 1.1 cgd loginit();
151 1.34 tls #endif
152 1.1 cgd
153 1.1 cgd /*
154 1.3 andrew * Now that we have the logfile and the ACU open
155 1.3 andrew * return to the real uid and gid. These things will
156 1.3 andrew * be closed on exit. Swap real and effective uid's
157 1.3 andrew * so we can get the original permissions back
158 1.3 andrew * for removing the uucp lock.
159 1.3 andrew */
160 1.3 andrew user_uid();
161 1.3 andrew
162 1.3 andrew /*
163 1.1 cgd * Kludge, their's no easy way to get the initialization
164 1.1 cgd * in the right order, so force it here
165 1.1 cgd */
166 1.15 lukem if ((PH = getenv("PHONES")) == NULL)
167 1.26 christos PH = path_phones;
168 1.1 cgd vinit(); /* init variables */
169 1.31 tls setparity("none"); /* set the parity table */
170 1.1 cgd
171 1.1 cgd /*
172 1.1 cgd * Hardwired connections require the
173 1.1 cgd * line speed set before they make any transmissions
174 1.1 cgd * (this is particularly true of things like a DF03-AC)
175 1.1 cgd */
176 1.33 tls if (HW) {
177 1.33 tls if (ttysetup(i) != 0) {
178 1.33 tls warnx("bad baud rate %d",
179 1.33 tls (int)number(value(BAUDRATE)));
180 1.33 tls daemon_uid();
181 1.33 tls (void)uu_unlock(uucplock);
182 1.33 tls exit(3);
183 1.33 tls }
184 1.33 tls }
185 1.25 christos if ((q = connect()) != NULL) {
186 1.25 christos printf("\07%s\n[EOT]\n", q);
187 1.1 cgd daemon_uid();
188 1.1 cgd (void)uu_unlock(uucplock);
189 1.1 cgd exit(1);
190 1.1 cgd }
191 1.33 tls if (!HW) {
192 1.33 tls if (ttysetup(i) != 0) {
193 1.33 tls warnx("bad baud rate %d",
194 1.33 tls (int)number(value(BAUDRATE)));
195 1.33 tls daemon_uid();
196 1.33 tls (void)uu_unlock(uucplock);
197 1.33 tls exit(3);
198 1.33 tls }
199 1.33 tls }
200 1.35 perry
201 1.18 mellon
202 1.19 mellon /*
203 1.19 mellon * Direct connections with no carrier require using O_NONBLOCK on
204 1.19 mellon * open, but we don't want to keep O_NONBLOCK after open because it
205 1.19 mellon * will cause busy waits.
206 1.19 mellon */
207 1.19 mellon if (DC &&
208 1.19 mellon ((fcarg = fcntl(FD, F_GETFL, 0)) < 0 ||
209 1.19 mellon fcntl(FD, F_SETFL, fcarg & ~O_NONBLOCK) < 0)) {
210 1.32 tls warn("can't clear O_NONBLOCK");
211 1.18 mellon daemon_uid();
212 1.21 mrg (void)uu_unlock(uucplock);
213 1.18 mellon exit(1);
214 1.18 mellon }
215 1.35 perry
216 1.1 cgd cucommon:
217 1.1 cgd /*
218 1.1 cgd * From here down the code is shared with
219 1.1 cgd * the "cu" version of tip.
220 1.1 cgd */
221 1.1 cgd
222 1.8 pk tcgetattr(0, &defterm);
223 1.8 pk term = defterm;
224 1.8 pk term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
225 1.8 pk term.c_iflag &= ~(INPCK|ICRNL);
226 1.8 pk term.c_oflag &= ~OPOST;
227 1.8 pk term.c_cc[VMIN] = 1;
228 1.8 pk term.c_cc[VTIME] = 0;
229 1.8 pk defchars = term;
230 1.8 pk term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
231 1.35 perry term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
232 1.8 pk term.c_cc[VLNEXT] = _POSIX_VDISABLE;
233 1.1 cgd raw();
234 1.1 cgd
235 1.1 cgd pipe(fildes); pipe(repdes);
236 1.15 lukem (void)signal(SIGALRM, alrmtimeout);
237 1.1 cgd
238 1.1 cgd /*
239 1.1 cgd * Everything's set up now:
240 1.1 cgd * connection established (hardwired or dialup)
241 1.1 cgd * line conditioned (baud rate, mode, etc.)
242 1.1 cgd * internal data structures (variables)
243 1.1 cgd * so, fork one process for local side and one for remote.
244 1.1 cgd */
245 1.23 is printf("%s", cumode ? "Connected\r\n" : "\07connected\r\n");
246 1.15 lukem switch (pid = fork()) {
247 1.15 lukem default:
248 1.1 cgd tipin();
249 1.15 lukem break;
250 1.15 lukem case 0:
251 1.1 cgd tipout();
252 1.15 lukem break;
253 1.15 lukem case -1:
254 1.15 lukem err(1, "can't fork");
255 1.15 lukem }
256 1.1 cgd /*NOTREACHED*/
257 1.15 lukem exit(0); /* XXX: pacify gcc */
258 1.1 cgd }
259 1.1 cgd
260 1.1 cgd void
261 1.35 perry cleanup(int dummy)
262 1.1 cgd {
263 1.1 cgd
264 1.1 cgd daemon_uid();
265 1.1 cgd (void)uu_unlock(uucplock);
266 1.1 cgd if (odisc)
267 1.1 cgd ioctl(0, TIOCSETD, (char *)&odisc);
268 1.1 cgd exit(0);
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd /*
272 1.1 cgd * Muck with user ID's. We are setuid to the owner of the lock
273 1.1 cgd * directory when we start. user_uid() reverses real and effective
274 1.1 cgd * ID's after startup, to run with the user's permissions.
275 1.1 cgd * daemon_uid() switches back to the privileged uid for unlocking.
276 1.1 cgd * Finally, to avoid running a shell with the wrong real uid,
277 1.1 cgd * shell_uid() sets real and effective uid's to the user's real ID.
278 1.1 cgd */
279 1.1 cgd static int uidswapped;
280 1.1 cgd
281 1.15 lukem void
282 1.35 perry user_uid(void)
283 1.1 cgd {
284 1.21 mrg
285 1.1 cgd if (uidswapped == 0) {
286 1.5 pk seteuid(uid);
287 1.1 cgd uidswapped = 1;
288 1.1 cgd }
289 1.1 cgd }
290 1.1 cgd
291 1.15 lukem void
292 1.35 perry daemon_uid(void)
293 1.1 cgd {
294 1.1 cgd
295 1.1 cgd if (uidswapped) {
296 1.5 pk seteuid(euid);
297 1.1 cgd uidswapped = 0;
298 1.1 cgd }
299 1.1 cgd }
300 1.1 cgd
301 1.15 lukem void
302 1.35 perry shell_uid(void)
303 1.1 cgd {
304 1.21 mrg
305 1.6 jtc seteuid(uid);
306 1.1 cgd }
307 1.1 cgd
308 1.1 cgd /*
309 1.1 cgd * put the controlling keyboard into raw mode
310 1.1 cgd */
311 1.15 lukem void
312 1.35 perry raw(void)
313 1.1 cgd {
314 1.21 mrg
315 1.8 pk tcsetattr(0, TCSADRAIN, &term);
316 1.1 cgd }
317 1.1 cgd
318 1.1 cgd
319 1.1 cgd /*
320 1.1 cgd * return keyboard to normal mode
321 1.1 cgd */
322 1.15 lukem void
323 1.35 perry unraw(void)
324 1.1 cgd {
325 1.21 mrg
326 1.8 pk tcsetattr(0, TCSADRAIN, &defterm);
327 1.1 cgd }
328 1.1 cgd
329 1.1 cgd static jmp_buf promptbuf;
330 1.1 cgd
331 1.1 cgd /*
332 1.1 cgd * Print string ``s'', then read a string
333 1.1 cgd * in from the terminal. Handles signals & allows use of
334 1.1 cgd * normal erase and kill characters.
335 1.1 cgd */
336 1.15 lukem int
337 1.35 perry prompt(const char *s, char *p, size_t l)
338 1.1 cgd {
339 1.15 lukem int c;
340 1.15 lukem char *b = p;
341 1.1 cgd sig_t oint, oquit;
342 1.1 cgd
343 1.15 lukem #if __GNUC__ /* XXX: pacify gcc */
344 1.15 lukem (void)&p;
345 1.15 lukem #endif
346 1.15 lukem
347 1.1 cgd stoprompt = 0;
348 1.1 cgd oint = signal(SIGINT, intprompt);
349 1.1 cgd oquit = signal(SIGQUIT, SIG_IGN);
350 1.1 cgd unraw();
351 1.1 cgd printf("%s", s);
352 1.1 cgd if (setjmp(promptbuf) == 0)
353 1.21 mrg while ((c = getchar()) != -1 && (*p = c) != '\n' &&
354 1.21 mrg b + l > p)
355 1.1 cgd p++;
356 1.1 cgd *p = '\0';
357 1.1 cgd
358 1.1 cgd raw();
359 1.1 cgd (void)signal(SIGINT, oint);
360 1.1 cgd (void)signal(SIGQUIT, oquit);
361 1.1 cgd return (stoprompt || p == b);
362 1.1 cgd }
363 1.1 cgd
364 1.1 cgd /*
365 1.1 cgd * Interrupt service routine during prompting
366 1.1 cgd */
367 1.1 cgd void
368 1.35 perry intprompt(int dummy)
369 1.1 cgd {
370 1.1 cgd
371 1.1 cgd (void)signal(SIGINT, SIG_IGN);
372 1.1 cgd stoprompt = 1;
373 1.1 cgd printf("\r\n");
374 1.1 cgd longjmp(promptbuf, 1);
375 1.1 cgd }
376 1.1 cgd
377 1.1 cgd /*
378 1.1 cgd * ****TIPIN TIPIN****
379 1.1 cgd */
380 1.15 lukem void
381 1.35 perry tipin(void)
382 1.1 cgd {
383 1.1 cgd char gch, bol = 1;
384 1.1 cgd
385 1.1 cgd /*
386 1.1 cgd * Kinda klugey here...
387 1.1 cgd * check for scripting being turned on from the .tiprc file,
388 1.1 cgd * but be careful about just using setscript(), as we may
389 1.1 cgd * send a SIGEMT before tipout has a chance to set up catching
390 1.1 cgd * it; so wait a second, then setscript()
391 1.1 cgd */
392 1.1 cgd if (boolean(value(SCRIPT))) {
393 1.1 cgd sleep(1);
394 1.1 cgd setscript();
395 1.1 cgd }
396 1.1 cgd
397 1.1 cgd while (1) {
398 1.8 pk gch = getchar()&STRIP_PAR;
399 1.1 cgd if ((gch == character(value(ESCAPE))) && bol) {
400 1.1 cgd if (!(gch = escape()))
401 1.1 cgd continue;
402 1.35 perry } else if (!cumode &&
403 1.16 lukem gch && gch == character(value(RAISECHAR))) {
404 1.9 cgd setboolean(value(RAISE), !boolean(value(RAISE)));
405 1.1 cgd continue;
406 1.1 cgd } else if (gch == '\r') {
407 1.1 cgd bol = 1;
408 1.20 thorpej xpwrite(FD, &gch, 1);
409 1.1 cgd if (boolean(value(HALFDUPLEX)))
410 1.1 cgd printf("\r\n");
411 1.1 cgd continue;
412 1.16 lukem } else if (!cumode && gch && gch == character(value(FORCE)))
413 1.8 pk gch = getchar()&STRIP_PAR;
414 1.1 cgd bol = any(gch, value(EOL));
415 1.22 christos if (boolean(value(RAISE)) && islower((unsigned char)gch))
416 1.28 dsl gch = toupper((unsigned char)gch);
417 1.20 thorpej xpwrite(FD, &gch, 1);
418 1.1 cgd if (boolean(value(HALFDUPLEX)))
419 1.1 cgd printf("%c", gch);
420 1.1 cgd }
421 1.1 cgd }
422 1.1 cgd
423 1.1 cgd /*
424 1.1 cgd * Escape handler --
425 1.1 cgd * called on recognition of ``escapec'' at the beginning of a line
426 1.1 cgd */
427 1.15 lukem int
428 1.35 perry escape(void)
429 1.1 cgd {
430 1.15 lukem char gch;
431 1.15 lukem esctable_t *p;
432 1.1 cgd char c = character(value(ESCAPE));
433 1.1 cgd
434 1.8 pk gch = (getchar()&STRIP_PAR);
435 1.1 cgd for (p = etable; p->e_char; p++)
436 1.1 cgd if (p->e_char == gch) {
437 1.1 cgd if ((p->e_flags&PRIV) && uid)
438 1.1 cgd continue;
439 1.1 cgd printf("%s", ctrl(c));
440 1.1 cgd (*p->e_func)(gch);
441 1.1 cgd return (0);
442 1.1 cgd }
443 1.1 cgd /* ESCAPE ESCAPE forces ESCAPE */
444 1.1 cgd if (c != gch)
445 1.20 thorpej xpwrite(FD, &c, 1);
446 1.1 cgd return (gch);
447 1.1 cgd }
448 1.1 cgd
449 1.15 lukem int
450 1.35 perry any(char c, const char *p)
451 1.1 cgd {
452 1.21 mrg
453 1.1 cgd while (p && *p)
454 1.1 cgd if (*p++ == c)
455 1.1 cgd return (1);
456 1.1 cgd return (0);
457 1.1 cgd }
458 1.1 cgd
459 1.1 cgd char *
460 1.35 perry interp(const char *s)
461 1.1 cgd {
462 1.1 cgd static char buf[256];
463 1.25 christos char *p = buf, c;
464 1.25 christos const char *q;
465 1.1 cgd
466 1.21 mrg while ((c = *s++) != 0 && buf + sizeof buf - p > 2) {
467 1.1 cgd for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
468 1.1 cgd if (*q++ == c) {
469 1.1 cgd *p++ = '\\'; *p++ = *q;
470 1.1 cgd goto next;
471 1.1 cgd }
472 1.1 cgd if (c < 040) {
473 1.1 cgd *p++ = '^'; *p++ = c + 'A'-1;
474 1.1 cgd } else if (c == 0177) {
475 1.1 cgd *p++ = '^'; *p++ = '?';
476 1.1 cgd } else
477 1.1 cgd *p++ = c;
478 1.1 cgd next:
479 1.1 cgd ;
480 1.1 cgd }
481 1.1 cgd *p = '\0';
482 1.1 cgd return (buf);
483 1.1 cgd }
484 1.1 cgd
485 1.1 cgd char *
486 1.35 perry ctrl(char c)
487 1.1 cgd {
488 1.1 cgd static char s[3];
489 1.1 cgd
490 1.1 cgd if (c < 040 || c == 0177) {
491 1.1 cgd s[0] = '^';
492 1.1 cgd s[1] = c == 0177 ? '?' : c+'A'-1;
493 1.1 cgd s[2] = '\0';
494 1.1 cgd } else {
495 1.1 cgd s[0] = c;
496 1.1 cgd s[1] = '\0';
497 1.1 cgd }
498 1.1 cgd return (s);
499 1.1 cgd }
500 1.1 cgd
501 1.1 cgd /*
502 1.1 cgd * Help command
503 1.1 cgd */
504 1.15 lukem void
505 1.35 perry help(char c)
506 1.1 cgd {
507 1.15 lukem esctable_t *p;
508 1.1 cgd
509 1.1 cgd printf("%c\r\n", c);
510 1.1 cgd for (p = etable; p->e_char; p++) {
511 1.1 cgd if ((p->e_flags&PRIV) && uid)
512 1.1 cgd continue;
513 1.1 cgd printf("%2s", ctrl(character(value(ESCAPE))));
514 1.1 cgd printf("%-2s %c %s\r\n", ctrl(p->e_char),
515 1.1 cgd p->e_flags&EXP ? '*': ' ', p->e_help);
516 1.1 cgd }
517 1.1 cgd }
518 1.1 cgd
519 1.1 cgd /*
520 1.1 cgd * Set up the "remote" tty's state
521 1.1 cgd */
522 1.33 tls int
523 1.35 perry ttysetup(int spd)
524 1.1 cgd {
525 1.8 pk struct termios cntrl;
526 1.1 cgd
527 1.8 pk tcgetattr(FD, &cntrl);
528 1.25 christos cfsetospeed(&cntrl, spd);
529 1.25 christos cfsetispeed(&cntrl, spd);
530 1.8 pk cntrl.c_cflag &= ~(CSIZE|PARENB);
531 1.8 pk cntrl.c_cflag |= CS8;
532 1.14 mellon if (DC)
533 1.13 mellon cntrl.c_cflag |= CLOCAL;
534 1.29 yamt if (boolean(value(HARDWAREFLOW)))
535 1.35 perry cntrl.c_cflag |= CRTSCTS;
536 1.8 pk cntrl.c_iflag &= ~(ISTRIP|ICRNL);
537 1.8 pk cntrl.c_oflag &= ~OPOST;
538 1.8 pk cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
539 1.8 pk cntrl.c_cc[VMIN] = 1;
540 1.8 pk cntrl.c_cc[VTIME] = 0;
541 1.1 cgd if (boolean(value(TAND)))
542 1.8 pk cntrl.c_iflag |= IXOFF;
543 1.33 tls return(tcsetattr(FD, TCSAFLUSH, &cntrl));
544 1.1 cgd }
545 1.1 cgd
546 1.1 cgd static char partab[0200];
547 1.1 cgd
548 1.1 cgd /*
549 1.1 cgd * Do a write to the remote machine with the correct parity.
550 1.1 cgd * We are doing 8 bit wide output, so we just generate a character
551 1.1 cgd * with the right parity and output it.
552 1.1 cgd */
553 1.15 lukem void
554 1.35 perry xpwrite(int fd, char *buf, int n)
555 1.1 cgd {
556 1.15 lukem int i;
557 1.15 lukem char *bp;
558 1.1 cgd
559 1.1 cgd bp = buf;
560 1.6 jtc if (bits8 == 0)
561 1.6 jtc for (i = 0; i < n; i++) {
562 1.6 jtc *bp = partab[(*bp) & 0177];
563 1.6 jtc bp++;
564 1.1 cgd }
565 1.6 jtc if (write(fd, buf, n) < 0) {
566 1.1 cgd if (errno == EIO)
567 1.1 cgd tipabort("Lost carrier.");
568 1.1 cgd /* this is questionable */
569 1.32 tls warn("write");
570 1.1 cgd }
571 1.1 cgd }
572 1.1 cgd
573 1.1 cgd /*
574 1.1 cgd * Build a parity table with appropriate high-order bit.
575 1.1 cgd */
576 1.15 lukem void
577 1.35 perry setparity(const char *defparity)
578 1.1 cgd {
579 1.15 lukem int i, flip, clr, set;
580 1.25 christos const char *parity;
581 1.26 christos static char *curpar;
582 1.1 cgd
583 1.26 christos if (value(PARITY) == NULL || (value(PARITY))[0] == '\0') {
584 1.26 christos if (curpar != NULL)
585 1.26 christos free(curpar);
586 1.26 christos value(PARITY) = curpar = strdup(defparity);
587 1.26 christos }
588 1.1 cgd parity = value(PARITY);
589 1.1 cgd if (equal(parity, "none")) {
590 1.1 cgd bits8 = 1;
591 1.1 cgd return;
592 1.1 cgd }
593 1.1 cgd bits8 = 0;
594 1.1 cgd flip = 0;
595 1.1 cgd clr = 0377;
596 1.1 cgd set = 0;
597 1.1 cgd if (equal(parity, "odd"))
598 1.1 cgd flip = 0200; /* reverse bit 7 */
599 1.1 cgd else if (equal(parity, "zero"))
600 1.1 cgd clr = 0177; /* turn off bit 7 */
601 1.1 cgd else if (equal(parity, "one"))
602 1.1 cgd set = 0200; /* turn on bit 7 */
603 1.1 cgd else if (!equal(parity, "even")) {
604 1.1 cgd (void) fprintf(stderr, "%s: unknown parity value\r\n", parity);
605 1.1 cgd (void) fflush(stderr);
606 1.1 cgd }
607 1.1 cgd for (i = 0; i < 0200; i++)
608 1.15 lukem partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
609 1.1 cgd }
610