From: Greg Ercolano <erco@(email surpressed)>
Subject: Re: [Q+A] Handling of quoting in render commands
   Date: Wed, 11 May 2011 13:32:19 -0400
Msg# 2100
View Complete Thread (2 articles) | All Threads
Last Next
Greg Ercolano wrote:
> command   "//some/path with spaces/myrender.exe" "one two" "three four"
> [..]
>     ..then it would run on the farm as you'd expect, and with
>     argv[1] set to "one two",
>     argv[2] set to "three four"
> 
>     If you find you need to embed backslashes or quotes,
>     you can escape them with the backslash character, eg:
> 
> command "//some/path/myrender.exe" "This is a quote(\")" "This is a backslash(\\)"
> 
>     This behavior is handled by the WIN32 call CreateProcess(),
>     which is documented here (*):
>     http://msdn.microsoft.com/en-us/library/ms682425%28v=vs.85%29.aspx
  
   Actually, it seems to be more complicated than that.
   This complexity is not my fault! ;) It's how Microsoft works.

   CreateProcess() handles quotes around the command only,
   and the Microsoft C library handles quotes around arguments
   with its own separate rules for quoting and escaping.

   So in other words:

      "//some/path/myrender.exe" "This is a quote(\")" "This is a backslash(\\)"
      \________________________/ \_____________________________________________/
         CreateProcess()                     Microsoft C Library
         quoting rules.                        quoting rules.

   The CreateProcess() rules for quoting are relatively simple (*):
   http://msdn.microsoft.com/en-us/library/ms682425%28v=vs.85%29.aspx

   The Microsoft C Library quoting rules for quoting are more complex (**):
   http://msdn.microsoft.com/en-us/library/a1y7w461.aspx

   The CreateProcess() docs simply say double quotes can be used to protect
   spaces in the command, nothing about backslash quoting, or escaping quotes.

   The Microsoft C Library handles argument quoting at the time the
   command is invoked, and since just about everything you run is a
   C/C++ program (perl, python, cmd, maya, nuke, etc), those rules are
   followed for argument parsing.

   I'm quoting their docs verbatim here, as this directly affects
   how the Rush render command's arguments are interpreted on windows machines:

--- quote

    * Arguments are delimited by white space, which is either a space or a tab.

    * A string surrounded by double quotation marks is interpreted as a single
      argument, regardless of white space contained within. A quoted string can
      be embedded in an argument. Note that the caret (^) is not recognized as an
      escape character or delimiter.

    * A double quotation mark preceded by a backslash, \", is interpreted as a
      literal double quotation mark (").

    * Backslashes are interpreted literally, unless they immediately precede
      a double quotation mark.

    * If an even number of backslashes is followed by a double quotation mark,
      then one backslash (\) is placed in the argv array for every pair of
      backslashes (\\), and the double quotation mark (") is interpreted as a
      string delimiter.

    * If an odd number of backslashes is followed by a double quotation mark,
      then one backslash (\) is placed in the argv array for every pair of
      backslashes (\\) and the double quotation mark is interpreted as an
      escape sequence by the remaining backslash, causing a literal double
      quotation mark (") to be placed in argv.

--- snip

	My brain imploded somewhere on that fifth or sixth paragraph,
        but hopefully some of you can benefit from this. YMMV.

	If you can't get the above working, your best bet is to just
	empirically try escaping variations until it works.

        Or, /avoid/ the problem completely and don't specify complex info
	on the command line; throw your arguments into a file, and pass the
	file as the only argument to your render script, so it can load the
	file and get the data unmolested by the above parsing rules.


(*)  If the "CreateProcess" link goes stale, search MSDN for CreateProcess.

(**) If the "Microsoft C Library Parsing" link goes stale, search MSDN
     for "Parsing C Command-Line Arguments".


Last Next