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