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