/*
 *  MODULE  : p88.c
 *  main program of the PASCAL-XSC to C compiler
{*  last modification:	19.08.92  16:14   }

 *  uli 06.03.91 : new style headers.
 *  uli 06.03.91 : ERRMESS changed to ERRmess
 *  uli 06.03.91 : del_extension replaced by prognam
 *  uli 07.03.91 : CHECK replaced by SYMCPRINT
 *  uli 08.03.91 : ERRmess changed to errfnam()
 *  uli 06.05.91 : on_verbose or not os_verbose
 *  uli 13.05.91 : constant pool will be dumped.
 *  uli 14.06.91 : (on_verbose or not os_verbose)
 *  uli 15.07.91 : Compiling stdin ...
 *  uli 26.11.91 : Copyright added.
 *  uli 14.01.92 : No prototype for exit.
 *  uli 04.02.92 : TIMER requested.
 *  PJ  03.08.92 : Prompt for DEMOVERSION added. Copyright string updated.
 *  PJ  19.08.92 : Prompt for DEMOVERSION extended.
*/

/* #define SYMCPRINT   /* forces test output */

#include "version.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include           "impor.t"
#include "sysutil.h"
#include "error.h"         /* declaration of errcount */
#include "option.h"
#include "scan.h"
#include "symtab.h"
#include "idtab.h"         /* idinit */
#include "optab.h"
#include "blktab.h"
#include "bintab.h"
#include "generate.h"
#include "module.h"
#include "std.h"           /* stdinit */


#ifdef LINT_ARGS
global boolean pass1 (void);
/* global void    exit (int retcode) ; */
#else
global boolean pass1();
#endif

#include           "implemen.t"


#ifdef LINT_ARGS
int main(int argc, char **argv)
#else
int main(argc,argv)
  int argc;
  char **argv;
#endif
BEGIN(main)
  char * filename, * envname ;

#ifdef TIMER
  total_in ();
  time_in (0);
  time_in (1);
#endif

 errcount = mescount = 0 ;
 lstfile  = stdout;                      /* direct output file to standard */
 envname = setdefault ();                      /* set default options */

 filename = readoptions (argc, argv) ;                /* read command line */
 in_terminal = not filename ;

 if ( envname  and  ( on_verbose or not os_verbose ) )
    printf(" environment file %s found.\n", envname);

 fnaminit (filename);

 remove (errfnam());

 initmoduniquename ();

 if (os_list) {
   lstfile = fopen(lstfnam(),"w");
   if (not(lstfile)) {
     lstfile = stdout;
     print ("not able to create listing-file ");
     print (lstfnam());
     printline ();
   }
 }


/* PJ 03.08.92, 19.08.92 : */

#ifndef DEMOVERSION
 if (on_verbose or not os_verbose) then
  begin
#endif
#ifdef DEMOVERSION
   printf( "Compiling %s with PASCAL-XSC demo version\n"
            , in_terminal ? "stdin" : srcfnam( ) );
   printf( "   for MS-DOS computers with BORLAND C++ compiler\n" );
   printf( "based on version %s\n", VERSION );
#else
   printf("Compiling %s with PASCAL-XSC version %s\n",
       in_terminal ? "stdin" : srcfnam() , VERSION);
#endif
   printf("(C) Copyright University of Karlsruhe 1991-1994\n");
#ifdef DEMOVERSION
   putchar( '\n' );
   printf( "Full compiler versions are available from :\n" );
   printf( "        Numerik Software GmbH\n" );
   printf( "        P.O. Box 2232\n" );
   printf( "        D-76492 Baden-Baden\n" );
   printf( "        Germany\n\n" );
#endif
#ifndef DEMOVERSION
  end
#endif

/* END PJ 03.08.92, 19.08.92 */

 opensrc (srcfnam());
 prognam();  /* deletes the extension in the variables name and filename */

 scaninit () ;

#ifdef SYMCPRINT   /* write symc values */
 begin int i ;
 for (i=cNosym; i<=cHighest; i++) printf ("%3d %s\n", i, symptext[i]) ;
 end ;
 exit (0) ;
#endif

 syminit () ;
 idinit () ;
 stdinit () ;
 opinit () ;

 remove (lnkfnam());            /* prevents a linker call */
 if (on_remove) {               /* remove all output files */
    remove (codfnam());
    remove (dclfnam());
 }
#if INTERMEDIATE
 geninit();
#endif

 prognam () ; /* deletes the extension in the variables name and filename */

 (void) pass1 () ;      /* call PASS1, uses global filename if module */

 if (on_dump)
  begin
    print (" Constant Pool is") ;
    if  (bincpool)  then
     begin
       while  (binfather(bincpool))   bincpool = binfather (bincpool) ;
       printline () ;
       dump (bincpool) ;
     end
    else  print (" empty.") ;
    printline () ;

    dump (binprev) ;
  end

 closesrc () ;

 if (errcount > 0) then
   printf ("%d errors found\n", errcount) ;
 if (on_verbose or not os_verbose) then
  begin
    if  (not on_warning and mescount-errcount > 0)  then
      printf ("%d warnings suppressed\n", mescount-errcount) ;
    printf ("Analysis complete\n") ;
  end ;

#if INTERMEDIATE
 genfin();
 if (on_remove and not (os_code ? on_code : errcount==0) )
    remove (codfnam());

#else
 if (on_terminal or (os_code ? on_code : errcount==0) ) {
    pos.line = 0 ; pos.col = 1 ;
    geninit ();
    generate (binprev);
    genfin();
    if (on_verbose or not os_verbose)
      printf ("Code generation complete\n");
  }
#endif

  finish (errcount) ;
  return errcount ; /* cannot be reached */

END /*main*/

