parser.l 24.2 KB
Newer Older
1
/* -*-C-*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * IDL Compiler
 *
 * Copyright 2002 Ove Kaaven
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21
 */

%option stack
22
%option noinput nounput noyy_top_state
23
%option 8bit never-interactive prefix="parser_"
24 25 26 27

nl	\r?\n
ws	[ \f\t\r]
cident	[a-zA-Z_][0-9a-zA-Z_]*
28 29 30
u_suffix	(u|U)
l_suffix	(l|L)
int	[0-9]+({l_suffix}?{u_suffix}?|{u_suffix}?{l_suffix}?)?
31
hexd	[0-9a-fA-F]
32
hex	0(x|X){hexd}+({l_suffix}?{u_suffix}?|{u_suffix}?{l_suffix}?)?
33
uuid	{hexd}{8}-{hexd}{4}-{hexd}{4}-{hexd}{4}-{hexd}{12}
34
double	[0-9]+\.[0-9]+([eE][+-]?[0-9]+)*
35 36

%x QUOTE
37
%x WSTRQUOTE
38 39
%x ATTR
%x PP_LINE
40
%x PP_PRAGMA
41
%x SQUOTE
42 43 44

%{

45
#include "config.h"
46
#include "wine/port.h"
47

48 49 50 51 52
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
Huw Davies's avatar
Huw Davies committed
53
#include <errno.h>
54
#include <limits.h>
55

56 57 58
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
59
#define YY_NO_UNISTD_H
60
#endif
61 62 63 64

#include "widl.h"
#include "utils.h"
#include "parser.h"
65
#include "wine/wpp.h"
66

67
#include "parser.tab.h"
68 69 70 71 72 73 74 75 76

static void addcchar(char c);
static char *get_buffered_cstring(void);

static char *cbuffer;
static int cbufidx;
static int cbufalloc = 0;

static int kw_token(const char *kw);
77
static int attr_token(const char *kw);
78

79 80
static void switch_to_acf(void);

81 82
static warning_list_t *disabled_warnings = NULL;

83
#define MAX_IMPORT_DEPTH 20
84 85
struct {
  YY_BUFFER_STATE state;
86 87
  char *input_name;
  int   line_number;
88 89 90 91
  char *temp_name;
} import_stack[MAX_IMPORT_DEPTH];
int import_stack_ptr = 0;

92 93
/* converts an integer in string form to an unsigned long and prints an error
 * on overflow */
94
static unsigned int xstrtoul(const char *nptr, char **endptr, int base)
95
{
96
    unsigned long val;
97 98

    errno = 0;
99 100
    val = strtoul(nptr, endptr, base);
    if ((val == ULONG_MAX && errno == ERANGE) || ((unsigned int)val != val))
101
        error_loc("integer constant %s is too large\n", nptr);
102
    return val;
103 104
}

105
UUID *parse_uuid(const char *u)
106
{
107
  UUID* uuid = xmalloc(sizeof(UUID));
108 109
  char b[3];
  /* it would be nice to use UuidFromStringA */
110 111 112
  uuid->Data1 = strtoul(u, NULL, 16);
  uuid->Data2 = strtoul(u+9, NULL, 16);
  uuid->Data3 = strtoul(u+14, NULL, 16);
113
  b[2] = 0;
114 115 116 117 118 119 120 121
  memcpy(b, u+19, 2); uuid->Data4[0] = strtoul(b, NULL, 16);
  memcpy(b, u+21, 2); uuid->Data4[1] = strtoul(b, NULL, 16);
  memcpy(b, u+24, 2); uuid->Data4[2] = strtoul(b, NULL, 16);
  memcpy(b, u+26, 2); uuid->Data4[3] = strtoul(b, NULL, 16);
  memcpy(b, u+28, 2); uuid->Data4[4] = strtoul(b, NULL, 16);
  memcpy(b, u+30, 2); uuid->Data4[5] = strtoul(b, NULL, 16);
  memcpy(b, u+32, 2); uuid->Data4[6] = strtoul(b, NULL, 16);
  memcpy(b, u+34, 2); uuid->Data4[7] = strtoul(b, NULL, 16);
122 123 124
  return uuid;
}

125 126 127 128 129 130 131 132
%}

