Exemple #1
0
static int socket_error( SGS_CTX )
{
	int astext = 0, e = 0;
	SGSFN( "socket_error" );
	if( !sgs_LoadArgs( C, "|b", &astext ) )
		return 0;
	
	if( sgs_PushGlobal( C, SCKERRVN ) == SGS_SUCCESS )
		e = (int) sgs_GetInt( C, -1 );
	else if( !astext )
		sgs_PushInt( C, 0 );
	
	if( !astext )
		return 1;
	
#ifdef _WIN32
	{
		char buf[ 1024 ];
		DWORD numwr = FormatMessageA
		(
			FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD) e,
			MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
			buf, 1024, NULL
		);
		if( !numwr )
			STDLIB_WARN( "failed to retrieve error message" )
		sgs_PushStringBuf( C, buf, (sgs_SizeVal) numwr );
	}
#else
	sgs_PushString( C, strerror( e ) );
#endif
	return 1;
}
Exemple #2
0
static int sockaddr_dump( SGS_CTX, sgs_VarObj* data, int depth )
{
	char buf[ 32 ];
	sprintf( buf, "socket_address [family=%hu] ", (unsigned short) GET_SAF );
	sgs_PushString( C, buf );
	sgs_PushObjectPtr( C, data );
	sgs_StringConcat( C, 2 );
	return SGS_SUCCESS;
}
Exemple #3
0
static int sgs_socket_gethostname( SGS_CTX )
{
	char buf[ 256 ];
	SGSFN( "socket_gethostname" );
	if( !sgs_LoadArgs( C, "." ) )
		return 0;
	if( !sockassert( C, gethostname( buf, 256 ) == 0 ) )
		STDLIB_WARN( "failed to get host name" )
	buf[ 256-1 ] = 0;
	sgs_PushString( C, buf );
	return 1;
}
Exemple #4
0
static void sockaddr_push_full_addr_string( SGS_CTX, sgs_VarObj* data )
{
	char addr[ 64 ] = {0};
#ifdef _WIN32
	DWORD ioasz = 64;
	WSAAddressToString( (LPSOCKADDR) data->data, sizeof(struct sockaddr_storage), NULL, addr, &ioasz );
#else
	if( GET_SAF == AF_INET || GET_SAF == AF_INET6 )
	{
		char pb[ 8 ];
		inet_ntop( GET_SAF, GET_SAF == AF_INET ?
			(void*) &GET_SAI->sin_addr :
			(void*) &GET_SAI6->sin6_addr, addr, 64 );
		sprintf( pb, ":%u", (int) GET_SAF == AF_INET ? GET_SAI->sin_port : GET_SAI6->sin6_port );
		strcat( addr, pb );
	}
#endif
	addr[ 64-1 ] = 0;
	if( *addr )
		sgs_PushString( C, addr );
	else
		sgs_PushString( C, "-" );
}
Exemple #5
0
static int sgs_socket_getaddrinfo( SGS_CTX )
{
	sgs_StkIdx sz0, sz1;
	struct addrinfo hints, *list = NULL, *pp;
	char *addr, *servport;
	sgs_Int socktype = SOCK_STREAM, af = AF_UNSPEC;
	
	SGSFN( "socket_getaddrinfo" );
	/* address, port, socktype, address_family */
	if( !sgs_LoadArgs( C, "ss|ii", &addr, &servport, &socktype, &af ) )
		return 0;
	
	memset( &hints, 0, sizeof(hints) );
	hints.ai_socktype = (int) socktype;
	hints.ai_family = (int) af;
	
	if( !sockassert( C, getaddrinfo( addr, servport, &hints, &list ) == 0 ) )
		STDLIB_WARN( "failed to get address info" )
	
	pp = list;
	sz0 = sgs_StackSize( C );
	while( pp )
	{
		sz1 = sgs_StackSize( C );
		
		sgs_PushString( C, "flags" );
		sgs_PushInt( C, pp->ai_flags );
		sgs_PushString( C, "family" );
		sgs_PushInt( C, pp->ai_family );
		sgs_PushString( C, "socktype" );
		sgs_PushInt( C, pp->ai_socktype );
		sgs_PushString( C, "protocol" );
		sgs_PushInt( C, pp->ai_protocol );
		sgs_PushString( C, "canonname" );
		if( pp->ai_canonname )
			sgs_PushString( C, pp->ai_canonname );
		else
			sgs_PushNull( C );
		sgs_PushString( C, "addr" );
		push_sockaddr( C, (struct sockaddr_storage*) (void*) pp->ai_addr, pp->ai_addrlen );
		
		sgs_PushDict( C, sgs_StackSize( C ) - sz1 );
		pp = pp->ai_next;
	}
	freeaddrinfo( list );
	sgs_PushArray( C, sgs_StackSize( C ) - sz0 );
	return 1;
}
Exemple #6
0
int ss_Initialize( int argc, char* argv[], int debug )
{
	int ret;
	SGS_CTX;
	
	GEnabledDebugging = debug;
	
#if SS_STARTUP_PROFILING
	printf( "ss_Initialize called: %f\n", sgs_GetTime() );
#endif
	
	C = g_C = sgs_CreateEngine();
	
#if SS_STARTUP_PROFILING
	printf( "SGS engine created: %f\n", sgs_GetTime() );
#endif
	
	if( GEnabledDebugging )
		sgs_InitIDbg( C, &D );
	else
		sgs_SetMsgFunc( C, ss_MsgFunc, NULL );
	
#if SS_STARTUP_PROFILING
	printf( "debug printing initialized: %f\n", sgs_GetTime() );
#endif
	
	/* preinit first-use libs */
	sgs_LoadLib_Fmt( C );
	sgs_LoadLib_IO( C );
	sgs_LoadLib_Math( C );
	sgs_LoadLib_OS( C );
	sgs_LoadLib_RE( C );
	sgs_LoadLib_String( C );
	
#if SS_STARTUP_PROFILING
	printf( "SGS libraries loaded: %f\n", sgs_GetTime() );
#endif
	
	ss_InitDebug( C );
	ss_InitExtMath( C );
	ss_InitImage( C );
	
#if SS_STARTUP_PROFILING
	printf( "SS libraries loaded: %f\n", sgs_GetTime() );
#endif
	
	/* preinit tmp buffer */
	g_tmpbuf = sgs_membuf_create();
	sgs_membuf_reserve( &g_tmpbuf, C, 1024 );
	
	/* load command line arguments */
	{
		int i;
		for( i = 0; i < argc; ++i )
		{
			sgs_PushString( C, argv[ i ] );
		}
		sgs_CreateArray( C, NULL, argc );
		sgs_SetGlobalByName( C, "sys_args", sgs_StackItem( C, -1 ) );
		sgs_Pop( C, 1 );
	}
	
	/* push some system info */
	sgs_SetGlobalByName( C, "sys_scripting_engine", sgs_MakePtr( C ) );
	
#if SS_STARTUP_PROFILING
	printf( "system info pushed: %f\n", sgs_GetTime() );
#endif
	
	/* run the preconfig script */
	sgs_ExecString( C, scr_preconfig );
	
#if SS_STARTUP_PROFILING
	printf( "preconfig done: %f\n", sgs_GetTime() );
#endif
	
	/* run the config file */
	ret = sgs_Include( C, "core/config" );
	if( !ret )
	{
		sgs_Msg( C, SGS_ERROR, "Could not run core/config (configuration script)." );
		return -2;
	}
	/* run the primary extensions */
	ret = sgs_Include( C, "core/ext" );
	if( !ret )
	{
		sgs_Msg( C, SGS_ERROR, "Could not run core/ext." );
		return -2;
	}
	
#if SS_STARTUP_PROFILING
	printf( "configured engine scripts: %f\n", sgs_GetTime() );
#endif
	
	/* run the main file */
	ret = sgs_Include( C, "main" );
	if( !ret )
	{
		sgs_Msg( C, SGS_ERROR, "Could not execute 'main'." );
		return -3;
	}
	
#if SS_STARTUP_PROFILING
	printf( "ran main script: %f\n", sgs_GetTime() );
#endif
	
	/* configure the framework (optional) */
	sgs_GlobalCall( C, "configure", 0, 0 );
	
#if SS_STARTUP_PROFILING
	printf( "called 'configure': %f\n", sgs_GetTime() );
#endif
	
	/* check if already required to exit */
	if( sgs_GlobalBool( C, "sys_exit" ) )
		return 0;
	
	if( GEnabledProfiler )
		sgs_ProfInit( C, &P, GEnabledProfiler );
	
#if SS_STARTUP_PROFILING
	printf( "pre-SDL init: %f\n", sgs_GetTime() );
#endif
	
	/* initialize SDL */
	if( SDL_Init(
		SDL_INIT_TIMER | SDL_INIT_VIDEO |
		SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC |
		SDL_INIT_GAMECONTROLLER |
		SDL_INIT_EVENTS | SDL_INIT_NOPARACHUTE
	) < 0 )
	{
		sgs_Msg( C, SGS_ERROR, "Couldn't initialize SDL: %s", SDL_GetError() );
		return -5;
	}
	
	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
	
#if SS_STARTUP_PROFILING
	printf( "post-SDL init: %f\n", sgs_GetTime() );
#endif
	
	/* initialize script-space SDL API */
	ss_InitSDL( C );
	
	/* initialize script-space rendering API */
	ss_InitGraphics( C );
	
#if SS_STARTUP_PROFILING
	printf( "initialized SDL/Graphics subsystems: %f\n", sgs_GetTime() );
#endif
	
	/* initialize the application */
	if( SGS_FAILED( sgs_GlobalCall( C, "initialize", 0, 0 ) ) )
	{
		sgs_Msg( C, SGS_ERROR, "Failed to initialize the application." );
		return -8;
	}
	
#if SS_STARTUP_PROFILING
	printf( "called 'initialize': %f\n", sgs_GetTime() );
#endif
	
	return 1;
}
Exemple #7
0
int main( int argc, char* argv[] )
{
	int i;
	BYTE buf[ 4096 ], *path, *data;
	DWORD read, written, scriptsize = 0;
	HANDLE fh;
	
	path = buf;
	
	/* open EXE file */
	read = GetModuleFileName( NULL, (CHAR*) buf, sizeof( buf ) );
	if( read >= sizeof( buf ) )
	{
		path = malloc( read + 1 );
		GetModuleFileName( NULL, (CHAR*) buf, sizeof( buf ) );
	}
	fh = CreateFileFast( (CHAR*) buf, FILE_READ, OPEN_EXISTING );
	if( path != buf )
		free( path );
	if( fh == INVALID_HANDLE_VALUE )
		return E_FAIL;
	SetFilePointer( fh, -4, NULL, FILE_END );
	ReadFile( fh, &scriptsize, sizeof( scriptsize ), &read, NULL );
	
	/* read the following data */
	if( scriptsize == 0 )
	{
		if( argc == 3 )
		{
			HANDLE hsgs, hout = CreateFileFast( argv[ 1 ], FILE_WRITE, CREATE_ALWAYS );
			if( !hout )
			{
				MessageBox( 0, "Could not open executable file for writing", APPNAME, MB_ICONERROR );
				CloseHandle( fh );
				return E_FAIL;
			}
			hsgs = CreateFileFast( argv[ 2 ], FILE_READ, OPEN_EXISTING );
			if( !hsgs )
			{
				MessageBox( 0, "Could not open script file for reading", APPNAME, MB_ICONERROR );
				CloseHandle( hout );
				CloseHandle( fh );
				return E_FAIL;
			}
			SetFilePointer( fh, 0, NULL, FILE_BEGIN );
			while( ReadFile( fh, buf, sizeof( buf ), &read, NULL ) && read )
				WriteFile( hout, buf, read, &written, NULL );
			while( ReadFile( hsgs, buf, sizeof( buf ), &read, NULL ) && read )
				WriteFile( hout, buf, read, &written, NULL );
			scriptsize = GetFileSize( hsgs, NULL );
			WriteFile( hout, &scriptsize, 4, &written, NULL );
			CloseHandle( fh );
			CloseHandle( hsgs );
			CloseHandle( hout );
			MessageBox( 0, "File saved!", APPNAME, MB_ICONINFORMATION );
			return S_OK;
		}
		else
		{
			const char* info = "To create an executable from .sgs"
				", run sgsexe <output-file.exe> <script-file.sgs>."
				"\n\nglobal 'argv' will be the array of arguments";
			MessageBox( 0, info, APPNAME, MB_ICONINFORMATION );
			CloseHandle( fh );
			return E_ABORT;
		}
	}
	
	data = malloc( scriptsize );
	SetFilePointer( fh, -4-(LONG)scriptsize, NULL, FILE_END );
	ReadFile( fh, data, scriptsize, &read, NULL );
	CloseHandle( fh );
	
	{
		SGS_CTX = sgs_CreateEngine();
		
		for( i = 0; i < argc; ++i )
			sgs_PushString( C, argv[ i ] );
		
		sgs_CreateArray( C, NULL, argc );
		sgs_SetGlobalByName( C, "argv", sgs_StackItem( C, -1 ) );
		sgs_Pop( C, 1 );
		
		sgs_SetGlobalByName( C, "argc", sgs_MakeInt( argc ) );
		
		sgs_ExecBuffer( C, (char*) data, scriptsize );
		
		sgs_DestroyEngine( C );
	}
	
	free( data );
	
	return 0;
}
Exemple #8
0
static int sockaddr_getindex( SGS_CTX, sgs_VarObj* data, sgs_Variable* key, int prop )
{
	char* name;
	if( sgs_ParseStringP( C, key, &name, NULL ) )
	{
		if( STREQ( name, "family" ) ){ sgs_PushInt( C, GET_SAF ); return SGS_SUCCESS; }
		if( STREQ( name, "port" ) )
		{
			if( GET_SAF == AF_INET ){ sgs_PushInt( C, ntohs( GET_SAI->sin_port ) ); }
			else if( GET_SAF == AF_INET6 ){ sgs_PushInt( C, ntohs( GET_SAI6->sin6_port ) ); }
			else { sgs_PushNull( C ); sgs_Msg( C, SGS_WARNING, "port supported only for AF_INET[6]" ); }
			return SGS_SUCCESS;
		}
		if( STREQ( name, "addr_u32" ) )
		{
			if( GET_SAF == AF_INET ){ sgs_PushInt( C, ntohl( GET_SAI->sin_addr.s_addr ) ); }
			else { sgs_PushNull( C ); sgs_Msg( C, SGS_WARNING, "addr_u32 supported only for AF_INET" ); }
			return SGS_SUCCESS;
		}
		if( STREQ( name, "addr_buf" ) )
		{
			if( GET_SAF == AF_INET ){ sgs_PushStringBuf( C, (char*) &GET_SAI->sin_addr.s_addr, 4 ); }
			else if( GET_SAF == AF_INET6 ){ sgs_PushStringBuf( C, (char*) &GET_SAI6->sin6_addr.s6_addr, 16 ); }
			else { sgs_PushNull( C ); sgs_Msg( C, SGS_WARNING, "addr_buf supported only for AF_INET[6]" ); }
			return SGS_SUCCESS;
		}
		if( STREQ( name, "addr_bytes" ) )
		{
			char* buf = NULL;
			int i, sz = 0;
			if( GET_SAF == AF_INET )
			{
				buf = (char*) &GET_SAI->sin_addr.s_addr;
				sz = 4;
			}
			else if( GET_SAF == AF_INET6 )
			{
				buf = (char*) &GET_SAI6->sin6_addr.s6_addr;
				sz = 16;
			}
			if( buf )
			{
				for( i = 0; i < sz; ++i )
					sgs_PushInt( C, buf[ i ] );
				sgs_PushArray( C, sz );
			}
			else { sgs_PushNull( C ); sgs_Msg( C, SGS_WARNING, "addr_bytes supported only for AF_INET[6]" ); }
			return SGS_SUCCESS;
		}
		if( STREQ( name, "addr_string" ) )
		{
			char addr[ 64 ] = {0};
#ifdef _WIN32
			DWORD ioasz = 64;
			WSAAddressToString( (LPSOCKADDR) data->data, sizeof(struct sockaddr_storage), NULL, addr, &ioasz );
			*strrchr( addr, ':' ) = 0;
#else
			if( GET_SAF == AF_INET )
				inet_ntop( GET_SAF, &GET_SAI->sin_addr, addr, 64 );
			else if( GET_SAF == AF_INET6 )
				inet_ntop( GET_SAF, &GET_SAI6->sin6_addr, addr, 64 );
#endif
			addr[ 64-1 ] = 0;
			if( *addr )
				sgs_PushString( C, addr );
			else
				sgs_PushString( C, "-" );
			return SGS_SUCCESS;
		}
		if( STREQ( name, "full_addr_string" ) )
		{
			sockaddr_push_full_addr_string( C, data );
			return SGS_SUCCESS;
		}
	}
	return SGS_ENOTFND;
}