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