1 /*	$OpenBSD: map_parse.y,v 1.3 2002/02/16 21:27:38 millert Exp $	*/
2 /*	$NetBSD: map_parse.y,v 1.2 1999/02/08 11:08:23 hannken Exp $ */
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Juergen Hannken-Illjes.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /* Parse a keyboard map. Statements are one of
41  *
42  * keysym sym1 = sym2		Assign the key containing `sym2' to
43  *				the key containing `sym1'. This is a copy
44  *				from the old to the new map. Therefore it
45  *				is possible to exchange keys.
46  *
47  * kecode pos = sym ...		assign the symbols to key `pos'.
48  *				The first symbol may be a command.
49  *				The following symbols are assigned
50  *				to the normal and altgr groups.
51  *				Missing symbols are generated automacically
52  *				as either the upper case variant or the
53  *				normal group.
54  */
55 
56 %{
57 
58 #include <sys/time.h>
59 #include <dev/wscons/wsksymdef.h>
60 #include <dev/wscons/wsconsio.h>
61 #include <err.h>
62 #include "wsconsctl.h"
63 
64 extern struct wskbd_map_data kbmap;	/* from keyboard.c */
65 
66 static struct wscons_keymap mapdata[KS_NUMKEYCODES];
67 struct wskbd_map_data newkbmap;		/* used in util.c */
68 static struct wscons_keymap *cur_mp;
69 
70 static int ksym_lookup(keysym_t);
71 
72 static int
ksym_lookup(ksym)73 ksym_lookup(ksym)
74 	keysym_t ksym;
75 {
76 	int i;
77 	struct wscons_keymap *mp;
78 
79 	for (i = 0; i < kbmap.maplen; i++) {
80 		mp = kbmap.map + i;
81 		if (mp->command == ksym ||
82 		    mp->group1[0] == ksym || mp->group1[1] == ksym ||
83 		    mp->group2[0] == ksym || mp->group2[1] == ksym)
84 			return(i);
85 	}
86 
87 	errx(1, "keysym %s not found", ksym2name(ksym));
88 }
89 
90 %}
91 
92 %union {
93 		keysym_t kval;
94 		int ival;
95 	}
96 
97 %token T_KEYSYM T_KEYCODE
98 %token <kval> T_KEYSYM_VAR T_KEYSYM_CMD_VAR
99 %token <ival> T_NUMBER
100 
101 %type <kval> keysym_var
102 
103 %%
104 
105 program		: = {
106 			int i;
107 			struct wscons_keymap *mp;
108 
109 			for (i = 0; i < KS_NUMKEYCODES; i++) {
110 				mp = mapdata + i;
111 				mp->command = KS_voidSymbol;
112 				mp->group1[0] = KS_voidSymbol;
113 				mp->group1[1] = KS_voidSymbol;
114 				mp->group2[0] = KS_voidSymbol;
115 				mp->group2[1] = KS_voidSymbol;
116 			}
117 
118 			newkbmap.maplen = 0;
119 			newkbmap.map = mapdata;
120 		} expr_list
121 		;
122 
123 expr_list	: expr
124 		| expr_list expr
125 		;
126 
127 expr		: keysym_expr
128 		| keycode_expr
129 		;
130 
131 keysym_expr	: T_KEYSYM keysym_var "=" keysym_var = {
132 			int src, dst;
133 
134 			dst = ksym_lookup($2);
135 			src = ksym_lookup($4);
136 			newkbmap.map[dst] = kbmap.map[src];
137 			if (dst >= newkbmap.maplen)
138 				newkbmap.maplen = dst + 1;
139 		}
140 		;
141 
142 keycode_expr	: T_KEYCODE T_NUMBER "=" = {
143 			if ($2 >= KS_NUMKEYCODES)
144 				errx(1, "%d: keycode too large", $2);
145 			if ($2 >= newkbmap.maplen)
146 				newkbmap.maplen = $2 + 1;
147 			cur_mp = mapdata + $2;
148 		} keysym_cmd keysym_list
149 		;
150 
151 keysym_cmd	: /* empty */
152 		| T_KEYSYM_CMD_VAR = {
153 			cur_mp->command = $1;
154 		}
155 		;
156 
157 keysym_list	: keysym_var = {
158 			cur_mp->group1[0] = $1;
159 			cur_mp->group1[1] = ksym_upcase(cur_mp->group1[0]);
160 			cur_mp->group2[0] = cur_mp->group1[0];
161 			cur_mp->group2[1] = cur_mp->group1[1];
162 		}
163 		| keysym_var keysym_var = {
164 			cur_mp->group1[0] = $1;
165 			cur_mp->group1[1] = $2;
166 			cur_mp->group2[0] = cur_mp->group1[0];
167 			cur_mp->group2[1] = cur_mp->group1[1];
168 		}
169 		| keysym_var keysym_var keysym_var = {
170 			cur_mp->group1[0] = $1;
171 			cur_mp->group1[1] = $2;
172 			cur_mp->group2[0] = $3;
173 			cur_mp->group2[1] = ksym_upcase(cur_mp->group2[0]);
174 		}
175 		| keysym_var keysym_var keysym_var keysym_var = {
176 			cur_mp->group1[0] = $1;
177 			cur_mp->group1[1] = $2;
178 			cur_mp->group2[0] = $3;
179 			cur_mp->group2[1] = $4;
180 		}
181 		;
182 
183 keysym_var	: T_KEYSYM_VAR = {
184 			$$ = $1;
185 		}
186 		| T_NUMBER = {
187 			char name[2];
188 			int res;
189 
190 			if ($1 < 0 || $1 > 9)
191 				yyerror("keysym expected");
192 			name[0] = $1 + '0';
193 			name[1] = '\0';
194 			res = name2ksym(name);
195 			if (res < 0)
196 				yyerror("keysym expected");
197 			$$ = res;
198 		}
199 		;
200 %%
201 
202 void
yyerror(msg)203 yyerror(msg)
204 	char *msg;
205 {
206 	errx(1, "parse: %s", msg);
207 }
208