xkbscan: Copy, rather than assign, file name
[platform/upstream/libxkbcommon.git] / src / xkbcomp / xkbscan.l
1 /************************************************************
2  Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc.
3
4  Permission to use, copy, modify, and distribute this
5  software and its documentation for any purpose and without
6  fee is hereby granted, provided that the above copyright
7  notice appear in all copies and that both that copyright
8  notice and this permission notice appear in supporting
9  documentation, and that the name of Silicon Graphics not be
10  used in advertising or publicity pertaining to distribution
11  of the software without specific prior written permission.
12  Silicon Graphics makes no representation about the suitability
13  of this software for any purpose. It is provided "as is"
14  without any express or implied warranty.
15
16  SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18  AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19  GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22  OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23  THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25  ********************************************************/
26
27 %{
28
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <X11/Xos.h>
32
33 #include "tokens.h"
34 #include "utils.h"
35 #include "parseutils.h"
36
37 const char *yystring;
38 char *scanFile = NULL;
39 int lineNum = 0;
40
41 int scanInt;
42
43 static char *s;
44 char scanBuf[1024];
45
46 #define BUFSIZE 4096
47
48 %}
49
50 %option case-insensitive
51 %option yylineno
52 %option noyywrap
53
54 %x S_STR S_KEY
55
56 %%
57
58 "//"[^\n]*
59 "#"[^\n]*
60
61 \"                      s = scanBuf; BEGIN(S_STR);
62 \<                      s = scanBuf; BEGIN(S_KEY);
63
64 <S_STR>\"               BEGIN(INITIAL); *s = '\0'; return STRING;
65 <S_KEY>\>               BEGIN(INITIAL); *s = '\0'; return KEYNAME;
66
67 <S_STR,S_KEY>\\[0-7]{1,3} {
68                         /* octal escape sequence */
69                         int result;
70
71                         (void) sscanf( yytext + 1, "%o", &result );
72
73                         if (result > 0xff) {
74                             fprintf(stderr, "Illegal octal escape %s\n", yytext);
75                             return ERROR_TOK;
76                         }
77
78                         *s++ = result;
79                     }
80
81 <S_STR,S_KEY>\\[0-9]+ {
82                         fprintf(stderr, "Illegal octal escape %s\n", yytext);
83                         return ERROR_TOK;
84                     }
85
86 <S_STR,S_KEY>\\n        *s++ = '\n';
87 <S_STR,S_KEY>\\t        *s++ = '\t';
88 <S_STR,S_KEY>\\r        *s++ = '\r';
89 <S_STR,S_KEY>\\b        *s++ = '\b';
90 <S_STR,S_KEY>\\f        *s++ = '\f';
91 <S_STR,S_KEY>\\v        *s++ = '\v';
92 <S_STR,S_KEY>\\e        *s++ = '\033';
93
94 <S_STR,S_KEY>.          *s++ = yytext[0];
95
96 xkb_keymap              return XKB_KEYMAP;
97 xkb_keycodes            return XKB_KEYCODES;
98 xkb_types               return XKB_TYPES;
99 xkb_symbols             return XKB_SYMBOLS;
100 xkb_compat              return XKB_COMPATMAP;
101 xkb_compat_map          return XKB_COMPATMAP;
102 xkb_compatibility       return XKB_COMPATMAP;
103 xkb_compatibility_map   return XKB_COMPATMAP;
104 xkb_geometry            return XKB_GEOMETRY;
105 xkb_semantics           return XKB_SEMANTICS;
106 xkb_layout              return XKB_LAYOUT;
107 include                 return INCLUDE;
108 override                return OVERRIDE;
109 augment                 return AUGMENT;
110 replace                 return REPLACE;
111 alternate               return ALTERNATE;
112 partial                 return PARTIAL;
113 default                 return DEFAULT;
114 hidden                  return HIDDEN;
115 virtual_modifiers       return VIRTUAL_MODS;
116 type                    return TYPE;
117 interpret               return INTERPRET;
118 action                  return ACTION_TOK;
119 key                     return KEY;
120 alias                   return ALIAS;
121 group                   return GROUP;
122 modmap                  return MODIFIER_MAP;
123 mod_map                 return MODIFIER_MAP;
124 modifier_map            return MODIFIER_MAP;
125 indicator               return INDICATOR;
126 shape                   return SHAPE;
127 row                     return ROW;
128 keys                    return KEYS;
129 section                 return SECTION;
130 overlay                 return OVERLAY;
131 text                    return TEXT;
132 outline                 return OUTLINE;
133 solid                   return SOLID;
134 logo                    return LOGO;
135 virtual                 return VIRTUAL;
136 alphanumeric_keys       return ALPHANUMERIC_KEYS;
137 modifier_keys           return MODIFIER_KEYS;
138 keypad_keys             return KEYPAD_KEYS;
139 function_keys           return FUNCTION_KEYS;
140 alternate_group         return ALTERNATE_GROUP;
141
142 [a-zA-Z_][a-zA-Z_0-9]*  memcpy(scanBuf, yytext, yyleng + 1); return IDENT;
143
144 0x[a-fA-F0-9]+          |
145 [0-9]+                  {
146                             char *end;
147                             scanInt = strtol(yytext, &end, 0);
148
149                             return INTEGER;
150                         }
151 [0-9]+\.[0-9]+ {
152                             char *end;
153                             scanInt = strtod(yytext, &end) * XkbGeomPtsPerMM;
154
155                             return FLOAT;
156                         }
157
158 "="                     return EQUALS;
159 "+"                     return PLUS;
160 "-"                     return MINUS;
161 "/"                     return DIVIDE;
162 "*"                     return TIMES;
163 "{"                     return OBRACE;
164 "}"                     return CBRACE;
165 "("                     return OPAREN;
166 ")"                     return CPAREN;
167 "["                     return OBRACKET;
168 "]"                     return CBRACKET;
169 "."                     return DOT;
170 ","                     return COMMA;
171 ";"                     return SEMI;
172 "!"                     return EXCLAM;
173 "~"                     return INVERT;
174
175 [ \t\r\n\v]+            
176
177 <<EOF>>                 return END_OF_FILE;
178
179 .                       return ERROR_TOK;
180
181 %%
182
183 void
184 yyerror(const char *s)
185 {
186     if (warningLevel>0) {
187         (void)fprintf(stderr,"%s: line %d of %s\n",s,yylineno,
188                                         (scanFile?scanFile:"(unknown)"));
189         if ((warningLevel>3))
190             (void)fprintf(stderr,"last scanned symbol is: %s\n",scanBuf);
191     }
192     return;
193 }
194
195 void setScanState(char *file, int lineno)
196 {
197   yylineno = 1;
198   if (scanFile)
199     free(scanFile);
200   scanFile = strdup(file);
201 }
202
203 int
204 XKBParseString(const char *string, XkbFile ** pRtrn)
205 {
206     YY_BUFFER_STATE state;
207
208     *pRtrn = NULL;
209     if (string == NULL)
210         return 1;
211
212     state = yy_scan_string(string);
213     rtrnValue = NULL;
214     if (yyparse() != 0)
215         return 0;
216
217     yy_delete_buffer(state);
218     yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
219     *pRtrn = rtrnValue;
220     CheckDefaultMap(rtrnValue);
221     rtrnValue = NULL;
222
223     return 1;
224 }
225
226 int
227 XKBParseFile(FILE * file, XkbFile ** pRtrn)
228 {
229     if (file)
230     {
231         yyin = file;
232         yystring = NULL;
233         rtrnValue = NULL;
234         if (yyparse() == 0)
235         {
236             *pRtrn = rtrnValue;
237             CheckDefaultMap(rtrnValue);
238             rtrnValue = NULL;
239             return 1;
240         }
241         *pRtrn = NULL;
242         return 0;
243     }
244     *pRtrn = NULL;
245     return 1;
246 }