#include "defines.h"
#include OSBIND
#include <stdio.h>
#include <string.h>
#define __BIG_GENERATOR__   1
#include "code.h"

lword    __lx;
word     __x;

#if OS == TOS
static char _usage[] =
"Usage: nasm [-{brqstvwx}][-a xx][-e #][-o binary][-h includedir] <source>\n";
#else
static char _usage[] =
"Usage: nasm [-{brqsvwx}][-a xx][-e #][-o binary][-h includedir] <source>\n";
#endif

void           *nmalloc();
char           *getenv(), 
               *mend_together();

void           finalshit(){}
void           h_print(){}
int            maxerrors = 1,
               tok_remain,
               tossable,
               wherewasit;

static char   infile[ 256],
              outfile[ 256],
              buffer[ 400],
              path[ 128];



main( argc, argv, envp)
char  *argv[];
char  **envp;
{
   char  *p, *q;
   int   i = 0;

   nwarning("This program is only rudimentary at best. USE NASM65");
   while( ++i < argc)
   {
      if( *argv[i] == '-')
         switch( Xtolower( argv[i][1]))
         {
            case 'o' :
            case 'a' :
            case 'd' :  /* maybe we should use the env. as well */
            case 'e' :
            case 'h' :
               ++i;

            case 'b' :
#if OS == TOS
# if WITHNOISY
            case 'n' :
# endif
            case 't' :
#endif            
            case 'q' :
            case 'r' :
            case 's' :
            case ':' :
            case 'v' :
            case 'w' :
            case 'x' :
               break;

            default  :
               goto usage;
         }
      else
      {
         if( infile[0])
            goto usage;
         wherewasit = i;
         strcpy( infile, argv[i]);
      }
   }
   if( ! infile[0])
      goto usage;

   if( ! (p = getenv( "CPP")))
   {
      if( ! (p = getenv( "BINDIR")))
         nferror("Don't know where to find \"cpp\"");
      strcpy( path, p);
      if( p[ (i = (int) strlen( p)) - 1] != '\\')
      {
         path[ i++] = '\\';
         path[ i]   = 0;
      }
#if OS == TOS
      strcat( p = path, "cpp.ttp");
#else
      strcat( p = path, "cpp");
#endif      
   }

   if( ! (q = getenv( "INC65")))
      q = "\\include";

   strcpy( outfile, infile);
   complete( outfile, ".i", 1);
#if OS == UNIX || AMIGA
   sprintf( buffer, "%s -o=%s -I%s %s", p, outfile, q, infile);
   if( i = execve( p, buffer, *envp))
      exit( 1);
#else
   sprintf( buffer, "-o=%s -I%s %s", outfile, q, infile);
   if( i = exec( p, buffer, *envp))
      exit( 1);
#endif



   if( ! (p = getenv( "NASM65")))
   {
      if( ! (p = getenv( "BINDIR")))
         nferror("Don't know where to find \"nasm65\"");
      strcpy( path, p);
      if( p[ (i = (int) strlen( p)) - 1] != '\\')
      {
         path[ i++] = '\\';
         path[ i]   = 0;
      }
#if OS == TOS
      strcat( p = path, "nasm65.ttp");
#else
      strcat( p = path, "nasm65");
#endif      
   }
#if OS == TOS
   i = exec( p, mend_together( p, argc, argv), *envp);
#else
   i = execve( p, mend_together( p, argc, argv), *envp);
#endif
   unlink( outfile);
   return( i);

usage:
   fputs( _usage, ESTREAM);
   fputs( "\
\t-x           : list unresolved references\n\
\t-w           : write output file anyway (W.T.F.)\n\
\t-h <path>    : \"system\" include file directory\n\
\t-o <file>    : output file specifier\n\
\t-r           : create runnable binary file\n\
\t-e <digit>   : set maximal number of errors before aborting\n\
\t-s           : show calling of macros and include files\n\
\t-d <l>[=<v>] : defines label <l>. optionally assigns <v> as value to it\n\
\t-a <value>   : alignment value (1->even 255->page)\n\
\t-q           : quiet flag\n\
\t-b           : optimize small segments\n", ESTREAM);
#if OS == TOS
   fputs( "\
\t-t           : wait for keypress before exit\n\
\t-n           : noisy tokens\n", ESTREAM);
#endif
   return( 1);
}



char  *mend_together( program, argc, argv)
char  *program;
int   argc;
char  *argv[];
{
   char     *p;
   int      i;
   size_t   len;

#if OS == UNIX
   len = strlen( program);
#else
   len = 0;
#endif
   for( i = 1; i < argc; i++)
      if( i == wherewasit)
         len += strlen( outfile);
      else
         len += strlen( argv[ i]);

   len += strlen( outfile) + 1 + 2 + argc + 2;
   p = nmalloc( len);
#if OS == UNIX
   strcpy( p, program);
#else
   *p = 0;
#endif
   for( i = 1; i < argc; i++)
   {
      if( i > 1)
         strcat( p, " ");
      if( i == wherewasit)
         strcat( p, outfile);
      else
         strcat( p, argv[ i]);
   }
   strcat( p, " -_ ");
   strcat( p, infile);
   return( p);
}
