void DString_InsertChar( DString *self, const char ch, daoint at ) { char chs[2]; chs[0] = ch; chs[1] = '\0'; DString_InsertChars( self, chs, at, 0, 1 ); }
static void DaoSTD_Resource( DaoProcess *proc, DaoValue *p[], int N ) { FILE *fin; DString *file = DString_Copy( p[0]->xString.value ); if( DaoVmSpace_SearchResource( proc->vmSpace, file, proc->activeNamespace->path ) == 0 ){ DString_InsertChars( file, "resource file not found: ", 0, 0, -1 ); DaoProcess_RaiseError( proc, NULL, file->chars ); DString_Delete( file ); return; } DaoVmSpace_ReadFile( proc->vmSpace, file, file ); DaoProcess_PutString( proc, file ); DString_Delete( file ); }
void DString_ReplaceChars( DString *self, const char *chs, daoint start, daoint rm ) { DString_InsertChars( self, chs, start, rm, strlen( chs ) ); }
void DString_Insert( DString *self, DString *chs, daoint at, daoint rm, daoint cp ) { DString_InsertChars( self, chs->chars, at, rm, cp ); }
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; }
/* // C printf format: %[parameter][flags][width][.precision][length]type // // Dao writef format: %[flags][width][.precision]type[color] // // Where 'flags', 'width' and 'precision' will conform to the C format, // but 'type' can only be: // d, i, o, u, x/X : for integer; // e/E, f/F, g/G : for float and double; // c/C : for character, C for local encoding; // s/S : for string, S for local encoding; // p : for any type, write address; // a : automatic, for any type, write in the default format; // Namely the standard ones except 'n', and plus 'a'. // // Optional 'color' format will be in form of: [foreground:background], [foreground] // or [:background]. The supported color name format will depend on the color printing // handle. Mininum requirement is the support of the following 8 color names: // black, white, red, green, blue, yellow, magenta, cyan. */ static void DaoIO_Writef0( DaoStream *self, DaoProcess *proc, DaoValue *p[], int N ) { DaoValue *value; DString *fmt2; DString *fgcolor = NULL; DString *bgcolor = NULL; DMap *cycmap = NULL; const char *convs = "asSpcCdiouxXfFeEgG"; char F, *s, *end, *fg, *bg, *fmt, message[100]; int i, k, id = 0; if( DaoIO_CheckMode( self, proc, DAO_STREAM_WRITABLE ) == 0 ) return; fmt2 = DString_New(); for(i=0; i<N; i++){ if( p[i]->type > DAO_ARRAY ){ cycmap = DHash_New(0,0); break; } } s = p[0]->xString.value->chars; end = s + p[0]->xString.value->size; for(; s<end; s++){ if( *s != '%' ){ DaoStream_WriteChar( self, *s ); continue; } fmt = s; s += 1; if( *s =='%' || *s == '[' ){ DaoStream_WriteChar( self, *s ); continue; } if( ++id >= N || p[id] == NULL ) goto NullParameter; value = p[id]; /* flags: */ while( *s == '+' || *s == '-' || *s == '#' || *s == '0' || *s == ' ' ) s += 1; while( isdigit( *s ) ) s += 1; /* width; */ if( *s == '.' ){ /* precision: */ s += 1; while( isdigit( *s ) ) s += 1; } DString_SetBytes( fmt2, fmt, s - fmt + 1 ); if( strchr( convs, *s ) == NULL ){ DaoProcess_RaiseWarning( proc, NULL, "invalid format conversion" ); continue; } F = *s; s += 1; fg = bg = NULL; if( *s == '[' ){ s += 1; fmt = s; while( isalnum( *s ) ) s += 1; if( fgcolor == NULL ) fgcolor = DString_New(); DString_SetBytes( fgcolor, fmt, s - fmt ); if( fgcolor->size ) fg = fgcolor->chars; if( *s == ':' ){ s += 1; fmt = s; while( isalnum( *s ) ) s += 1; if( bgcolor == NULL ) bgcolor = DString_New(); DString_SetBytes( bgcolor, fmt, s - fmt ); if( bgcolor->size ) bg = bgcolor->chars; } if( *s != ']' ) goto WrongColor; }else{ s -= 1; } if( fg || bg ){ if( DaoStream_SetColor( self, fg, bg ) == 0 ) goto WrongColor; } self->format = fmt2->chars; if( F == 'c' || F == 'C' ){ if( value->type != DAO_INTEGER ) goto WrongParameter; DString_Reset( fmt2, 0 ); DString_AppendWChar( fmt2, value->xInteger.value ); self->format = "%s"; if( F == 'C' ) DString_ToLocal( fmt2 ); DaoStream_WriteString( self, fmt2 ); }else if( F == 'd' || F == 'i' || F == 'o' || F == 'x' || F == 'X' ){ if( value->type == DAO_NONE || value->type > DAO_FLOAT ) goto WrongParameter; DString_InsertChars( fmt2, "ll", fmt2->size-1, 0, 2 ); self->format = fmt2->chars; DaoStream_WriteInt( self, DaoValue_GetInteger( value ) ); }else if( toupper( F ) == 'E' || toupper( F ) == 'F' || toupper( F ) == 'G' ){ if( value->type == DAO_NONE || value->type > DAO_FLOAT ) goto WrongParameter; DaoStream_WriteFloat( self, DaoValue_GetFloat( value ) ); }else if( F == 's' && value->type == DAO_STRING ){ DaoStream_WriteString( self, value->xString.value ); }else if( F == 'S' && value->type == DAO_STRING ){ DaoStream_WriteLocalString( self, value->xString.value ); }else if( F == 'p' ){ DaoStream_WritePointer( self, value ); }else if( F == 'a' ){ self->format = NULL; if( value->type > DAO_ARRAY ) DMap_Reset( cycmap ); DaoValue_Print( value, self, cycmap, proc ); }else{ goto WrongParameter; } self->format = NULL; if( fg || bg ) DaoStream_SetColor( self, NULL, NULL ); continue; NullParameter: sprintf( message, "%i-th parameter is null!", id ); DaoProcess_RaiseWarning( proc, NULL, message ); continue; WrongColor: sprintf( message, "%i-th parameter has wrong color format!", id ); DaoProcess_RaiseWarning( proc, NULL, message ); continue; WrongParameter: self->format = NULL; if( fg || bg ) DaoStream_SetColor( self, NULL, NULL ); sprintf( message, "%i-th parameter has wrong type for format \"%s\"!", id, fmt2->chars ); DaoProcess_RaiseWarning( proc, NULL, message ); } if( cycmap ) DMap_Delete( cycmap ); if( fgcolor ) DString_Delete( fgcolor ); if( bgcolor ) DString_Delete( bgcolor ); DString_Delete( fmt2 ); }