/*
 * IDL Compiler
 *
 * Copyright 2002 Ove Kaaven
 * based on WRC code by Bertho Stultiens
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <signal.h>

#define WIDL_FULLVERSION "0.1"

#include "widl.h"
#include "utils.h"
#include "preproc.h"
#include "parser.h"
#include "proxy.h"

/* future options to reserve characters for: */
/* a = alignment of structures */
/* A = ACF input filename */
/* c = client stub only? */
/* C = client stub filename */
/* J = do not search standard include path */
/* O = generate interpreted stubs */
/* p = proxy only? */
/* P = proxy filename */
/* s = server stub only? */
/* S = server stub filename */
/* u = UUID file only? */
/* U = UUID filename */
/* w = select win16/win32 output (?) */

static char usage[] =
"Usage: widl [options...] infile.idl\n"
"   -b          Make headers compatible with ICOM macros\n"
"   -B          Make headers use ICOM macros\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 only\n"
"   -H file     Name of header file (default is infile.h)\n"
"   -I path     Set include search dir to path (multiple -I allowed)\n"
"   -N          Do not preprocess input\n"
"   -t          Generate typelib only\n"
"   -T file     Name of typelib file (default is infile.tlb)\n"
"   -V          Print version and exit\n"
"   -W          Enable pedantic warnings\n"
"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"
;

char version_string[] = "Wine IDL Compiler Version " WIDL_FULLVERSION "\n"
			"Copyright 2002 Ove Kaaven\n";

int win32 = 1;
int debuglevel = DEBUGLEVEL_NONE;

int pedantic = 0;
int do_everything = 1;
int preprocess_only = 0;
int header_only = 0;
int typelib_only = 0;
int no_preprocess = 0;
int compat_icom = 0;
int use_icom = 0;

char *input_name;
char *header_name;
char *header_token;
char *typelib_name;
char *proxy_name;
char *proxy_token;

int line_number = 1;
int char_number = 1;

FILE *header;
FILE *proxy;
FILE *temp_file = 0;

time_t now;

int getopt (int argc, char *const *argv, const char *optstring);
static void segvhandler(int sig);

static char *make_token(const char *name)
{
  char *token;
  char *slash;
  int i;

  slash = strrchr(name, '/');
  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;
}

int main(int argc,char *argv[])
{
  extern char* optarg;
  extern int   optind;
  int optc;
  int ret;

  signal(SIGSEGV, segvhandler);

  now = time(NULL);

  while((optc = getopt(argc, argv, "bBd:D:EhH:I:NtT:VW")) != EOF) {
    switch(optc) {
    case 'b':
      compat_icom = 1;
      break;
    case 'B':
      use_icom = 1;
      break;
    case 'd':
      debuglevel = strtol(optarg, NULL, 0);
      break;
    case 'D':
      add_cmdline_define(optarg);
      break;
    case 'E':
      do_everything = 0;
      preprocess_only = 1;
      break;
    case 'h':
      do_everything = 0;
      header_only = 1;
      break;
    case 'H':
      header_name = strdup(optarg);
      break;
    case 'I':
      add_include_path(optarg);
      break;
    case 'N':
      no_preprocess = 1;
      break;
    case 't':
      do_everything = 0;
      typelib_only = 1;
      break;
    case 'T':
      typelib_name = strdup(optarg);
      break;
    case 'V':
      printf(version_string);
      return 0;
    case 'W':
      pedantic = 1;
      break;
    default:
      fprintf(stderr, usage);
      return 1;
    }
  }

  if(optind < argc) {
    input_name = argv[optind];
  }
  else {
    fprintf(stderr, usage);
    return 1;
  }

  if(debuglevel)
  {
    setbuf(stdout,0);
    setbuf(stderr,0);
  }

  yydebug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
  yy_flex_debug = debuglevel & DEBUGLEVEL_TRACE ? 1 : 0;
  ppdebug = debuglevel & DEBUGLEVEL_PPTRACE ? 1 : 0;
  pp_flex_debug = debuglevel & DEBUGLEVEL_PPLEX ? 1 : 0;

  if (!header_name) {
    header_name = dup_basename(input_name, ".idl");
    strcat(header_name, ".h");
  }

  if (!typelib_name) {
    typelib_name = dup_basename(input_name, ".idl");
    strcat(typelib_name, ".tlb");
  }

  if (!proxy_name) {
    proxy_name = dup_basename(input_name, ".idl");
    proxy_token = xstrdup(proxy_name);
    strcat(proxy_name, "_p.c");
  }

  add_cmdline_define("__WIDL__");

  if (!no_preprocess) {
    chat("Starting preprocess");
    if(!(ppout = tmpfile ()))
      error("Could not create a temp-file\n");
    temp_file = ppout;
    if(!(ppin = fopen(input_name, "rb")))
      error("Could not open %s\n", input_name);
    fprintf(ppout, "# 1 \"%s\" 1\n", input_name ? input_name : "");
    ret = preproc_parse();
    fclose(ppin);
    if (ret)
      exit(1);

    yyin = ppout;
    if (fseek (yyin, 0L, SEEK_SET)) {
      fprintf(stderr, "Could not move to beginning of temporary file\n");
      return 1;
    }
  }
  else {
    if(!(yyin = fopen(input_name, "r"))) {
      fprintf(stderr, "Could not open %s for input\n", input_name);
      return 1;
    }
  }

  header_token = make_token(header_name);

  header = fopen(header_name, "w");
  fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", WIDL_FULLVERSION, input_name);
  fprintf(header, "#include <rpc.h>\n");
  fprintf(header, "#include <rpcndr.h>\n\n");
  fprintf(header, "#ifndef __WIDL_%s\n", header_token);
  fprintf(header, "#define __WIDL_%s\n", header_token);
  fprintf(header, "#ifdef __cplusplus\n");
  fprintf(header, "extern \"C\" {\n");
  fprintf(header, "#endif\n");

  ret = yyparse();

  fprintf(header, "#ifdef __cplusplus\n");
  fprintf(header, "}\n");
  fprintf(header, "#endif\n");
  fprintf(header, "#endif /* __WIDL_%s */\n", header_token);
  fclose(header);
  fclose(yyin);

  if(ret) {
    exit(1);
  }

  return 0;
}

static void segvhandler(int sig)
{
  fprintf(stderr, "\n%s:%d: Oops, segment violation\n", input_name, line_number);
  fflush(stdout);
  fflush(stderr);
  abort();
}
