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