- Implemented animated cursors and icons resource types.
[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
69 %option stack
70 %option never-interactive
71
72 /* Some shortcut definitions */
73 ws      [ \f\t\r]
74 cident  [a-zA-Z_][0-9a-zA-Z_]*
75
76 %{
77
78 /*#define LEX_DEBUG*/
79
80 #include "config.h"
81
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <ctype.h>
86 #include <assert.h>
87
88 #include "wrc.h"
89 #include "utils.h"
90 #include "preproc.h"
91 #include "parser.h"
92 #include "newstruc.h"
93
94 #include "y.tab.h"
95
96 #define YY_USE_PROTOS
97 #define YY_NO_UNPUT
98 #define YY_NO_TOP_STATE
99
100 /* Always update the current character position within a line */
101 #define YY_USER_ACTION  char_number+=yyleng; wanted_id = want_id; want_id = 0;
102
103 static void addcchar(char c);
104 static void addwchar(short s);
105 static string_t *get_buffered_cstring(void);
106 static string_t *get_buffered_wstring(void);
107 static string_t *make_string(char *s);
108
109 static char *cbuffer;           /* Buffers for string collection */
110 static int cbufidx;
111 static int cbufalloc = 0;
112 static short *wbuffer;
113 static int wbufidx;
114 static int wbufalloc = 0;
115 static int stripslevel = 0;     /* Count {} during pp_strips/pp_stripe mode */
116 static int stripplevel = 0;     /* Count () during pp_strips mode */
117 static int cjunk_tagline;       /* Where did we start stripping (helps error tracking) */
118
119 /*
120  * This one is a bit tricky.
121  * We set 'want_id' in the parser to get the first
122  * identifier we get across in the scanner, but we
123  * also want it to be reset at nearly any token we
124  * see. Exceptions are:
125  * - newlines
126  * - comments
127  * - whitespace
128  *
129  * The scanner will automatically reset 'want_id'
130  * after *each* scanner reduction and puts is value
131  * into the var below. In this way we can see the
132  * state after the YY_RULE_SETUP (i.e. the user action;
133  * see above) and don't have to worry too much when
134  * it needs to be reset.
135  */
136 static int wanted_id = 0;
137 static int save_wanted_id;      /* To save across comment reductions */
138
139 struct keyword {
140         char    *keyword;
141         int     token;
142         int     isextension;
143         int     needcase;
144         int     alwayskw;
145 };
146
147 static struct keyword keywords[] = {
148         { "ACCELERATORS",       tACCELERATORS,          0, 0, 0},
149         { "ALT",                tALT,                   0, 0, 0},
150         { "ASCII",              tASCII,                 0, 0, 0},
151         { "AUTO3STATE",         tAUTO3STATE,            1, 0, 0},
152         { "AUTOCHECKBOX",       tAUTOCHECKBOX,          1, 0, 0},
153         { "AUTORADIOBUTTON",    tAUTORADIOBUTTON,       1, 0, 0},
154         { "BEGIN",              tBEGIN,                 0, 0, 0},
155         { "BITMAP",             tBITMAP,                0, 0, 0},
156         { "BLOCK",              tBLOCK,                 0, 0, 0},
157         { "BUTTON",             tBUTTON,                1, 0, 0},
158         { "CAPTION",            tCAPTION,               0, 0, 0},
159         { "CHARACTERISTICS",    tCHARACTERISTICS,       1, 0, 0},
160         { "CHECKBOX",           tCHECKBOX,              0, 0, 0},
161         { "CHECKED",            tCHECKED,               0, 0, 0},
162         { "CLASS",              tCLASS,                 0, 0, 0},
163         { "COMBOBOX",           tCOMBOBOX,              0, 0, 0},
164         { "CONTROL",            tCONTROL,               0, 0, 0},
165         { "CTEXT",              tCTEXT,                 0, 0, 0},
166         { "CURSOR",             tCURSOR,                0, 0, 0},
167         { "DEFPUSHBUTTON",      tDEFPUSHBUTTON,         0, 0, 0},
168         { "DIALOG",             tDIALOG,                0, 0, 0},
169         { "DIALOGEX",           tDIALOGEX,              1, 0, 0},
170         { "DISCARDABLE",        tDISCARDABLE,           0, 0, 0},
171         { "DLGINIT",            tDLGINIT,               0, 0, 0},
172         { "EDITTEXT",           tEDITTEXT,              0, 0, 0},
173         { "END",                tEND,                   0, 0, 0},
174         { "enum",               tENUM,                  0, 1, 1},
175         { "EXSTYLE",            tEXSTYLE,               0, 0, 0},
176         { "extern",             tEXTERN,                0, 1, 1},
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         { "inline",             tINLINE,                0, 1, 1},
193         { "LANGUAGE",           tLANGUAGE,              1, 0, 1},
194         { "LISTBOX",            tLISTBOX,               0, 0, 0},
195         { "LOADONCALL",         tLOADONCALL,            0, 0, 0},
196         { "LTEXT",              tLTEXT,                 0, 0, 0},
197         { "MENU",               tMENU,                  0, 0, 0},
198         { "MENUBARBREAK",       tMENUBARBREAK,          0, 0, 0},
199         { "MENUBREAK",          tMENUBREAK,             0, 0, 0},
200         { "MENUEX",             tMENUEX,                1, 0, 0},
201         { "MENUITEM",           tMENUITEM,              0, 0, 0},
202         { "MESSAGETABLE",       tMESSAGETABLE,          1, 0, 0},
203         { "MOVEABLE",           tMOVEABLE,              0, 0, 0},
204         { "NOINVERT",           tNOINVERT,              0, 0, 0},
205         { "NOT",                tNOT,                   0, 0, 0},
206         { "POPUP",              tPOPUP,                 0, 0, 0},
207         { "PRELOAD",            tPRELOAD,               0, 0, 0},
208         { "PRODUCTVERSION",     tPRODUCTVERSION,        0, 0, 0},
209         { "PURE",               tPURE,                  0, 0, 0},
210         { "PUSHBUTTON",         tPUSHBUTTON,            0, 0, 0},
211         { "RADIOBUTTON",        tRADIOBUTTON,           0, 0, 0},
212         { "RCDATA",             tRCDATA,                0, 0, 0},
213         { "RTEXT",              tRTEXT,                 0, 0, 0},
214         { "SCROLLBAR",          tSCROLLBAR,             0, 0, 0},
215         { "SEPARATOR",          tSEPARATOR,             0, 0, 0},
216         { "SHIFT",              tSHIFT,                 0, 0, 0},
217         { "STATE3",             tSTATE3,                1, 0, 0},
218         { "static",             tSTATIC,                0, 1, 1},
219         { "STRING",             tSTRING,                0, 0, 0},
220         { "STRINGTABLE",        tSTRINGTABLE,           0, 0, 1},
221         { "struct",             tSTRUCT,                0, 1, 1},
222         { "STYLE",              tSTYLE,                 0, 0, 0},
223         { "TOOLBAR",            tTOOLBAR,               1, 0, 0},
224         { "typedef",            tTYPEDEF,               0, 1, 1},
225         { "VALUE",              tVALUE,                 0, 0, 0},
226         { "VERSION",            tVERSION,               1, 0, 0},
227         { "VERSIONINFO",        tVERSIONINFO,           0, 0, 0},
228         { "VIRTKEY",            tVIRTKEY,               0, 0, 0}
229 };
230
231 #define NKEYWORDS       (sizeof(keywords)/sizeof(keywords[0]))
232 #define KWP(p)          ((struct keyword *)(p))
233 static int kw_cmp_func(const void *s1, const void *s2)
234 {
235         int ret;
236         ret = strcasecmp(KWP(s1)->keyword, KWP(s2)->keyword);
237         if(!ret && (KWP(s1)->needcase || KWP(s2)->needcase))
238                 return strcmp(KWP(s1)->keyword, KWP(s2)->keyword);
239         else
240                 return ret;
241 }
242
243 #define KW_BSEARCH
244 #define DO_SORT
245 static struct keyword *iskeyword(char *kw)
246 {
247         struct keyword *kwp;
248         struct keyword key;
249         key.keyword = kw;
250         key.needcase = 0;
251 #ifdef DO_SORT
252         {
253                 /* Make sure that it is sorted for bsearsh */
254                 static int sorted = 0;
255                 if(!sorted)
256                 {
257                         qsort(keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
258                         sorted = 1;
259                 }
260         }
261 #endif
262 #ifdef KW_BSEARCH
263         kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
264 #else
265         {
266                 int i;
267                 for(i = 0; i < NKEYWORDS; i++)
268                 {
269                         if(!kw_cmp_func(&key, &keywords[i]))
270                                 break;
271                 }
272                 if(i < NKEYWORDS)
273                         kwp = &keywords[i];
274                 else
275                         kwp = NULL;
276         }
277 #endif
278
279         if(kwp == NULL || (kwp->isextension && !extensions))
280                 return NULL;
281         else
282                 return kwp;
283 }
284
285 %}
286
287 /*
288  **************************************************************************
289  * The flexer starts here
290  **************************************************************************
291  */
292 %%
293         /*
294          * Strip everything until a ';' taking
295          * into account braces {} for structures,
296          * classes and enums.
297          */
298 <pp_strips>\{                   stripslevel++;
299 <pp_strips>\}                   stripslevel--;
300 <pp_strips>;                    if(!stripslevel) yy_pop_state();
301 <pp_strips>\/[^*\n]             ; /* To catch comments */
302 <pp_strips>[^\{\};\n#/]*        ; /* Ignore rest */
303 <pp_strips>\n                   line_number++; char_number = 1;
304
305 <pp_stripp>\(                   stripplevel++;
306 <pp_stripp>\)                   {
307                                         stripplevel--;
308                                         if(!stripplevel)
309                                         {
310                                                 yy_pop_state();
311                                                 yy_push_state(pp_stripp_final);
312                                         }
313                                 }
314 <pp_stripp>\/[^*\n]             ; /* To catch comments */
315 <pp_stripp>[^\(\);\n#/]*        ; /* Ignore rest */
316 <pp_stripp>\n                   line_number++; char_number = 1;
317
318 <pp_stripp_final>{ws}*          ; /* Ignore */
319 <pp_stripp_final>;              yy_pop_state(); /* Kill the semicolon */
320 <pp_stripp_final>\n             line_number++; char_number = 1; yy_pop_state();
321 <pp_stripp_final>.              yyless(0); yy_pop_state();
322
323 \{                      return tBEGIN;
324 \}                      return tEND;
325
326 [0-9]+[lL]?             { yylval.num = strtoul(yytext,  0, 10); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
327 0[xX][0-9A-Fa-f]+[lL]?  { yylval.num = strtoul(yytext,  0, 16); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
328 0[oO][0-7]+[lL]?        { yylval.num = strtoul(yytext+2, 0, 8); return toupper(yytext[yyleng-1]) == 'L' ? tLNUMBER : tNUMBER; }
329
330         /*
331          * The next to rules scan identifiers and filenames.
332          * This is achieved by using the priority ruling
333          * of the scanner where a '.' is valid in a filename
334          * and *only* in a filename. In this case, the second
335          * rule will be reduced because it is longer.
336          */
337 [A-Za-z_0-9]+           {
338                                 struct keyword *tok = iskeyword(yytext);
339
340                                 if(tok)
341                                 {
342                                         if(tok->token == tCLASS && !strcmp(yytext, "class"))
343                                                 return tCPPCLASS;
344                                         else if(wanted_id && !tok->alwayskw)
345                                         {
346                                                 yylval.str = make_string(yytext);
347                                                 return tIDENT;
348                                         }
349                                         else
350                                                 return tok->token;
351                                 }
352                                 else
353                                 {
354                                         yylval.str = make_string(yytext);
355                                         return tIDENT;
356                                 }
357                         }
358 [A-Za-z_0-9.]+          yylval.str = make_string(yytext); return tFILENAME;
359
360         /*
361          * Wide string scanning
362          */
363 L\"                     {
364                                 yy_push_state(yylstr);
365                                 wbufidx = 0;
366                                 if(!win32)
367                                         yywarning("16bit resource contains unicode strings\n");
368                         }
369 <yylstr>\"{ws}+ |
370 <yylstr>\"              {
371                                 yy_pop_state();
372                                 yylval.str = get_buffered_wstring();
373                                 return tSTRING;
374                         }
375 <yylstr>\\[0-7]{1,6}    { /* octal escape sequence */
376                                 int result;
377                                 result = strtol(yytext+1, 0, 8);
378                                 if ( result > 0xffff )
379                                         yyerror("Character constant out of range");
380                                 addwchar((short)result);
381                         }
382 <yylstr>\\x[0-9a-fA-F]{4} {  /* hex escape sequence */
383                                 int result;
384                                 result = strtol(yytext+2, 0, 16);
385                                 addwchar((short)result);
386                         }
387 <yylstr>\\x[0-9a-fA-F]{1,3} {  yyerror("Invalid hex escape sequence '%s'", yytext); }
388
389 <yylstr>\\[0-9]+        yyerror("Bad escape secuence");
390 <yylstr>\\a             addwchar('\a');
391 <yylstr>\\b             addwchar('\b');
392 <yylstr>\\f             addwchar('\f');
393 <yylstr>\\n             addwchar('\n');
394 <yylstr>\\r             addwchar('\r');
395 <yylstr>\\t             addwchar('\t');
396 <yylstr>\\v             addwchar('\v');
397 <yylstr>\\.             if(yytext[1] != '\n') addwchar(yytext[1]);
398 <yylstr>\"\"            addcchar('\"');         /* "bla""bla"  -> "bla\"bla" */
399 <yylstr>\\\"\"          addcchar('\"');         /* "bla\""bla" -> "bla\"bla" */
400 <yylstr>\"{ws}+\"       ;                       /* "bla" "bla" -> "blabla" */
401 <yylstr>[^\\\n\"]+      {
402                                 char *yptr = yytext;
403                                 while(*yptr)    /* FIXME: codepage translation */
404                                         addwchar(*yptr++ & 0xff);
405                         }
406 <yylstr>\n              yyerror("Unterminated string");
407
408         /*
409          * Normal string scanning
410          */
411 \"                      yy_push_state(yystr); cbufidx = 0;
412 <yystr>\"{ws}+  |
413 <yystr>\"               {
414                                 yy_pop_state();
415                                 yylval.str = get_buffered_cstring();
416                                 return tSTRING;
417                         }
418 <yystr>\\[0-7]{1,3}     { /* octal escape sequence */
419                                 int result;
420                                 result = strtol(yytext+1, 0, 8);
421                                 if ( result > 0xff )
422                                         yyerror("Character constant out of range");
423                                 addcchar((char)result);
424                         }
425 <yystr>\\x[0-9a-fA-F]{2} {  /* hex escape sequence */
426                                 int result;
427                                 result = strtol(yytext+2, 0, 16);
428                                 addcchar((char)result);
429                         }
430 <yystr>\\x[0-9a-fA-F]   {  yyerror("Invalid hex escape sequence '%s'", yytext); }
431
432 <yystr>\\[0-9]+         yyerror("Bad escape secuence");
433 <yystr>\\a              addcchar('\a');
434 <yystr>\\b              addcchar('\b');
435 <yystr>\\f              addcchar('\f');
436 <yystr>\\n              addcchar('\n');
437 <yystr>\\r              addcchar('\r');
438 <yystr>\\t              addcchar('\t');
439 <yystr>\\v              addcchar('\v');
440 <yystr>\\.              if(yytext[1] != '\n') addcchar(yytext[1]);
441 <yystr>[^\\\n\"]+       {
442                                 char *yptr = yytext;
443                                 while(*yptr)
444                                         addcchar(*yptr++);
445                         }
446 <yystr>\"\"             addcchar('\"');         /* "bla""bla"   -> "bla\"bla" */
447 <yystr>\\\"\"           addcchar('\"');         /* "bla\""bla"  -> "bla\"bla" */
448 <yystr>\"{ws}+\"        ;                       /* "bla" "bla"  -> "blabla" */
449 <yystr>\n               yyerror("Unterminated string");
450
451         /*
452          * Raw data scanning
453          */
454 \'                      yy_push_state(yyrcd); cbufidx = 0;
455 <yyrcd>\'               {
456                                 yy_pop_state();
457                                 yylval.raw = new_raw_data();
458                                 yylval.raw->size = cbufidx;
459                                 yylval.raw->data = xmalloc(yylval.raw->size);
460                                 memcpy(yylval.raw->data, cbuffer, yylval.raw->size);
461                                 return tRAWDATA;
462                         }
463 <yyrcd>[0-9a-fA-F]{2}   {
464                                 int result;
465                                 result = strtol(yytext, 0, 16);
466                                 addcchar((char)result);
467                         }
468 <yyrcd>{ws}+            ;       /* Ignore space */
469 <yyrcd>\n               line_number++; char_number = 1;
470 <yyrcd>.                yyerror("Malformed data-line");
471
472         /*
473          * Comment stripping
474          * Should never occur after preprocessing
475          */
476 <INITIAL,pp_stripp,pp_strips>"/*"       {
477                                 yy_push_state(comment);
478                                 save_wanted_id = wanted_id;
479                                 if(!no_preprocess)
480                                         yywarning("Found comments after preprocessing, please report");
481                         }
482 <comment>[^*\n]*        ;
483 <comment>"*"+[^*/\n]*   ;
484 <comment>\n             line_number++; char_number = 1;
485 <comment>"*"+"/"        yy_pop_state(); want_id = save_wanted_id;
486
487 ;[^\n]*                 want_id = wanted_id; /* not really comment, but left-over c-junk */
488 "//"[^\n]*              want_id = wanted_id; if(!no_preprocess) yywarning("Found comments after preprocessing, please report");
489
490 \n                      {
491                                 want_id = wanted_id;
492                                 line_number++;
493                                 char_number = 1;
494                                 if(want_nl)
495                                 {
496                                         want_nl = 0;
497                                         return tNL;
498                                 }
499                         }
500 {ws}+                   want_id = wanted_id;    /* Eat whitespace */
501
502 <INITIAL>.              return yytext[0];
503
504 <<EOF>>                 {
505                                 if(YY_START == pp_strips || YY_START == pp_stripe || YY_START == pp_stripp || YY_START == pp_stripp_final)
506                                         yyerror("Unexpected end of file during c-junk scanning (started at %d)", cjunk_tagline);
507                                 else
508                                         yyterminate();
509                         }
510
511 <*>.|\n                 {
512                                 /* Catch all rule to find any unmatched text */
513                                 if(*yytext == '\n')
514                                 {
515                                         line_number++;
516                                         char_number = 1;
517                                 }
518                                 yywarning("Unmatched text '%c' (0x%02x) YY_START=%d stripslevel=%d",
519                                         isprint(*yytext) ? *yytext : '.', *yytext, YY_START,stripslevel);
520                         }
521
522 %%
523
524 #ifndef yywrap
525 int yywrap(void)
526 {
527 #if 0
528         if(bufferstackidx > 0)
529         {
530                 return 0;
531         }
532 #endif
533         return 1;
534 }
535 #endif
536
537 /* These dup functions copy the enclosed '\0' from
538  * the resource string.
539  */
540 static void addcchar(char c)
541 {
542         if(cbufidx >= cbufalloc)
543         {
544                 cbufalloc += 1024;
545                 cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
546                 if(cbufalloc > 65536)
547                         yywarning("Reallocating string buffer larger than 64kB");
548         }
549         cbuffer[cbufidx++] = c;
550 }
551
552 static void addwchar(short s)
553 {
554         if(wbufidx >= wbufalloc)
555         {
556                 wbufalloc += 1024;
557                 wbuffer = xrealloc(wbuffer, wbufalloc * sizeof(wbuffer[0]));
558                 if(wbufalloc > 65536)
559                         yywarning("Reallocating wide string buffer larger than 64kB");
560         }
561
562         /*
563          * BS 08-Aug-1999 FIXME: The '& 0xff' is probably a bug, but I have
564          * not experienced it yet and I seem to remember that this was for
565          * a reason. But, as so many things you tend to forget why.
566          * I guess that there were problems due to the sign extension of
567          * shorts WRT chars (e.g. 0x80 becomes 0xff80 instead of 0x0080).
568          * This should then be fixed in the lexer calling the function.
569          */
570         wbuffer[wbufidx++] = (short)(s & 0xff);
571 }
572
573 static string_t *get_buffered_cstring(void)
574 {
575         string_t *str = new_string();
576         str->size = cbufidx;
577         str->type = str_char;
578         str->str.cstr = (char *)xmalloc(cbufidx+1);
579         memcpy(str->str.cstr, cbuffer, cbufidx);
580         str->str.cstr[cbufidx] = '\0';
581         return str;
582 }
583
584 static string_t *get_buffered_wstring(void)
585 {
586         string_t *str = new_string();
587         str->size = wbufidx;
588         str->type = str_unicode;
589         str->str.wstr = (short *)xmalloc(2*(wbufidx+1));
590         memcpy(str->str.wstr, wbuffer, wbufidx);
591         str->str.wstr[wbufidx] = 0;
592         return str;
593 }
594
595 static string_t *make_string(char *s)
596 {
597         string_t *str = new_string();
598         str->size = strlen(s);
599         str->type = str_char;
600         str->str.cstr = (char *)xmalloc(str->size+1);
601         memcpy(str->str.cstr, s, str->size+1);
602         return str;
603 }
604
605 /* Called from the parser to kill c-junk */
606 void strip_extern(void)
607 {
608         cjunk_tagline = line_number;
609         yy_push_state(pp_stripe);
610 }
611
612 void strip_til_semicolon(void)
613 {
614         cjunk_tagline = line_number;
615         yy_push_state(pp_strips);
616 }
617
618 void strip_til_parenthesis(void)
619 {
620         cjunk_tagline = line_number;
621         stripplevel = 1;        /* One scanned already */
622         yy_push_state(pp_stripp);
623 }
624