[BACK]Return to tip.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tip

Annotation of src/usr.bin/tip/tip.c, Revision 1.36

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