2023-11-24 21:04:16 +03:30
# include <windows.h>
2013-11-30 09:15:42 -05:00
# include <tchar.h>
# include <Shlwapi.h>
# include "resource.h"
2013-12-07 09:17:35 -05:00
# include <vector>
2018-03-13 10:38:27 -05:00
# include <shlobj.h>
2018-03-13 23:40:07 +01:00
# include <regex>
# include <iostream>
2016-01-31 21:35:57 +00:00
2013-11-30 09:15:42 -05:00
# pragma comment(lib, "Shlwapi.lib")
2018-09-05 04:22:42 +04:30
# pragma comment(lib, "comctl32.lib")
2018-08-31 17:02:56 -05:00
# pragma warning( disable : 4091 )
2013-11-30 09:15:42 -05:00
2013-12-07 09:17:35 -05:00
# ifndef UNICODE
# error "Must be compiled with unicode support."
# endif
2013-11-30 09:15:42 -05:00
2013-12-07 09:17:35 -05:00
# define USE_TASKBAR_API (_WIN32_WINNT >= _WIN32_WINNT_WIN7)
# define MB_TITLE L"Cmder Launcher"
2014-02-27 18:34:37 -05:00
# define SHELL_MENU_REGISTRY_PATH_BACKGROUND L"Directory\\Background\\shell\\Cmder"
# define SHELL_MENU_REGISTRY_PATH_LISTITEM L"Directory\\shell\\Cmder"
2019-03-16 19:15:36 -04:00
# define SHELL_MENU_REGISTRY_DRIVE_PATH_BACKGROUND L"Drive\\Background\\shell\\Cmder"
# define SHELL_MENU_REGISTRY_DRIVE_PATH_LISTITEM L"Drive\\shell\\Cmder"
2013-12-07 09:17:35 -05:00
2015-09-04 22:16:30 +02:00
# define streqi(a, b) (_wcsicmp((a), (b)) == 0)
2013-12-07 09:17:35 -05:00
# define WIDEN2(x) L ## x
# define WIDEN(x) WIDEN2(x)
# define __WFUNCTION__ WIDEN(__FUNCTION__)
# define FAIL_ON_ERROR(x) { DWORD ec; if ((ec = (x)) != ERROR_SUCCESS) { ShowErrorAndExit(ec, __WFUNCTION__, __LINE__); } }
2018-09-05 04:22:42 +04:30
void TaskDialogOpen ( PCWSTR mainStr , PCWSTR contentStr )
{
HRESULT hr = NULL ;
TASKDIALOGCONFIG tsk = { sizeof ( tsk ) } ;
HWND hOwner = NULL ;
HINSTANCE hInstance = GetModuleHandle ( NULL ) ;
PCWSTR tskTitle = MAKEINTRESOURCE ( IDS_TITLE ) ;
tsk . hInstance = hInstance ;
tsk . pszMainIcon = MAKEINTRESOURCE ( IDI_CMDER ) ;
tsk . pszWindowTitle = tskTitle ;
2022-09-09 21:31:25 +04:30
tsk . pszMainInstruction = mainStr ;
2018-09-05 04:22:42 +04:30
tsk . pszContent = contentStr ;
TASKDIALOG_BUTTON btns [ 1 ] = {
{ IDOK , L " OK " }
} ;
tsk . dwFlags = TDF_ALLOW_DIALOG_CANCELLATION | TDF_ENABLE_HYPERLINKS ;
tsk . pButtons = btns ;
tsk . cButtons = _countof ( btns ) ;
tsk . hwndParent = hOwner ;
int selectedButtonId = IDOK ;
hr = TaskDialogIndirect ( & tsk , & selectedButtonId , NULL , NULL ) ;
}
2013-12-07 09:17:35 -05:00
void ShowErrorAndExit ( DWORD ec , const wchar_t * func , int line )
2013-11-30 09:15:42 -05:00
{
2013-12-07 09:17:35 -05:00
wchar_t * buffer ;
2015-09-04 22:16:30 +02:00
if ( FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS ,
2016-04-13 14:16:58 -04:00
NULL , ec , 0 , ( LPWSTR ) & buffer , 0 , NULL ) = = 0 )
2013-12-07 09:17:35 -05:00
{
buffer = L " Unknown error. FormatMessage failed. " ;
}
wchar_t message [ 1024 ] ;
swprintf_s ( message , L " %s \n Function: %s \n Line: %d " , buffer , func , line ) ;
LocalFree ( buffer ) ;
2013-11-30 09:15:42 -05:00
2013-12-07 09:17:35 -05:00
MessageBox ( NULL , message , MB_TITLE , MB_OK | MB_ICONERROR ) ;
exit ( 1 ) ;
}
typedef struct _option
{
std : : wstring name ;
bool hasVal ;
std : : wstring value ;
bool set ;
} option ;
typedef std : : pair < std : : wstring , std : : wstring > optpair ;
2015-08-19 02:13:22 +03:00
bool FileExists ( const wchar_t * filePath )
{
HANDLE hFile = CreateFile ( filePath , GENERIC_READ , FILE_SHARE_READ , NULL , OPEN_EXISTING , 0 , NULL ) ;
if ( hFile ! = INVALID_HANDLE_VALUE )
{
CloseHandle ( hFile ) ;
return true ;
}
return false ;
}
2020-06-24 07:23:28 +02:00
void StartCmder ( std : : wstring path = L " " , bool is_single_mode = false , std : : wstring taskName = L " " , std : : wstring title = L " " , std : : wstring iconPath = L " " , std : : wstring cfgRoot = L " " , bool use_user_cfg = true , std : : wstring conemu_args = L " " )
2013-12-07 09:17:35 -05:00
{
2013-11-30 09:15:42 -05:00
# if USE_TASKBAR_API
2013-12-07 09:17:35 -05:00
wchar_t appId [ MAX_PATH ] = { 0 } ;
2013-11-30 09:15:42 -05:00
# endif
2013-12-07 09:17:35 -05:00
wchar_t exeDir [ MAX_PATH ] = { 0 } ;
wchar_t icoPath [ MAX_PATH ] = { 0 } ;
wchar_t cfgPath [ MAX_PATH ] = { 0 } ;
2016-10-02 22:23:12 -05:00
wchar_t backupCfgPath [ MAX_PATH ] = { 0 } ;
2016-10-02 21:50:56 -05:00
wchar_t cpuCfgPath [ MAX_PATH ] = { 0 } ;
wchar_t userCfgPath [ MAX_PATH ] = { 0 } ;
2018-03-13 10:38:27 -05:00
wchar_t defaultCfgPath [ MAX_PATH ] = { 0 } ;
2023-09-25 11:14:54 -04:00
wchar_t terminalPath [ MAX_PATH ] = { 0 } ;
2018-03-13 10:38:27 -05:00
wchar_t configDirPath [ MAX_PATH ] = { 0 } ;
wchar_t userConfigDirPath [ MAX_PATH ] = { 0 } ;
wchar_t userBinDirPath [ MAX_PATH ] = { 0 } ;
wchar_t userProfiledDirPath [ MAX_PATH ] = { 0 } ;
2018-08-31 17:02:56 -05:00
wchar_t userProfilePath [ MAX_PATH ] = { 0 } ;
wchar_t legacyUserProfilePath [ MAX_PATH ] = { 0 } ;
2018-09-01 18:17:09 -04:00
wchar_t userAliasesPath [ MAX_PATH ] = { 0 } ;
wchar_t legacyUserAliasesPath [ MAX_PATH ] = { 0 } ;
2013-12-07 09:17:35 -05:00
wchar_t args [ MAX_PATH * 2 + 256 ] = { 0 } ;
2018-11-10 17:25:17 -05:00
wchar_t userConEmuCfgPath [ MAX_PATH ] = { 0 } ;
2023-09-25 11:14:54 -04:00
wchar_t windowsTerminalDir [ MAX_PATH ] = { 0 } ;
wchar_t conEmuDir [ MAX_PATH ] = { 0 } ;
2023-11-24 11:35:24 -05:00
wchar_t winDir [ MAX_PATH ] = { 0 } ;
2023-09-25 11:14:54 -04:00
wchar_t emulatorPath [ MAX_PATH ] = { 0 } ;
2018-11-10 17:25:17 -05:00
2013-11-30 09:15:42 -05:00
2018-03-13 10:38:27 -05:00
std : : wstring cmderStart = path ;
std : : wstring cmderTask = taskName ;
2020-06-24 07:17:48 +02:00
std : : wstring cmderTitle = title ;
2023-09-25 11:14:54 -04:00
std : : wstring cmderTerminalArgs = conemu_args ;
2018-03-13 10:38:27 -05:00
std : : copy ( cfgRoot . begin ( ) , cfgRoot . end ( ) , userConfigDirPath ) ;
userConfigDirPath [ cfgRoot . length ( ) ] = 0 ;
2013-11-30 09:15:42 -05:00
GetModuleFileName ( NULL , exeDir , sizeof ( exeDir ) ) ;
# if USE_TASKBAR_API
2013-12-07 09:17:35 -05:00
wcscpy_s ( appId , exeDir ) ;
2013-11-30 09:15:42 -05:00
# endif
PathRemoveFileSpec ( exeDir ) ;
2020-06-24 02:50:15 +02:00
if ( PathFileExists ( iconPath . c_str ( ) ) )
{
std : : copy ( iconPath . begin ( ) , iconPath . end ( ) , icoPath ) ;
icoPath [ iconPath . length ( ) ] = 0 ;
}
else
{
PathCombine ( icoPath , exeDir , L " icons \\ cmder.ico " ) ;
}
2015-10-31 16:29:10 -06:00
2018-03-13 10:38:27 -05:00
PathCombine ( configDirPath , exeDir , L " config " ) ;
2018-08-31 17:02:56 -05:00
2018-11-08 21:25:42 -05:00
/*
Convert legacy user - profile . cmd to new name user_profile . cmd
*/
2018-08-31 17:02:56 -05:00
PathCombine ( legacyUserProfilePath , configDirPath , L " user-profile.cmd " ) ;
2018-11-08 21:25:42 -05:00
if ( PathFileExists ( legacyUserProfilePath ) )
{
2018-08-31 17:02:56 -05:00
PathCombine ( userProfilePath , configDirPath , L " user_profile.cmd " ) ;
2023-09-25 11:14:54 -04:00
char * lPr = ( char * ) malloc ( MAX_PATH ) ;
char * pR = ( char * ) malloc ( MAX_PATH ) ;
2018-08-31 17:02:56 -05:00
size_t i ;
wcstombs_s ( & i , lPr , ( size_t ) MAX_PATH ,
legacyUserProfilePath , ( size_t ) MAX_PATH ) ;
wcstombs_s ( & i , pR , ( size_t ) MAX_PATH ,
userProfilePath , ( size_t ) MAX_PATH ) ;
rename ( lPr , pR ) ;
}
2018-11-08 21:25:42 -05:00
/*
Convert legacy user - aliases . cmd to new name user_aliases . cmd
*/
2018-09-01 18:17:09 -04:00
PathCombine ( legacyUserAliasesPath , configDirPath , L " user-aliases.cmd " ) ;
2018-11-08 21:25:42 -05:00
if ( PathFileExists ( legacyUserAliasesPath ) )
{
2018-09-01 18:17:09 -04:00
PathCombine ( userAliasesPath , configDirPath , L " user_aliases.cmd " ) ;
2023-09-25 11:14:54 -04:00
char * lPr = ( char * ) malloc ( MAX_PATH ) ;
char * pR = ( char * ) malloc ( MAX_PATH ) ;
2018-09-01 18:17:09 -04:00
size_t i ;
wcstombs_s ( & i , lPr , ( size_t ) MAX_PATH ,
legacyUserAliasesPath , ( size_t ) MAX_PATH ) ;
wcstombs_s ( & i , pR , ( size_t ) MAX_PATH ,
userAliasesPath , ( size_t ) MAX_PATH ) ;
rename ( lPr , pR ) ;
}
2022-10-15 12:46:13 +03:30
2018-11-08 21:25:42 -05:00
/*
2018-11-11 08:04:35 -05:00
Was / c [ path ] specified ?
2018-11-08 21:25:42 -05:00
*/
2018-03-13 10:38:27 -05:00
if ( wcscmp ( userConfigDirPath , L " " ) = = 0 )
{
2022-09-09 21:31:25 +04:30
// No - It wasn't.
2018-03-13 10:38:27 -05:00
PathCombine ( userConfigDirPath , exeDir , L " config " ) ;
}
else
{
2018-11-08 21:25:42 -05:00
// Yes - It was.
2018-03-13 10:38:27 -05:00
PathCombine ( userBinDirPath , userConfigDirPath , L " bin " ) ;
SHCreateDirectoryEx ( 0 , userBinDirPath , 0 ) ;
PathCombine ( userConfigDirPath , userConfigDirPath , L " config " ) ;
SHCreateDirectoryEx ( 0 , userConfigDirPath , 0 ) ;
PathCombine ( userProfiledDirPath , userConfigDirPath , L " profile.d " ) ;
SHCreateDirectoryEx ( 0 , userProfiledDirPath , 0 ) ;
2018-11-08 21:25:42 -05:00
/*
Convert legacy user - profile . cmd to new name user_profile . cmd
*/
2018-08-31 17:02:56 -05:00
PathCombine ( legacyUserProfilePath , userConfigDirPath , L " user-profile.cmd " ) ;
2018-11-08 21:25:42 -05:00
if ( PathFileExists ( legacyUserProfilePath ) )
{
2018-08-31 17:02:56 -05:00
PathCombine ( userProfilePath , userConfigDirPath , L " user_profile.cmd " ) ;
2023-09-25 11:14:54 -04:00
char * lPr = ( char * ) malloc ( MAX_PATH ) ;
char * pR = ( char * ) malloc ( MAX_PATH ) ;
2018-08-31 17:02:56 -05:00
size_t i ;
wcstombs_s ( & i , lPr , ( size_t ) MAX_PATH ,
legacyUserProfilePath , ( size_t ) MAX_PATH ) ;
wcstombs_s ( & i , pR , ( size_t ) MAX_PATH ,
userProfilePath , ( size_t ) MAX_PATH ) ;
rename ( lPr , pR ) ;
}
2018-09-01 18:17:09 -04:00
2018-11-08 21:25:42 -05:00
/*
Convert legacy user - aliases . cmd to new name user_aliases . cmd
*/
2018-09-01 18:17:09 -04:00
PathCombine ( legacyUserAliasesPath , userConfigDirPath , L " user-aliases.cmd " ) ;
2018-11-08 21:25:42 -05:00
if ( PathFileExists ( legacyUserAliasesPath ) )
{
2018-09-01 18:17:09 -04:00
PathCombine ( userAliasesPath , userConfigDirPath , L " user_aliases.cmd " ) ;
2023-09-25 11:14:54 -04:00
char * lPr = ( char * ) malloc ( MAX_PATH ) ;
char * pR = ( char * ) malloc ( MAX_PATH ) ;
2018-09-01 18:17:09 -04:00
size_t i ;
wcstombs_s ( & i , lPr , ( size_t ) MAX_PATH ,
legacyUserAliasesPath , ( size_t ) MAX_PATH ) ;
wcstombs_s ( & i , pR , ( size_t ) MAX_PATH ,
userAliasesPath , ( size_t ) MAX_PATH ) ;
rename ( lPr , pR ) ;
}
2018-03-13 10:38:27 -05:00
}
2023-09-25 11:14:54 -04:00
PathCombine ( windowsTerminalDir , exeDir , L " vendor \\ windows-terminal " ) ;
PathCombine ( conEmuDir , exeDir , L " vendor \\ conemu-maximus5 " ) ;
2023-11-24 11:35:24 -05:00
GetEnvironmentVariable ( L " WINDIR " , winDir , MAX_PATH ) ;
2018-03-13 10:38:27 -05:00
2023-09-25 11:14:54 -04:00
if ( PathFileExists ( windowsTerminalDir ) )
{
// Set path to vendored ConEmu config file
PathCombine ( cfgPath , windowsTerminalDir , L " settings \\ settings.json " ) ;
2018-03-13 10:38:27 -05:00
2023-09-25 11:14:54 -04:00
// Set path to Cmder default ConEmu config file
PathCombine ( defaultCfgPath , exeDir , L " vendor \\ windows_terminal_default_settings.json " ) ;
2016-10-02 18:34:40 -05:00
2023-09-25 11:14:54 -04:00
// Check for machine-specific then user config source file.
PathCombine ( cpuCfgPath , userConfigDirPath , L " windows_terminal_%COMPUTERNAME%_settings.json " ) ;
ExpandEnvironmentStrings ( cpuCfgPath , cpuCfgPath , sizeof ( cpuCfgPath ) / sizeof ( cpuCfgPath [ 0 ] ) ) ;
2018-11-08 21:25:42 -05:00
2023-09-25 11:14:54 -04:00
// Set path to Cmder user ConEmu config file
PathCombine ( userCfgPath , userConfigDirPath , L " user_windows_terminal_settings.json " ) ;
}
2023-11-24 11:35:24 -05:00
else if ( PathFileExists ( conEmuDir ) )
2023-09-25 11:14:54 -04:00
{
// Set path to vendored ConEmu config file
PathCombine ( cfgPath , conEmuDir , L " ConEmu.xml " ) ;
// Set path to Cmder default ConEmu config file
PathCombine ( defaultCfgPath , exeDir , L " vendor \\ ConEmu.xml.default " ) ;
// Check for machine-specific then user config source file.
PathCombine ( cpuCfgPath , userConfigDirPath , L " ConEmu-%COMPUTERNAME%.xml " ) ;
ExpandEnvironmentStrings ( cpuCfgPath , cpuCfgPath , sizeof ( cpuCfgPath ) / sizeof ( cpuCfgPath [ 0 ] ) ) ;
// Set path to Cmder user ConEmu config file
PathCombine ( userCfgPath , userConfigDirPath , L " user-ConEmu.xml " ) ;
}
2024-05-31 18:31:16 -04:00
if ( PathFileExists ( cpuCfgPath ) | | use_user_cfg = = false ) // config/[cpu specific terminal emulator config] file exists or /m was specified on command line, use machine specific config.
2018-11-08 21:25:42 -05:00
{
2018-11-11 08:04:35 -05:00
if ( cfgRoot . length ( ) = = 0 ) // '/c [path]' was NOT specified
2018-11-08 21:25:42 -05:00
{
2024-05-31 18:31:16 -04:00
if ( PathFileExists ( cfgPath ) ) // [terminal emulator config] file exists, copy [terminal emulator config] to config/user_[terminal emulator config] file to backup any settings changes from previous sessions.
2018-03-13 10:38:27 -05:00
{
2024-05-31 18:31:16 -04:00
if ( ! CopyFile ( cfgPath , cpuCfgPath , FALSE ) )
2023-11-24 11:35:24 -05:00
{
2024-08-15 10:24:02 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
2024-05-31 18:31:16 -04:00
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/windows-terminal/settings/settings.json file to config/windows_terminal_%COMPUTERNAME%_settings.json! Access Denied. "
: L " Failed to copy vendor/windows-terminal/settings/settings.json file to config/windows_teerminal_%COMPUTERNAME%_settigns.json! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
else if ( PathFileExists ( conEmuDir ) )
{
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/conemu-maximus5/ConEmu.xml file to config/ConEmu-%COMPUTERNAME%.xml! Access Denied. "
: L " Failed to copy vendor/conemu-maximus5/ConEmu.xml file to config/ConEmu-%COMPUTERNAME%.xml! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2018-11-10 17:25:17 -05:00
}
2018-03-13 10:38:27 -05:00
}
2024-05-31 18:31:16 -04:00
else // [terminal emulator config] file does not exist, copy config/[cpu specific terminal emulator config] file to [terminal emulator config] file
2018-03-13 10:38:27 -05:00
{
2024-05-31 18:31:16 -04:00
if ( ! CopyFile ( cpuCfgPath , cfgPath , FALSE ) )
2023-11-24 11:35:24 -05:00
{
2024-08-15 10:24:02 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
2024-05-31 18:31:16 -04:00
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy config/windows_terminal_%COMPUTERNAME%_settings.json file to vendor/windows-terminal/settings/settings.json! Access Denied. "
: L " Failed to copy config/windows_terminal_%COMPUTERNAME%_settings.json file to vendor/windows-terminal/settings/settings.json! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
else if ( PathFileExists ( conEmuDir ) )
{
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy config/ConEmu-%COMPUTERNAME%.xml file to vendor/conemu-maximus5/ConEmu.xml! Access Denied. "
: L " Failed to copy config/ConEmu-%COMPUTERNAME%.xml file to vendor/conemu-maximus5/ConEmu.xml! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2018-11-12 08:30:44 -05:00
}
2018-03-13 10:38:27 -05:00
}
}
2016-10-02 21:50:56 -05:00
}
2024-05-31 18:31:16 -04:00
else if ( PathFileExists ( userCfgPath ) ) // config/user[terminal emulator config] file exists, use it.
2018-11-10 17:25:17 -05:00
{
2018-11-11 08:04:35 -05:00
if ( cfgRoot . length ( ) = = 0 ) // '/c [path]' was NOT specified
2018-11-10 17:25:17 -05:00
{
2024-05-31 18:31:16 -04:00
if ( PathFileExists ( cfgPath ) ) // [terminal emulator config] file exists, copy [terminal emulator config] to config/user_[terminal emulator config] file to backup any settings changes from previous sessions.
2018-03-13 10:38:27 -05:00
{
2018-11-10 17:25:17 -05:00
if ( ! CopyFile ( cfgPath , userCfgPath , FALSE ) )
{
2024-08-15 10:24:02 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
2023-09-25 11:14:54 -04:00
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/windows-terminal/settings/settings.json file to config/windows_terminal_settings.json! Access Denied. "
: L " Failed to copy vendor/windows-terminal/settings/settings.json file to config/windows_teerminal_settigns.json! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2023-11-24 11:35:24 -05:00
else if ( PathFileExists ( conEmuDir ) )
2023-09-25 11:14:54 -04:00
{
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/conemu-maximus5/ConEmu.xml file to config/user-conemu.xml! Access Denied. "
: L " Failed to copy vendor/conemu-maximus5/ConEmu.xml file to config/user-conemu.xml! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2018-11-10 17:25:17 -05:00
}
}
2024-05-31 18:31:16 -04:00
else // [terminal emulator config] file does not exist, copy config/user_[terminal emulator config] file to [terminal emulator config] file
2018-11-10 17:25:17 -05:00
{
if ( ! CopyFile ( userCfgPath , cfgPath , FALSE ) )
{
2024-08-15 10:24:02 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
2023-09-25 11:14:54 -04:00
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy config/user_windows_terminal_settings.json file to vendor/windows-terminal/settings/settings.json! Access Denied. "
: L " Failed to copy config/user_windows_terminal_settings.json file to vendor/windows-terminal/settings/settings.json! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2023-11-24 11:35:24 -05:00
else if ( PathFileExists ( conEmuDir ) )
2023-09-25 11:14:54 -04:00
{
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy config/user-conemu.xml file to vendor/conemu-maximus5/ConEmu.xml! Access Denied. "
: L " Failed to copy config/user-conemu.xml file to vendor/conemu-maximus5/ConEmu.xml! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2018-11-10 17:25:17 -05:00
}
2018-03-13 10:38:27 -05:00
}
}
2024-05-31 18:31:16 -04:00
else if ( ! PathFileExists ( windowsTerminalDir ) ) { // '/c [path]' was specified, don't copy anything and use existing user_[terminal emulator config] file.
2018-11-10 17:25:17 -05:00
PathCombine ( userConEmuCfgPath , userConfigDirPath , L " user-ConEmu.xml " ) ;
}
2016-10-02 21:50:56 -05:00
}
2022-09-09 21:31:25 +04:30
else if ( cfgRoot . length ( ) = = 0 ) // '/c [path]' was NOT specified
2018-11-08 21:25:42 -05:00
{
2024-05-31 18:31:16 -04:00
if ( PathFileExists ( cfgPath ) ) // [terminal emulator config] file exists, copy [terminal emulator config] file to config/user_[terminal emulator config] file.
2018-11-08 21:25:42 -05:00
{
2018-03-13 10:38:27 -05:00
if ( ! CopyFile ( cfgPath , userCfgPath , FALSE ) )
{
2024-08-15 10:24:02 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
2023-09-25 11:14:54 -04:00
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/windows-terminal/settings/settings.json file to config/user_windows_terminal_settings.json! Access Denied. "
: L " Failed to copy vendor/windows-terminal/settings/settings.json file to config/user_windows_terminal_settigns.json! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2023-11-24 11:35:24 -05:00
else if ( PathFileExists ( conEmuDir ) )
2023-09-25 11:14:54 -04:00
{
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/conemu-maximus5/ConEmu.xml file to config/user-conemu.xml! Access Denied. "
: L " Failed to copy vendor/conemu-maximus5/ConEmu.xml file to config/user-conemu.xml! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
2023-11-24 11:35:24 -05:00
}
2018-03-13 10:38:27 -05:00
}
2024-05-31 18:31:16 -04:00
else // vendor/[terminal emulator config].default config exists, copy Cmder vendor/[terminal emulator config].default file to [terminal emulator config] file.
2018-03-13 10:38:27 -05:00
{
2018-11-10 17:25:17 -05:00
if ( ! CopyFile ( defaultCfgPath , cfgPath , FALSE ) )
{
2024-08-15 10:24:02 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
2023-09-25 11:14:54 -04:00
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/windows-terminal_default_settings_settings.json file to vendor/windows-terminal/settings/settings.json! Access Denied. "
: L " Failed to copy vendor/windows-terminal_default_settings_settings.json file to vendor/windows-terminal/settings/settigns.json! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2023-11-24 11:35:24 -05:00
else if ( PathFileExists ( conEmuDir ) )
2023-09-25 11:14:54 -04:00
{
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/ConEmu.xml.default file to vendor/conemu-maximus5/ConEmu.xml! Access Denied. "
: L " Failed to copy vendor/ConEmu.xml.default file to vendor/conemu-maximus5/ConEmu.xml! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2018-11-10 17:25:17 -05:00
}
2018-03-13 10:38:27 -05:00
}
}
2024-08-15 10:24:02 -04:00
else if ( ! CopyFile ( defaultCfgPath , cfgPath , FALSE ) & & PathFileExists ( conEmuDir ) )
2024-05-31 18:31:16 -04:00
{
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/ConEmu.xml.default file to vendor/conemu-maximus5/ConEmu.xml! Access Denied. "
: L " Failed to copy vendor/ConEmu.xml.default file to vendor/conemu-maximus5/ConEmu.xml! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2018-03-13 10:38:27 -05:00
}
}
2024-05-31 18:31:16 -04:00
else if ( PathFileExists ( cfgPath ) ) // This is a first time Cmder.exe run and [terminal emulator config] file exists, copy [terminal emulator config] file to config/user_[terminal emulator config] file.
2018-11-11 07:55:14 -05:00
{
2018-03-13 10:38:27 -05:00
if ( ! CopyFile ( cfgPath , userCfgPath , FALSE ) )
{
2024-08-15 10:24:02 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
2023-09-25 11:14:54 -04:00
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/windows-terminal/settings/settings.json file to config/user_windows_terminal_settings_settings.json! Access Denied. "
: L " Failed to copy vendor/windows-terminal/settings/settings.json file to config/user_windows_terminal_settings_settigns.json! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
else
{
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/conemu-maximus5/ConEmu.xml file to config/user-conemu.xml! Access Denied. "
: L " Failed to copy vendor/conemu-maximus5/ConEmu.xml file to config/user-conemu.xml! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2018-03-13 10:38:27 -05:00
}
2018-11-11 07:55:14 -05:00
PathCombine ( userConEmuCfgPath , userConfigDirPath , L " user-ConEmu.xml " ) ;
2016-10-02 21:50:56 -05:00
}
2024-05-31 18:31:16 -04:00
else if ( wcscmp ( defaultCfgPath , L " " ) = = 0 ) // '/c [path]' was specified and 'vendor/[terminal emulator config].default' config exists, copy Cmder 'vendor/[terminal emulator config].default' file to '[user specified path]/config/user_[terminal emulator config]'.
2018-11-08 21:25:42 -05:00
{
2018-11-10 17:25:17 -05:00
if ( ! CopyFile ( defaultCfgPath , userCfgPath , FALSE ) )
2018-03-13 10:38:27 -05:00
{
2024-08-15 10:24:02 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
2023-09-25 11:14:54 -04:00
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/windows-terminal_default_settings_settings.json file to [user specified path]/config/user_windows_terminal_settings.json! Access Denied. "
: L " Failed to copy vendor/windows-terminal_default_settings_settings.json file to [user specified path]/config/user_windows_terminal_settings.json! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
else
{
MessageBox ( NULL ,
( GetLastError ( ) = = ERROR_ACCESS_DENIED )
? L " Failed to copy vendor/ConEmu.xml.default file to [user specified path]/config/user_ConEmu.xml! Access Denied. "
: L " Failed to copy vendor/ConEmu.xml.default file to [user specified path]/config/user_ConEmu.xml! " , MB_TITLE , MB_ICONSTOP ) ;
exit ( 1 ) ;
}
2018-03-13 10:38:27 -05:00
}
2018-11-10 17:25:17 -05:00
PathCombine ( userConEmuCfgPath , userConfigDirPath , L " user-ConEmu.xml " ) ;
2015-03-17 14:39:27 -06:00
}
2015-10-31 16:29:10 -06:00
2016-06-30 01:55:13 +02:00
SYSTEM_INFO sysInfo ;
GetNativeSystemInfo ( & sysInfo ) ;
2023-09-25 11:14:54 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
PathCombine ( terminalPath , exeDir , L " vendor \\ windows-terminal \\ WindowsTerminal.exe " ) ;
2016-06-30 01:55:13 +02:00
}
2023-11-24 11:35:24 -05:00
else if ( PathFileExists ( conEmuDir ) )
2018-11-08 21:25:42 -05:00
{
2023-09-25 11:14:54 -04:00
PathCombine ( terminalPath , exeDir , L " vendor \\ conemu-maximus5 \\ ConEmu64.exe " ) ;
2016-06-30 01:55:13 +02:00
}
2023-11-24 11:35:24 -05:00
else
{
PathCombine ( terminalPath , winDir , L " system32 \\ cmd.exe " ) ;
}
2013-11-30 09:15:42 -05:00
2023-09-25 11:14:54 -04:00
if ( ! PathFileExists ( windowsTerminalDir ) ) {
swprintf_s ( args , L " %s /Icon \" %s \" " , args , icoPath ) ;
}
2018-11-10 17:25:17 -05:00
if ( ! streqi ( cmderStart . c_str ( ) , L " " ) )
{
2023-09-25 11:14:54 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
swprintf_s ( args , L " %s -d \" %s \" " , args , cmderStart . c_str ( ) ) ;
}
else
{
swprintf_s ( args , L " %s /dir \" %s \" " , args , cmderStart . c_str ( ) ) ;
}
2018-11-10 17:25:17 -05:00
}
2015-09-04 22:16:30 +02:00
if ( is_single_mode )
2014-11-20 10:59:17 -05:00
{
2023-09-25 11:26:42 -04:00
if ( ! PathFileExists ( windowsTerminalDir ) ) {
2023-09-25 11:14:54 -04:00
swprintf_s ( args , L " %s /single " , args ) ;
}
2014-11-20 10:59:17 -05:00
}
2018-11-10 17:25:17 -05:00
2020-06-24 07:17:48 +02:00
if ( ! streqi ( cmderTitle . c_str ( ) , L " " ) )
{
2023-09-25 11:14:54 -04:00
if ( ! PathFileExists ( windowsTerminalDir ) ) {
swprintf_s ( args , L " %s /title \" %s \" " , args , cmderTitle . c_str ( ) ) ;
}
2020-06-24 07:17:48 +02:00
}
2018-11-10 17:25:17 -05:00
if ( cfgRoot . length ( ) ! = 0 )
{
2023-09-25 11:14:54 -04:00
if ( ! PathFileExists ( windowsTerminalDir ) ) {
swprintf_s ( args , L " %s -loadcfgfile \" %s \" " , args , userConEmuCfgPath ) ;
}
2014-11-20 10:59:17 -05:00
}
2013-11-30 09:15:42 -05:00
2023-09-25 11:14:54 -04:00
if ( ! streqi ( cmderTerminalArgs . c_str ( ) , L " " ) )
2019-05-12 15:18:15 +02:00
{
2023-09-25 11:14:54 -04:00
swprintf_s ( args , L " %s %s " , args , cmderTerminalArgs . c_str ( ) ) ;
2014-11-20 10:59:17 -05:00
}
2013-11-30 09:15:42 -05:00
2022-12-22 12:16:51 +01:00
// The `/run` arg and its value MUST be the last arg of ConEmu
// see : https://conemu.github.io/en/ConEmuArgs.html
// > This must be the last used switch (excepting -new_console and -cur_console)
if ( ! streqi ( cmderTask . c_str ( ) , L " " ) )
{
2023-09-25 11:14:54 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
swprintf_s ( args , L " %s -p \" %s \" " , args , cmderTask . c_str ( ) ) ;
}
2023-11-24 11:35:24 -05:00
else if ( PathFileExists ( conEmuDir ) )
2023-09-25 11:14:54 -04:00
{
swprintf_s ( args , L " %s /run {%s} " , args , cmderTask . c_str ( ) ) ;
}
2023-11-24 11:35:24 -05:00
else
{
swprintf_s ( args , L " %s %s " , args , cmderTask . c_str ( ) ) ;
}
2022-12-22 12:16:51 +01:00
}
2018-01-08 19:22:42 +01:00
SetEnvironmentVariable ( L " CMDER_ROOT " , exeDir ) ;
2018-03-13 10:38:27 -05:00
if ( wcscmp ( userConfigDirPath , configDirPath ) ! = 0 )
{
SetEnvironmentVariable ( L " CMDER_USER_CONFIG " , userConfigDirPath ) ;
SetEnvironmentVariable ( L " CMDER_USER_BIN " , userBinDirPath ) ;
}
2018-01-08 19:22:42 +01:00
2016-01-15 13:13:16 +00:00
// Ensure EnvironmentVariables are propagated.
2016-10-14 23:36:22 +02:00
2013-11-30 09:15:42 -05:00
STARTUPINFO si = { 0 } ;
2018-03-13 10:38:27 -05:00
2013-11-30 09:15:42 -05:00
si . cb = sizeof ( STARTUPINFO ) ;
# if USE_TASKBAR_API
si . lpTitle = appId ;
si . dwFlags = STARTF_TITLEISAPPID ;
# endif
PROCESS_INFORMATION pi ;
2023-11-24 11:35:24 -05:00
// MessageBox(NULL, terminalPath, _T("Error"), MB_OK);
// MessageBox(NULL, args, _T("Error"), MB_OK);
if ( ! CreateProcess ( terminalPath , args , NULL , NULL , false , 0 , NULL , NULL , & si , & pi ) )
{
2023-09-26 11:17:16 -04:00
if ( PathFileExists ( windowsTerminalDir ) ) {
MessageBox ( NULL , _T ( " Unable to create the Windows Terminal process! " ) , _T ( " Error " ) , MB_OK ) ;
}
2023-11-24 11:35:24 -05:00
else if ( PathFileExists ( conEmuDir ) )
2023-09-26 11:17:16 -04:00
{
MessageBox ( NULL , _T ( " Unable to create the ConEmu process! " ) , _T ( " Error " ) , MB_OK ) ;
}
2023-11-24 11:35:24 -05:00
else
{
MessageBox ( NULL , _T ( " Unable to create the Cmd process! " ) , _T ( " Error " ) , MB_OK ) ;
}
2016-01-31 21:35:57 +00:00
return ;
}
2013-12-07 09:17:35 -05:00
}
bool IsUserOnly ( std : : wstring opt )
{
bool userOnly ;
if ( streqi ( opt . c_str ( ) , L " ALL " ) )
{
userOnly = false ;
}
else if ( streqi ( opt . c_str ( ) , L " USER " ) )
{
userOnly = true ;
}
else
{
MessageBox ( NULL , L " Unrecognized option for /REGISTER or /UNREGISTER. Must be either ALL or USER. " , MB_TITLE , MB_OK ) ;
exit ( 1 ) ;
}
return userOnly ;
}
HKEY GetRootKey ( std : : wstring opt )
{
HKEY root ;
if ( IsUserOnly ( opt ) )
{
2016-04-13 14:16:58 -04:00
FAIL_ON_ERROR ( RegCreateKeyEx ( HKEY_CURRENT_USER , L " Software \\ Classes " , 0 , NULL , REG_OPTION_NON_VOLATILE , KEY_ALL_ACCESS , NULL , & root , NULL ) ) ;
2013-12-07 09:17:35 -05:00
}
else
{
root = HKEY_CLASSES_ROOT ;
}
return root ;
}
2018-12-07 21:05:51 -08:00
void RegisterShellMenu ( std : : wstring opt , wchar_t * keyBaseName , std : : wstring cfgRoot , bool single )
2013-12-07 09:17:35 -05:00
{
2018-11-12 09:22:05 -05:00
wchar_t userConfigDirPath [ MAX_PATH ] = { 0 } ;
2014-02-27 18:34:37 -05:00
// First, get the paths we will use
wchar_t exePath [ MAX_PATH ] = { 0 } ;
wchar_t icoPath [ MAX_PATH ] = { 0 } ;
GetModuleFileName ( NULL , exePath , sizeof ( exePath ) ) ;
2018-03-13 23:40:07 +01:00
wchar_t commandStr [ MAX_PATH + 20 ] = { 0 } ;
2018-12-07 21:05:51 -08:00
wchar_t baseCommandStr [ MAX_PATH + 20 ] = { 0 } ;
2023-09-25 11:57:33 -04:00
if ( single ) {
2023-09-25 11:44:09 -04:00
swprintf_s ( baseCommandStr , L " \" %s \" /single " , exePath ) ;
2018-12-07 21:05:51 -08:00
}
else {
2023-09-25 11:44:09 -04:00
swprintf_s ( baseCommandStr , L " \" %s \" " , exePath ) ;
2018-12-07 21:05:51 -08:00
}
2018-11-12 09:22:05 -05:00
if ( cfgRoot . length ( ) = = 0 ) // '/c [path]' was NOT specified
{
2018-12-07 21:05:51 -08:00
swprintf_s ( commandStr , L " %s \" %%V \" " , baseCommandStr ) ;
2018-11-12 09:22:05 -05:00
}
else {
std : : copy ( cfgRoot . begin ( ) , cfgRoot . end ( ) , userConfigDirPath ) ;
userConfigDirPath [ cfgRoot . length ( ) ] = 0 ;
2018-12-07 21:05:51 -08:00
swprintf_s ( commandStr , L " %s /c \" %s \" \" %%V \" " , baseCommandStr , userConfigDirPath ) ;
2018-11-12 09:22:05 -05:00
}
2014-02-27 18:34:37 -05:00
2018-03-13 23:40:07 +01:00
// Now that we have `commandStr`, it's OK to change `exePath`...
PathRemoveFileSpec ( exePath ) ;
2014-02-27 18:34:37 -05:00
2018-03-13 23:40:07 +01:00
PathCombine ( icoPath , exePath , L " icons \\ cmder.ico " ) ;
2014-02-27 18:34:37 -05:00
2018-03-13 23:40:07 +01:00
// Now set the registry keys
HKEY root = GetRootKey ( opt ) ;
2013-12-07 09:17:35 -05:00
2018-03-13 23:40:07 +01:00
HKEY cmderKey ;
FAIL_ON_ERROR ( RegCreateKeyEx ( root , keyBaseName , 0 , NULL , REG_OPTION_NON_VOLATILE , KEY_ALL_ACCESS , NULL , & cmderKey , NULL ) ) ;
2013-12-07 09:17:35 -05:00
2018-03-13 23:40:07 +01:00
FAIL_ON_ERROR ( RegSetValue ( cmderKey , L " " , REG_SZ , L " Cmder Here " , NULL ) ) ;
FAIL_ON_ERROR ( RegSetValueEx ( cmderKey , L " NoWorkingDirectory " , 0 , REG_SZ , ( BYTE * ) L " " , 2 ) ) ;
2013-12-07 09:17:35 -05:00
2018-03-13 23:40:07 +01:00
FAIL_ON_ERROR ( RegSetValueEx ( cmderKey , L " Icon " , 0 , REG_SZ , ( BYTE * ) icoPath , wcslen ( icoPath ) * sizeof ( wchar_t ) ) ) ;
2014-02-27 18:34:37 -05:00
2018-03-13 23:40:07 +01:00
HKEY command ;
FAIL_ON_ERROR ( RegCreateKeyEx ( cmderKey , L " command " , 0 , NULL , REG_OPTION_NON_VOLATILE , KEY_ALL_ACCESS , NULL , & command , NULL ) ) ;
2013-12-07 09:17:35 -05:00
2018-03-13 23:40:07 +01:00
FAIL_ON_ERROR ( RegSetValue ( command , L " " , REG_SZ , commandStr , NULL ) ) ;
2013-12-07 09:17:35 -05:00
2018-03-13 23:40:07 +01:00
RegCloseKey ( command ) ;
RegCloseKey ( cmderKey ) ;
RegCloseKey ( root ) ;
2013-12-07 09:17:35 -05:00
}
2014-02-27 18:34:37 -05:00
void UnregisterShellMenu ( std : : wstring opt , wchar_t * keyBaseName )
2013-12-07 09:17:35 -05:00
{
HKEY root = GetRootKey ( opt ) ;
HKEY cmderKey ;
2016-04-13 14:16:58 -04:00
FAIL_ON_ERROR ( RegCreateKeyEx ( root , keyBaseName , 0 , NULL , REG_OPTION_NON_VOLATILE , KEY_ALL_ACCESS , NULL , & cmderKey , NULL ) ) ;
2014-08-26 23:52:49 +01:00
FAIL_ON_ERROR ( RegDeleteTree ( cmderKey , NULL ) ) ;
2018-04-12 21:41:42 -04:00
RegDeleteKeyEx ( root , keyBaseName , KEY_ALL_ACCESS , NULL ) ;
2013-12-07 09:17:35 -05:00
RegCloseKey ( cmderKey ) ;
RegCloseKey ( root ) ;
}
2018-03-13 10:38:27 -05:00
struct cmderOptions
{
std : : wstring cmderCfgRoot = L " " ;
std : : wstring cmderStart = L " " ;
std : : wstring cmderTask = L " " ;
2020-06-24 07:17:48 +02:00
std : : wstring cmderTitle = L " Cmder " ;
2020-06-24 02:50:15 +02:00
std : : wstring cmderIcon = L " " ;
2018-03-13 10:38:27 -05:00
std : : wstring cmderRegScope = L " USER " ;
2023-09-25 11:14:54 -04:00
std : : wstring cmderTerminalArgs = L " " ;
2018-03-13 10:38:27 -05:00
bool cmderSingle = false ;
2018-11-12 07:56:55 -05:00
bool cmderUserCfg = true ;
2018-03-13 10:38:27 -05:00
bool registerApp = false ;
bool unRegisterApp = false ;
bool error = false ;
} ;
cmderOptions GetOption ( )
{
cmderOptions cmderOptions ;
LPWSTR * szArgList ;
int argCount ;
2023-09-25 11:57:33 -04:00
wchar_t windowsTerminalDir [ MAX_PATH ] = { 0 } ;
2023-11-24 11:35:24 -05:00
wchar_t conEmuDir [ MAX_PATH ] = { 0 } ;
wchar_t vendorDir [ MAX_PATH ] = { 0 } ;
2023-09-25 12:02:57 -04:00
wchar_t exeDir [ MAX_PATH ] = { 0 } ;
2023-11-24 11:35:24 -05:00
wchar_t cmdInit [ MAX_PATH ] = { 0 } ;
2023-09-25 12:02:57 -04:00
GetModuleFileName ( NULL , exeDir , sizeof ( exeDir ) ) ;
PathRemoveFileSpec ( exeDir ) ;
2023-11-24 11:35:24 -05:00
PathCombine ( vendorDir , exeDir , L " vendor " ) ;
PathCombine ( windowsTerminalDir , vendorDir , L " windows-terminal " ) ;
PathCombine ( conEmuDir , vendorDir , L " ConEmu-Maximus5 " ) ;
PathCombine ( cmdInit , vendorDir , L " init.bat " ) ;
2023-09-25 11:57:33 -04:00
2018-03-13 10:38:27 -05:00
szArgList = CommandLineToArgvW ( GetCommandLine ( ) , & argCount ) ;
for ( int i = 1 ; i < argCount ; i + + )
{
// MessageBox(NULL, szArgList[i], L"Arglist contents", MB_OK);
2018-11-12 08:30:44 -05:00
if ( cmderOptions . error = = false ) {
if ( _wcsicmp ( L " /c " , szArgList [ i ] ) = = 0 )
{
TCHAR userProfile [ MAX_PATH ] ;
const DWORD ret = GetEnvironmentVariable ( L " USERPROFILE " , userProfile , MAX_PATH ) ;
2018-03-13 10:38:27 -05:00
2018-11-12 08:30:44 -05:00
wchar_t cmderCfgRoot [ MAX_PATH ] = { 0 } ;
PathCombine ( cmderCfgRoot , userProfile , L " cmder_cfg " ) ;
2018-03-13 10:38:27 -05:00
2018-11-12 08:30:44 -05:00
cmderOptions . cmderCfgRoot = cmderCfgRoot ;
2018-03-13 10:38:27 -05:00
2018-11-12 08:30:44 -05:00
if ( szArgList [ i + 1 ] ! = NULL & & szArgList [ i + 1 ] [ 0 ] ! = ' / ' )
{
cmderOptions . cmderCfgRoot = szArgList [ i + 1 ] ;
i + + ;
}
2018-03-13 10:38:27 -05:00
}
2018-11-12 08:30:44 -05:00
else if ( _wcsicmp ( L " /start " , szArgList [ i ] ) = = 0 )
2018-09-03 11:06:39 -05:00
{
2018-11-12 08:30:44 -05:00
int len = wcslen ( szArgList [ i + 1 ] ) ;
if ( wcscmp ( & szArgList [ i + 1 ] [ len - 1 ] , L " \" " ) = = 0 )
{
szArgList [ i + 1 ] [ len - 1 ] = ' \0 ' ;
}
2018-03-13 10:38:27 -05:00
2018-11-12 08:30:44 -05:00
if ( PathFileExists ( szArgList [ i + 1 ] ) )
{
cmderOptions . cmderStart = szArgList [ i + 1 ] ;
i + + ;
}
else
{
MessageBox ( NULL , szArgList [ i + 1 ] , L " /START - Folder does not exist! " , MB_OK ) ;
}
}
2023-11-24 11:35:24 -05:00
else if ( _wcsicmp ( L " /task " , szArgList [ i ] ) = = 0 | | PathFileExists ( windowsTerminalDir ) | | PathFileExists ( conEmuDir ) )
2018-03-13 10:38:27 -05:00
{
2018-11-12 08:30:44 -05:00
cmderOptions . cmderTask = szArgList [ i + 1 ] ;
2018-03-13 10:38:27 -05:00
i + + ;
}
2023-11-24 11:35:24 -05:00
else if ( _wcsicmp ( L " /title " , szArgList [ i ] ) = = 0 & & ! PathFileExists ( windowsTerminalDir ) & & PathFileExists ( conEmuDir ) )
2020-06-24 07:17:48 +02:00
{
cmderOptions . cmderTitle = szArgList [ i + 1 ] ;
i + + ;
}
2023-11-24 11:35:24 -05:00
else if ( _wcsicmp ( L " /icon " , szArgList [ i ] ) = = 0 & & ! PathFileExists ( windowsTerminalDir ) & & PathFileExists ( conEmuDir ) )
2018-03-13 10:38:27 -05:00
{
2020-06-24 02:50:15 +02:00
cmderOptions . cmderIcon = szArgList [ i + 1 ] ;
2018-03-13 10:38:27 -05:00
i + + ;
}
2023-11-24 11:35:24 -05:00
else if ( _wcsicmp ( L " /single " , szArgList [ i ] ) = = 0 & & ! PathFileExists ( windowsTerminalDir ) & & PathFileExists ( conEmuDir ) )
2018-11-08 21:25:42 -05:00
{
2018-11-12 08:30:44 -05:00
cmderOptions . cmderSingle = true ;
2018-03-13 10:38:27 -05:00
}
2023-11-24 11:35:24 -05:00
else if ( _wcsicmp ( L " /m " , szArgList [ i ] ) = = 0 & & ! PathFileExists ( windowsTerminalDir ) & & PathFileExists ( conEmuDir ) )
2018-03-13 23:40:07 +01:00
{
2018-11-12 08:30:44 -05:00
cmderOptions . cmderUserCfg = false ;
2018-03-13 10:38:27 -05:00
}
2018-11-12 08:30:44 -05:00
else if ( _wcsicmp ( L " /register " , szArgList [ i ] ) = = 0 )
2018-03-13 10:38:27 -05:00
{
2018-11-12 08:30:44 -05:00
cmderOptions . registerApp = true ;
cmderOptions . unRegisterApp = false ;
if ( szArgList [ i + 1 ] ! = NULL )
2018-03-13 10:38:27 -05:00
{
2018-11-12 08:30:44 -05:00
if ( _wcsicmp ( L " all " , szArgList [ i + 1 ] ) = = 0 | | _wcsicmp ( L " user " , szArgList [ i + 1 ] ) = = 0 )
{
cmderOptions . cmderRegScope = szArgList [ i + 1 ] ;
i + + ;
}
2018-03-13 10:38:27 -05:00
}
}
2018-11-12 08:30:44 -05:00
else if ( _wcsicmp ( L " /unregister " , szArgList [ i ] ) = = 0 )
2018-09-03 11:06:39 -05:00
{
2018-11-12 08:30:44 -05:00
cmderOptions . unRegisterApp = true ;
cmderOptions . registerApp = false ;
if ( szArgList [ i + 1 ] ! = NULL )
{
if ( _wcsicmp ( L " all " , szArgList [ i + 1 ] ) = = 0 | | _wcsicmp ( L " user " , szArgList [ i + 1 ] ) = = 0 )
{
cmderOptions . cmderRegScope = szArgList [ i + 1 ] ;
i + + ;
}
}
2018-09-03 11:06:39 -05:00
}
2019-05-19 22:54:47 +02:00
/* Used for passing arguments to conemu prog */
else if ( _wcsicmp ( L " /x " , szArgList [ i ] ) = = 0 )
2019-05-12 15:18:15 +02:00
{
2023-09-25 11:14:54 -04:00
cmderOptions . cmderTerminalArgs = szArgList [ i + 1 ] ;
2019-05-12 15:18:15 +02:00
i + + ;
}
2019-10-06 16:52:50 -04:00
/* Bare double dash, remaining commandline is for conemu */
else if ( _wcsicmp ( L " -- " , szArgList [ i ] ) = = 0 )
{
std : : wstring cmdline = std : : wstring ( GetCommandLineW ( ) ) ;
auto doubledash = cmdline . find ( L " -- " ) ;
if ( doubledash ! = std : : string : : npos )
{
2023-09-25 11:14:54 -04:00
cmderOptions . cmderTerminalArgs = cmdline . substr ( doubledash + 4 ) ;
2019-10-06 16:52:50 -04:00
}
break ;
}
2018-11-12 08:30:44 -05:00
else if ( cmderOptions . cmderStart = = L " " )
2018-03-13 10:38:27 -05:00
{
2018-11-12 08:30:44 -05:00
int len = wcslen ( szArgList [ i ] ) ;
if ( wcscmp ( & szArgList [ i ] [ len - 1 ] , L " \" " ) = = 0 )
{
szArgList [ i ] [ len - 1 ] = ' \0 ' ;
}
if ( PathFileExists ( szArgList [ i ] ) )
2018-03-13 10:38:27 -05:00
{
2018-11-12 08:30:44 -05:00
cmderOptions . cmderStart = szArgList [ i ] ;
2018-03-13 10:38:27 -05:00
i + + ;
}
2018-11-12 08:30:44 -05:00
else
{
cmderOptions . error = true ;
}
2018-09-03 11:06:39 -05:00
}
2018-11-08 21:25:42 -05:00
else
{
2018-11-12 08:30:44 -05:00
cmderOptions . error = true ;
2018-03-13 10:38:27 -05:00
}
}
2018-09-05 03:18:59 +04:30
2022-09-09 21:31:25 +04:30
}
2023-11-24 11:35:24 -05:00
if ( ! PathFileExists ( windowsTerminalDir ) & & ! PathFileExists ( conEmuDir ) )
{
cmderOptions . cmderTask = L " /k \" " ;
cmderOptions . cmderTask + = cmdInit ;
cmderOptions . cmderTask + = L " \" " ;
}
2022-09-09 21:31:25 +04:30
if ( cmderOptions . error = = true )
{
wchar_t validOptions [ 512 ] ;
HMODULE hMod = GetModuleHandle ( NULL ) ;
LoadString ( hMod , IDS_SWITCHES , validOptions , 512 ) ;
// display list of valid options on unrecognized parameter
TaskDialogOpen ( L " Unrecognized parameter. " , validOptions ) ;
2018-03-13 10:38:27 -05:00
}
LocalFree ( szArgList ) ;
return cmderOptions ;
}
2018-03-23 09:20:07 -05:00
2013-12-07 09:17:35 -05:00
int APIENTRY _tWinMain ( _In_ HINSTANCE hInstance ,
_In_opt_ HINSTANCE hPrevInstance ,
_In_ LPTSTR lpCmdLine ,
_In_ int nCmdShow )
{
UNREFERENCED_PARAMETER ( hPrevInstance ) ;
UNREFERENCED_PARAMETER ( lpCmdLine ) ;
UNREFERENCED_PARAMETER ( nCmdShow ) ;
2018-03-13 10:38:27 -05:00
cmderOptions cmderOptions = GetOption ( ) ;
2013-12-07 09:17:35 -05:00
2023-09-25 11:57:33 -04:00
wchar_t windowsTerminalDir [ MAX_PATH ] = { 0 } ;
2023-09-25 12:02:57 -04:00
wchar_t exeDir [ MAX_PATH ] = { 0 } ;
GetModuleFileName ( NULL , exeDir , sizeof ( exeDir ) ) ;
PathRemoveFileSpec ( exeDir ) ;
PathCombine ( windowsTerminalDir , exeDir , L " vendor \\ windows-terminal " ) ;
2023-09-25 11:57:33 -04:00
2018-11-08 21:25:42 -05:00
if ( cmderOptions . registerApp = = true )
{
2023-09-25 12:13:42 -04:00
if ( PathFileExists ( windowsTerminalDir ) )
{
2023-09-25 11:57:33 -04:00
RegisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_PATH_BACKGROUND , cmderOptions . cmderCfgRoot , cmderOptions . cmderSingle ) ;
RegisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_PATH_LISTITEM , cmderOptions . cmderCfgRoot , cmderOptions . cmderSingle ) ;
RegisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_DRIVE_PATH_BACKGROUND , cmderOptions . cmderCfgRoot , cmderOptions . cmderSingle ) ;
RegisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_DRIVE_PATH_LISTITEM , cmderOptions . cmderCfgRoot , cmderOptions . cmderSingle ) ;
2023-09-25 12:06:24 -04:00
}
2023-09-25 11:57:33 -04:00
else
{
RegisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_PATH_BACKGROUND , cmderOptions . cmderCfgRoot , false ) ;
RegisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_PATH_LISTITEM , cmderOptions . cmderCfgRoot , false ) ;
RegisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_DRIVE_PATH_BACKGROUND , cmderOptions . cmderCfgRoot , false ) ;
RegisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_DRIVE_PATH_LISTITEM , cmderOptions . cmderCfgRoot , false ) ;
}
2014-11-20 10:59:17 -05:00
}
2018-03-13 23:40:07 +01:00
else if ( cmderOptions . unRegisterApp = = true )
2014-11-20 10:59:17 -05:00
{
2018-03-13 10:38:27 -05:00
UnregisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_PATH_BACKGROUND ) ;
UnregisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_PATH_LISTITEM ) ;
2019-03-16 19:15:36 -04:00
UnregisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_DRIVE_PATH_BACKGROUND ) ;
UnregisterShellMenu ( cmderOptions . cmderRegScope , SHELL_MENU_REGISTRY_DRIVE_PATH_LISTITEM ) ;
2013-12-07 09:17:35 -05:00
}
2018-03-13 10:38:27 -05:00
else if ( cmderOptions . error = = true )
2017-06-17 22:29:06 +08:00
{
2018-03-13 10:38:27 -05:00
return 1 ;
2013-12-07 09:17:35 -05:00
}
else
{
2023-09-25 11:14:54 -04:00
StartCmder ( cmderOptions . cmderStart , cmderOptions . cmderSingle , cmderOptions . cmderTask , cmderOptions . cmderTitle , cmderOptions . cmderIcon , cmderOptions . cmderCfgRoot , cmderOptions . cmderUserCfg , cmderOptions . cmderTerminalArgs ) ;
2013-12-07 09:17:35 -05:00
}
2018-03-13 23:40:07 +01:00
2013-11-30 09:15:42 -05:00
return 0 ;
2014-01-21 18:25:24 +00:00
}