(no subject) [entries|reading|network|archive]
simont

[ userinfo | dreamwidth userinfo ]
[ archive | journal archive ]

Sun 2002-08-04 18:34

Ick. Yuck. Bleargh. And things like that.. Just when you thought Windows had recently made some steps in the direction of being a halfway plausible operating environment, you turn over a metaphorical rock and find something like this under it, and realise just how far it's still got to go.

I want to give the PuTTY tools a lot more command-line options, and also standardise most of those options across all the various tools. Unfortunately, some of the tools are console applications (to which Windows provides argc and argv) and some are GUI applications (which get a single command-line string); so in order to use the same argument-processing code I'll clearly have to split up the command line of a GUI app into argc and argv. And it seemed obvious to me that it would be good to do this the same way that Windows console apps do it, so there would be no nasty surprises in moving a piece of command line from one tool to another.

So I sat down with a test rig and started investigating just what the rules are which Windows uses to split a command line into argument words. It honours double quotes and shows no awareness at all of single quotes; that's fair enough. A backslash can be used to escape a double quote; that's fair enough too. Backslashes can be used to escape each other as well, but only before a double quote - the rest of the time they behave like non-special characters. Slightly nasty, but fair enough given that they're the Windows path separator and really can't be messed about with too much.

Then I noticed that the string "a""b" produced the result a"b. Erk, I thought, they're supporting the BASIC approach of doubling quotes to escape them as well as the C approach of using a backslash. And then I thought, hang on, what happens if you have some backslashes before more than one quote? So I tried it, and seeing one or two oddities I then tried increasing the number of quotes past two to see what would happen; and the answer, when I finally worked it out, almost blew my brain out of my ears in its sheer outstanding ghastliness.

It turns out that a sequence of consecutive double quotes has a mod three behaviour, not the mod two that you might expect! Inside a quoted section, a quote character ends the quoted section and generates no text. If a subsequent quote appears immediately after doing that, it will generate a literal quote and not affect the current "are we in a quoted section" state; and to start another quoted section after that you need a third quote character. Backslashes, it turns out, are the simple and predictable end of the mechanism: a string of backslashes before a double quote will escape each other in pairs and if there's one left over it will escape the (first) double quote after it. But a long string of N consecutive double quotes will produce roughly N/3 literal quote characters, and will leave you in a quoted segment or not depending on the value of N mod 3.

You can't make this stuff up. If I'd tried to come up with an egregiously unpleasant quoting scheme, I'd probably have managed something in which backslashes changed their behaviour depending on context; and I might have thought of shoehorning the BASIC quote-doubling behaviour and the C backslash-escaping behaviour into the same mechanism at the same time; but there is no way, not even in my most perverse moods, that I would ever have come up with anything remotely as horrid as what is actually present in Windows.

Still; now I've written my own implementation of this thing which I hesitate to dignify as an "algorithm", so I get to hide it in the depths of the PuTTY code and with any luck never look at it again. Now I shall go and soothe my traumatised brain by eating a very big steak.

LinkReply
[identity profile] rowan-leigh.livejournal.comSun 2002-08-04 14:17
Ouch.

It's testament to your good writing skills that I was able to follow that at all. You definitely deserve the steak. :-)
Link Reply to this | Thread
[personal profile] simontMon 2002-08-05 01:15
Why thank you :-)

Not sure what useful tasks that particular writing skill qualifies me for. I'd say "writing Windows manuals", but I suspect that that (even if it wasn't a job I'd conscientiously object to) would probably require the ability to specify this sort of grotty behaviour in a way that doesn't emphasise how far away it is from anything remotely sane! Or more likely the ability to carefully avoid specifying it at all...
Link Reply to this | Parent
navigation
[ go | Previous Entry | Next Entry ]
[ add | to Memories ]