windowsbatch-filecmdchdirdoskey

Sanitizing command parameters in a DOSKEY macro


Let's say I have the following in my AutoRun script:

@doskey cd = @( ^
    for /f usebackq^^ delims^^=^^ eol^^= %%a in (^
        '$* ' ^
    ) do @( ^
        if "%%~a"==" " ( ^
            if /i not "%%CD%%"=="%%USERPROFILE%%" ( ^
                chdir /d "%%USERPROFILE%%" ^&^& ^
                set "OLDPWD=%%CD%%" ^
            ) ^
        ) else if "%%~a"=="- " ( ^
            if /i not "%%CD%%"=="%%OLDPWD%%" ( ^
                chdir /d "%%OLDPWD%%" ^&^& ^
                set "OLDPWD=%%CD%%" ^
            ) ^
        ) else ( ^
            if /i not "%%CD%%"=="%%~a" ( ^
                chdir /d "%%~a" ^&^& ^
                set "OLDPWD=%%CD%%" ^
            ) ^
        ) ^
    ) ^
)

This is supposed to emulate the behaviour of POSIX cd in cmd.exe, and for the most part it works great:

C:\Users\user>cd C:\

C:\>cd

C:\Users\user>cd -

C:\>

However, all it takes is an incomplete pair of double quotes to make that house of cards fall:

C:\>cd "
2> was unexpected at this time.

Is there a way of sanitizing macro input inline so that it can handle misplaced quotes properly?

I've tried/thought of the following:


Solution

  • One simple way is to use the definition batch file for some code, too.
    The macro uses set args= at the end and call then a batch file for the main code.
    With this technique the code is also much more readable.

    @echo off
    REM *** Trampoline jump for function calls of the form ex. "C:\:function:\..\MyBatchFile.bat"
    FOR /F "tokens=3 delims=:" %%L in ("%~0") DO goto :%%L
    
    
    doskey cd=for %%# in ( 1 1 2) do @if %%#==2 ( "%~d0\:__cd:\..\%~pnx0" ) else set args=$*
    
    exit /b
    
    :__cd
    
    setlocal EnableDelayedExpansion
    
    for /F "delims=" %%a in (""!args!"") DO (
      endlocal
      if "%%~a"=="" (
          if /i not "%CD%"=="%USERPROFILE%" (
              chdir /d "%USERPROFILE%" && set "OLDPWD=%CD%"
          ) 
      ) else if "%%~a"=="-" (
           if /i not "%CD%"=="%OLDPWD%" (
                chdir /d "%OLDPWD%" && set "OLDPWD=%CD%"
           ) 
      ) else (
            if /i not "%CD%"=="%%~a" (
                chdir /d %%~a && set "OLDPWD=%CD%"
            )
      )
    )
    

    Doskey only:
    But if you insist to solve it only with a doskey macro, you could use disappearing carets.

    Line2 do the trick:
    FOR %%^^^^ in ("") do defines an empty %%^ FOR meta variable.

    This is used in for /f "delims=" %%a in (^^""$*%%~^^"^")
    The %%~^^ escapes the next quote, but only if $* contains unbalanced quotes, and after the expansion the %%~^^ disappears.
    The quote escaping solves the problem of unbalanced quotes in $*, because in the case of none or balanced quotes in $* it expands to a balanced quote string, but this works even for unbalanced quotes in $*

    doskey cd=( ^
        FOR %%^^^^ in ("") do @for /f "delims=" %%a in (^^""$*%%~^^"^") do @( ^
            if "%%~a"=="" ( ^
                if /i not "%%CD%%"=="%%USERPROFILE%%" ( ^
                    chdir /d "%%USERPROFILE%%" ^&^& ^
                    set "OLDPWD=%%CD%%" ^
                ) ^
            ) else if "%%~a"=="-" ( ^
                if /i not "%%CD%%"=="%%OLDPWD%%" ( ^
                    chdir /d "%%OLDPWD%%" ^&^& ^
                    set "OLDPWD=%%CD%%" ^
                ) ^
            ) else ( ^
                if /i not "%%CD%%"=="%%~a" ( ^
                    chdir /d %%~a  ^&^& ^
                    set "OLDPWD=%%CD%%" ^
                ) ^
            ) ^
        ) ^
    )
    

    Batch file only version:

    The great advantage of using a batch file, it can be used in batch files, too, but doskey macros can't.
    That's because doskey macros has to be typed in via keyboard, they can't be activated in any other way.
    But it can't be named cd.bat because cd.bat will only be called when the file is in the current directory, else the built in cd will be used

    cdX.bat (the file should be in the path)

    @echo off
    setlocal EnableDelayedExpansion
    
    for /F "delims=" %%a in (""!args!"") DO (
      endlocal
      if "%~1"=="" (
          if /i not "%CD%"=="%USERPROFILE%" (
              chdir /d "%USERPROFILE%" && set "OLDPWD=%CD%"
          ) 
      ) else if "%~1"=="-" (
           if /i not "%CD%"=="%OLDPWD%" (
                chdir /d "%OLDPWD%" && set "OLDPWD=%CD%"
           ) 
      ) else (
            if /i not "%CD%"=="%~1" (
                chdir /d "%~1" && set "OLDPWD=%CD%"
            )
      )
    )