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