widl.c 21.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * IDL Compiler
 *
 * Copyright 2002 Ove Kaaven
 * based on WRC code by Bertho Stultiens
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22
 */

#include "config.h"
23
#include "wine/port.h"
24

25
#include <errno.h>
26
#include <limits.h>
27 28
#include <stdio.h>
#include <stdlib.h>
29 30 31
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
32 33 34 35
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <signal.h>
36 37 38
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
39 40 41 42

#include "widl.h"
#include "utils.h"
#include "parser.h"
43
#include "wine/wpp.h"
44
#include "header.h"
45

46 47 48 49 50
/* future options to reserve characters for: */
/* A = ACF input filename */
/* J = do not search standard include path */
/* w = select win16/win32 output (?) */

51
static const char usage[] =
52
"Usage: widl [options...] infile.idl\n"
53
"   or: widl [options...] --dlldata-only name1 [name2...]\n"
54 55 56 57 58 59 60 61 62 63 64 65
"   -b arch            Set the target architecture\n"
"   -c                 Generate client stub\n"
"   -d n               Set debug level to 'n'\n"
"   -D id[=val]        Define preprocessor identifier id=val\n"
"   -E                 Preprocess only\n"
"   -h                 Generate headers\n"
"   -H file            Name of header file (default is infile.h)\n"
"   -I path            Set include search dir to path (multiple -I allowed)\n"
"   --local-stubs=file Write empty stubs for call_as/local methods to file\n"
"   -m32, -m64         Set the kind of typelib to build (Win32 or Win64)\n"
"   -N                 Do not preprocess input\n"
"   --oldnames         Use old naming conventions\n"
66
"   -o, --output=NAME  Set the output file name\n"
67
"   -Otype             Type of stubs to generate (-Os, -Oi, -Oif)\n"
68 69
"   -p                 Generate proxy\n"
"   --prefix-all=p     Prefix names of client stubs / server functions with 'p'\n"
70 71
"   --prefix-client=p  Prefix names of client stubs with 'p'\n"
"   --prefix-server=p  Prefix names of server functions with 'p'\n"
72
"   -r                 Generate registration script\n"
73 74 75 76 77 78 79
"   -s                 Generate server stub\n"
"   -t                 Generate typelib\n"
"   -u                 Generate interface identifiers file\n"
"   -V                 Print version and exit\n"
"   -W                 Enable pedantic warnings\n"
"   --win32            Only generate 32-bit code\n"
"   --win64            Only generate 64-bit code\n"
80 81
"   --win32-align n    Set win32 structure alignment to 'n'\n"
"   --win64-align n    Set win64 structure alignment to 'n'\n"
82 83 84 85 86 87 88 89 90
"Debug level 'n' is a bitmask with following meaning:\n"
"    * 0x01 Tell which resource is parsed (verbose mode)\n"
"    * 0x02 Dump internal structures\n"
"    * 0x04 Create a parser trace (yydebug=1)\n"
"    * 0x08 Preprocessor messages\n"
"    * 0x10 Preprocessor lex messages\n"
"    * 0x20 Preprocessor yacc trace\n"
;

91
static const char version_string[] = "Wine IDL Compiler version " PACKAGE_VERSION "\n"
92 93 94
			"Copyright 2002 Ove Kaaven\n";

int debuglevel = DEBUGLEVEL_NONE;
95
int parser_debug, yy_flex_debug;
96 97

int pedantic = 0;
98
int do_everything = 1;
99
static int preprocess_only = 0;
Huw Davies's avatar
Huw Davies committed
100 101 102
int do_header = 0;
int do_typelib = 0;
int do_proxies = 0;
103 104
int do_client = 0;
int do_server = 0;
105
int do_regscript = 0;
106
int do_idfile = 0;
107
int do_dlldata = 0;
108
static int no_preprocess = 0;
109
int old_names = 0;
110 111
int do_win32 = 1;
int do_win64 = 1;
112 113
int win32_packing = 8;
int win64_packing = 8;
114
static enum stub_mode stub_mode = MODE_Os;
115 116 117

