BOOL SetVariable( CStr &varName, CValue value ) { VarError.Empty(); int pos; CValue *val; if ( varName.GetLength() > 0 && varName.GetAt(0) == L'[' ) { CStr expression = varName.Mid( 1 ); CInterpreter parser; CValue result = parser.EvaluateExpression( expression, FALSE ); int subPos = parser.GetErrorPosition(); if ( parser.GetError() != 0 ) { VarError = CStr( L"Error in variable reference: " ) + parser.GetErrorMessage(); return FALSE; } if ( expression[subPos] != L']' ) { VarError = L"Variable reference not closed (missing ']')"; return FALSE; } val = GetVariable( (CStr)result, TRUE, &pos ); } else{ val = GetVariable( varName, TRUE, &pos ); } val->CopyFrom( value ); return (pos != -1); }
BOOL CMortScriptApp::InitInstance() { SetRegistryKey( L"Mort" ); CParseCmdLine myCmdLine; myCmdLine.ParseCmdLine( m_lpCmdLine ); TCHAR exe[MAX_PATH]; ::GetModuleFileName(NULL,exe,MAX_PATH); ExecuteFileName = exe; CStr file = myCmdLine.Filename; // m_lpCmdLine; // file.TrimLeft( '\"' ); file.TrimRight( '\"' ); if ( myCmdLine.RegOnly || file.IsEmpty() ) { RegisterFileClass(); RegisterFileType( L".jscr", L"JScripts" ); RegisterFileType( L".mscr", L"JScripts" ); if ( !myCmdLine.RegOnly ) { MessageBox( NULL , L".jscr and .mscr extensions registered.\nPlease run any .jscr/.mscr file or read the manual.\n\n" L"(c) Mirko Schenk 2005-2007" , L"JScripts V" + CStr( VERSION_INFO ) , MB_OK|MB_SETFOREGROUND ); } } else { if ( file.GetLength() >= 4 /* file.GetLength() >= 8 && file.Right(8).CompareNoCase( L".mortrun" ) == 0 || file.GetLength() >= 5 && file.Right(5).CompareNoCase( L".mscr" ) == 0 */ ) { CStr mutexName = file; mutexName.MakeLower(); #ifdef DESKTOP // Windows XP doesn't like some path characters in the mutex' name mutexName.Replace( ':', '_' ); mutexName.Replace( '\\', '/' ); #endif MutexName = (LPCTSTR)mutexName; HANDLE mutex = ::CreateMutex(NULL, FALSE, MutexName); if ( mutex!=NULL ) { int exists = ::GetLastError(); if ( exists == ERROR_ALREADY_EXISTS) { DWORD procId = GetRunningScriptProcId( file ); if ( procId != NULL ) { /* CString msg; msg.Format( L"Process ID: %08x", procId ); MessageBox( NULL, msg, L"Debug", MB_SETFOREGROUND ); */ FindAppT findApp; findApp.procId = procId; findApp.hWnd = NULL; ::EnumWindows( FindApplicationWindowProc, (LPARAM)&findApp ); if ( findApp.hWnd != NULL ) { // msg.Format( L"Set foreground window: %08x", findApp.hWnd ); // MessageBox( NULL, msg, L"Debug", MB_SETFOREGROUND ); ::SetForegroundWindow( findApp.hWnd ); } } else exists = 0; //MessageBox( NULL, L"Process opened", L"Debug", MB_SETFOREGROUND ); /* TCHAR procName[256]; ::GetModuleFileName((HMODULE)procId,procName,256); //MessageBox( NULL, procName, L"Debug", MB_SETFOREGROUND ); if ( CString(procName).Right(14).CompareNoCase( L"MortScript.exe" ) == 0 ) { int aw = MessageBox( NULL , L"Script seems to be running. Cancel old script?" , L"Script already running" , MB_YESNO|MB_SETFOREGROUND ); if ( aw == IDYES ) { RegWriteDW( HKEY_CURRENT_USER, L"Software\\JScripts\\Abort", MutexName, 1 ); DWORD exitCode = 0; SetCursor(LoadStandardCursor(IDC_WAIT)); for ( int i=0; i<=10; i++ ) { Sleep(1000); if ( GetExitCodeProcess( hProc, &exitCode ) == FALSE ) { //MessageBox( NULL, L"GetExitCode failed", L"Debug", MB_SETFOREGROUND ); exitCode = 0; break; } else { if ( exitCode != STILL_ACTIVE ) { //MessageBox( NULL, L"No longer active", L"Debug", MB_SETFOREGROUND ); break; } } } SetCursor(LoadStandardCursor(IDC_ARROW)); if ( exitCode == STILL_ACTIVE ) { int aw = MessageBox( NULL , L"Script seems to be hanging or busy. Terminate old script?" , L"Script still running" , MB_YESNO|MB_SETFOREGROUND ); if ( aw == IDYES ) { TerminateProcess( hProc, 0 ); } } } } else { exists = 0; } CloseHandle( hProc ); } else { exists = 0; } */ } if ( exists != ERROR_ALREADY_EXISTS ) { HKEY key; if ( RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", 0, 0, &key ) == ERROR_SUCCESS ) { RegDeleteValue( key, MutexName ); RegCloseKey( key ); } DWORD currProcId = GetCurrentProcessId(); //CString dbg; //dbg.Format(L"ProcId: %d", GetCurrentProcessId()); //MessageBox( NULL, dbg, L"Debug", MB_SETFOREGROUND ); // Remove old script entries with same process id if ( RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", 0, 0, &key ) == ERROR_SUCCESS ) { int idx; TCHAR valName[MAX_PATH]; DWORD valSize = MAX_PATH, type; DWORD value, valueSize = sizeof(DWORD); CStrArray oldProcesses; for ( idx = 0; RegEnumValue( key, idx, valName, &valSize, NULL, &type, (BYTE*)&value, &valueSize ) == ERROR_SUCCESS; idx++ ) { if ( type == REG_DWORD && value == currProcId ) { oldProcesses.Add( valName ); } valSize = MAX_PATH; valueSize = sizeof(DWORD); } for ( idx = 0; idx < oldProcesses.GetSize(); idx++ ) { RegDeleteValue( key, oldProcesses.GetAt(idx) ); } RegCloseKey( key ); } RegWriteDW( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", MutexName, currProcId ); if ( myCmdLine.WaitForFile > 0 && (long)FileOrDirExists( file, 0 ) == 0 ) { CDlgWait wait(NULL); wait.AllowOK = TRUE; wait.Countdown = myCmdLine.WaitForFile; wait.Expression = L"FileExists(\"" + file + L"\")"; wait.Title = L"JScripts"; wait.m_Label = L"Script " + file + " not found, waiting for existance (storage card not initialized?)"; wait.DoModal(); } if ( myCmdLine.WaitForFile <= 0 || (long)FileOrDirExists( file, 0 ) == 1 ) { g_hInst = theApp.m_hInstance; SYSTEMTIME now; GetLocalTime( &now ); long seed = SystemTimeToUnixTime( now ); srand( seed ); AppPath = m_pszHelpFilePath; AppPath = AppPath.Left( AppPath.ReverseFind('\\') ); ScriptAborted = CreateEvent( 0, TRUE, FALSE, CStr(MutexName)+L"ScriptAborted" ); StatusDialog = new CDlgStatus( file ); //status.DoModal(); CInterpreter interpreter; //Debug(file); //jwz:modi interpreter.RunFile( file ); interpreter.Parser(); //jwz:modi end if ( StatusWindow != NULL ) { StatusDialog->ScriptFinished = TRUE; SendMessage( StatusWindow, WM_EXIT_STATUS, 0, 0 ); WaitForSingleObject( StatusDialogFinished, INFINITE ); CloseHandle( StatusDialogFinished ); } delete StatusDialog; UnloadToolhelp(); CloseHandle( ScriptAborted ); ReleaseMutex( mutex ); Variables.RemoveAll(); for ( int i = 0; i<LocalVariablesStack.GetSize(); i++ ) delete (CMapStrToValue*)LocalVariablesStack.GetAt(i); POSITION pos = FileHandles.GetStartPosition(); CStr key; void *value; while ( pos != 0 ) { FileHandles.GetNextAssoc( pos, key, value ); if ( value != NULL ) { delete (CFileInfo*)value; } } FileHandles.RemoveAll(); if ( ChoiceFont != NULL ) DeleteObject( ChoiceFont ); if ( StatusListFont != NULL ) DeleteObject( StatusListFont ); } if ( RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", 0, KEY_WRITE, &key ) == ERROR_SUCCESS ) { RegDeleteValue( key, MutexName ); RegCloseKey( key ); } } CloseHandle( mutex ); } else { int error = ::GetLastError(); MessageBox( NULL , L"Error creating mutex" , L"JScripts" , MB_OK|MB_SETFOREGROUND ); } } else { MessageBox( NULL , L"Invalid file type for MortScript" , L"JScripts" , MB_OK|MB_SETFOREGROUND ); } } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }
BOOL CIniFile::WriteFile( LPCTSTR filename, CStr &content, BOOL append, UINT cp ) { HANDLE file; char *buffer = NULL; char *tmpStr = NULL; ULONG iLen, feLen; BOOL rc = TRUE, comPort = FALSE; if ( filename[wcslen(filename)-1] == ':' ) { file = CreateFile( filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); if ( file != INVALID_HANDLE_VALUE ) SetComPort( filename, file ); comPort = TRUE; } else { CStr fileWithPath; fileWithPath = filename; if ( CurrentInterpreter != NULL && ( fileWithPath.GetLength() < 2 || ( fileWithPath.GetAt(0) != '\\' && fileWithPath.GetAt(1) != ':' ) ) ) { int len = CurrentInterpreter->ScriptFile.ReverseFind('\\'); if ( len == -1 ) fileWithPath = L"\\" + fileWithPath; else fileWithPath = CurrentInterpreter->ScriptFile.Left( len+1 ) + fileWithPath; } if ( append ) { file = CreateFile( fileWithPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if ( SetFilePointer( file, 0, NULL, FILE_END ) == 0 ) append = FALSE; } else file = CreateFile( fileWithPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); } if ( file == INVALID_HANDLE_VALUE ) rc = FALSE; if ( rc == TRUE ) { iLen = content.GetLength(); if ( cp != CP_UNICODE ) { tmpStr = new char[iLen*2+1]; // special characters might use 2 bytes! if ( tmpStr == NULL ) rc = FALSE; } else tmpStr = NULL; } if ( rc == TRUE ) { ULONG written; int stat; if ( cp != CP_UNICODE && cp != CP_UNICODE_PREFIX ) { if ( cp == CP_UTF8_PREFIX ) { if ( ! comPort && ! append ) ::WriteFile( file, (void*)"\xEF\xBB\xBF", 3, &written, NULL ); // omit the \0! cp = CP_UTF8; } feLen = WideCharToMultiByte( cp, 0, (LPCTSTR)content, -1, tmpStr, iLen*2+1, NULL, NULL ); if ( !comPort ) stat = ::WriteFile( file, (void*)tmpStr, feLen-1, &written, NULL ); // omit the \0! else { stat = 1, written = 1; for ( ULONG i=0; i<feLen-1 && stat != 0 && written == 1; i++ ) { stat = ::WriteFile( file, (void*)&(tmpStr[i]), 1, &written, NULL ); // omit the \0! } } } else { if ( cp == CP_UNICODE_PREFIX ) { if ( ! comPort && ! append ) ::WriteFile( file, (void*)"\xFF\xFE", 2, &written, NULL ); // omit the \0! } stat = ::WriteFile( file, (void*)(LPCTSTR)content, (content.GetLength())*2, &written, NULL ); // omit the \0! } if ( stat == 0 ) rc = FALSE; } if ( file != NULL ) CloseHandle( file ); if ( buffer != NULL ) content.ReleaseBuffer(); if ( tmpStr != NULL ) delete[] tmpStr; return rc; }
void CIniFile::Parse( LPCTSTR cont ) { CStr content = cont; CStr sectionName, key, value; CMapStrToString *section = NULL; BOOL hasEmptySection = FALSE; Sections.RemoveAll(); while ( content.GetLength() > 0 ) { CStr line; int pos = content.Find( _T("\n") ); if ( pos != -1 ) { line = content.Left( pos ); content = content.Mid( pos+1 ); } else { line = content; content = _T(""); } line.TrimLeft(); line.TrimRight(); if ( line.GetLength() > 0 && line.GetAt(0) != '#' && line.GetAt(0) != ';' ) { if ( line.GetAt(0) == '[' && line.Right(1) == L"]" ) { sectionName = line.Mid( 1, line.GetLength()-2 ); sectionName.MakeLower(); section = new CMapStrToString(); Sections.SetAt( sectionName, section ); } else { int eqPos = line.Find( '=' ); if ( eqPos != -1 ) { key = line.Left( eqPos ); key.TrimLeft(); key.TrimRight(); value = line.Mid( eqPos+1 ); value.TrimLeft(); value.TrimRight(); if ( value.Left(1) == L"\"" && value.Right(1) == L"\"" ) { value = value.Mid( 1, value.GetLength()-2 ); } key.MakeLower(); if ( section == NULL && hasEmptySection == FALSE ) { section = new CMapStrToString(); Sections.SetAt( L"", section ); hasEmptySection = TRUE; } section->SetAt( key, value ); } } } } }
int CIniFile::ReadFile( LPCTSTR filename, CStr &content, int size, int cp ) { HANDLE file = NULL; char *buffer = NULL; char *tmpStr = NULL; ULONG fileSize, readSize; ULONG bufSize; BOOL rc = 0, com; int timeout; if ( wcsnicmp( filename, L"COM", 3 ) == 0 && filename[wcslen(filename)-1] == ':' ) { file = CreateFile( filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL ); if ( file != INVALID_HANDLE_VALUE ) { timeout = SetComPort( filename, file ); } com = TRUE; } else { CStr fileWithPath; fileWithPath = filename; if ( CurrentInterpreter != NULL && ( fileWithPath.GetLength() < 2 || ( fileWithPath.GetAt(0) != '\\' && fileWithPath.GetAt(1) != ':' ) ) ) { int len = CurrentInterpreter->ScriptFile.ReverseFind('\\'); if ( len == -1 ) fileWithPath = L"\\" + fileWithPath; else fileWithPath = CurrentInterpreter->ScriptFile.Left( len+1 ) + fileWithPath; } file = CreateFile( fileWithPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); com = FALSE; } if ( file == INVALID_HANDLE_VALUE ) { int error = GetLastError(); rc = -2; } if ( rc == 0 ) { if ( size <= 0 ) { fileSize = GetFileSize( file, NULL ); } else { fileSize = size; } if ( fileSize > 1024 * 1024 || fileSize == -1 ) { // Too big rc = -3; } } if ( rc == 0 ) { bufSize = (fileSize+1)*sizeof(TCHAR); buffer = (char*)content.GetBufferSetLength( bufSize ); if ( buffer == NULL ) rc = -4; } if ( rc == 0 ) { tmpStr = new char[fileSize+2]; // if it's unicode, two binary zeroes are required at the end if ( tmpStr == NULL ) rc = -4; } if ( rc == 0 ) { if ( ! com ) { int stat = ::ReadFile( file, (void*)tmpStr, fileSize, &readSize, NULL ); if ( stat == 0 ) { int error = GetLastError(); rc = -5; } } else { comPort = &file; comTargetSize = fileSize+1; comTargetBuffer = tmpStr; DWORD dwThreadID; HANDLE readThread = CreateThread( NULL, 0, PortReadThread, 0, 0, &dwThreadID ); if ( readThread != NULL ) { WaitForSingleObject( readThread, timeout ); TerminateThread( readThread, 0 ); CloseHandle( readThread ); SetCommMask( file, 0 ); readSize = comReadSize; } else { rc = -6; readSize = 0; } } } if ( rc == 0 ) { if ( cp == CP_ACP ) { if ( readSize >= 3 && memcmp( tmpStr, (void*)"\xEF\xBB\xBF", 3 ) == 0 ) { cp = CP_UTF8_PREFIX; } if ( readSize >= 2 && memcmp( tmpStr, (void*)"\xFF\xFE", 2 ) == 0 ) { cp = CP_UNICODE_PREFIX; } } if ( cp != CP_UNICODE && cp != CP_UNICODE_PREFIX && cp != -1) { int offset = 0; // Strip first 3 bytes for prefixed UFT8 if ( cp == CP_UTF8_PREFIX ) { if ( memcmp( tmpStr, (void*)"\xEF\xBB\xBF", 3 ) == 0 ) { offset = 3; } cp = CP_UTF8; } // set binary zero at end tmpStr[readSize] = '\0'; MultiByteToWideChar( cp, 0, tmpStr+offset, readSize-offset+1, (LPTSTR)buffer, bufSize ); } else if (cp != -1) { int offset = 0; // Strip first 2 bytes for prefixed UFT8 if ( cp == CP_UNICODE_PREFIX ) { if ( memcmp( tmpStr, (void*)"\xFF\xFE", 2 ) == 0 ) { offset = 2; } cp = CP_UNICODE; } // for unicode, two binary zeroes are required at the end tmpStr[readSize] = '\0'; tmpStr[readSize+1] = '\0'; wcscpy( (TCHAR*)buffer, (TCHAR*)(tmpStr+offset) ); } } if ( file != NULL ) CloseHandle( file ); if ( buffer != NULL ) content.ReleaseBuffer(); if ( tmpStr != NULL ) delete[] tmpStr; return rc; }