[2.6.1] Bug/Question: Windows CMD line parsing

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.

Post a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: [2.6.1] Bug/Question: Windows CMD line parsing

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by Edward-san » Fri Jan 06, 2017 1:51 pm

I suspect it died because this discussion wasn't moved to a separated thread. :P

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by zalewa » Fri Jan 06, 2017 6:31 am

Why did this subject die? I verified edward-san's patch and it does indeed work. Is there any case where it won't? Like a CVAR that handles "" value differently than all the others?

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by Edward-san » Mon Nov 28, 2016 9:41 am

So "" is really converted to an empty argument. I still don't get why graf says the fix is hacky, since the string builder must somehow tell if there was an argument or not, even if it's empty.

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by zalewa » Mon Nov 28, 2016 7:05 am

Edward-san wrote:I'd like to know what's passed to the function BuildString in c_dispatch.cpp when the program is run with `zdoom -iwad doom2 +name ""` in Windows, in particular argv[1] (argc should be 2 when called).

Code: Select all

> zdoom -iwad doom2 +name ""

argc=2
argv[0]=+name
argv[1]= 

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by Edward-san » Mon Nov 28, 2016 5:06 am

I'd like to know what's passed to the function BuildString in c_dispatch.cpp when the program is run with `zdoom -iwad doom2 +name ""` in Windows, in particular argv[1] (argc should be 2 when called).

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by zalewa » Mon Nov 28, 2016 4:03 am

Edward-san wrote:Uh ... does WIndows pass "" as literally `""` in the args[] array? It would make my fix incomplete, of course ...
Yes. WinAPI has a function that allows you tokenize the string into a LPWSTR array. You just need to call CommandLineToArgvW() on the GetCommandLineW(). Here's the whole code:

Code: Select all

static QStringList tokenize(const QString &cmdLine)
{
	if (cmdLine.isEmpty())
	{
		// CommandLineToArgvW() returns path to current executable
		// if lpCmdLine argument is an empty string. We don't want that
		// here.
		return QStringList();
	}
	int numArgs = 0;
	LPCWSTR winapiCmdLine = (LPCWSTR)cmdLine.utf16();
	LPWSTR* winapiTokens = CommandLineToArgvW(winapiCmdLine, &numArgs);

	if (winapiTokens == NULL)
	{
		return QStringList();
	}

	QStringList result;
	for (int i = 0; i < numArgs; ++i)
	{
		// Conversion to "ushort*" seems to work for LPWSTR.
		result << QString::fromUtf16((const ushort*)winapiTokens[i]);
	}
	LocalFree(winapiTokens);
	return result;
}
Replace QString with whatever is used in ZDoom for Unicode-aware strings.

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by Edward-san » Mon Nov 28, 2016 3:33 am

But that makes "" work also on my Linux machine, because it has the same problem. :?

In my case, "" is interpreted as an empty argument, but it's still present in the args[] array! So doesn't it make sense to quote it so it's not lost?


[edit] Uh ... does WIndows pass "" as literally `""` in the args[] array? It would make my fix incomplete, of course ...

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by Graf Zahl » Sun Nov 27, 2016 5:31 pm

Shouldn't an empty argument be - well - EMPTY? This looks like a hack in the wrong place to me.

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by Edward-san » Sun Nov 27, 2016 5:28 pm

How about this PR?

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by zalewa » Sun Nov 27, 2016 4:36 pm

Gez wrote:I'm pretty sure that's not a ZDoom issue but a Windows issue. The parameters are given to the program by the OS.
Nope, Windows sends the command line to the program as-is, leaving it up to the program how to interpret it.
Spoiler: See these

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by Gez » Sun Nov 27, 2016 4:17 pm

I'm pretty sure that's not a ZDoom issue but a Windows issue. The parameters are given to the program by the OS.

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by zalewa » Sun Nov 27, 2016 3:54 pm

Sorry for thread necromancy, but I still need one more thing with this topic.

I just tested with commit 0488b18 and all the patterns posted above work just as they should (apart from "Example na\\\"me", which cuts the `"me` part, but that's pretty minor stuff).
However, I also need to be able to blank a cvar from a command line, which is something that eludes me.

When I start the game with:

Code: Select all

zdoom.exe -iwad doom2.wad +name "example name"
And then use 'name' CCMD I correctly get the game to say `"name" is "example name"`.

If I close the game and start it with this command line:

Code: Select all

zdoom.exe -iwad doom2.wad +name ""
I still get `"name" is "example name"`. I would like to get `"name" is ""` (a blank name).

ZDoom seems to ignore the argument when it's empty, or rather, if you inspect the launch log in the console, it treats an empty argument as an argumentless CCMD and types out `"name" is "example name"` in the launch log.

Would it be possible to have an option to blank cvars from the command line?

Re: [2.6.1] Bug/Question: Windows CMD line parsing

by randi » Thu Jan 10, 2013 9:22 pm

Fixed in r4025.

[2.6.1] Bug/Question: Windows CMD line parsing

by zalewa » Thu Aug 16, 2012 10:54 am

Hi. This post is related to the issue described here:
http://zandronum.com/tracker/view.php?id=973

The question is: how does the command line parsing work on Windows? How does it treat the quotation marks and is the current behavior proper? I've done some testing and here are the results:
  • The following command line works correctly. The in-game sv_hostname variable is properly set to 'ExampleServer':

    Code: Select all

    zdoom.exe -iwad doom2.wad +sv_hostname "ExampleServer"
    
  • This command line also works correctly. The in-game sv_hostname cvar is set to 'Example Server' as expected:

    Code: Select all

    zdoom.exe -iwad doom2.wad +sv_hostname "Example Server"
    
  • Here begins weird behavior. In WinAPI the CommandLineToArgvW(GetCommandLineW()) function combo translates an argument like this one to 'Examp"le Server'. However, in ZDoom the in-game cvar gets cut off and says 'Examp':

    Code: Select all

    zdoom.exe -iwad doom2.wad +sv_hostname "Examp\"le Server"
    
  • Now, this one works correctly. You insert a double backslash and a backslash followed by the quotation mark. I guess the game parses it to \" and this gets interpreted properly when being applied to the cvar. The in-game cvar now says 'Examp"le Server' as expected:

    Code: Select all

    zdoom.exe -iwad doom2.wad +sv_hostname "Examp\\\"le Server"
    
    However ...
  • ... when spacebar is removed then this messes up yet again. The result for the following command line is 'sv_hostname' being set to 'Examp\':

    Code: Select all

    zdoom.exe -iwad doom2.wad +sv_hostname "Examp\\\"leServer"
    
I'm trying to figure it out so Doomseeker can create proper command lines for users to copy&paste, but so far nothing works quite right. Am I doing something wrong or is there a bug with command line parsing?

Note: The problem seems to be platform independent. I did some checks with Zandronum on Linux and it pretty much behaves the same way there.

Top