char *input_name;
char *header_name;
118
char *local_stubs_name;
119
char *header_token;
120
char *typelib_name;
121
char *dlldata_name;
122
char *proxy_name;
123
char *proxy_token;
124 125 126 127
char *client_name;
char *client_token;
char *server_name;
char *server_token;
128 129
char *regscript_name;
char *regscript_token;
130 131
static char *idfile_name;
static char *idfile_token;
132
char *temp_name;
133 134
const char *prefix_client = "";
const char *prefix_server = "";
135 136 137

int line_number = 1;

138
static FILE *idfile;
139

140
unsigned int pointer_size = 0;
141
syskind_t typelib_kind = sizeof(void*) == 8 ? SYS_WIN64 : SYS_WIN32;
142

143 144
time_t now;

145 146
enum {
    OLDNAMES_OPTION = CHAR_MAX + 1,
147 148
    DLLDATA_OPTION,
    DLLDATA_ONLY_OPTION,
149
    LOCAL_STUBS_OPTION,
150 151
    PREFIX_ALL_OPTION,
    PREFIX_CLIENT_OPTION,
152 153
    PREFIX_SERVER_OPTION,
    WIN32_OPTION,
154 155 156
    WIN64_OPTION,
    WIN32_ALIGN_OPTION,
    WIN64_ALIGN_OPTION
157 158
};

159
static const char short_options[] =
160
    "b:cC:d:D:EhH:I:m:No:O:pP:rsS:tT:uU:VW";
161
static const struct option long_options[] = {
162 163 164 165
    { "dlldata", 1, NULL, DLLDATA_OPTION },
    { "dlldata-only", 0, NULL, DLLDATA_ONLY_OPTION },
    { "local-stubs", 1, NULL, LOCAL_STUBS_OPTION },
    { "oldnames", 0, NULL, OLDNAMES_OPTION },
166
    { "output", 0, NULL, 'o' },
167 168 169 170 171 172 173 174
    { "prefix-all", 1, NULL, PREFIX_ALL_OPTION },
    { "prefix-client", 1, NULL, PREFIX_CLIENT_OPTION },
    { "prefix-server", 1, NULL, PREFIX_SERVER_OPTION },
    { "win32", 0, NULL, WIN32_OPTION },
    { "win64", 0, NULL, WIN64_OPTION },
    { "win32-align", 1, NULL, WIN32_ALIGN_OPTION },
    { "win64-align", 1, NULL, WIN64_ALIGN_OPTION },
    { NULL, 0, NULL, 0 }
175 176
};

177 178
static void rm_tempfile(void);

179 180 181 182 183 184 185
enum stub_mode get_stub_mode(void)
{
    /* old-style interpreted stubs are not supported on 64-bit */
    if (stub_mode == MODE_Oi && pointer_size == 8) return MODE_Oif;
    return stub_mode;
}

186 187 188 189 190 191 192
static char *make_token(const char *name)
{
  char *token;
  char *slash;
  int i;

  slash = strrchr(name, '/');
193 194 195
  if(!slash)
    slash = strrchr(name, '\\');

196 197 198 199 200 201 202 203 204 205
  if (slash) name = slash + 1;

  token = xstrdup(name);
  for (i=0; token[i]; i++) {
    if (!isalnum(token[i])) token[i] = '_';
    else token[i] = toupper(token[i]);
  }
  return token;
}

