Hide syms array
[platform/upstream/kbd.git] / src / libkeymap / parser.y
1 /* parser.y
2  *
3  * This file is part of kbd project.
4  * Copyright (C) 1993  Risto Kankkunen.
5  * Copyright (C) 1993  Eugene G. Crosser.
6  * Copyright (C) 1994-2007  Andries E. Brouwer.
7  * Copyright (C) 2007-2013  Alexey Gladkov <gladkov.alexey@gmail.com>
8  *
9  * This file is covered by the GNU General Public License,
10  * which should be included with kbd as the file COPYING.
11  */
12 %{
13 #define YY_HEADER_EXPORT_START_CONDITIONS 1
14
15 #include "nls.h"
16 #include "kbd.h"
17
18 #include "contextP.h"
19 #include "ksyms.h"
20 #include "modifiers.h"
21
22 #include "parser.h"
23 #include "analyze.h"
24 %}
25
26 %code requires {
27 #include "keymap.h"
28
29 #ifndef STRDATA_STRUCT
30 #define STRDATA_STRUCT
31 #define MAX_PARSER_STRING 512
32 struct strdata {
33         unsigned int len;
34         unsigned char data[MAX_PARSER_STRING];
35 };
36 #endif
37 }
38
39 %language "C"
40 %yacc
41 %defines
42 %debug
43 %error-verbose
44
45 /* Pure yylex.  */
46 %define api.pure
47 %lex-param { yyscan_t scanner }
48
49 /* Pure yyparse.  */
50 %parse-param { void *scanner }
51 %parse-param { struct lk_ctx *ctx }
52
53 %token EOL NUMBER LITERAL CHARSET KEYMAPS KEYCODE EQUALS
54 %token PLAIN SHIFT CONTROL ALT ALTGR SHIFTL SHIFTR CTRLL CTRLR CAPSSHIFT
55 %token COMMA DASH STRING STRLITERAL COMPOSE TO CCHAR ERROR PLUS
56 %token UNUMBER ALT_IS_META STRINGS AS USUAL ON FOR
57
58 %union {
59         long long int num;
60         struct strdata str;
61 }
62
63 %type <str>  STRLITERAL
64 %type <num>  CCHAR
65 %type <num>  LITERAL
66 %type <num>  NUMBER
67 %type <num>  UNUMBER
68 %type <num>  compsym
69 %type <num>  rvalue
70
71 %{
72 static int
73 yyerror(yyscan_t scanner __attribute__ ((unused)),
74         struct lk_ctx *ctx, const char *s)
75 {
76         ERR(ctx, "%s", s);
77         return 0;
78 }
79
80 static int
81 strings_as_usual(struct lk_ctx *ctx)
82 {
83         /*
84          * 26 strings, mostly inspired by the VT100 family
85          */
86         char *stringvalues[30] = {
87                 /* F1 .. F20 */
88                 "\033[[A",  "\033[[B",  "\033[[C",  "\033[[D",  "\033[[E",
89                 "\033[17~", "\033[18~", "\033[19~", "\033[20~", "\033[21~",
90                 "\033[23~", "\033[24~", "\033[25~", "\033[26~",
91                 "\033[28~", "\033[29~",
92                 "\033[31~", "\033[32~", "\033[33~", "\033[34~",
93                 /* Find,    Insert,     Remove,     Select,     Prior */
94                 "\033[1~",  "\033[2~",  "\033[3~",  "\033[4~",  "\033[5~",
95                 /* Next,    Macro,      Help,       Do,         Pause */
96                 "\033[6~",  0,          0,          0,          0
97         };
98         int i;
99
100         for (i = 0; i < 30; i++) {
101                 if (stringvalues[i]) {
102                         struct kbsentry ke;
103                         ke.kb_func = i;
104                         strncpy((char *)ke.kb_string, stringvalues[i],
105                                 sizeof(ke.kb_string));
106                         ke.kb_string[sizeof(ke.kb_string) - 1] = 0;
107
108                         if (lk_add_func(ctx, ke) == -1)
109                                 return -1;
110                 }
111         }
112         return 0;
113 }
114
115 static int
116 compose_as_usual(struct lk_ctx *ctx, char *charset)
117 {
118         if (charset && strcmp(charset, "iso-8859-1")) {
119                 ERR(ctx, _("loadkeys: don't know how to compose for %s"), charset);
120                 return -1;
121
122         } else {
123                 struct ccc {
124                         unsigned char c1, c2, c3;
125                 } def_latin1_composes[68] = {
126                         { '`', 'A', 0300 }, { '`', 'a', 0340 },
127                         { '\'', 'A', 0301 }, { '\'', 'a', 0341 },
128                         { '^', 'A', 0302 }, { '^', 'a', 0342 },
129                         { '~', 'A', 0303 }, { '~', 'a', 0343 },
130                         { '"', 'A', 0304 }, { '"', 'a', 0344 },
131                         { 'O', 'A', 0305 }, { 'o', 'a', 0345 },
132                         { '0', 'A', 0305 }, { '0', 'a', 0345 },
133                         { 'A', 'A', 0305 }, { 'a', 'a', 0345 },
134                         { 'A', 'E', 0306 }, { 'a', 'e', 0346 },
135                         { ',', 'C', 0307 }, { ',', 'c', 0347 },
136                         { '`', 'E', 0310 }, { '`', 'e', 0350 },
137                         { '\'', 'E', 0311 }, { '\'', 'e', 0351 },
138                         { '^', 'E', 0312 }, { '^', 'e', 0352 },
139                         { '"', 'E', 0313 }, { '"', 'e', 0353 },
140                         { '`', 'I', 0314 }, { '`', 'i', 0354 },
141                         { '\'', 'I', 0315 }, { '\'', 'i', 0355 },
142                         { '^', 'I', 0316 }, { '^', 'i', 0356 },
143                         { '"', 'I', 0317 }, { '"', 'i', 0357 },
144                         { '-', 'D', 0320 }, { '-', 'd', 0360 },
145                         { '~', 'N', 0321 }, { '~', 'n', 0361 },
146                         { '`', 'O', 0322 }, { '`', 'o', 0362 },
147                         { '\'', 'O', 0323 }, { '\'', 'o', 0363 },
148                         { '^', 'O', 0324 }, { '^', 'o', 0364 },
149                         { '~', 'O', 0325 }, { '~', 'o', 0365 },
150                         { '"', 'O', 0326 }, { '"', 'o', 0366 },
151                         { '/', 'O', 0330 }, { '/', 'o', 0370 },
152                         { '`', 'U', 0331 }, { '`', 'u', 0371 },
153                         { '\'', 'U', 0332 }, { '\'', 'u', 0372 },
154                         { '^', 'U', 0333 }, { '^', 'u', 0373 },
155                         { '"', 'U', 0334 }, { '"', 'u', 0374 },
156                         { '\'', 'Y', 0335 }, { '\'', 'y', 0375 },
157                         { 'T', 'H', 0336 }, { 't', 'h', 0376 },
158                         { 's', 's', 0337 }, { '"', 'y', 0377 },
159                         { 's', 'z', 0337 }, { 'i', 'j', 0377 }
160                 };
161                 int i;
162                 for (i = 0; i < 68; i++) {
163                         struct ccc ptr = def_latin1_composes[i];
164                         if (lk_add_compose(ctx, ptr.c1, ptr.c2, ptr.c3) == -1)
165                                 return -1;
166                 }
167         }
168         return 0;
169 }
170
171 %}
172
173 %%
174 keytable        :
175                 | keytable line
176                 ;
177 line            : EOL
178                 | charsetline
179                 | altismetaline
180                 | usualstringsline
181                 | usualcomposeline
182                 | keymapline
183                 | singleline
184                 | strline
185                 | compline
186                 ;
187 charsetline     : CHARSET STRLITERAL EOL
188                         {
189                                 if (lk_set_charset(ctx, (char *) $2.data)) {
190                                         ERR(ctx,
191                                                 _("unknown charset %s - ignoring charset request\n"),
192                                                 (char *) $2.data);
193                                         YYERROR;
194                                 }
195                                 ctx->keywords |= LK_KEYWORD_CHARSET;
196
197                                 /* Unicode: The first 256 code points were made
198                                    identical to the content of ISO 8859-1 */
199                                 if (ctx->flags & LK_FLAG_PREFER_UNICODE &&
200                                     !strcasecmp((char *) $2.data, "iso-8859-1"))
201                                         ctx->flags ^= LK_FLAG_PREFER_UNICODE;
202                         }
203                 ;
204 altismetaline   : ALT_IS_META EOL
205                         {
206                                 ctx->keywords |= LK_KEYWORD_ALTISMETA;
207                         }
208                 ;
209 usualstringsline: STRINGS AS USUAL EOL
210                         {
211                                 if (strings_as_usual(ctx) == -1)
212                                         YYERROR;
213                                 ctx->keywords |= LK_KEYWORD_STRASUSUAL;
214                         }
215                 ;
216 usualcomposeline: COMPOSE AS USUAL FOR STRLITERAL EOL
217                         {
218                                 if (compose_as_usual(ctx, (char *) $5.data) == -1)
219                                         YYERROR;
220                         }
221                   | COMPOSE AS USUAL EOL
222                         {
223                                 if (compose_as_usual(ctx, 0) == -1)
224                                         YYERROR;
225                         }
226                 ;
227 keymapline      : KEYMAPS range EOL
228                         {
229                                 ctx->keywords |= LK_KEYWORD_KEYMAPS;
230                         }
231                 ;
232 range           : range COMMA range0
233                 | range0
234                 ;
235 range0          : NUMBER DASH NUMBER
236                         {
237                                 int i;
238                                 for (i = $1; i <= $3; i++) {
239                                         if (lk_add_map(ctx, i) == -1)
240                                                 YYERROR;
241                                 }
242                         }
243                 | NUMBER
244                         {
245                                 if (lk_add_map(ctx, $1) == -1)
246                                         YYERROR;
247                         }
248                 ;
249 strline         : STRING LITERAL EQUALS STRLITERAL EOL
250                         {
251                                 struct kbsentry ke;
252
253                                 if (KTYP($2) != KT_FN) {
254                                         ERR(ctx, _("'%s' is not a function key symbol"),
255                                                 get_sym(ctx, KTYP($2), KVAL($2)));
256                                         YYERROR;
257                                 }
258
259                                 ke.kb_func = KVAL($2);
260                                 strncpy((char *) ke.kb_string,
261                                         (char *) $4.data,
262                                         sizeof(ke.kb_string));
263                                 ke.kb_string[sizeof(ke.kb_string) - 1] = 0;
264
265                                 if (lk_add_func(ctx, ke) == -1)
266                                         YYERROR;
267                         }
268                 ;
269 compline        : COMPOSE compsym compsym TO compsym EOL
270                         {
271                                 if (lk_add_compose(ctx, $2, $3, $5) == -1)
272                                         YYERROR;
273                         }
274                  | COMPOSE compsym compsym TO rvalue EOL
275                         {
276                                 if (lk_add_compose(ctx, $2, $3, $5) == -1)
277                                         YYERROR;
278                         }
279                 ;
280 compsym         : CCHAR         {       $$ = $1;                }
281                 | UNUMBER       {       $$ = $1 ^ 0xf000;       }
282                 ;
283 singleline      : KEYCODE NUMBER EQUALS rvalue0 EOL
284                         {
285                                 unsigned int j, i, keycode;
286                                 int *val;
287
288                                 if (ctx->key_line->count == 1) {
289                                         char one = 1;
290                                         /* Some files do not have a keymaps line, and
291                                          * we have to wait until all input has been read
292                                          * before we know which maps to fill. */
293                                         lk_array_set(ctx->key_constant, $2, &one);
294
295                                         /* On the other hand, we now have include files,
296                                          * and it should be possible to override lines
297                                          * from an include file. So, kill old defs. */
298                                         for (j = 0; j < ctx->keymap->total; j++) {
299                                                 if (!lk_map_exists(ctx, j))
300                                                         continue;
301
302                                                 if (lk_del_key(ctx, j, $2) < 0)
303                                                         YYERROR;
304                                         }
305                                 }
306
307                                 if (ctx->keywords & LK_KEYWORD_KEYMAPS) {
308                                         i = 0;
309
310                                         for (j = 0; j < ctx->keymap->total; j++) {
311                                                 if (!lk_map_exists(ctx, j))
312                                                         continue;
313
314                                                 if (ctx->key_line->count != 1 || i == 0) {
315                                                         keycode = K_HOLE;
316
317                                                         if (i < ctx->key_line->count) {
318                                                                 val = lk_array_get(ctx->key_line, i);
319                                                                 keycode = *val;
320                                                         }
321
322                                                         if (lk_add_key(ctx, j, $2, keycode) < 0)
323                                                                 YYERROR;
324                                                 }
325                                                 i++;
326                                         }
327
328                                         if (i < ctx->key_line->count) {
329                                                 ERR(ctx, _("too many (%d) entries on one line"),
330                                                         ctx->key_line->count);
331                                                 YYERROR;
332                                         }
333                                 } else {
334                                         for (i = 0; i < ctx->key_line->count; i++) {
335                                                 val = lk_array_get(ctx->key_line, i);
336
337                                                 if (lk_add_key(ctx, i, $2, *val) < 0)
338                                                         YYERROR;
339                                         }
340                                 }
341                         }
342
343                 | modifiers KEYCODE NUMBER EQUALS rvalue EOL
344                         {
345                                 if (lk_add_key(ctx, ctx->mod, $3, $5) < 0)
346                                         YYERROR;
347                                 ctx->mod = 0;
348                         }
349                 | PLAIN KEYCODE NUMBER EQUALS rvalue EOL
350                         {
351                                 if (lk_add_key(ctx, 0, $3, $5) < 0)
352                                         YYERROR;
353                                 ctx->mod = 0;
354                         }
355                 ;
356 modifiers       : modifiers modifier
357                 | modifier
358                 ;
359 modifier        : SHIFT         { ctx->mod |= M_SHIFT;  }
360                 | CONTROL       { ctx->mod |= M_CTRL;   }
361                 | ALT           { ctx->mod |= M_ALT;            }
362                 | ALTGR         { ctx->mod |= M_ALTGR;  }
363                 | SHIFTL        { ctx->mod |= M_SHIFTL; }
364                 | SHIFTR        { ctx->mod |= M_SHIFTR; }
365                 | CTRLL         { ctx->mod |= M_CTRLL;  }
366                 | CTRLR         { ctx->mod |= M_CTRLR;  }
367                 | CAPSSHIFT     { ctx->mod |= M_CAPSSHIFT;      }
368                 ;
369                 ;
370
371 rvalue0         :
372                 | rvalue1 rvalue0
373                 ;
374 rvalue1         : rvalue
375                         {
376                                 int val = $1;
377                                 lk_array_append(ctx->key_line, &val);
378                         }
379                 ;
380 rvalue          : NUMBER        { $$ = convert_code(ctx, $1, TO_AUTO);          }
381                 | PLUS NUMBER   { $$ = add_capslock(ctx, $2);                   }
382                 | UNUMBER       { $$ = convert_code(ctx, $1^0xf000, TO_AUTO);   }
383                 | PLUS UNUMBER  { $$ = add_capslock(ctx, $2^0xf000);            }
384                 | LITERAL       { $$ = $1;                                      }
385                 | PLUS LITERAL  { $$ = add_capslock(ctx, $2);                   }
386                 ;
387 %%
388
389 int
390 lk_parse_keymap(struct lk_ctx *ctx, lkfile_t *f)
391 {
392         yyscan_t scanner;
393         int rc = -1;
394
395         ctx->mod = 0;
396
397         yylex_init(&scanner);
398         yylex_init_extra(ctx, &scanner);
399
400         INFO(ctx, _("Loading %s"), f->pathname);
401
402         if (stack_push(ctx, f, scanner) == -1)
403                 goto fail;
404
405         if (yyparse(scanner, ctx))
406                 goto fail;
407
408         rc = 0;
409
410         stack_pop(ctx, scanner);
411
412  fail:  yylex_destroy(scanner);
413         return rc;
414 }