2 * Copyright (c) 1992, 1993 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
39 "@(#)keycap.c, 3.20, Last Edit-Date: [Tue Dec 20 14:51:50 1994]";
41 /*---------------------------------------------------------------------------*
43 * keycap.c Keyboard capabilities database handling
44 * -------------------------------------------------------
46 * converted from printcap by Holger Veit (veit@du9ds3.uni-duisburg.de)
48 * BUG: Should use a "last" pointer in tbuf, so that searching
49 * for capabilities alphabetically would not be a n**2/2
50 * process when large numbers of capabilities are given.
52 * Note: If we add a last pointer now we will screw up the
53 * tc capability. We really should compile termcap.
55 * modified by Hellmuth Michaelis (hm@hcshh.hcs.de) to fit into the
56 * vt220 driver pcvt 2.0 distribution
58 * -hm header conversion & cosmetic changes for pcvt 2.0 distribution
59 * -hm debugging remapping
60 * -hm cleaning up from termcap ....
61 * -hm split off header file keycap.h
63 *---------------------------------------------------------------------------*/
70 #define KEYCAP_BUFSIZ 1024
72 #define MAXHOP 32 /* max number of tc= indirections */
76 static FILE *pfp = NULL; /* keycap data base file pointer */
78 static int hopcount; /* detect infinite loops in keycap, init 0 */
81 static int knamatch();
82 static char *kdecode();
84 /*---------------------------------------------------------------------------*
86 *---------------------------------------------------------------------------*/
87 static char *nmatch(id,cstr)
90 register n = strlen(id);
91 register char *c = cstr+n;
93 if (strncmp(id,cstr,n)==0 &&
94 (*c==':' || *c=='|' || *c=='=' || *c=='#') || *c=='@')
99 /*---------------------------------------------------------------------------*
100 * Get an entry for keyboard name in buffer bp from the keycap file.
101 * Parse is very rudimentary, we just notice escaped newlines.
102 *---------------------------------------------------------------------------*/
108 register int i = 0, cnt = 0;
109 char ibuf[KEYCAP_BUFSIZ];
116 tf = open(KEYCAP_PATH, 0);
124 cnt = read(tf, ibuf, KEYCAP_BUFSIZ);
133 if (cp > bp && cp[-1] == '\\'){
139 if (cp >= bp+KEYCAP_BUFSIZ) {
140 write(2,"Keycap entry too long\n", 23);
148 * The real work for the match.
150 if (knamatch(name)) {
157 /*---------------------------------------------------------------------------*
158 * knchktc: check the last entry, see if it's tc=xxx. If so, recursively
159 * find xxx and append that entry (minus the names) to take the place of
160 * the tc=xxx entry. Note that this works because of the left to right scan.
161 *---------------------------------------------------------------------------*/
164 register char *p, *q;
165 char tcname[16]; /* name of similar keyboard */
166 char tcbuf[KEYCAP_BUFSIZ];
167 char *holdtbuf = tbuf;
170 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
173 write(2, "Bad keycap entry\n", 18);
177 /* p now points to beginning of last field */
178 if (p[0] != 't' || p[1] != 'c')
182 while (q && *q != ':')
185 if (++hopcount > MAXHOP) {
186 write(2, "Infinite tc= loop\n", 18);
189 if (kgetent(tcbuf, tcname) != 1)
191 for (q=tcbuf; *q != ':'; q++)
193 l = p - holdtbuf + strlen(q);
194 if (l > KEYCAP_BUFSIZ) {
195 write(2, "Keycap entry too long\n", 23);
196 q[KEYCAP_BUFSIZ - (p-tbuf)] = 0;
203 /*---------------------------------------------------------------------------*
204 * knamatch deals with name matching. The first field of the keycap entry
205 * is a sequence of names separated by |'s, so we compare against each such
206 * name. The normal : terminator after the last name (before the first field)
208 *---------------------------------------------------------------------------*/
209 static int knamatch(np)
212 register char *Np, *Bp;
215 if (*Bp == '#' || *Bp == 0)
218 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
220 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
222 while (*Bp && *Bp != ':' && *Bp != '|')
224 if (*Bp == 0 || *Bp == ':')
230 /*---------------------------------------------------------------------------*
231 * Skip to the next field. Notice that this is very dumb, not knowing about
232 * \: escapes or any such. If necessary, :'s can be put into the keycap file
234 *---------------------------------------------------------------------------*/
235 static char *kskip(bp)
238 while (*bp && *bp != ':')
245 /*---------------------------------------------------------------------------*
246 * Return the (numeric) option id. Numeric options look like 'li#80' i.e.
247 * the option string is separated from the numeric value by a # character.
248 * If the option is not found we return -1. Note that we handle octal
249 * numbers beginning with 0.
250 *---------------------------------------------------------------------------*/
254 register int i, base;
255 register char *bp = tbuf,*xp;
261 if ((xp=nmatch(id,bp)) == 0)
263 bp = xp; /* we have an entry */
274 i *= base, i += *bp++ - '0';
279 /*---------------------------------------------------------------------------*
280 * Handle a flag option. Flag options are given "naked", i.e. followed by
281 * a : or the end of the buffer. Return 1 if we find the option, or 0 if
283 *---------------------------------------------------------------------------*/
287 register char *bp = tbuf,*xp;
293 if ((xp=nmatch(id,bp)) != 0) {
295 if (!*bp || *bp == ':')
303 /*---------------------------------------------------------------------------*
304 * Get a string valued option. These are given as 'cl=^Z'. Much decoding
305 * is done on the strings, and the strings are placed in area, which is a
306 * ref parameter which is updated. No checking on area overflow.
307 *---------------------------------------------------------------------------*/
308 char *kgetstr(id, area)
312 register char *bp = tbuf,*xp;
318 if ((xp = nmatch(id,bp)) == 0)
326 return (kdecode(bp, area));
330 /*---------------------------------------------------------------------------*
331 * kdecode does the grung work to decode the string capability escapes.
332 *---------------------------------------------------------------------------*/
333 static char *kdecode(str, area)
343 while ((c = *str++) && c != ':') {
351 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
364 c <<= 3, c |= *str++ - '0';
365 while (--i && isdigit(*str));
377 /*-------------------------------- EOF --------------------------------------*/