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