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