user32/tests: Fix some window test failures on various Windows platforms.
[wine] / tools / wrc / parser.y
1 %{
2 /*
3  * Copyright 1994       Martin von Loewis
4  * Copyright 1998-2000  Bertho A. Stultiens (BS)
5  *           1999       Juergen Schmied (JS)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * History:
22  * 24-Jul-2000 BS       - Made a fix for broken Berkeley yacc on
23  *                        non-terminals (see cjunk rule).
24  * 21-May-2000 BS       - Partial implementation of font resources.
25  *                      - Corrected language propagation for binary
26  *                        resources such as bitmaps, icons, cursors,
27  *                        userres and rcdata. The language is now
28  *                        correct in .res files.
29  *                      - Fixed reading the resource name as ident,
30  *                        so that it may overlap keywords.
31  * 20-May-2000 BS       - Implemented animated cursors and icons
32  *                        resource types.
33  * 30-Apr-2000 BS       - Reintegration into the wine-tree
34  * 14-Jan-2000 BS       - Redid the usertype resources so that they
35  *                        are compatible.
36  * 02-Jan-2000 BS       - Removed the preprocessor from the grammar
37  *                        except for the # command (line numbers).
38  *
39  * 06-Nov-1999 JS       - see CHANGES
40  *
41  * 29-Dec-1998 AdH      - Grammar and function extensions.
42  *                           grammar: TOOLBAR resources, Named ICONs in
43  *                              DIALOGS
44  *                           functions: semantic actions for the grammar
45  *                              changes, resource files can now be anywhere
46  *                              on the include path instead of just in the
47  *                              current directory
48  *
49  * 20-Jun-1998 BS       - Fixed a bug in load_file() where the name was not
50  *                        printed out correctly.
51  *
52  * 17-Jun-1998 BS       - Fixed a bug in CLASS statement parsing which should
53  *                        also accept a tSTRING as argument.
54  *
55  * 25-May-1998 BS       - Found out that I need to support language, version
56  *                        and characteristics in inline resources (bitmap,
57  *                        cursor, etc) but they can also be specified with
58  *                        a filename. This renders my filename-scanning scheme
59  *                        worthless. Need to build newline parsing to solve
60  *                        this one.
61  *                        It will come with version 1.1.0 (sigh).
62  *
63  * 19-May-1998 BS       - Started to build a builtin preprocessor
64  *
65  * 30-Apr-1998 BS       - Redid the stringtable parsing/handling. My previous
66  *                        ideas had some serious flaws.
67  *
68  * 27-Apr-1998 BS       - Removed a lot of dead comments and put it in a doc
69  *                        file.
70  *
71  * 21-Apr-1998 BS       - Added correct behavior for cursors and icons.
72  *                      - This file is growing too big. It is time to strip
73  *                        things and put it in a support file.
74  *
75  * 19-Apr-1998 BS       - Tagged the stringtable resource so that only one
76  *                        resource will be created. This because the table
77  *                        has a different layout than other resources. The
78  *                        table has to be sorted, and divided into smaller
79  *                        resource entries (see comment in source).
80  *
81  * 17-Apr-1998 BS       - Almost all strings, including identifiers, are parsed
82  *                        as string_t which include unicode strings upon
83  *                        input.
84  *                      - Parser now emits a warning when compiling win32
85  *                        extensions in win16 mode.
86  *
87  * 16-Apr-1998 BS       - Raw data elements are now *optionally* separated
88  *                        by commas. Read the comments in file sq2dq.l.
89  *                      - FIXME: there are instances in the source that rely
90  *                        on the fact that int==32bit and pointers are int size.
91  *                      - Fixed the conflict in menuex by changing a rule
92  *                        back into right recursion. See note in source.
93  *                      - UserType resources cannot have an expression as its
94  *                        typeclass. See note in source.
95  *
96  * 15-Apr-1998 BS       - Changed all right recursion into left recursion to
97  *                        get reduction of the parsestack.
98  *                        This also helps communication between bison and flex.
99  *                        Main advantage is that the Empty rule gets reduced
100  *                        first, which is used to allocate/link things.
101  *                        It also added a shift/reduce conflict in the menuex
102  *                        handling, due to expression/option possibility,
103  *                        although not serious.
104  *
105  * 14-Apr-1998 BS       - Redone almost the entire parser. We're not talking
106  *                        about making it more efficient, but readable (for me)
107  *                        and slightly easier to expand/change.
108  *                        This is done primarily by using more reduce states
109  *                        with many (intuitive) types for the various resource
110  *                        statements.
111  *                      - Added expression handling for all resources where a
112  *                        number is accepted (not only for win32). Also added
113  *                        multiply and division (not MS compatible, but handy).
114  *                        Unary minus introduced a shift/reduce conflict, but
115  *                        it is not serious.
116  *
117  * 13-Apr-1998 BS       - Reordered a lot of things
118  *                      - Made the source more readable
119  *                      - Added Win32 resource definitions
120  *                      - Corrected syntax problems with an old yacc (;)
121  *                      - Added extra comment about grammar
122  */
123 #include "config.h"
124 #include "wine/port.h"
125
126 #include <stdio.h>
127 #include <stdlib.h>
128 #include <stdarg.h>
129 #include <assert.h>
130 #include <ctype.h>
131 #include <string.h>
132 #ifdef HAVE_ALLOCA_H
133 #include <alloca.h>
134 #endif
135
136 #include "wrc.h"
137 #include "utils.h"
138 #include "newstruc.h"
139 #include "dumpres.h"
140 #include "wine/wpp.h"
141 #include "wine/unicode.h"
142 #include "parser.h"
143 #include "windef.h"
144 #include "winbase.h"
145 #include "wingdi.h"
146 #include "winuser.h"
147
148 #if defined(YYBYACC)
149         /* Berkeley yacc (byacc) doesn't seem to know about these */
150         /* Some *BSD supplied versions do define these though */
151 # ifndef YYEMPTY
152 #  define YYEMPTY       (-1)    /* Empty lookahead value of yychar */
153 # endif
154 # ifndef YYLEX
155 #  define YYLEX         yylex()
156 # endif
157
158 #elif defined(YYBISON)
159         /* Bison was used for original development */
160         /* #define YYEMPTY -2 */
161         /* #define YYLEX   yylex() */
162
163 #else
164         /* No yacc we know yet */
165 # if !defined(YYEMPTY) || !defined(YYLEX)
166 #  error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
167 # elif defined(__GNUC__)        /* gcc defines the #warning directive */
168 #  warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
169   /* #else we just take a chance that it works... */
170 # endif
171 #endif
172
173 int want_nl = 0;        /* Signal flex that we need the next newline */
174 int want_id = 0;        /* Signal flex that we need the next identifier */
175 stringtable_t *tagstt;  /* Stringtable tag.
176                          * It is set while parsing a stringtable to one of
177                          * the stringtables in the sttres list or a new one
178                          * if the language was not parsed before.
179                          */
180 stringtable_t *sttres;  /* Stringtable resources. This holds the list of
181                          * stringtables with different lanuages
182                          */
183 static int dont_want_id = 0;    /* See language parsing for details */
184
185 /* Set to the current options of the currently scanning stringtable */
186 static int *tagstt_memopt;
187 static characts_t *tagstt_characts;
188 static version_t *tagstt_version;
189
190 static const char riff[4] = "RIFF";     /* RIFF file magic for animated cursor/icon */
191
192 /* Prototypes of here defined functions */
193 static event_t *get_event_head(event_t *p);
194 static control_t *get_control_head(control_t *p);
195 static ver_value_t *get_ver_value_head(ver_value_t *p);
196 static ver_block_t *get_ver_block_head(ver_block_t *p);
197 static resource_t *get_resource_head(resource_t *p);
198 static menuex_item_t *get_itemex_head(menuex_item_t *p);
199 static menu_item_t *get_item_head(menu_item_t *p);
200 static raw_data_t *merge_raw_data_str(raw_data_t *r1, string_t *str);
201 static raw_data_t *merge_raw_data_int(raw_data_t *r1, int i);
202 static raw_data_t *merge_raw_data_long(raw_data_t *r1, int i);
203 static raw_data_t *merge_raw_data(raw_data_t *r1, raw_data_t *r2);
204 static raw_data_t *str2raw_data(string_t *str);
205 static raw_data_t *int2raw_data(int i);
206 static raw_data_t *long2raw_data(int i);
207 static raw_data_t *load_file(string_t *name, language_t *lang);
208 static itemex_opt_t *new_itemex_opt(int id, int type, int state, int helpid);
209 static event_t *add_string_event(string_t *key, int id, int flags, event_t *prev);
210 static event_t *add_event(int key, int id, int flags, event_t *prev);
211 static dialogex_t *dialogex_version(version_t *v, dialogex_t *dlg);
212 static dialogex_t *dialogex_characteristics(characts_t *c, dialogex_t *dlg);
213 static dialogex_t *dialogex_language(language_t *l, dialogex_t *dlg);
214 static dialogex_t *dialogex_menu(name_id_t *m, dialogex_t *dlg);
215 static dialogex_t *dialogex_class(name_id_t *n, dialogex_t *dlg);
216 static dialogex_t *dialogex_font(font_id_t *f, dialogex_t *dlg);
217 static dialogex_t *dialogex_caption(string_t *s, dialogex_t *dlg);
218 static dialogex_t *dialogex_exstyle(style_t *st, dialogex_t *dlg);
219 static dialogex_t *dialogex_style(style_t *st, dialogex_t *dlg);
220 static name_id_t *convert_ctlclass(name_id_t *cls);
221 static control_t *ins_ctrl(int type, int special_style, control_t *ctrl, control_t *prev);
222 static dialog_t *dialog_version(version_t *v, dialog_t *dlg);
223 static dialog_t *dialog_characteristics(characts_t *c, dialog_t *dlg);
224 static dialog_t *dialog_language(language_t *l, dialog_t *dlg);
225 static dialog_t *dialog_menu(name_id_t *m, dialog_t *dlg);
226 static dialog_t *dialog_class(name_id_t *n, dialog_t *dlg);
227 static dialog_t *dialog_font(font_id_t *f, dialog_t *dlg);
228 static dialog_t *dialog_caption(string_t *s, dialog_t *dlg);
229 static dialog_t *dialog_exstyle(style_t * st, dialog_t *dlg);
230 static dialog_t *dialog_style(style_t * st, dialog_t *dlg);
231 static resource_t *build_stt_resources(stringtable_t *stthead);
232 static stringtable_t *find_stringtable(lvc_t *lvc);
233 static toolbar_item_t *ins_tlbr_button(toolbar_item_t *prev, toolbar_item_t *idrec);
234 static toolbar_item_t *get_tlbr_buttons_head(toolbar_item_t *p, int *nitems);
235 static string_t *make_filename(string_t *s);
236 static resource_t *build_fontdirs(resource_t *tail);
237 static resource_t *build_fontdir(resource_t **fnt, int nfnt);
238 static int rsrcid_to_token(int lookahead);
239
240 %}
241 %union{
242         string_t        *str;
243         int             num;
244         int             *iptr;
245         char            *cptr;
246         resource_t      *res;
247         accelerator_t   *acc;
248         bitmap_t        *bmp;
249         dialog_t        *dlg;
250         dialogex_t      *dlgex;
251         font_t          *fnt;
252         fontdir_t       *fnd;
253         menu_t          *men;
254         menuex_t        *menex;
255         html_t          *html;
256         rcdata_t        *rdt;
257         stringtable_t   *stt;
258         stt_entry_t     *stte;
259         user_t          *usr;
260         messagetable_t  *msg;
261         versioninfo_t   *veri;
262         control_t       *ctl;
263         name_id_t       *nid;
264         font_id_t       *fntid;
265         language_t      *lan;
266         version_t       *ver;
267         characts_t      *chars;
268         event_t         *event;
269         menu_item_t     *menitm;
270         menuex_item_t   *menexitm;
271         itemex_opt_t    *exopt;
272         raw_data_t      *raw;
273         lvc_t           *lvc;
274         ver_value_t     *val;
275         ver_block_t     *blk;
276         ver_words_t     *verw;
277         toolbar_t       *tlbar;
278         toolbar_item_t  *tlbarItems;
279         dlginit_t       *dginit;
280         style_pair_t    *styles;
281         style_t         *style;
282         ani_any_t       *ani;
283 }
284
285 %token tNL
286 %token <num> tNUMBER tLNUMBER
287 %token <str> tSTRING tIDENT tFILENAME
288 %token <raw> tRAWDATA
289 %token tACCELERATORS tBITMAP tCURSOR tDIALOG tDIALOGEX tMENU tMENUEX tMESSAGETABLE
290 %token tRCDATA tVERSIONINFO tSTRINGTABLE tFONT tFONTDIR tICON tHTML
291 %token tAUTO3STATE tAUTOCHECKBOX tAUTORADIOBUTTON tCHECKBOX tDEFPUSHBUTTON
292 %token tPUSHBUTTON tRADIOBUTTON tSTATE3 /* PUSHBOX */
293 %token tGROUPBOX tCOMBOBOX tLISTBOX tSCROLLBAR
294 %token tCONTROL tEDITTEXT
295 %token tRTEXT tCTEXT tLTEXT
296 %token tBLOCK tVALUE
297 %token tSHIFT tALT tASCII tVIRTKEY tGRAYED tCHECKED tINACTIVE tNOINVERT
298 %token tPURE tIMPURE tDISCARDABLE tLOADONCALL tPRELOAD tFIXED tMOVEABLE
299 %token tCLASS tCAPTION tCHARACTERISTICS tEXSTYLE tSTYLE tVERSION tLANGUAGE
300 %token tFILEVERSION tPRODUCTVERSION tFILEFLAGSMASK tFILEOS tFILETYPE tFILEFLAGS tFILESUBTYPE
301 %token tMENUBARBREAK tMENUBREAK tMENUITEM tPOPUP tSEPARATOR
302 %token tHELP
303 %token tTOOLBAR tBUTTON
304 %token tBEGIN tEND
305 %token tDLGINIT
306 %left '|'
307 %left '^'
308 %left '&'
309 %left '+' '-'
310 %left '*' '/'
311 %right '~' tNOT
312 %left pUPM
313
314 %type <res>     resource_file resource resources resource_definition
315 %type <stt>     stringtable strings
316 %type <fnt>     font
317 %type <fnd>     fontdir
318 %type <acc>     accelerators
319 %type <event>   events
320 %type <bmp>     bitmap
321 %type <ani>     cursor icon
322 %type <dlg>     dialog dlg_attributes
323 %type <ctl>     ctrls gen_ctrl lab_ctrl ctrl_desc iconinfo
324 %type <iptr>    helpid
325 %type <dlgex>   dialogex dlgex_attribs
326 %type <ctl>     exctrls gen_exctrl lab_exctrl exctrl_desc
327 %type <html>    html
328 %type <rdt>     rcdata
329 %type <raw>     raw_data raw_elements opt_data file_raw
330 %type <veri>    versioninfo fix_version
331 %type <verw>    ver_words
332 %type <blk>     ver_blocks ver_block
333 %type <val>     ver_values ver_value
334 %type <men>     menu
335 %type <menitm>  item_definitions menu_body
336 %type <menex>   menuex
337 %type <menexitm> itemex_definitions menuex_body
338 %type <exopt>   itemex_p_options itemex_options
339 %type <msg>     messagetable
340 %type <usr>     userres
341 %type <num>     item_options
342 %type <nid>     nameid nameid_s ctlclass usertype
343 %type <num>     acc_opt acc accs
344 %type <iptr>    loadmemopts lamo lama
345 %type <fntid>   opt_font opt_exfont opt_expr
346 %type <lvc>     opt_lvc
347 %type <lan>     opt_language
348 %type <chars>   opt_characts
349 %type <ver>     opt_version
350 %type <num>     expr xpr
351 %type <iptr>    e_expr
352 %type <tlbar>   toolbar
353 %type <tlbarItems>      toolbar_items
354 %type <dginit>  dlginit
355 %type <styles>  optional_style_pair
356 %type <num>     any_num
357 %type <style>   style
358 %type <str>     filename
359
360 %%
361
362 resource_file
363         : resources {
364                 resource_t *rsc;
365                 /* First add stringtables to the resource-list */
366                 rsc = build_stt_resources(sttres);
367                 /* 'build_stt_resources' returns a head and $1 is a tail */
368                 if($1)
369                 {
370                         $1->next = rsc;
371                         if(rsc)
372                                 rsc->prev = $1;
373                 }
374                 else
375                         $1 = rsc;
376                 /* Find the tail again */
377                 while($1 && $1->next)
378                         $1 = $1->next;
379                 /* Now add any fontdirecory */
380                 rsc = build_fontdirs($1);
381                 /* 'build_fontdir' returns a head and $1 is a tail */
382                 if($1)
383                 {
384                         $1->next = rsc;
385                         if(rsc)
386                                 rsc->prev = $1;
387                 }
388                 else
389                         $1 = rsc;
390                 /* Final statement before were done */
391                 resource_top = get_resource_head($1);
392                 }
393         ;
394
395 /* Resources are put into a linked list */
396 resources
397         : /* Empty */           { $$ = NULL; want_id = 1; }
398         | resources resource    {
399                 if($2)
400                 {
401                         resource_t *tail = $2;
402                         resource_t *head = $2;
403                         while(tail->next)
404                                 tail = tail->next;
405                         while(head->prev)
406                                 head = head->prev;
407                         head->prev = $1;
408                         if($1)
409                                 $1->next = head;
410                         $$ = tail;
411                         /* Check for duplicate identifiers */
412                         while($1 && head)
413                         {
414                                 resource_t *rsc = $1;
415                                 while(rsc)
416                                 {
417                                         if(rsc->type == head->type
418                                         && rsc->lan->id == head->lan->id
419                                         && rsc->lan->sub == head->lan->sub
420                                         && !compare_name_id(rsc->name, head->name)
421                                         && (rsc->type != res_usr || !compare_name_id(rsc->res.usr->type,head->res.usr->type)))
422                                         {
423                                                 yyerror("Duplicate resource name '%s'", get_nameid_str(rsc->name));
424                                         }
425                                         rsc = rsc->prev;
426                                 }
427                                 head = head->next;
428                         }
429                 }
430                 else if($1)
431                 {
432                         resource_t *tail = $1;
433                         while(tail->next)
434                                 tail = tail->next;
435                         $$ = tail;
436                 }
437                 else
438                         $$ = NULL;
439
440                 if(!dont_want_id)       /* See comments in language parsing below */
441                         want_id = 1;
442                 dont_want_id = 0;
443                 }
444         /*
445          * The following newline rule will never get reduced because we never
446          * get the tNL token, unless we explicitly set the 'want_nl'
447          * flag, which we don't.
448          * The *ONLY* reason for this to be here is because Berkeley
449          * yacc (byacc), at least version 1.9, has a bug.
450          * (identified in the generated parser on the second
451          *  line with:
452          *  static char yysccsid[] = "@(#)yaccpar   1.9 (Berkeley) 02/21/93";
453          * )
454          * This extra rule fixes it.
455          * The problem is that the expression handling rule "expr: xpr"
456          * is not reduced on non-terminal tokens, defined above in the
457          * %token declarations. Token tNL is the only non-terminal that
458          * can occur. The error becomes visible in the language parsing
459          * rule below, which looks at the look-ahead token and tests it
460          * for tNL. However, byacc already generates an error upon reading
461          * the token instead of keeping it as a lookahead. The reason
462          * lies in the lack of a $default transition in the "expr : xpr . "
463          * state (currently state 25). It is probably omitted because tNL
464          * is a non-terminal and the state contains 2 s/r conflicts. The
465          * state enumerates all possible transitions instead of using a
466          * $default transition.
467          * All in all, it is a bug in byacc. (period)
468          */
469         | resources tNL
470         ;
471
472
473 /* Parse top level resource definitions etc. */
474 resource
475         : expr usrcvt resource_definition {
476                 $$ = $3;
477                 if($$)
478                 {
479                         if($1 > 65535 || $1 < -32768)
480                                 yyerror("Resource's ID out of range (%d)", $1);
481                         $$->name = new_name_id();
482                         $$->name->type = name_ord;
483                         $$->name->name.i_name = $1;
484                         chat("Got %s (%d)\n", get_typename($3), $$->name->name.i_name);
485                         }
486                         }
487         | tIDENT usrcvt resource_definition {
488                 $$ = $3;
489                 if($$)
490                 {
491                         $$->name = new_name_id();
492                         $$->name->type = name_str;
493                         $$->name->name.s_name = $1;
494                         chat("Got %s (%s)\n", get_typename($3), $$->name->name.s_name->str.cstr);
495                 }
496                 }
497         | stringtable {
498                 /* Don't do anything, stringtables are converted to
499                  * resource_t structures when we are finished parsing and
500                  * the final rule of the parser is reduced (see above)
501                  */
502                 $$ = NULL;
503                 chat("Got STRINGTABLE\n");
504                 }
505         | tLANGUAGE {want_nl = 1; } expr ',' expr {
506                 /* We *NEED* the newline to delimit the expression.
507                  * Otherwise, we would not be able to set the next
508                  * want_id anymore because of the token-lookahead.
509                  *
510                  * However, we can test the lookahead-token for
511                  * being "non-expression" type, in which case we
512                  * continue. Fortunately, tNL is the only token that
513                  * will break expression parsing and is implicitly
514                  * void, so we just remove it. This scheme makes it
515                  * possible to do some (not all) fancy preprocessor
516                  * stuff.
517                  * BTW, we also need to make sure that the next
518                  * reduction of 'resources' above will *not* set
519                  * want_id because we already have a lookahead that
520                  * cannot be undone.
521                  */
522                 if(yychar != YYEMPTY && yychar != tNL)
523                         dont_want_id = 1;
524
525                 if(yychar == tNL)
526                         yychar = YYEMPTY;       /* Could use 'yyclearin', but we already need the*/
527                                                 /* direct access to yychar in rule 'usrcvt' below. */
528                 else if(yychar == tIDENT)
529                         parser_warning("LANGUAGE statement not delimited with newline; next identifier might be wrong\n");
530
531                 want_nl = 0;    /* We don't want it anymore if we didn't get it */
532
533                 if(!win32)
534                         parser_warning("LANGUAGE not supported in 16-bit mode\n");
535                 free(currentlanguage);
536                 if (get_language_codepage($3, $5) == -1)
537                         yyerror( "Language %04x is not supported", ($5<<10) + $3);
538                 currentlanguage = new_language($3, $5);
539                 $$ = NULL;
540                 chat("Got LANGUAGE %d,%d (0x%04x)\n", $3, $5, ($5<<10) + $3);
541                 }
542         ;
543
544 /*
545  * Remapping of numerical resource types
546  * (see also comment of called function below)
547  */
548 usrcvt  : /* Empty */   { yychar = rsrcid_to_token(yychar); }
549         ;
550
551 /*
552  * Get a valid name/id
553  */
554 nameid  : expr  {
555                 if($1 > 65535 || $1 < -32768)
556                         yyerror("Resource's ID out of range (%d)", $1);
557                 $$ = new_name_id();
558                 $$->type = name_ord;
559                 $$->name.i_name = $1;
560                 }
561         | tIDENT {
562                 $$ = new_name_id();
563                 $$->type = name_str;
564                 $$->name.s_name = $1;
565                 }
566         ;
567
568 /*
569  * Extra string recognition for CLASS statement in dialogs
570  */
571 nameid_s: nameid        { $$ = $1; }
572         | tSTRING       {
573                 $$ = new_name_id();
574                 $$->type = name_str;
575                 $$->name.s_name = $1;
576                 }
577         ;
578
579 /* get the value for a single resource*/
580 resource_definition
581         : accelerators  { $$ = new_resource(res_acc, $1, $1->memopt, $1->lvc.language); }
582         | bitmap        { $$ = new_resource(res_bmp, $1, $1->memopt, $1->data->lvc.language); }
583         | cursor {
584                 resource_t *rsc;
585                 if($1->type == res_anicur)
586                 {
587                         $$ = rsc = new_resource(res_anicur, $1->u.ani, $1->u.ani->memopt, $1->u.ani->data->lvc.language);
588                 }
589                 else if($1->type == res_curg)
590                 {
591                         cursor_t *cur;
592                         $$ = rsc = new_resource(res_curg, $1->u.curg, $1->u.curg->memopt, $1->u.curg->lvc.language);
593                         for(cur = $1->u.curg->cursorlist; cur; cur = cur->next)
594                         {
595                                 rsc->prev = new_resource(res_cur, cur, $1->u.curg->memopt, $1->u.curg->lvc.language);
596                                 rsc->prev->next = rsc;
597                                 rsc = rsc->prev;
598                                 rsc->name = new_name_id();
599                                 rsc->name->type = name_ord;
600                                 rsc->name->name.i_name = cur->id;
601                         }
602                 }
603                 else
604                         internal_error(__FILE__, __LINE__, "Invalid top-level type %d in cursor resource\n", $1->type);
605                 free($1);
606                 }
607         | dialog        { $$ = new_resource(res_dlg, $1, $1->memopt, $1->lvc.language); }
608         | dialogex {
609                 if(win32)
610                         $$ = new_resource(res_dlgex, $1, $1->memopt, $1->lvc.language);
611                 else
612                         $$ = NULL;
613                 }
614         | dlginit       { $$ = new_resource(res_dlginit, $1, $1->memopt, $1->data->lvc.language); }
615         | font          { $$ = new_resource(res_fnt, $1, $1->memopt, $1->data->lvc.language); }
616         | fontdir       { $$ = new_resource(res_fntdir, $1, $1->memopt, $1->data->lvc.language); }
617         | icon {
618                 resource_t *rsc;
619                 if($1->type == res_aniico)
620                 {
621                         $$ = rsc = new_resource(res_aniico, $1->u.ani, $1->u.ani->memopt, $1->u.ani->data->lvc.language);
622                 }
623                 else if($1->type == res_icog)
624                 {
625                         icon_t *ico;
626                         $$ = rsc = new_resource(res_icog, $1->u.icog, $1->u.icog->memopt, $1->u.icog->lvc.language);
627                         for(ico = $1->u.icog->iconlist; ico; ico = ico->next)
628                         {
629                                 rsc->prev = new_resource(res_ico, ico, $1->u.icog->memopt, $1->u.icog->lvc.language);
630                                 rsc->prev->next = rsc;
631                                 rsc = rsc->prev;
632                                 rsc->name = new_name_id();
633                                 rsc->name->type = name_ord;
634                                 rsc->name->name.i_name = ico->id;
635                         }
636                 }
637                 else
638                         internal_error(__FILE__, __LINE__, "Invalid top-level type %d in icon resource\n", $1->type);
639                 free($1);
640                 }
641         | menu          { $$ = new_resource(res_men, $1, $1->memopt, $1->lvc.language); }
642         | menuex {
643                 if(win32)
644                         $$ = new_resource(res_menex, $1, $1->memopt, $1->lvc.language);
645                 else
646                         $$ = NULL;
647                 }
648         | messagetable  { $$ = new_resource(res_msg, $1, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, $1->data->lvc.language); }
649         | html          { $$ = new_resource(res_html, $1, $1->memopt, $1->data->lvc.language); }
650         | rcdata        { $$ = new_resource(res_rdt, $1, $1->memopt, $1->data->lvc.language); }
651         | toolbar       { $$ = new_resource(res_toolbar, $1, $1->memopt, $1->lvc.language); }
652         | userres       { $$ = new_resource(res_usr, $1, $1->memopt, $1->data->lvc.language); }
653         | versioninfo   { $$ = new_resource(res_ver, $1, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, $1->lvc.language); }
654         ;
655
656
657 filename: tFILENAME     { $$ = make_filename($1); }
658         | tIDENT        { $$ = make_filename($1); }
659         | tSTRING       { $$ = make_filename($1); }
660         ;
661
662 /* ------------------------------ Bitmap ------------------------------ */
663 bitmap  : tBITMAP loadmemopts file_raw  { $$ = new_bitmap($3, $2); }
664         ;
665
666 /* ------------------------------ Cursor ------------------------------ */
667 cursor  : tCURSOR loadmemopts file_raw  {
668                 $$ = new_ani_any();
669                 if($3->size > 4 && !memcmp($3->data, riff, sizeof(riff)))
670                 {
671                         $$->type = res_anicur;
672                         $$->u.ani = new_ani_curico(res_anicur, $3, $2);
673                 }
674                 else
675                 {
676                         $$->type = res_curg;
677                         $$->u.curg = new_cursor_group($3, $2);
678                 }
679         }
680         ;
681
682 /* ------------------------------ Icon ------------------------------ */
683 icon    : tICON loadmemopts file_raw    {
684                 $$ = new_ani_any();
685                 if($3->size > 4 && !memcmp($3->data, riff, sizeof(riff)))
686                 {
687                         $$->type = res_aniico;
688                         $$->u.ani = new_ani_curico(res_aniico, $3, $2);
689                 }
690                 else
691                 {
692                         $$->type = res_icog;
693                         $$->u.icog = new_icon_group($3, $2);
694                 }
695         }
696         ;
697
698 /* ------------------------------ Font ------------------------------ */
699         /*
700          * The reading of raw_data for fonts is a Borland BRC
701          * extension. MS generates an error. However, it is
702          * most logical to support this, considering how wine
703          * enters things in CVS (ascii).
704          */
705 font    : tFONT loadmemopts file_raw    { $$ = new_font($3, $2); }
706         ;
707
708         /*
709          * The fontdir is a Borland BRC extension which only
710          * reads the data as 'raw_data' from the file.
711          * I don't know whether it is interpreted.
712          * The fontdir is generated if it was not present and
713          * fonts are defined in the source.
714          */
715 fontdir : tFONTDIR loadmemopts file_raw { $$ = new_fontdir($3, $2); }
716         ;
717
718 /* ------------------------------ MessageTable ------------------------------ */
719 /* It might be interesting to implement the MS Message compiler here as well
720  * to get everything in one source. Might be a future project.
721  */
722 messagetable
723         : tMESSAGETABLE loadmemopts file_raw    {
724                 if(!win32)
725                         parser_warning("MESSAGETABLE not supported in 16-bit mode\n");
726                 $$ = new_messagetable($3, $2);
727                 }
728         ;
729
730 /* ------------------------------ HTML ------------------------------ */
731 html    : tHTML loadmemopts file_raw    { $$ = new_html($3, $2); }
732         ;
733
734 /* ------------------------------ RCData ------------------------------ */
735 rcdata  : tRCDATA loadmemopts file_raw  { $$ = new_rcdata($3, $2); }
736         ;
737
738 /* ------------------------------ DLGINIT ------------------------------ */
739 dlginit : tDLGINIT loadmemopts file_raw { $$ = new_dlginit($3, $2); }
740         ;
741
742 /* ------------------------------ UserType ------------------------------ */
743 userres : usertype loadmemopts file_raw         {
744 #ifdef WORDS_BIGENDIAN
745                         if(pedantic && byteorder != WRC_BO_LITTLE)
746 #else
747                         if(pedantic && byteorder == WRC_BO_BIG)
748 #endif
749                                 parser_warning("Byteordering is not little-endian and type cannot be interpreted\n");
750                         $$ = new_user($1, $3, $2);
751                 }
752         ;
753
754 usertype: tNUMBER {
755                 $$ = new_name_id();
756                 $$->type = name_ord;
757                 $$->name.i_name = $1;
758                 }
759         | tIDENT {
760                 $$ = new_name_id();
761                 $$->type = name_str;
762                 $$->name.s_name = $1;
763                 }
764         ;
765
766 /* ------------------------------ Accelerator ------------------------------ */
767 accelerators
768         : tACCELERATORS loadmemopts opt_lvc tBEGIN events tEND {
769                 $$ = new_accelerator();
770                 if($2)
771                 {
772                         $$->memopt = *($2);
773                         free($2);
774                 }
775                 else
776                 {
777                         $$->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
778                 }
779                 if(!$5)
780                         yyerror("Accelerator table must have at least one entry");
781                 $$->events = get_event_head($5);
782                 if($3)
783                 {
784                         $$->lvc = *($3);
785                         free($3);
786                 }
787                 if(!$$->lvc.language)
788                         $$->lvc.language = dup_language(currentlanguage);
789                 }
790         ;
791
792 events  : /* Empty */                           { $$=NULL; }
793         | events tSTRING ',' expr acc_opt       { $$=add_string_event($2, $4, $5, $1); }
794         | events expr ',' expr acc_opt          { $$=add_event($2, $4, $5, $1); }
795         ;
796
797 /*
798  * The empty rule generates a s/r conflict because of {bi,u}nary expr
799  * on - and +. It cannot be solved in any way because it is the same as
800  * the if/then/else problem (LALR(1) problem). The conflict is moved
801  * away by forcing it to be in the expression handling below.
802  */
803 acc_opt : /* Empty */   { $$ = 0; }
804         | ',' accs      { $$ = $2; }
805         ;
806
807 accs    : acc           { $$ = $1; }
808         | accs ',' acc  { $$ = $1 | $3; }
809         ;
810
811 acc     : tNOINVERT     { $$ = WRC_AF_NOINVERT; }
812         | tSHIFT        { $$ = WRC_AF_SHIFT; }
813         | tCONTROL      { $$ = WRC_AF_CONTROL; }
814         | tALT          { $$ = WRC_AF_ALT; }
815         | tASCII        { $$ = WRC_AF_ASCII; }
816         | tVIRTKEY      { $$ = WRC_AF_VIRTKEY; }
817         ;
818
819 /* ------------------------------ Dialog ------------------------------ */
820 /* FIXME: Support EXSTYLE in the dialog line itself */
821 dialog  : tDIALOG loadmemopts expr ',' expr ',' expr ',' expr dlg_attributes
822           tBEGIN  ctrls tEND {
823                 if($2)
824                 {
825                         $10->memopt = *($2);
826                         free($2);
827                 }
828                 else
829                         $10->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
830                 $10->x = $3;
831                 $10->y = $5;
832                 $10->width = $7;
833                 $10->height = $9;
834                 $10->controls = get_control_head($12);
835                 $$ = $10;
836                 if(!$$->gotstyle)
837                 {
838                         $$->style = new_style(0,0);
839                         $$->style->or_mask = WS_POPUP;
840                         $$->gotstyle = TRUE;
841                 }
842                 if($$->title)
843                         $$->style->or_mask |= WS_CAPTION;
844                 if($$->font)
845                         $$->style->or_mask |= DS_SETFONT;
846
847                 $$->style->or_mask &= ~($$->style->and_mask);
848                 $$->style->and_mask = 0;
849
850                 if(!$$->lvc.language)
851                         $$->lvc.language = dup_language(currentlanguage);
852                 }
853         ;
854
855 dlg_attributes
856         : /* Empty */                           { $$=new_dialog(); }
857         | dlg_attributes tSTYLE style           { $$=dialog_style($3,$1); }
858         | dlg_attributes tEXSTYLE style         { $$=dialog_exstyle($3,$1); }
859         | dlg_attributes tCAPTION tSTRING       { $$=dialog_caption($3,$1); }
860         | dlg_attributes opt_font               { $$=dialog_font($2,$1); }
861         | dlg_attributes tCLASS nameid_s        { $$=dialog_class($3,$1); }
862         | dlg_attributes tMENU nameid           { $$=dialog_menu($3,$1); }
863         | dlg_attributes opt_language           { $$=dialog_language($2,$1); }
864         | dlg_attributes opt_characts           { $$=dialog_characteristics($2,$1); }
865         | dlg_attributes opt_version            { $$=dialog_version($2,$1); }
866         ;
867
868 ctrls   : /* Empty */                           { $$ = NULL; }
869         | ctrls tCONTROL        gen_ctrl        { $$=ins_ctrl(-1, 0, $3, $1); }
870         | ctrls tEDITTEXT       ctrl_desc       { $$=ins_ctrl(CT_EDIT, 0, $3, $1); }
871         | ctrls tLISTBOX        ctrl_desc       { $$=ins_ctrl(CT_LISTBOX, 0, $3, $1); }
872         | ctrls tCOMBOBOX       ctrl_desc       { $$=ins_ctrl(CT_COMBOBOX, 0, $3, $1); }
873         | ctrls tSCROLLBAR      ctrl_desc       { $$=ins_ctrl(CT_SCROLLBAR, 0, $3, $1); }
874         | ctrls tCHECKBOX       lab_ctrl        { $$=ins_ctrl(CT_BUTTON, BS_CHECKBOX, $3, $1); }
875         | ctrls tDEFPUSHBUTTON  lab_ctrl        { $$=ins_ctrl(CT_BUTTON, BS_DEFPUSHBUTTON, $3, $1); }
876         | ctrls tGROUPBOX       lab_ctrl        { $$=ins_ctrl(CT_BUTTON, BS_GROUPBOX, $3, $1);}
877         | ctrls tPUSHBUTTON     lab_ctrl        { $$=ins_ctrl(CT_BUTTON, BS_PUSHBUTTON, $3, $1); }
878 /*      | ctrls tPUSHBOX        lab_ctrl        { $$=ins_ctrl(CT_BUTTON, BS_PUSHBOX, $3, $1); } */
879         | ctrls tRADIOBUTTON    lab_ctrl        { $$=ins_ctrl(CT_BUTTON, BS_RADIOBUTTON, $3, $1); }
880         | ctrls tAUTO3STATE     lab_ctrl        { $$=ins_ctrl(CT_BUTTON, BS_AUTO3STATE, $3, $1); }
881         | ctrls tSTATE3         lab_ctrl        { $$=ins_ctrl(CT_BUTTON, BS_3STATE, $3, $1); }
882         | ctrls tAUTOCHECKBOX   lab_ctrl        { $$=ins_ctrl(CT_BUTTON, BS_AUTOCHECKBOX, $3, $1); }
883         | ctrls tAUTORADIOBUTTON lab_ctrl       { $$=ins_ctrl(CT_BUTTON, BS_AUTORADIOBUTTON, $3, $1); }
884         | ctrls tLTEXT          lab_ctrl        { $$=ins_ctrl(CT_STATIC, SS_LEFT, $3, $1); }
885         | ctrls tCTEXT          lab_ctrl        { $$=ins_ctrl(CT_STATIC, SS_CENTER, $3, $1); }
886         | ctrls tRTEXT          lab_ctrl        { $$=ins_ctrl(CT_STATIC, SS_RIGHT, $3, $1); }
887         /* special treatment for icons, as the extent is optional */
888         | ctrls tICON nameid_s opt_comma expr ',' expr ',' expr iconinfo {
889                 $10->title = $3;
890                 $10->id = $5;
891                 $10->x = $7;
892                 $10->y = $9;
893                 $$ = ins_ctrl(CT_STATIC, SS_ICON, $10, $1);
894                 }
895         ;
896
897 lab_ctrl
898         : nameid_s opt_comma expr ',' expr ',' expr ',' expr ',' expr optional_style_pair {
899                 $$=new_control();
900                 $$->title = $1;
901                 $$->id = $3;
902                 $$->x = $5;
903                 $$->y = $7;
904                 $$->width = $9;
905                 $$->height = $11;
906                 if($12)
907                 {
908                         $$->style = $12->style;
909                         $$->gotstyle = TRUE;
910                         if ($12->exstyle)
911                         {
912                             $$->exstyle = $12->exstyle;
913                             $$->gotexstyle = TRUE;
914                         }
915                         free($12);
916                 }
917                 }
918         ;
919
920 ctrl_desc
921         : expr ',' expr ',' expr ',' expr ',' expr optional_style_pair {
922                 $$ = new_control();
923                 $$->id = $1;
924                 $$->x = $3;
925                 $$->y = $5;
926                 $$->width = $7;
927                 $$->height = $9;
928                 if($10)
929                 {
930                         $$->style = $10->style;
931                         $$->gotstyle = TRUE;
932                         if ($10->exstyle)
933                         {
934                             $$->exstyle = $10->exstyle;
935                             $$->gotexstyle = TRUE;
936                         }
937                         free($10);
938                 }
939                 }
940         ;
941
942 iconinfo: /* Empty */
943                 { $$ = new_control(); }
944
945         | ',' expr ',' expr {
946                 $$ = new_control();
947                 $$->width = $2;
948                 $$->height = $4;
949                 }
950         | ',' expr ',' expr ',' style {
951                 $$ = new_control();
952                 $$->width = $2;
953                 $$->height = $4;
954                 $$->style = $6;
955                 $$->gotstyle = TRUE;
956                 }
957         | ',' expr ',' expr ',' style ',' style {
958                 $$ = new_control();
959                 $$->width = $2;
960                 $$->height = $4;
961                 $$->style = $6;
962                 $$->gotstyle = TRUE;
963                 $$->exstyle = $8;
964                 $$->gotexstyle = TRUE;
965                 }
966         ;
967
968 gen_ctrl: nameid_s opt_comma expr ',' ctlclass ',' style ',' expr ',' expr ',' expr ',' expr ',' style {
969                 $$=new_control();
970                 $$->title = $1;
971                 $$->id = $3;
972                 $$->ctlclass = convert_ctlclass($5);
973                 $$->style = $7;
974                 $$->gotstyle = TRUE;
975                 $$->x = $9;
976                 $$->y = $11;
977                 $$->width = $13;
978                 $$->height = $15;
979                 $$->exstyle = $17;
980                 $$->gotexstyle = TRUE;
981                 }
982         | nameid_s opt_comma expr ',' ctlclass ',' style ',' expr ',' expr ',' expr ',' expr {
983                 $$=new_control();
984                 $$->title = $1;
985                 $$->id = $3;
986                 $$->ctlclass = convert_ctlclass($5);
987                 $$->style = $7;
988                 $$->gotstyle = TRUE;
989                 $$->x = $9;
990                 $$->y = $11;
991                 $$->width = $13;
992                 $$->height = $15;
993                 }
994         ;
995
996 opt_font
997         : tFONT expr ',' tSTRING        { $$ = new_font_id($2, $4, 0, 0); }
998         ;
999
1000 /* ------------------------------ style flags ------------------------------ */
1001 optional_style_pair
1002         : /* Empty */           { $$ = NULL; }
1003         | ',' style             { $$ = new_style_pair($2, 0); }
1004         | ',' style ',' style   { $$ = new_style_pair($2, $4); }
1005         ;
1006
1007 style
1008         : style '|' style       { $$ = new_style($1->or_mask | $3->or_mask, $1->and_mask | $3->and_mask); free($1); free($3);}
1009         | '(' style ')'         { $$ = $2; }
1010         | any_num               { $$ = new_style($1, 0); }
1011         | tNOT any_num          { $$ = new_style(0, $2); }
1012         ;
1013
1014 ctlclass
1015         : expr  {
1016                 $$ = new_name_id();
1017                 $$->type = name_ord;
1018                 $$->name.i_name = $1;
1019                 }
1020         | tSTRING {
1021                 $$ = new_name_id();
1022                 $$->type = name_str;
1023                 $$->name.s_name = $1;
1024                 }
1025         ;
1026
1027 /* ------------------------------ DialogEx ------------------------------ */
1028 dialogex: tDIALOGEX loadmemopts expr ',' expr ',' expr ',' expr helpid dlgex_attribs
1029           tBEGIN  exctrls tEND {
1030                 if(!win32)
1031                         parser_warning("DIALOGEX not supported in 16-bit mode\n");
1032                 if($2)
1033                 {
1034                         $11->memopt = *($2);
1035                         free($2);
1036                 }
1037                 else
1038                         $11->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1039                 $11->x = $3;
1040                 $11->y = $5;
1041                 $11->width = $7;
1042                 $11->height = $9;
1043                 if($10)
1044                 {
1045                         $11->helpid = *($10);
1046                         $11->gothelpid = TRUE;
1047                         free($10);
1048                 }
1049                 $11->controls = get_control_head($13);
1050                 $$ = $11;
1051
1052                 assert($$->style != NULL);
1053                 if(!$$->gotstyle)
1054                 {
1055                         $$->style->or_mask = WS_POPUP;
1056                         $$->gotstyle = TRUE;
1057                 }
1058                 if($$->title)
1059                         $$->style->or_mask |= WS_CAPTION;
1060                 if($$->font)
1061                         $$->style->or_mask |= DS_SETFONT;
1062
1063                 $$->style->or_mask &= ~($$->style->and_mask);
1064                 $$->style->and_mask = 0;
1065
1066                 if(!$$->lvc.language)
1067                         $$->lvc.language = dup_language(currentlanguage);
1068                 }
1069         ;
1070
1071 dlgex_attribs
1072         : /* Empty */                           { $$=new_dialogex(); }
1073         | dlgex_attribs tSTYLE style            { $$=dialogex_style($3,$1); }
1074         | dlgex_attribs tEXSTYLE style          { $$=dialogex_exstyle($3,$1); }
1075         | dlgex_attribs tCAPTION tSTRING        { $$=dialogex_caption($3,$1); }
1076         | dlgex_attribs opt_font                { $$=dialogex_font($2,$1); }
1077         | dlgex_attribs opt_exfont              { $$=dialogex_font($2,$1); }
1078         | dlgex_attribs tCLASS nameid_s         { $$=dialogex_class($3,$1); }
1079         | dlgex_attribs tMENU nameid            { $$=dialogex_menu($3,$1); }
1080         | dlgex_attribs opt_language            { $$=dialogex_language($2,$1); }
1081         | dlgex_attribs opt_characts            { $$=dialogex_characteristics($2,$1); }
1082         | dlgex_attribs opt_version             { $$=dialogex_version($2,$1); }
1083         ;
1084
1085 exctrls : /* Empty */                           { $$ = NULL; }
1086         | exctrls tCONTROL      gen_exctrl      { $$=ins_ctrl(-1, 0, $3, $1); }
1087         | exctrls tEDITTEXT     exctrl_desc     { $$=ins_ctrl(CT_EDIT, 0, $3, $1); }
1088         | exctrls tLISTBOX      exctrl_desc     { $$=ins_ctrl(CT_LISTBOX, 0, $3, $1); }
1089         | exctrls tCOMBOBOX     exctrl_desc     { $$=ins_ctrl(CT_COMBOBOX, 0, $3, $1); }
1090         | exctrls tSCROLLBAR    exctrl_desc     { $$=ins_ctrl(CT_SCROLLBAR, 0, $3, $1); }
1091         | exctrls tCHECKBOX     lab_exctrl      { $$=ins_ctrl(CT_BUTTON, BS_CHECKBOX, $3, $1); }
1092         | exctrls tDEFPUSHBUTTON lab_exctrl     { $$=ins_ctrl(CT_BUTTON, BS_DEFPUSHBUTTON, $3, $1); }
1093         | exctrls tGROUPBOX     lab_exctrl      { $$=ins_ctrl(CT_BUTTON, BS_GROUPBOX, $3, $1);}
1094         | exctrls tPUSHBUTTON   lab_exctrl      { $$=ins_ctrl(CT_BUTTON, BS_PUSHBUTTON, $3, $1); }
1095 /*      | exctrls tPUSHBOX      lab_exctrl      { $$=ins_ctrl(CT_BUTTON, BS_PUSHBOX, $3, $1); } */
1096         | exctrls tRADIOBUTTON  lab_exctrl      { $$=ins_ctrl(CT_BUTTON, BS_RADIOBUTTON, $3, $1); }
1097         | exctrls tAUTO3STATE   lab_exctrl      { $$=ins_ctrl(CT_BUTTON, BS_AUTO3STATE, $3, $1); }
1098         | exctrls tSTATE3       lab_exctrl      { $$=ins_ctrl(CT_BUTTON, BS_3STATE, $3, $1); }
1099         | exctrls tAUTOCHECKBOX lab_exctrl      { $$=ins_ctrl(CT_BUTTON, BS_AUTOCHECKBOX, $3, $1); }
1100         | exctrls tAUTORADIOBUTTON lab_exctrl   { $$=ins_ctrl(CT_BUTTON, BS_AUTORADIOBUTTON, $3, $1); }
1101         | exctrls tLTEXT        lab_exctrl      { $$=ins_ctrl(CT_STATIC, SS_LEFT, $3, $1); }
1102         | exctrls tCTEXT        lab_exctrl      { $$=ins_ctrl(CT_STATIC, SS_CENTER, $3, $1); }
1103         | exctrls tRTEXT        lab_exctrl      { $$=ins_ctrl(CT_STATIC, SS_RIGHT, $3, $1); }
1104         /* special treatment for icons, as the extent is optional */
1105         | exctrls tICON nameid_s opt_comma expr ',' expr ',' expr iconinfo {
1106                 $10->title = $3;
1107                 $10->id = $5;
1108                 $10->x = $7;
1109                 $10->y = $9;
1110                 $$ = ins_ctrl(CT_STATIC, SS_ICON, $10, $1);
1111                 }
1112         ;
1113
1114 gen_exctrl
1115         : nameid_s opt_comma expr ',' ctlclass ',' style ',' expr ',' expr ',' expr ','
1116           expr ',' style helpid opt_data {
1117                 $$=new_control();
1118                 $$->title = $1;
1119                 $$->id = $3;
1120                 $$->ctlclass = convert_ctlclass($5);
1121                 $$->style = $7;
1122                 $$->gotstyle = TRUE;
1123                 $$->x = $9;
1124                 $$->y = $11;
1125                 $$->width = $13;
1126                 $$->height = $15;
1127                 if($17)
1128                 {
1129                         $$->exstyle = $17;
1130                         $$->gotexstyle = TRUE;
1131                 }
1132                 if($18)
1133                 {
1134                         $$->helpid = *($18);
1135                         $$->gothelpid = TRUE;
1136                         free($18);
1137                 }
1138                 $$->extra = $19;
1139                 }
1140         | nameid_s opt_comma expr ',' ctlclass ',' style ',' expr ',' expr ',' expr ',' expr opt_data {
1141                 $$=new_control();
1142                 $$->title = $1;
1143                 $$->id = $3;
1144                 $$->style = $7;
1145                 $$->gotstyle = TRUE;
1146                 $$->ctlclass = convert_ctlclass($5);
1147                 $$->x = $9;
1148                 $$->y = $11;
1149                 $$->width = $13;
1150                 $$->height = $15;
1151                 $$->extra = $16;
1152                 }
1153         ;
1154
1155 lab_exctrl
1156         : nameid_s opt_comma expr ',' expr ',' expr ',' expr ',' expr optional_style_pair helpid opt_data {
1157                 $$=new_control();
1158                 $$->title = $1;
1159                 $$->id = $3;
1160                 $$->x = $5;
1161                 $$->y = $7;
1162                 $$->width = $9;
1163                 $$->height = $11;
1164                 if($12)
1165                 {
1166                         $$->style = $12->style;
1167                         $$->gotstyle = TRUE;
1168
1169                         if ($12->exstyle)
1170                         {
1171                             $$->exstyle = $12->exstyle;
1172                             $$->gotexstyle = TRUE;
1173                         }
1174                         free($12);
1175                 }
1176
1177                 $$->extra = $14;
1178                 }
1179         ;
1180
1181 exctrl_desc
1182         : expr ',' expr ',' expr ',' expr ',' expr optional_style_pair helpid opt_data {
1183                 $$ = new_control();
1184                 $$->id = $1;
1185                 $$->x = $3;
1186                 $$->y = $5;
1187                 $$->width = $7;
1188                 $$->height = $9;
1189                 if($10)
1190                 {
1191                         $$->style = $10->style;
1192                         $$->gotstyle = TRUE;
1193
1194                         if ($10->exstyle)
1195                         {
1196                             $$->exstyle = $10->exstyle;
1197                             $$->gotexstyle = TRUE;
1198                         }
1199                         free($10);
1200                 }
1201                 $$->extra = $12;
1202                 }
1203         ;
1204
1205 opt_data: /* Empty */   { $$ = NULL; }
1206         | raw_data      { $$ = $1; }
1207         ;
1208
1209 helpid  : /* Empty */   { $$ = NULL; }
1210         | ',' expr      { $$ = new_int($2); }
1211         ;
1212
1213 opt_exfont
1214         : tFONT expr ',' tSTRING ',' expr ',' expr  opt_expr { $$ = new_font_id($2, $4, $6, $8); }
1215         ;
1216
1217 /*
1218  * FIXME: This odd expression is here to nullify an extra token found
1219  * in some appstudio produced resources which appear to do nothing.
1220  */
1221 opt_expr: /* Empty */   { $$ = NULL; }
1222         | ',' expr      { $$ = NULL; }
1223         ;
1224
1225 /* ------------------------------ Menu ------------------------------ */
1226 menu    : tMENU loadmemopts opt_lvc menu_body {
1227                 if(!$4)
1228                         yyerror("Menu must contain items");
1229                 $$ = new_menu();
1230                 if($2)
1231                 {
1232                         $$->memopt = *($2);
1233                         free($2);
1234                 }
1235                 else
1236                         $$->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1237                 $$->items = get_item_head($4);
1238                 if($3)
1239                 {
1240                         $$->lvc = *($3);
1241                         free($3);
1242                 }
1243                 if(!$$->lvc.language)
1244                         $$->lvc.language = dup_language(currentlanguage);
1245                 }
1246         ;
1247
1248 menu_body
1249         : tBEGIN item_definitions tEND  { $$ = $2; }
1250         ;
1251
1252 item_definitions
1253         : /* Empty */   {$$ = NULL;}
1254         | item_definitions tMENUITEM tSTRING opt_comma expr item_options {
1255                 $$=new_menu_item();
1256                 $$->prev = $1;
1257                 if($1)
1258                         $1->next = $$;
1259                 $$->id =  $5;
1260                 $$->state = $6;
1261                 $$->name = $3;
1262                 }
1263         | item_definitions tMENUITEM tSEPARATOR {
1264                 $$=new_menu_item();
1265                 $$->prev = $1;
1266                 if($1)
1267                         $1->next = $$;
1268                 }
1269         | item_definitions tPOPUP tSTRING item_options menu_body {
1270                 $$ = new_menu_item();
1271                 $$->prev = $1;
1272                 if($1)
1273                         $1->next = $$;
1274                 $$->popup = get_item_head($5);
1275                 $$->name = $3;
1276                 }
1277         ;
1278
1279 /* NOTE: item_options is right recursive because it would introduce
1280  * a shift/reduce conflict on ',' in itemex_options due to the
1281  * empty rule here. The parser is now forced to look beyond the ','
1282  * before reducing (force shift).
1283  * Right recursion here is not a problem because we cannot expect
1284  * more than 7 parserstack places to be occupied while parsing this
1285  * (who would want to specify a MF_x flag twice?).
1286  */
1287 item_options
1288         : /* Empty */                           { $$ = 0; }
1289         | opt_comma tCHECKED            item_options    { $$ = $3 | MF_CHECKED; }
1290         | opt_comma tGRAYED             item_options    { $$ = $3 | MF_GRAYED; }
1291         | opt_comma tHELP               item_options    { $$ = $3 | MF_HELP; }
1292         | opt_comma tINACTIVE           item_options    { $$ = $3 | MF_DISABLED; }
1293         | opt_comma tMENUBARBREAK       item_options    { $$ = $3 | MF_MENUBARBREAK; }
1294         | opt_comma tMENUBREAK  item_options    { $$ = $3 | MF_MENUBREAK; }
1295         ;
1296
1297 /* ------------------------------ MenuEx ------------------------------ */
1298 menuex  : tMENUEX loadmemopts opt_lvc menuex_body       {
1299                 if(!win32)
1300                         parser_warning("MENUEX not supported in 16-bit mode\n");
1301                 if(!$4)
1302                         yyerror("MenuEx must contain items");
1303                 $$ = new_menuex();
1304                 if($2)
1305                 {
1306                         $$->memopt = *($2);
1307                         free($2);
1308                 }
1309                 else
1310                         $$->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE | WRC_MO_DISCARDABLE;
1311                 $$->items = get_itemex_head($4);
1312                 if($3)
1313                 {
1314                         $$->lvc = *($3);
1315                         free($3);
1316                 }
1317                 if(!$$->lvc.language)
1318                         $$->lvc.language = dup_language(currentlanguage);
1319                 }
1320         ;
1321
1322 menuex_body
1323         : tBEGIN itemex_definitions tEND { $$ = $2; }
1324         ;
1325
1326 itemex_definitions
1327         : /* Empty */   {$$ = NULL; }
1328         | itemex_definitions tMENUITEM tSTRING itemex_options {
1329                 $$ = new_menuex_item();
1330                 $$->prev = $1;
1331                 if($1)
1332                         $1->next = $$;
1333                 $$->name = $3;
1334                 $$->id = $4->id;
1335                 $$->type = $4->type;
1336                 $$->state = $4->state;
1337                 $$->helpid = $4->helpid;
1338                 $$->gotid = $4->gotid;
1339                 $$->gottype = $4->gottype;
1340                 $$->gotstate = $4->gotstate;
1341                 $$->gothelpid = $4->gothelpid;
1342                 free($4);
1343                 }
1344         | itemex_definitions tMENUITEM tSEPARATOR {
1345                 $$ = new_menuex_item();
1346                 $$->prev = $1;
1347                 if($1)
1348                         $1->next = $$;
1349                 }
1350         | itemex_definitions tPOPUP tSTRING itemex_p_options menuex_body {
1351                 $$ = new_menuex_item();
1352                 $$->prev = $1;
1353                 if($1)
1354                         $1->next = $$;
1355                 $$->popup = get_itemex_head($5);
1356                 $$->name = $3;
1357                 $$->id = $4->id;
1358                 $$->type = $4->type;
1359                 $$->state = $4->state;
1360                 $$->helpid = $4->helpid;
1361                 $$->gotid = $4->gotid;
1362                 $$->gottype = $4->gottype;
1363                 $$->gotstate = $4->gotstate;
1364                 $$->gothelpid = $4->gothelpid;
1365                 free($4);
1366                 }
1367         ;
1368
1369 itemex_options
1370         : /* Empty */                   { $$ = new_itemex_opt(0, 0, 0, 0); }
1371         | ',' expr {
1372                 $$ = new_itemex_opt($2, 0, 0, 0);
1373                 $$->gotid = TRUE;
1374                 }
1375         | ',' e_expr ',' e_expr item_options {
1376                 $$ = new_itemex_opt($2 ? *($2) : 0, $4 ? *($4) : 0, $5, 0);
1377                 $$->gotid = TRUE;
1378                 $$->gottype = TRUE;
1379                 $$->gotstate = TRUE;
1380                 free($2);
1381                 free($4);
1382                 }
1383         | ',' e_expr ',' e_expr ',' expr {
1384                 $$ = new_itemex_opt($2 ? *($2) : 0, $4 ? *($4) : 0, $6, 0);
1385                 $$->gotid = TRUE;
1386                 $$->gottype = TRUE;
1387                 $$->gotstate = TRUE;
1388                 free($2);
1389                 free($4);
1390                 }
1391         ;
1392
1393 itemex_p_options
1394         : /* Empty */                   { $$ = new_itemex_opt(0, 0, 0, 0); }
1395         | ',' expr {
1396                 $$ = new_itemex_opt($2, 0, 0, 0);
1397                 $$->gotid = TRUE;
1398                 }
1399         | ',' e_expr ',' expr {
1400                 $$ = new_itemex_opt($2 ? *($2) : 0, $4, 0, 0);
1401                 free($2);
1402                 $$->gotid = TRUE;
1403                 $$->gottype = TRUE;
1404                 }
1405         | ',' e_expr ',' e_expr ',' expr {
1406                 $$ = new_itemex_opt($2 ? *($2) : 0, $4 ? *($4) : 0, $6, 0);
1407                 free($2);
1408                 free($4);
1409                 $$->gotid = TRUE;
1410                 $$->gottype = TRUE;
1411                 $$->gotstate = TRUE;
1412                 }
1413         | ',' e_expr ',' e_expr ',' e_expr ',' expr {
1414                 $$ = new_itemex_opt($2 ? *($2) : 0, $4 ? *($4) : 0, $6 ? *($6) : 0, $8);
1415                 free($2);
1416                 free($4);
1417                 free($6);
1418                 $$->gotid = TRUE;
1419                 $$->gottype = TRUE;
1420                 $$->gotstate = TRUE;
1421                 $$->gothelpid = TRUE;
1422                 }
1423         ;
1424
1425 /* ------------------------------ StringTable ------------------------------ */
1426 /* Stringtables are parsed differently than other resources because their
1427  * layout is substantially different from other resources.
1428  * The table is parsed through a _global_ variable 'tagstt' which holds the
1429  * current stringtable descriptor (stringtable_t *) and 'sttres' that holds a
1430  * list of stringtables of different languages.
1431  */
1432 stringtable
1433         : stt_head tBEGIN strings tEND {
1434                 if(!$3)
1435                 {
1436                         yyerror("Stringtable must have at least one entry");
1437                 }
1438                 else
1439                 {
1440                         stringtable_t *stt;
1441                         /* Check if we added to a language table or created
1442                          * a new one.
1443                          */
1444                          for(stt = sttres; stt; stt = stt->next)
1445                          {
1446                                 if(stt == tagstt)
1447                                         break;
1448                          }
1449                          if(!stt)
1450                          {
1451                                 /* It is a new one */
1452                                 if(sttres)
1453                                 {
1454                                         sttres->prev = tagstt;
1455                                         tagstt->next = sttres;
1456                                         sttres = tagstt;
1457                                 }
1458                                 else
1459                                         sttres = tagstt;
1460                          }
1461                          /* Else were done */
1462                 }
1463                 free(tagstt_memopt);
1464                 tagstt_memopt = NULL;
1465
1466                 $$ = tagstt;
1467                 }
1468         ;
1469
1470 /* This is to get the language of the currently parsed stringtable */
1471 stt_head: tSTRINGTABLE loadmemopts opt_lvc {
1472                 if((tagstt = find_stringtable($3)) == NULL)
1473                         tagstt = new_stringtable($3);
1474                 tagstt_memopt = $2;
1475                 tagstt_version = $3->version;
1476                 tagstt_characts = $3->characts;
1477                 free($3);
1478                 }
1479         ;
1480
1481 strings : /* Empty */   { $$ = NULL; }
1482         | strings expr opt_comma tSTRING {
1483                 int i;
1484                 assert(tagstt != NULL);
1485                 if($2 > 65535 || $2 < -32768)
1486                         yyerror("Stringtable entry's ID out of range (%d)", $2);
1487                 /* Search for the ID */
1488                 for(i = 0; i < tagstt->nentries; i++)
1489                 {
1490                         if(tagstt->entries[i].id == $2)
1491                                 yyerror("Stringtable ID %d already in use", $2);
1492                 }
1493                 /* If we get here, then we have a new unique entry */
1494                 tagstt->nentries++;
1495                 tagstt->entries = xrealloc(tagstt->entries, sizeof(tagstt->entries[0]) * tagstt->nentries);
1496                 tagstt->entries[tagstt->nentries-1].id = $2;
1497                 tagstt->entries[tagstt->nentries-1].str = $4;
1498                 if(tagstt_memopt)
1499                         tagstt->entries[tagstt->nentries-1].memopt = *tagstt_memopt;
1500                 else
1501                         tagstt->entries[tagstt->nentries-1].memopt = WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE | WRC_MO_PURE;
1502                 tagstt->entries[tagstt->nentries-1].version = tagstt_version;
1503                 tagstt->entries[tagstt->nentries-1].characts = tagstt_characts;
1504
1505                 if(pedantic && !$4->size)
1506                         parser_warning("Zero length strings make no sense\n");
1507                 if(!win32 && $4->size > 254)
1508                         yyerror("Stringtable entry more than 254 characters");
1509                 if(win32 && $4->size > 65534) /* Hmm..., does this happen? */
1510                         yyerror("Stringtable entry more than 65534 characters (probably something else that went wrong)");
1511                 $$ = tagstt;
1512                 }
1513         ;
1514
1515 opt_comma       /* There seem to be two ways to specify a stringtable... */
1516         : /* Empty */
1517         | ','
1518         ;
1519
1520 /* ------------------------------ VersionInfo ------------------------------ */
1521 versioninfo
1522         : tVERSIONINFO loadmemopts fix_version tBEGIN ver_blocks tEND {
1523                 $$ = $3;
1524                 if($2)
1525                 {
1526                         $$->memopt = *($2);
1527                         free($2);
1528                 }
1529                 else
1530                         $$->memopt = WRC_MO_MOVEABLE | (win32 ? WRC_MO_PURE : 0);
1531                 $$->blocks = get_ver_block_head($5);
1532                 /* Set language; there is no version or characteristics */
1533                 $$->lvc.language = dup_language(currentlanguage);
1534                 }
1535         ;
1536
1537 fix_version
1538         : /* Empty */                   { $$ = new_versioninfo(); }
1539         | fix_version tFILEVERSION expr ',' expr ',' expr ',' expr {
1540                 if($1->gotit.fv)
1541                         yyerror("FILEVERSION already defined");
1542                 $$ = $1;
1543                 $$->filever_maj1 = $3;
1544                 $$->filever_maj2 = $5;
1545                 $$->filever_min1 = $7;
1546                 $$->filever_min2 = $9;
1547                 $$->gotit.fv = 1;
1548                 }
1549         | fix_version tPRODUCTVERSION expr ',' expr ',' expr ',' expr {
1550                 if($1->gotit.pv)
1551                         yyerror("PRODUCTVERSION already defined");
1552                 $$ = $1;
1553                 $$->prodver_maj1 = $3;
1554                 $$->prodver_maj2 = $5;
1555                 $$->prodver_min1 = $7;
1556                 $$->prodver_min2 = $9;
1557                 $$->gotit.pv = 1;
1558                 }
1559         | fix_version tFILEFLAGS expr {
1560                 if($1->gotit.ff)
1561                         yyerror("FILEFLAGS already defined");
1562                 $$ = $1;
1563                 $$->fileflags = $3;
1564                 $$->gotit.ff = 1;
1565                 }
1566         | fix_version tFILEFLAGSMASK expr {
1567                 if($1->gotit.ffm)
1568                         yyerror("FILEFLAGSMASK already defined");
1569                 $$ = $1;
1570                 $$->fileflagsmask = $3;
1571                 $$->gotit.ffm = 1;
1572                 }
1573         | fix_version tFILEOS expr {
1574                 if($1->gotit.fo)
1575                         yyerror("FILEOS already defined");
1576                 $$ = $1;
1577                 $$->fileos = $3;
1578                 $$->gotit.fo = 1;
1579                 }
1580         | fix_version tFILETYPE expr {
1581                 if($1->gotit.ft)
1582                         yyerror("FILETYPE already defined");
1583                 $$ = $1;
1584                 $$->filetype = $3;
1585                 $$->gotit.ft = 1;
1586                 }
1587         | fix_version tFILESUBTYPE expr {
1588                 if($1->gotit.fst)
1589                         yyerror("FILESUBTYPE already defined");
1590                 $$ = $1;
1591                 $$->filesubtype = $3;
1592                 $$->gotit.fst = 1;
1593                 }
1594         ;
1595
1596 ver_blocks
1597         : /* Empty */                   { $$ = NULL; }
1598         | ver_blocks ver_block {
1599                 $$ = $2;
1600                 $$->prev = $1;
1601                 if($1)
1602                         $1->next = $$;
1603                 }
1604         ;
1605
1606 ver_block
1607         : tBLOCK tSTRING tBEGIN ver_values tEND {
1608                 $$ = new_ver_block();
1609                 $$->name = $2;
1610                 $$->values = get_ver_value_head($4);
1611                 }
1612         ;
1613
1614 ver_values
1615         : /* Empty */                   { $$ = NULL; }
1616         | ver_values ver_value {
1617                 $$ = $2;
1618                 $$->prev = $1;
1619                 if($1)
1620                         $1->next = $$;
1621                 }
1622         ;
1623
1624 ver_value
1625         : ver_block {
1626                 $$ = new_ver_value();
1627                 $$->type = val_block;
1628                 $$->value.block = $1;
1629                 }
1630         | tVALUE tSTRING ',' tSTRING {
1631                 $$ = new_ver_value();
1632                 $$->type = val_str;
1633                 $$->key = $2;
1634                 $$->value.str = $4;
1635                 }
1636         | tVALUE tSTRING ',' ver_words {
1637                 $$ = new_ver_value();
1638                 $$->type = val_words;
1639                 $$->key = $2;
1640                 $$->value.words = $4;
1641                 }
1642         ;
1643
1644 ver_words
1645         : expr                  { $$ = new_ver_words($1); }
1646         | ver_words ',' expr    { $$ = add_ver_words($1, $3); }
1647         ;
1648
1649 /* ------------------------------ Toolbar ------------------------------ */
1650 toolbar: tTOOLBAR loadmemopts expr ',' expr opt_lvc tBEGIN toolbar_items tEND {
1651                 int nitems;
1652                 toolbar_item_t *items = get_tlbr_buttons_head($8, &nitems);
1653                 $$ = new_toolbar($3, $5, items, nitems);
1654                 if($2)
1655                 {
1656                         $$->memopt = *($2);
1657                         free($2);
1658                 }
1659                 else
1660                 {
1661                         $$->memopt = WRC_MO_MOVEABLE | WRC_MO_PURE;
1662                 }
1663                 if($6)
1664                 {
1665                         $$->lvc = *($6);
1666                         free($6);
1667                 }
1668                 if(!$$->lvc.language)
1669                 {
1670                         $$->lvc.language = dup_language(currentlanguage);
1671                 }
1672                 }
1673         ;
1674
1675 toolbar_items
1676         :  /* Empty */                  { $$ = NULL; }
1677         | toolbar_items tBUTTON expr    {
1678                 toolbar_item_t *idrec = new_toolbar_item();
1679                 idrec->id = $3;
1680                 $$ = ins_tlbr_button($1, idrec);
1681                 }
1682         | toolbar_items tSEPARATOR      {
1683                 toolbar_item_t *idrec = new_toolbar_item();
1684                 idrec->id = 0;
1685                 $$ = ins_tlbr_button($1, idrec);
1686         }
1687         ;
1688
1689 /* ------------------------------ Memory options ------------------------------ */
1690 loadmemopts
1691         : /* Empty */           { $$ = NULL; }
1692         | loadmemopts lamo {
1693                 if($1)
1694                 {
1695                         *($1) |= *($2);
1696                         $$ = $1;
1697                         free($2);
1698                 }
1699                 else
1700                         $$ = $2;
1701                 }
1702         | loadmemopts lama {
1703                 if($1)
1704                 {
1705                         *($1) &= *($2);
1706                         $$ = $1;
1707                         free($2);
1708                 }
1709                 else
1710                 {
1711                         *$2 &= WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE | WRC_MO_PURE;
1712                         $$ = $2;
1713                 }
1714                 }
1715         ;
1716
1717 lamo    : tPRELOAD      { $$ = new_int(WRC_MO_PRELOAD); }
1718         | tMOVEABLE     { $$ = new_int(WRC_MO_MOVEABLE); }
1719         | tDISCARDABLE  { $$ = new_int(WRC_MO_DISCARDABLE); }
1720         | tPURE         { $$ = new_int(WRC_MO_PURE); }
1721         ;
1722
1723 lama    : tLOADONCALL   { $$ = new_int(~WRC_MO_PRELOAD); }
1724         | tFIXED        { $$ = new_int(~WRC_MO_MOVEABLE); }
1725         | tIMPURE       { $$ = new_int(~WRC_MO_PURE); }
1726         ;
1727
1728 /* ------------------------------ Win32 options ------------------------------ */
1729 opt_lvc : /* Empty */           { $$ = new_lvc(); }
1730         | opt_lvc opt_language {
1731                 if(!win32)
1732                         parser_warning("LANGUAGE not supported in 16-bit mode\n");
1733                 if($1->language)
1734                         yyerror("Language already defined");
1735                 $$ = $1;
1736                 $1->language = $2;
1737                 }
1738         | opt_lvc opt_characts {
1739                 if(!win32)
1740                         parser_warning("CHARACTERISTICS not supported in 16-bit mode\n");
1741                 if($1->characts)
1742                         yyerror("Characteristics already defined");
1743                 $$ = $1;
1744                 $1->characts = $2;
1745                 }
1746         | opt_lvc opt_version {
1747                 if(!win32)
1748                         parser_warning("VERSION not supported in 16-bit mode\n");
1749                 if($1->version)
1750                         yyerror("Version already defined");
1751                 $$ = $1;
1752                 $1->version = $2;
1753                 }
1754         ;
1755
1756         /*
1757          * This here is another s/r conflict on {bi,u}nary + and -.
1758          * It is due to the look-ahead which must determine when the
1759          * rule opt_language ends. It could be solved with adding a
1760          * tNL at the end, but that seems unreasonable to do.
1761          * The conflict is now moved to the expression handling below.
1762          */
1763 opt_language
1764         : tLANGUAGE expr ',' expr       { $$ = new_language($2, $4);
1765                                           if (get_language_codepage($2, $4) == -1)
1766                                                 yyerror( "Language %04x is not supported", ($4<<10) + $2);
1767                                         }
1768         ;
1769
1770 opt_characts
1771         : tCHARACTERISTICS expr         { $$ = new_characts($2); }
1772         ;
1773
1774 opt_version
1775         : tVERSION expr                 { $$ = new_version($2); }
1776         ;
1777
1778 /* ------------------------------ Raw data handling ------------------------------ */
1779 raw_data: opt_lvc tBEGIN raw_elements tEND {
1780                 if($1)
1781                 {
1782                         $3->lvc = *($1);
1783                         free($1);
1784                 }
1785
1786                 if(!$3->lvc.language)
1787                         $3->lvc.language = dup_language(currentlanguage);
1788
1789                 $$ = $3;
1790                 }
1791         ;
1792
1793 raw_elements
1794         : tRAWDATA                      { $$ = $1; }
1795         | tNUMBER                       { $$ = int2raw_data($1); }
1796         | '-' tNUMBER                   { $$ = int2raw_data(-($2)); }
1797         | tLNUMBER                      { $$ = long2raw_data($1); }
1798         | '-' tLNUMBER                  { $$ = long2raw_data(-($2)); }
1799         | tSTRING                       { $$ = str2raw_data($1); }
1800         | raw_elements opt_comma tRAWDATA { $$ = merge_raw_data($1, $3); free($3->data); free($3); }
1801         | raw_elements opt_comma tNUMBER  { $$ = merge_raw_data_int($1, $3); }
1802         | raw_elements opt_comma '-' tNUMBER  { $$ = merge_raw_data_int($1, -($4)); }
1803         | raw_elements opt_comma tLNUMBER { $$ = merge_raw_data_long($1, $3); }
1804         | raw_elements opt_comma '-' tLNUMBER { $$ = merge_raw_data_long($1, -($4)); }
1805         | raw_elements opt_comma tSTRING  { $$ = merge_raw_data_str($1, $3); }
1806         ;
1807
1808 /* File data or raw data */
1809 file_raw: filename      { $$ = load_file($1,dup_language(currentlanguage)); }
1810         | raw_data      { $$ = $1; }
1811         ;
1812
1813 /* ------------------------------ Win32 expressions ------------------------------ */
1814 /* All win16 numbers are also handled here. This is inconsistent with MS'
1815  * resource compiler, but what the heck, its just handy to have.
1816  */
1817 e_expr  : /* Empty */   { $$ = 0; }
1818         | expr          { $$ = new_int($1); }
1819         ;
1820
1821 /* This rule moves ALL s/r conflicts on {bi,u}nary - and + to here */
1822 expr    : xpr   { $$ = ($1); }
1823         ;
1824
1825 xpr     : xpr '+' xpr   { $$ = ($1) + ($3); }
1826         | xpr '-' xpr   { $$ = ($1) - ($3); }
1827         | xpr '|' xpr   { $$ = ($1) | ($3); }
1828         | xpr '&' xpr   { $$ = ($1) & ($3); }
1829         | xpr '*' xpr   { $$ = ($1) * ($3); }
1830         | xpr '/' xpr   { $$ = ($1) / ($3); }
1831         | xpr '^' xpr   { $$ = ($1) ^ ($3); }
1832         | '~' xpr       { $$ = ~($2); }
1833         | '-' xpr %prec pUPM    { $$ = -($2); }
1834         | '+' xpr %prec pUPM    { $$ = $2; }
1835         | '(' xpr ')'   { $$ = $2; }
1836         | any_num       { $$ = $1; }
1837         | tNOT any_num  { $$ = ~($2); }
1838         ;
1839
1840 any_num : tNUMBER       { $$ = $1; }
1841         | tLNUMBER      { $$ = $1; }
1842         ;
1843
1844 %%
1845 /* Dialog specific functions */
1846 static dialog_t *dialog_style(style_t * st, dialog_t *dlg)
1847 {
1848         assert(dlg != NULL);
1849         if(dlg->style == NULL)
1850         {
1851                 dlg->style = new_style(0,0);
1852         }
1853
1854         if(dlg->gotstyle)
1855         {
1856                 parser_warning("Style already defined, or-ing together\n");
1857         }
1858         else
1859         {
1860                 dlg->style->or_mask = 0;
1861                 dlg->style->and_mask = 0;
1862         }
1863         dlg->style->or_mask |= st->or_mask;
1864         dlg->style->and_mask |= st->and_mask;
1865         dlg->gotstyle = TRUE;
1866         free(st);
1867         return dlg;
1868 }
1869
1870 static dialog_t *dialog_exstyle(style_t *st, dialog_t *dlg)
1871 {
1872         assert(dlg != NULL);
1873         if(dlg->exstyle == NULL)
1874         {
1875                 dlg->exstyle = new_style(0,0);
1876         }
1877
1878         if(dlg->gotexstyle)
1879         {
1880                 parser_warning("ExStyle already defined, or-ing together\n");
1881         }
1882         else
1883         {
1884                 dlg->exstyle->or_mask = 0;
1885                 dlg->exstyle->and_mask = 0;
1886         }
1887         dlg->exstyle->or_mask |= st->or_mask;
1888         dlg->exstyle->and_mask |= st->and_mask;
1889         dlg->gotexstyle = TRUE;
1890         free(st);
1891         return dlg;
1892 }
1893
1894 static dialog_t *dialog_caption(string_t *s, dialog_t *dlg)
1895 {
1896         assert(dlg != NULL);
1897         if(dlg->title)
1898                 yyerror("Caption already defined");
1899         dlg->title = s;
1900         return dlg;
1901 }
1902
1903 static dialog_t *dialog_font(font_id_t *f, dialog_t *dlg)
1904 {
1905         assert(dlg != NULL);
1906         if(dlg->font)
1907                 yyerror("Font already defined");
1908         dlg->font = f;
1909         return dlg;
1910 }
1911
1912 static dialog_t *dialog_class(name_id_t *n, dialog_t *dlg)
1913 {
1914         assert(dlg != NULL);
1915         if(dlg->dlgclass)
1916                 yyerror("Class already defined");
1917         dlg->dlgclass = n;
1918         return dlg;
1919 }
1920
1921 static dialog_t *dialog_menu(name_id_t *m, dialog_t *dlg)
1922 {
1923         assert(dlg != NULL);
1924         if(dlg->menu)
1925                 yyerror("Menu already defined");
1926         dlg->menu = m;
1927         return dlg;
1928 }
1929
1930 static dialog_t *dialog_language(language_t *l, dialog_t *dlg)
1931 {
1932         assert(dlg != NULL);
1933         if(dlg->lvc.language)
1934                 yyerror("Language already defined");
1935         dlg->lvc.language = l;
1936         return dlg;
1937 }
1938
1939 static dialog_t *dialog_characteristics(characts_t *c, dialog_t *dlg)
1940 {
1941         assert(dlg != NULL);
1942         if(dlg->lvc.characts)
1943                 yyerror("Characteristics already defined");
1944         dlg->lvc.characts = c;
1945         return dlg;
1946 }
1947
1948 static dialog_t *dialog_version(version_t *v, dialog_t *dlg)
1949 {
1950         assert(dlg != NULL);
1951         if(dlg->lvc.version)
1952                 yyerror("Version already defined");
1953         dlg->lvc.version = v;
1954         return dlg;
1955 }
1956
1957 /* Controls specific functions */
1958 static control_t *ins_ctrl(int type, int special_style, control_t *ctrl, control_t *prev)
1959 {
1960         /* Hm... this seems to be jammed in at all time... */
1961         int defaultstyle = WS_CHILD | WS_VISIBLE;
1962
1963         assert(ctrl != NULL);
1964         ctrl->prev = prev;
1965
1966         if(prev)
1967                 prev->next = ctrl;
1968
1969         /* Check for duplicate identifiers */
1970         while (prev)
1971         {
1972                 if (ctrl->id != -1 && ctrl->id == prev->id)
1973                         parser_warning("Duplicate dialog control id %d\n", ctrl->id);
1974                 prev = prev->prev;
1975         }
1976
1977         if(type != -1)
1978         {
1979                 ctrl->ctlclass = new_name_id();
1980                 ctrl->ctlclass->type = name_ord;
1981                 ctrl->ctlclass->name.i_name = type;
1982         }
1983
1984         switch(type)
1985         {
1986         case CT_BUTTON:
1987                 if(special_style != BS_GROUPBOX && special_style != BS_RADIOBUTTON)
1988                         defaultstyle |= WS_TABSTOP;
1989                 break;
1990         case CT_EDIT:
1991                 defaultstyle |= WS_TABSTOP | WS_BORDER;
1992                 break;
1993         case CT_LISTBOX:
1994                 defaultstyle |= LBS_NOTIFY | WS_BORDER;
1995                 break;
1996         case CT_COMBOBOX:
1997                 if (!(ctrl->style->or_mask & (CBS_SIMPLE | CBS_DROPDOWN | CBS_DROPDOWNLIST)))
1998                     defaultstyle |= CBS_SIMPLE;
1999                 break;
2000         case CT_STATIC:
2001                 if(special_style == SS_CENTER || special_style == SS_LEFT || special_style == SS_RIGHT)
2002                         defaultstyle |= WS_GROUP;
2003                 break;
2004         }
2005
2006         if(!ctrl->gotstyle)     /* Handle default style setting */
2007         {
2008                 switch(type)
2009                 {
2010                 case CT_EDIT:
2011                         defaultstyle |= ES_LEFT;
2012                         break;
2013                 case CT_LISTBOX:
2014                         defaultstyle |= LBS_NOTIFY;
2015                         break;
2016                 case CT_COMBOBOX:
2017                         defaultstyle |= CBS_SIMPLE | WS_TABSTOP;
2018                         break;
2019                 case CT_SCROLLBAR:
2020                         defaultstyle |= SBS_HORZ;
2021                         break;
2022                 case CT_BUTTON:
2023                         switch(special_style)
2024                         {
2025                         case BS_CHECKBOX:
2026                         case BS_DEFPUSHBUTTON:
2027                         case BS_PUSHBUTTON:
2028 /*                      case BS_PUSHBOX:        */
2029                         case BS_AUTORADIOBUTTON:
2030                         case BS_AUTO3STATE:
2031                         case BS_3STATE:
2032                         case BS_AUTOCHECKBOX:
2033                                 defaultstyle |= WS_TABSTOP;
2034                                 break;
2035                         default:
2036                                 parser_warning("Unknown default button control-style 0x%08x\n", special_style);
2037                         case BS_GROUPBOX:
2038                         case BS_RADIOBUTTON:
2039                                 break;
2040                         }
2041                         break;
2042
2043                 case CT_STATIC:
2044                         switch(special_style)
2045                         {
2046                         case SS_LEFT:
2047                         case SS_RIGHT:
2048                         case SS_CENTER:
2049                                 defaultstyle |= WS_GROUP;
2050                                 break;
2051                         case SS_ICON:   /* Special case */
2052                                 break;
2053                         default:
2054                                 parser_warning("Unknown default static control-style 0x%08x\n", special_style);
2055                                 break;
2056                         }
2057                         break;
2058                 case -1:        /* Generic control */
2059                         goto byebye;
2060
2061                 default:
2062                         yyerror("Internal error (report this): Got weird control type 0x%08x", type);
2063                 }
2064         }
2065
2066         /* The SS_ICON flag is always forced in for icon controls */
2067         if(type == CT_STATIC && special_style == SS_ICON)
2068                 defaultstyle |= SS_ICON;
2069
2070         if (!ctrl->gotstyle)
2071                 ctrl->style = new_style(0,0);
2072
2073         /* combine all styles */
2074         ctrl->style->or_mask = ctrl->style->or_mask | defaultstyle | special_style;
2075         ctrl->gotstyle = TRUE;
2076 byebye:
2077         /* combine with NOT mask */
2078         if (ctrl->gotstyle)
2079         {
2080                 ctrl->style->or_mask &= ~(ctrl->style->and_mask);
2081                 ctrl->style->and_mask = 0;
2082         }
2083         if (ctrl->gotexstyle)
2084         {
2085                 ctrl->exstyle->or_mask &= ~(ctrl->exstyle->and_mask);
2086                 ctrl->exstyle->and_mask = 0;
2087         }
2088         return ctrl;
2089 }
2090
2091 static int get_class_idW(const WCHAR *cc)
2092 {
2093         static const WCHAR szBUTTON[]    = {'B','U','T','T','O','N',0};
2094         static const WCHAR szCOMBOBOX[]  = {'C','O','M','B','O','B','O','X',0};
2095         static const WCHAR szLISTBOX[]   = {'L','I','S','T','B','O','X',0};
2096         static const WCHAR szEDIT[]      = {'E','D','I','T',0};
2097         static const WCHAR szSTATIC[]    = {'S','T','A','T','I','C',0};
2098         static const WCHAR szSCROLLBAR[] = {'S','C','R','O','L','L','B','A','R',0};
2099
2100         if(!strcmpiW(szBUTTON, cc))
2101                 return CT_BUTTON;
2102         if(!strcmpiW(szCOMBOBOX, cc))
2103                 return CT_COMBOBOX;
2104         if(!strcmpiW(szLISTBOX, cc))
2105                 return CT_LISTBOX;
2106         if(!strcmpiW(szEDIT, cc))
2107                 return CT_EDIT;
2108         if(!strcmpiW(szSTATIC, cc))
2109                 return CT_STATIC;
2110         if(!strcmpiW(szSCROLLBAR, cc))
2111                 return CT_SCROLLBAR;
2112
2113         return -1;
2114 }
2115
2116 static int get_class_idA(const char *cc)
2117 {
2118         if(!strcasecmp("BUTTON", cc))
2119                 return CT_BUTTON;
2120         if(!strcasecmp("COMBOBOX", cc))
2121                 return CT_COMBOBOX;
2122         if(!strcasecmp("LISTBOX", cc))
2123                 return CT_LISTBOX;
2124         if(!strcasecmp("EDIT", cc))
2125                 return CT_EDIT;
2126         if(!strcasecmp("STATIC", cc))
2127                 return CT_STATIC;
2128         if(!strcasecmp("SCROLLBAR", cc))
2129                 return CT_SCROLLBAR;
2130
2131         return -1;
2132 }
2133
2134
2135 static name_id_t *convert_ctlclass(name_id_t *cls)
2136 {
2137         int iclass;
2138
2139         if(cls->type == name_ord)
2140                 return cls;
2141         assert(cls->type == name_str);
2142         if(cls->name.s_name->type == str_unicode)
2143                 iclass = get_class_idW(cls->name.s_name->str.wstr);
2144         else
2145                 iclass = get_class_idA(cls->name.s_name->str.cstr);
2146
2147         if (iclass == -1)
2148                 return cls;     /* No default, return user controlclass */
2149
2150         free(cls->name.s_name->str.cstr);
2151         free(cls->name.s_name);
2152         cls->type = name_ord;
2153         cls->name.i_name = iclass;
2154         return cls;
2155 }
2156
2157 /* DialogEx specific functions */
2158 static dialogex_t *dialogex_style(style_t * st, dialogex_t *dlg)
2159 {
2160         assert(dlg != NULL);
2161         if(dlg->style == NULL)
2162         {
2163                 dlg->style = new_style(0,0);
2164         }
2165
2166         if(dlg->gotstyle)
2167         {
2168                 parser_warning("Style already defined, or-ing together\n");
2169         }
2170         else
2171         {
2172                 dlg->style->or_mask = 0;
2173                 dlg->style->and_mask = 0;
2174         }
2175         dlg->style->or_mask |= st->or_mask;
2176         dlg->style->and_mask |= st->and_mask;
2177         dlg->gotstyle = TRUE;
2178         free(st);
2179         return dlg;
2180 }
2181
2182 static dialogex_t *dialogex_exstyle(style_t * st, dialogex_t *dlg)
2183 {
2184         assert(dlg != NULL);
2185         if(dlg->exstyle == NULL)
2186         {
2187                 dlg->exstyle = new_style(0,0);
2188         }
2189
2190         if(dlg->gotexstyle)
2191         {
2192                 parser_warning("ExStyle already defined, or-ing together\n");
2193         }
2194         else
2195         {
2196                 dlg->exstyle->or_mask = 0;
2197                 dlg->exstyle->and_mask = 0;
2198         }
2199         dlg->exstyle->or_mask |= st->or_mask;
2200         dlg->exstyle->and_mask |= st->and_mask;
2201         dlg->gotexstyle = TRUE;
2202         free(st);
2203         return dlg;
2204 }
2205
2206 static dialogex_t *dialogex_caption(string_t *s, dialogex_t *dlg)
2207 {
2208         assert(dlg != NULL);
2209         if(dlg->title)
2210                 yyerror("Caption already defined");
2211         dlg->title = s;
2212         return dlg;
2213 }
2214
2215 static dialogex_t *dialogex_font(font_id_t *f, dialogex_t *dlg)
2216 {
2217         assert(dlg != NULL);
2218         if(dlg->font)
2219                 yyerror("Font already defined");
2220         dlg->font = f;
2221         return dlg;
2222 }
2223
2224 static dialogex_t *dialogex_class(name_id_t *n, dialogex_t *dlg)
2225 {
2226         assert(dlg != NULL);
2227         if(dlg->dlgclass)
2228                 yyerror("Class already defined");
2229         dlg->dlgclass = n;
2230         return dlg;
2231 }
2232
2233 static dialogex_t *dialogex_menu(name_id_t *m, dialogex_t *dlg)
2234 {
2235         assert(dlg != NULL);
2236         if(dlg->menu)
2237                 yyerror("Menu already defined");
2238         dlg->menu = m;
2239         return dlg;
2240 }
2241
2242 static dialogex_t *dialogex_language(language_t *l, dialogex_t *dlg)
2243 {
2244         assert(dlg != NULL);
2245         if(dlg->lvc.language)
2246                 yyerror("Language already defined");
2247         dlg->lvc.language = l;
2248         return dlg;
2249 }
2250
2251 static dialogex_t *dialogex_characteristics(characts_t *c, dialogex_t *dlg)
2252 {
2253         assert(dlg != NULL);
2254         if(dlg->lvc.characts)
2255                 yyerror("Characteristics already defined");
2256         dlg->lvc.characts = c;
2257         return dlg;
2258 }
2259
2260 static dialogex_t *dialogex_version(version_t *v, dialogex_t *dlg)
2261 {
2262         assert(dlg != NULL);
2263         if(dlg->lvc.version)
2264                 yyerror("Version already defined");
2265         dlg->lvc.version = v;
2266         return dlg;
2267 }
2268
2269 /* Accelerator specific functions */
2270 static event_t *add_event(int key, int id, int flags, event_t *prev)
2271 {
2272         event_t *ev = new_event();
2273
2274         if((flags & (WRC_AF_VIRTKEY | WRC_AF_ASCII)) == (WRC_AF_VIRTKEY | WRC_AF_ASCII))
2275                 yyerror("Cannot use both ASCII and VIRTKEY");
2276
2277         ev->key = key;
2278         ev->id = id;
2279         ev->flags = flags & ~WRC_AF_ASCII;
2280         ev->prev = prev;
2281         if(prev)
2282                 prev->next = ev;
2283         return ev;
2284 }
2285
2286 static event_t *add_string_event(string_t *key, int id, int flags, event_t *prev)
2287 {
2288     int keycode = 0;
2289     event_t *ev = new_event();
2290
2291     if(key->type == str_char)
2292     {
2293         if((flags & WRC_AF_VIRTKEY) && (!isupper(key->str.cstr[0] & 0xff) && !isdigit(key->str.cstr[0] & 0xff)))
2294                 yyerror("VIRTKEY code is not equal to ascii value");
2295
2296         if(key->str.cstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
2297         {
2298                 yyerror("Cannot use both '^' and CONTROL modifier");
2299         }
2300         else if(key->str.cstr[0] == '^')
2301         {
2302                 keycode = toupper(key->str.cstr[1]) - '@';
2303                 if(keycode >= ' ')
2304                         yyerror("Control-code out of range");
2305         }
2306         else
2307                 keycode = key->str.cstr[0];
2308     }
2309     else
2310     {
2311         if((flags & WRC_AF_VIRTKEY) && !isupperW(key->str.wstr[0]) && !isdigitW(key->str.wstr[0]))
2312                 yyerror("VIRTKEY code is not equal to ascii value");
2313
2314         if(key->str.wstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
2315         {
2316                 yyerror("Cannot use both '^' and CONTROL modifier");
2317         }
2318         else if(key->str.wstr[0] == '^')
2319         {
2320                 keycode = toupperW(key->str.wstr[1]) - '@';
2321                 if(keycode >= ' ')
2322                         yyerror("Control-code out of range");
2323         }
2324         else
2325                 keycode = key->str.wstr[0];
2326     }
2327
2328     ev->key = keycode;
2329     ev->id = id;
2330     ev->flags = flags & ~WRC_AF_ASCII;
2331     ev->prev = prev;
2332     if(prev)
2333         prev->next = ev;
2334     return ev;
2335 }
2336
2337 /* MenuEx specific functions */
2338 static itemex_opt_t *new_itemex_opt(int id, int type, int state, int helpid)
2339 {
2340         itemex_opt_t *opt = xmalloc(sizeof(itemex_opt_t));
2341         memset( opt, 0, sizeof(*opt) );
2342         opt->id = id;
2343         opt->type = type;
2344         opt->state = state;
2345         opt->helpid = helpid;
2346         return opt;
2347 }
2348
2349 /* Raw data functions */
2350 static raw_data_t *load_file(string_t *filename, language_t *lang)
2351 {
2352         FILE *fp = NULL;
2353         char *path;
2354         raw_data_t *rd;
2355         string_t *name;
2356         int codepage = get_language_codepage(lang->id, lang->sub);
2357
2358         /* FIXME: we may want to use utf-8 here */
2359         if (codepage <= 0 && filename->type != str_char)
2360                 yyerror("Cannot convert filename to ASCII string");
2361         name = convert_string( filename, str_char, codepage );
2362         if (!(path = wpp_find_include(name->str.cstr, input_name)))
2363                 yyerror("Cannot open file %s", name->str.cstr);
2364         if (!(fp = fopen( path, "rb" )))
2365                 yyerror("Cannot open file %s", name->str.cstr);
2366         free( path );
2367         rd = new_raw_data();
2368         fseek(fp, 0, SEEK_END);
2369         rd->size = ftell(fp);
2370         fseek(fp, 0, SEEK_SET);
2371         if (rd->size)
2372         {
2373                 rd->data = xmalloc(rd->size);
2374                 fread(rd->data, rd->size, 1, fp);
2375         }
2376         else rd->data = NULL;
2377         fclose(fp);
2378         rd->lvc.language = lang;
2379         free_string(name);
2380         return rd;
2381 }
2382
2383 static raw_data_t *int2raw_data(int i)
2384 {
2385         raw_data_t *rd;
2386
2387         if( ( i >= 0 && (int)((unsigned short)i) != i) ||
2388             ( i < 0  && (int)((short)i) != i) )
2389                 parser_warning("Integer constant out of 16bit range (%d), truncated to %d\n", i, (short)i);
2390
2391         rd = new_raw_data();
2392         rd->size = sizeof(short);
2393         rd->data = xmalloc(rd->size);
2394         switch(byteorder)
2395         {
2396 #ifdef WORDS_BIGENDIAN
2397         default:
2398 #endif
2399         case WRC_BO_BIG:
2400                 rd->data[0] = HIBYTE(i);
2401                 rd->data[1] = LOBYTE(i);
2402                 break;
2403
2404 #ifndef WORDS_BIGENDIAN
2405         default:
2406 #endif
2407         case WRC_BO_LITTLE:
2408                 rd->data[1] = HIBYTE(i);
2409                 rd->data[0] = LOBYTE(i);
2410                 break;
2411         }
2412         return rd;
2413 }
2414
2415 static raw_data_t *long2raw_data(int i)
2416 {
2417         raw_data_t *rd;
2418         rd = new_raw_data();
2419         rd->size = sizeof(int);
2420         rd->data = xmalloc(rd->size);
2421         switch(byteorder)
2422         {
2423 #ifdef WORDS_BIGENDIAN
2424         default:
2425 #endif
2426         case WRC_BO_BIG:
2427                 rd->data[0] = HIBYTE(HIWORD(i));
2428                 rd->data[1] = LOBYTE(HIWORD(i));
2429                 rd->data[2] = HIBYTE(LOWORD(i));
2430                 rd->data[3] = LOBYTE(LOWORD(i));
2431                 break;
2432
2433 #ifndef WORDS_BIGENDIAN
2434         default:
2435 #endif
2436         case WRC_BO_LITTLE:
2437                 rd->data[3] = HIBYTE(HIWORD(i));
2438                 rd->data[2] = LOBYTE(HIWORD(i));
2439                 rd->data[1] = HIBYTE(LOWORD(i));
2440                 rd->data[0] = LOBYTE(LOWORD(i));
2441                 break;
2442         }
2443         return rd;
2444 }
2445
2446 static raw_data_t *str2raw_data(string_t *str)
2447 {
2448         raw_data_t *rd;
2449         rd = new_raw_data();
2450         rd->size = str->size * (str->type == str_char ? 1 : 2);
2451         rd->data = xmalloc(rd->size);
2452         if(str->type == str_char)
2453                 memcpy(rd->data, str->str.cstr, rd->size);
2454         else if(str->type == str_unicode)
2455         {
2456                 int i;
2457                 switch(byteorder)
2458                 {
2459 #ifdef WORDS_BIGENDIAN
2460                 default:
2461 #endif
2462                 case WRC_BO_BIG:
2463                         for(i = 0; i < str->size; i++)
2464                         {
2465                                 rd->data[2*i + 0] = HIBYTE((WORD)str->str.wstr[i]);
2466                                 rd->data[2*i + 1] = LOBYTE((WORD)str->str.wstr[i]);
2467                         }
2468                         break;
2469 #ifndef WORDS_BIGENDIAN
2470                 default:
2471 #endif
2472                 case WRC_BO_LITTLE:
2473                         for(i = 0; i < str->size; i++)
2474                         {
2475                                 rd->data[2*i + 1] = HIBYTE((WORD)str->str.wstr[i]);
2476                                 rd->data[2*i + 0] = LOBYTE((WORD)str->str.wstr[i]);
2477                         }
2478                         break;
2479                 }
2480         }
2481         else
2482                 internal_error(__FILE__, __LINE__, "Invalid stringtype\n");
2483         return rd;
2484 }
2485
2486 static raw_data_t *merge_raw_data(raw_data_t *r1, raw_data_t *r2)
2487 {
2488         r1->data = xrealloc(r1->data, r1->size + r2->size);
2489         memcpy(r1->data + r1->size, r2->data, r2->size);
2490         r1->size += r2->size;
2491         return r1;
2492 }
2493
2494 static raw_data_t *merge_raw_data_int(raw_data_t *r1, int i)
2495 {
2496         raw_data_t *t = int2raw_data(i);
2497         merge_raw_data(r1, t);
2498         free(t->data);
2499         free(t);
2500         return r1;
2501 }
2502
2503 static raw_data_t *merge_raw_data_long(raw_data_t *r1, int i)
2504 {
2505         raw_data_t *t = long2raw_data(i);
2506         merge_raw_data(r1, t);
2507         free(t->data);
2508         free(t);
2509         return r1;
2510 }
2511
2512 static raw_data_t *merge_raw_data_str(raw_data_t *r1, string_t *str)
2513 {
2514         raw_data_t *t = str2raw_data(str);
2515         merge_raw_data(r1, t);
2516         free(t->data);
2517         free(t);
2518         return r1;
2519 }
2520
2521 /* Function the go back in a list to get the head */
2522 static menu_item_t *get_item_head(menu_item_t *p)
2523 {
2524         if(!p)
2525                 return NULL;
2526         while(p->prev)
2527                 p = p->prev;
2528         return p;
2529 }
2530
2531 static menuex_item_t *get_itemex_head(menuex_item_t *p)
2532 {
2533         if(!p)
2534                 return NULL;
2535         while(p->prev)
2536                 p = p->prev;
2537         return p;
2538 }
2539
2540 static resource_t *get_resource_head(resource_t *p)
2541 {
2542         if(!p)
2543                 return NULL;
2544         while(p->prev)
2545                 p = p->prev;
2546         return p;
2547 }
2548
2549 static ver_block_t *get_ver_block_head(ver_block_t *p)
2550 {
2551         if(!p)
2552                 return NULL;
2553         while(p->prev)
2554                 p = p->prev;
2555         return p;
2556 }
2557
2558 static ver_value_t *get_ver_value_head(ver_value_t *p)
2559 {
2560         if(!p)
2561                 return NULL;
2562         while(p->prev)
2563                 p = p->prev;
2564         return p;
2565 }
2566
2567 static control_t *get_control_head(control_t *p)
2568 {
2569         if(!p)
2570                 return NULL;
2571         while(p->prev)
2572                 p = p->prev;
2573         return p;
2574 }
2575
2576 static event_t *get_event_head(event_t *p)
2577 {
2578         if(!p)
2579                 return NULL;
2580         while(p->prev)
2581                 p = p->prev;
2582         return p;
2583 }
2584
2585 /* Find a stringtable with given language */
2586 static stringtable_t *find_stringtable(lvc_t *lvc)
2587 {
2588         stringtable_t *stt;
2589
2590         assert(lvc != NULL);
2591
2592         if(!lvc->language)
2593                 lvc->language = dup_language(currentlanguage);
2594
2595         for(stt = sttres; stt; stt = stt->next)
2596         {
2597                 if(stt->lvc.language->id == lvc->language->id
2598                 && stt->lvc.language->sub == lvc->language->sub)
2599                 {
2600                         /* Found a table with the same language */
2601                         /* The version and characteristics are now handled
2602                          * in the generation of the individual stringtables.
2603                          * This enables localized analysis.
2604                         if((stt->lvc.version && lvc->version && *(stt->lvc.version) != *(lvc->version))
2605                         || (!stt->lvc.version && lvc->version)
2606                         || (stt->lvc.version && !lvc->version))
2607                                 parser_warning("Stringtable's versions are not the same, using first definition\n");
2608
2609                         if((stt->lvc.characts && lvc->characts && *(stt->lvc.characts) != *(lvc->characts))
2610                         || (!stt->lvc.characts && lvc->characts)
2611                         || (stt->lvc.characts && !lvc->characts))
2612                                 parser_warning("Stringtable's characteristics are not the same, using first definition\n");
2613                         */
2614                         return stt;
2615                 }
2616         }
2617         return NULL;
2618 }
2619
2620 /* qsort sorting function for string table entries */
2621 #define STE(p)  ((const stt_entry_t *)(p))
2622 static int sort_stt_entry(const void *e1, const void *e2)
2623 {
2624         return STE(e1)->id - STE(e2)->id;
2625 }
2626 #undef STE
2627
2628 static resource_t *build_stt_resources(stringtable_t *stthead)
2629 {
2630         stringtable_t *stt;
2631         stringtable_t *newstt;
2632         resource_t *rsc;
2633         resource_t *rsclist = NULL;
2634         resource_t *rsctail = NULL;
2635         int i;
2636         int j;
2637         DWORD andsum;
2638         DWORD orsum;
2639         characts_t *characts;
2640         version_t *version;
2641
2642         if(!stthead)
2643                 return NULL;
2644
2645         /* For all languages defined */
2646         for(stt = stthead; stt; stt = stt->next)
2647         {
2648                 assert(stt->nentries > 0);
2649
2650                 /* Sort the entries */
2651                 if(stt->nentries > 1)
2652                         qsort(stt->entries, stt->nentries, sizeof(stt->entries[0]), sort_stt_entry);
2653
2654                 for(i = 0; i < stt->nentries; )
2655                 {
2656                         newstt = new_stringtable(&stt->lvc);
2657                         newstt->entries = xmalloc(16 * sizeof(stt_entry_t));
2658                         memset( newstt->entries, 0, 16 * sizeof(stt_entry_t) );
2659                         newstt->nentries = 16;
2660                         newstt->idbase = stt->entries[i].id & ~0xf;
2661                         for(j = 0; j < 16 && i < stt->nentries; j++)
2662                         {
2663                                 if(stt->entries[i].id - newstt->idbase == j)
2664                                 {
2665                                         newstt->entries[j] = stt->entries[i];
2666                                         i++;
2667                                 }
2668                         }
2669                         andsum = ~0;
2670                         orsum = 0;
2671                         characts = NULL;
2672                         version = NULL;
2673                         /* Check individual memory options and get
2674                          * the first characteristics/version
2675                          */
2676                         for(j = 0; j < 16; j++)
2677                         {
2678                                 if(!newstt->entries[j].str)
2679                                         continue;
2680                                 andsum &= newstt->entries[j].memopt;
2681                                 orsum |= newstt->entries[j].memopt;
2682                                 if(!characts)
2683                                         characts = newstt->entries[j].characts;
2684                                 if(!version)
2685                                         version = newstt->entries[j].version;
2686                         }
2687                         if(andsum != orsum)
2688                         {
2689                                 warning("Stringtable's memory options are not equal (idbase: %d)\n", newstt->idbase);
2690                         }
2691                         /* Check version and characteristics */
2692                         for(j = 0; j < 16; j++)
2693                         {
2694                                 if(characts
2695                                 && newstt->entries[j].characts
2696                                 && *newstt->entries[j].characts != *characts)
2697                                         warning("Stringtable's characteristics are not the same (idbase: %d)\n", newstt->idbase);
2698                                 if(version
2699                                 && newstt->entries[j].version
2700                                 && *newstt->entries[j].version != *version)
2701                                         warning("Stringtable's versions are not the same (idbase: %d)\n", newstt->idbase);
2702                         }
2703                         rsc = new_resource(res_stt, newstt, newstt->memopt, newstt->lvc.language);
2704                         rsc->name = new_name_id();
2705                         rsc->name->type = name_ord;
2706                         rsc->name->name.i_name = (newstt->idbase >> 4) + 1;
2707                         rsc->memopt = andsum; /* Set to least common denominator */
2708                         newstt->memopt = andsum;
2709                         newstt->lvc.characts = characts;
2710                         newstt->lvc.version = version;
2711                         if(!rsclist)
2712                         {
2713                                 rsclist = rsc;
2714                                 rsctail = rsc;
2715                         }
2716                         else
2717                         {
2718                                 rsctail->next = rsc;
2719                                 rsc->prev = rsctail;
2720                                 rsctail = rsc;
2721                         }
2722                 }
2723         }
2724         return rsclist;
2725 }
2726
2727
2728 static toolbar_item_t *ins_tlbr_button(toolbar_item_t *prev, toolbar_item_t *idrec)
2729 {
2730         idrec->prev = prev;
2731         if(prev)
2732                 prev->next = idrec;
2733
2734         return idrec;
2735 }
2736
2737 static toolbar_item_t *get_tlbr_buttons_head(toolbar_item_t *p, int *nitems)
2738 {
2739         if(!p)
2740         {
2741                 *nitems = 0;
2742                 return NULL;
2743         }
2744
2745         *nitems = 1;
2746
2747         while(p->prev)
2748         {
2749                 (*nitems)++;
2750                 p = p->prev;
2751         }
2752
2753         return p;
2754 }
2755
2756 static string_t *make_filename(string_t *str)
2757 {
2758     if(str->type == str_char)
2759     {
2760         char *cptr;
2761
2762         /* Remove escaped backslash and convert to forward */
2763         for(cptr = str->str.cstr; (cptr = strchr(cptr, '\\')) != NULL; cptr++)
2764         {
2765                 if(cptr[1] == '\\')
2766                 {
2767                         memmove(cptr, cptr+1, strlen(cptr));
2768                         str->size--;
2769                 }
2770                 *cptr = '/';
2771         }
2772     }
2773     else
2774     {
2775         WCHAR *wptr;
2776
2777         /* Remove escaped backslash and convert to forward */
2778         for(wptr = str->str.wstr; (wptr = strchrW(wptr, '\\')) != NULL; wptr++)
2779         {
2780                 if(wptr[1] == '\\')
2781                 {
2782                         memmove(wptr, wptr+1, strlenW(wptr));
2783                         str->size--;
2784                 }
2785                 *wptr = '/';
2786         }
2787     }
2788     return str;
2789 }
2790
2791 /*
2792  * Process all resources to extract fonts and build
2793  * a fontdir resource.
2794  *
2795  * Note: MS' resource compiler (build 1472) does not
2796  * handle font resources with different languages.
2797  * The fontdir is generated in the last active language
2798  * and font identifiers must be unique across the entire
2799  * source.
2800  * This is not logical considering the localization
2801  * constraints of all other resource types. MS has,
2802  * most probably, never testet localized fonts. However,
2803  * using fontresources is rare, so it might not occur
2804  * in normal applications.
2805  * Wine does require better localization because a lot
2806  * of languages are coded into the same executable.
2807  * Therefore, I will generate fontdirs for *each*
2808  * localized set of fonts.
2809  */
2810 static resource_t *build_fontdir(resource_t **fnt, int nfnt)
2811 {
2812         static int once = 0;
2813         if(!once)
2814         {
2815                 warning("Need to parse fonts, not yet implemented (fnt: %p, nfnt: %d)\n", fnt, nfnt);
2816                 once++;
2817         }
2818         return NULL;
2819 }
2820
2821 static resource_t *build_fontdirs(resource_t *tail)
2822 {
2823         resource_t *rsc;
2824         resource_t *lst = NULL;
2825         resource_t **fnt = NULL;        /* List of all fonts */
2826         int nfnt = 0;
2827         resource_t **fnd = NULL;        /* List of all fontdirs */
2828         int nfnd = 0;
2829         resource_t **lanfnt = NULL;
2830         int nlanfnt = 0;
2831         int i;
2832         name_id_t nid;
2833         string_t str;
2834         int fntleft;
2835
2836         nid.type = name_str;
2837         nid.name.s_name = &str;
2838         str.type = str_char;
2839         str.str.cstr = xstrdup("FONTDIR");
2840         str.size = 7;
2841
2842         /* Extract all fonts and fontdirs */
2843         for(rsc = tail; rsc; rsc = rsc->prev)
2844         {
2845                 if(rsc->type == res_fnt)
2846                 {
2847                         nfnt++;
2848                         fnt = xrealloc(fnt, nfnt * sizeof(*fnt));
2849                         fnt[nfnt-1] = rsc;
2850                 }
2851                 else if(rsc->type == res_fntdir)
2852                 {
2853                         nfnd++;
2854                         fnd = xrealloc(fnd, nfnd * sizeof(*fnd));
2855                         fnd[nfnd-1] = rsc;
2856                 }
2857         }
2858
2859         /* Verify the name of the present fontdirs */
2860         for(i = 0; i < nfnd; i++)
2861         {
2862                 if(compare_name_id(&nid, fnd[i]->name))
2863                 {
2864                         warning("User supplied FONTDIR entry has an invalid name '%s', ignored\n",
2865                                 get_nameid_str(fnd[i]->name));
2866                         fnd[i] = NULL;
2867                 }
2868         }
2869
2870         /* Sanity check */
2871         if(nfnt == 0)
2872         {
2873                 if(nfnd != 0)
2874                         warning("Found %d FONTDIR entries without any fonts present\n", nfnd);
2875                 goto clean;
2876         }
2877
2878         /* Copy space */
2879         lanfnt = xmalloc(nfnt * sizeof(*lanfnt));
2880         memset( lanfnt, 0, nfnt * sizeof(*lanfnt));
2881
2882         /* Get all fonts covered by fontdirs */
2883         for(i = 0; i < nfnd; i++)
2884         {
2885                 int j;
2886                 WORD cnt;
2887                 int isswapped = 0;
2888
2889                 if(!fnd[i])
2890                         continue;
2891                 for(j = 0; j < nfnt; j++)
2892                 {
2893                         if(!fnt[j])
2894                                 continue;
2895                         if(fnt[j]->lan->id == fnd[i]->lan->id && fnt[j]->lan->sub == fnd[i]->lan->sub)
2896                         {
2897                                 lanfnt[nlanfnt] = fnt[j];
2898                                 nlanfnt++;
2899                                 fnt[j] = NULL;
2900                         }
2901                 }
2902
2903                 cnt = *(WORD *)fnd[i]->res.fnd->data->data;
2904                 if(nlanfnt == cnt)
2905                         isswapped = 0;
2906                 else if(nlanfnt == BYTESWAP_WORD(cnt))
2907                         isswapped = 1;
2908                 else
2909                         error("FONTDIR for language %d,%d has wrong count (%d, expected %d)\n",
2910                                 fnd[i]->lan->id, fnd[i]->lan->sub, cnt, nlanfnt);
2911 #ifdef WORDS_BIGENDIAN
2912                 if((byteorder == WRC_BO_LITTLE && !isswapped) || (byteorder != WRC_BO_LITTLE && isswapped))
2913 #else
2914                 if((byteorder == WRC_BO_BIG && !isswapped) || (byteorder != WRC_BO_BIG && isswapped))
2915 #endif
2916                 {
2917                         internal_error(__FILE__, __LINE__, "User supplied FONTDIR needs byteswapping\n");
2918                 }
2919         }
2920
2921         /* We now have fonts left where we need to make a fontdir resource */
2922         for(i = fntleft = 0; i < nfnt; i++)
2923         {
2924                 if(fnt[i])
2925                         fntleft++;
2926         }
2927         while(fntleft)
2928         {
2929                 /* Get fonts of same language in lanfnt[] */
2930                 for(i = nlanfnt = 0; i < nfnt; i++)
2931                 {
2932                         if(fnt[i])
2933                         {
2934                                 if(!nlanfnt)
2935                                 {
2936                         addlanfnt:
2937                                         lanfnt[nlanfnt] = fnt[i];
2938                                         nlanfnt++;
2939                                         fnt[i] = NULL;
2940                                         fntleft--;
2941                                 }
2942                                 else if(fnt[i]->lan->id == lanfnt[0]->lan->id && fnt[i]->lan->sub == lanfnt[0]->lan->sub)
2943                                         goto addlanfnt;
2944                         }
2945                 }
2946                 /* and build a fontdir */
2947                 rsc = build_fontdir(lanfnt, nlanfnt);
2948                 if(rsc)
2949                 {
2950                         if(lst)
2951                         {
2952                                 lst->next = rsc;
2953                                 rsc->prev = lst;
2954                         }
2955                         lst = rsc;
2956                 }
2957         }
2958
2959         free(lanfnt);
2960 clean:
2961         free(fnt);
2962         free(fnd);
2963         free(str.str.cstr);
2964         return lst;
2965 }
2966
2967 /*
2968  * This gets invoked to determine whether the next resource
2969  * is to be of a standard-type (e.g. bitmaps etc.), or should
2970  * be a user-type resource. This function is required because
2971  * there is the _possibility_ of a lookahead token in the
2972  * parser, which is generated from the "expr" state in the
2973  * "nameid" parsing.
2974  *
2975  * The general resource format is:
2976  * <identifier> <type> <flags> <resourcebody>
2977  *
2978  * The <identifier> can either be tIDENT or "expr". The latter
2979  * will always generate a lookahead, which is the <type> of the
2980  * resource to parse. Otherwise, we need to get a new token from
2981  * the scanner to determine the next step.
2982  *
2983  * The problem arrises when <type> is numerical. This case should
2984  * map onto default resource-types and be parsed as such instead
2985  * of being mapped onto user-type resources.
2986  *
2987  * The trick lies in the fact that yacc (bison) doesn't care about
2988  * intermediate changes of the lookahead while reducing a rule. We
2989  * simply replace the lookahead with a token that will result in
2990  * a shift to the appropriate rule for the specific resource-type.
2991  */
2992 static int rsrcid_to_token(int lookahead)
2993 {
2994         int token;
2995         const char *type = "?";
2996
2997         /* Get a token if we don't have one yet */
2998         if(lookahead == YYEMPTY)
2999                 lookahead = YYLEX;
3000
3001         /* Only numbers are possibly interesting */
3002         switch(lookahead)
3003         {
3004         case tNUMBER:
3005         case tLNUMBER:
3006                 break;
3007         default:
3008                 return lookahead;
3009         }
3010
3011         token = lookahead;
3012
3013         switch(yylval.num)
3014         {
3015         case WRC_RT_CURSOR:
3016                 type = "CURSOR";
3017                 token = tCURSOR;
3018                 break;
3019         case WRC_RT_ICON:
3020                 type = "ICON";
3021                 token = tICON;
3022                 break;
3023         case WRC_RT_BITMAP:
3024                 type = "BITMAP";
3025                 token = tBITMAP;
3026                 break;
3027         case WRC_RT_FONT:
3028                 type = "FONT";
3029                 token = tFONT;
3030                 break;
3031         case WRC_RT_FONTDIR:
3032                 type = "FONTDIR";
3033                 token = tFONTDIR;
3034                 break;
3035         case WRC_RT_RCDATA:
3036                 type = "RCDATA";
3037                 token = tRCDATA;
3038                 break;
3039         case WRC_RT_MESSAGETABLE:
3040                 type = "MESSAGETABLE";
3041                 token = tMESSAGETABLE;
3042                 break;
3043         case WRC_RT_DLGINIT:
3044                 type = "DLGINIT";
3045                 token = tDLGINIT;
3046                 break;
3047         case WRC_RT_ACCELERATOR:
3048                 type = "ACCELERATOR";
3049                 token = tACCELERATORS;
3050                 break;
3051         case WRC_RT_MENU:
3052                 type = "MENU";
3053                 token = tMENU;
3054                 break;
3055         case WRC_RT_DIALOG:
3056                 type = "DIALOG";
3057                 token = tDIALOG;
3058                 break;
3059         case WRC_RT_VERSION:
3060                 type = "VERSION";
3061                 token = tVERSIONINFO;
3062                 break;
3063         case WRC_RT_TOOLBAR:
3064                 type = "TOOLBAR";
3065                 token = tTOOLBAR;
3066                 break;
3067         case WRC_RT_HTML:
3068                 type = "HTML";
3069                 token = tHTML;
3070                 break;
3071
3072         case WRC_RT_STRING:
3073                 type = "STRINGTABLE";
3074                 break;
3075
3076         case WRC_RT_ANICURSOR:
3077         case WRC_RT_ANIICON:
3078         case WRC_RT_GROUP_CURSOR:
3079         case WRC_RT_GROUP_ICON:
3080                 parser_warning("Usertype uses reserved type ID %d, which is auto-generated\n", yylval.num);
3081                 return lookahead;
3082
3083         case WRC_RT_DLGINCLUDE:
3084         case WRC_RT_PLUGPLAY:
3085         case WRC_RT_VXD:
3086                 parser_warning("Usertype uses reserved type ID %d, which is not supported by wrc yet\n", yylval.num);
3087         default:
3088                 return lookahead;
3089         }
3090
3091         return token;
3092 }