Unable to pass batch file arguments unaltered

Aug 23, 2010
647
9
I have this script as a wrapper for Cygwin processes:

Code:
@SET PROMPT=@$S$P$_
@ECHO OFF
ON BREAK REM

CALL "%~dp0fix-path.btm"

SET ENV=%@PATH[%@FULL["%~0"]]env.exe
SET CMDNAME=%@EXECSTR[cygpath.exe -u "%~1"]
SHIFT

SET LANG=%@EXECSTR[locale -uU]
"%[ENV]" -- "%[CMDNAME]" "%$"
QUIT %?

Now, the problem is that it chokes when I pass it arguments enclosed in braces,
like your typical
Code:
git for-each-ref --format %(refname) %(objectname) --sort -committerdate
passage.

Is there a way to send arguments unaltered to the child process?
 

samintz

Scott Mintz
May 20, 2008
1,511
18
Solon, OH, USA
Is the problem the %(objectname) et. al.?
One option is to disable environment variable expansion using SETDOS /X. Another is to double up the %'s.
 
Aug 23, 2010
647
9
Yes, it is. Even with %@, the %(...) is executed before the program is called.
And the resulting command line contains
Code:
git for-each-ref --format 2 2 --sort -committerdate
where "2" looks like your usual "not found" return code. I appreciate the ability, but is it documented?
 
Aug 23, 2010
647
9
`SETDOS /X-4` doesn't work for the purpose.
TCC: Unknown command "refname"
TCC: Unknown command "objectname"
`SETDOS /X-3` breaks the script completely.
TCC: …\bin\cygwrap.btm [16] Unknown command "%[ENV]"

How to replace % with %% in an arbitrary string, if that string is evaluated long before I can make substitution?
 

samintz

Scott Mintz
May 20, 2008
1,511
18
Solon, OH, USA
I would think setdos /x-4 is what you want. However, I'm not clear on how what you posted gets called and when.

If your command is
Code:
git for-each-ref --format %(refname) %(objectname) --sort -committerdate
Then just double the %'s right there.
Code:
git for-each-ref --format %%(refname) %%(objectname) --sort -committerdate
 
Aug 23, 2010
647
9
My command is `git-wrapper.sh whatever arguments the caller decide to add`.
The association for ".sh" files is
Code:
$ assoc .sh
.sh=unixshell.script

$ ftype unixshell.script
unixshell.script="C:\Program Files\JPSoft\TCC-RT\TCC.EXE" /B /C "C:\Cygwin64\bin\cygwrap.btm" "%1" %*
 

samintz

Scott Mintz
May 20, 2008
1,511
18
Solon, OH, USA
I am not sure how you are invoking your scripts. I wrote a test framework:
Code:
assoc .foo=test.script
ftype test.script=c:\tc25\tcc.exe /b /c c:\tc25\echotest.btm %%*
I created echotest.btm:
Code:
@ECHO ON
setlocal
SET PROMPT=@$S$P$_
ON BREAK REM

rem CALL "%~dp0fix-path.btm"

SET ENV=%@PATH[%@FULL["%~0"]]env.exe
SET CMDNAME=%@EXECSTR[cygpath.exe -u "%~1"]
SHIFT

SET LANG=%@EXECSTR[locale -uU]
setdos /x-4
echo "%[ENV]" -- "%[CMDNAME]" "%$"
setdos /x0
QUIT %?
And then created test.foo which is basically empty. And invoked it:
Code:
test.foo git for-each-ref --format %%%%(refname) %%%%(objectname) --sort -committerdate
And got this output:
Code:
setlocal
SET PROMPT=@$S$P$_
ON BREAK REM
rem CALL "%~dp0fix-path.btm"
SET ENV=C:\tc25\env.exe
SET CMDNAME=test.foo
"C:\tc25\env.exe" -- "test.foo" "git for-each-ref --format %(refname) %(objectname) --sort -committerdate"
 

samintz

Scott Mintz
May 20, 2008
1,511
18
Solon, OH, USA
I think the arguments pass through the parser 2 or 3 times, and each time a % gets removed. Which is why I had to specify 4 %'s.

It might be easier to do a replace of some other symbol with %'s. e.g. #(refname) --> %(refname)
 
