PTEXT GetPokerHandName( PHAND pHand, PTEXT *ppLastValue ) { int value; PVARTEXT pvt; if( *ppLastValue ) LineRelease( *ppLastValue ); pvt = VarTextCreate(); value = ValuePokerHand( pHand, NULL, TRUE ); if( !value ) { vtprintf( pvt, WIDE("No hand") ); } else switch( value >> 20 ) { case 0: vtprintf( pvt, WIDE("High Card - %s"), cardlongnames[ ((value>>16)&0xF)-1 ] ); break; case 1: vtprintf( pvt, WIDE("A Pair of %ss"), cardlongnames[ (value&0xF) ] ); break; case 2: vtprintf( pvt, WIDE("2 Pair %ss and %ss") , cardlongnames[ (value&0xF0)>>4] , cardlongnames[ (value&0xF) ] ); break; case 3: vtprintf( pvt, WIDE("3 of a Kind %ss"), cardlongnames[ (value&0xF) ] ); break; case 4: vtprintf( pvt, WIDE("Straight") ); break; case 5: vtprintf( pvt, WIDE("Flush") ); break; case 6: vtprintf( pvt, WIDE("Full House %ss over %ss") , cardlongnames[ (value&0xF0)>>4] , cardlongnames[ (value&0xF) ] ); break; case 7: vtprintf( pvt, WIDE("4 of a Kind %ss"), cardlongnames[(value&0xF0)>>4] ); break; case 8: vtprintf( pvt, WIDE("Straight Flush") ); break; case 9: vtprintf( pvt, WIDE("Royal Flush") ); break; case 10: vtprintf( pvt, WIDE("5 of a Kind %ss"), cardlongnames[value&0xF] ); break; } *ppLastValue = VarTextGet( pvt ); VarTextDestroy( &pvt ); return *ppLastValue; }
//-------------------------------------- int CPROC PARSE( PSENTIENT ps, PTEXT parameters ) { PTEXT temp; PTEXT devname; if( ( devname = GetParam( ps, ¶meters ) ) ) { // 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, ¶meters ) ) ) { 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; }
int FillControlsList( PSI_CONTROL control, int nLevel, CTEXTSTR basename, CTEXTSTR priorname ) { CTEXTSTR name; PCLASSROOT data = NULL; PVARTEXT pvt = NULL; int added = 0; PLISTITEM pli; if( priorname ) { pli = AddListItemEx( control, nLevel, priorname ); } for( name = GetFirstRegisteredName( basename, &data ); name; name = GetNextRegisteredName( &data ) ) { pvt = VarTextCreate(); vtprintf( pvt, WIDE("%s/%s"), basename, name ); if( priorname && ( strcmp( name, WIDE( "button_create" ) ) == 0 ) ) { // okay then add this one... //snprintf( newname, sizeof( newname ), WIDE("%s/%s"), basename, name ); //if( NameHasBranches( &data ) ) { // eat the first two parts - intershell/controls/ // create the control name as that... CTEXTSTR controlpath = strchr( basename, '/' ); if( controlpath ) { controlpath++; controlpath = strchr( controlpath, '/' ); if( controlpath ) controlpath++; } //AddLink( &g.extra_types, StrDup( basename ) ); added++; SetItemData( pli, (uintptr_t)StrDup( controlpath ) ); break; } } else { if( NameHasBranches( &data ) ) { added += FillControlsList( control, nLevel+1, GetText( VarTextPeek( pvt ) ), name ); } } } if( !added ) DeleteListItem( control, pli ); VarTextDestroy( &pvt ); return added; }
int SQLQueryf( PODBC odbc, CTEXTSTR *result, CTEXTSTR fmt, ... ) { int result_code; PTEXT cmd; PVARTEXT pvt = VarTextCreate(); va_list args; va_start( args, fmt ); vvtprintf( pvt, fmt, args ); cmd = VarTextGet( pvt ); VarTextDestroy( &pvt ); result_code = SQLQueryEx( odbc, (CTEXTSTR)GetText( cmd ), result DBG_ARGS(SQLQueryf) ); LineRelease( cmd ); return result_code; }
int DoSQLRecordQueryf( int *nResults, CTEXTSTR **result, CTEXTSTR **fields, CTEXTSTR fmt, ... ) { int result_code; PTEXT cmd; PVARTEXT pvt = VarTextCreate(); va_list args; va_start( args, fmt ); vvtprintf( pvt, fmt, args ); cmd = VarTextGet( pvt ); VarTextDestroy( &pvt ); result_code = SQLRecordQueryEx( NULL, GetText( cmd ), nResults, result, fields DBG_ARGS(DoSQLRecordQueryf) ); LineRelease( cmd ); return result_code; }
int DoSQLCommandf( CTEXTSTR fmt, ... ) { int result; PTEXT cmd; PVARTEXT pvt = VarTextCreate(); va_list args; va_start( args, fmt ); vvtprintf( pvt, fmt, args ); cmd = VarTextGet( pvt ); VarTextDestroy( &pvt ); result = DoSQLCommandEx( GetText( cmd ) DBG_ARGS(DoSQLCommandf) ); LineRelease( cmd ); return result; }
//-------------------------------------- int CPROC MEMORY( PSENTIENT ps, PTEXT parameters ) { uint32_t nFree, nUsed, nChunks, nFreeChunks; PVARTEXT vt; PTEXT temp, next; next = GetParam( ps, ¶meters ); while( temp = next ) { if( temp && TextLike( temp, WIDE("log") ) ) { temp = GetParam( ps, ¶meters ); if( temp && TextLike( temp, WIDE("off") ) ) SetAllocateLogging( FALSE ); else { SetAllocateLogging( TRUE ); if( temp ) { next = temp; continue; } } } if( temp && TextLike( temp, WIDE("debug") ) ) { temp = GetParam( ps, ¶meters ); if( temp && TextLike( temp, WIDE("off") ) ) SetAllocateDebug( TRUE ); else { SetAllocateDebug( FALSE ); if( temp ) { next = temp; continue; } } } next = GetParam( ps, ¶meters ); } GetMemStats( &nFree, &nUsed, &nChunks, &nFreeChunks ); vt = VarTextCreate( ); vtprintf( vt, WIDE("Memory : Free %ld(%ld), Used %ld(%ld) (optional paramters \'log\',\'debug\' followed by off to disable."), nFree, nFreeChunks, nUsed, nChunks - nFreeChunks ); EnqueLink( &ps->Command->Output, VarTextGet( vt ) ); VarTextDestroy( &vt ); return FALSE; }
int SQLCommandf( PODBC odbc, CTEXTSTR fmt, ... ) { int result; PTEXT cmd; PVARTEXT pvt = VarTextCreateExx( 4096, 16384 * 16 ); va_list args; va_start( args, fmt ); vvtprintf( pvt, fmt, args ); cmd = VarTextGet( pvt ); if( cmd ) { VarTextDestroy( &pvt ); result = SQLCommandEx( odbc, GetText( cmd ) DBG_ARGS(SQLCommandf) ); LineRelease( cmd ); } else lprintf( WIDE("ERROR: Sql format failed: %s"), fmt ); return result; }
// Run a program completely detached from the current process // it runs independantly. Program does not suspend until it completes. // No way at all to know if the program works or fails. SYSTEM_PROC( PTASK_INFO, LaunchPeerProgramExx )( CTEXTSTR program, CTEXTSTR path, PCTEXTSTR args , int flags , TaskOutput OutputHandler , TaskEnd EndNotice , uintptr_t psv DBG_PASS ) { PTASK_INFO task; if( program && program[0] ) { #ifdef WIN32 int launch_flags = ( ( flags & LPP_OPTION_NEW_CONSOLE ) ? CREATE_NEW_CONSOLE : 0 ) | ( ( flags & LPP_OPTION_NEW_GROUP ) ? CREATE_NEW_PROCESS_GROUP : 0 ) ; PVARTEXT pvt = VarTextCreateEx( DBG_VOIDRELAY ); PTEXT cmdline; PTEXT final_cmdline; LOGICAL needs_quotes; TEXTSTR expanded_path = ExpandPath( program ); char *new_path; TEXTSTR expanded_working_path = path?ExpandPath( path ):ExpandPath( WIDE(".") ); int first = TRUE; //TEXTCHAR saved_path[256]; task = (PTASK_INFO)AllocateEx( sizeof( TASK_INFO ) DBG_RELAY ); MemSet( task, 0, sizeof( TASK_INFO ) ); task->psvEnd = psv; task->EndNotice = EndNotice; if( l.ExternalFindProgram ) { new_path = l.ExternalFindProgram( expanded_path ); if( new_path ) { Release( expanded_path ); expanded_path = new_path; } } #ifdef _DEBUG xlprintf(LOG_NOISE)( WIDE("%s[%s]"), path, expanded_working_path ); #endif if( StrCmp( path, WIDE(".") ) == 0 ) { path = NULL; Release( expanded_working_path ); expanded_working_path = NULL; } if( expanded_path && StrChr( expanded_path, ' ' ) ) needs_quotes = TRUE; else needs_quotes = FALSE; if( needs_quotes ) vtprintf( pvt, WIDE( "\"" ) ); /* if( !IsAbsolutePath( expanded_path ) && expanded_working_path ) { //lprintf( "needs working path too" ); vtprintf( pvt, WIDE("%s/"), expanded_working_path ); } */ vtprintf( pvt, WIDE("%s"), expanded_path ); if( needs_quotes ) vtprintf( pvt, WIDE( "\"" ) ); if( flags & LPP_OPTION_FIRST_ARG_IS_ARG ) ; else { if( args && args[0] )// arg[0] is passed with linux programs, and implied with windows. args++; } while( args && args[0] ) { if( args[0][0] == 0 ) vtprintf( pvt, WIDE( " \"\"" ) ); else if( StrChr( args[0], ' ' ) ) vtprintf( pvt, WIDE(" \"%s\""), args[0] ); else vtprintf( pvt, WIDE(" %s"), args[0] ); first = FALSE; args++; } cmdline = VarTextGet( pvt ); vtprintf( pvt, WIDE( "cmd.exe /c %s" ), GetText( cmdline ) ); final_cmdline = VarTextGet( pvt ); VarTextDestroy( &pvt ); MemSet( &task->si, 0, sizeof( STARTUPINFO ) ); task->si.cb = sizeof( STARTUPINFO ); #ifdef _DEBUG xlprintf(LOG_NOISE)( WIDE( "quotes?%s path [%s] program [%s] [cmd.exe (%s)]"), needs_quotes?WIDE( "yes"):WIDE( "no"), expanded_working_path, expanded_path, GetText( final_cmdline ) ); #endif /* if( path ) { GetCurrentPath( saved_path, sizeof( saved_path ) ); SetCurrentPath( path ); } */ task->OutputEvent = OutputHandler; if( OutputHandler ) { SECURITY_ATTRIBUTES sa; sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; sa.nLength = sizeof( sa ); CreatePipe( &task->hReadOut, &task->hWriteOut, &sa, 0 ); //CreatePipe( &hReadErr, &hWriteErr, &sa, 0 ); CreatePipe( &task->hReadIn, &task->hWriteIn, &sa, 0 ); task->si.hStdInput = task->hReadIn; task->si.hStdError = task->hWriteOut; task->si.hStdOutput = task->hWriteOut; task->si.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; if( !( flags & LPP_OPTION_DO_NOT_HIDE ) ) task->si.wShowWindow = SW_HIDE; else task->si.wShowWindow = SW_SHOW; } else { task->si.dwFlags |= STARTF_USESHOWWINDOW; if( !( flags & LPP_OPTION_DO_NOT_HIDE ) ) task->si.wShowWindow = SW_HIDE; else task->si.wShowWindow = SW_SHOW; } { HINSTANCE hShellProcess = 0; int success = 0; #ifdef WIN32 if( flags & LPP_OPTION_IMPERSONATE_EXPLORER ) { HANDLE hExplorer = GetImpersonationToken(); if( ( CreateProcessAsUser( hExplorer, NULL //program , GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( CreateProcessAsUser( hExplorer, program , GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( CreateProcessAsUser( hExplorer, program , NULL // GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( CreateProcessAsUser( hExplorer, WIDE( "cmd.exe" ) , GetText( final_cmdline ) , NULL, NULL, TRUE , launch_flags | CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) ) { success = 1; } CloseHandle( hExplorer ); } else #endif { if( ( CreateProcess( NULL //program , GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | ( OutputHandler?CREATE_NO_WINDOW:0 )//CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( CreateProcess( program , GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | ( OutputHandler?CREATE_NO_WINDOW:0 )//CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( CreateProcess( program , NULL // GetText( cmdline ) , NULL, NULL, TRUE , launch_flags | ( OutputHandler?CREATE_NO_WINDOW:0 )//CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || ( TryShellExecute( task, expanded_working_path, program, cmdline ) ) || ( CreateProcess( NULL//WIDE( "cmd.exe" ) , GetText( final_cmdline ) , NULL, NULL, TRUE , launch_flags | ( OutputHandler?CREATE_NO_WINDOW:0 )//CREATE_NEW_PROCESS_GROUP , NULL , expanded_working_path , &task->si , &task->pi ) || FixHandles(task) || DumpError() ) || 0 ) { success = 1; } } if( success ) { //CloseHandle( task->hReadIn ); //CloseHandle( task->hWriteOut ); #ifdef _DEBUG xlprintf(LOG_NOISE)( WIDE("Success running %s[%s] in %s (%p): %d"), program, GetText( cmdline ), expanded_working_path, task->pi.hProcess, GetLastError() ); #endif if( OutputHandler ) { task->hStdIn.handle = task->hWriteIn; task->hStdIn.pLine = NULL; //task->hStdIn.pdp = pdp; task->hStdIn.hThread = 0; task->hStdIn.bNextNew = TRUE; task->hStdOut.handle = task->hReadOut; task->hStdOut.pLine = NULL; //task->hStdOut.pdp = pdp; task->hStdOut.bNextNew = TRUE; task->hStdOut.hThread = ThreadTo( HandleTaskOutput, (uintptr_t)task ); ThreadTo( WaitForTaskEnd, (uintptr_t)task ); } else { //task->hThread = ThreadTo( WaitForTaskEnd, (uintptr_t)task ); } } else { xlprintf(LOG_NOISE)( WIDE("Failed to run %s[%s]: %d"), program, GetText( cmdline ), GetLastError() ); CloseHandle( task->hWriteIn ); CloseHandle( task->hReadIn ); CloseHandle( task->hWriteOut ); CloseHandle( task->hReadOut ); CloseHandle( task->pi.hProcess ); CloseHandle( task->pi.hThread ); Release( task ); task = NULL; } } LineRelease( cmdline ); LineRelease( final_cmdline ); Release( expanded_working_path ); Release( expanded_path ); /* if( path ) SetCurrentPath( saved_path ); */ return task; #endif #ifdef __LINUX__ { pid_t newpid; TEXTCHAR saved_path[256]; xlprintf(LOG_ALWAYS)( WIDE("Expand Path was not implemented in linux code!") ); task = (PTASK_INFO)Allocate( sizeof( TASK_INFO ) ); MemSet( task, 0, sizeof( TASK_INFO ) ); task->psvEnd = psv; task->EndNotice = EndNotice; task->OutputEvent = OutputHandler; if( OutputHandler ) { pipe(task->hStdIn.pair); task->hStdIn.handle = task->hStdIn.pair[1]; pipe(task->hStdOut.pair); task->hStdOut.handle = task->hStdOut.pair[0]; } // always have to thread to taskend so waitpid can clean zombies. ThreadTo( WaitForTaskEnd, (uintptr_t)task ); if( path ) { GetCurrentPath( saved_path, sizeof( saved_path ) ); SetCurrentPath( path ); } if( !( newpid = fork() ) ) { char *_program = CStrDup( program ); // in case exec fails, we need to // drop any registered exit procs... //close( task->hStdIn.pair[1] ); //close( task->hStdOut.pair[0] ); //close( task->hStdErr.pair[0] ); if( OutputHandler ) { dup2( task->hStdIn.pair[0], 0 ); dup2( task->hStdOut.pair[1], 1 ); dup2( task->hStdOut.pair[1], 2 ); } DispelDeadstart(); //usleep( 100000 ); execve( _program, (char *const*)args, environ ); lprintf( WIDE( "Direct execute failed... trying along path..." ) ); { char *tmp = strdup( getenv( "PATH" ) ); char *tok; for( tok = strtok( tmp, ":" ); tok; tok = strtok( NULL, ":" ) ) { char fullname[256]; snprintf( fullname, sizeof( fullname ), "%s/%s", tok, _program ); lprintf( WIDE( "program:[%s]" ), fullname ); ((char**)args)[0] = fullname; execve( fullname, (char*const*)args, environ ); } Release( tmp ); } if( OutputHandler ) { close( task->hStdIn.pair[0] ); close( task->hStdOut.pair[1] ); } //close( task->hWriteErr ); close( 0 ); close( 1 ); close( 2 ); lprintf( WIDE( "exec failed - and this is ALLL bad... %d" ), errno ); //DebugBreak(); // well as long as this runs before // the other all will be well... task = NULL; // shit - what can I do now?! exit(0); // just in case exec fails... need to fault this. } else { if( OutputHandler ) { close( task->hStdIn.pair[0] ); close( task->hStdOut.pair[1] ); } } if( OutputHandler ) ThreadTo( HandleTaskOutput, (uintptr_t)task ); task->pid = newpid; lprintf( WIDE("Forked, and set the pid..") ); // how can I know if the command failed? // well I can't - but the user's callback will be invoked // when the above exits. if( path ) { // if path is NULL we didn't change the path... SetCurrentPath( saved_path ); } return task; } #endif } return FALSE; }
int CPROC FillList( uintptr_t psv, CTEXTSTR name, POPTION_TREE_NODE ID, int flags ) { PLISTFILL plf = (PLISTFILL)psv; LISTFILL lf = *plf; if( StrCmp( name, WIDE(".") ) == 0 ) return TRUE; lf.nLevel++; lf.flags.bSecondLevel = 1; lprintf( WIDE("%d - %s (%p)"), plf->nLevel, name, ID ); { if( lf.nLevel == 1 ) { if( pvtIni ) { PTEXT pINI = VarTextGet( pvtIni ); CRYPTOEncryptMemory( GetText( pINI ), GetTextSize( pINI ) ); if( output ) { fwrite( GetText(pINI), 1, GetTextSize( pINI ), output ); LineRelease( pINI ); fclose( output ); output = NULL; } VarTextDestroy( &pvtIni ); } // if it's this branch, then don't auto export it. if( StrCmp( name, WIDE("INI Store") ) == 0 ) return TRUE; pvtIni = VarTextCreate(); output = sack_fopen( 0, name, WIDE("wt") ); } else if( lf.nLevel == 2 ) { lprintf( WIDE("Section %s"), name ); vtprintf( pvtSection, WIDE("%s"), name ); } else if( lf.nLevel >= 3 ) { POPTION_TREE_NODE ID_Value = GetOptionValueIndex( ID ); if( ID_Value ) { TEXTCHAR buffer[256]; if( (int)GetOptionStringValue( ID_Value, buffer, sizeof( buffer ) ) > 0 ) { if( VarTextPeek( pvtSection ) ) { PTEXT section =VarTextGet( pvtSection ); vtprintf( pvtIni, WIDE("[%s]\n"), GetText( section ) ); LineRelease( section ); } vtprintf( pvtIni, WIDE("%s=%s\n"), name, buffer ); } else { vtprintf( pvtSection, WIDE("/%s"), name ); } } else { vtprintf( pvtSection, WIDE("/%s"), name ); } } } EnumOptions( ID, FillList, (uintptr_t)&lf ); //lprintf( WIDE("done with all children under this node.") ); return TRUE; }
//-------------------------------------- int CPROC COMMAND( PSENTIENT ps, PTEXT parameters ) { PTEXT temp; PTEXT devname; if( ( devname = GetParam( ps, ¶meters ) ) ) { // 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, ¶meters ) ) ) { 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; }