Example #1
0
//--------------------------------------
int CPROC PARSE( PSENTIENT ps, PTEXT parameters )
{
	PTEXT temp;
	PTEXT devname;
	if( ( devname = GetParam( ps, &parameters ) ) )
	{
	// find device to see if it's already open...
	// by name!
	// but - this causes a problem for nested scripts
	// which are going to have to be artificiailly inceminated
	// with a unique name maybe something like script_#####
		PDATAPATH pdp = FindOpenDevice( ps, devname );
		if( pdp )
		{
			if( !ps->CurrentMacro )
			{
				PVARTEXT pvt;
				pvt = VarTextCreate();
				vtprintf( pvt, WIDE("Device named %s is already open."), GetText( devname ) );
				EnqueLink( &ps->Command->Output, (POINTER)VarTextGet( pvt ) );
				VarTextDestroy( &pvt );
			}
			return -1;
		}
		if( ( temp = GetParam( ps, &parameters ) ) )
		{
			PDATAPATH pdp;
			lprintf( WIDE("Opening device handle named: %s"), GetText( devname ) );
			if( ( pdp = OpenDevice( &ps->Data, ps, temp, parameters ) ) )
			{
				while( devname->flags & TF_INDIRECT )
					devname = GetIndirect( devname );
				pdp->pName = SegDuplicate( devname );
				if( pdp->Type )
				{
					if( ps->CurrentMacro )
					{
						ps->CurrentMacro->state.flags.bSuccess = TRUE;
					}
				}
				else
				{
					DestroyDataPath( pdp ); // new one did not finish open...
				}
			}
		}
		else
		{
			DECLTEXT( msg, WIDE("Parse must specify a name (/(parse/open) <name> <device> <options...)") );
			EnqueLink( &ps->Command->Output, (PTEXT)&msg );
		}
	}
	return FALSE;
}
Example #2
0
static PTEXT GetSubst( int _2bytecode )
{
	DECLTEXTSZ( subst, 2);
   subst.data.size = 1;
	switch( _2bytecode )
	{
	case '+': subst.data.data[0] = ' '; return SegDuplicate( (PTEXT)&subst );
	case '02': subst.data.data[0] = '\x20'; return SegDuplicate( (PTEXT)&subst );
	case 'c2': case 'C2': subst.data.data[0] = '\x2C'; return SegDuplicate( (PTEXT)&subst );
	case 'b5': case 'B5': subst.data.data[0] = '\x5B'; return SegDuplicate( (PTEXT)&subst );
	case 'd5': case 'D5': subst.data.data[0] = '\x5D'; return SegDuplicate( (PTEXT)&subst );
	case 'f2': case 'F2': subst.data.data[0] = '\x2F'; return SegDuplicate( (PTEXT)&subst );
	case '22': subst.data.data[0] = '\x22'; return SegDuplicate( (PTEXT)&subst );
	case '72': subst.data.data[0] = '\x27'; return SegDuplicate( (PTEXT)&subst );
	case '62': subst.data.data[0] = '\x26'; return SegDuplicate( (PTEXT)&subst );
	}
   return NULL;
}
Example #3
0
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;
		}
