static void DaoStream_ReadLines( DaoStream *self, DaoList *list, DaoProcess *proc, int count, int chop ) { DaoValue *res; DaoString *line; DaoVmCode *sect = DaoProcess_InitCodeSection( proc, 1 ); daoint i = 0; if( sect == NULL ){ line = DaoString_New(); while( (count == 0 || (i++) < count) && DaoStream_ReadLine( self, line->value ) ){ if( line->value->size == 0 && self->AtEnd != NULL && self->AtEnd( self ) ) break; if( chop ) DString_Chop( line->value, 0 ); DaoList_Append( list, (DaoValue*) line ); } DaoString_Delete( line ); }else{ ushort_t entry = proc->topFrame->entry; if( sect->b ){ DaoString tmp = {DAO_STRING,0,0,0,1,NULL}; DString tmp2 = DString_WrapChars( "" ); tmp.value = & tmp2; line = (DaoString*) DaoProcess_SetValue( proc, sect->a, (DaoValue*)(void*) &tmp ); } while( (count == 0 || (i++) < count) && DaoStream_ReadLine( self, line->value ) ){ if( line->value->size == 0 && self->AtEnd != NULL && self->AtEnd( self ) ) break; if( chop ) DString_Chop( line->value, 0 ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; res = proc->stackValues[0]; if( res && res->type != DAO_NONE ) DaoList_Append( list, res ); } DaoProcess_PopFrame( proc ); } }
static void DaoIO_ReadLines2( DaoProcess *proc, DaoValue *p[], int N ) { DaoValue *res; DaoString *line; DaoVmCode *sect = DaoGetSectionCode( proc->activeCode ); DaoList *list = DaoProcess_PutList( proc ); DaoStream *self = & p[0]->xStream; daoint i = 0, count = p[1]->xInteger.value; int chop = p[2]->xInteger.value; if( sect == NULL || DaoProcess_PushSectionFrame( proc ) == NULL ){ line = DaoString_New(1); while( (i++) < count && DaoStream_ReadLine( self, line->data ) ){ if( chop ) DString_Chop( line->data ); DaoList_Append( list, (DaoValue*) line ); } DaoString_Delete( line ); }else{ ushort_t entry = proc->topFrame->entry; DaoString tmp = {DAO_STRING,0,0,0,1,NULL}; DString tmp2 = DString_WrapMBS( "" ); tmp.data = & tmp2; line = (DaoString*) DaoProcess_SetValue( proc, sect->a, (DaoValue*)(void*) &tmp ); DaoProcess_AcquireCV( proc ); while( (i++) < count && DaoStream_ReadLine( self, line->data ) ){ if( chop ) DString_Chop( line->data ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; res = proc->stackValues[0]; if( res && res->type != DAO_NONE ) DaoList_Append( list, res ); } DaoProcess_ReleaseCV( proc ); DaoProcess_PopFrame( proc ); } }
static void DaoIO_Read( DaoProcess *proc, DaoValue *p[], int N ) { DaoStream *self = proc->stdioStream; DString *ds = DaoProcess_PutMBString( proc, "" ); int count = 0; if( self == NULL ) self = proc->vmSpace->stdioStream; if( N >0 ) self = & p[0]->xStream; if( N >1 ) count = p[1]->xInteger.value; if( (self->attribs & (DAO_IO_FILE | DAO_IO_PIPE)) && self->file == NULL ){ DaoProcess_RaiseException( proc, DAO_ERROR, "stream is not open!" ); return; } if( ( self->mode & DAO_IO_READ ) == 0 ){ DaoProcess_RaiseException( proc, DAO_ERROR, "stream is not readable" ); return; } if( self->file == NULL && self->redirect && self->redirect->StdioRead ){ self->redirect->StdioRead( self->redirect, ds, count ); }else if( count ){ FILE *fd = stdin; DString_Clear( ds ); if( self->file ) fd = self->file; if( count >0 ){ DString_Resize( ds, count ); DString_Resize( ds, fread( ds->mbs, 1, count, fd ) ); }else{ struct stat info; fstat( fileno( fd ), &info ); DString_Resize( ds, info.st_size - ftell( fd )/2 ); DString_Resize( ds, fread( ds->mbs, 1, ds->size, fd ) ); } if( fd == stdin ) fseek( stdin, 0, SEEK_END ); }else{ DaoStream_ReadLine( self, ds ); } }
void STD_Debug( DaoProcess *proc, DaoValue *p[], int N ) { DaoUserHandler *handler = proc->vmSpace->userHandler; DaoRoutine *routine = proc->activeRoutine; DaoStream *stream = proc->vmSpace->stdioStream; DString *input; DArray *tokens; DMap *cycData; char *chs, *cmd; int i; if( ! (proc->vmSpace->options & DAO_OPTION_DEBUG ) ) return; input = DString_New(1); if( N > 0 && DaoValue_CastCstruct( p[0], dao_type_stream ) ){ stream = (DaoStream*)p[0]; p ++; N --; } if( N > 0 ){ Dao_AboutVars( proc->activeNamespace, p, N, input ); DaoStream_WriteString( stream, input ); DaoStream_WriteMBS( stream, "\n" ); DString_Delete( input ); return; } if( handler && handler->StdlibDebug ){ handler->StdlibDebug( handler, proc ); return; } tokens = DArray_New(D_STRING); cycData = DMap_New(0,0); while(1){ if( proc->vmSpace->ReadLine ){ chs = proc->vmSpace->ReadLine( "(debug) " ); if( chs ){ DString_SetMBS( input, chs ); DString_Trim( input ); if( input->size && proc->vmSpace->AddHistory ) proc->vmSpace->AddHistory( chs ); dao_free( chs ); } }else{ DaoStream_WriteMBS( stream, "(debug) " ); DaoStream_ReadLine( stream, input ); } if( input->size == 0 ) continue; SplitByWhiteSpaces( input->mbs, tokens ); if( tokens->size == 0 ) continue; cmd = tokens->items.pString[0]->mbs; if( strcmp( cmd, "q" ) == 0 || strcmp( cmd, "quit" ) == 0 ){ break; }else if( strcmp( cmd, "k" ) == 0 || strcmp( cmd, "kill" ) == 0 ){ proc->status = DAO_PROCESS_ABORTED; break; }else if( strcmp( cmd, "a" ) == 0 || strcmp( cmd, "about" ) == 0 ){ if( tokens->size > 1 ){ ushort_t reg = (ushort_t)strtod( tokens->items.pString[1]->mbs, 0 ); DaoType *tp = proc->activeTypes[ reg ]; DString_Clear( input ); Dao_AboutVar( proc->activeNamespace, proc->activeValues[reg], input ); DaoStream_WriteMBS( stream, "type: " ); if( tp ) DaoStream_WriteString( stream, tp->name ); else DaoStream_WriteMBS( stream, "?" ); DaoStream_WriteMBS( stream, ", value: " ); DaoStream_WriteString( stream, input ); DaoStream_WriteMBS( stream, "\n" ); } }else if( strcmp( cmd, "g" ) == 0 || strcmp( cmd, "goto" ) == 0 ){ if( tokens->size > 1 ){ int n = atoi( tokens->items.pString[1]->mbs ); int entry = proc->activeCode - proc->activeRoutine->body->vmCodes->data.codes; if( n < 0 ) n = entry - n; if( n >= routine->body->vmCodes->size ) n = routine->body->vmCodes->size -1; proc->topFrame->entry = n; proc->status = DAO_PROCESS_STACKED; return; } }else if( strcmp( cmd, "h" ) == 0 || strcmp( cmd, "help" ) == 0 ){ DaoStream_WriteMBS( stream, help ); }else if( strcmp( cmd, "l" ) == 0 || strcmp( cmd, "list" ) == 0 ){ DString *mbs = DString_New(1); int entry = proc->activeCode - proc->activeRoutine->body->vmCodes->data.codes; int start = entry - 10; int end = entry; if( tokens->size >1 ){ int dn = atoi( tokens->items.pString[1]->mbs ); if( dn < 0 ){ start = entry + dn; }else if( dn > 0 ){ start = entry; end = entry + dn; } } if( start < 0 ) start = 0; if( end >= routine->body->vmCodes->size ) end = routine->body->vmCodes->size - 1; DaoStream_WriteString( stream, routine->routName ); DaoStream_WriteMBS( stream, "(): " ); if( routine->routType ) DaoStream_WriteString( stream, routine->routType->name ); DaoStream_WriteMBS( stream, "\n" ); DaoStream_WriteMBS( stream, daoRoutineCodeHeader ); DaoStream_WriteMBS( stream, sep ); for( i=start; i<=end; i++ ){ DaoRoutine_FormatCode( routine, i, *routine->body->annotCodes->items.pVmc[i], mbs ); DaoStream_WriteString( stream, mbs ); } DString_Delete( mbs ); }else if( strcmp( cmd, "p" ) == 0 || strcmp( cmd, "print" ) == 0 ){ if( tokens->size > 1 ){ ushort_t reg = (ushort_t)atoi( tokens->items.pString[1]->mbs ); DaoValue_Print( proc->activeValues[reg], proc, stream, cycData ); DaoStream_WriteMBS( stream, "\n" ); } }else if( strcmp( cmd, "t" ) == 0 || strcmp( cmd, "trace" ) == 0 ){ int depth = 1; if( tokens->size >1 ) depth = atoi( tokens->items.pString[1]->mbs ); DaoProcess_Trace( proc, depth ); }else{ DaoStream_WriteMBS( stream, "Unknown debugging command.\n" ); } } DString_Delete( input ); DArray_Delete( tokens ); }