[self-interest] Difference in command line argument order in Linux and OS X

Russell Allen mail at russell-allen.com
Sun Feb 2 01:26:54 UTC 2014


OK, to partially answer my own question:

GNU getopt, which is used on Linux, changes the order of argv as a side effect: 

http://man7.org/linux/man-pages/man3/getopt.3.html

"By default, getopt() permutes the contents of argv as it scans, so that eventually all the nonoptions are at the end."

OS X uses a BSD getopt which is sane.

As Wikipedia says ( https://en.wikipedia.org/wiki/Getopt ) :

"getopt is a system dependent function. The implementation of getopt in GNU C Library does permute the contents of the argument vector as it scans, so that eventually all the non-option arguments are at the end. On the contrary, the implementation of getopt in BSD C Library does not permute the argument vector and returns -1 if it encounters a non-option argument."

The Self VM uses getopt to parse the VM options, then passes the arguments onto the Self world via argv. So on OSX it is normal argv, on Linux it is argv as altered by getopt:

In shell.cpp:

  OS::opterr= 0; // disable printed warning about unrecognized options
  while(opt_i= OS::optind,
        (c = OS::getopt(argc, (char* const*)argv, "Ff:hl:prtcs:woa")) != (char)-1) {
    if (strlen(argv[opt_i]) != 2) continue; // ignore multi-character options
    switch(c) {
    case 'F':
	etc ...

  // save remaining args for Self-level access
  prog_argc= argc;
  prog_argv= argv;

So my next question is, what is the best way to fix this so that _CommandLine returns the arguments in proper order on Linux?

Russell

On 1 Feb 2014, at 9:55 pm, Russell Allen <mail at russell-allen.com> wrote:

> Just in case anyone has any ideas, I get this in OS X as expected:
> 
> 
> Macintosh-2:mio russellallen$ Self -port 4000 -headless
> Self Virtual Machine Version 4.1.13, Thu 09 Jan 14 15:06:29 Mac OS X i386 (4.4-272-g885323f-dirty)
> Copyright 1989-2003: The Self Group (type _Credits for credits)
> 
> for I386:  LogVMMessages = true
> for I386:  PrintScriptName  = true
> for I386:  Inline = true
> for I386:  SICDeferUncommonBranches = false (not implemented)
> for I386:  SICReplaceOnStack = false (not implemented)
> for I386:  SaveOutgoingArgumentsOfPatchedFrames = true
> VM# _CommandLine
> <0>: ( | parent* = <1>. | object array: {'/Applications/Self Control.app/Contents/Resources/Self.app/Contents/MacOS/Self', '-port', '4000', '-headless'} )
> 
> but I get this in Linux (Fedora 19):
> 
> [self at messaging ~]$ ./selfvm -port 4000 -headless
> Self Virtual Machine Version 4.1.13, Sat 01 Feb 14 08:00:32 Linux i386 (4.5.0-7-gb833000)
> Copyright 1989-2003: The Self Group (type _Credits for credits)
> 
> for I386:  LogVMMessages = true
> for I386:  PrintScriptName  = true
> for I386:  Inline = true
> for I386:  SICDeferUncommonBranches = false (not implemented)
> for I386:  SICReplaceOnStack = false (not implemented)
> for I386:  SaveOutgoingArgumentsOfPatchedFrames = true
> VM# _CommandLine
> <0>: ( | parent* = <1>. | object array: {'./selfvm', '-port', '-headless', '4000'} )
> 
> Why on earth would the order of the command line arguments vary between platforms? Or more precisely, why is Linux reordering my arguments?
> 
> Russell
> 
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.selflanguage.org/pipermail/self-interest/attachments/20140202/e8f9f96b/attachment.html>


More information about the Self-interest mailing list