~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/tools/wrc/parser.y

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.