1

I'm trying to write a kind of redirect script to stabilize the clang-format executable used by Visual studio over the different projects that I have.

For example:

  • Project 1: Has .clang-format based on LLVM 5.0.0
  • Project 2: Has .clang-format based on LLVM 8.0.0

So instead of using the default clang-format I'm trying to use a clang-format.cmd that based on the project has been using.

My current approach:

@echo OFF
:checkFormat
IF EXIST "clang-format.exe" ( 
   call clang-format.exe %* <&0 >&1
) ELSE (
   REM Loop the dir tree until the root the of the disk
   if "%cd:~3,1%"=="" (
      exit -1
   )
   cd ..
   goto checkFormat
)

However, whenever this script gets called from Visual Studio, I get an error:

An error occurred while formatting with ClangFormat The pipe has been ended.

I've tried replacing the call with another cmd that prints something to file and it doesn't seem to be to called. However, I did already add similar file creation before the 'call', which confirms it is being reached and in the directory that I expect.

How do I verify what the content is of STDIN? How do I redirect this stdin to the actual executable?

EDIT When running from within a CMD-prompt, it gives following error:

The handle could not be duplicated during redirection of handle 0.

Community
  • 1
  • 1
JVApen
  • 10,085
  • 3
  • 26
  • 56
  • The proper command to provide comment within a batch file is `Rem` not `::`, whilst `::` can work, it is problematic within a parenthesised block, please change it to `Rem Loop the dir tree until the root the of the disk`. – Compo Apr 27 '19 at 11:10
  • @Compo can you link to a more elaborate explanation on why this is? – JVApen Apr 27 '19 at 11:14
  • @JVApen `::` is a broken label. It is not a standard `rem`ark method, inside the code block it will cause some issues most of the time. – Gerhard Apr 27 '19 at 11:22
  • 1
    Yes @JVApen, you can start with [this one](https://stackoverflow.com/a/12407934). – Compo Apr 27 '19 at 11:23
  • Adapted, tested, no change – JVApen Apr 27 '19 at 11:25
  • 1
    what is the reason for `call`ing the executable? you also say you are calling it from visual studio, does the same error occur when you run the script manually? Lastly, why this aproach? why not search for the exe and run it instead? – Gerhard Apr 27 '19 at 11:31
  • Why are you climbing down the tree, one step at a time? Do you not know the locations of the different versions of `clang-format.exe`? – Compo Apr 27 '19 at 11:32
  • Visual studio launches it in the directory of the file. I know it's in the root of the project, however, I don't know where this root is. As all is in subversion, I don't even have an absolute path. – JVApen Apr 27 '19 at 11:36
  • @GerhardBarnard I've actually forgot to run it from the CMD directly. Gives `The handle could not be duplicated during redirection of handle 0.` The `call` is just trying things out, might indeed not be OK. I've have to give VS an 'executable' in the configuration which gets called on format by VS – JVApen Apr 27 '19 at 11:39
  • 3
    Remove ` &1` they are useless and wrong – jeb Apr 27 '19 at 11:44
  • Removing the `call` and the `&1` seem to have solved the problem. Thanks for all the effort! – JVApen Apr 27 '19 at 11:48
  • @jeb.. how about posting an answer? :) – Gerhard Apr 27 '19 at 12:02

1 Answers1

1

Remove the part <&0 >&1, because the constructs are wrong.

>&1 will be interpreted as

append handle 1 (STDOUT) to handle 1 (STDOUT)

That can't work.

The <0& will be interpreted nearly the same way

append handle 0 (STDIN) to handle 0 (STDIN)

jeb
  • 70,992
  • 15
  • 159
  • 202