/*
 **************************************************************************
 * The flexer starts here
 **************************************************************************
 */
%%
133
<INITIAL>^{ws}*\#{ws}*pragma{ws}+ yy_push_state(PP_PRAGMA);
134 135
<INITIAL,ATTR>^{ws}*\#{ws}*	yy_push_state(PP_LINE);
<PP_LINE>[^\n]*         {
136 137 138 139 140
                            int lineno;
                            char *cptr, *fname;
                            yy_pop_state();
                            lineno = (int)strtol(yytext, &cptr, 10);
                            if(!lineno)
141
                                error_loc("Malformed '#...' line-directive; invalid linenumber\n");
142 143
                            fname = strchr(cptr, '"');
                            if(!fname)
144
                                error_loc("Malformed '#...' line-directive; missing filename\n");
145 146 147
                            fname++;
                            cptr = strchr(fname, '"');
                            if(!cptr)
148
                                error_loc("Malformed '#...' line-directive; missing terminating \"\n");
149 150 151 152
                            *cptr = '\0';
                            line_number = lineno - 1;  /* We didn't read the newline */
                            input_name = xstrdup(fname);
                        }
153
<PP_PRAGMA>midl_echo[^\n]*  yyless(9); yy_pop_state(); return tCPPQUOTE;
154 155 156 157 158
<PP_PRAGMA>winrt[^\n]*  {
                            if(import_stack_ptr) {
                                if(!winrt_mode)
                                    error_loc("winrt IDL file imported in non-winrt mode\n");
                            }else {
159 160
                                const char *ptr = yytext+5;

161
                                winrt_mode = TRUE;
162 163 164 165 166

                                while(isspace(*ptr))
                                    ptr++;
                                if(!strncmp(ptr, "ns_prefix", 9) && (!*(ptr += 9) || isspace(*ptr)))
                                    use_abi_namespace = TRUE;
167 168 169
                            }
                            yy_pop_state();
                        }
170
<PP_PRAGMA>[^\n]*       parser_lval.str = xstrdup(yytext); yy_pop_state(); return aPRAGMA;
171
<INITIAL>^{ws}*midl_pragma{ws}+warning return tPRAGMA_WARNING;
172
<INITIAL,ATTR>\"	yy_push_state(QUOTE); cbufidx = 0;
173 174
<QUOTE>\"		{
				yy_pop_state();
175
				parser_lval.str = get_buffered_cstring();
176 177
				return aSTRING;
			}
178
<INITIAL,ATTR>L\"	yy_push_state(WSTRQUOTE); cbufidx = 0;
179 180 181 182 183
<WSTRQUOTE>\"		{
				yy_pop_state();
				parser_lval.str = get_buffered_cstring();
				return aWSTRING;
			}
184 185 186 187 188 189 190
<INITIAL,ATTR>\'	yy_push_state(SQUOTE); cbufidx = 0;
<SQUOTE>\'		{
				yy_pop_state();
				parser_lval.str = get_buffered_cstring();
				return aSQSTRING;
			}
<QUOTE,WSTRQUOTE,SQUOTE>\\\\	|
191
<QUOTE,WSTRQUOTE>\\\"	addcchar(yytext[1]);
192 193 194
<SQUOTE>\\\'	addcchar(yytext[1]);
<QUOTE,WSTRQUOTE,SQUOTE>\\.	addcchar('\\'); addcchar(yytext[1]);
<QUOTE,WSTRQUOTE,SQUOTE>.	addcchar(yytext[0]);
195 196 197 198
<INITIAL,ATTR>\[	yy_push_state(ATTR); return '[';
<ATTR>\]		yy_pop_state(); return ']';
<ATTR>{cident}		return attr_token(yytext);
<ATTR>{uuid}			{
199
				parser_lval.uuid = parse_uuid(yytext);
200 201
				return aUUID;
			}
