X-Recipient: archive-cygwin@delorie.com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:reply-to:subject:to:references:from:message-id
	:date:mime-version:in-reply-to:content-type
	:content-transfer-encoding; q=dns; s=default; b=gF1ETJHx61rKrZ3W
	rohz6dfr+cYIdd2ftEO9/VTjvY9sr2TmwVmlo/Zf7nhUfZG3S0zgyyb5KcTNTLcG
	Um+xpUPkZo73DF+0JdoDj6o/yeg9sJuFopcwnEvVSymSP4uZcmzvSJJWJR3h9XjB
	RPFZgufyrEdW2OY6KJdteZ4dw40=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:reply-to:subject:to:references:from:message-id
	:date:mime-version:in-reply-to:content-type
	:content-transfer-encoding; s=default; bh=DlI89qaelc84e+H2A0m7HC
	OEUUc=; b=I7ZoYy0AMYhRfphSYHxQQjsHLiRtBIhqFrTBfCTN1P84yF6daMI7b3
	2O2vDyrTp8b3VgEk2toJp5IqSPW0H0wCowwnoTfr8y0itRDVV937iKA86F+tFQT3
	U878KLOlL6Bxy9Izhk2+H5WzqEvAxf71VP29aWp1iJ1D3Pr00Twp8=
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Authentication-Results: sourceware.org; auth=none
X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=inherit, reproduction, Gos, shell32.dll
X-HELO: smtp-out-no.shaw.ca
Reply-To: Brian.Inglis@systematicsw.ab.ca
Subject: Re: Command line processing in dcrt0.cc does not match Microsoft parsing rules
To: "cygwin@cygwin.com" <cygwin@cygwin.com>
References: <MWHPR21MB08452919F35B1B0C5F0EB4DCB9BD0@MWHPR21MB0845.namprd21.prod.outlook.com> <MWHPR21MB0845F78385792965A94E0CD9B9BD0@MWHPR21MB0845.namprd21.prod.outlook.com> <MWHPR21MB084508155AB621C7AD81309CB9B90@MWHPR21MB0845.namprd21.prod.outlook.com> <MWHPR21MB08456D9F03AF8BD450E6AB2EB9B80@MWHPR21MB0845.namprd21.prod.outlook.com>
From: Brian Inglis <Brian.Inglis@SystematicSw.ab.ca>
Openpgp: preference=signencrypt
Message-ID: <43ba61e8-a3d6-d811-7c86-787da9240a41@SystematicSw.ab.ca>
Date: Wed, 4 Sep 2019 23:29:48 -0600
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0
MIME-Version: 1.0
In-Reply-To: <MWHPR21MB08456D9F03AF8BD450E6AB2EB9B80@MWHPR21MB0845.namprd21.prod.outlook.com>
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
X-IsSubscribed: yes

On 2019-09-04 17:46, Stephen Provine wrote:
> On 2019-09-04 10:20, Brian Inglis wrote:
>> and ask if you really expect anyone else to use or reproduce this insanity,
>> rather than a sane POSIX parser?
> 
> I know it's insanity, but it's insanity that almost all Windows programs inherit and
> implement consistently enough because they use standard libraries or functions
> to do the parsing. The Go command line parser used to use CommandLineToArgvW
> and only switched away from it due to performance (it's in shell32.dll and that takes
> a long time to load). I don't know how accurate their manual reproduction is, but
> they seemed to study the sources I sent pretty carefully.
> 
> Anyway, my specific problem is that I have Go code with an array of arguments that
> I want to pass verbatim (no glob expansion) to a bash script. I've figured out how to
> override Go's default code for building the command line string, but it's not clear how
> to correctly construct the command line string. If the POSIX rules are being followed,
> I'd expect the following to work:
> 
>     bash.exe script.sh arg1 "*" arg3
> 
> But it always expands the "*" to all the files in the current directory. I've also tried \* and
> '*', but same problem. So how do I build a command line string that takes each argument
> literally with no processing?

As standard on Unix systems, just add another level of quoting for each level of
interpretation, as bash will process that command line, then bash will process
the script command line.

How are you running the command line; I get the same results under cmd or
mintty/bash:

$ bash -nvx script.sh arg1 "*" arg3
#!/bin/bash
# script.sh - echo args

argc=$#
argv=("$0" "$@")
echo argc $argc argv[0] "${argv[0]}"

for ((a = 1; a <= $argc; ++a))
do
    echo argv[$a] "${argv[$a]}"
done

C:\ > bash script.sh arg1 "*" arg3
argc 3 argv[0] script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

C:\ > bash -c 'script.sh arg1 "*" arg3'
argc 3 argv[0] /mnt/c/Users/bwi/bin/script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

$ bash script.sh arg1 "*" arg3
argc 3 argv[0] script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

$ bash -c 'script.sh arg1 "*" arg3'
argc 3 argv[0] /home/bwi/bin/script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

$ cmd /c bash script.sh arg1 "\*" arg3
argc 3 argv[0] script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

$ cmd /c bash -c 'script.sh arg1 "*" arg3'
argc 3 argv[0] /mnt/c/Users/bwi/bin/script.sh
argv[1] arg1
argv[2] *
argv[3] arg3

but with un-double-quoted (and backslash escaped) * I get a list of the current
directory files from all of these commands.

Invoking bash with options -vx or set -vx in script.sh will let you see what is
happening on stderr. Many errors cause non-interactive shell scripts to exit, so
check for child process error return codes (often 128+errno). If you are not
careful within script.sh, many unquoted uses of $2 may expand the *. Double
quotes allow command and parameter substitution, and history expansion, but
suppress pathname expansion. You should refer to each parameter within script.sh
as "$1" "$2" "$3", or you might need to quote some or each argument character
and enclose the * in double quotes e.g. \""\*"\" to pass thru the Go command
line interface.
Can you not tell the interface to verbatim passthru the string for execution?

You may check any of the POSIX shell, dash/ash/sh shell, ksh Korn shell, or bash
shell man pages or docs for more details on variations between shells and
extensions to POSIX operation.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

