/*
 *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 *%	  Copyright (C) 1991, by WATCOM Systems Inc. All rights     %
 *%	  reserved. No part of this software may be reproduced	    %
 *%	  in any form or by any means - graphic, electronic or	    %
 *%	  mechanical, including photocopying, recording, taping     %
 *%	  or information storage and retrieval systems - except     %
 *%	  with the written permission of WATCOM Systems Inc.	    %
 *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  Modified:	By:		Reason:
  ---------	---		-------
  91/07/03	G. Coschi	initial implementation
------------------------------------------------- Version 9.0 -------
  92/08/04	D.J.Gaudet	make argv[0] the path to the executable
  92/10/26	Greg Bentz	make InitRtns() come after InitThreadProc
  92/11/07	F.W.Crigger	cleaned up thread support
  93/02/16	G. Coschi	__OS2Init() takes argument indicating if in
  				a DLL
--------------------------------------------------------Version-11.0---------
  01-jun-95	Greg Bentz	variety.h and dll linkage
  15-oct-95	Greg Bentz	modified for rtdll startup
*/

#include "variety.h"
#include "widechar.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define INCL_DOSPROCESS
#define INCL_DOSEXCEPTIONS
#include <wos2.h>
#include "sigtab.h"
#include "initfini.h"
#include "thread.h"

#ifdef __SW_BR
    _WCRTLINK extern	unsigned    __hmodule;
    _WCRTLINK extern	void	    (*__process_fini)(unsigned,unsigned);
    _WCRTLINK extern	int	    ___Argc;	/* argument count */
    _WCRTLINK extern	char	    **___Argv;	/* argument vector */
    _WCRTLINK extern	int	    ___wArgc;	/* argument count */
    _WCRTLINK extern	wchar_t	    **___wArgv;	/* argument vector */

    extern	void	__CommonInit( void );
    extern	int	main( int, char ** );
    extern	int	wmain( int, wchar_t ** );
#else
    extern	void	__OS2MainInit( EXCEPTIONREGISTRATIONRECORD *,
				       void *, unsigned, char *,
				       char * );
    #ifdef __WIDECHAR__
	extern	void	__wCMain( void );
	#if defined(_M_IX86)
	    #pragma aux __wCMain   "*"
	#endif
    #else
	extern	void	__CMain( void );
	#if defined(_M_IX86)
	    #pragma aux __CMain   "*"
	#endif
    #endif
    extern	unsigned	__ThreadDataSize;
#endif

void __F_NAME(__OS2Main,__wOS2Main)( unsigned hmod, unsigned reserved,
/********************************************************************/
    char *env, char *cmd )
{
    EXCEPTIONREGISTRATIONRECORD	xcpt;
    reserved = reserved;
    #ifdef __SW_BR
	__hmodule = hmod;
	env = env;
	cmd = cmd;
	__XCPTHANDLER = &xcpt;
	__process_fini = &__FiniRtns;
	__InitRtns( 255 );
	__CommonInit();
	#ifdef __WIDECHAR__
	    exit( wmain( ___wArgc, ___wArgv ) );
	#else
	    exit( main( ___Argc, ___Argv ) );
	#endif
    #else
    {
	thread_data	*tdata;

	__InitRtns( 1 );

	tdata = __alloca( __ThreadDataSize );
	memset( tdata, 0, __ThreadDataSize );
	// tdata->__allocated = 0;
	tdata->__data_size = __ThreadDataSize;

	__OS2MainInit( &xcpt, tdata, hmod, env, cmd );
	__F_NAME(__CMain,__wCMain)();
    }
    #endif
}
#if defined(_M_IX86)
    #ifdef __WIDECHAR__
	#pragma aux __wOS2Main "*" parm caller []
    #else
	#pragma aux __OS2Main "*" parm caller []
    #endif
#endif
