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

~ [ 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.