Comment #0 by andrej.mitrovich — 2012-08-12T14:22:34Z
When interfacing with C/C++ libraries one common problem is having to extract argc/argv arguments from D main() and passing them back to library functions. A common solution is:
// Array of pointers to command line parameters.
char*[] argv = args.map!((a)=>(a.dup~'\0').ptr).array;
But it would be much simpler (and more efficient) if argc & argv were stored somewhere in druntime. There's a Runtime.args() function in core.runtime which exports the runtime args as a D-friendly array, so we could provide a cArgs equivalent which returns a struct with argc/argv.
druntime/src/rt/dmain2.d is where main is called from:
extern (C) int main(int argc, char** argv) { ... }
From there we could easily store argc+argv into a global struct variable, similar to how _d_args is exposed as a __gshared string[].
Comment #1 by andrej.mitrovich — 2012-10-02T16:53:34Z
I'd like to fix this myself. But I'm not sure how this should be exposed in the Runtime struct. Should it be two properties, 'argc' and 'argv', or a single property such as the following, in core.runtime:
extern (C) CArgs rt_cArgs();
static @property string[] cArgs()
{
return rt_cArgs();
}
and in src\rt\dmain2.d:
struct CArgs
{
int argc
char** argv;
}
__gshared CArgs _cArgs = null;
extern (C) string[] rt_cArgs()
{
return _cArgs;
}
extern (C) int main(int argc, char** argv)
{
_cArgs.argc = argc;
_cArgs.argv = argv;
...
}
Thoughts?
Comment #2 by andrej.mitrovich — 2012-10-02T16:54:34Z
(In reply to comment #1)
> I'd like to fix this myself. But I'm not sure how this should be exposed in the
> Runtime struct. Should it be two properties, 'argc' and 'argv', or a single
> property such as the following, in core.runtime:
>
> extern (C) CArgs rt_cArgs();
>
> static @property string[] cArgs()
> {
> return rt_cArgs();
> }
That should be:
> static @property CArgs cArgs()
and
> extern (C) CArgs rt_cArgs()
Obviously I'm just pseudocoding. :)
Comment #3 by alex — 2012-10-02T17:00:47Z
I suppose it wouldn't be a problem to have a cArgs property on the Runtime struct. I'd prefer returning a struct over two separate properties.
Comment #4 by github-bugzilla — 2012-10-23T13:56:59Z