[2.6.1] Bug/Question: Windows CMD line parsing

Bugs that have been investigated and resolved somehow.

Moderator: GZDoom Developers

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 Reply
zalewa
Posts: 14
Joined: Sun Dec 03, 2006 5:50 pm

[2.6.1] Bug/Question: Windows CMD line parsing

Post by zalewa »

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.
User avatar
randi
Site Admin
Posts: 7749
Joined: Wed Jul 09, 2003 10:30 pm
Contact:

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

Post by randi »

Fixed in r4025.
zalewa
Posts: 14
Joined: Sun Dec 03, 2006 5:50 pm

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

Post by zalewa »

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?
Gez
 
 
Posts: 17938
Joined: Fri Jul 06, 2007 3:22 pm

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

Post by Gez »

I'm pretty sure that's not a ZDoom issue but a Windows issue. The parameters are given to the program by the OS.
zalewa
Posts: 14
Joined: Sun Dec 03, 2006 5:50 pm

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

Post by zalewa »

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
Edward-san
Posts: 1774
Joined: Sat Oct 17, 2009 9:40 am

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

Post by Edward-san »

How about this PR?
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49223
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

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

Post by Graf Zahl »

Shouldn't an empty argument be - well - EMPTY? This looks like a hack in the wrong place to me.
Edward-san
Posts: 1774
Joined: Sat Oct 17, 2009 9:40 am

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

Post by Edward-san »

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 ...
zalewa
Posts: 14
Joined: Sun Dec 03, 2006 5:50 pm

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

Post by zalewa »

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.
Edward-san
Posts: 1774
Joined: Sat Oct 17, 2009 9:40 am

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

Post by Edward-san »

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).
zalewa
Posts: 14
Joined: Sun Dec 03, 2006 5:50 pm

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

Post by zalewa »

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]= 
Edward-san
Posts: 1774
Joined: Sat Oct 17, 2009 9:40 am

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

Post by Edward-san »

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.
zalewa
Posts: 14
Joined: Sun Dec 03, 2006 5:50 pm

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

Post by zalewa »

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?
Edward-san
Posts: 1774
Joined: Sat Oct 17, 2009 9:40 am

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

Post by Edward-san »

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

Return to “Closed Bugs [GZDoom]”