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