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