void DebugStrUnitTest() { struct strTests { wchar_t szTest[100], szCmp[100]; } Tests[] = { {L"Line1\n#Comment1\nLine2\r\n#Comment2\r\nEnd of file", L"Line1\nLine2\r\nEnd of file"}, {L"Line1\n#Comment1\r\n", L"Line1\n"} }; int iCmp; for (INT_PTR i = 0; i < countof(Tests); i++) { StripLines(Tests[i].szTest, L"#"); iCmp = wcscmp(Tests[i].szTest, Tests[i].szCmp); _ASSERTE(iCmp == 0); StripLines(Tests[i].szTest, L"#"); iCmp = wcscmp(Tests[i].szTest, Tests[i].szCmp); _ASSERTE(iCmp == 0); } // Some compilation and operators check CEStr str1; LPCWSTR pszTest = Tests[0].szTest; { str1 = lstrdup(pszTest); } iCmp = lstrcmp(str1, pszTest); _ASSERTE(iCmp == 0); { CEStr str2(lstrdup(pszTest)); wchar_t* pszDup = lstrdup(pszTest); iCmp = lstrcmp(str2, pszTest); _ASSERTE(iCmp == 0 && str2.ms_Arg && str2.ms_Arg != pszTest); } // The following block must to be able to compile #if 0 { CEStr str3(pszDup); str3 = pszTest; } { CEStr str4; str4 = pszTest; iCmp = lstrcmp(str4, pszTest); _ASSERTE(iCmp == 0 && str4.ms_Arg && str4.ms_Arg != pszTest); } { CEStr str5; str5 = str1; iCmp = lstrcmp(str5, pszTest); _ASSERTE(iCmp == 0 && str5.ms_Arg && str3.ms_Arg != str1.ms_Arg); } #endif iCmp = lstrcmp(str1, pszTest); _ASSERTE(iCmp == 0); }
lstrvec* lwords(const char* ss) { char* aa = lstrdup(ss); int ii, jj; int nn = strlen(aa); int spaces = 0; lstrvec* xs; /* How many spaces? */ for (ii = 0; ii < nn; ++ii) { if (aa[ii] == ' ') { aa[ii] = '\0'; spaces += 1; while (aa[ii + 1] == ' ') { ii += 1; aa[ii] = '\0'; } } } xs = lstrvec_alloc(spaces + 1); for (ii = 0; ii < spaces + 1; ++ii) { lstrvec_set(xs, ii, lstrdup(aa)); aa += strlen(aa); while(aa[0] == '\0') aa += 1; } return xs; }
void SetLogin(LPCWSTR asUser, LPCWSTR asPassword) { SafeFree(m_Server.szUser); if (m_Server.szPassword) SecureZeroMemory(m_Server.szPassword, lstrlen(m_Server.szPassword)*sizeof(*m_Server.szPassword)); SafeFree(m_Server.szPassword); if (asUser) m_Server.szUser = lstrdup(asUser); if (asPassword) m_Server.szPassword = lstrdup(asPassword); };
// asProxy = "" - autoconfigure // asProxy = "server:port" void SetProxy(LPCWSTR asProxy, LPCWSTR asProxyUser, LPCWSTR asProxyPassword) { SafeFree(m_Proxy.szProxy); SafeFree(m_Proxy.szProxyUser); if (m_Proxy.szProxyPassword) SecureZeroMemory(m_Proxy.szProxyPassword, lstrlen(m_Proxy.szProxyPassword)*sizeof(*m_Proxy.szProxyPassword)); SafeFree(m_Proxy.szProxyPassword); if (asProxy) m_Proxy.szProxy = lstrdup(asProxy); if (asProxyUser) m_Proxy.szProxyUser = lstrdup(asProxyUser); if (asProxyPassword) m_Proxy.szProxyPassword = lstrdup(asProxyPassword); };
int CIconList::CreateTabIcon(LPCWSTR asIconDescr, bool bAdmin, LPCWSTR asWorkDir) { if (bAdmin && !gpSet->isAdminShield()) bAdmin = false; if (!asIconDescr || !*asIconDescr) return GetTabIcon(bAdmin); int iCreatedIcon = -1; MSectionLockSimple CS; CS.Lock(mpcs, 30000); for (INT_PTR i = 0; i < m_Icons.size(); i++) { const TabIconCache& icn = m_Icons[i]; if ((icn.bAdmin!=FALSE) != bAdmin) continue; if (lstrcmpi(icn.pszIconDescr, asIconDescr) != 0) continue; // Already was created! iCreatedIcon = icn.nIconIdx; goto wrap; } iCreatedIcon = CreateTabIconInt(asIconDescr, bAdmin, asWorkDir); if (iCreatedIcon == -1) { // To avoid numerous CreateTabIconInt calls - just remember "No icon" for that asIconDescr TabIconCache DummyIcon = {lstrdup(asIconDescr), bAdmin, -1}; m_Icons.push_back(DummyIcon); } wrap: return iCreatedIcon; }
static LONG GetString(HANDLE hk, LPCWSTR asName, wchar_t** pszValue) { LONG nRegRc = -1; #if FAR_UNICODE>=1900 FarSettingsItem fsi = {0}; fsi.Name = asName; fsi.Type = FST_STRING; //if (psi.SettingsControl(hk, SCTL_GET, 0, &fsi) && (fsi.Data.Size == sizeof(DWORD))) if (psi.SettingsControl(hk, SCTL_GET, 0, &fsi)) { *pszValue = lstrdup(fsi.String); nRegRc = 0; } else if (cfg) cfg->bSomeValuesMissed = TRUE; #else DWORD nSize = 0; nRegRc = RegQueryValueExW((HKEY)hk, asName, NULL, NULL, NULL, &nSize); if (nRegRc == 0) { _ASSERTE(*pszValue == NULL); *pszValue = (wchar_t*)calloc(nSize+2,1); nRegRc = RegQueryValueExW((HKEY)hk, asName, NULL, NULL, (LPBYTE)*pszValue, &nSize); } else { if (cfg) cfg->bSomeValuesMissed = TRUE; } #endif MCHKHEAP; return nRegRc; }
TreeNode* alloc_string_node(const char* name) { TreeNode* tn = alloc_tree_node(STRING_TYPE); tn->data = (void*) lstrdup(name); return tn; }
// asProxy = "" - autoconfigure // asProxy = "server:port" void SetProxy(LPCWSTR asProxy, LPCWSTR asProxyUser, LPCWSTR asProxyPassword) { SafeFree(m_Proxy.szProxy); SafeFree(m_Proxy.szProxyUser); if (m_Proxy.szProxyPassword) SecureZeroMemory(m_Proxy.szProxyPassword, lstrlen(m_Proxy.szProxyPassword)*sizeof(*m_Proxy.szProxyPassword)); SafeFree(m_Proxy.szProxyPassword); // Empty string - for ‘autoconfig’ m_Proxy.szProxy = lstrdup(asProxy ? asProxy : L""); if (asProxyUser) m_Proxy.szProxyUser = lstrdup(asProxyUser); if (asProxyPassword) m_Proxy.szProxyPassword = lstrdup(asProxyPassword); };
bool CConEmuUpdate::QueryConfirmationInt(LPCWSTR asConfirmInfo) { if (mb_InShowLastError || !gpConEmu) return false; // Если отображается ошибк - не звать bool lbConfirm; if (gpConEmu->isMainThread()) { Assert(!mb_InetMode && mb_ManualCallMode); DontEnable de; int iBtn = MessageBox(NULL, asConfirmInfo, ms_DefaultTitle, MB_ICONQUESTION|MB_SETFOREGROUND|MB_SYSTEMMODAL|MB_YESNO); mb_InShowLastError = false; lbConfirm = (iBtn == IDYES); } else { MSectionLock SC; SC.Lock(mp_LastErrorSC, TRUE); SafeFree(ms_LastErrorInfo); ms_LastErrorInfo = lstrdup(asConfirmInfo); SC.Unlock(); lbConfirm = gpConEmu->ReportUpdateConfirmation(); } return lbConfirm; }
TreeNode* alloc_fun_node(const char* rtype, const char* name, TreeNode* params, TreeNode* body) { char* rcvr = 0; if (rtype) { rcvr = lstrdup(rtype); } TreeNode* tn = alloc_tree_node(FUN_TYPE); tn->name = lstrdup(name); tn->data = rcvr; tn->arg0 = params; tn->arg1 = body; return tn; }
bool CConEmuInside::InitInside(bool bRunAsAdmin, bool bSyncDir, LPCWSTR pszSyncDirCmdFmt, DWORD nParentPID, HWND hParentWnd) { CConEmuInside* pInside = gpConEmu->mp_Inside; if (!pInside) { pInside = new CConEmuInside(); if (!pInside) { return false; } gpConEmu->mp_Inside = pInside; } pInside->mn_InsideParentPID = nParentPID; pInside->mh_InsideParentWND = hParentWnd; pInside->m_InsideIntegration = (hParentWnd == NULL) ? ii_Auto : ii_Simple; pInside->mb_InsideIntegrationShift = bRunAsAdmin /*isPressed(VK_SHIFT)*/; if (bSyncDir) { pInside->mb_InsideSynchronizeCurDir = true; pInside->ms_InsideSynchronizeCurDir = lstrdup(pszSyncDirCmdFmt); // \eCD /d %1 - \e - ESC, \b - BS, \n - ENTER, %1 - "dir", %2 - "bash dir" } else { _ASSERTE(pInside->ms_InsideSynchronizeCurDir==NULL); pInside->mb_InsideSynchronizeCurDir = false; } return true; }
// Issue 1191: ConEmu was launched instead of explorer from taskbar pinned library icon void CSetPgIntegr::UnregisterShellInvalids() { HKEY hkDir; if (0 == RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Classes\\LibraryFolder\\shell", 0, KEY_READ, &hkDir)) { int iOthers = 0; MArray<wchar_t*> lsNames; for (DWORD i = 0; i < 512; i++) { wchar_t szName[MAX_PATH+32] = {}; wchar_t szCmd[MAX_PATH*4]; DWORD cchMax = countof(szName) - 32; if (0 != RegEnumKeyEx(hkDir, i, szName, &cchMax, NULL, NULL, NULL, NULL)) break; wchar_t* pszSlash = szName + _tcslen(szName); wcscat_c(szName, L"\\command"); HKEY hkCmd = NULL; if (0 == RegOpenKeyEx(hkDir, szName, 0, KEY_READ, &hkCmd)) { DWORD cbMax = sizeof(szCmd)-2; if (0 == RegQueryValueEx(hkCmd, NULL, NULL, NULL, (LPBYTE)szCmd, &cbMax)) { szCmd[cbMax>>1] = 0; *pszSlash = 0; //LPCWSTR pszInside = StrStrI(szCmd, L"-inside"); LPCWSTR pszConEmu = StrStrI(szCmd, L"conemu"); if (pszConEmu) lsNames.push_back(lstrdup(szName)); else iOthers++; } RegCloseKey(hkCmd); }
ConEmuHotKey* ConEmuHotKeyList::Add(int DescrLangID, ConEmuHotKeyType HkType, HotkeyEnabled_t Enabled, LPCWSTR Name, HotkeyFKey_t fkey, bool OnKeyUp, LPCWSTR GuiMacro) { static ConEmuHotKey dummy = {}; ConEmuHotKey* p = &dummy; #ifdef _DEBUG for (INT_PTR i = 0; i < size(); i++) { if ((*this)[i].DescrLangID == DescrLangID) { _ASSERTE(FALSE && "This item was already added!"); } } #endif INT_PTR iNew = this->push_back(dummy); if (iNew >= 0) { p = &((*this)[iNew]); memset(p, 0, sizeof(*p)); p->DescrLangID = DescrLangID; p->HkType = HkType; p->Enabled = Enabled; lstrcpyn(p->Name, Name, countof(p->Name)); p->fkey = fkey; p->OnKeyUp = OnKeyUp; p->GuiMacro = GuiMacro ? lstrdup(GuiMacro) : NULL; } return p; }
TreeNode* alloc_symbol_node(const char* name) { TreeNode* tn = alloc_tree_node(SYMBOL_TYPE); tn->data = (void*) lstrdup(name); return tn; }
bool RConStartArgs::CheckUserToken(HWND hPwd) { //SafeFree(pszUserProfile); bUseEmptyPassword = FALSE; //if (hLogonToken) { CloseHandle(hLogonToken); hLogonToken = NULL; } if (!pszUserName || !*pszUserName) return FALSE; //wchar_t szPwd[MAX_PATH]; szPwd[0] = 0; //szUserPassword[0] = 0; if (!GetWindowText(hPwd, szUserPassword, MAX_PATH-1)) { szUserPassword[0] = 0; bUseEmptyPassword = TRUE; } SafeFree(pszDomain); wchar_t* pszSlash = wcschr(pszUserName, L'\\'); if (pszSlash) { pszDomain = pszUserName; *pszSlash = 0; pszUserName = lstrdup(pszSlash+1); } HANDLE hLogonToken = CheckUserToken(); bool bIsValid = (hLogonToken != NULL); // Token itself is not needed now SafeCloseHandle(hLogonToken); return bIsValid; }
void DebugNeedCmdUnitTests() { BOOL b; struct strTests { LPCWSTR pszCmd; BOOL bNeed; } Tests[] = { {L"\"C:\\windows\\notepad.exe -f \"makefile\" COMMON=\"../../../plugins/common\"\"", FALSE}, {L"\"\"C:\\windows\\notepad.exe -new_console\"\"", FALSE}, {L"\"\"cmd\"\"", FALSE}, {L"cmd /c \"\"C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe\" -?\"", FALSE}, {L"cmd /c \"dir c:\\\"", FALSE}, {L"abc.cmd", TRUE}, // Do not do too many heuristic. If user really needs redirection (for 'root'!) // he must explicitly call "cmd /c ...". With only exception if first exe not found. {L"notepad text & start explorer", FALSE}, }; LPCWSTR psArgs; BOOL bNeedCut, bRootIsCmd, bAlwaysConfirm, bAutoDisable; CmdArg szExe; for (INT_PTR i = 0; i < countof(Tests); i++) { szExe.Empty(); RConStartArgs rcs; rcs.pszSpecialCmd = lstrdup(Tests[i].pszCmd); rcs.ProcessNewConArg(); b = IsNeedCmd(TRUE, rcs.pszSpecialCmd, szExe, &psArgs, &bNeedCut, &bRootIsCmd, &bAlwaysConfirm, &bAutoDisable); _ASSERTE(b == Tests[i].bNeed); } }
// Найти "ComSpec" и вернуть lstrdup на него wchar_t* GetComspec(const ConEmuComspec* pOpt) { wchar_t* pszComSpec = NULL; if (pOpt) { if ((pOpt->csType == cst_Explicit) && *pOpt->ComspecExplicit) { pszComSpec = lstrdup(pOpt->ComspecExplicit); } if (!pszComSpec && (pOpt->csBits == csb_SameOS)) { BOOL bWin64 = IsWindows64(); if (bWin64 ? *pOpt->Comspec64 : *pOpt->Comspec32) pszComSpec = lstrdup(bWin64 ? pOpt->Comspec64 : pOpt->Comspec32); } if (!pszComSpec && (pOpt->csBits == csb_SameApp)) { BOOL bWin64 = WIN3264TEST(FALSE,TRUE); if (bWin64 ? *pOpt->Comspec64 : *pOpt->Comspec32) pszComSpec = lstrdup(bWin64 ? pOpt->Comspec64 : pOpt->Comspec32); } if (!pszComSpec) { BOOL bWin64 = (pOpt->csBits != csb_x32); if (bWin64 ? *pOpt->Comspec64 : *pOpt->Comspec32) pszComSpec = lstrdup(bWin64 ? pOpt->Comspec64 : pOpt->Comspec32); } } else { _ASSERTE(pOpt && L"pOpt должен быть передан, по идее"); } if (!pszComSpec) { wchar_t szComSpec[MAX_PATH]; pszComSpec = lstrdup(GetComspecFromEnvVar(szComSpec, countof(szComSpec))); } // Уже должно быть хоть что-то _ASSERTE(pszComSpec && *pszComSpec); return pszComSpec; }
TreeNode* alloc_unop_node(const char* op, TreeNode* arg0) { TreeNode* tn = alloc_tree_node(UNOP_TYPE); tn->name = lstrdup(op); tn->arg0 = arg0; return tn; }
TreeNode* alloc_dot_node(TreeNode* arg0, TreeNode* attr) { TreeNode *tn = alloc_tree_node(DOT_TYPE); tn->name = lstrdup((char*)attr->data); tn->arg0 = arg0; return tn; }
void SetCmdStringFormat(LPCWSTR asFormat) { SafeFree(szCmdStringFormat); if (asFormat && *asFormat) { szCmdStringFormat = lstrdup(asFormat); } }
void DebugProcessNameTest() { CProcessData processes; CEStr lsName(lstrdup(L"xxx.exe")), lsPath; DWORD nPID = GetCurrentProcessId(); bool bRc = processes.GetProcessName(nPID, lsName.GetBuffer(MAX_PATH), MAX_PATH, lsPath.GetBuffer(MAX_PATH*2), MAX_PATH*2, NULL); _ASSERTE(bRc); }
void RConStartArgs::RunArgTests() { struct { LPCWSTR pszArg, pszNeed; } cTests[] = { { L"\"c:\\cmd.exe\" \"-new_console\" \"c:\\file.txt\"", L"\"c:\\cmd.exe\" \"c:\\file.txt\"" }, { L"\"c:\\cmd.exe\" -new_console:n \"c:\\file.txt\"", L"\"c:\\cmd.exe\" \"c:\\file.txt\"" }, { L"\"c:\\cmd.exe\" \"-new_console:n\" \"c:\\file.txt\"", L"\"c:\\cmd.exe\" \"c:\\file.txt\"" }, { L"c:\\cmd.exe \"-new_console:n\" \"c:\\file.txt\"", L"c:\\cmd.exe \"c:\\file.txt\"" }, { L"\"c:\\cmd.exe\" \"-new_console:n\" c:\\file.txt", L"\"c:\\cmd.exe\" c:\\file.txt" }, { L"c:\\file.txt -cur_console", L"c:\\file.txt" }, { L"\"c:\\file.txt\" -cur_console", L"\"c:\\file.txt\"" }, { L" -cur_console \"c:\\file.txt\"", L" \"c:\\file.txt\"" }, { L"-cur_console \"c:\\file.txt\"", L"\"c:\\file.txt\"" }, { L"-cur_console c:\\file.txt", L"c:\\file.txt" }, }; for (size_t i = 0; i < countof(cTests); i++) { RConStartArgs arg; arg.pszSpecialCmd = lstrdup(cTests[i].pszArg); arg.ProcessNewConArg(); if (lstrcmp(arg.pszSpecialCmd, cTests[i].pszNeed) != 0) { //_ASSERTE(FALSE && "arg.ProcessNewConArg failed"); OutputDebugString(L"arg.ProcessNewConArg failed\n"); } int nDbg = 0; } }
UINT DownloadFile(LPCWSTR asSource, LPCWSTR asTarget, DWORD& crc, DWORD& size, BOOL abShowAllErrors = FALSE) { UINT iRc = E_UNEXPECTED; UINT nWait; wchar_t* pszCommand = NULL; wchar_t* szCmdDirectory = NULL; // Destination directory for file creation MCHKHEAP; // Split target into directory and file-name LPCWSTR pszName = PointToName(asTarget); if (pszName > asTarget) { szCmdDirectory = lstrdup(asTarget); if (!szCmdDirectory) { iRc = E_OUTOFMEMORY; goto wrap; } szCmdDirectory[pszName-asTarget] = 0; } // Prepare command line for downloader tool pszCommand = CreateCommand(asSource, pszName, iRc); if (!pszCommand) { _ASSERTE(iRc!=0); goto wrap; } _ASSERTE(m_PI.hProcess==NULL); MCHKHEAP; nWait = ExecuteDownloader(pszCommand, szCmdDirectory); // Now check the result of downloader proc if (nWait != 0) { iRc = nWait; goto wrap; } if (!CalcFileHash(asTarget, size, crc)) { iRc = GetLastError(); if (!iRc) iRc = E_UNEXPECTED; goto wrap; } iRc = 0; // OK wrap: MCHKHEAP; SafeFree(pszCommand); SafeFree(szCmdDirectory); CloseHandles(); return iRc; };
bool FileSearchInDir(LPCWSTR asFilePath, CEStr& rsFound) { // Possibilities // a) asFilePath does not contain path, only: "far" // b) asFilePath contains path, but without extension: "C:\\Program Files\\Far\\Far" LPCWSTR pszSearchFile = asFilePath; LPCWSTR pszSlash = wcsrchr(asFilePath, L'\\'); if (pszSlash) { if ((pszSlash - asFilePath) >= MAX_PATH) { // No need to continue, this is invalid path to executable return false; } // C:\Far\Far.exe /w /pC:\Far\Plugins\ConEmu;C:\Far\Plugins.My if (!IsFilePath(asFilePath, false)) { return false; } // Do not allow '/' here LPCWSTR pszFwd = wcschr(asFilePath, L'/'); if (pszFwd != NULL) { return false; } } wchar_t* pszSearchPath = NULL; if (pszSlash) { if ((pszSearchPath = lstrdup(asFilePath)) != NULL) { pszSearchFile = pszSlash + 1; pszSearchPath[pszSearchFile - asFilePath] = 0; } } // Попытаемся найти "по путям" (%PATH%) wchar_t *pszFilePart; wchar_t szFind[MAX_PATH+1]; LPCWSTR pszExt = PointToExt(asFilePath); DWORD nLen = SearchPath(pszSearchPath, pszSearchFile, pszExt ? NULL : L".exe", countof(szFind), szFind, &pszFilePart); SafeFree(pszSearchPath); if (nLen && (nLen < countof(szFind)) && FileExists(szFind)) { // asFilePath will be invalid after .Set rsFound.Set(szFind); return true; } return false; }
TreeNode* alloc_binop_node(const char* op, TreeNode* arg0, TreeNode* arg1) { TreeNode* tn = alloc_tree_node(BINOP_TYPE); tn->name = lstrdup(op); tn->arg0 = arg0; tn->arg1 = arg1; return tn; }
// Set resource item bool CLngRc::SetResource(MArray<LngRcItem>& arr, int idx, LPCWSTR asValue, bool bLocalized) { if (idx < 0) { _ASSERTE(idx >= 0); return false; } _ASSERTE(!bLocalized || (asValue && *asValue)); if (idx >= arr.size()) { LngRcItem dummy = {}; arr.set_at(idx, dummy); } bool bOk = false; LngRcItem& item = arr[idx]; // Caching: no resource was found for that id if (!asValue || !*asValue) { if (item.Str) item.Str[0] = 0; item.Processed = true; item.Localized = false; return true; } size_t iLen = wcslen(asValue); if (iLen >= (u16)-1) { // Too long string? _ASSERTE(iLen < (u16)-1); } else { if (item.Str && (item.MaxLen >= iLen)) { _wcscpy_c(item.Str, item.MaxLen, asValue); } else { //TODO: thread-safe SafeFree(item.Str); item.MaxLen = iLen; item.Str = lstrdup(asValue); } bOk = (item.Str != NULL); } item.Processed = bOk; item.Localized = (bOk && bLocalized); return bOk; }
TreeNode* alloc_bind_node(TreeNode* name, TreeNode* arg0) { assert(name->type == SYMBOL_TYPE); TreeNode *tn = alloc_tree_node(BIND_TYPE); tn->name = lstrdup((char*)name->data); tn->arg0 = arg0; return tn; }
static void cache_put(const char* data, size_t size, const char* hash) { int nn = cache_next; cache[nn].size = size; cache[nn].data = GC_malloc_atomic(size); memcpy(cache[nn].data, data, size); cache[nn].hash = lstrdup(hash); cache_next = (nn + 1) % MAX_CACHE_SIZE; }
void lstrvec_set(lstrvec* vec, int ii, char* vv) { if (ii < 0 || ii > vec->size - 1) { fprintf(stderr, "lstrvec: Out of bounds get(%d) (size = %d).\n", ii, vec->size); abort(); } vec->xs[ii] = lstrdup(vv); }
char* lstrvec_get(lstrvec* vec, int ii) { if (ii < 0 || ii > vec->size - 1) { fprintf(stderr, "lstrvec: Out of bounds get(%d) (size = %d).\n", ii, vec->size); abort(); } return lstrdup(vec->xs[ii]); }