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

Annotation of src/usr.bin/tset/map.c, Revision 1.2

1.2     ! deraadt     1: /*     $OpenBSD: map.c,v 1.4 1994/12/07 05:08:07 jtc Exp $     */
1.1       deraadt     2: /*     $NetBSD: map.c,v 1.4 1994/12/07 05:08:07 jtc Exp $      */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1991, 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.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char sccsid[] = "@(#)map.c      8.1 (Berkeley) 6/9/93";
                     40: #endif
1.2     ! deraadt    41: static char rcsid[] = "$OpenBSD: map.c,v 1.4 1994/12/07 05:08:07 jtc Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: #include <sys/types.h>
                     45: #include <termios.h>
                     46: #include <errno.h>
                     47: #include <stdlib.h>
                     48: #include <string.h>
                     49: #include "extern.h"
                     50:
                     51: int    baudrate __P((char *));
                     52:
                     53: /* Baud rate conditionals for mapping. */
                     54: #define        GT              0x01
                     55: #define        EQ              0x02
                     56: #define        LT              0x04
                     57: #define        NOT             0x08
                     58: #define        GE              (GT | EQ)
                     59: #define        LE              (LT | EQ)
                     60:
                     61: typedef struct map {
                     62:        struct map *next;       /* Linked list of maps. */
                     63:        char *porttype;         /* Port type, or "" for any. */
                     64:        char *type;             /* Terminal type to select. */
                     65:        int conditional;        /* Baud rate conditionals bitmask. */
                     66:        int speed;              /* Baud rate to compare against. */
                     67: } MAP;
                     68:
                     69: MAP *cur, *maplist;
                     70:
                     71: /*
                     72:  * Syntax for -m:
                     73:  * [port-type][test baudrate]:terminal-type
                     74:  * The baud rate tests are: >, <, @, =, !
                     75:  */
                     76: void
                     77: add_mapping(port, arg)
                     78:        char *port, *arg;
                     79: {
                     80:        MAP *mapp;
                     81:        char *copy, *p, *termp;
                     82:
                     83:        copy = strdup(arg);
                     84:        mapp = malloc((u_int)sizeof(MAP));
                     85:        if (copy == NULL || mapp == NULL)
                     86:                err("%s", strerror(errno));
                     87:        mapp->next = NULL;
                     88:        if (maplist == NULL)
                     89:                cur = maplist = mapp;
                     90:        else {
                     91:                cur->next = mapp;
                     92:                cur =  mapp;
                     93:        }
                     94:
                     95:        mapp->porttype = arg;
                     96:        mapp->conditional = 0;
                     97:
                     98:        arg = strpbrk(arg, "><@=!:");
                     99:
                    100:        if (arg == NULL) {                      /* [?]term */
                    101:                mapp->type = mapp->porttype;
                    102:                mapp->porttype = NULL;
                    103:                goto done;
                    104:        }
                    105:
                    106:        if (arg == mapp->porttype)              /* [><@=! baud]:term */
                    107:                termp = mapp->porttype = NULL;
                    108:        else
                    109:                termp = arg;
                    110:
                    111:        for (;; ++arg)                          /* Optional conditionals. */
                    112:                switch(*arg) {
                    113:                case '<':
                    114:                        if (mapp->conditional & GT)
                    115:                                goto badmopt;
                    116:                        mapp->conditional |= LT;
                    117:                        break;
                    118:                case '>':
                    119:                        if (mapp->conditional & LT)
                    120:                                goto badmopt;
                    121:                        mapp->conditional |= GT;
                    122:                        break;
                    123:                case '@':
                    124:                case '=':                       /* Not documented. */
                    125:                        mapp->conditional |= EQ;
                    126:                        break;
                    127:                case '!':
                    128:                        mapp->conditional |= NOT;
                    129:                        break;
                    130:                default:
                    131:                        goto next;
                    132:                }
                    133:
                    134: next:  if (*arg == ':') {
                    135:                if (mapp->conditional)
                    136:                        goto badmopt;
                    137:                ++arg;
                    138:        } else {                                /* Optional baudrate. */
                    139:                arg = index(p = arg, ':');
                    140:                if (arg == NULL)
                    141:                        goto badmopt;
                    142:                *arg++ = '\0';
                    143:                mapp->speed = baudrate(p);
                    144:        }
                    145:
                    146:        if (*arg == NULL)                       /* Non-optional type. */
                    147:                goto badmopt;
                    148:
                    149:        mapp->type = arg;
                    150:
                    151:        /* Terminate porttype, if specified. */
                    152:        if (termp != NULL)
                    153:                *termp = '\0';
                    154:
                    155:        /* If a NOT conditional, reverse the test. */
                    156:        if (mapp->conditional & NOT)
                    157:                mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
                    158:
                    159:        /* If user specified a port with an option flag, set it. */
                    160: done:  if (port) {
                    161:                if (mapp->porttype)
                    162: badmopt:               err("illegal -m option format: %s", copy);
                    163:                mapp->porttype = port;
                    164:        }
                    165:
                    166: #ifdef MAPDEBUG
                    167:        (void)printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
                    168:        (void)printf("type: %s\n", mapp->type);
                    169:        (void)printf("conditional: ");
                    170:        p = "";
                    171:        if (mapp->conditional & GT) {
                    172:                (void)printf("GT");
                    173:                p = "/";
                    174:        }
                    175:        if (mapp->conditional & EQ) {
                    176:                (void)printf("%sEQ", p);
                    177:                p = "/";
                    178:        }
                    179:        if (mapp->conditional & LT)
                    180:                (void)printf("%sLT", p);
                    181:        (void)printf("\nspeed: %d\n", mapp->speed);
                    182: #endif
                    183: }
                    184:
                    185: /*
                    186:  * Return the type of terminal to use for a port of type 'type', as specified
                    187:  * by the first applicable mapping in 'map'.  If no mappings apply, return
                    188:  * 'type'.
                    189:  */
                    190: char *
                    191: mapped(type)
                    192:        char *type;
                    193: {
                    194:        MAP *mapp;
                    195:        int match;
                    196:
                    197:        for (mapp = maplist; mapp; mapp = mapp->next)
                    198:                if (mapp->porttype == NULL || !strcmp(mapp->porttype, type)) {
                    199:                        switch (mapp->conditional) {
                    200:                        case 0:                 /* No test specified. */
                    201:                                match = 1;
                    202:                                break;
                    203:                        case EQ:
                    204:                                match = (ospeed == mapp->speed);
                    205:                                break;
                    206:                        case GE:
                    207:                                match = (ospeed >= mapp->speed);
                    208:                                break;
                    209:                        case GT:
                    210:                                match = (ospeed > mapp->speed);
                    211:                                break;
                    212:                        case LE:
                    213:                                match = (ospeed <= mapp->speed);
                    214:                                break;
                    215:                        case LT:
                    216:                                match = (ospeed < mapp->speed);
                    217:                                break;
                    218:                        }
                    219:                        if (match)
                    220:                                return (mapp->type);
                    221:                }
                    222:        /* No match found; return given type. */
                    223:        return (type);
                    224: }
                    225:
                    226: int
                    227: baudrate(rate)
                    228:        char *rate;
                    229: {
                    230:
                    231:        /* The baudrate number can be preceded by a 'B', which is ignored. */
                    232:        if (*rate == 'B')
                    233:                ++rate;
                    234:
                    235:        return (atoi(rate));
                    236: }