tip.c revision 1.55 1 1.55 dholland /* $NetBSD: tip.c,v 1.55 2014/07/27 04:32:23 dholland 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.47 hubertf #include <ctype.h>
34 1.48 hubertf #include <libgen.h>
35 1.44 jdc
36 1.1 cgd #ifndef lint
37 1.49 lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
38 1.49 lukem The Regents of the University of California. All rights reserved.");
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.6 jtc #if 0
43 1.6 jtc static char sccsid[] = "@(#)tip.c 8.1 (Berkeley) 6/6/93";
44 1.6 jtc #endif
45 1.55 dholland __RCSID("$NetBSD: tip.c,v 1.55 2014/07/27 04:32:23 dholland Exp $");
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * tip - UNIX link to other systems
50 1.1 cgd * tip [-v] [-speed] system-name
51 1.1 cgd * or
52 1.44 jdc * cu [options] [phone-number|"dir"]
53 1.1 cgd */
54 1.1 cgd #include "tip.h"
55 1.1 cgd #include "pathnames.h"
56 1.1 cgd
57 1.51 joerg __dead static void tipusage(void);
58 1.44 jdc
59 1.35 perry int escape(void);
60 1.35 perry int main(int, char **);
61 1.51 joerg __dead static void intprompt(int);
62 1.51 joerg __dead static void tipin(void);
63 1.15 lukem
64 1.1 cgd char PNbuf[256]; /* This limits the size of a number */
65 1.1 cgd
66 1.26 christos static char path_phones[] = _PATH_PHONES;
67 1.26 christos
68 1.15 lukem int
69 1.35 perry main(int argc, char *argv[])
70 1.1 cgd {
71 1.25 christos char *System = NULL;
72 1.44 jdc int c, i;
73 1.15 lukem char *p;
74 1.25 christos const char *q;
75 1.1 cgd char sbuf[12];
76 1.55 dholland int cmdlineBR;
77 1.18 mellon int fcarg;
78 1.1 cgd
79 1.53 christos setprogname(argv[0]);
80 1.1 cgd gid = getgid();
81 1.1 cgd egid = getegid();
82 1.1 cgd uid = getuid();
83 1.1 cgd euid = geteuid();
84 1.53 christos if (strcmp(getprogname(), "cu") == 0) {
85 1.1 cgd cumode = 1;
86 1.1 cgd cumain(argc, argv);
87 1.1 cgd goto cucommon;
88 1.1 cgd }
89 1.1 cgd
90 1.1 cgd if (argc > 4) {
91 1.44 jdc tipusage();
92 1.1 cgd }
93 1.1 cgd if (!isatty(0)) {
94 1.46 christos (void)fprintf(stderr, "%s: must be interactive\n", getprogname());
95 1.1 cgd exit(1);
96 1.1 cgd }
97 1.1 cgd
98 1.55 dholland cmdlineBR = 0;
99 1.44 jdc while((c = getopt(argc, argv, "v0123456789")) != -1) {
100 1.44 jdc switch(c) {
101 1.1 cgd
102 1.1 cgd case 'v':
103 1.1 cgd vflag++;
104 1.1 cgd break;
105 1.1 cgd
106 1.1 cgd case '0': case '1': case '2': case '3': case '4':
107 1.1 cgd case '5': case '6': case '7': case '8': case '9':
108 1.55 dholland cmdlineBR = cmdlineBR * 10 + (c - '0');
109 1.55 dholland BR = cmdlineBR;
110 1.1 cgd break;
111 1.1 cgd
112 1.1 cgd default:
113 1.32 tls warnx("%s, unknown option", argv[1]);
114 1.1 cgd break;
115 1.1 cgd }
116 1.1 cgd }
117 1.1 cgd
118 1.44 jdc argc -= optind;
119 1.44 jdc argv += optind;
120 1.44 jdc
121 1.44 jdc if (argc != 1)
122 1.44 jdc tipusage();
123 1.44 jdc else
124 1.44 jdc System = argv[0];
125 1.44 jdc
126 1.44 jdc
127 1.25 christos if (System == NULL)
128 1.1 cgd goto notnumber;
129 1.25 christos if (isalpha((unsigned char)*System))
130 1.1 cgd goto notnumber;
131 1.1 cgd /*
132 1.1 cgd * System name is really a phone number...
133 1.1 cgd * Copy the number then stomp on the original (in case the number
134 1.1 cgd * is private, we don't want 'ps' or 'w' to find it).
135 1.1 cgd */
136 1.25 christos if (strlen(System) > sizeof PNbuf - 1) {
137 1.32 tls errx(1, "phone number too long (max = %d bytes)",
138 1.17 mrg (int)sizeof(PNbuf) - 1);
139 1.1 cgd }
140 1.27 jrf (void)strlcpy(PNbuf, System, sizeof(PNbuf));
141 1.25 christos for (p = System; *p; p++)
142 1.1 cgd *p = '\0';
143 1.1 cgd PN = PNbuf;
144 1.15 lukem (void)snprintf(sbuf, sizeof sbuf, "tip%d", (int)BR);
145 1.25 christos System = sbuf;
146 1.1 cgd
147 1.1 cgd notnumber:
148 1.1 cgd (void)signal(SIGINT, cleanup);
149 1.1 cgd (void)signal(SIGQUIT, cleanup);
150 1.1 cgd (void)signal(SIGHUP, cleanup);
151 1.1 cgd (void)signal(SIGTERM, cleanup);
152 1.1 cgd
153 1.25 christos if ((i = hunt(System)) == 0) {
154 1.46 christos (void)printf("all ports busy\n");
155 1.1 cgd exit(3);
156 1.1 cgd }
157 1.1 cgd if (i == -1) {
158 1.36 perry errx(3, "link down\n");
159 1.1 cgd }
160 1.1 cgd setbuf(stdout, NULL);
161 1.1 cgd
162 1.1 cgd /*
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.40 yamt if (ttysetup((speed_t)number(value(BAUDRATE))) != 0) {
178 1.36 perry errx(3, "bad baud rate %d",
179 1.33 tls (int)number(value(BAUDRATE)));
180 1.33 tls }
181 1.33 tls }
182 1.46 christos if ((q = tip_connect()) != NULL) {
183 1.36 perry errx(1, "\07%s\n[EOT]\n", q);
184 1.1 cgd }
185 1.33 tls if (!HW) {
186 1.40 yamt if (ttysetup((speed_t)number(value(BAUDRATE))) != 0) {
187 1.36 perry errx(3, "bad baud rate %d",
188 1.33 tls (int)number(value(BAUDRATE)));
189 1.33 tls }
190 1.33 tls }
191 1.35 perry
192 1.18 mellon
193 1.41 tls cucommon:
194 1.41 tls /*
195 1.41 tls * From here down the code is shared with
196 1.41 tls * the "cu" version of tip.
197 1.41 tls */
198 1.41 tls
199 1.19 mellon /*
200 1.19 mellon * Direct connections with no carrier require using O_NONBLOCK on
201 1.19 mellon * open, but we don't want to keep O_NONBLOCK after open because it
202 1.19 mellon * will cause busy waits.
203 1.19 mellon */
204 1.19 mellon if (DC &&
205 1.19 mellon ((fcarg = fcntl(FD, F_GETFL, 0)) < 0 ||
206 1.19 mellon fcntl(FD, F_SETFL, fcarg & ~O_NONBLOCK) < 0)) {
207 1.36 perry err(1, "can't clear O_NONBLOCK");
208 1.18 mellon }
209 1.35 perry
210 1.46 christos (void)tcgetattr(0, &defterm);
211 1.8 pk term = defterm;
212 1.8 pk term.c_lflag &= ~(ICANON|IEXTEN|ECHO);
213 1.8 pk term.c_iflag &= ~(INPCK|ICRNL);
214 1.8 pk term.c_oflag &= ~OPOST;
215 1.8 pk term.c_cc[VMIN] = 1;
216 1.8 pk term.c_cc[VTIME] = 0;
217 1.8 pk defchars = term;
218 1.8 pk term.c_cc[VINTR] = term.c_cc[VQUIT] = term.c_cc[VSUSP] =
219 1.35 perry term.c_cc[VDSUSP] = term.c_cc[VDISCARD] =
220 1.8 pk term.c_cc[VLNEXT] = _POSIX_VDISABLE;
221 1.1 cgd raw();
222 1.1 cgd
223 1.46 christos (void)pipe(attndes);
224 1.46 christos (void)pipe(fildes);
225 1.46 christos (void)pipe(repdes);
226 1.15 lukem (void)signal(SIGALRM, alrmtimeout);
227 1.1 cgd
228 1.1 cgd /*
229 1.1 cgd * Everything's set up now:
230 1.1 cgd * connection established (hardwired or dialup)
231 1.1 cgd * line conditioned (baud rate, mode, etc.)
232 1.1 cgd * internal data structures (variables)
233 1.1 cgd * so, fork one process for local side and one for remote.
234 1.1 cgd */
235 1.46 christos (void)printf("%s", cumode ? "Connected\r\n" : "\07connected\r\n");
236 1.15 lukem switch (pid = fork()) {
237 1.15 lukem default:
238 1.1 cgd tipin();
239 1.15 lukem break;
240 1.15 lukem case 0:
241 1.1 cgd tipout();
242 1.15 lukem break;
243 1.15 lukem case -1:
244 1.15 lukem err(1, "can't fork");
245 1.15 lukem }
246 1.1 cgd /*NOTREACHED*/
247 1.15 lukem exit(0); /* XXX: pacify gcc */
248 1.1 cgd }
249 1.1 cgd
250 1.1 cgd void
251 1.44 jdc tipusage(void)
252 1.44 jdc {
253 1.46 christos (void)fprintf(stderr, "usage: %s [-v] [-speed] system-name\n",
254 1.44 jdc getprogname());
255 1.46 christos (void)fprintf(stderr, " %s [-v] [-speed] phone-number\n",
256 1.44 jdc getprogname());
257 1.44 jdc exit(1);
258 1.44 jdc }
259 1.44 jdc
260 1.44 jdc void
261 1.39 christos /*ARGSUSED*/
262 1.46 christos cleanup(int dummy __unused)
263 1.1 cgd {
264 1.1 cgd
265 1.1 cgd if (odisc)
266 1.46 christos (void)ioctl(0, TIOCSETD, &odisc);
267 1.1 cgd exit(0);
268 1.1 cgd }
269 1.1 cgd
270 1.1 cgd /*
271 1.1 cgd * put the controlling keyboard into raw mode
272 1.1 cgd */
273 1.15 lukem void
274 1.35 perry raw(void)
275 1.1 cgd {
276 1.21 mrg
277 1.46 christos (void)tcsetattr(0, TCSADRAIN, &term);
278 1.1 cgd }
279 1.1 cgd
280 1.1 cgd
281 1.1 cgd /*
282 1.1 cgd * return keyboard to normal mode
283 1.1 cgd */
284 1.15 lukem void
285 1.35 perry unraw(void)
286 1.1 cgd {
287 1.21 mrg
288 1.46 christos (void)tcsetattr(0, TCSADRAIN, &defterm);
289 1.1 cgd }
290 1.1 cgd
291 1.1 cgd static jmp_buf promptbuf;
292 1.1 cgd
293 1.1 cgd /*
294 1.1 cgd * Print string ``s'', then read a string
295 1.1 cgd * in from the terminal. Handles signals & allows use of
296 1.1 cgd * normal erase and kill characters.
297 1.1 cgd */
298 1.15 lukem int
299 1.45 christos prompt(const char *s, char *volatile p, size_t l)
300 1.1 cgd {
301 1.15 lukem int c;
302 1.15 lukem char *b = p;
303 1.1 cgd sig_t oint, oquit;
304 1.1 cgd
305 1.1 cgd stoprompt = 0;
306 1.1 cgd oint = signal(SIGINT, intprompt);
307 1.1 cgd oquit = signal(SIGQUIT, SIG_IGN);
308 1.1 cgd unraw();
309 1.46 christos (void)printf("%s", s);
310 1.1 cgd if (setjmp(promptbuf) == 0)
311 1.21 mrg while ((c = getchar()) != -1 && (*p = c) != '\n' &&
312 1.21 mrg b + l > p)
313 1.1 cgd p++;
314 1.1 cgd *p = '\0';
315 1.1 cgd
316 1.1 cgd raw();
317 1.1 cgd (void)signal(SIGINT, oint);
318 1.1 cgd (void)signal(SIGQUIT, oquit);
319 1.1 cgd return (stoprompt || p == b);
320 1.1 cgd }
321 1.1 cgd
322 1.1 cgd /*
323 1.1 cgd * Interrupt service routine during prompting
324 1.1 cgd */
325 1.51 joerg static void
326 1.39 christos /*ARGSUSED*/
327 1.46 christos intprompt(int dummy __unused)
328 1.1 cgd {
329 1.1 cgd
330 1.1 cgd (void)signal(SIGINT, SIG_IGN);
331 1.1 cgd stoprompt = 1;
332 1.46 christos (void)printf("\r\n");
333 1.1 cgd longjmp(promptbuf, 1);
334 1.1 cgd }
335 1.1 cgd
336 1.1 cgd /*
337 1.1 cgd * ****TIPIN TIPIN****
338 1.1 cgd */
339 1.51 joerg static void
340 1.35 perry tipin(void)
341 1.1 cgd {
342 1.1 cgd char gch, bol = 1;
343 1.1 cgd
344 1.1 cgd /*
345 1.1 cgd * Kinda klugey here...
346 1.1 cgd * check for scripting being turned on from the .tiprc file,
347 1.1 cgd * but be careful about just using setscript(), as we may
348 1.1 cgd * send a SIGEMT before tipout has a chance to set up catching
349 1.1 cgd * it; so wait a second, then setscript()
350 1.1 cgd */
351 1.1 cgd if (boolean(value(SCRIPT))) {
352 1.46 christos (void)sleep(1);
353 1.1 cgd setscript();
354 1.1 cgd }
355 1.1 cgd
356 1.39 christos for (;;) {
357 1.8 pk gch = getchar()&STRIP_PAR;
358 1.1 cgd if ((gch == character(value(ESCAPE))) && bol) {
359 1.1 cgd if (!(gch = escape()))
360 1.1 cgd continue;
361 1.35 perry } else if (!cumode &&
362 1.16 lukem gch && gch == character(value(RAISECHAR))) {
363 1.9 cgd setboolean(value(RAISE), !boolean(value(RAISE)));
364 1.1 cgd continue;
365 1.1 cgd } else if (gch == '\r') {
366 1.1 cgd bol = 1;
367 1.20 thorpej xpwrite(FD, &gch, 1);
368 1.1 cgd if (boolean(value(HALFDUPLEX)))
369 1.46 christos (void)printf("\r\n");
370 1.1 cgd continue;
371 1.16 lukem } else if (!cumode && gch && gch == character(value(FORCE)))
372 1.8 pk gch = getchar()&STRIP_PAR;
373 1.1 cgd bol = any(gch, value(EOL));
374 1.22 christos if (boolean(value(RAISE)) && islower((unsigned char)gch))
375 1.28 dsl gch = toupper((unsigned char)gch);
376 1.20 thorpej xpwrite(FD, &gch, 1);
377 1.1 cgd if (boolean(value(HALFDUPLEX)))
378 1.46 christos (void)printf("%c", gch);
379 1.1 cgd }
380 1.1 cgd }
381 1.1 cgd
382 1.1 cgd /*
383 1.1 cgd * Escape handler --
384 1.1 cgd * called on recognition of ``escapec'' at the beginning of a line
385 1.1 cgd */
386 1.15 lukem int
387 1.35 perry escape(void)
388 1.1 cgd {
389 1.15 lukem char gch;
390 1.15 lukem esctable_t *p;
391 1.1 cgd char c = character(value(ESCAPE));
392 1.1 cgd
393 1.8 pk gch = (getchar()&STRIP_PAR);
394 1.1 cgd for (p = etable; p->e_char; p++)
395 1.1 cgd if (p->e_char == gch) {
396 1.1 cgd if ((p->e_flags&PRIV) && uid)
397 1.1 cgd continue;
398 1.46 christos (void)printf("%s", ctrl(c));
399 1.1 cgd (*p->e_func)(gch);
400 1.1 cgd return (0);
401 1.1 cgd }
402 1.1 cgd /* ESCAPE ESCAPE forces ESCAPE */
403 1.1 cgd if (c != gch)
404 1.20 thorpej xpwrite(FD, &c, 1);
405 1.1 cgd return (gch);
406 1.1 cgd }
407 1.1 cgd
408 1.15 lukem int
409 1.35 perry any(char c, const char *p)
410 1.1 cgd {
411 1.21 mrg
412 1.1 cgd while (p && *p)
413 1.1 cgd if (*p++ == c)
414 1.1 cgd return (1);
415 1.1 cgd return (0);
416 1.1 cgd }
417 1.1 cgd
418 1.1 cgd char *
419 1.35 perry interp(const char *s)
420 1.1 cgd {
421 1.1 cgd static char buf[256];
422 1.25 christos char *p = buf, c;
423 1.25 christos const char *q;
424 1.1 cgd
425 1.21 mrg while ((c = *s++) != 0 && buf + sizeof buf - p > 2) {
426 1.1 cgd for (q = "\nn\rr\tt\ff\033E\bb"; *q; q++)
427 1.1 cgd if (*q++ == c) {
428 1.1 cgd *p++ = '\\'; *p++ = *q;
429 1.1 cgd goto next;
430 1.1 cgd }
431 1.1 cgd if (c < 040) {
432 1.1 cgd *p++ = '^'; *p++ = c + 'A'-1;
433 1.1 cgd } else if (c == 0177) {
434 1.1 cgd *p++ = '^'; *p++ = '?';
435 1.1 cgd } else
436 1.1 cgd *p++ = c;
437 1.1 cgd next:
438 1.1 cgd ;
439 1.1 cgd }
440 1.1 cgd *p = '\0';
441 1.1 cgd return (buf);
442 1.1 cgd }
443 1.1 cgd
444 1.1 cgd char *
445 1.35 perry ctrl(char c)
446 1.1 cgd {
447 1.1 cgd static char s[3];
448 1.1 cgd
449 1.1 cgd if (c < 040 || c == 0177) {
450 1.1 cgd s[0] = '^';
451 1.1 cgd s[1] = c == 0177 ? '?' : c+'A'-1;
452 1.1 cgd s[2] = '\0';
453 1.1 cgd } else {
454 1.1 cgd s[0] = c;
455 1.1 cgd s[1] = '\0';
456 1.1 cgd }
457 1.1 cgd return (s);
458 1.1 cgd }
459 1.1 cgd
460 1.1 cgd /*
461 1.1 cgd * Help command
462 1.1 cgd */
463 1.15 lukem void
464 1.35 perry help(char c)
465 1.1 cgd {
466 1.15 lukem esctable_t *p;
467 1.1 cgd
468 1.46 christos (void)printf("%c\r\n", c);
469 1.1 cgd for (p = etable; p->e_char; p++) {
470 1.1 cgd if ((p->e_flags&PRIV) && uid)
471 1.1 cgd continue;
472 1.46 christos (void)printf("%2s", ctrl(character(value(ESCAPE))));
473 1.46 christos (void)printf("%-2s %c %s\r\n", ctrl(p->e_char),
474 1.1 cgd p->e_flags&EXP ? '*': ' ', p->e_help);
475 1.1 cgd }
476 1.1 cgd }
477 1.1 cgd
478 1.1 cgd /*
479 1.1 cgd * Set up the "remote" tty's state
480 1.1 cgd */
481 1.33 tls int
482 1.39 christos ttysetup(speed_t spd)
483 1.1 cgd {
484 1.8 pk struct termios cntrl;
485 1.1 cgd
486 1.46 christos (void)tcgetattr(FD, &cntrl);
487 1.46 christos (void)cfsetospeed(&cntrl, spd);
488 1.46 christos (void)cfsetispeed(&cntrl, spd);
489 1.8 pk cntrl.c_cflag &= ~(CSIZE|PARENB);
490 1.8 pk cntrl.c_cflag |= CS8;
491 1.14 mellon if (DC)
492 1.13 mellon cntrl.c_cflag |= CLOCAL;
493 1.29 yamt if (boolean(value(HARDWAREFLOW)))
494 1.35 perry cntrl.c_cflag |= CRTSCTS;
495 1.8 pk cntrl.c_iflag &= ~(ISTRIP|ICRNL);
496 1.8 pk cntrl.c_oflag &= ~OPOST;
497 1.8 pk cntrl.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
498 1.8 pk cntrl.c_cc[VMIN] = 1;
499 1.8 pk cntrl.c_cc[VTIME] = 0;
500 1.1 cgd if (boolean(value(TAND)))
501 1.54 mlelstv cntrl.c_iflag |= IXOFF|IXON;
502 1.52 christos else
503 1.54 mlelstv cntrl.c_iflag &= ~(IXOFF|IXON);
504 1.46 christos return tcsetattr(FD, TCSAFLUSH, &cntrl);
505 1.1 cgd }
506 1.1 cgd
507 1.1 cgd static char partab[0200];
508 1.1 cgd
509 1.1 cgd /*
510 1.1 cgd * Do a write to the remote machine with the correct parity.
511 1.1 cgd * We are doing 8 bit wide output, so we just generate a character
512 1.1 cgd * with the right parity and output it.
513 1.1 cgd */
514 1.15 lukem void
515 1.38 perry xpwrite(int fd, char *buf, size_t n)
516 1.1 cgd {
517 1.50 lukem size_t i;
518 1.15 lukem char *bp;
519 1.1 cgd
520 1.1 cgd bp = buf;
521 1.6 jtc if (bits8 == 0)
522 1.6 jtc for (i = 0; i < n; i++) {
523 1.6 jtc *bp = partab[(*bp) & 0177];
524 1.6 jtc bp++;
525 1.1 cgd }
526 1.6 jtc if (write(fd, buf, n) < 0) {
527 1.1 cgd if (errno == EIO)
528 1.1 cgd tipabort("Lost carrier.");
529 1.1 cgd /* this is questionable */
530 1.32 tls warn("write");
531 1.1 cgd }
532 1.1 cgd }
533 1.1 cgd
534 1.1 cgd /*
535 1.1 cgd * Build a parity table with appropriate high-order bit.
536 1.1 cgd */
537 1.15 lukem void
538 1.35 perry setparity(const char *defparity)
539 1.1 cgd {
540 1.15 lukem int i, flip, clr, set;
541 1.25 christos const char *parity;
542 1.26 christos static char *curpar;
543 1.1 cgd
544 1.39 christos if (value(PARITY) == NULL || ((char *)value(PARITY))[0] == '\0') {
545 1.26 christos if (curpar != NULL)
546 1.26 christos free(curpar);
547 1.26 christos value(PARITY) = curpar = strdup(defparity);
548 1.26 christos }
549 1.1 cgd parity = value(PARITY);
550 1.53 christos if (strcmp(parity, "none") == 0) {
551 1.1 cgd bits8 = 1;
552 1.1 cgd return;
553 1.1 cgd }
554 1.1 cgd bits8 = 0;
555 1.1 cgd flip = 0;
556 1.1 cgd clr = 0377;
557 1.1 cgd set = 0;
558 1.53 christos if (strcmp(parity, "odd") == 0)
559 1.1 cgd flip = 0200; /* reverse bit 7 */
560 1.53 christos else if (strcmp(parity, "zero") == 0)
561 1.1 cgd clr = 0177; /* turn off bit 7 */
562 1.53 christos else if (strcmp(parity, "one") == 0)
563 1.1 cgd set = 0200; /* turn on bit 7 */
564 1.53 christos else if (strcmp(parity, "even") != 0) {
565 1.46 christos (void)fprintf(stderr, "%s: unknown parity value\r\n", parity);
566 1.46 christos (void)fflush(stderr);
567 1.1 cgd }
568 1.1 cgd for (i = 0; i < 0200; i++)
569 1.15 lukem partab[i] = ((evenpartab[i] ^ flip) | set) & clr;
570 1.1 cgd }
571