[PATCH 40/57][Arm][OBJDUMP] Add support for MVE instructions: vdup, veor, vfma, vfms...
[binutils-gdb.git] / binutils / rclex.c
blob37d4309f48dd67ab55dce08271619ae61ad8cac0
1 /* rclex.c -- lexer for Windows rc files parser */
3 /* Copyright (C) 1997-2019 Free Software Foundation, Inc.
5 Written by Kai Tietz, Onevision.
7 This file is part of GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
25 /* This is a lexer used by the Windows rc file parser. It basically
26 just recognized a bunch of keywords. */
28 #include "sysdep.h"
29 #include "bfd.h"
30 #include "bucomm.h"
31 #include "libiberty.h"
32 #include "safe-ctype.h"
33 #include "windres.h"
34 #include "rcparse.h"
36 #include <assert.h>
38 /* Whether we are in rcdata mode, in which we returns the lengths of
39 strings. */
41 static int rcdata_mode;
43 /* Whether we are suppressing lines from cpp (including windows.h or
44 headers from your C sources may bring in externs and typedefs).
45 When active, we return IGNORED_TOKEN, which lets us ignore these
46 outside of resource constructs. Thus, it isn't required to protect
47 all the non-preprocessor lines in your header files with #ifdef
48 RC_INVOKED. It also means your RC file can't include other RC
49 files if they're named "*.h". Sorry. Name them *.rch or whatever. */
51 static int suppress_cpp_data;
53 #define IGNORE_CPP(x) (suppress_cpp_data ? IGNORED_TOKEN : (x))
55 /* The first filename we detect in the cpp output. We use this to
56 tell included files from the original file. */
58 static char *initial_fn;
60 /* List of allocated strings. */
62 struct alloc_string
64 struct alloc_string *next;
65 char *s;
68 static struct alloc_string *strings;
70 struct rclex_keywords
72 const char *name;
73 int tok;
76 #define K(KEY) { #KEY, KEY }
77 #define KRT(KEY) { #KEY, RT_##KEY }
79 static const struct rclex_keywords keywds[] =
81 K(ACCELERATORS), K(ALT), K(ANICURSOR), K(ANIICON), K(ASCII),
82 K(AUTO3STATE), K(AUTOCHECKBOX), K(AUTORADIOBUTTON),
83 K(BEDIT), { "BEGIN", BEG }, K(BITMAP), K(BLOCK), K(BUTTON),
84 K(CAPTION), K(CHARACTERISTICS), K(CHECKBOX), K(CHECKED),
85 K(CLASS), K(COMBOBOX), K(CONTROL), K(CTEXT), K(CURSOR),
86 K(DEFPUSHBUTTON), K(DIALOG), K(DIALOGEX), K(DISCARDABLE),
87 K(DLGINCLUDE), K(DLGINIT),
88 K(EDITTEXT), K(END), K(EXSTYLE),
89 K(FILEFLAGS), K(FILEFLAGSMASK), K(FILEOS), K(FILESUBTYPE),
90 K(FILETYPE), K(FILEVERSION), K(FIXED), K(FONT), K(FONTDIR),
91 K(GRAYED), KRT(GROUP_CURSOR), KRT(GROUP_ICON), K(GROUPBOX),
92 K(HEDIT), K(HELP), K(HTML),
93 K(ICON), K(IEDIT), K(IMPURE), K(INACTIVE),
94 K(LANGUAGE), K(LISTBOX), K(LOADONCALL), K(LTEXT),
95 K(MANIFEST), K(MENU), K(MENUBARBREAK), K(MENUBREAK),
96 K(MENUEX), K(MENUITEM), K(MESSAGETABLE), K(MOVEABLE),
97 K(NOINVERT), K(NOT),
98 K(PLUGPLAY), K(POPUP), K(PRELOAD), K(PRODUCTVERSION),
99 K(PURE), K(PUSHBOX), K(PUSHBUTTON),
100 K(RADIOBUTTON), K(RCDATA), K(RTEXT),
101 K(SCROLLBAR), K(SEPARATOR), K(SHIFT), K(STATE3),
102 K(STRINGTABLE), K(STYLE),
103 K(TOOLBAR),
104 K(USERBUTTON),
105 K(VALUE), { "VERSION", VERSIONK }, K(VERSIONINFO),
106 K(VIRTKEY), K(VXD),
107 { NULL, 0 },
110 /* External input stream from resrc */
111 extern FILE *cpp_pipe;
113 /* Lexical scanner helpers. */
114 static int rclex_lastch = -1;
115 static size_t rclex_tok_max = 0;
116 static size_t rclex_tok_pos = 0;
117 static char *rclex_tok = NULL;
119 static int
120 rclex_translatekeyword (const char *key)
122 if (key && ISUPPER (key[0]))
124 const struct rclex_keywords *kw = &keywds[0];
128 if (! strcmp (kw->name, key))
129 return kw->tok;
130 ++kw;
132 while (kw->name != NULL);
134 return STRING;
137 /* Handle a C preprocessor line. */
139 static void
140 cpp_line (void)
142 const char *s = rclex_tok;
143 int line;
144 char *send, *fn;
145 size_t len, mlen;
147 ++s;
148 while (ISSPACE (*s))
149 ++s;
151 /* Check for #pragma code_page ( DEFAULT | <nr>). */
152 len = strlen (s);
153 mlen = strlen ("pragma");
154 if (len > mlen && memcmp (s, "pragma", mlen) == 0 && ISSPACE (s[mlen]))
156 const char *end;
158 s += mlen + 1;
159 while (ISSPACE (*s))
160 ++s;
161 len = strlen (s);
162 mlen = strlen ("code_page");
163 if (len <= mlen || memcmp (s, "code_page", mlen) != 0)
164 /* FIXME: We ought to issue a warning message about an unrecognised pragma. */
165 return;
166 s += mlen;
167 while (ISSPACE (*s))
168 ++s;
169 if (*s != '(')
170 /* FIXME: We ought to issue an error message about a malformed pragma. */
171 return;
172 ++s;
173 while (ISSPACE (*s))
174 ++s;
175 if (*s == 0 || (end = strchr (s, ')')) == NULL)
176 /* FIXME: We ought to issue an error message about a malformed pragma. */
177 return;
178 len = (size_t) (end - s);
179 fn = xmalloc (len + 1);
180 if (len)
181 memcpy (fn, s, len);
182 fn[len] = 0;
183 while (len > 0 && (fn[len - 1] > 0 && fn[len - 1] <= 0x20))
184 fn[--len] = 0;
185 if (! len || (len == strlen ("DEFAULT") && strcasecmp (fn, "DEFAULT") == 0))
186 wind_current_codepage = wind_default_codepage;
187 else if (len > 0)
189 rc_uint_type ncp;
191 if (fn[0] == '0' && (fn[1] == 'x' || fn[1] == 'X'))
192 ncp = (rc_uint_type) strtol (fn + 2, NULL, 16);
193 else
194 ncp = (rc_uint_type) strtol (fn, NULL, 10);
195 if (ncp == CP_UTF16 || ! unicode_is_valid_codepage (ncp))
196 fatal (_("invalid value specified for pragma code_page.\n"));
197 wind_current_codepage = ncp;
199 free (fn);
200 return;
203 line = strtol (s, &send, 0);
204 if (*send != '\0' && ! ISSPACE (*send))
205 return;
207 /* Subtract 1 because we are about to count the newline. */
208 rc_lineno = line - 1;
210 s = send;
211 while (ISSPACE (*s))
212 ++s;
214 if (*s != '"')
215 return;
217 ++s;
218 send = strchr (s, '"');
219 if (send == NULL)
220 return;
222 fn = xmalloc (send - s + 1);
223 strncpy (fn, s, send - s);
224 fn[send - s] = '\0';
226 free (rc_filename);
227 rc_filename = fn;
229 if (! initial_fn)
231 initial_fn = xmalloc (strlen (fn) + 1);
232 strcpy (initial_fn, fn);
235 /* Allow the initial file, regardless of name. Suppress all other
236 files if they end in ".h" (this allows included "*.rc"). */
237 if (strcmp (initial_fn, fn) == 0
238 || strcmp (fn + strlen (fn) - 2, ".h") != 0)
239 suppress_cpp_data = 0;
240 else
241 suppress_cpp_data = 1;
244 /* Allocate a string of a given length. */
246 static char *
247 get_string (int len)
249 struct alloc_string *as;
251 as = xmalloc (sizeof *as);
252 as->s = xmalloc (len);
254 as->next = strings;
255 strings = as;
257 return as->s;
260 /* Handle a quoted string. The quotes are stripped. A pair of quotes
261 in a string are turned into a single quote. Adjacent strings are
262 merged separated by whitespace are merged, as in C. */
264 static char *
265 handle_quotes (rc_uint_type *len)
267 const char *input = rclex_tok;
268 char *ret, *s;
269 const char *t;
270 int ch;
271 int num_xdigits;
273 ret = get_string (strlen (input) + 1);
275 s = ret;
276 t = input;
277 if (*t == '"')
278 ++t;
279 while (*t != '\0')
281 if (*t == '\\')
283 ++t;
284 switch (*t)
286 case '\0':
287 rcparse_warning ("backslash at end of string");
288 break;
290 case '\"':
291 rcparse_warning ("use \"\" to put \" in a string");
292 *s++ = '"';
293 ++t;
294 break;
296 case 'a':
297 *s++ = ESCAPE_B; /* Strange, but true... */
298 ++t;
299 break;
301 case 'b':
302 *s++ = ESCAPE_B;
303 ++t;
304 break;
306 case 'f':
307 *s++ = ESCAPE_F;
308 ++t;
309 break;
311 case 'n':
312 *s++ = ESCAPE_N;
313 ++t;
314 break;
316 case 'r':
317 *s++ = ESCAPE_R;
318 ++t;
319 break;
321 case 't':
322 *s++ = ESCAPE_T;
323 ++t;
324 break;
326 case 'v':
327 *s++ = ESCAPE_V;
328 ++t;
329 break;
331 case '\\':
332 *s++ = *t++;
333 break;
335 case '0': case '1': case '2': case '3':
336 case '4': case '5': case '6': case '7':
337 ch = *t - '0';
338 ++t;
339 if (*t >= '0' && *t <= '7')
341 ch = (ch << 3) | (*t - '0');
342 ++t;
343 if (*t >= '0' && *t <= '7')
345 ch = (ch << 3) | (*t - '0');
346 ++t;
349 *s++ = ch;
350 break;
352 case 'x': case 'X':
353 ++t;
354 ch = 0;
355 /* We only handle single byte chars here. Make sure
356 we finish an escape sequence like "/xB0ABC" after
357 the first two digits. */
358 num_xdigits = 2;
359 while (num_xdigits--)
361 if (*t >= '0' && *t <= '9')
362 ch = (ch << 4) | (*t - '0');
363 else if (*t >= 'a' && *t <= 'f')
364 ch = (ch << 4) | (*t - 'a' + 10);
365 else if (*t >= 'A' && *t <= 'F')
366 ch = (ch << 4) | (*t - 'A' + 10);
367 else
368 break;
369 ++t;
371 *s++ = ch;
372 break;
374 default:
375 rcparse_warning ("unrecognized escape sequence");
376 *s++ = '\\';
377 *s++ = *t++;
378 break;
381 else if (*t != '"')
382 *s++ = *t++;
383 else if (t[1] == '\0')
384 break;
385 else if (t[1] == '"')
387 *s++ = '"';
388 t += 2;
390 else
392 ++t;
393 if (! ISSPACE (*t))
394 rcparse_warning ("unexpected character after '\"'");
395 while (ISSPACE (*t))
397 if ((*t) == '\n')
398 ++rc_lineno;
399 ++t;
401 if (*t == '\0')
402 break;
403 assert (*t == '"');
404 ++t;
408 *s = '\0';
410 *len = s - ret;
412 return ret;
415 /* Allocate a unicode string of a given length. */
417 static unichar *
418 get_unistring (int len)
420 return (unichar *) get_string (len * sizeof (unichar));
423 /* Handle a quoted unicode string. The quotes are stripped. A pair of quotes
424 in a string are turned into a single quote. Adjacent strings are
425 merged separated by whitespace are merged, as in C. */
427 static unichar *
428 handle_uniquotes (rc_uint_type *len)
430 const char *input = rclex_tok;
431 unichar *ret, *s;
432 const char *t;
433 int ch;
434 int num_xdigits;
436 ret = get_unistring (strlen (input) + 1);
438 s = ret;
439 t = input;
440 if ((*t == 'L' || *t == 'l') && t[1] == '"')
441 t += 2;
442 else if (*t == '"')
443 ++t;
444 while (*t != '\0')
446 if (*t == '\\')
448 ++t;
449 switch (*t)
451 case '\0':
452 rcparse_warning ("backslash at end of string");
453 break;
455 case '\"':
456 rcparse_warning ("use \"\" to put \" in a string");
457 break;
459 case 'a':
460 *s++ = ESCAPE_B; /* Strange, but true... */
461 ++t;
462 break;
464 case 'b':
465 *s++ = ESCAPE_B;
466 ++t;
467 break;
469 case 'f':
470 *s++ = ESCAPE_F;
471 ++t;
472 break;
474 case 'n':
475 *s++ = ESCAPE_N;
476 ++t;
477 break;
479 case 'r':
480 *s++ = ESCAPE_R;
481 ++t;
482 break;
484 case 't':
485 *s++ = ESCAPE_T;
486 ++t;
487 break;
489 case 'v':
490 *s++ = ESCAPE_V;
491 ++t;
492 break;
494 case '\\':
495 *s++ = (unichar) *t++;
496 break;
498 case '0': case '1': case '2': case '3':
499 case '4': case '5': case '6': case '7':
500 ch = *t - '0';
501 ++t;
502 if (*t >= '0' && *t <= '7')
504 ch = (ch << 3) | (*t - '0');
505 ++t;
506 if (*t >= '0' && *t <= '7')
508 ch = (ch << 3) | (*t - '0');
509 ++t;
512 *s++ = (unichar) ch;
513 break;
515 case 'x': case 'X':
516 ++t;
517 ch = 0;
518 /* We only handle two byte chars here. Make sure
519 we finish an escape sequence like "/xB0ABC" after
520 the first two digits. */
521 num_xdigits = 4;
522 while (num_xdigits--)
524 if (*t >= '0' && *t <= '9')
525 ch = (ch << 4) | (*t - '0');
526 else if (*t >= 'a' && *t <= 'f')
527 ch = (ch << 4) | (*t - 'a' + 10);
528 else if (*t >= 'A' && *t <= 'F')
529 ch = (ch << 4) | (*t - 'A' + 10);
530 else
531 break;
532 ++t;
534 *s++ = (unichar) ch;
535 break;
537 default:
538 rcparse_warning ("unrecognized escape sequence");
539 *s++ = '\\';
540 *s++ = (unichar) *t++;
541 break;
544 else if (*t != '"')
545 *s++ = (unichar) *t++;
546 else if (t[1] == '\0')
547 break;
548 else if (t[1] == '"')
550 *s++ = '"';
551 t += 2;
553 else
555 ++t;
556 assert (ISSPACE (*t));
557 while (ISSPACE (*t))
559 if ((*t) == '\n')
560 ++rc_lineno;
561 ++t;
563 if (*t == '\0')
564 break;
565 assert (*t == '"');
566 ++t;
570 *s = '\0';
572 *len = s - ret;
574 return ret;
577 /* Discard all the strings we have allocated. The parser calls this
578 when it no longer needs them. */
580 void
581 rcparse_discard_strings (void)
583 struct alloc_string *as;
585 as = strings;
586 while (as != NULL)
588 struct alloc_string *n;
590 free (as->s);
591 n = as->next;
592 free (as);
593 as = n;
596 strings = NULL;
599 /* Enter rcdata mode. */
600 void
601 rcparse_rcdata (void)
603 rcdata_mode = 1;
606 /* Go back to normal mode from rcdata mode. */
607 void
608 rcparse_normal (void)
610 rcdata_mode = 0;
613 static void
614 rclex_tok_add_char (int ch)
616 if (! rclex_tok || rclex_tok_max <= rclex_tok_pos)
618 char *h = xmalloc (rclex_tok_max + 9);
620 if (! h)
621 abort ();
622 if (rclex_tok)
624 memcpy (h, rclex_tok, rclex_tok_pos + 1);
625 free (rclex_tok);
627 else
628 rclex_tok_pos = 0;
629 rclex_tok_max += 8;
630 rclex_tok = h;
632 if (ch != -1)
633 rclex_tok[rclex_tok_pos++] = (char) ch;
634 rclex_tok[rclex_tok_pos] = 0;
637 static int
638 rclex_readch (void)
640 int r = -1;
642 if ((r = rclex_lastch) != -1)
643 rclex_lastch = -1;
644 else
646 char ch;
649 if (! cpp_pipe || feof (cpp_pipe)
650 || fread (&ch, 1, 1,cpp_pipe) != 1)
651 break;
652 r = ((int) ch) & 0xff;
654 while (r == 0 || r == '\r');
656 rclex_tok_add_char (r);
657 return r;
660 static int
661 rclex_peekch (void)
663 int r;
665 if ((r = rclex_lastch) == -1)
667 if ((r = rclex_readch ()) != -1)
669 rclex_lastch = r;
670 if (rclex_tok_pos > 0)
671 rclex_tok[--rclex_tok_pos] = 0;
674 return r;
677 static void
678 rclex_string (void)
680 int c;
682 while ((c = rclex_peekch ()) != -1)
684 if (c == '\n')
685 break;
686 if (c == '\\')
688 rclex_readch ();
689 if ((c = rclex_peekch ()) == -1 || c == '\n')
690 break;
691 rclex_readch ();
693 else if (rclex_readch () == '"')
695 /* PR 6714
696 Skip any whitespace after the end of the double quotes. */
699 c = rclex_peekch ();
700 if (ISSPACE (c))
701 rclex_readch ();
702 else
703 c = -1;
705 while (c != -1);
707 if (rclex_peekch () == '"')
708 rclex_readch ();
709 else
710 break;
715 static rc_uint_type
716 read_digit (int ch)
718 rc_uint_type base = 10;
719 rc_uint_type ret, val;
720 int warned = 0;
722 ret = 0;
723 if (ch == '0')
725 base = 8;
726 switch (rclex_peekch ())
728 case 'o': case 'O':
729 rclex_readch ();
730 base = 8;
731 break;
733 case 'x': case 'X':
734 rclex_readch ();
735 base = 16;
736 break;
739 else
740 ret = (rc_uint_type) (ch - '0');
741 while ((ch = rclex_peekch ()) != -1)
743 if (ISDIGIT (ch))
744 val = (rc_uint_type) (ch - '0');
745 else if (ch >= 'a' && ch <= 'f')
746 val = (rc_uint_type) ((ch - 'a') + 10);
747 else if (ch >= 'A' && ch <= 'F')
748 val = (rc_uint_type) ((ch - 'A') + 10);
749 else
750 break;
751 rclex_readch ();
752 if (! warned && val >= base)
754 warned = 1;
755 rcparse_warning ("digit exceeds base");
757 ret *= base;
758 ret += val;
760 return ret;
763 /* yyparser entry method. */
766 yylex (void)
768 char *s;
769 unichar *us;
770 rc_uint_type length;
771 int ch;
773 /* Make sure that rclex_tok is initialized. */
774 if (! rclex_tok)
775 rclex_tok_add_char (-1);
781 /* Clear token. */
782 rclex_tok_pos = 0;
783 rclex_tok[0] = 0;
785 if ((ch = rclex_readch ()) == -1)
786 return -1;
787 if (ch == '\n')
788 ++rc_lineno;
790 while (ch <= 0x20);
792 switch (ch)
794 case '#':
795 while ((ch = rclex_peekch ()) != -1 && ch != '\n')
796 rclex_readch ();
797 cpp_line ();
798 ch = IGNORED_TOKEN;
799 break;
801 case '{':
802 ch = IGNORE_CPP (BEG);
803 break;
805 case '}':
806 ch = IGNORE_CPP (END);
807 break;
809 case '0': case '1': case '2': case '3': case '4':
810 case '5': case '6': case '7': case '8': case '9':
811 yylval.i.val = read_digit (ch);
812 yylval.i.dword = 0;
813 switch (rclex_peekch ())
815 case 'l': case 'L':
816 rclex_readch ();
817 yylval.i.dword = 1;
818 break;
820 ch = IGNORE_CPP (NUMBER);
821 break;
822 case '"':
823 rclex_string ();
824 ch = IGNORE_CPP ((! rcdata_mode ? QUOTEDSTRING : SIZEDSTRING));
825 if (ch == IGNORED_TOKEN)
826 break;
827 s = handle_quotes (&length);
828 if (! rcdata_mode)
829 yylval.s = s;
830 else
832 yylval.ss.length = length;
833 yylval.ss.s = s;
835 break;
836 case 'L': case 'l':
837 if (rclex_peekch () == '"')
839 rclex_readch ();
840 rclex_string ();
841 ch = IGNORE_CPP ((! rcdata_mode ? QUOTEDUNISTRING : SIZEDUNISTRING));
842 if (ch == IGNORED_TOKEN)
843 break;
844 us = handle_uniquotes (&length);
845 if (! rcdata_mode)
846 yylval.uni = us;
847 else
849 yylval.suni.length = length;
850 yylval.suni.s = us;
852 break;
854 /* Fall through. */
855 default:
856 if (ISIDST (ch) || ch=='$')
858 while ((ch = rclex_peekch ()) != -1
859 && (ISIDNUM (ch) || ch == '$' || ch == '.'
860 || ch == ':' || ch == '\\' || ch == '/'
861 || ch == '_' || ch == '-')
863 rclex_readch ();
864 ch = IGNORE_CPP (rclex_translatekeyword (rclex_tok));
865 if (ch == STRING)
867 s = get_string (strlen (rclex_tok) + 1);
868 strcpy (s, rclex_tok);
869 yylval.s = s;
871 else if (ch == BLOCK)
873 const char *hs = NULL;
875 switch (yylex ())
877 case STRING:
878 case QUOTEDSTRING:
879 hs = yylval.s;
880 break;
881 case SIZEDSTRING:
882 hs = yylval.s = yylval.ss.s;
883 break;
885 if (! hs)
887 rcparse_warning ("BLOCK expects a string as argument.");
888 ch = IGNORED_TOKEN;
890 else if (! strcmp (hs, "StringFileInfo"))
891 ch = BLOCKSTRINGFILEINFO;
892 else if (! strcmp (hs, "VarFileInfo"))
893 ch = BLOCKVARFILEINFO;
895 break;
897 ch = IGNORE_CPP (ch);
898 break;
901 while (ch == IGNORED_TOKEN);
903 return ch;