static char* DaoReadLine( const char *s, DString *buffer ) { int ch; char *line; DString_Reset( buffer, 0 ); #ifdef DAO_WITH_THREAD if( ! DThread_IsMain() ){ printf( "%s", s ); fflush( stdout ); while( (ch = getchar()) != '\n' ) DString_AppendWChar( buffer, ch ); return DString_GetData( buffer ); } #endif readingline = 1; count = 0; #ifdef DAO_USE_READLINE line = readline( s ); DString_SetChars( buffer, line ); free( line ); #endif readingline = 0; return DString_GetData( buffer ); }
void DaoxShader_CompileShader( DaoxShader *self, int type, DArray *strings ) { daoint i, n = strings->size; uint_t shader = glCreateShader( type ); const GLchar **sources; GLint length, shader_ok; if( shader == 0 ){ fprintf(stderr, "Failed to create shader of type %i\n", type ); return; } sources = (const GLchar**) malloc( n*sizeof(GLchar*) ); for(i=0; i<n; ++i){ sources[i] = (const GLchar*) DString_GetData( strings->items.pString[i] ); if( i == 0 ) sources[i] += 2; /* skip //; */ } glShaderSource( shader, n, sources, NULL ); glCompileShader( shader ); free( sources ); glGetShaderiv(shader, GL_COMPILE_STATUS, &shader_ok); if( !shader_ok ){ const char *log2; DString *log = DString_New(1); glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length ); DString_Resize( log, length ); log2 = DString_GetData(log); glGetShaderInfoLog( shader, length, NULL, (char*)log2 ); fprintf(stderr, "Failed to compile shader!\nWith error message: %s", log2 ); glDeleteShader(shader); return; } switch( type ){ case GL_VERTEX_SHADER : if( self->vertexShader ) glDeleteShader( self->vertexShader ); self->vertexShader = shader; break; case GL_FRAGMENT_SHADER : if( self->fragmentShader ) glDeleteShader( self->fragmentShader ); self->fragmentShader = shader; break; } if( shader && self->program ) glAttachShader( self->program, shader ); }
static void PIPE_New( DaoProcess *proc, DaoValue *p[], int N ) { DaoPipeStream *stream = NULL; DString *fname = p[0]->xString.value; char *mode; stream = DaoPipeStream_New(); DaoProcess_PutValue( proc, (DaoValue*)stream ); if( DString_Size( fname ) == 0 ){ DaoProcess_RaiseError( proc, "Param", "Empty command" ); return; } mode = DString_GetData( p[1]->xString.value ); stream->file = popen( DString_GetData( fname ), mode ); if( stream->file == NULL ){ char errbuf[512]; GetErrorMessage( errno, errbuf, sizeof(errbuf) ); DaoProcess_RaiseError( proc, "Stream", errbuf ); return; } if( strstr( mode, "r" ) ) stream->base.mode |= DAO_STREAM_READABLE; if( strstr( mode, "w" ) ) stream->base.mode |= DAO_STREAM_WRITABLE; DaoFileStream_InitCallbacks( stream ); }
void DaoCGI_SendFile( DaoProcess *proc, DaoValue *p[], int N ) { DString *mbs; DString *file = DaoValue_TryGetString( p[0] ); DString *mime = DaoValue_TryGetString( p[1] ); DString *notfound = DaoValue_TryGetString( p[2] ); char buf[IO_BUF_SIZE]; FILE *fin = fopen( DString_GetData( file ), "r" ); if( fin == NULL ) { printf( "%s", DString_GetData( notfound ) ); return; } mbs = DString_New(); printf( "Content-Type: %s\n\n", DString_GetData( mime ) ); while(1) { size_t count = fread( buf, 1, IO_BUF_SIZE, fin ); if( count ==0 ) break; DString_Reset( mbs, 0 ); DString_AppendBytes( mbs, buf, count ); DaoFile_WriteString( stdout, mbs ); } fclose( fin ); DString_Delete( mbs ); }
static FILE* DaoIO_OpenFile( DaoProcess *proc, DString *name, const char *mode, int silent ) { DString *fname = DString_Copy( name ); char buf[200]; FILE *fin; DaoIO_MakePath( proc, fname ); fin = Dao_OpenFile( fname->chars, mode ); DString_Delete( fname ); if( fin == NULL && silent == 0 ){ snprintf( buf, sizeof(buf), "error opening file: %s", DString_GetData( name ) ); DaoProcess_RaiseError( proc, "Stream", buf ); return NULL; } return fin; }
static void DaoSTD_Load( DaoProcess *proc, DaoValue *p[], int N ) { DaoNamespace *ns; DaoVmSpace *vms = proc->vmSpace; DString *name = p[0]->xString.value; int import = p[1]->xInteger.value; int runim = p[2]->xInteger.value; int res = 0; DList_PushFront( vms->pathLoading, proc->activeNamespace->path ); ns = DaoVmSpace_LoadEx( vms, DString_GetData( name ), runim ); DaoProcess_PutValue( proc, (DaoValue*) ns ); if( ns == NULL ) DaoProcess_RaiseError( proc, NULL, "loading failed" ); DList_PopFront( vms->pathLoading ); if( import && ns ) DaoNamespace_AddParent( proc->activeNamespace, ns ); }
static void DaoIO_Open( DaoProcess *proc, DaoValue *p[], int N ) { DaoFileStream *self = NULL; char *mode; self = DaoFileStream_New(); DaoProcess_PutValue( proc, (DaoValue*)self ); if( N == 0 ){ do self->file = tmpfile(); while ( !self->file && errno == EINTR ); self->base.Read = DaoFileStream_Read; self->base.Write = DaoFileStream_Write; self->base.AtEnd = DaoFileStream_AtEnd; self->base.Flush = DaoFileStream_Flush; self->base.mode |= DAO_STREAM_WRITABLE | DAO_STREAM_READABLE; if( !self->file ){ char errbuf[512]; GetErrorMessage( errno, errbuf, sizeof(errbuf) ); DaoProcess_RaiseError( proc, "Stream", errbuf ); return; } }else{ mode = DString_GetData( p[1]->xString.value ); if( p[0]->type == DAO_INTEGER ){ self->file = fdopen( p[0]->xInteger.value, mode ); if( self->file == NULL ){ char errbuf[512]; GetErrorMessage( errno, errbuf, sizeof(errbuf) ); DaoProcess_RaiseError( proc, "Stream", errbuf ); return; } }else{ self->file = DaoIO_OpenFile( proc, p[0]->xString.value, mode, 0 ); } if( strstr( mode, "+" ) ){ self->base.mode |= DAO_STREAM_WRITABLE | DAO_STREAM_READABLE; }else{ if( strstr( mode, "r" ) ){ self->base.mode |= DAO_STREAM_READABLE; } if( strstr( mode, "w" ) || strstr( mode, "a" ) ){ self->base.mode |= DAO_STREAM_WRITABLE; } } DaoFileStream_InitCallbacks( self ); } }
void DaoxShader_Finalize( DaoxShader *self ) { GLint length, program_ok; if( self->program == 0 ) return; int shaderAttribute = 0; glBindFragDataLocation( self->program, 0, "fragColor"); glLinkProgram( self->program ); glGetProgramiv( self->program, GL_LINK_STATUS, &program_ok ); if( !program_ok ){ const char *log2; DString *log = DString_New(1); glGetProgramiv( self->program, GL_INFO_LOG_LENGTH, &length ); DString_Resize( log, length ); log2 = DString_GetData(log); glGetProgramInfoLog( self->program, length, NULL, (char*)log2 ); fprintf(stderr, "Failed to link shader program with error message: %s\n", log2 ); glDeleteProgram(self->program); self->program = 0; } }
int main( int argc, char **argv ) { int restart = 0; int i, k, idsrc, vmods = 0; DString *opts, *args; /*mtrace(); */ for(i=1; i<argc; i++){ if( strcmp( argv[i], "-r" ) ==0 || strcmp( argv[i], "--restart" ) ==0 ){ restart = i; break; } } if( restart ) DaoRestartRun( argv, argc, restart ); vmSpace = DaoInit( argv[0] ); idsrc = -1; for(i=1; i<argc; i++){ if( strcmp( argv[i], "-e" ) ==0 || strcmp( argv[i], "--eval" ) ==0 ) break; /* also allows execution of script files without suffix .dao */ if( argv[i][0] != '-' ){ idsrc = i; break; } } #ifdef DAO_WITH_STATIC_MODULES idsrc = 1; vmods = 0; while( dao_virtual_modules[vmods].name ){ DaoVmSpace_AddVirtualModule( vmSpace, & dao_virtual_modules[vmods] ); vmods ++; } #endif k = idsrc; if( k < 0 ) k = argc; opts = DString_New(); args = DString_New(); for(i=1; i<k; i++ ){ DString_AppendChars( opts, argv[i] ); DString_AppendChar( opts, '\1' ); } if( idsrc >= 0 ){ for(i=idsrc; i<argc; i++ ){ DString_AppendChars( args, argv[i] ); DString_AppendChar( args, '\1' ); } } DaoVmSpace_ParseOptions( vmSpace, DString_GetData( opts ) ); #ifdef DAO_WITH_STATIC_MODULES if( vmods ){ DString_InsertChar( args, '\1', 0 ); DString_InsertChars( args, dao_virtual_modules[0].name, 0, 0, 0 ); /* set the path for the virtual files: */ DaoVmSpace_SetPath( vmSpace, "/@/" ); }else #endif if( idsrc < 0 && argc == 1 ){ DString_AppendChar( opts, '\1' ); DString_AppendChars( opts, "-vi" ); DaoVmSpace_ParseOptions( vmSpace, DString_GetData( opts ) ); } #ifdef DAO_USE_READLINE DaoVmSpace_ReadLine( vmSpace, DaoReadLine ); DaoVmSpace_AddHistory( vmSpace, (AddHistory) add_history ); read_history( NULL ); #endif signal( SIGINT, DaoSignalHandler ); /* Start execution. */ k = ! DaoVmSpace_RunMain( vmSpace, DString_GetData( args ) ); #ifdef DAO_USE_READLINE write_history( NULL ); #endif DString_Delete( args ); DString_Delete( opts ); DaoQuit(); return k; }
int main( int argc, char **argv ) { int restart = 0; int i, k, idsrc, vmods = 0; DString *opts = NULL, *args = NULL; /*mtrace(); */ for(i=1; i<argc; i++){ if( strcmp( argv[i], "-r" ) ==0 || strcmp( argv[i], "--restart" ) ==0 ){ restart = i; break; } } if( restart ) DaoRestartRun( argv, argc, restart ); vmSpace = DaoInit( argv[0] ); idsrc = -1; for(i=1; i<argc; i++){ if( strcmp( argv[i], "-e" ) ==0 || strcmp( argv[i], "--eval" ) ==0 ) break; /* also allows execution of script files without suffix .dao */ if( argv[i][0] != '-' ){ idsrc = i; break; } } #ifdef DAO_WITH_STATIC_MODULES /* // For single file deployment. // Identify the script argument, such that the arguments before the script // can be passed in as virtual machine arguments. // Example: ./script --restart script.dao ... */ args = DString_Copy( vmSpace->daoBinFile ); DString_Erase( args, 0, vmSpace->daoBinPath->size ); DString_AppendChars( args, ".dao" ); idsrc = 1; for(i=1; i<argc; i++){ if( strcmp( argv[i], args->chars ) == 0 ){ idsrc = i; break; } } vmods = DaoVmSpace_AddVirtualModules( vmSpace, dao_virtual_modules ); DString_Reset( args, 0 ); #endif k = idsrc; if( k < 0 ) k = argc; if( opts == NULL ) opts = DString_New(); if( args == NULL ) args = DString_New(); for(i=1; i<k; i++ ){ DString_AppendChars( opts, argv[i] ); DString_AppendChar( opts, '\1' ); } if( idsrc >= 0 ){ #ifdef DAO_WITH_STATIC_MODULES idsrc += 1; #endif for(i=idsrc; i<argc; i++ ){ DString_AppendChars( args, argv[i] ); DString_AppendChar( args, '\1' ); } } DaoVmSpace_ParseOptions( vmSpace, DString_GetData( opts ) ); #ifdef DAO_WITH_STATIC_MODULES if( vmods ){ DString_InsertChars( args, "/@/\1", 0, 0, 0 ); DString_InsertChars( args, dao_virtual_modules[0].name, 3, 0, 0 ); /* set the path for the virtual files: */ DaoVmSpace_SetPath( vmSpace, "/@/" ); }else #endif if( idsrc < 0 && argc == 1 ){ DString_AppendChar( opts, '\1' ); DString_AppendChars( opts, "-vi" ); DaoVmSpace_ParseOptions( vmSpace, DString_GetData( opts ) ); } #ifdef DAO_USE_READLINE DaoVmSpace_ReadLine( vmSpace, DaoReadLine ); DaoVmSpace_AddHistory( vmSpace, (AddHistory) add_history ); read_history( NULL ); #endif signal( SIGINT, DaoSignalHandler ); signal( SIGSEGV, DaoStackTrace ); /* Start execution. */ k = DaoVmSpace_RunMain( vmSpace, DString_GetData( args ) ); #ifdef DAO_USE_READLINE write_history( NULL ); #endif DString_Delete( args ); DString_Delete( opts ); DaoQuit(); return k; }
char* DaoValue_TryGetChars( DaoValue *self ) { if( self->type != DAO_STRING ) return NULL; return DString_GetData( self->xString.value ); }