Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2020-08-04
16:09 • Closed ticket [efd15d181a]: problem with % in exec on windows plus 6 other... artifact: 0cd4bc9531 user: sebres
2020-07-29
14:51 • Open ticket [efd15d181a]. artifact: f4d341a143 user: bll
14:40 • Ticket [efd15d181a]: 3 changes artifact: 62f8d64656 user: bll
14:38 • Ticket [efd15d181a]: 3 changes artifact: aafc0b24cd user: bll
2020-07-28
07:28 • Ticket [efd15d181a]: 3 changes artifact: 7747bd6406 user: anonymous
01:52 • Ticket [efd15d181a]: 3 changes artifact: 9168b08841 user: bll
2020-07-27
08:25 • Ticket [efd15d181a]: 3 changes artifact: 9bc0bbb0da user: anonymous
2020-07-26
17:13 • Pending ticket [efd15d181a]. artifact: 4eb08d3a82 user: jan.nijtmans
2020-07-25
14:37 • New ticket [efd15d181a]. artifact: ab44091769 user: anonymous

problem with % in [exec] on windows

Closed / Duplicateefd15d181ae9be3b538eb23534d37d8f1d96cd57Created 6 years agoUpdated 6 years ago
Descriptionplain2020-07-25 14:37:56
Using percentage sign % in [exec] command or in [open] a command pipeline on windows is broken in recent version.

Tcl version: 8.6.4.1
Tcl command: exec cmd /c echo %programdata%
Command result: C:\ProgramData
-> Result is correct

Tcl version: 8.6.10.1
Tcl command: exec cmd /c echo %programdata%
Command result: ""%"programdata"%""
-> Result is wrong

I could not find a way how to pass-through a simple percentage sign unmasked to the underlaying windows operating system.
jan.nijtmanswiki2020-07-26 17:13:17

This changed starting with commit [86e0fd3ecf9a48af] for security reasons. You should be able to use:

exec cmd /c echo $env(programdata)

@sebres, do you want to comment more on this change, why it was done?

anonymousplain2020-07-27 08:25:14
What a really wanted to do:

catch {exec WMIC PROCESS WHERE "NAME!='WMIC.EXE' AND \
	COMMANDLINE LIKE '%MyJarFile.jar%-p 12345%'" \
	GET PROCESSID /FORMAT:VALUE} result

Percentage sign in Windows WMIC command stands for wildcard and not for Windows variable. Therefore using "env" does not help here.

How is it possible to pass-through plain percentage sign? Maybe by masking it somehow? Seems no good decision to inhibit that totally .
bllplain2020-07-28 01:52:51
Remember that Tcl will process the double quotes and in this instance you
want to pass them to the WMIC command.

For example, I have an exec in my program:

  set cmd "\"$::sysvars::v(prog.tasklist)\" /fi \"pid eq $ppid\" /nh 2>nul"
  exec -ignorestderr -- {*}$cmd 2>@1

as the quoted string: "pid eq <some-pid>" must be passed to windows with
the double quotes included.

Examples (these all work):

% close [open "my file" w]
% exec write "\"my file\""
% close [open abc%def w]
% exec write abc%def
% close [open %abcdef% w]
% exec write %abcdef%
%

So try your command with the double quote marks escaped.  This may take
a little work to figure out.  That's why I like to set the command line
to execute first, then exec it with exec {*}$cmd.  I can puts $cmd and
see exactly what will be passed to exec (since it is a list, you have to
remember that many words will be brace quoted).
anonymousplain2020-07-28 07:28:55
Not double quotation marks are making troubles but percentage signs!

Tcl version: 8.6.4.1
Tcl command:
exec wmic process where {commandline like '%wmic%'} get processid,commandline
Command output:
CommandLine                                                                                            ProcessId
C:\WINDOWS\System32\Wbem\wmic.exe process where "commandline like '%wmic%'" get processid,commandline  2884
-> Result is correct

Tcl version: 8.6.10.1
Tcl command:
exec wmic process where {commandline like '%wmic%'} get processid,commandline
Command output:
No instances available
-> Result is wrong

Command does not contain any double quotation mark to be escaped or not!
Either escaping percentage signs or single quotation marks results in wmic error message.
So how should wmic command now look like in order to get correct result as before? Any working solution would be appreciated.
bllplain2020-07-29 14:38:35
Testing on windows (win7/64) where tt.exe dumps the command line arguments it

bll@win7-64 MINGW64 ~
$ cat tt.tcl
set out [exec ./tt wmic process where {commandline like '%wmic%'} get processid,commandline]
puts $out
exit

bll@win7-64 MINGW64 ~
$ /c/Users/bll/Desktop/BallroomDJ/windows/64/tcl/bin/tclsh.exe tt.tcl
argc: 7
0: C:\msys64\home\bll\tt.exe
1: wmic
2: process
3: where
4: commandline like '%wmic%'
5: get
6: processid,commandline

Printing the constructed command line out in the Tcl source:

ds: C:\msys64\home\bll\tt.exe wmic process where "commandline like '"%"wmic"%"'" get processid,commandline

So something, somewhere is stripping out those quotation marks that get
added by Tcl.  

I really don't know what is stripping the quotation marks. The program
should receive them.

Obviously cmd.exe /c echo %programdata% does receive the quotation marks 
and displays them (as it should).

It looks broken to me.
bllplain2020-07-29 14:40:02
And the string in question is already quoted.  It doesn't need to be protected,
even if it was being passed to cmd.exe.
bllplain2020-07-29 14:51:54
I suspect that mingw's argument processing on windows handles all the quotation
marks for the user.  That would explain my C program.

I am reopening this ticket.  On Windows, the program should receive the arguments
as is, as on windows, the programs parse the arguments, not the shell.
sebreswiki2020-08-04 16:09:02

The main reason for the fix is described in [21b0629c81] (as well as self-descriptive in test cases). Shortly it is a certain insanity by the argument parsing in MS - in opposite to *nux the arguments are parsed by program so differently if called a program processor (comspec) and other executable. The current handling of %-char is not optimal (see further ticket [344b1a0c0f]) and I'll try to fix that, but this ticket is rather a duplicate, so I'll close it here.

As for the fix, the main issue is that parameter substitution in comspec is ambiguous, so proper quoting/escape is hardly possible and MS refuses to fix it due to compat reasons

Thus at the moment the fix may look like:

  • try to "escape" as safe as possible
  • raise exception "Unpaired quote found" ...
Whereas it must additionally distinguish between execution of comspec (shell), a batch-script and other executable. Let alone the escape using ^ char together with double/triple quotation (or using \") is impossible, the %-char is allowed as part of file name.

Closed as dup of [344b1a0c0f].

Log in (anonymous is fine) to add a comment.