147 points · by nikbackm · 9y ago · 41 comments
http://www.windowsinspired.com/how-a-windows-programs-splits...
"She said "you cant do this", didnt she?"
Parses the same way in Bash: $ for A in "She said "you can\'t do this\!", didn't she?"; do echo -$A-; done
-She said you-
-can't-
-do-
-this!, didn't she?-
So I'd have thought this would be a given. It doesn't look so unintuitive – though I'm not sure if the results are the same for the same reasons.So I went to take a look at the Bash source code. Which is (expectedly) pretty hairy. Top-of-stack quoting characters are referenced through-out, to enable a `pass_next_character` which seems to me similar conceptually to `ignore_special_chars`.
I understand not many people write a program suitable as a top level shell (for use in /etc/passwd) but for completeness...
The cases when it comes to parameter parsing on Linux and Unix operating systems are things like:
* A POSIX-conformant shell parsing a command line, in a shell script or entered interactively, into an argument vector.
* systemd parsing a string in ExecStart= in one of systemd's service definitions into an argument vector.
* Things that parse .desktop files, as given in the article.
* execlineb or nosh parsing a file into an argument vector.
- http://skarnet.org/software/execline/grammar.html
- http://jdebp.eu./Softwares/nosh/guide/nosh.html
* init parsing single fields of records in /etc/ttys or /etc/inittab into argument vectors.
* cron programs parsing fields in crontab records into argument vectors.
and so forth. These are all subtly or blatantly different, and the headlined article has glossed over this to a degree.
I suppose most people don't talk to /bin/login any more unless they ssh someplace.
Except now it's clear it was a premature optimisation, if it takes 10 pages to document the parsing behaviour, which probably also cost millions of wasted developer hours fixing weird corner cases.
Maybe at that time they thought, "well programmers can just call GetCommandLine() to get the args in a consistent way".
https://en.wikipedia.org/wiki/Zero_page_(CP/M)
I don't think it's an optimisation, but a design choice of simplicity; in fact, CP/M (and DOS) would parse the first two arguments if present, and put the results into the filename fields of the two default FCBs, allowing programs which take two filename arguments (traditionally, an input and an output) to be implemented easily without having any commandline parsing logic of their own. More info on that here:
http://www.gaby.de/cpm/manuals/archive/cpm22htm/ch5.htm
It would be interesting to trace the origins of command-line parameter passing design even further, to the mainframe OSes that came before UNIX, but I'm not so familiar with them.
Edit: OpenVMS appears to also pass a plain string to the process, which is then parsed within: http://hoffmanlabs.org/vmsfaq/vmsfaq_015.html (section 10.3)
It would be safe to use
printf("arg[%d] = %s", i, argv[i]);But seriously, that's an accident waiting to happen. Like a less knowledgeable user trying to delete a file named *
And there are some really awful things you can do by abusing shell expansion. Like this example (WARNING: DON'T BLINDLY RUN THESE!).
Go to a directory with non-important files, and do:
touch ./-f
touch ./-r
rm *
Since there's a good chance that all of your files have sensible names (that is, they don't start with weird symbols), that will expand to: rm -f -r [the rest of your files]
Oops. Kiss your files AND directories goodbye. That includes write protected files that would give rise to a warning if you tried to remove them. And in a cruel ironic twist, the files named '-r' and '-f' are preserved.But the problem is actually made worse by the fact that most command line parsing libraries allow flags to be condensed into a single option (like -r -f can be condensed into -rf).
GNU utilities do this by default. This coupled with expanding * turns ANY file that starts with a leading - into a potential source of doom.
So, imagine that you have a file named '-truffle' in there somewhere. rm * expands to 'rm -truffle [your files]'. That is interpreted by rm as "rm -t -r -u -f -f -l -e [your files]" (note the presence of '-r' and '-f').
Gasp and horror!
However, your salvation is that rm halts if it encounters an unknown option (like -u). You can wipe your brow and sigh in relief, because you just dodged a bullet.
IIRC, the Unix haters handbook dedicates a whole chapter to these types of landmines, and the "funny" thing is that most of it still applies, about two decades after it was written.
The biggest downside of the Windows way, though, is that there stops being one answer to "how do I pass arbitrary strings on the command line to another process"?
Sure, most programs use CommandLineToArgvW but it's also hard to find information on how a particular program parses its command line.
https://blogs.msdn.microsoft.com/twistylittlepassagesallalik... is an overview of the fun that is command line handling on Windows.
Hypothetically that is not strictly true. You could create a shell that would be a very thin wrapper around CreateProcess and not do any interpretation or parsing of the childs arguments. In unixy shell must do at minimum some degree of parsing to split the arguments.
I've been writing my own Linux $SHELL and one of the features is that asterisks don't get expanded by the shell to avoid accidental bugs from the situations you described.
rm $(glob *.bak) rm @{g *.bak}
`g` returns a JSON array of file names and the @{} tells the shell to expand the array into parameters.This way you don't need to worry about spaces in file names or other problems with escaping.
What are the significant differences?
Is "flexibility", for lack of a better word, really a desirable property for entering command line arguments?
Should parsing, cf. entering, arguments be simple or complex?
Are there better ways to pass arguments/parameters than on the commandline?
Besides config files, which themselves may introduce parsing complexities.
Consider passing "arguments" as environmental variables, e.g., as in daemontools, envdir. Variables can be read from "files" in a chroot directory.
- command line options override environment variables, which in turn override config files (meaning applied to all three, for the same config setting).
I thought this was a good idea and can lead to flexibility, though also some complexity.
I think this might be a good scenario for the use of those three methods of configuration:
- config file settings for common values for options, or values that rarely change. So you set them to those values and change them rarely, when you need to make the new values permanent for a while.
- env. var settings for say, a session of work - in that session you want to override the config file value, and use it for (most of) the duration of your work session
- command-line options to override either of the previous two, say for just a command invocation or two (setting will automatically then revert to either of the two previous methods's values).
Interested to know if others have noticed this pattern (or think it is one) and any comments, since as I said, I don't remember seeing it written down anywhere.
Reading environment variables from process memory is less expensive (requiring only memory access) than reading files, which requires at least three syscalls (open, read, close) and thus multiple context switches, and, worst of all, disk access (unless you're putting the files in a tmpfs).
> Are there better ways to pass arguments/parameters than on the commandline?
If such a system were designed today, data would probably be strongly typed (passed as structured objects) rather than stringly typed (passed as text). In fact, to my understanding, this is what Powershell does. I think that the conceptual simplicity of using free-form text is a virtue for Unix more than a burden, but that's also an argument of taste.
As does every program that reads from a config file.
One does not have to use files to set the variables of course. This just works well for long-running-programs, e.g. daemons.
"... unless you're putting files in a tmpfs)"
Always files are in mfs.
As for strongly typed data and passing data instead of text, I prefer k to Powershell. There is also a uniformity to APL built-in functions. The number of arguments is limited. PS is quite slow on startup and too verbose.
As for "free-form" sometimes rearranging arguments (as glibc is capable of) can cause more complexity than is warranted.
Consider a program like signify. Then consider gpg.
The best command line program "interfaces" IMO are the ones the fewest options and least possible variability in arguments. Best interface is no interface, etc.
One of the obvious benefits for UNIX programs of this nature is portability.
Flipping this around, why does the 'command line' have to be a string (or a list of strings) that my program has to parse, rather than a ready-to-be-used data structure? Consider instead an alternative universe where the command-line arguments to your program are represented with something like a JSON object. Or better, a struct that the command-line interpreter can type-check.
Bash command line splitting alone is nasty, let alone the 6 other shells. Every language under the sun has a dozen and twelve libraries to parse argv[] into something useful.
If you document every nook, cranny and special case of those libraries, the list for unix will dwarf the special cases of windows. In fact, I think bash command line parsing alone can dwarf the windows section, according to the man page.
Where as on Linux / UNIX, parameters arrive at the application already split. Your point about the shells is a good one because at least on Linux you only need to learn the nuances of your preferred shell (though in my experience all the main ones seem to follow the same rules reliably) knowing that you wont have any weird issues for example with quotes being included or not included in argv[] in some applications but not others despite formatting the parameters in exactly the same way.
From a personal point of view, I do find working with parameters in Windows a highly frustrating affair when compared with Linux. However I appreciate the issue is down to inheritance rather than design and Microsoft cannot easily change something like this without breaking a thousand things in subtle and often unexpected ways.