diff --git a/src/ConEmu.vcxproj b/src/ConEmu.vcxproj index bed0b41190..6dc6b1c7ff 100644 --- a/src/ConEmu.vcxproj +++ b/src/ConEmu.vcxproj @@ -407,6 +407,7 @@ + @@ -637,6 +638,7 @@ + diff --git a/src/ConEmu.vcxproj.filters b/src/ConEmu.vcxproj.filters index 203235ffc2..c43752e448 100644 --- a/src/ConEmu.vcxproj.filters +++ b/src/ConEmu.vcxproj.filters @@ -390,6 +390,9 @@ Headers + + Headers + Headers @@ -1076,6 +1079,9 @@ Sources + + Sources + Sources diff --git a/src/ConEmu/ConEmu.cpp b/src/ConEmu/ConEmu.cpp index d930f7e8f0..7f082b65fc 100644 --- a/src/ConEmu/ConEmu.cpp +++ b/src/ConEmu/ConEmu.cpp @@ -93,6 +93,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "SetPgDebug.h" #include "SetPgInfo.h" #include "Status.h" +#include "SystemEnvironment.h" #include "TabBar.h" #include "TrayIcon.h" #include "Update.h" @@ -131,6 +132,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define DEBUGSTRDPI(s) DEBUGSTR(s) #define DEBUGSTRNOLOG(s) //DEBUGSTR(s) #define DEBUGSTRDESTROY(s) DEBUGSTR(s) +#define DEBUGSTRSETCHANGE(s) DEBUGSTR(s) #ifdef _DEBUG //#define DEBUGSHOWFOCUS(s) DEBUGSTR(s) #endif @@ -386,6 +388,8 @@ CConEmuMain::CConEmuMain() } } + saved_environment_ = std::make_shared(); + saved_environment_->LoadFromRegistry(); // Load current comspec form registry HKEY hk; @@ -8239,6 +8243,58 @@ void CConEmuMain::RefreshWindowStyles() //OnTransparent(); } +void CConEmuMain::ReloadEnvironmentVariables() +{ + const wchar_t dbg_msg[] = L"Reloading environment variables from system registry"; + if (!LogString(dbg_msg)) DEBUGSTRSETCHANGE(dbg_msg); + + + auto new_environment = std::make_shared(); + new_environment->LoadFromRegistry(); + + const auto& old_data = saved_environment_->env_data; + const auto& new_data = new_environment->env_data; + for (const auto& old_var : old_data) + { + if (!new_data.count(old_var.first)) + { + std::wstring dbg_log(L" Erased: `" + old_var.second.name + + L"`, old value: `" + old_var.second.data + L"`"); + if (!LogString(dbg_log.c_str())) DEBUGSTRSETCHANGE(dbg_log.c_str()); + + SetEnvironmentVariable(old_var.second.name.c_str(), nullptr); + } + } + for (const auto& new_var : new_data) + { + auto old_iter = old_data.find(new_var.first); + if (old_iter == old_data.end() + || (old_iter->second.data != new_var.second.data)) + { + std::wstring old_value((old_iter == old_data.end()) + ? std::wstring(L"Absent") + : std::wstring(L"`" + old_iter->second.data + L"`")); + std::wstring dbg_log(L" Changed: `" + new_var.second.name + + L"`, new value: `" + new_var.second.data + L"`, old_value: " + + old_value); + if (!LogString(dbg_log.c_str())) DEBUGSTRSETCHANGE(dbg_log.c_str()); + + if (new_var.second.expandable) + { + CEStr value(ExpandEnvStr(new_var.second.data.c_str())); + if (!value.IsEmpty()) + { + SetEnvironmentVariable(new_var.second.name.c_str(), value); + continue; + } + } + SetEnvironmentVariable(new_var.second.name.c_str(), new_var.second.data.c_str()); + } + } + + saved_environment_ = new_environment; +} + void CConEmuMain::OnGlobalSettingsChanged() { UpdateGuiInfoMapping(); @@ -13693,6 +13749,18 @@ LRESULT CConEmuMain::WndProc(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam case WM_SETTINGCHANGE: { + const wchar_t* change_verb = nullptr; + const wchar_t kEnvironmentVerb[] = L"Environment"; + wchar_t szDbg[128]; swprintf_c(szDbg, L"WM_SETTINGCHANGE: W=x%04X, L=x%04X", (DWORD)wParam, (DWORD)lParam); + if (lParam && !IsBadStringPtr((LPCWSTR)lParam, 64)) + { + change_verb = (LPCWSTR)lParam; + wcscat_c(szDbg, L", "); + size_t cur_len = wcslen(szDbg); + lstrcpyn(szDbg + cur_len, change_verb, std::size(szDbg) - cur_len); + } + if (!LogString(szDbg)) DEBUGSTRSETCHANGE(szDbg); + if (wParam == SPI_SETWORKAREA) { ReloadMonitorInfo(); @@ -13704,6 +13772,12 @@ LRESULT CConEmuMain::WndProc(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam if (wm != wmNormal && !isIconic() && isMeForeground()) SetWindowMode(wm); } + + if (change_verb && 0 == wcscmp(change_verb, kEnvironmentVerb) + && gpSet->AutoReloadEnvironment) + { + ReloadEnvironmentVariables(); + } } break; case WM_DISPLAYCHANGE: diff --git a/src/ConEmu/ConEmu.h b/src/ConEmu/ConEmu.h index f773e9f529..a1d398237d 100644 --- a/src/ConEmu/ConEmu.h +++ b/src/ConEmu/ConEmu.h @@ -76,6 +76,7 @@ union CESize; class CPushInfo; class CAltNumpad; struct HandleMonitor; +struct SystemEnvironment; struct ConsoleInfoArg { @@ -86,6 +87,7 @@ struct ConsoleInfoArg TOPLEFTCOORD TopLeft; }; +#include #include "DwmHelper.h" #include "TaskBar.h" #include "FrameHolder.h" @@ -845,6 +847,11 @@ class CConEmuMain MSectionLockSimple* pcsLock; bool wait; } m_LockConhostStart = {}; + + public: + void ReloadEnvironmentVariables(); + private: + std::shared_ptr saved_environment_; }; // Message Logger diff --git a/src/ConEmu/ConEmu.rc b/src/ConEmu/ConEmu.rc index e0ada3c67b..410c239b90 100644 --- a/src/ConEmu/ConEmu.rc +++ b/src/ConEmu/ConEmu.rc @@ -932,14 +932,16 @@ STYLE DS_SETFONT | DS_FIXEDSYS | DS_CONTROL | WS_CHILD EXSTYLE WS_EX_CONTROLPARENT FONT 8, "MS Shell Dlg", 0, 0, 0x1 BEGIN - GROUPBOX "Set up environment variables, cmd.exe aliases, codepage",gbSetAlias,4,1,375,206 + GROUPBOX "Set up environment variables, cmd.exe aliases, codepage",gbSetAlias,4,1,375,196 LTEXT "Here you may set up your environment variables, default code page, and so on...\r\nOne line may have only one command! You may comment lines with # sign.",stSetCommands,10,13,363,17,SS_NOPREFIX LTEXT "set ""PATH=%ConEmuBaseDir%\\Scripts;%PATH%""\r\nchcp utf-8\r\nalias st=start $* -cur_console:n",stSetCommands2,22,32,352,24,SS_NOPREFIX - EDITTEXT tSetCommands,9,61,365,140,ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_NOHIDESEL | ES_WANTRETURN | WS_VSCROLL | WS_HSCROLL - GROUPBOX "Add to %PATH% environment variable",gbAddToPath,4,211,375,28 - CONTROL "Add %ConEmuDir% to %PATH%",cbAddConEmu2Path,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,11,224,176,8 + EDITTEXT tSetCommands,9,61,365,130,ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_NOHIDESEL | ES_WANTRETURN | WS_VSCROLL | WS_HSCROLL + GROUPBOX "",IDC_STATIC,4,199,375,41 + CONTROL "Add %ConEmuDir% to %PATH%",cbAddConEmu2Path,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,11,210,176,8 CONTROL "Add %ConEmuBaseDir% to %PATH%",cbAddConEmuBase2Path, - "Button",BS_AUTOCHECKBOX | WS_TABSTOP,194,224,176,8 + "Button",BS_AUTOCHECKBOX | WS_TABSTOP,194,210,176,8 + CONTROL "Reload environment variables automatically",cbAutoReloadEnvironment, + "Button",BS_AUTOCHECKBOX | WS_TABSTOP,11,225,176,8 END IDD_SPG_MARKCOPY DIALOGEX 0, 0, 381, 242 diff --git a/src/ConEmu/Options.cpp b/src/ConEmu/Options.cpp index d4b9dc8ed6..503187c15d 100644 --- a/src/ConEmu/Options.cpp +++ b/src/ConEmu/Options.cpp @@ -456,6 +456,7 @@ void Settings::InitSettings() } } + AutoReloadEnvironment = true; psEnvironmentSet = lstrdup( L"set PATH=%ConEmuBaseDir%\\Scripts;%PATH%\r\n" L"\r\n" @@ -2719,6 +2720,7 @@ void Settings::LoadSettings(bool& rbNeedCreateVanilla, const SettingsStorage* ap //FindComspec(&ComSpec); //Update Comspec(&ComSpec); --> CSettings::SettingsLoaded + reg->Load(L"AutoReloadEnvironment", AutoReloadEnvironment); this->LoadMSZ(reg, L"EnvironmentSet", psEnvironmentSet, L"\r\n", true); reg->Load(L"CTS.Intelligent", isCTSIntelligent); @@ -3784,6 +3786,7 @@ BOOL Settings::SaveSettings(BOOL abSilent /*= FALSE*/, const SettingsStorage* ap reg->Save(L"ComSpec.EnvAddExePath", _bool((ComSpec.AddConEmu2Path & CEAP_AddConEmuExeDir) == CEAP_AddConEmuExeDir)); reg->Save(L"ComSpec.UncPaths", _bool(ComSpec.isAllowUncPaths)); reg->Save(L"ComSpec.Path", ComSpec.ComspecExplicit); + reg->Save(L"AutoReloadEnvironment", AutoReloadEnvironment); this->SaveMSZ(reg, L"EnvironmentSet", psEnvironmentSet, L"\r\n", false); reg->Save(L"CTS.Intelligent", isCTSIntelligent); { diff --git a/src/ConEmu/Options.h b/src/ConEmu/Options.h index 07baf273e2..14400a9452 100644 --- a/src/ConEmu/Options.h +++ b/src/ConEmu/Options.h @@ -194,6 +194,8 @@ struct Settings ConEmuComspec ComSpec; + //reg->Load(L"AutoReloadEnvironment", AutoReloadEnvironment); + bool AutoReloadEnvironment; //reg->LoadMSZ(L"EnvironmentSet", psEnvironmentSet); wchar_t* psEnvironmentSet; // commands: multiline, "\r\n" separated diff --git a/src/ConEmu/SetDlgButtons.cpp b/src/ConEmu/SetDlgButtons.cpp index 2f90002885..05a885b6e3 100644 --- a/src/ConEmu/SetDlgButtons.cpp +++ b/src/ConEmu/SetDlgButtons.cpp @@ -189,6 +189,9 @@ bool CSetDlgButtons::ProcessButtonClick(HWND hDlg, WORD CB, BYTE uCheck) case cbComspecUpdateEnv: OnBtn_ComspecUpdateEnv(hDlg, CB, uCheck); break; + case cbAutoReloadEnvironment: + OnBtn_AutoReloadEnvironment(hDlg, CB, uCheck); + break; case cbAddConEmu2Path: OnBtn_AddConEmu2Path(hDlg, CB, uCheck); break; @@ -2085,6 +2088,16 @@ void CSetDlgButtons::OnBtn_ComspecUpdateEnv(HWND hDlg, WORD CB, BYTE uCheck) } // cbComspecUpdateEnv +// cbAutoReloadEnvironment +void CSetDlgButtons::OnBtn_AutoReloadEnvironment(HWND hDlg, WORD CB, BYTE uCheck) +{ + _ASSERTE(CB==cbAutoReloadEnvironment); + + gpSet->AutoReloadEnvironment = uCheck; + +} // cbAutoReloadEnvironment + + // cbAddConEmu2Path void CSetDlgButtons::OnBtn_AddConEmu2Path(HWND hDlg, WORD CB, BYTE uCheck) { diff --git a/src/ConEmu/SetDlgButtons.h b/src/ConEmu/SetDlgButtons.h index f0a348440b..84f0e5940a 100644 --- a/src/ConEmu/SetDlgButtons.h +++ b/src/ConEmu/SetDlgButtons.h @@ -102,6 +102,7 @@ class CSetDlgButtons : public CDlgItemHelper static void OnBtn_ComspecTest(HWND hDlg, WORD CB, BYTE uCheck); static void OnBtn_ComspecBitsRadio(HWND hDlg, WORD CB, BYTE uCheck); static void OnBtn_ComspecUpdateEnv(HWND hDlg, WORD CB, BYTE uCheck); + static void OnBtn_AutoReloadEnvironment(HWND hDlg, WORD CB, BYTE uCheck); static void OnBtn_AddConEmu2Path(HWND hDlg, WORD CB, BYTE uCheck); static void OnBtn_AddConEmuBase2Path(HWND hDlg, WORD CB, BYTE uCheck); static void OnBtn_ComspecUncPaths(HWND hDlg, WORD CB, BYTE uCheck); diff --git a/src/ConEmu/SetPgEnvironment.cpp b/src/ConEmu/SetPgEnvironment.cpp index 492580d4ed..a9d131491f 100644 --- a/src/ConEmu/SetPgEnvironment.cpp +++ b/src/ConEmu/SetPgEnvironment.cpp @@ -46,6 +46,7 @@ LRESULT CSetPgEnvironment::OnInitDialog(HWND hDlg, bool abInitial) { checkDlgButton(hDlg, cbAddConEmu2Path, (gpSet->ComSpec.AddConEmu2Path & CEAP_AddConEmuExeDir) ? BST_CHECKED : BST_UNCHECKED); checkDlgButton(hDlg, cbAddConEmuBase2Path, (gpSet->ComSpec.AddConEmu2Path & CEAP_AddConEmuBaseDir) ? BST_CHECKED : BST_UNCHECKED); + checkDlgButton(hDlg, cbAutoReloadEnvironment, (gpSet->AutoReloadEnvironment) ? BST_CHECKED : BST_UNCHECKED); SetDlgItemText(hDlg, tSetCommands, gpSet->psEnvironmentSet ? gpSet->psEnvironmentSet : L""); diff --git a/src/ConEmu/SystemEnvironment.cpp b/src/ConEmu/SystemEnvironment.cpp new file mode 100644 index 0000000000..908ae1c216 --- /dev/null +++ b/src/ConEmu/SystemEnvironment.cpp @@ -0,0 +1,85 @@ + +/* +Copyright (c) 2019-present Maximus5 +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the authors may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#define HIDE_USE_EXCEPTION_INFO +#define SHOWDEBUGSTR + +#include "Header.h" +#include + +#include "../common/WRegistry.h" +#include "SystemEnvironment.h" + +void SystemEnvironment::LoadFromRegistry() +{ + env_data.clear(); + RegEnumValues(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", + SysEnvValueCallback, (LPARAM)this, true); + RegEnumValues(HKEY_CURRENT_USER, L"Environment", + SysEnvValueCallback, (LPARAM)this, true); +} + +std::wstring SystemEnvironment::MakeEnvName(const std::wstring& s) +{ + // Windows environment names are case-insensitive + std::wstring lc_str; lc_str.reserve(s.size()); + for (const auto& c : s) + { + lc_str.push_back(std::towlower(c)); + } + return lc_str; +} + +bool SystemEnvironment::SysEnvValueCallback(HKEY hk, LPCWSTR pszName, DWORD dwType, LPARAM lParam) +{ + if (!pszName || !*pszName || !(dwType == REG_SZ || dwType == REG_EXPAND_SZ)) + return true; + CEStr reg_data; + if (RegGetStringValue(hk, nullptr, pszName, reg_data) < 0) + return true; + auto& env_data = ((SystemEnvironment*)lParam)->env_data; + const auto key = MakeEnvName(pszName); + auto& data = env_data[key]; + data.expandable = (dwType == REG_EXPAND_SZ); + if (data.name.empty()) + data.name = pszName; + if (key == L"path") + { + // concatenate "%PATH%" as "USER;SYSTEM" + if (!reg_data.IsEmpty()) + data.data = reg_data.c_str(L"") + + std::wstring(data.data.empty() ? L"" : L";") + + data.data; + } + else + { + data.data = reg_data.c_str(L""); + } + return true; +} diff --git a/src/ConEmu/SystemEnvironment.h b/src/ConEmu/SystemEnvironment.h new file mode 100644 index 0000000000..e575a9fc0a --- /dev/null +++ b/src/ConEmu/SystemEnvironment.h @@ -0,0 +1,50 @@ + +/* +Copyright (c) 2019-present Maximus5 +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the authors may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#pragma once + +#include +#include + +// The class is not thread-safe +struct SystemEnvironment +{ + void LoadFromRegistry(); + + static std::wstring MakeEnvName(const std::wstring& s); + static bool WINAPI SysEnvValueCallback(HKEY hk, LPCWSTR pszName, DWORD dwType, LPARAM lParam); + + struct VariableData + { + bool expandable; // for REG_EXPAND_SZ + std::wstring name; // original case + std::wstring data; // contents + }; + std::unordered_map env_data; +}; diff --git a/src/ConEmu/resource.h b/src/ConEmu/resource.h index 4d9be4cb25..a88ee08eae 100644 --- a/src/ConEmu/resource.h +++ b/src/ConEmu/resource.h @@ -1342,6 +1342,7 @@ #define vkCTSCtrlShiftLeft 3103 #define vkCTSCtrlShiftRight 3104 #define cbAnsiLogCodes 3105 +#define cbAutoReloadEnvironment 3106 #define IDC_STATIC -1 // Next default values for new objects @@ -1350,7 +1351,7 @@ #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 246 #define _APS_NEXT_COMMAND_VALUE 40010 -#define _APS_NEXT_CONTROL_VALUE 3106 +#define _APS_NEXT_CONTROL_VALUE 3107 #define _APS_NEXT_SYMED_VALUE 130 #endif #endif