Example #4
0
int CPROC MakeProcess( PSENTIENT ps, PENTITY peInit, PTEXT parameters )
{
	// parameters specify command and parameters to launch...
	// perhaps a working directory? May or may not want it
	// in the current directory...
	// /make process test WIDE("command arguments") WIDE("work path") <attributes?>
	// /make process test WIDE("notepad trigger.txt") 
	PSENTIENT ps2;
	PPROCESS process = New( PROCESS );
	TEXTCHAR MyPath[256];
	GetCurrentPath( MyPath, sizeof( MyPath ) );
	ps2 = CreateAwareness( peInit );
	MemSet( process, 0, sizeof( PROCESS ) );
	Log( WIDE("Have a process, and woke it up... setting the link") );
	SetLink( &peInit->pPlugin, iProcess, process );
	SetLink( &peInit->pDestroy, iProcess, DestroyProcess );
	Log( WIDE("Set the link, getting params...") );
	{
		PTEXT text, cmd = NULL;
		text = GetParam( ps, &parameters );
		if( text && TextIs( text, WIDE("\"") ) )
		{
	      Log( WIDE("Found a quote, getting command line") );
			while( (text = GetParam( ps, &parameters )) && !TextIs( text, WIDE("\"") ) )
			{
				cmd = SegAppend( cmd, SegDuplicate( text ) );
			}
			cmd->format.position.offset.spaces = 0;
			process->command = BuildLine( cmd );
			if( text ) // closed, and may have start path....
			{
				text = GetParam( ps, &parameters );
		   	if( text && TextIs( text, WIDE("\"") ) )
				{
					Log( WIDE("Found a quote, getting the path") );
					while( (text = GetParam( ps, &parameters )) && !TextIs( text, WIDE("\"") ) )
					{
						cmd = SegAppend( cmd, SegDuplicate( text ) );
		   		}
		   		cmd->format.position.offset.spaces = 0;
		   		process->directory = BuildLine( cmd );
				}
			}
		}
		else
		{
			DECLTEXT( msg, WIDE("Must specify process to execute in quotes (\")") );
			EnqueLink( &ps->Command->Output, &msg );
		WakeAThread( ps2 );
			return -1; // abort creation.
		}
	}
	Log2( WIDE("Starting %s in %s"), GetText( process->command ), GetText( process->directory ) );
	process->si.cb = sizeof( process->si );
	// remaining startup info members are NULL - specifying we do NOT care
	// why why when where how the process starts.
	if( StartProcess( process ) )
	{
		DECLTEXTSZ( msg, 256 );
		msg.data.size = snprintf( msg.data.data, 256*sizeof(TEXTCHAR), WIDE("Failed to start \"%s\" in \"%s\" error: %ld"),
										 GetText( process->command ),
										 GetText( process->directory ),
										 GetLastError() );
		EnqueLink( &ps->Command->Output, SegDuplicate( (PTEXT)&msg ) );
		WakeAThread( ps2 );
		return -1; // abort creation.
	}
	// well otherwise would seem we've launched a valid application
	// we have valid process and thread handles to it which can be monitored
	// and well that's about that.
	WakeAThread( ps2 );
	return 0;
}
Example #5
0
void ProcessEnum( void )
{
	PTEXT pLine = GetCurrentWord();
   char *text;
	// look for enum keyword....
	while( pLine )
	{
      text = GetText( pLine );
		if( TextIs( pLine, WIDE("enum") ) )
		{
			if( g.current_enum )
			{
				fprintf( stderr, WIDE("%s(%d): Syntax error enumeration within enum?\n")
                    , GetCurrentFileName(), GetCurrentLine() );
			}
			g.current_enum = Allocate( sizeof( ENUM_TABLE ) );
			g.current_enum->entries = NULL;
			g.current_enum->name = NULL;
			if( g.current_entry )
			{
				fprintf( stderr, WIDE("Coding error - uncommited enumeration value. Lost memory.\n") );
            g.current_entry = NULL;
			}
         g.flags.get_identifier = 1;
		}
		else
		{
			if( g.flags.get_identifier )
			{
				if( text[0] != '{' )
				{
					if( g.current_enum->name )
					{
						fprintf( stderr, WIDE("%s(%d): Error multiple identifers for enum\n")
								 , GetCurrentFileName(), GetCurrentLine() );
					}
					else
                  g.current_enum->name = SegDuplicate( pLine );
				}
				else
				{
					// well whether or not there was an identifier (name)
               // we're done getting it....
					g.flags.get_identifier = 0;
				}
			}
			else // collecting enumeration values...
			{
				if( text[0] == '}' )
				{
				}
				else if( text[0] == ',' )
				{
				}
				else if( text[0] == '=' )
				{
				}
            else if( g.current_entry )
				{
					if( text[0] == ',' )
					{
						if( !g.flags.current_value_set )
						{
                     if( g.current_enum->last_entry )
								g.current_entry->value = g.current_enum->last_entry->value + 1;
							else
								g.current_entry->value = 0;
						}
						if( g.current_enum->last_entry )
							g.current_enum->last_entry->next = g.current_entry;
						else
                     g.current_enum->entries = g.current_entry;
						g.current_enum->last_entry = g.current_entry;
                  g.current_entry = NULL;
					}
					else
					{
					}
				}
				else
				{
					if( text[0] == ',' )
					{
                  fprintf( stderr, WIDE("Error - unexpected comma sepeartor enumeration.\n") );
					}
					if( text[0] == '=' )
					{
                  fprintf( stderr, WIDE("Error - unexpected comma sepeartor enumeration.\n") );
					}
					g.current_entry = Allocate( sizeof( ENUM_ENTRY ) );
               g.flags.current_value_set = 0;
					g.current_entry->next = NULL;
					g.current_entry->name = SegDuplicate( pLine );
				}
			}
		}
      pLine = NEXTLINE( pLine );
	}
}
Example #6
0
int GetInputData( int bWord, PSENTIENT ps, PTEXT parameters )
{
	if( ps->Data )
	{
		PTEXT pSave, temp;
		PTEXT *pInd = NULL;
		pSave = parameters;
		lprintf( WIDE("Get data from datapath...") );
		if( !( ( temp = GetParam( ps, &parameters ) ) ) )
		{
			lprintf( WIDE("No parameters specified to get line") );
			LineRelease( ps->pLastResult );
			ps->pLastResult = NULL;
			pInd = &ps->pLastResult;
		}
		else
		{
			if( temp == pSave )
			{
				DECLTEXT( msg, WIDE("is not a valid variable reference.") );
				EnqueLink( &ps->Command->Output, SegAppend( SegDuplicate( temp ), (PTEXT)&msg ) );
				return FALSE;
			}
			if( temp->flags & TF_INDIRECT )
			{
				LineRelease( GetIndirect( temp ) );
				SetIndirect( temp, NULL );
				pInd = (PTEXT*)&temp->data.size;
			}
			else
			{
				DECLTEXT( msg, WIDE("is not a valid variable reference(not indirect).") );
				EnqueLink( &ps->Command->Output, SegAppend( SegDuplicate( temp ), (PTEXT)&msg ) );
				return FALSE;
			}
		}

		if( !pInd )
		{
			DECLTEXT( msg, WIDE("FATAL ERROR: Did not set pInd (GetLine)") );
			EnqueLink( &ps->Command->Output, (PTEXT)&msg );
				return FALSE;
		  }
		// try grabbing an existing line - could be put there from BURST of prior...

		if( !ps->Data->CurrentWord ) // just got last word off line...
		{
			if( ps->Data->CurrentLine )
			{
				LineRelease( ps->Data->CurrentLine );
				ps->Data->CurrentLine = NULL;
			}
		}

		if( !ps->Data->CurrentLine )
		{
			 // Gather Data from input source....
		 // data from HT_CLIENT 'appears' asynchrous
		  // to scripting....
			if( ps->Data->Read )
				ps->Data->Read( ps->Data ); // this returns a completeline...
			ps->Data->CurrentLine = (PTEXT)DequeLink( &ps->Data->Input );
		}

		if( ps->Data->CurrentLine )
			if( !ps->Data->CurrentWord )
				ps->Data->CurrentWord = ps->Data->CurrentLine;

		if( ps->Data->CurrentWord )
		{
			// indirect pointer will be null.
			if( bWord )
			{
				*pInd = SegDuplicate( ps->Data->CurrentWord );
				ps->Data->CurrentWord = NEXTLINE( ps->Data->CurrentWord );
			}
			else
			{
				// duplicate to end of line... which may be whole - or
				// may be partial because of prior GETWORD from the line...
				// then release the data input line...
				// must duplicate the data in either case cause
				// it will be deleted with additional setting of the
				// variable...
					 *pInd = TextDuplicate( ps->Data->CurrentWord, FALSE );
				LineRelease( ps->Data->CurrentWord );
				ps->Data->CurrentLine = NULL; // do NOT release this line...
				ps->Data->CurrentWord = NULL; // no word on line
			}
		}
		if( ps->CurrentMacro && *pInd )
			ps->CurrentMacro->state.flags.bSuccess = TRUE;
	}
	 return FALSE;
}
Example #7
0
//--------------------------------------
int CPROC COMMAND( PSENTIENT ps, PTEXT parameters )
{
	PTEXT temp;
	 PTEXT devname;
	 if( ( devname = GetParam( ps, &parameters ) ) )
	 {
		  // find device to see if it's already open...
		  // by name! 
		  // but - this causes a problem for nested scripts
		  // which are going to have to be artificiailly inceminated
		  // with a unique name maybe something like script_#####
		  PDATAPATH pdp = FindOpenDevice( ps, devname );
		  if( pdp )
		  {
			if( !ps->CurrentMacro )
			{
					 PVARTEXT pvt;
					 pvt = VarTextCreate();
					 vtprintf( pvt, WIDE("Device named %s is already open."), GetText( devname ) );
					 EnqueLink( &ps->Command->Output, (POINTER)VarTextGet( pvt ) );
					 VarTextDestroy( &pvt );
				}
				return -1;
		  }
		 if( ( temp = GetParam( ps, &parameters ) ) )
		  {
			  PDATAPATH pdp;

			  //lprintf( WIDE("Opening device handle named: %s"), GetText( devname ) );
			  if( ( pdp = OpenDevice( &ps->Command, ps, temp, parameters ) ) )
			  {
				  PTEXT pCommand;
				  PDATAPATH pOldPath;
				  while( devname->flags & TF_INDIRECT )
					  devname = GetIndirect( devname );
				  pdp->pName = SegDuplicate( devname );
				  if( ps->Command )
				  {
				  // remove old command queue so close won't remove
				  // ps->Command->Input = NULL;
				  // close the command queue...

				  // update to the new queue in the sentience
				  // if the new one is a data source, then we
				  // need to move old commands to new path -
				  // otherwise they may/will get lost.
					  if( ps->Command->flags.Data_Source
						 && !ps->Command->flags.Dont_Relay_Prior)
								 {
									 pOldPath = ps->Command->pPrior;

												 // move all commands from old queue to new queue
									 while( ( pCommand = (PTEXT)DequeLink( &pOldPath->Input ) ) )
									 {
										 EnqueLink( &ps->Command->Input, pCommand );
									 }

									 while( ( pCommand = (PTEXT)DequeLink( &pOldPath->Output ) ) )
									 {
										 EnqueLink( &ps->Command->Output, pCommand );
									 }
								 }
				  }
				  if( ps->CurrentMacro )
					  ps->CurrentMacro->state.flags.bSuccess = TRUE;
			  }
		  }
		  else
		  {
				DECLTEXT( msg, WIDE("Command must specify a name (/command <name> <device> <options...)") );
				EnqueLink( &ps->Command->Output, (PTEXT)&msg );
		  }
	}
	return FALSE;
}