Removed some more trailing whitespace.
[wine] / tools / wrc / parser.l
1 /* -*-C-*-
2  *
3  * Copyright 1998-2000  Bertho A. Stultiens (BS)
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * History:
20  * 21-May-2000 BS       - Fixed the ident requirement of resource names
21  *                        which can be keywords.
22  * 30-Apr-2000 BS       - Reintegration into the wine-tree
23  * 11-Jan-2000 BS       - Very drastic cleanup because we don't have a
24  *                        preprocessor in here anymore.
25  * 02-Jan-2000 BS       - Removed the preprocessor code
26  * 23-Dec-1999 BS       - Removed the copyright for Martin von Loewis.
27  *                        There is really nothing left of his code in
28  *                        this parser.
29  * 20-Jun-1998 BS       - Changed the filename conversion. Filenames are
30  *                        case-sensitive inder *nix, but not under dos.
31  *                        default behaviour is to convert to lower case.
32  *                      - All backslashes are converted to forward and
33  *                        both single and double slash is recognized as
34  *                        MS/Borland does.
35  *                      - Fixed a bug in 'yywf' case that prevented
36  *                        double quoted names to be scanned propperly.
37  *
38  * 19-May-1998 BS       - Started to build a preprocessor.
39  *                      - Changed keyword processing completely to
40  *                        table-lookups.
41  *
42  * 20-Apr-1998 BS       - Added ';' comment stripping
43  *
44  * 17-Apr-1998 BS       - Made the win32 keywords optional when compiling in
45  *                        16bit mode
46  *
47  * 15-Apr-1998 BS       - Changed string handling to include escapes
48  *                      - Added unicode string handling (no codepage
49  *                        translation though).
50  *                      - 'Borrowed' the main idea of string scanning from
51  *                        the flex manual pages.
52  *                      - Added conditional handling of scanning depending
53  *                        on the state of the parser. This was mainly required
54  *                        to distinguish a file to load or raw data that
55  *                        follows. MS's definition of filenames is rather
56  *                        complex... It can be unquoted or double quoted. If
57  *                        double quoted, then the '\\' char is not automatically
58  *                        escaped according to Borland's rc compiler, but it
59  *                        accepts both "\\path\\file.rc" and "\path\file.rc".
60  *                        This makes life very hard! I go for the escaped
61  *                        version, as this seems to be the documented way...
62  *                      - Single quoted strings are now parsed and converted
63  *                        here.
64  *                      - Added comment stripping. The implementation is
65  *                        'borrowed' from the flex manpages.
66  *                      - Rebuild string processing so that it may contain
67  *                        escaped '\0'.
68  */
69
70 /* Exclusive string handling */
71 %x yystr
72 /* Exclusive unicode string handling */
73 %x yylstr
74 /* Exclusive rcdata single quoted data handling */
75 %x yyrcd
76 /* Exclusive comment eating... */
77 %x comment
78 /* Set when stripping c-junk */
79 %x pp_stripe
80 %x pp_strips
81 %x pp_stripp
82 %x pp_stripp_final
83 /* Set when scanning #line style directives */
84 %x pp_line
85
86 %option stack
87 %option never-interactive
88
89 /* Some shortcut definitions */
90 ws      [ \f\t\r]
91 cident  [a-zA-Z_][0-9a-zA-Z_]*
92
93 %{
94
95 /*#define LEX_DEBUG*/
96
97 #include <stdio.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <ctype.h>
101 #include <assert.h>
102
103 #include "wrc.h"
104 #include "utils.h"
105 #include "preproc.h"
106 #include "parser.h"
107 #include "newstruc.h"
108
109 #include "y.tab.h"
110
111 #define YY_USE_PROTOS
112 #define YY_NO_UNPUT
113 #define YY_NO_TOP_STATE
114
115 /* Always update the current character position within a line */
116 #define YY_USER_ACTION  char_number+=yyleng; wanted_id = want_id; want_id = 0;
117
118 static void addcchar(char c);
119 static void addwchar(short s);
120 static string_t *get_buffered_cstring(void);
121 static string_t *get_buffered_wstring(void);
122 static string_t *make_string(char *s);
123
124 static char *cbuffer;           /* Buffers for string collection */
125 static int cbufidx;
126 static int cbufalloc = 0;
127 static short *wbuffer;
128 static int wbufidx;
129 static int wbufalloc = 0;
130 static int stripslevel = 0;     /* Count {} during pp_strips/pp_stripe mode */
131 static int stripplevel = 0;     /* Count () during pp_strips mode */
132 static int cjunk_tagline;       /* Where did we start stripping (helps error tracking) */
133
134 /*
135  * This one is a bit tricky.
136  * We set 'want_id' in the parser to get the first
137  * identifier we get across in the scanner, but we
138  * also want it to be reset at nearly any token we
139  * see. Exceptions are:
140  * - newlines
141  * - comments
142  * - whitespace
143  *
144  * The scanner will automatically reset 'want_id'
145  * after *each* scanner reduction and puts is value
146  * into the var below. In this way we can see the
147  * state after the YY_RULE_SETUP (i.e. the user action;
148  * see above) and don't have to worry too much when
149  * it needs to be reset.
150  */
151 static int wanted_id = 0;
152 static int save_wanted_id;      /* To save across comment reductions */
153
154 struct keyword {
155         char    *keyword;
156         int     token;
157         int     isextension;
158         int     needcase;
159         int     alwayskw;
160 };
161
162 static struct keyword keywords[] = {
163         { "ACCELERATORS",       tACCELERATORS,          0, 0, 0},
164         { "ALT",                tALT,                   0, 0, 0},
165         { "ASCII",              tASCII,                 0, 0, 0},
166         { "AUTO3STATE",         tAUTO3STATE,            1, 0, 0},
167         { "AUTOCHECKBOX",       tAUTOCHECKBOX,          1, 0, 0},
168         { "AUTORADIOBUTTON",    tAUTORADIOBUTTON,       1, 0, 0},
169         { "BEGIN",              tBEGIN,                 0, 0, 0},
170         { "BITMAP",             tBITMAP,                0, 0, 0},
171         { "BLOCK",              tBLOCK,                 0, 0, 0},
172         { "BUTTON",             tBUTTON,                1, 0, 0},
173         { "CAPTION",            tCAPTION,               0, 0, 0},
174         { "CHARACTERISTICS",    tCHARACTERISTICS,       1, 0, 0},
175         { "CHECKBOX",           tCHECKBOX,              0, 0, 0},
176         { "CHECKED",            tCHECKED,               0, 0, 0},
177         { "CLASS",              tCLASS,                 0, 0, 0},
178         { "COMBOBOX",           tCOMBOBOX,              0, 0, 0},
179         { "CONTROL",            tCONTROL,               0, 0, 0},
180         { "CTEXT",              tCTEXT,                 0, 0, 0},
181         { "CURSOR",             tCURSOR,                0, 0, 0},
182         { "DEFPUSHBUTTON",      tDEFPUSHBUTTON,         0, 0, 0},
183         { "DIALOG",             tDIALOG,                0, 0, 0},
184         { "DIALOGEX",           tDIALOGEX,              1, 0, 0},
185         { "DISCARDABLE",        tDISCARDABLE,           0, 0, 0},
186         { "DLGINIT",            tDLGINIT,               0, 0, 0},
187         { "EDITTEXT",           tEDITTEXT,              0, 0, 0},
188         { "END",                tEND,                   0, 0, 0},
189         { "EXSTYLE",            tEXSTYLE,               0, 0, 0},
190         { "FILEFLAGS",          tFILEFLAGS,             0, 0, 0},
191         { "FILEFLAGSMASK",      tFILEFLAGSMASK,         0, 0, 0},
192         { "FILEOS",             tFILEOS,                0, 0, 0},
193         { "FILESUBTYPE",        tFILESUBTYPE,           0, 0, 0},
194         { "FILETYPE",           tFILETYPE,              0, 0, 0},
195         { "FILEVERSION",        tFILEVERSION,           0, 0, 0},
196         { "FIXED",              tFIXED,                 0, 0, 0},
197         { "FONT",               tFONT,                  0, 0, 0},
198         { "FONTDIR",            tFONTDIR,               0, 0, 0},       /* This is a Borland BRC extension */
199         { "GRAYED",             tGRAYED,                0, 0, 0},
200         { "GROUPBOX",           tGROUPBOX,              0, 0, 0},
201         { "HELP",               tHELP,                  0, 0, 0},
202         { "ICON",               tICON,                  0, 0, 0},
203         { "IMPURE",             tIMPURE,                0, 0, 0},
204         { "INACTIVE",           tINACTIVE,              0, 0, 0},
205         { "LANGUAGE",           tLANGUAGE,              1, 0, 1},
206         { "LISTBOX",            tLISTBOX,               0, 0, 0},
207         { "LOADONCALL",         tLOADONCALL,            0, 0, 0},
208         { "LTEXT",              tLTEXT,                 0, 0, 0},
209         { "MENU",               tMENU,                  0, 0, 0},
210         { "MENUBARBREAK",       tMENUBARBREAK,          0, 0, 0},
211         { "MENUBREAK",          tMENUBREAK,             0, 0, 0},
212         { "MENUEX",             tMENUEX,                1, 0, 0},
213         { "MENUITEM",           tMENUITEM,              0, 0, 0},
214         { "MESSAGETABLE",       tMESSAGETABLE,          1, 0, 0},
215         { "MOVEABLE",           tMOVEABLE,              0, 0, 0},
216         { "NOINVERT",           tNOINVERT,              0, 0, 0},
217         { "NOT",                tNOT,                   0, 0, 0},
218         { "POPUP",              tPOPUP,                 0, 0, 0},
219         { "PRELOAD",            tPRELOAD,               0, 0, 0},
220         { "PRODUCTVERSION",     tPRODUCTVERSION,        0, 0, 0},
221         { "PURE",               tPURE,                  0, 0, 0},
222         { "PUSHBUTTON",         tPUSHBUTTON,            0, 0, 0},
223         { "RADIOBUTTON",        tRADIOBUTTON,           0, 0, 0},
224         { "RCDATA",             tRCDATA,                0, 0, 0},
225         { "RTEXT",              tRTEXT,                 0, 0, 0},
226         { "SCROLLBAR",          tSCROLLBAR,             0, 0, 0},
227         { "SEPARATOR",          tSEPARATOR,             0, 0, 0},
228         { "SHIFT",              tSHIFT,                 0, 0, 0},
229         { "STATE3",             tSTATE3,                1, 0, 0},
230         { "STRING",             tSTRING,                0, 0, 0},
231         { "STRINGTABLE",        tSTRINGTABLE,           0, 0, 1},
232         { "STYLE",              tSTYLE,                 0, 0, 0},
233         { "TOOLBAR",            tTOOLBAR,               1, 0, 0},
234         { "VALUE",              tVALUE,                 0, 0, 0},
235         { "VERSION",            tVERSION,               1, 0, 0},
236         { "VERSIONINFO",        tVERSIONINFO,           0, 0, 0},
237         { "VIRTKEY",            tVIRTKEY,               0, 0, 0}
238 };
239
240 #define NKEYWORDS       (sizeof(keywords)/sizeof(keywords[0]))
241 #define KWP(p)          ((struct keyword *)(p))
242 static int kw_cmp_func(const void *s1, const void *s2)
243 {
244         int ret;
245         ret = strcasecmp(KWP(s1)->keyword, KWP(s2)->keyword);
246         if(!ret && (KWP(s1)->needcase || KWP(s2)->needcase))
247                 return strcmp(KWP(s1)->keyword, KWP(s2)->keyword);
248         else
249                 return ret;
250 }
251
252 #define KW_BSEARCH
253 #define DO_SORT
254 static struct keyword *iskeyword(char *kw)
255 {
256         struct keyword *kwp;
257         struct keyword key;
258         key.keyword = kw;
259         key.needcase = 0;
260 #ifdef DO_SORT
261         {
262                 /* Make sure that it is sorted for bsearsh */
263                 static int sorted = 0;
264                 if(!sorted)
265                 {
266                         qsort(keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
267                         sorted = 1;
268                 }
269         }
270 #endif
271 #ifdef KW_BSEARCH
272         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
273 #else
274         {
275                 int i;
276                 for(i = 0; i < NKEYWORDS; i++)
277                 {
278                         if(!kw_cmp_func(&key, &keywords[i]))
279                                 break;
280                 }
281                 if(i < NKEYWORDS)
282                         kwp = &keywords[i];
283                 else
284                         kwp = NULL;
285         }
286 #endif
287
288         if(kwp == NULL || (kwp->isextension && !extensions))
289                 return NULL;
290         else
291                 return kwp;
292 }
293
294 %}
295
296 /*
297  **************************************************************************
298  * The flexer starts here
299  **************************************************************************
300  */
301 %%
302         /*
303          * Catch the GCC-style line statements here and parse them.
304          * This has the advantage that you can #include at any
305          * stage in the resource file.
306          * The preprocessor generates line directives in the format:
307          * # <linenum> "filename" <codes>
308          *
309          * Codes can be a sequence of:
310          * - 1 start of new file
311          * - 2 returning to previous
312          * - 3 system header
313          * - 4 interpret as C-code
314          *
315          * 4 is not used and 1 mutually excludes 2
316          * Anyhow, we are not really interested in these at all
317          * because we only want to know the linenumber and
318          * filename.
319          */
320 <INITIAL,pp_strips,pp_stripp>^{ws}*\#{ws}*      yy_push_state(pp_line);
321 <pp_line>[^\n]* {
322                 int lineno;
323                 char *cptr;
324                 char *fname;
325                 yy_pop_state();
326                 lineno = (int)strtol(yytext, &cptr, 10);
327                 if(!lineno)
328                         yyerror("Malformed '#...' line-directive; invalid linenumber");
329                 fname = strchr(cptr, '"');
330                 if(!fname)
331                         yyerror("Malformed '#...' line-directive; missing filename");
332                 fname++;
333                 cptr = strchr(fname, '"');
334                 if(!cptr)
335                         yyerror("Malformed '#...' line-directive; missing terminating \"");
336                 *cptr = '\0';
337                 line_number = lineno - 1;       /* We didn't read the newline */
338                 input_name = xstrdup(fname);
339         }
340
341         /*
342          * Strip everything until a ';' taking
343          * into account braces {} for structures,
344          * classes and enums.
345          */
346 <pp_strips>\{                   stripslevel++;
347 <pp_strips>\}                   stripslevel--;
348 <pp_strips>;                    if(!stripslevel) yy_pop_state();
349 <pp_strips>\/[^*\n]             ; /* To catch comments */
350 <pp_strips>[^\{\};\n#/]*        ; /* Ignore rest */
351 <pp_strips>\n                   line_number++; char_number = 1;
352
353 <pp_stripp>\(                   stripplevel++;
354 <pp_stripp>\)                   {
355                                         stripplevel--;
356                                         if(!stripplevel)
357                                         {
358                                                 yy_pop_state();
359                                                 yy_push_state(pp_stripp_final);
360                                         }
361                                 }
362 <pp_stripp>\/[^*\n]             ; /* To catch comments */
363 <pp_stripp>[^\(\);\n#/]*        ; /* Ignore rest */
364 <pp_stripp>\n                   line_number++; char_number = 1;
365
366 <pp_stripp_final>{ws}*          ; /* Ignore */
367 <pp_stripp_final>;              yy_pop_state(); /* Kill the semicolon */
368 <pp_stripp_final>\n             line_number++; char_number = 1; yy_pop_state();
369 <pp_stripp_final>.              yyless(0); yy_pop_state();
370
371 \{                      return tBEGIN;
372 \}                      return tEND;
373
374 [0-9]+[lL]?             { yylval.num = strtoul(yytext,  0, 10); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
375 0[xX][0-9A-Fa-f]+[lL]?  { yylval.num = strtoul(yytext,  0, 16); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
376 0[oO][0-7]+[lL]?        { yylval.num = strtoul(yytext+2, 0, 8); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
377
378         /*
379          * The next two rules scan identifiers and filenames.
380          * This is achieved by using the priority ruling
381          * of the scanner where a '.' is valid in a filename
382          * and *only* in a filename. In this case, the second
383          * rule will be reduced because it is longer.
384          */
385 [A-Za-z_0-9]+           {
386                                 struct keyword *tok = iskeyword(yytext);
387
388                                 if(tok)
389                                 {
390                                         if(wanted_id && !tok->alwayskw)
391                                         {
392                                                 yylval.str = make_string(yytext);
393                                                 return tIDENT;
394                                         }
395                                         else
396                                                 return tok->token;
397                                 }
398                                 else
399                                 {
400                                         yylval.str = make_string(yytext);
401                                         return tIDENT;
402                                 }
403                         }
404 [A-Za-z_0-9./\\]+               yylval.str = make_string(yytext); return tFILENAME;
405
406         /*
407          * Wide string scanning
408          */
409 L\"                     {
410                                 yy_push_state(yylstr);
411                                 wbufidx = 0;
412                                 if(!win32)
413                                         yywarning("16bit resource contains unicode strings\n");
414                         }
415 <yylstr>\"{ws}+ |
416 <yylstr>\"              {
417                                 yy_pop_state();
418                                 yylval.str = get_buffered_wstring();
419                                 return tSTRING;
420                         }
421 <yylstr>\\[0-7]{1,6}    { /* octal escape sequence */
422                                 int result;
423                                 result = strtol(yytext+1, 0, 8);
424                                 if ( result > 0xffff )
425                                         yyerror("Character constant out of range");
426                                 addwchar((short)result);
427                         }
428 <yylstr>\\x[0-9a-fA-F]{4} {  /* hex escape sequence */
429                                 int result;
430                                 result = strtol(yytext+2, 0, 16);
431                                 addwchar((short)result);
432                         }
433 <yylstr>\\x[0-9a-fA-F]{1,3} {  yyerror("Invalid hex escape sequence '%s'", yytext); }
434
435 <yylstr>\\[0-9]+        yyerror("Bad escape sequence");
436 <yylstr>\\a             addwchar('\a');
437 <yylstr>\\b             addwchar('\b');
438 <yylstr>\\f             addwchar('\f');
439 <yylstr>\\n             addwchar('\n');
440 <yylstr>\\r             addwchar('\r');
441 <yylstr>\\t             addwchar('\t');
442 <yylstr>\\v             addwchar('\v');
443 <yylstr>\\(\n|.)        addwchar(yytext[1]);
444 <yylstr>\\\r\n          addwchar(yytext[2]);
445 <yylstr>\"\"            addcchar('\"');         /* "bla""bla"  -> "bla\"bla" */
446 <yylstr>\\\"\"          addcchar('\"');         /* "bla\""bla" -> "bla\"bla" */
447 <yylstr>\"{ws}+\"       ;                       /* "bla" "bla" -> "blabla" */
448 <yylstr>[^\\\n\"]+      {
449                                 char *yptr = yytext;
450                                 while(*yptr)    /* FIXME: codepage translation */
451                                         addwchar(*yptr++ & 0xff);
452                         }
453 <yylstr>\n              yyerror("Unterminated string");
454
455         /*
456          * Normal string scanning
457          */
458 \"                      yy_push_state(yystr); cbufidx = 0;
459 <yystr>\"{ws}+  |
460 <yystr>\"               {
461                                 yy_pop_state();
462                                 yylval.str = get_buffered_cstring();
463                                 return tSTRING;
464                         }
465 <yystr>\\[0-7]{1,3}     { /* octal escape sequence */
466                                 int result;
467                                 result = strtol(yytext+1, 0, 8);
468                                 if ( result > 0xff )
469                                         yyerror("Character constant out of range");
470                                 addcchar((char)result);
471                         }
472 <yystr>\\x[0-9a-fA-F]{2} {  /* hex escape sequence */
473                                 int result;
474                                 result = strtol(yytext+2, 0, 16);
475                                 addcchar((char)result);
476                         }
477 <yystr>\\x[0-9a-fA-F]   {  yyerror("Invalid hex escape sequence '%s'", yytext); }
478
479 <yystr>\\[0-9]+         yyerror("Bad escape secuence");
480 <yystr>\\a              addcchar('\a');
481 <yystr>\\b              addcchar('\b');
482 <yystr>\\f              addcchar('\f');
483 <yystr>\\n              addcchar('\n');
484 <yystr>\\r              addcchar('\r');
485 <yystr>\\t              addcchar('\t');
486 <yystr>\\v              addcchar('\v');
487 <yystr>\\(\n|.)         addcchar(yytext[1]);
488 <yystr>\\\r\n           addcchar(yytext[2]);
489 <yystr>[^\\\n\"]+       {
490                                 char *yptr = yytext;
491                                 while(*yptr)
492                                         addcchar(*yptr++);
493                         }
494 <yystr>\"\"             addcchar('\"');         /* "bla""bla"   -> "bla\"bla" */
495 <yystr>\\\"\"           addcchar('\"');         /* "bla\""bla"  -> "bla\"bla" */
496 <yystr>\"{ws}+\"        ;                       /* "bla" "bla"  -> "blabla" */
497 <yystr>\n               yyerror("Unterminated string");
498
499         /*
500          * Raw data scanning
501          */
502 \'                      yy_push_state(yyrcd); cbufidx = 0;
503 <yyrcd>\'               {
504                                 yy_pop_state();
505                                 yylval.raw = new_raw_data();
506                                 yylval.raw->size = cbufidx;
507                                 yylval.raw->data = xmalloc(yylval.raw->size);
508                                 memcpy(yylval.raw->data, cbuffer, yylval.raw->size);
509                                 return tRAWDATA;
510                         }
511 <yyrcd>[0-9a-fA-F]{2}   {
512                                 int result;
513                                 result = strtol(yytext, 0, 16);
514                                 addcchar((char)result);
515                         }
516 <yyrcd>{ws}+            ;       /* Ignore space */
517 <yyrcd>\n               line_number++; char_number = 1;
518 <yyrcd>.                yyerror("Malformed data-line");
519
520         /*
521          * Comment stripping
522          * Should never occur after preprocessing
523          */
524 <INITIAL,pp_stripp,pp_strips>"/*"       {
525                                 yy_push_state(comment);
526                                 save_wanted_id = wanted_id;
527                                 if(!no_preprocess)
528                                         yywarning("Found comments after preprocessing, please report");
529                         }
530 <comment>[^*\n]*        ;
531 <comment>"*"+[^*/\n]*   ;
532 <comment>\n             line_number++; char_number = 1;
533 <comment>"*"+"/"        yy_pop_state(); want_id = save_wanted_id;
534
535 ;[^\n]*                 want_id = wanted_id; /* not really comment, but left-over c-junk */
536 "//"[^\n]*              want_id = wanted_id; if(!no_preprocess) yywarning("Found comments after preprocessing, please report");
537
538 \n                      {
539                                 want_id = wanted_id;
540                                 line_number++;
541                                 char_number = 1;
542                                 if(want_nl)
543                                 {
544                                         want_nl = 0;
545                                         return tNL;
546                                 }
547                         }
548 {ws}+                   want_id = wanted_id;    /* Eat whitespace */
549
550 <INITIAL>.              return yytext[0];
551
552 <<EOF>>                 {
553                                 if(YY_START == pp_strips || YY_START == pp_stripe || YY_START == pp_stripp || YY_START == pp_stripp_final)
554                                         yyerror("Unexpected end of file during c-junk scanning (started at %d)", cjunk_tagline);
555                                 else
556                                         yyterminate();
557                         }
558
559 <*>.|\n                 {
560                                 /* Catch all rule to find any unmatched text */
561                                 if(*yytext == '\n')
562                                 {
563                                         line_number++;
564                                         char_number = 1;
565                                 }
566                                 yywarning("Unmatched text '%c' (0x%02x) YY_START=%d stripslevel=%d",
567                                         isprint(*yytext & 0xff) ? *yytext : '.', *yytext, YY_START,stripslevel);
568                         }
569
570 %%
571
572 #ifndef yywrap
573 int yywrap(void)
574 {
575 #if 0
576         if(bufferstackidx > 0)
577         {
578                 return 0;
579         }
580 #endif
581         return 1;
582 }
583 #endif
584
585 /* These dup functions copy the enclosed '\0' from
586  * the resource string.
587  */
588 static void addcchar(char c)
589 {
590         if(cbufidx >= cbufalloc)
591         {
592                 cbufalloc += 1024;
593                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
594                 if(cbufalloc > 65536)
595                         yywarning("Reallocating string buffer larger than 64kB");
596         }
597         cbuffer[cbufidx++] = c;
598 }
599
600 static void addwchar(short s)
601 {
602         if(wbufidx >= wbufalloc)
603         {
604                 wbufalloc += 1024;
605                 wbuffer = xrealloc(wbuffer, wbufalloc * sizeof(wbuffer[0]));
606                 if(wbufalloc > 65536)
607                         yywarning("Reallocating wide string buffer larger than 64kB");
608         }
609
610         /*
611          * BS 08-Aug-1999 FIXME: The '& 0xff' is probably a bug, but I have
612          * not experienced it yet and I seem to remember that this was for
613          * a reason. But, as so many things you tend to forget why.
614          * I guess that there were problems due to the sign extension of
615          * shorts WRT chars (e.g. 0x80 becomes 0xff80 instead of 0x0080).
616          * This should then be fixed in the lexer calling the function.
617          */
618         wbuffer[wbufidx++] = (short)(s & 0xff);
619 }
620
621 static string_t *get_buffered_cstring(void)
622 {
623         string_t *str = new_string();
624         str->size = cbufidx;
625         str->type = str_char;
626         str->str.cstr = (char *)xmalloc(cbufidx+1);
627         memcpy(str->str.cstr, cbuffer, cbufidx);
628         str->str.cstr[cbufidx] = '\0';
629         return str;
630 }
631
632 static string_t *get_buffered_wstring(void)
633 {
634         string_t *str = new_string();
635         str->size = wbufidx;
636         str->type = str_unicode;
637         str->str.wstr = (short *)xmalloc(2*(wbufidx+1));
638         memcpy(str->str.wstr, wbuffer, wbufidx);
639         str->str.wstr[wbufidx] = 0;
640         return str;
641 }
642
643 static string_t *make_string(char *s)
644 {
645         string_t *str = new_string();
646         str->size = strlen(s);
647         str->type = str_char;
648         str->str.cstr = (char *)xmalloc(str->size+1);
649         memcpy(str->str.cstr, s, str->size+1);
650         return str;
651 }