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