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