202
<INITIAL,ATTR>{hex}	{
203
				parser_lval.num = xstrtoul(yytext, NULL, 0);
204 205
				return aHEXNUM;
			}
206
<INITIAL,ATTR>{int}	{
207
				parser_lval.num = xstrtoul(yytext, NULL, 0);
208 209
				return aNUM;
			}
210 211 212 213
<INITIAL>{double}	{
				parser_lval.dbl = strtod(yytext, NULL);
				return aDOUBLE;
			}
214
SAFEARRAY{ws}*/\(	return tSAFEARRAY;
215
{cident}		return kw_token(yytext);
216 217 218 219
<INITIAL,ATTR>\n	line_number++;
<INITIAL,ATTR>{ws}
<INITIAL,ATTR>\<\<	return SHL;
<INITIAL,ATTR>\>\>	return SHR;
220
<INITIAL,ATTR>\-\>	return MEMBERPTR;
221 222 223 224 225 226
<INITIAL,ATTR>==	return EQUALITY;
<INITIAL,ATTR>!=	return INEQUALITY;
<INITIAL,ATTR>\>=	return GREATEREQUAL;
<INITIAL,ATTR>\<=	return LESSEQUAL;
<INITIAL,ATTR>\|\|	return LOGICALOR;
<INITIAL,ATTR>&&	return LOGICALAND;
227
<INITIAL,ATTR>\.\.\.	return ELLIPSIS;
228
<INITIAL,ATTR>.		return yytext[0];
229
<<EOF>>			{
230 231 232 233 234 235 236 237
                            if (import_stack_ptr)
                                return aEOF;
                            if (acf_name)
                            {
                                switch_to_acf();
                                return aACF;
                            }
                            yyterminate();
238 239 240
			}
%%

241 242
#ifndef parser_wrap
int parser_wrap(void)
243 244 245 246 247
{
	return 1;
}
#endif

248
struct keyword {
249 250
	const char *kw;
	int token;
251
	int winrt_only : 1;
252 253
};

254
/* This table MUST be alphabetically sorted on the kw field */
255
static const struct keyword keywords[] = {
256 257 258 259 260 261 262 263 264 265 266 267 268 269
	{"FALSE",           tFALSE,          0},
	{"NULL",            tNULL,           0},
	{"TRUE",            tTRUE,           0},
	{"__cdecl",         tCDECL,          0},
	{"__fastcall",      tFASTCALL,       0},
	{"__int32",         tINT32,          0},
	{"__int3264",       tINT3264,        0},
	{"__int64",         tINT64,          0},
	{"__pascal",        tPASCAL,         0},
	{"__stdcall",       tSTDCALL,        0},
	{"_cdecl",          tCDECL,          0},
	{"_fastcall",       tFASTCALL,       0},
	{"_pascal",         tPASCAL,         0},
	{"_stdcall",        tSTDCALL,        0},
270
	{"apicontract",     tAPICONTRACT,    1},
271 272 273 274 275 276 277 278
	{"boolean",         tBOOLEAN,        0},
	{"byte",            tBYTE,           0},
	{"case",            tCASE,           0},
	{"cdecl",           tCDECL,          0},
	{"char",            tCHAR,           0},
	{"coclass",         tCOCLASS,        0},
	{"const",           tCONST,          0},
	{"cpp_quote",       tCPPQUOTE,       0},
279
	{"declare",         tDECLARE,        1},
280
	{"default",         tDEFAULT,        0},
281
	{"delegate",        tDELEGATE,       1},
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	{"dispinterface",   tDISPINTERFACE,  0},
	{"double",          tDOUBLE,         0},
	{"enum",            tENUM,           0},
	{"error_status_t",  tERRORSTATUST,   0},
	{"extern",          tEXTERN,         0},
	{"float",           tFLOAT,          0},
	{"handle_t",        tHANDLET,        0},
	{"hyper",           tHYPER,          0},
	{"import",          tIMPORT,         0},
	{"importlib",       tIMPORTLIB,      0},
	{"inline",          tINLINE,         0},
	{"int",             tINT,            0},
	{"interface",       tINTERFACE,      0},
	{"library",         tLIBRARY,        0},
	{"long",            tLONG,           0},
	{"methods",         tMETHODS,        0},
	{"module",          tMODULE,         0},
	{"namespace",       tNAMESPACE,      1},
	{"pascal",          tPASCAL,         0},
	{"properties",      tPROPERTIES,     0},
	{"register",        tREGISTER,       0},
303
	{"requires",        tREQUIRES,       1},
304
	{"runtimeclass",    tRUNTIMECLASS,   1},
305 306 307 308 309 310 311 312 313 314 315 316 317
	{"short",           tSHORT,          0},
	{"signed",          tSIGNED,         0},
	{"sizeof",          tSIZEOF,         0},
	{"small",           tSMALL,          0},
	{"static",          tSTATIC,         0},
	{"stdcall",         tSTDCALL,        0},
	{"struct",          tSTRUCT,         0},
	{"switch",          tSWITCH,         0},
	{"typedef",         tTYPEDEF,        0},
	{"union",           tUNION,          0},
	{"unsigned",        tUNSIGNED,       0},
	{"void",            tVOID,           0},
	{"wchar_t",         tWCHAR,          0},
318
};
319
#define NKEYWORDS (sizeof(keywords)/sizeof(keywords[0]))
320

321 322 323
/* keywords only recognized in attribute lists
 * This table MUST be alphabetically sorted on the kw field
 */
324 325
static const struct keyword attr_keywords[] =
{
326
	{"activatable",                 tACTIVATABLE,               1},
327
	{"aggregatable",                tAGGREGATABLE,              0},
328
	{"agile",                       tAGILE,                     1},
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
	{"all_nodes",                   tALLNODES,                  0},
	{"allocate",                    tALLOCATE,                  0},
	{"annotation",                  tANNOTATION,                0},
	{"apartment",                   tAPARTMENT,                 0},
	{"appobject",                   tAPPOBJECT,                 0},
	{"async",                       tASYNC,                     0},
	{"async_uuid",                  tASYNCUUID,                 0},
	{"auto_handle",                 tAUTOHANDLE,                0},
	{"bindable",                    tBINDABLE,                  0},
	{"both",                        tBOTH,                      0},
	{"broadcast",                   tBROADCAST,                 0},
	{"byte_count",                  tBYTECOUNT,                 0},
	{"call_as",                     tCALLAS,                    0},
	{"callback",                    tCALLBACK,                  0},
	{"code",                        tCODE,                      0},
	{"comm_status",                 tCOMMSTATUS,                0},
	{"context_handle",              tCONTEXTHANDLE,             0},
	{"context_handle_noserialize",  tCONTEXTHANDLENOSERIALIZE,  0},
	{"context_handle_serialize",    tCONTEXTHANDLENOSERIALIZE,  0},
348
	{"contract",                    tCONTRACT,                  1},
349
	{"contractversion",             tCONTRACTVERSION,           1},
350
	{"control",                     tCONTROL,                   0},
351
	{"custom",                      tCUSTOM,                    0},
352 353 354 355 356 357 358 359 360 361 362 363 364 365
	{"decode",                      tDECODE,                    0},
	{"defaultbind",                 tDEFAULTBIND,               0},
	{"defaultcollelem",             tDEFAULTCOLLELEM,           0},
	{"defaultvalue",                tDEFAULTVALUE,              0},
	{"defaultvtable",               tDEFAULTVTABLE,             0},
	{"disable_consistency_check",   tDISABLECONSISTENCYCHECK,   0},
	{"displaybind",                 tDISPLAYBIND,               0},
	{"dllname",                     tDLLNAME,                   0},
	{"dont_free",                   tDONTFREE,                  0},
	{"dual",                        tDUAL,                      0},
	{"enable_allocate",             tENABLEALLOCATE,            0},
	{"encode",                      tENCODE,                    0},
	{"endpoint",                    tENDPOINT,                  0},
	{"entry",                       tENTRY,                     0},
366 367
	{"eventadd",                    tEVENTADD,                  1},
	{"eventremove",                 tEVENTREMOVE,               1},
368
	{"exclusiveto",                 tEXCLUSIVETO,               1},
369 370
	{"explicit_handle",             tEXPLICITHANDLE,            0},
	{"fault_status",                tFAULTSTATUS,               0},
371
	{"flags",                       tFLAGS,                     1},
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	{"force_allocate",              tFORCEALLOCATE,             0},
	{"free",                        tFREE,                      0},
	{"handle",                      tHANDLE,                    0},
	{"helpcontext",                 tHELPCONTEXT,               0},
	{"helpfile",                    tHELPFILE,                  0},
	{"helpstring",                  tHELPSTRING,                0},
	{"helpstringcontext",           tHELPSTRINGCONTEXT,         0},
	{"helpstringdll",               tHELPSTRINGDLL,             0},
	{"hidden",                      tHIDDEN,                    0},
	{"id",                          tID,                        0},
	{"idempotent",                  tIDEMPOTENT,                0},
	{"ignore",                      tIGNORE,                    0},
	{"iid_is",                      tIIDIS,                     0},
	{"immediatebind",               tIMMEDIATEBIND,             0},
	{"implicit_handle",             tIMPLICITHANDLE,            0},
	{"in",                          tIN,                        0},
	{"in_line",                     tIN_LINE,                   0},
	{"input_sync",                  tINPUTSYNC,                 0},
	{"lcid",                        tLCID,                      0},
	{"length_is",                   tLENGTHIS,                  0},
	{"licensed",                    tLICENSED,                  0},
	{"local",                       tLOCAL,                     0},
394
	{"marshaling_behavior",         tMARSHALINGBEHAVIOR,        1},
395 396
	{"maybe",                       tMAYBE,                     0},
	{"message",                     tMESSAGE,                   0},
397
	{"mta" ,                        tMTA,                       0},
398 399 400 401
	{"neutral",                     tNEUTRAL,                   0},
	{"nocode",                      tNOCODE,                    0},
	{"nonbrowsable",                tNONBROWSABLE,              0},
	{"noncreatable",                tNONCREATABLE,              0},
402
	{"none",                        tNONE,                      1},
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	{"nonextensible",               tNONEXTENSIBLE,             0},
	{"notify",                      tNOTIFY,                    0},
	{"notify_flag",                 tNOTIFYFLAG,                0},
	{"object",                      tOBJECT,                    0},
	{"odl",                         tODL,                       0},
	{"oleautomation",               tOLEAUTOMATION,             0},
	{"optimize",                    tOPTIMIZE,                  0},
	{"optional",                    tOPTIONAL,                  0},
	{"out",                         tOUT,                       0},
	{"partial_ignore",              tPARTIALIGNORE,             0},
	{"pointer_default",             tPOINTERDEFAULT,            0},
	{"progid",                      tPROGID,                    0},
	{"propget",                     tPROPGET,                   0},
	{"propput",                     tPROPPUT,                   0},
	{"propputref",                  tPROPPUTREF,                0},
	{"proxy",                       tPROXY,                     0},
	{"ptr",                         tPTR,                       0},
	{"public",                      tPUBLIC,                    0},
	{"range",                       tRANGE,                     0},
	{"readonly",                    tREADONLY,                  0},
	{"ref",                         tREF,                       0},
	{"represent_as",                tREPRESENTAS,               0},
	{"requestedit",                 tREQUESTEDIT,               0},
	{"restricted",                  tRESTRICTED,                0},
	{"retval",                      tRETVAL,                    0},
	{"single",                      tSINGLE,                    0},
	{"single_node",                 tSINGLENODE,                0},
	{"size_is",                     tSIZEIS,                    0},
	{"source",                      tSOURCE,                    0},
432
	{"standard",                    tSTANDARD,                  1},
433
	{"static",                      tSTATIC,                    1},
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
	{"strict_context_handle",       tSTRICTCONTEXTHANDLE,       0},
	{"string",                      tSTRING,                    0},
	{"switch_is",                   tSWITCHIS,                  0},
	{"switch_type",                 tSWITCHTYPE,                0},
	{"threading",                   tTHREADING,                 0},
	{"transmit_as",                 tTRANSMITAS,                0},
	{"uidefault",                   tUIDEFAULT,                 0},
	{"unique",                      tUNIQUE,                    0},
	{"user_marshal",                tUSERMARSHAL,               0},
	{"usesgetlasterror",            tUSESGETLASTERROR,          0},
	{"uuid",                        tUUID,                      0},
	{"v1_enum",                     tV1ENUM,                    0},
	{"vararg",                      tVARARG,                    0},
	{"version",                     tVERSION,                   0},
	{"vi_progid",                   tVIPROGID,                  0},
	{"wire_marshal",                tWIREMARSHAL,               0},
450 451
};

452 453 454 455 456 457
/* attributes TODO:
    first_is
    last_is
    max_is
    min_is
*/
458

459
#define KWP(p) ((const struct keyword *)(p))
460 461 462 463 464

static int kw_cmp_func(const void *s1, const void *s2)
{
	return strcmp(KWP(s1)->kw, KWP(s2)->kw);
}
465 466 467

static int kw_token(const char *kw)
{
468 469 470
	struct keyword key, *kwp;
	key.kw = kw;
	kwp = bsearch(&key, keywords, NKEYWORDS, sizeof(keywords[0]), kw_cmp_func);
471
	if (kwp && (!kwp->winrt_only || winrt_mode)) {
472
		parser_lval.str = xstrdup(kwp->kw);
473 474
		return kwp->token;
	}
475
	parser_lval.str = xstrdup(kw);
476
	return is_type(kw) ? aKNOWNTYPE : aIDENTIFIER;
477 478
}

479 480 481 482 483 484
static int attr_token(const char *kw)
{
        struct keyword key, *kwp;
        key.kw = kw;
        kwp = bsearch(&key, attr_keywords, sizeof(attr_keywords)/sizeof(attr_keywords[0]),
                      sizeof(attr_keywords[0]), kw_cmp_func);
485
        if (kwp && (!kwp->winrt_only || winrt_mode)) {
486 487 488 489 490 491
            parser_lval.str = xstrdup(kwp->kw);
            return kwp->token;
        }
        return kw_token(kw);
}

492 493 494 495 496 497 498
static void addcchar(char c)
{
	if(cbufidx >= cbufalloc)
	{
		cbufalloc += 1024;
		cbuffer = xrealloc(cbuffer, cbufalloc * sizeof(cbuffer[0]));
		if(cbufalloc > 65536)
499
			parser_warning("Reallocating string buffer larger than 64kB\n");
500 501 502 503 504 505 506 507 508 509
	}
	cbuffer[cbufidx++] = c;
}

static char *get_buffered_cstring(void)
{
	addcchar(0);
	return xstrdup(cbuffer);
}

510
void pop_import(void)
511 512 513 514 515 516 517 518 519 520 521
{
	int ptr = import_stack_ptr-1;

	fclose(yyin);
	yy_delete_buffer( YY_CURRENT_BUFFER );
	yy_switch_to_buffer( import_stack[ptr].state );
	if (temp_name) {
		unlink(temp_name);
		free(temp_name);
	}
	temp_name = import_stack[ptr].temp_name;
522 523
	input_name = import_stack[ptr].input_name;
	line_number = import_stack[ptr].line_number;
524 525 526 527
	import_stack_ptr--;
}

struct imports {
528 529
    char *name;
    struct imports *next;
530 531
} *first_import;

532
int do_import(char *fname)
533
{
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    FILE *f;
    char *path, *name;
    struct imports *import;
    int ptr = import_stack_ptr;
    int ret, fd;

    import = first_import;
    while (import && strcmp(import->name, fname))
        import = import->next;
    if (import) return 0; /* already imported */

    import = xmalloc(sizeof(struct imports));
    import->name = xstrdup(fname);
    import->next = first_import;
    first_import = import;

    /* don't search for a file name with a path in the include directories,
     * for compatibility with MIDL */
    if (strchr( fname, '/' ) || strchr( fname, '\\' ))
        path = xstrdup( fname );
    else if (!(path = wpp_find_include( fname, input_name )))
        error_loc("Unable to open include file %s\n", fname);
556

557 558 559
    if (import_stack_ptr == MAX_IMPORT_DEPTH)
        error_loc("Exceeded max import depth\n");

560 561 562 563 564 565
    import_stack[ptr].temp_name = temp_name;
    import_stack[ptr].input_name = input_name;
    import_stack[ptr].line_number = line_number;
    import_stack_ptr++;
    input_name = path;
    line_number = 1;
566

567 568 569
    name = xstrdup( "widl.XXXXXX" );
    if((fd = mkstemps( name, 0 )) == -1)
        error("Could not generate a temp name from %s\n", name);
570

571 572 573
    temp_name = name;
    if (!(f = fdopen(fd, "wt")))
        error("Could not open fd %s for writing\n", name);
574

575 576 577
    ret = wpp_parse( path, f );
    fclose( f );
    if (ret) exit(1);
578

579 580
    if((f = fopen(temp_name, "r")) == NULL)
        error_loc("Unable to open %s\n", temp_name);
581

582 583 584
    import_stack[ptr].state = YY_CURRENT_BUFFER;
    yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
    return 1;
585 586 587 588 589 590 591 592 593
}

void abort_import(void)
{
	int ptr;

	for (ptr=0; ptr<import_stack_ptr; ptr++)
		unlink(import_stack[ptr].temp_name);
}
594

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
static void switch_to_acf(void)
{
    int ptr = import_stack_ptr;
    int ret, fd;
    char *name;
    FILE *f;

    assert(import_stack_ptr == 0);

    input_name = acf_name;
    acf_name = NULL;
    line_number = 1;

    name = xstrdup( "widl.XXXXXX" );
    if((fd = mkstemps( name, 0 )) == -1)
        error("Could not generate a temp name from %s\n", name);

    temp_name = name;
    if (!(f = fdopen(fd, "wt")))
        error("Could not open fd %s for writing\n", name);

    ret = wpp_parse(input_name, f);
    fclose(f);
    if (ret) exit(1);

    if((f = fopen(temp_name, "r")) == NULL)
        error_loc("Unable to open %s\n", temp_name);

    import_stack[ptr].state = YY_CURRENT_BUFFER;
    yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
}

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
static void warning_disable(int warning)
{
    warning_t *warning_entry;
    LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
        if(warning_entry->num == warning)
            return;
    warning_entry = xmalloc( sizeof(*warning_entry) );
    warning_entry->num = warning;
    list_add_tail(disabled_warnings, &warning_entry->entry);
}

static void warning_enable(int warning)
{
    warning_t *warning_entry;
    LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
        if(warning_entry->num == warning)
        {
            list_remove(&warning_entry->entry);
            free(warning_entry);
            break;
        }
}

650
int do_warning(const char *toggle, warning_list_t *wnum)
651 652 653 654 655 656 657 658 659 660 661 662
{
    warning_t *warning, *next;
    int ret = 1;
    if(!disabled_warnings)
    {
        disabled_warnings = xmalloc( sizeof(*disabled_warnings) );
        list_init( disabled_warnings );
    }

    if(!strcmp(toggle, "disable"))
        LIST_FOR_EACH_ENTRY(warning, wnum, warning_t, entry)
            warning_disable(warning->num);
663
    else if(!strcmp(toggle, "enable") || !strcmp(toggle, "default"))
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
        LIST_FOR_EACH_ENTRY(warning, wnum, warning_t, entry)
            warning_enable(warning->num);
    else
        ret = 0;

    LIST_FOR_EACH_ENTRY_SAFE(warning, next, wnum, warning_t, entry)
        free(warning);
    return ret;
}

int is_warning_enabled(int warning)
{
    warning_t *warning_entry;
    if(!disabled_warnings)
        return 1;
    LIST_FOR_EACH_ENTRY(warning_entry, disabled_warnings, warning_t, entry)
        if(warning_entry->num == warning)
            return 0;
    return 1;
}