2007-05-27 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / binutils / rclex.c
blobedcf2cece6b30cbdca2348accc23293103b22fe5
1 /* rclex.c -- lexer for Windows rc files parser */
3 /* Copyright 1997, 1998, 1999, 2001, 2002, 2003, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 Written by Kai Tietz, Onevision.
8 This file is part of GNU Binutils.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 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 supressing 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;
146 ++s;
147 while (ISSPACE (*s))
148 ++s;
150 line = strtol (s, &send, 0);
151 if (*send != '\0' && ! ISSPACE (*send))
152 return;
154 /* Subtract 1 because we are about to count the newline. */
155 rc_lineno = line - 1;
157 s = send;
158 while (ISSPACE (*s))
159 ++s;
161 if (*s != '"')
162 return;
164 ++s;
165 send = strchr (s, '"');
166 if (send == NULL)
167 return;
169 fn = xmalloc (send - s + 1);
170 strncpy (fn, s, send - s);
171 fn[send - s] = '\0';
173 free (rc_filename);
174 rc_filename = fn;
176 if (! initial_fn)
178 initial_fn = xmalloc (strlen (fn) + 1);
179 strcpy (initial_fn, fn);
182 /* Allow the initial file, regardless of name. Suppress all other
183 files if they end in ".h" (this allows included "*.rc"). */
184 if (strcmp (initial_fn, fn) == 0
185 || strcmp (fn + strlen (fn) - 2, ".h") != 0)
186 suppress_cpp_data = 0;
187 else
188 suppress_cpp_data = 1;
191 /* Allocate a string of a given length. */
193 static char *
194 get_string (int len)
196 struct alloc_string *as;
198 as = xmalloc (sizeof *as);
199 as->s = xmalloc (len);
201 as->next = strings;
202 strings = as;
204 return as->s;
207 /* Handle a quoted string. The quotes are stripped. A pair of quotes
208 in a string are turned into a single quote. Adjacent strings are
209 merged separated by whitespace are merged, as in C. */
211 static char *
212 handle_quotes (rc_uint_type *len)
214 const char *input = rclex_tok;
215 char *ret, *s;
216 const char *t;
217 int ch;
218 int num_xdigits;
220 ret = get_string (strlen (input) + 1);
222 s = ret;
223 t = input;
224 if (*t == '"')
225 ++t;
226 while (*t != '\0')
228 if (*t == '\\')
230 ++t;
231 switch (*t)
233 case '\0':
234 rcparse_warning ("backslash at end of string");
235 break;
237 case '\"':
238 rcparse_warning ("use \"\" to put \" in a string");
239 *s++ = '"';
240 ++t;
241 break;
243 case 'a':
244 *s++ = ESCAPE_B; /* Strange, but true... */
245 ++t;
246 break;
248 case 'b':
249 *s++ = ESCAPE_B;
250 ++t;
251 break;
253 case 'f':
254 *s++ = ESCAPE_F;
255 ++t;
256 break;
258 case 'n':
259 *s++ = ESCAPE_N;
260 ++t;
261 break;
263 case 'r':
264 *s++ = ESCAPE_R;
265 ++t;
266 break;
268 case 't':
269 *s++ = ESCAPE_T;
270 ++t;
271 break;
273 case 'v':
274 *s++ = ESCAPE_V;
275 ++t;
276 break;
278 case '\\':
279 *s++ = *t++;
280 break;
282 case '0': case '1': case '2': case '3':
283 case '4': case '5': case '6': case '7':
284 ch = *t - '0';
285 ++t;
286 if (*t >= '0' && *t <= '7')
288 ch = (ch << 3) | (*t - '0');
289 ++t;
290 if (*t >= '0' && *t <= '7')
292 ch = (ch << 3) | (*t - '0');
293 ++t;
296 *s++ = ch;
297 break;
299 case 'x': case 'X':
300 ++t;
301 ch = 0;
302 /* We only handle single byte chars here. Make sure
303 we finish an escape sequence like "/xB0ABC" after
304 the first two digits. */
305 num_xdigits = 2;
306 while (num_xdigits--)
308 if (*t >= '0' && *t <= '9')
309 ch = (ch << 4) | (*t - '0');
310 else if (*t >= 'a' && *t <= 'f')
311 ch = (ch << 4) | (*t - 'a' + 10);
312 else if (*t >= 'A' && *t <= 'F')
313 ch = (ch << 4) | (*t - 'A' + 10);
314 else
315 break;
316 ++t;
318 *s++ = ch;
319 break;
321 default:
322 rcparse_warning ("unrecognized escape sequence");
323 *s++ = '\\';
324 *s++ = *t++;
325 break;
328 else if (*t != '"')
329 *s++ = *t++;
330 else if (t[1] == '\0')
331 break;
332 else if (t[1] == '"')
334 *s++ = '"';
335 t += 2;
337 else
339 rcparse_warning ("unexpected character after '\"'");
340 ++t;
341 assert (ISSPACE (*t));
342 while (ISSPACE (*t))
344 if ((*t) == '\n')
345 ++rc_lineno;
346 ++t;
348 if (*t == '\0')
349 break;
350 assert (*t == '"');
351 ++t;
355 *s = '\0';
357 *len = s - ret;
359 return ret;
362 /* Allocate a unicode string of a given length. */
364 static unichar *
365 get_unistring (int len)
367 return (unichar *) get_string (len * sizeof (unichar));
370 /* Handle a quoted unicode string. The quotes are stripped. A pair of quotes
371 in a string are turned into a single quote. Adjacent strings are
372 merged separated by whitespace are merged, as in C. */
374 static unichar *
375 handle_uniquotes (rc_uint_type *len)
377 const char *input = rclex_tok;
378 unichar *ret, *s;
379 const char *t;
380 int ch;
381 int num_xdigits;
383 ret = get_unistring (strlen (input) + 1);
385 s = ret;
386 t = input;
387 if ((*t == 'L' || *t == 'l') && t[1] == '"')
388 t += 2;
389 else if (*t == '"')
390 ++t;
391 while (*t != '\0')
393 if (*t == '\\')
395 ++t;
396 switch (*t)
398 case '\0':
399 rcparse_warning ("backslash at end of string");
400 break;
402 case '\"':
403 rcparse_warning ("use \"\" to put \" in a string");
404 break;
406 case 'a':
407 *s++ = ESCAPE_B; /* Strange, but true... */
408 ++t;
409 break;
411 case 'b':
412 *s++ = ESCAPE_B;
413 ++t;
414 break;
416 case 'f':
417 *s++ = ESCAPE_F;
418 ++t;
419 break;
421 case 'n':
422 *s++ = ESCAPE_N;
423 ++t;
424 break;
426 case 'r':
427 *s++ = ESCAPE_R;
428 ++t;
429 break;
431 case 't':
432 *s++ = ESCAPE_T;
433 ++t;
434 break;
436 case 'v':
437 *s++ = ESCAPE_V;
438 ++t;
439 break;
441 case '\\':
442 *s++ = (unichar) *t++;
443 break;
445 case '0': case '1': case '2': case '3':
446 case '4': case '5': case '6': case '7':
447 ch = *t - '0';
448 ++t;
449 if (*t >= '0' && *t <= '7')
451 ch = (ch << 3) | (*t - '0');
452 ++t;
453 if (*t >= '0' && *t <= '7')
455 ch = (ch << 3) | (*t - '0');
456 ++t;
459 *s++ = (unichar) ch;
460 break;
462 case 'x': case 'X':
463 ++t;
464 ch = 0;
465 /* We only handle two byte chars here. Make sure
466 we finish an escape sequence like "/xB0ABC" after
467 the first two digits. */
468 num_xdigits = 4;
469 while (num_xdigits--)
471 if (*t >= '0' && *t <= '9')
472 ch = (ch << 4) | (*t - '0');
473 else if (*t >= 'a' && *t <= 'f')
474 ch = (ch << 4) | (*t - 'a' + 10);
475 else if (*t >= 'A' && *t <= 'F')
476 ch = (ch << 4) | (*t - 'A' + 10);
477 else
478 break;
479 ++t;
481 *s++ = (unichar) ch;
482 break;
484 default:
485 rcparse_warning ("unrecognized escape sequence");
486 *s++ = '\\';
487 *s++ = (unichar) *t++;
488 break;
491 else if (*t != '"')
492 *s++ = (unichar) *t++;
493 else if (t[1] == '\0')
494 break;
495 else if (t[1] == '"')
497 *s++ = '"';
498 t += 2;
500 else
502 ++t;
503 assert (ISSPACE (*t));
504 while (ISSPACE (*t))
506 if ((*t) == '\n')
507 ++rc_lineno;
508 ++t;
510 if (*t == '\0')
511 break;
512 assert (*t == '"');
513 ++t;
517 *s = '\0';
519 *len = s - ret;
521 return ret;
524 /* Discard all the strings we have allocated. The parser calls this
525 when it no longer needs them. */
527 void
528 rcparse_discard_strings (void)
530 struct alloc_string *as;
532 as = strings;
533 while (as != NULL)
535 struct alloc_string *n;
537 free (as->s);
538 n = as->next;
539 free (as);
540 as = n;
543 strings = NULL;
546 /* Enter rcdata mode. */
547 void
548 rcparse_rcdata (void)
550 rcdata_mode = 1;
553 /* Go back to normal mode from rcdata mode. */
554 void
555 rcparse_normal (void)
557 rcdata_mode = 0;
560 static void
561 rclex_tok_add_char (int ch)
563 if (! rclex_tok || rclex_tok_max <= rclex_tok_pos)
565 char *h = xmalloc (rclex_tok_max + 9);
567 if (! h)
568 abort ();
569 if (rclex_tok)
571 memcpy (h, rclex_tok, rclex_tok_pos + 1);
572 free (rclex_tok);
574 else
575 rclex_tok_pos = 0;
576 rclex_tok_max += 8;
577 rclex_tok = h;
579 if (ch != -1)
580 rclex_tok[rclex_tok_pos++] = (char) ch;
581 rclex_tok[rclex_tok_pos] = 0;
584 static int
585 rclex_readch (void)
587 int r = -1;
589 if ((r = rclex_lastch) != -1)
590 rclex_lastch = -1;
591 else
593 char ch;
596 if (! cpp_pipe || feof (cpp_pipe)
597 || fread (&ch, 1, 1,cpp_pipe) != 1)
598 break;
599 r = ((int) ch) & 0xff;
601 while (r == 0 || r == '\r');
603 rclex_tok_add_char (r);
604 return r;
607 static int
608 rclex_peekch (void)
610 int r;
612 if ((r = rclex_lastch) == -1)
614 if ((r = rclex_readch ()) != -1)
616 rclex_lastch = r;
617 if (rclex_tok_pos > 0)
618 rclex_tok[--rclex_tok_pos] = 0;
621 return r;
624 static void
625 rclex_string (void)
627 int c;
629 while ((c = rclex_peekch ()) != -1)
631 if (c == '\n')
632 break;
633 if (c == '\\')
635 rclex_readch ();
636 if ((c = rclex_peekch ()) == -1 || c == '\n')
637 break;
638 rclex_readch ();
640 else if (rclex_readch () == '"')
642 if (rclex_peekch () == '"')
643 rclex_readch ();
644 else
645 break;
650 static rc_uint_type
651 read_digit (int ch)
653 rc_uint_type base = 10;
654 rc_uint_type ret, val;
655 int warned = 0;
657 ret = 0;
658 if (ch == '0')
660 base = 8;
661 switch (rclex_peekch ())
663 case 'o': case 'O':
664 rclex_readch ();
665 base = 8;
666 break;
668 case 'x': case 'X':
669 rclex_readch ();
670 base = 16;
671 break;
674 else
675 ret = (rc_uint_type) (ch - '0');
676 while ((ch = rclex_peekch ()) != -1)
678 if (ISDIGIT (ch))
679 val = (rc_uint_type) (ch - '0');
680 else if (ch >= 'a' && ch <= 'f')
681 val = (rc_uint_type) ((ch - 'a') + 10);
682 else if (ch >= 'A' && ch <= 'F')
683 val = (rc_uint_type) ((ch - 'A') + 10);
684 else
685 break;
686 rclex_readch ();
687 if (! warned && val >= base)
689 warned = 1;
690 rcparse_warning ("digit exceeds base");
692 ret *= base;
693 ret += val;
695 return ret;
698 /* yyparser entry method. */
701 yylex (void)
703 char *s;
704 unichar *us;
705 rc_uint_type length;
706 int ch;
708 /* Make sure that rclex_tok is initialized. */
709 if (! rclex_tok)
710 rclex_tok_add_char (-1);
716 /* Clear token. */
717 rclex_tok_pos = 0;
718 rclex_tok[0] = 0;
720 if ((ch = rclex_readch ()) == -1)
721 return -1;
722 if (ch == '\n')
723 ++rc_lineno;
725 while (ch <= 0x20);
727 switch (ch)
729 case '#':
730 while ((ch = rclex_peekch ()) != -1 && ch != '\n')
731 rclex_readch ();
732 cpp_line ();
733 ch = IGNORED_TOKEN;
734 break;
736 case '{':
737 ch = IGNORE_CPP (BEG);
738 break;
740 case '}':
741 ch = IGNORE_CPP (END);
742 break;
744 case '0': case '1': case '2': case '3': case '4':
745 case '5': case '6': case '7': case '8': case '9':
746 yylval.i.val = read_digit (ch);
747 yylval.i.dword = 0;
748 switch (rclex_peekch ())
750 case 'l': case 'L':
751 rclex_readch ();
752 yylval.i.dword = 1;
753 break;
755 ch = IGNORE_CPP (NUMBER);
756 break;
757 case '"':
758 rclex_string ();
759 ch = IGNORE_CPP ((! rcdata_mode ? QUOTEDSTRING : SIZEDSTRING));
760 if (ch == IGNORED_TOKEN)
761 break;
762 s = handle_quotes (&length);
763 if (! rcdata_mode)
764 yylval.s = s;
765 else
767 yylval.ss.length = length;
768 yylval.ss.s = s;
770 break;
771 case 'L': case 'l':
772 if (rclex_peekch () == '"')
774 rclex_readch ();
775 rclex_string ();
776 ch = IGNORE_CPP ((! rcdata_mode ? QUOTEDUNISTRING : SIZEDUNISTRING));
777 if (ch == IGNORED_TOKEN)
778 break;
779 us = handle_uniquotes (&length);
780 if (! rcdata_mode)
781 yylval.uni = us;
782 else
784 yylval.suni.length = length;
785 yylval.suni.s = us;
787 break;
789 /* Fall through. */
790 default:
791 if (ISIDST (ch) || ch=='$')
793 while ((ch = rclex_peekch ()) != -1 && (ISIDNUM (ch) || ch == '$' || ch == '.'))
794 rclex_readch ();
795 ch = IGNORE_CPP (rclex_translatekeyword (rclex_tok));
796 if (ch == STRING)
798 s = get_string (strlen (rclex_tok) + 1);
799 strcpy (s, rclex_tok);
800 yylval.s = s;
802 else if (ch == BLOCK)
804 const char *hs = NULL;
806 switch (yylex ())
808 case STRING:
809 case QUOTEDSTRING:
810 hs = yylval.s;
811 break;
812 case SIZEDSTRING:
813 hs = yylval.s = yylval.ss.s;
814 break;
816 if (! hs)
818 rcparse_warning ("BLOCK expects a string as argument.");
819 ch = IGNORED_TOKEN;
821 else if (! strcmp (hs, "StringFileInfo"))
822 ch = BLOCKSTRINGFILEINFO;
823 else if (! strcmp (hs, "VarFileInfo"))
824 ch = BLOCKVARFILEINFO;
826 break;
828 ch = IGNORE_CPP (ch);
829 break;
832 while (ch == IGNORED_TOKEN);
834 return ch;