Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle quoted paths #398

Merged
merged 2 commits into from
Oct 13, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions bin/alias.bat
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
@echo off

set ALIASES=%CMDER_ROOT%\config\aliases
setlocal
:: handle quotes within command definition, e.g. quoted long file names
set _x="%*"
set _x=%_x:"=%

if ["%*"] == [""] echo Use /? for help & echo. & goto :p_show
:: check command usage
if ["%_x%"] == [""] echo Use /? for help & echo. & goto :p_show
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably no need to replace %* with %_x% here because if ["%_x%"] == [""] then ["%*"] == [""] will be true as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason %_x% is used here instead of %* is because of the surrounding quotes. If you use if ["%*"] == [""] and try to run the following alias alias runapi=runas /netonly /user:domain\user "c:\Program Files (x86)\IIS Express\iisexpress.exe /site :WebAppName /trace:i" you will receive the error "Files was unexpected at this time", because the quotes surrounding %* get mismatched with the quotes around "c:\Program Files...".

if ["%1"] == ["/?"] goto:p_help
if ["%1"] == ["/reload"] goto:p_reload
:: /d flag for delete existing alias
Expand All @@ -13,20 +18,18 @@ if ["%2"] == [""] (
echo Insufficient parameters. & goto:p_help
)

::validate alias
setlocal
for /f "delims== tokens=1" %%G in ("%*") do set _temp2=%%G

set _temp=%_temp2: =%
:: validate alias
for /f "delims== tokens=1" %%G in ("%_x%") do set alias=%%G
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, since %_x% usage is not needed in arguments check (line 10) then it is better to move its definition to actual usage here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the %_x% is needed in the arguments check, the declaration must remain at the top of the file.

set _temp=%alias: =%

if not ["%_temp%"] == ["%_temp2%"] (
if not ["%_temp%"] == ["%alias%"] (
echo Your alias name can not contain a space
endlocal
goto:eof
)

:: replace already defined alias
findstr /b /v /i "%_temp%=" "%ALIASES%" >> "%ALIASES%.tmp"
findstr /b /v /i "%alias%=" "%ALIASES%" >> "%ALIASES%.tmp"
echo %* >> "%ALIASES%.tmp" && type "%ALIASES%.tmp" > "%ALIASES%" & @del /f /q "%ALIASES%.tmp"
doskey /macrofile="%ALIASES%"
endlocal
Expand Down