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