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