Move lk_ctx content to private part of library
[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                 | fullline
184                 | singleline
185                 | strline
186                 | compline
187                 ;
188 charsetline     : CHARSET STRLITERAL EOL
189                         {
190                                 if (lk_set_charset(ctx, (char *) $2.data)) {
191                                         ERR(ctx,
192                                                 _("unknown charset %s - ignoring charset request\n"),
193                                                 (char *) $2.data);
194                                         YYERROR;
195                                 }
196                                 ctx->keywords |= LK_KEYWORD_CHARSET;
197
198                                 /* Unicode: The first 256 code points were made
199                                    identical to the content of ISO 8859-1 */
200                                 if (ctx->flags & LK_FLAG_PREFER_UNICODE &&
201                                     !strcasecmp((char *) $2.data, "iso-8859-1"))
202                                         ctx->flags ^= LK_FLAG_PREFER_UNICODE;
203                         }
204                 ;
205 altismetaline   : ALT_IS_META EOL
206                         {
207                                 ctx->keywords |= LK_KEYWORD_ALTISMETA;
208                         }
209                 ;
210 usualstringsline: STRINGS AS USUAL EOL
211                         {
212                                 if (strings_as_usual(ctx) == -1)
213                                         YYERROR;
214                                 ctx->keywords |= LK_KEYWORD_STRASUSUAL;
215                         }
216                 ;
217 usualcomposeline: COMPOSE AS USUAL FOR STRLITERAL EOL
218                         {
219                                 if (compose_as_usual(ctx, (char *) $5.data) == -1)
220                                         YYERROR;
221                         }
222                   | COMPOSE AS USUAL EOL
223                         {
224                                 if (compose_as_usual(ctx, 0) == -1)
225                                         YYERROR;
226                         }
227                 ;
228 keymapline      : KEYMAPS range EOL
229                         {
230                                 ctx->keywords |= LK_KEYWORD_KEYMAPS;
231                         }
232                 ;
233 range           : range COMMA range0
234                 | range0
235                 ;
236 range0          : NUMBER DASH NUMBER
237                         {
238                                 int i;
239                                 for (i = $1; i <= $3; i++) {
240                                         if (lk_add_map(ctx, i) == -1)
241                                                 YYERROR;
242                                 }
243                         }
244                 | NUMBER
245                         {
246                                 if (lk_add_map(ctx, $1) == -1)
247                                         YYERROR;
248                         }
249                 ;
250 strline         : STRING LITERAL EQUALS STRLITERAL EOL
251                         {
252                                 struct kbsentry ke;
253
254                                 if (KTYP($2) != KT_FN) {
255                                         ERR(ctx, _("'%s' is not a function key symbol"),
256                                                 syms[KTYP($2)].table[KVAL($2)]);
257                                         YYERROR;
258                                 }
259
260                                 ke.kb_func = KVAL($2);
261                                 strncpy((char *) ke.kb_string,
262                                         (char *) $4.data,
263                                         sizeof(ke.kb_string));
264                                 ke.kb_string[sizeof(ke.kb_string) - 1] = 0;
265
266                                 if (lk_add_func(ctx, ke) == -1)
267                                         YYERROR;
268                         }
269                 ;
270 compline        : COMPOSE compsym compsym TO compsym EOL
271                         {
272                                 if (lk_add_compose(ctx, $2, $3, $5) == -1)
273                                         YYERROR;
274                         }
275                  | COMPOSE compsym compsym TO rvalue EOL
276                         {
277                                 if (lk_add_compose(ctx, $2, $3, $5) == -1)
278                                         YYERROR;
279                         }
280                 ;
281 compsym         : CCHAR         {       $$ = $1;                }
282                 | UNUMBER       {       $$ = $1 ^ 0xf000;       }
283                 ;
284 singleline      :       {
285                                 ctx->mod = 0;
286                         }
287                   modifiers KEYCODE NUMBER EQUALS rvalue EOL
288                         {
289                                 if (lk_add_key(ctx, ctx->mod, $4, $6) < 0)
290                                         YYERROR;
291                         }
292                 | PLAIN KEYCODE NUMBER EQUALS rvalue EOL
293                         {
294                                 if (lk_add_key(ctx, 0, $3, $5) < 0)
295                                         YYERROR;
296                         }
297                 ;
298 modifiers       : modifiers modifier
299                 | modifier
300                 ;
301 modifier        : SHIFT         { ctx->mod |= M_SHIFT;  }
302                 | CONTROL       { ctx->mod |= M_CTRL;   }
303                 | ALT           { ctx->mod |= M_ALT;            }
304                 | ALTGR         { ctx->mod |= M_ALTGR;  }
305                 | SHIFTL        { ctx->mod |= M_SHIFTL; }
306                 | SHIFTR        { ctx->mod |= M_SHIFTR; }
307                 | CTRLL         { ctx->mod |= M_CTRLL;  }
308                 | CTRLR         { ctx->mod |= M_CTRLR;  }
309                 | CAPSSHIFT     { ctx->mod |= M_CAPSSHIFT;      }
310                 ;
311 fullline        : KEYCODE NUMBER EQUALS rvalue0 EOL
312                         {
313                                 unsigned int j, i, keycode;
314                                 int *val;
315
316                                 if (ctx->key_line->count == 1) {
317                                         char one = 1;
318                                         /* Some files do not have a keymaps line, and
319                                          * we have to wait until all input has been read
320                                          * before we know which maps to fill. */
321                                         lk_array_set(ctx->key_constant, $2, &one);
322
323                                         /* On the other hand, we now have include files,
324                                          * and it should be possible to override lines
325                                          * from an include file. So, kill old defs. */
326                                         for (j = 0; j < ctx->keymap->total; j++) {
327                                                 if (!lk_map_exists(ctx, j))
328                                                         continue;
329
330                                                 if (lk_del_key(ctx, j, $2) < 0)
331                                                         YYERROR;
332                                         }
333                                 }
334
335                                 if (ctx->keywords & LK_KEYWORD_KEYMAPS) {
336                                         i = 0;
337
338                                         for (j = 0; j < ctx->keymap->total; j++) {
339                                                 if (!lk_map_exists(ctx, j))
340                                                         continue;
341
342                                                 if (ctx->key_line->count != 1 || i == 0) {
343                                                         keycode = K_HOLE;
344
345                                                         if (i < ctx->key_line->count) {
346                                                                 val = lk_array_get(ctx->key_line, i);
347                                                                 keycode = *val;
348                                                         }
349
350                                                         if (lk_add_key(ctx, j, $2, keycode) < 0)
351                                                                 YYERROR;
352                                                 }
353                                                 i++;
354                                         }
355
356                                         if (i < ctx->key_line->count) {
357                                                 ERR(ctx, _("too many (%d) entries on one line"),
358                                                         ctx->key_line->count);
359                                                 YYERROR;
360                                         }
361                                 } else {
362                                         for (i = 0; i < ctx->key_line->count; i++) {
363                                                 val = lk_array_get(ctx->key_line, i);
364
365                                                 if (lk_add_key(ctx, i, $2, *val) < 0)
366                                                         YYERROR;
367                                         }
368                                 }
369                         }
370                 ;
371
372 rvalue0         :
373                 | rvalue1 rvalue0
374                 ;
375 rvalue1         : rvalue
376                         {
377                                 int val = $1;
378                                 lk_array_append(ctx->key_line, &val);
379                         }
380                 ;
381 rvalue          : NUMBER        { $$ = convert_code(ctx, $1, TO_AUTO);          }
382                 | PLUS NUMBER   { $$ = add_capslock(ctx, $2);                   }
383                 | UNUMBER       { $$ = convert_code(ctx, $1^0xf000, TO_AUTO);   }
384                 | PLUS UNUMBER  { $$ = add_capslock(ctx, $2^0xf000);            }
385                 | LITERAL       { $$ = $1;                                      }
386                 | PLUS LITERAL  { $$ = add_capslock(ctx, $2);                   }
387                 ;
388 %%
389
390 int
391 lk_parse_keymap(struct lk_ctx *ctx, lkfile_t *f)
392 {
393         yyscan_t scanner;
394         int rc = -1;
395
396         yylex_init(&scanner);
397         yylex_init_extra(ctx, &scanner);
398
399         INFO(ctx, _("Loading %s"), f->pathname);
400
401         if (stack_push(ctx, f, scanner) == -1)
402                 goto fail;
403
404         if (yyparse(scanner, ctx))
405                 goto fail;
406
407         rc = 0;
408
409         stack_pop(ctx, scanner);
410
411  fail:  yylex_destroy(scanner);
412         return rc;
413 }