206 207 208 209 210 211 212 213 214
/* duplicate a basename into a valid C token */
static char *dup_basename_token(const char *name, const char *ext)
{
    char *p, *ret = dup_basename( name, ext );
    /* map invalid characters to '_' */
    for (p = ret; *p; p++) if (!isalnum(*p)) *p = '_';
    return ret;
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
static void add_widl_version_define(void)
{
    unsigned int version;
    const char *p = PACKAGE_VERSION;

    /* major */
    version = atoi(p) * 0x10000;
    p = strchr(p, '.');

    /* minor */
    if (p)
    {
        version += atoi(p + 1) * 0x100;
        p = strchr(p + 1, '.');
    }

    /* build */
    if (p)
        version += atoi(p + 1);

    if (version != 0)
    {
        char version_str[11];
        snprintf(version_str, sizeof(version_str), "0x%x", version);
        wpp_add_define("__WIDL__", version_str);
    }
    else
        wpp_add_define("__WIDL__", NULL);
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258
/* set the target platform */
static void set_target( const char *target )
{
    static const struct
    {
        const char *name;
        syskind_t   kind;
    } cpu_names[] =
    {
        { "i386",    SYS_WIN32 },
        { "i486",    SYS_WIN32 },
        { "i586",    SYS_WIN32 },
        { "i686",    SYS_WIN32 },
        { "i786",    SYS_WIN32 },
259
        { "amd64",   SYS_WIN64 },
260 261 262
        { "x86_64",  SYS_WIN64 },
        { "sparc",   SYS_WIN32 },
        { "alpha",   SYS_WIN32 },
263 264
        { "powerpc", SYS_WIN32 },
        { "arm",     SYS_WIN32 }
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    };

    unsigned int i;
    char *p, *spec = xstrdup( target );

    /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */

    if (!(p = strchr( spec, '-' ))) error( "Invalid target specification '%s'\n", target );
    *p++ = 0;
    for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
    {
        if (!strcmp( cpu_names[i].name, spec ))
        {
            typelib_kind = cpu_names[i].kind;
            free( spec );
            return;
        }
    }
    error( "Unrecognized CPU '%s'\n", spec );
}

286 287 288 289 290 291
/* clean things up when aborting on a signal */
static void exit_on_signal( int sig )
{
    exit(1);  /* this will call the atexit functions */
}

292 293 294 295 296 297 298
static void set_everything(int x)
{
  do_header = x;
  do_typelib = x;
  do_proxies = x;
  do_client = x;
  do_server = x;
299
  do_regscript = x;
300
  do_idfile = x;
301 302 303
  do_dlldata = x;
}

304
void start_cplusplus_guard(FILE *fp)
305 306 307 308 309 310
{
  fprintf(fp, "#ifdef __cplusplus\n");
  fprintf(fp, "extern \"C\" {\n");
  fprintf(fp, "#endif\n\n");
}

311
void end_cplusplus_guard(FILE *fp)
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
{
  fprintf(fp, "#ifdef __cplusplus\n");
  fprintf(fp, "}\n");
  fprintf(fp, "#endif\n\n");
}

typedef struct
{
  char *filename;
  struct list link;
} filename_node_t;

static void add_filename_node(struct list *list, const char *name)
{
  filename_node_t *node = xmalloc(sizeof *node);
327
  node->filename = dup_basename( name, ".idl" );
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
  list_add_tail(list, &node->link);
}

static void free_filename_nodes(struct list *list)
{
  filename_node_t *node, *next;
  LIST_FOR_EACH_ENTRY_SAFE(node, next, list, filename_node_t, link) {
    list_remove(&node->link);
    free(node->filename);
    free(node);
  }
}

static void write_dlldata_list(struct list *filenames)
{
  FILE *dlldata;
  filename_node_t *node;

  dlldata = fopen(dlldata_name, "w");
  if (!dlldata)
    error("couldn't open %s: %s\n", dlldata_name, strerror(errno));

  fprintf(dlldata, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
  fprintf(dlldata, "- Do not edit ***/\n\n");
  fprintf(dlldata, "#include <objbase.h>\n");
  fprintf(dlldata, "#include <rpcproxy.h>\n\n");
  start_cplusplus_guard(dlldata);

  LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
    fprintf(dlldata, "EXTERN_PROXY_FILE(%s)\n", node->filename);

  fprintf(dlldata, "\nPROXYFILE_LIST_START\n");
  fprintf(dlldata, "/* Start of list */\n");
  LIST_FOR_EACH_ENTRY(node, filenames, filename_node_t, link)
    fprintf(dlldata, "  REFERENCE_PROXY_FILE(%s),\n", node->filename);
  fprintf(dlldata, "/* End of list */\n");
  fprintf(dlldata, "PROXYFILE_LIST_END\n\n");

  fprintf(dlldata, "DLLDATA_ROUTINES(aProxyFileList, GET_DLL_CLSID)\n\n");
  end_cplusplus_guard(dlldata);
  fclose(dlldata);
}

static char *eat_space(char *s)
{
  while (isspace((unsigned char) *s))
    ++s;
  return s;
}

378
void write_dlldata(const statement_list_t *stmts)
379 380 381 382 383
{
  struct list filenames = LIST_INIT(filenames);
  filename_node_t *node;
  FILE *dlldata;

384
  if (!do_dlldata || !need_proxy_file(stmts))
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 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
    return;

  dlldata = fopen(dlldata_name, "r");
  if (dlldata) {
    static char marker[] = "REFERENCE_PROXY_FILE";
    char *line = NULL;
    size_t len = 0;

    while (widl_getline(&line, &len, dlldata)) {
      char *start, *end;
      start = eat_space(line);
      if (strncmp(start, marker, sizeof marker - 1) == 0) {
        start = eat_space(start + sizeof marker - 1);
        if (*start != '(')
          continue;
        end = start = eat_space(start + 1);
        while (*end && *end != ')')
          ++end;
        if (*end != ')')
          continue;
        while (isspace((unsigned char) end[-1]))
          --end;
        *end = '\0';
        if (start < end)
          add_filename_node(&filenames, start);
      }
    }

    if (ferror(dlldata))
      error("couldn't read from %s: %s\n", dlldata_name, strerror(errno));

    free(line);
    fclose(dlldata);
  }

  LIST_FOR_EACH_ENTRY(node, &filenames, filename_node_t, link)
    if (strcmp(proxy_token, node->filename) == 0) {
      /* We're already in the list, no need to regenerate this file.  */
      free_filename_nodes(&filenames);
      return;
    }

  add_filename_node(&filenames, proxy_token);
  write_dlldata_list(&filenames);
  free_filename_nodes(&filenames);
430 431
}

432 433 434 435 436 437 438 439
static void write_id_data_stmts(const statement_list_t *stmts)
{
  const statement_t *stmt;
  if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
  {
    if (stmt->type == STMT_TYPE)
    {
      const type_t *type = stmt->u.type;
440
      if (type_get_type(type) == TYPE_INTERFACE)
441 442
      {
        const UUID *uuid;
443
        if (!is_object(type) && !is_attr(type->attrs, ATTR_DISPINTERFACE))
444 445 446 447 448
          continue;
        uuid = get_attrp(type->attrs, ATTR_UUID);
        write_guid(idfile, is_attr(type->attrs, ATTR_DISPINTERFACE) ? "DIID" : "IID",
                   type->name, uuid);
      }
449
      else if (type_get_type(type) == TYPE_COCLASS)
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
      {
        const UUID *uuid = get_attrp(type->attrs, ATTR_UUID);
        write_guid(idfile, "CLSID", type->name, uuid);
      }
    }
    else if (stmt->type == STMT_LIBRARY)
    {
      const UUID *uuid = get_attrp(stmt->u.lib->attrs, ATTR_UUID);
      write_guid(idfile, "LIBID", stmt->u.lib->name, uuid);
      write_id_data_stmts(stmt->u.lib->stmts);
    }
  }
}

void write_id_data(const statement_list_t *stmts)
{
  if (!do_idfile) return;

  idfile_token = make_token(idfile_name);

  idfile = fopen(idfile_name, "w");
  if (! idfile) {
    error("Could not open %s for output\n", idfile_name);
    return;
  }

  fprintf(idfile, "/*** Autogenerated by WIDL %s ", PACKAGE_VERSION);
  fprintf(idfile, "from %s - Do not edit ***/\n\n", input_name);
  fprintf(idfile, "#include <rpc.h>\n");
  fprintf(idfile, "#include <rpcndr.h>\n\n");
  fprintf(idfile, "#include <initguid.h>\n\n");
  start_cplusplus_guard(idfile);

  write_id_data_stmts(stmts);

  fprintf(idfile, "\n");
  end_cplusplus_guard(idfile);

  fclose(idfile);
}

491 492 493
int main(int argc,char *argv[])
{
  int optc;
494
  int ret = 0;
495
  int opti = 0;
496
  char *output_name = NULL;
497

498 499 500 501 502
  signal( SIGTERM, exit_on_signal );
  signal( SIGINT, exit_on_signal );
#ifdef SIGHUP
  signal( SIGHUP, exit_on_signal );
#endif
503 504 505

  now = time(NULL);

506
  while((optc = getopt_long(argc, argv, short_options, long_options, &opti)) != EOF) {
507
    switch(optc) {
508 509 510 511 512 513 514
    case DLLDATA_OPTION:
      dlldata_name = xstrdup(optarg);
      break;
    case DLLDATA_ONLY_OPTION:
      do_everything = 0;
      do_dlldata = 1;
      break;
515 516 517 518
    case LOCAL_STUBS_OPTION:
      do_everything = 0;
      local_stubs_name = xstrdup(optarg);
      break;
519
    case OLDNAMES_OPTION:
520 521
      old_names = 1;
      break;
522 523 524 525 526 527 528 529 530 531
    case PREFIX_ALL_OPTION:
      prefix_client = xstrdup(optarg);
      prefix_server = xstrdup(optarg);
      break;
    case PREFIX_CLIENT_OPTION:
      prefix_client = xstrdup(optarg);
      break;
    case PREFIX_SERVER_OPTION:
      prefix_server = xstrdup(optarg);
      break;
532 533 534 535 536 537 538 539
    case WIN32_OPTION:
      do_win32 = 1;
      do_win64 = 0;
      break;
    case WIN64_OPTION:
      do_win32 = 0;
      do_win64 = 1;
      break;
540 541 542 543 544 545 546 547
    case WIN32_ALIGN_OPTION:
      win32_packing = strtol(optarg, NULL, 0);
      if(win32_packing != 2 && win32_packing != 4 && win32_packing != 8)
          error("Packing must be one of 2, 4 or 8\n");
      break;
    case WIN64_ALIGN_OPTION:
      win64_packing = strtol(optarg, NULL, 0);
      if(win64_packing != 2 && win64_packing != 4 && win64_packing != 8)
548 549
          error("Packing must be one of 2, 4 or 8\n");
      break;
550 551 552
    case 'b':
      set_target( optarg );
      break;
553 554 555 556 557
    case 'c':
      do_everything = 0;
      do_client = 1;
      break;
    case 'C':
558
      client_name = xstrdup(optarg);
559
      break;
560 561 562 563 564 565 566
    case 'd':
      debuglevel = strtol(optarg, NULL, 0);
      break;
    case 'D':
      wpp_add_cmdline_define(optarg);
      break;
    case 'E':
567
      do_everything = 0;
568 569
      preprocess_only = 1;
      break;
570
    case 'h':
571
      do_everything = 0;
Huw Davies's avatar
Huw Davies committed
572
      do_header = 1;
573
      break;
574
    case 'H':
575
      header_name = xstrdup(optarg);
576 577 578 579
      break;
    case 'I':
      wpp_add_include_path(optarg);
      break;
580 581 582 583 584
    case 'm':
      if (!strcmp( optarg, "32" )) typelib_kind = SYS_WIN32;
      else if (!strcmp( optarg, "64" )) typelib_kind = SYS_WIN64;
      else error( "Invalid -m argument '%s'\n", optarg );
      break;
585 586 587
    case 'N':
      no_preprocess = 1;
      break;
588 589 590
    case 'o':
      output_name = xstrdup(optarg);
      break;
591 592 593 594 595 596 597 598
    case 'O':
      if (!strcmp( optarg, "s" )) stub_mode = MODE_Os;
      else if (!strcmp( optarg, "i" )) stub_mode = MODE_Oi;
      else if (!strcmp( optarg, "ic" )) stub_mode = MODE_Oif;
      else if (!strcmp( optarg, "if" )) stub_mode = MODE_Oif;
      else if (!strcmp( optarg, "icf" )) stub_mode = MODE_Oif;
      else error( "Invalid argument '-O%s'\n", optarg );
      break;
599 600 601 602 603
    case 'p':
      do_everything = 0;
      do_proxies = 1;
      break;
    case 'P':
604
      proxy_name = xstrdup(optarg);
605
      break;
606 607 608 609
    case 'r':
      do_everything = 0;
      do_regscript = 1;
      break;
610 611 612 613 614
    case 's':
      do_everything = 0;
      do_server = 1;
      break;
    case 'S':
615
      server_name = xstrdup(optarg);
616
      break;
617 618
    case 't':
      do_everything = 0;
Huw Davies's avatar
Huw Davies committed
619
      do_typelib = 1;
620 621
      break;
    case 'T':
622
      typelib_name = xstrdup(optarg);
623
      break;
624 625 626 627 628
    case 'u':
      do_everything = 0;
      do_idfile = 1;
      break;
    case 'U':
629
      idfile_name = xstrdup(optarg);
630
      break;
631
    case 'V':
632
      printf("%s", version_string);
633 634 635 636 637
      return 0;
    case 'W':
      pedantic = 1;
      break;
    default:
638
      fprintf(stderr, "%s", usage);
639 640 641 642
      return 1;
    }
  }

Huw Davies's avatar
Huw Davies committed
643
  if(do_everything) {
644
    set_everything(TRUE);
Huw Davies's avatar
Huw Davies committed
645
  }
646

647 648 649
  if (!output_name) output_name = dup_basename(input_name, ".idl");

  if (!do_everything &&
650 651
      do_header + do_typelib + do_proxies + do_client +
      do_server + do_regscript + do_idfile + do_dlldata == 1)
652 653 654 655 656 657
  {
      if (do_header) header_name = output_name;
      else if (do_typelib) typelib_name = output_name;
      else if (do_proxies) proxy_name = output_name;
      else if (do_client) client_name = output_name;
      else if (do_server) server_name = output_name;
658
      else if (do_regscript) regscript_name = output_name;
659 660 661 662
      else if (do_idfile) idfile_name = output_name;
      else if (do_dlldata) dlldata_name = output_name;
  }

663 664 665
  if (!dlldata_name && do_dlldata)
    dlldata_name = xstrdup("dlldata.c");

666
  if(optind < argc) {
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    if (do_dlldata && !do_everything) {
      struct list filenames = LIST_INIT(filenames);
      for ( ; optind < argc; ++optind)
        add_filename_node(&filenames, argv[optind]);

      write_dlldata_list(&filenames);
      free_filename_nodes(&filenames);
      return 0;
    }
    else if (optind != argc - 1) {
      fprintf(stderr, "%s", usage);
      return 1;
    }
    else
      input_name = xstrdup(argv[optind]);
682 683
  }
  else {
684
    fprintf(stderr, "%s", usage);
685 686 687 688 689
    return 1;
  }

  if(debuglevel)
  {
690 691
    setbuf(stdout, NULL);
    setbuf(stderr, NULL);
692 693
  }

694
  parser_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
695 696 697 698 699 700
  yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;

  wpp_set_debug( (debuglevel & DEBUGLEVEL_PPLEX) != 0,
                 (debuglevel & DEBUGLEVEL_PPTRACE) != 0,
                 (debuglevel & DEBUGLEVEL_PPMSG) != 0 );

701
  if (!header_name) {
702 703 704 705
    header_name = dup_basename(input_name, ".idl");
    strcat(header_name, ".h");
  }

Huw Davies's avatar
Huw Davies committed
706
  if (!typelib_name && do_typelib) {
707 708 709 710
    typelib_name = dup_basename(input_name, ".idl");
    strcat(typelib_name, ".tlb");
  }

Huw Davies's avatar
Huw Davies committed
711
  if (!proxy_name && do_proxies) {
712 713 714 715
    proxy_name = dup_basename(input_name, ".idl");
    strcat(proxy_name, "_p.c");
  }

716 717 718 719 720 721 722 723 724 725
  if (!client_name && do_client) {
    client_name = dup_basename(input_name, ".idl");
    strcat(client_name, "_c.c");
  }

  if (!server_name && do_server) {
    server_name = dup_basename(input_name, ".idl");
    strcat(server_name, "_s.c");
  }

726 727 728 729 730
  if (!regscript_name && do_regscript) {
    regscript_name = dup_basename(input_name, ".idl");
    strcat(regscript_name, "_r.rgs");
  }

731 732 733 734 735
  if (!idfile_name && do_idfile) {
    idfile_name = dup_basename(input_name, ".idl");
    strcat(idfile_name, "_i.c");
  }

736 737 738
  if (do_proxies) proxy_token = dup_basename_token(proxy_name,"_p.c");
  if (do_client) client_token = dup_basename_token(client_name,"_c.c");
  if (do_server) server_token = dup_basename_token(server_name,"_s.c");
739
  if (do_regscript) regscript_token = dup_basename_token(regscript_name,"_r.rgs");
740

741
  add_widl_version_define();
742
  wpp_add_define("_WIN32", NULL);
743

744 745 746
  atexit(rm_tempfile);
  if (!no_preprocess)
  {
747
    chat("Starting preprocess\n");
748 749 750

    if (!preprocess_only)
    {
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
        FILE *output;
        int fd;
        char *name = xmalloc( strlen(header_name) + 8 );

        strcpy( name, header_name );
        strcat( name, ".XXXXXX" );

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

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

        ret = wpp_parse( input_name, output );
        fclose( output );
767 768 769 770 771 772 773 774
    }
    else
    {
        ret = wpp_parse( input_name, stdout );
    }

    if(ret) exit(1);
    if(preprocess_only) exit(0);
775
    if(!(parser_in = fopen(temp_name, "r"))) {
776 777 778
      fprintf(stderr, "Could not open %s for input\n", temp_name);
      return 1;
    }
779
  }
780
  else {
781
    if(!(parser_in = fopen(input_name, "r"))) {
782 783 784
      fprintf(stderr, "Could not open %s for input\n", input_name);
      return 1;
    }
785 786
  }

787
  header_token = make_token(header_name);
788

789
  init_types();
790
  ret = parser_parse();
791

792
  fclose(parser_in);
793 794 795 796

  if(ret) {
    exit(1);
  }
797 798 799

  /* Everything has been done successfully, don't delete any files.  */
  set_everything(FALSE);
800 801
  local_stubs_name = NULL;

802 803 804 805 806 807 808 809
  return 0;
}

static void rm_tempfile(void)
{
  abort_import();
  if(temp_name)
    unlink(temp_name);
810
  if (do_header)
811
    unlink(header_name);
812 813
  if (local_stubs_name)
    unlink(local_stubs_name);
814
  if (do_client)
815
    unlink(client_name);
816
  if (do_server)
817
    unlink(server_name);
818 819
  if (do_regscript)
    unlink(regscript_name);
820 821 822 823 824 825
  if (do_idfile)
    unlink(idfile_name);
  if (do_proxies)
    unlink(proxy_name);
  if (do_typelib)
    unlink(typelib_name);
826
}