PTEXT SplitLine( PTEXT pLine, INDEX nPos ) { PTEXT newseg, end; if( !nPos ) { if( PRIORLINE( pLine ) ) return PRIORLINE( pLine ); // otherwise we'll have to insert a new segment // in front of this.... newseg = SegCreate( BUILD_LINE_OUTPUT_SIZE ); newseg->data.size = 0; pLine->format.position.offset.spaces = 0; SegAppend( newseg, pLine ); return newseg; } else if( pLine->data.size == nPos ) { // if the point to split is not in the middle of // this segment.... return pLine; } newseg = SegCreate( BUILD_LINE_OUTPUT_SIZE ); end = NEXTLINE( pLine ); SegBreak( end ); // fill in new segment with trailing data newseg->data.size = pLine->data.size - nPos; MemCpy( newseg->data.data, pLine->data.data + nPos, newseg->data.size ); pLine->data.size = nPos; pLine->data.data[nPos] = 0; // terminate with null also... SegAppend( pLine , SegAppend( newseg, end ) ); return pLine; }
PTEXT win_get_line(HANDLE hFile) { // extern HANDLE hStdin; #define WORKSPACE 1024 // character for workspace PTEXT workline=(PTEXT)NULL; uint32_t length = 0; do { // create a workspace to read input from the file. workline=SegAppend(workline,SegCreate(WORKSPACE)); SetEnd( workline ); // read a line of input from the file. if( !ReadConsole( hFile , GetText(workline) , WORKSPACE , &length , NULL) ) // if no input read. { if (PRIORLINE(workline)) // if we've read some. { PTEXT t; workline=PRIORLINE(workline); // go back one. SegBreak(t = NEXTLINE(workline)); LineRelease(t); // destroy the current segment. } else { LineRelease(workline); // destory only segment. workline = NULL; } break; // get out of the loop- there is no more to read. } } while (GetText(workline)[length-1]!='\n'); //while not at the end of the line. if (workline&&length) // if I got a line, and there was some length to it. SetStart(workline); // set workline to the beginning. return(workline); // return the line read from the file. }
static PTEXT CPROC ParseCommand( PMYDATAPATH pdp, PTEXT buffer ) { if( buffer ) { PTEXT pCommand; //Log2( WIDE("buffer: %s(%d)"), GetText( buffer ), GetTextSize( buffer ) ); //LogBinary( buffer ); pCommand = burst( buffer ); LineRelease( buffer ); if( pCommand ) { PTEXT pTemp, pStart; int bEscaped = FALSE; pStart = pTemp = pCommand; while( pTemp ) { if( TextIs( pTemp, WIDE("\\") ) ) { if( !bEscaped ) { PTEXT pNext; pNext = NEXTLINE( pTemp ); pNext->format.position.offset.spaces = pTemp->format.position.offset.spaces; LineRelease( SegGrab( pTemp ) ); bEscaped = TRUE; pTemp = pNext; continue; } } if( !bEscaped && TextIs( pTemp, WIDE(";") ) ) { PTEXT pPrior; //Log( WIDE("Splitting the line and enqueing it...") ); pPrior = pTemp; SegBreak( pTemp ); if( pStart != pTemp ) { // end of line included! pStart = SegAppend( pStart, SegCreate(0) ); EnqueLink( &pdp->output, pStart ); } pStart = pTemp = NEXTLINE( pTemp ); SegBreak( pTemp ); LineRelease( pPrior ); // remove ';' bEscaped = FALSE; continue; } bEscaped = FALSE; pTemp = NEXTLINE( pTemp ); } if( pStart ) { PTEXT line; line = BuildLine( pStart ); //Log1( WIDE("Enqueu: %s"), GetText( line ) ); LineRelease( line ); EnqueLink( &pdp->output, pStart ); } } else { // well what now? I guess pLine got put into Partial... // next pass through this all for command recall will blow... Log( WIDE("No information from the burst!") ); } } return (PTEXT)DequeLink( &pdp->output ); }
void HTTPCollapse( PTEXT *ppText ) { PTEXT output; PTEXT input = *ppText; while( input ) { if( GetText( input )[0] == '+' ) { PTEXT subst; // sometimes a + can be attached to a number // so much for the natural language parser dealing with // a machine oriented protocol... if( GetTextSize( input ) > 1 ) { SegSplit( &input, 1 ); } subst = GetSubst( '+' ); SegInsert( subst, input ); LineRelease( SegGrab( input ) ); input = subst; if( !PRIORLINE( input ) ) (*ppText) = input; } else if( TextIs( input, WIDE("%") ) ) { PTEXT next = NEXTLINE( input ); if( next ) { PTEXT subst; SegSplit( &next, 2 ); subst = GetSubst( *(unsigned short*)GetText( next ) ); if( subst ) { PTEXT nextnext = NEXTLINE( next ); PTEXT prior = SegBreak( input ); if( !prior ) { *ppText = SegAppend( subst, nextnext ); } if( nextnext ) { SegBreak( nextnext ); if( !prior ) *ppText = nextnext; else SegAppend( prior, nextnext ); SegInsert( subst, nextnext ); input = nextnext; continue; } else { SegAppend( prior, (PTEXT)&subst ); input = NULL; continue; } } // if not subst... just continue stepping, no replacement nessecary? } } input = NEXTLINE( input ); } output = BuildLine( *ppText ); LineRelease( *ppText ); (*ppText) = output; }
void ParseURI( CTEXTSTR string ) { int state; PTEXT words; PTEXT delete_seg = NULL; PTEXT line = SegCreateFromText( string ); PTEXT filename = NULL; PTEXT varname = NULL; PTEXT varvalue = NULL; PTEXT content = NULL; int content_length; struct { uint32_t bInvalid : 1; uint32_t bGet : 1; uint32_t bPost : 1; uint32_t bBinary : 1; // reading the content... uint32_t bValue : 1; } flags; // we got a line, therefore we can process it... // only thing then is the last line in the block ... state = GET_FILENAME; words = burst( line ); delete_seg = words; // though... LineRelease( line ); flags.bValue = 0; while( words ) { DECLTEXT( page, WIDE("page") ); DECLTEXT( CGI, WIDE("CGI") ); //printf( "state:%d word:%s \r\n",state, GetText(words ) ); // what does stuff have now? the whole thign? a line? if( !GetTextSize( words ) ) switch( state ) { case GET_HTTP_EOL: state = GET_HTTP_METAVAR; break; case GET_HTTP_METAVAR: case GET_CGI: goto AddCGIVariable; break; } else switch( state ) { case RESET: state = GET_COMMAND; continue; // skip ahead and try new state; break; case GET_COMMAND: if( TextLike( words, WIDE("GET") ) ) { state = GET_FILENAME; //flags.bGet = TRUE; } else if( TextLike( words, WIDE("POST") ) ) { state = GET_FILENAME; //flags.bPost = TRUE; } else { flags.bInvalid = TRUE; } break; case GET_FILENAME: if( !filename && TextIs( words, WIDE("/") ) ) { // this is rude, and should never be done, // however this filter consumes all data anyhow, SO // mangling this will not hurt much... words->format.position.offset.spaces = 0; } if( TextIs( words, WIDE("?") ) || words->format.position.offset.spaces ) { if( !words->format.position.offset.spaces ) state = GET_CGI; else state = GET_HTTP_VERSION; filename = NEXTLINE( filename ); LineRelease( SegBreak( filename ) ); HTTPCollapse( &filename ); //AddVariableExxx( ps, ps->Current, (PTEXT)&page, filename, FALSE,TRUE,TRUE DBG_SRC ); //AddVariable( ps, ps->Current, (PTEXT)&CGI, TextDuplicate( NEXTLINE( words ), FALSE ) ); LineRelease( filename ); filename = NULL; } else { filename = SegAppend( filename, SegDuplicate( words ) ); } break; case GET_CGI: if( words->format.position.offset.spaces ) { state = GET_HTTP_VERSION; goto AddCGIVariable; } else { if( TextIs( words, WIDE("=") ) ) { HTTPCollapse( &varname ); flags.bValue = 1; } else if( TextIs( words, WIDE("&") ) ) { AddCGIVariable: HTTPCollapse( &varvalue ); HTTPCollapse( &varname ); if( TextLike( varname, WIDE("content-length") ) ) { content_length= IntCreateFromText( GetText( varvalue ) ); } { struct VAR *v = New( struct VAR ); v->varname = varname; varname = NULL; v->varvalue = varvalue; varvalue = NULL; AddLink( &l.vars, v ); } //AddVariableExxx( ps, ps->Current, pmdp->varname, pmdp->varvalue, FALSE,TRUE,TRUE DBG_SRC ); //LineRelease( varname ); //LineRelease( varvalue ); //varname = NULL; //varvalue = NULL; flags.bValue = 0; } else { if( flags.bValue ) { varvalue = SegAppend( varvalue, SegDuplicate( words ) ); } else { //printf( "add var" ); varname = SegAppend( varname, SegDuplicate( words ) ); } } } break; case GET_HTTP_VERSION: if( TextIs( words, WIDE("HTTP") ) ) { // okay - don't really do anything... next word is the version... } else { // TextIs( words, "/" ); // this is a token before the number... // Version better be something like 1.1 1.0? // well wait for EOL... state = GET_HTTP_EOL; } break; case GET_HTTP_EOL: if( !GetTextSize( words ) ) { state = GET_HTTP_METAVAR; } break; case GET_HTTP_METAVAR: { if( !flags.bValue && TextIs( words, WIDE(":") ) ) { flags.bValue = TRUE; } else { if( flags.bValue ) { varvalue = SegAppend( varvalue, SegDuplicate( words ) ); } else { varname = SegAppend( varname, SegDuplicate( words ) ); } } } break; case GET_HTTP_CONTENT: if( !content_length ) { DebugBreak(); state = RESET; } else { // hmm we've parsed everything up to here, but now we need blocks, binary blocks. content = SegAppend( content, words ); if( LineLength( content ) == content_length ) { //ProcessPostCGI( common.Owner, content ); //AddVariableExxx( ps, ps->Current, (PTEXT)&CGI, content, FALSE,TRUE,TRUE DBG_SRC ); //AddVariable( ps, ps->Current, (PTEXT)&CGI, content ); LineRelease( content ); //InvokeBehavior( "http.request", common.Owner->Current, common.Owner, NULL ); //InvokeBehavior( "http_request", common.Owner->Current, common.Owner, NULL ); state = RESET; } words = NULL; } break; }