0x00 前言
lib_path.bat分析学习是在init.bat的基础上进行调用的。
其他相关分析文章
cmder Cmder.bat分析学习
DIY自己的cmder.exe
cmder init.bat分析学习
0x01 正文
1. 1~10行
@echo off
call "%~dp0lib_base.cmd"
call "%%~dp0lib_console"
set lib_path=call "%~dp0lib_path.cmd"
if "%~1" == "/h" (
%lib_base% help "%~0"
) else if "%1" neq "" (
call :%*
)
exit /b
2.enhance_path
2.1 第一部分
if "%~1" neq "" (
set "add_path=%~1"
) else (
%lib_console% show_error "You must specify a directory to add to the path!"
exit 1
)
if "%~2" neq "" if /i "%~2" == "append" (
set "position=%~2"
) else (
set "position="
)
if "%fast_init%" == "1" (
if "%position%" == "append" (
set "PATH=%PATH%;%add_path%"
) else (
set "PATH=%add_path%;%PATH%"
)
)
set "PATH=%PATH:;;=;%"
if "%fast_init%" == "1" (
exit /b
)
2.2 第二部分
set found=0
set "find_query=%add_path%"
set "find_query=%find_query:\=\\%"
set "find_query=%find_query: =\ %"
if "%CMDER_CONFIGURED%" == "1" (
%lib_console% debug_output :enhance_path "Env Var - find_query=%find_query%"
echo "%path%"|%WINDIR%\System32\findstr >nul /I /R ";%find_query%\"$"
REM if "!ERRORLEVEL!" == "0" set found=1
call :set_found
)
%lib_console% debug_output :enhance_path "Env Var 1 - found=%found%"
2.3 第三部分
if "%found%" == "0" (
if "%CMDER_CONFIGURED%" == "1" (
echo "%path%"|%WINDIR%\System32\findstr >nul /i /r ";%find_query%;"
REM if "!ERRORLEVEL!" == "0" set found=1
call :set_found
)
%lib_console% debug_output :enhance_path "Env Var 2 - found=%found%"
)
if "%found%" == "0" (
%lib_console% debug_output :enhance_path "BEFORE Env Var - PATH=%path%"
if /i "%position%" == "append" (
%lib_console% debug_output :enhance_path "Appending '%add_path%'"
set "PATH=%PATH%;%add_path%"
) else (
%lib_console% debug_output :enhance_path "Prepending '%add_path%'"
set "PATH=%add_path%;%PATH%"
)
%lib_console% debug_output :enhance_path "AFTER Env Var - PATH=%path%"
)
rem :end_enhance_path
rem endlocal & set "PATH=%PATH:;;=;%"
set "PATH=%PATH:;;=;%"
exit /b
这里的部分其实感觉有一点重复了。
3.enhance_path_recursive
3.1 第一部分
if "%~1" neq "" (
set "add_path=%~1"
) else (
%lib_console% show_error "You must specify a directory to add to the path!"
exit 1
)
if "%~2" gtr "1" (
set "max_depth=%~2"
) else (
set "max_depth=1"
)
if "%~3" neq "" if /i "%~3" == "append" (
set "position=%~3"
) else (
set "position="
)
if "%fast_init%" == "1" (
call :enhance_path "%add_path%" %position%
)
set "PATH=%PATH:;;=;%"
if "%fast_init%" == "1" (
exit /b
)
3.2 第二部分
if "%depth%" == "" set depth=0
%lib_console% debug_output :enhance_path_recursive "Env Var - add_path=%add_path%"
%lib_console% debug_output :enhance_path_recursive "Env Var - position=%position%"
%lib_console% debug_output :enhance_path_recursive "Env Var - max_depth=%max_depth%"
if %max_depth% gtr %depth% (
%lib_console% debug_output :enhance_path_recursive "Adding parent directory - '%add_path%'"
call :enhance_path "%add_path%" %position%
REM set /a "depth=!depth!+1"
call :set_depth
call :loop_depth
)
rem :end_enhance_path_recursive
rem endlocal & set "PATH=%PATH%"
set "PATH=%PATH%"
exit /b
4.loop_depth
:loop_depth
for /d %%i in ("%add_path%\*") do (
%lib_console% debug_output :enhance_path_recursive "Env Var BEFORE - depth=%depth%"
%lib_console% debug_output :enhance_path_recursive "Found Subdirectory - '%%~fi'"
call :enhance_path_recursive "%%~fi" %max_depth% %position%
%lib_console% debug_output :enhance_path_recursive "Env Var AFTER- depth=%depth%"
)
exit /b
0x02 总结
这里的感觉是获取到了PATH,并且可以对整个文件夹进行PATH的获取。