// initialize the GO_CMD variable to hold the path for go.exe BOOL initialize_go_cmd(){ // allocate string to hold value obtained from environment variable LPTSTR raw_goroot = (LPTSTR) calloc(MAX_ENVIRON, sizeof(TCHAR)); if (raw_goroot == NULL){ ::MessageBox(nppData._nppHandle, TEXT("Out of memory, f**k!"), TEXT("E R R O R"), MB_OK); return FALSE; } // allocate memory to hold string "xyz\bin\go.exe" // will be freed in pluginCleanUp GO_CMD = (LPTSTR) calloc(MAX_PATH, sizeof(TCHAR)); if (GO_CMD == NULL){ ::MessageBox(nppData._nppHandle, TEXT("Out of memory, f**k!"), TEXT("E R R O R"), MB_OK); free(raw_goroot); return FALSE; } // if GOBIN is set set assume that go.exe is there DWORD length = GetEnvironmentVariable(_TEXT("GOBIN"), raw_goroot, MAX_ENVIRON); if (length != 0){ if (PathCombine(GO_CMD, raw_goroot, TEXT("go.exe")) == NULL) return FALSE; PathQuoteSpaces(GO_CMD); free(raw_goroot); return TRUE; } // if GOROOT is set assume that go.exe is there length = GetEnvironmentVariable(_TEXT("GOROOT"), raw_goroot, MAX_ENVIRON); if (length != 0){ if (PathCombine(GO_CMD, raw_goroot, TEXT("bin\\go.exe")) == NULL) return FALSE; PathQuoteSpaces(GO_CMD); free(raw_goroot); return TRUE; } // if neither is set, pray that go.exe is somewhere on the path if (GetLastError() == ERROR_ENVVAR_NOT_FOUND){ GO_CMD = _T("go.exe"); free(raw_goroot); CommandExec c = CommandExec(GO_CMD, _T("")); BOOL start = c.Start(); BOOL end = c.Wait(); DWORD status = c.ExitStatus(); if ( ! start && ! end && status ){ ::MessageBox(nppData._nppHandle, NO_GO_EXE_ERROR), _T("go configuration error"), MB_OK | MB_ICONERROR); }
static void ForcePathQuoteSpaces( WCHAR * _quotePath, const std::wstring & _path ) { if( _path.empty() == true ) { wcscpy_s( _quotePath, 2, L"" ); return; } std::wstring true_path = _path; if( _path[0] == L'/' ) { true_path[0] = true_path[1]; true_path[1] = L':'; } const WCHAR * pathBuffer = true_path.c_str(); size_t pathSize = true_path.size(); PathCanonicalize( _quotePath, pathBuffer ); if( PathQuoteSpaces( _quotePath ) == FALSE ) { wmemmove( _quotePath + 1, _quotePath, pathSize ); _quotePath[0] = '\"'; _quotePath[pathSize + 1] = '\"'; _quotePath[pathSize + 2] = 0; } };
// // Initialize your plugin data here // It will be called while plugin loading BOOL initialize_go_cmd(){ LPTSTR raw_goroot = (LPTSTR) calloc(MAX_ENVIRON, sizeof(TCHAR)); if (raw_goroot == NULL){ ::MessageBox(nppData._nppHandle, TEXT("Out of memory, f**k!"), TEXT("E R R O R"), MB_OK); return FALSE; } // we add one for the NULL here... GO_CMD = (LPTSTR) calloc(MAX_PATH, sizeof(TCHAR)); if (GO_CMD == NULL){ ::MessageBox(nppData._nppHandle, TEXT("Out of memory, f**k!"), TEXT("E R R O R"), MB_OK); free(raw_goroot); return FALSE; } DWORD length = GetEnvironmentVariable(_TEXT("GOBIN"), raw_goroot, MAX_ENVIRON); if (length != 0){ if (PathCombine(GO_CMD, raw_goroot, TEXT("go.exe")) == NULL) return FALSE; PathQuoteSpaces(GO_CMD); return TRUE; } length = GetEnvironmentVariable(_TEXT("GOROOT"), raw_goroot, MAX_ENVIRON); if (length == 0){ if (GetLastError() == ERROR_ENVVAR_NOT_FOUND){ ::MessageBox(nppData._nppHandle, TEXT("GOnpp can't find neither your GOROOT nor your GOBIN environment variable!\n \ Set it and restart notepad++ to use GOnpp."), TEXT("GOROOT not found!"), MB_OK); } free(raw_goroot); return FALSE; }
std::wstring CDeskBand::GetFilePaths(const std::map<std::wstring, ULONG>& items, std::wstring separator, bool quotespaces, bool includefiles, bool includefolders, bool useunc) { WCHAR buf[MAX_PATH+2]; std::wstring sRet; if (!items.empty()) { for (std::map<std::wstring, ULONG>::const_iterator it = items.begin(); it != items.end(); ++it) { if (((it->second & SFGAO_FOLDER)&&(includefolders))||(((it->second & SFGAO_FOLDER)==0)&&(includefiles))) { if (!sRet.empty()) sRet += separator; std::wstring sPath = it->first; if (useunc) { sPath = ConvertToUNC(sPath); } if (quotespaces) { _tcscpy_s(buf, _countof(buf), sPath.c_str()); PathQuoteSpaces(buf); sRet += buf; } else sRet += sPath; } } } return sRet; }
std::wstring CDeskBand::GetFileNames(const std::map<std::wstring, ULONG>& items, std::wstring separator, bool quotespaces, bool includefiles, bool includefolders) { std::wstring sRet; WCHAR buf[MAX_PATH+2]; if (!items.empty()) { for (std::map<std::wstring, ULONG>::const_iterator it = items.begin(); it != items.end(); ++it) { if (((it->second & SFGAO_FOLDER)&&(includefolders))||(((it->second & SFGAO_FOLDER)==0)&&(includefiles))) { size_t pos = it->first.find_last_of('\\'); if (pos != std::string::npos) { if (!sRet.empty()) sRet += separator; if (quotespaces) { _tcscpy_s(buf, _countof(buf), it->first.substr(pos+1).c_str()); PathQuoteSpaces(buf); sRet += buf; } else sRet += it->first.substr(pos+1); } } } } return sRet; }
void BrowsePushed(LPRUNDLG_DATA lprd) { HWND hDlg = lprd->hDlg; TCHAR szText[MAX_PATH]; // Get out of the "synchronized input queues" state if (lprd->dwThreadId) { AttachThreadInput(GetCurrentThreadId(), lprd->dwThreadId, FALSE); lprd->dwThreadId = 0; } GetDlgItemText(hDlg, IDD_COMMAND, szText, ARRAYSIZE(szText)); PathUnquoteSpaces(szText); if (GetFileNameFromBrowse(hDlg, szText, ARRAYSIZE(szText), lprd->lpszWorkingDir, MAKEINTRESOURCE(IDS_EXE), MAKEINTRESOURCE(IDS_PROGRAMSFILTER), MAKEINTRESOURCE(IDS_BROWSE))) { PathQuoteSpaces(szText); SetDlgItemText(hDlg, IDD_COMMAND, szText); EnableOKButton(hDlg, szText); // place the focus on OK // SetFocus(GetDlgItem(hDlg, IDOK)); SendMessage(hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg, IDOK), TRUE); } }
//************************************ // Method: GetAutoStartupApplicationPaths // FullName: CAutoStartupInfo::GetAutoStartupApplicationPaths // Access: private // Returns: bool // Qualifier: // Parameter: HKEY hKey_i // Parameter: StringVector & KeyValues_io //************************************ bool CAutoStartupInfo::GetAutoStartupApplicationPaths( const HKEY hKey_i ) { // Key name and key value buffers TCHAR szKeyNameBuff[MAX_PATH] = { 0 }; DWORD dwKeyNameLength = MAX_PATH; for( DWORD dwIndex = 0; RegEnumValue( hKey_i, dwIndex, szKeyNameBuff, &dwKeyNameLength, 0, 0, 0, 0 ) == ERROR_SUCCESS; ++dwIndex ) { CString csKeyValue; if( GetRegKeyStringValue( hKey_i, szKeyNameBuff, csKeyValue )) { if( IsEnvironmentVarPath( csKeyValue )) { // Get expanded path TCHAR szExpandedPath[MAX_PATH] = { 0 }; ExpandEnvironmentStrings( csKeyValue, szExpandedPath, MAX_PATH ); // Copy back to original buffer csKeyValue = szExpandedPath; } LPTSTR lptszKeyValueBuff = csKeyValue.GetBufferSetLength( csKeyValue.GetLength() + MAX_PATH ); // If path is unquoted then quote, because PathRemoveArgs requires paths with spaces to // be quoted if( !_tcschr( lptszKeyValueBuff, _T( '\"' ))) { PathQuoteSpaces( lptszKeyValueBuff ); } // Remove command line arguments from path PathRemoveArgs( lptszKeyValueBuff ); // Check whether file exists or not, searches for file in the standard paths // that are set BOOL bFound = PathFileExists( lptszKeyValueBuff ); // Removes any quotes from path PathUnquoteSpaces( lptszKeyValueBuff ); // If file is a shortcut then resolve to real path if( bFound && Utils::IsShortcut( lptszKeyValueBuff )) { bFound = Utils::ResolveShortcut( lptszKeyValueBuff ); } csKeyValue.ReleaseBuffer(); AddAutoStartExePath( csKeyValue ); }// End if dwKeyNameLength = MAX_PATH; }// End for return true; }// End GetAutoStartupApplicationPaths
STDAPI CreateShellCommand(LPCTSTR subkey) { TCHAR cmd[MAX_PATH + 20]; GetModuleFileName(0, cmd, MAX_PATH); PathQuoteSpaces(cmd); lstrcat(cmd, _T(" \"%1\"")); LONG err = RegSetValue(HKEY_CLASSES_ROOT, subkey, REG_SZ, cmd, lstrlen(cmd) * sizeof(TCHAR)); return HRESULT_FROM_WIN32(err); }
CString CFilePath::QuoteSpaces(CString const &sString) { if (sString.Find(' ')>=0) { CString sCopy(sString); CStringLock Buffer(sCopy, sCopy.GetLength() + 2); PathQuoteSpaces(Buffer); Buffer.Release(); return sCopy; } return sString; }
BOOL goCommand::combineCommandLine(LPCTSTR go_cmd, LPTSTR pkg) { PathQuoteSpaces(pkg); size_t size = _tcslen(go_cmd) + _tcslen(this->cmd) +_tcslen(pkg) + 2; LPTSTR args = (LPTSTR) calloc(size + 1, sizeof(TCHAR)); if (args == NULL) return FALSE; _tcsncat(args, go_cmd, __min(_tcslen(go_cmd), size - _tcslen(args))); _tcsncat(args, _T(" "), __min(sizeof(args), size - _tcslen(args))); _tcsncat(args, cmd, __min( _tcslen(cmd), size -_tcslen(args))); _tcsncat(args, _T(" "), __min(sizeof(args), size - _tcslen(args))); _tcsncat(args, pkg, __min( _tcslen(pkg), size -_tcslen(args))); this->commandLine = args; return TRUE; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Let windows think we started properly so it doesn't show the appstarting cursor MSG msg; PostMessage(NULL, WM_NULL, 0, 0); GetMessage(&msg, NULL, 0, 0); // Find vim LPSTR program = FindProgram(); if (program == NULL) { MessageBox(NULL, "Could not find a vimXY folder", "Error", MB_OK | MB_ICONERROR); return EXIT_FAILURE; } // Create a properly quoted command line PathQuoteSpaces(program); size_t bufsize = strlen(program) + 1 + strlen(lpCmdLine) + 1; LPSTR buf = new CHAR[bufsize]; strcpy_s(buf, bufsize, program); if (*lpCmdLine) { strcat_s(buf, bufsize, " "); strcat_s(buf, bufsize, lpCmdLine); } // Run Vim STARTUPINFO sinfo = {0}; PROCESS_INFORMATION pinfo = {0}; if (!CreateProcess(program, buf, NULL, NULL, FALSE, 0, NULL, NULL, &sinfo, &pinfo)) { Fail("Could not launch Vim"); } delete[] buf; // Wait for it to exit if (WaitForSingleObject(pinfo.hProcess, INFINITE) == WAIT_FAILED) { Fail("WaitForSingleObject"); } DWORD exitcode; if (!GetExitCodeProcess(pinfo.hProcess, &exitcode)) { Fail("GetExitCodeProcess"); } CloseHandle(pinfo.hProcess); CloseHandle(pinfo.hThread); return exitcode; }
int _tmain(int argc, TCHAR **argv) { check_console(); #ifdef UNICODE /* Ensure we write in UTF-16 mode, so that non-ASCII characters don't get mangled. If we were compiled in ANSI mode it won't work. */ _setmode(_fileno(stdout), _O_U16TEXT); _setmode(_fileno(stderr), _O_U16TEXT); #endif /* Remember if we are admin */ check_admin(); /* Set up function pointers. */ if (get_imports()) exit(111); /* Remember our path for later. */ GetModuleFileName(0, unquoted_imagepath, _countof(unquoted_imagepath)); GetModuleFileName(0, imagepath, _countof(imagepath)); PathQuoteSpaces(imagepath); /* Elevate */ if (argc > 1) { /* Valid commands are: start, stop, pause, continue, install, edit, get, set, reset, unset, remove */ if (str_equiv(argv[1], _T("start"))) exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2)); if (str_equiv(argv[1], _T("stop"))) exit(control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2)); if (str_equiv(argv[1], _T("restart"))) { int ret = control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2); if (ret) exit(ret); exit(control_service(NSSM_SERVICE_CONTROL_START, argc - 2, argv + 2)); } if (str_equiv(argv[1], _T("pause"))) exit(control_service(SERVICE_CONTROL_PAUSE, argc - 2, argv + 2)); if (str_equiv(argv[1], _T("continue"))) exit(control_service(SERVICE_CONTROL_CONTINUE, argc - 2, argv + 2)); if (str_equiv(argv[1], _T("status"))) exit(control_service(SERVICE_CONTROL_INTERROGATE, argc - 2, argv + 2)); if (str_equiv(argv[1], _T("rotate"))) exit(control_service(NSSM_SERVICE_CONTROL_ROTATE, argc - 2, argv + 2)); if (str_equiv(argv[1], _T("install"))) { if (! is_admin) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL)); create_messages(); exit(pre_install_service(argc - 2, argv + 2)); } if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset"))) { int ret = pre_edit_service(argc - 1, argv + 1); if (ret == 3 && ! is_admin && argc == 3) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT)); /* There might be a password here. */ for (int i = 0; i < argc; i++) SecureZeroMemory(argv[i], _tcslen(argv[i]) * sizeof(TCHAR)); exit(ret); } if (str_equiv(argv[1], _T("list"))) exit(list_nssm_services()); if (str_equiv(argv[1], _T("remove"))) { if (! is_admin) exit(elevate(argc, argv, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE)); exit(pre_remove_service(argc - 2, argv + 2)); } } /* Thread local storage for error message buffer */ tls_index = TlsAlloc(); /* Register messages */ if (is_admin) create_messages(); /* Optimisation for Windows 2000: When we're run from the command line the StartServiceCtrlDispatcher() call will time out after a few seconds on Windows 2000. On newer versions the call returns instantly. Check for stdin first and only try to call the function if there's no input stream found. Although it's possible that we're running with input redirected it's much more likely that we're actually running as a service. This will save time when running with no arguments from a command prompt. */ if (! GetStdHandle(STD_INPUT_HANDLE)) { /* Start service magic */ SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } }; if (! StartServiceCtrlDispatcher(table)) { unsigned long error = GetLastError(); /* User probably ran nssm with no argument */ if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1)); log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0); free_imports(); exit(100); } } else exit(usage(1)); /* And nothing more to do */ exit(0); }
void CPathStr::QuoteSpaces() { char *p = GetBuffer(MAX_PATH); PathQuoteSpaces(p); ReleaseBuffer(); }