Aug 23, 2010
647
9
You had to write them 4 times because you passed the arguments from one command interpreter to another.
The fold before passing arguments to script was expected, the second, however, was not.
 
Aug 23, 2010
647
9
What quoting, can you please provide an example?
Just, before you do, I want to once again stress the fact I have no control over invocation of the script itself.
I can only work with what was passed to it.
 

rconn

Administrator
Staff member
May 14, 2008
12,404
152
What quoting, can you please provide an example?
Just, before you do, I want to once again stress the fact I have no control over invocation of the script itself.
I can only work with what was passed to it.

You didn't specify previously that you don't have control over invocation of the script.

SETDOS /X-4 won't help, as it only affects nested variables -- and you're not using nested variables.

To clarify, somebody else is passing a command (like "git for-each-ref --format %(refname) %(objectname) --sort -committerdate") to your script, and you need to pass the %(var) variables as-is?

If you have control over the command line, you'd just back quote the line to prevent all variable expansion.

If you don't, and you're trying to handle a command that's already been parsed *before* it's passed to the batch file -- that's more complicated. There are several issues:

1) The parentheses are not valid variable name characters;
2) So you need to turn off variable expansion, but if you do,
3) you'll disable the variable function expansion.

You could try turning on CMD-style variables (i.e., %var%) when you're parsing the input; then TCC won't think that "%(refname)" should be processed as a variable.
 
Similar threads
Thread starter Title Forum Replies Date
N Unable to bind multiple keys to the same key mapping directive Support 9
Eric Peters New Computer, unable to register Support 3
A Unable to register TC23 Support 1
D Unable to uninstall TCC 21 Support 1
G How to? Unable to install TCMD 21 under Win2012R2 Support 5
K Unable to register TakeCommand Support 4
W Unable to register TCMD v21 Support 16
dcantor Unable to access help Support 9
WavSlave Fixed Unable to sort files by description in TCC v19 Support 2
nickles WAD del unable to delete files ending in "." Support 18
R Unable to start any BTM file from Explorer Support 5
M I'm unable to use @replace[] with a % character in string2 Support 12
Jay Sage Unable to Specify INI File Support 19
thedave WAD Unable to type various characters using Windows 8's onscreen keyboard Support 7
Dan Glynhampton Fixed @version "force" flag not working, and unable to set separator Support 3
D ftps gives "Unable to build data connection" Support 5
M Why am I unable to delete files? Support 8
Y Unable to register v13. Support 1
Y Unable to close v13 Support 26
N Unable to end tcc.exe process Support 1
Y Unable to install 10.00.50 Support 3
D Can alias pass parameters to for command? Support 3
vefatica PSHELL: How pass arguments to script? Support 15
M Backquoted parameters used in GoSub don't pass string as a single parameter Support 2
D How to? Pass OpenHere Folder to CMD? Support 6
samintz How to? Pass a structure to @WINAPI Support 1
Mordachai How to pass URL to sendmail? Support 10
A How to? Pass output from ffind to another command in a BTM file Support 5
J How to pass a character like " " to a function Support 3
Emilio III Control characters pass through? Support 6
fpefpe How to? shortcuts and batch files Support 4
vefatica `Back quotes` - command line vs. batch file Support 5
fpefpe How to? batch file size Support 2
FreezerBurnt Help making a CMD and TCC compatible batch file Support 7
M Oracle batch OPatch.bat.bat not working in TCC but correctly in CMD Support 5
Joe Caverly Set a batch variable (%1 - %n) when TCC is executing a batch file Support 5
P Kill all other instances of tcc from a btm batch file Support 2
fpefpe batch file ending early Support 18
R Batch Debugger Command Expansion Window Support 3
K robocopy cancelling batch file processing Support 28
K Batch debugger not stepping correctly Support 4
S incorrect message at start Cancel batch job 4START.bat ? (Y/N/A) : Support 1
MickeyF Why does this batch file give me 'unknown command "else"' error? Support 17
Joe Caverly "Functions" in cmd.exe batch files Support 6
S Batch files Calling Batch Files Support 2
P exit /b in batch files Support 7
P Labels in batch files: TCC vs CMD.exe Support 9
vefatica Batch parameters broken? Support 2
cxxl Set encoding for batch file Support 4
dcantor How to? Batch file to be executed by TCSTART Support 4

Similar threads