Skip to content

Commit 71e023f

Browse files
committed
Convert global.argv0 to a D slice
This is more of a proof of concept for future conversion. Here's a few principles used to do the conversion from `T*` to `T[]`: - If it's a string, the character at `s[$]` must be `'\0'`. It allows code to freely go back and forth between `char*` and `char[]`. - Move the implementation to extern(D) and slices, and provide C++ wrapper. - Stay on topic: those changes only focus on one parameter / module / function, because it's very easy to end up with a giant change that doesn't work. - If `'\0'` termination is required (e.g. calling a C function), changing from `char*` to `char[]` should can be done using `toCStringThen`.
1 parent dcba06e commit 71e023f

7 files changed

Lines changed: 17 additions & 21 deletions

File tree

src/dmd/dinifile.d

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import dmd.root.filename;
2424
import dmd.root.outbuffer;
2525
import dmd.root.port;
2626
import dmd.root.stringtable;
27+
import dmd.utils;
2728

2829
version (Windows) extern (C) int putenv(const char*);
2930
private enum LOG = false;
@@ -37,11 +38,12 @@ private enum LOG = false;
3738
* file path of the config file or NULL
3839
* Note: this is a memory leak
3940
*/
40-
const(char)* findConfFile(const(char)* argv0, const(char)* inifile)
41+
const(char)[] findConfFile(const(char)[] argv0, const(char)[] inifile)
4142
{
4243
static if (LOG)
4344
{
44-
printf("findinifile(argv0 = '%s', inifile = '%s')\n", argv0, inifile);
45+
printf("findinifile(argv0 = '%.*s', inifile = '%.*s')\n",
46+
argv0.length, argv0.ptr, inifile.length, inifile.ptr);
4547
}
4648
if (FileName.absolute(inifile))
4749
return inifile;
@@ -54,16 +56,16 @@ const(char)* findConfFile(const(char)* argv0, const(char)* inifile)
5456
* o directory off of argv0
5557
* o SYSCONFDIR=/etc (non-windows)
5658
*/
57-
auto filename = FileName.combine(getenv("HOME"), inifile);
59+
auto filename = FileName.combine(getenv("HOME").toDString, inifile);
5860
if (FileName.exists(filename))
5961
return filename;
6062
version (Windows)
6163
{
6264
// This fix by Tim Matthews
6365
char[MAX_PATH + 1] resolved_name;
64-
if (GetModuleFileNameA(null, resolved_name.ptr, MAX_PATH + 1) && FileName.exists(resolved_name.ptr))
66+
if (GetModuleFileNameA(null, resolved_name.ptr, MAX_PATH + 1) && FileName.exists(resolved_name))
6567
{
66-
filename = FileName.replaceName(resolved_name.ptr, inifile);
68+
filename = FileName.replaceName(resolved_name, inifile);
6769
if (FileName.exists(filename))
6870
return filename;
6971
}
@@ -74,13 +76,13 @@ const(char)* findConfFile(const(char)* argv0, const(char)* inifile)
7476
version (Posix)
7577
{
7678
// Search PATH for argv0
77-
auto p = getenv("PATH");
79+
const p = getenv("PATH");
7880
static if (LOG)
7981
{
8082
printf("\tPATH='%s'\n", p);
8183
}
8284
auto paths = FileName.splitPath(p);
83-
auto abspath = FileName.searchPath(paths, argv0, false);
85+
auto abspath = FileName.searchPath(paths, argv0.ptr, false).toDString;
8486
if (abspath)
8587
{
8688
auto absname = FileName.replaceName(abspath, inifile);

src/dmd/frontend.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ string findDMDConfig(string dmdFilePath)
7979
import dmd.dinifile : findConfFile;
8080
import std.string : fromStringz, toStringz;
8181

82-
auto f = findConfFile(dmdFilePath.toStringz, "dmd.conf");
82+
auto f = findConfFile(dmdFilePath.toStringz[0 .. dmdFilePath.length], "dmd.conf");
8383
if (f is null)
8484
return null;
8585

src/dmd/globals.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ struct Param
173173

174174
uint errorLimit = 20;
175175

176-
const(char)* argv0; // program name
176+
const(char)[] argv0; // program name
177177
Array!(const(char)*)* modFileAliasStrings; // array of char*'s of -I module filename alias strings
178178
Array!(const(char)*)* imppath; // array of char*'s of where to look for import modules
179179
Array!(const(char)*)* fileImppath; // array of char*'s of where to look for file import modules

src/dmd/globals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ struct Param
145145

146146
unsigned errorLimit;
147147

148-
const char *argv0; // program name
148+
DArray<const char> argv0; // program name
149149
Array<const char *> *modFileAliasStrings; // array of char*'s of -I module filename alias strings
150150
Array<const char *> *imppath; // array of char*'s of where to look for import modules
151151
Array<const char *> *fileImppath; // array of char*'s of where to look for file import modules

src/dmd/json.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ public:
922922
{
923923
objectStart();
924924
requiredProperty("cwd", getcwd(null, 0).toDString);
925-
requiredProperty("argv0", global.params.argv0.toDString);
925+
requiredProperty("argv0", global.params.argv0);
926926
requiredProperty("config", global.inifilename.toDString);
927927
requiredProperty("libName", global.params.libname.toDString);
928928

src/dmd/mars.d

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private int tryMain(size_t argc, const(char)** argv)
218218
//for (size_t i = 0; i < arguments.dim; ++i) printf("arguments[%d] = '%s'\n", i, arguments[i]);
219219
files.reserve(arguments.dim - 1);
220220
// Set default values
221-
global.params.argv0 = arguments[0];
221+
global.params.argv0 = arguments[0].toDString;
222222

223223
// Temporary: Use 32 bits as the default on Windows, for config parsing
224224
static if (TARGET.Windows)
@@ -235,11 +235,11 @@ private int tryMain(size_t argc, const(char)** argv)
235235
{
236236
version (Windows)
237237
{
238-
global.inifilename = findConfFile(global.params.argv0, "sc.ini");
238+
global.inifilename = findConfFile(global.params.argv0, "sc.ini").ptr;
239239
}
240240
else version (Posix)
241241
{
242-
global.inifilename = findConfFile(global.params.argv0, "dmd.conf");
242+
global.inifilename = findConfFile(global.params.argv0, "dmd.conf").ptr;
243243
}
244244
else
245245
{
@@ -1415,7 +1415,7 @@ private void printPredefinedVersions(FILE* stream)
14151415

14161416
extern(C) void printGlobalConfigs(FILE* stream)
14171417
{
1418-
stream.fprintf("binary %s\n", global.params.argv0);
1418+
stream.fprintf("binary %*s\n", global.params.argv0.length, global.params.argv0.ptr);
14191419
stream.fprintf("version %s\n", global._version);
14201420
stream.fprintf("config %s\n", global.inifilename ? global.inifilename : "(none)");
14211421
// Print DFLAGS environment variable

src/dmd/root/filename.d

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,6 @@ nothrow:
300300
/**************************************
301301
* Replace filename portion of path.
302302
*/
303-
extern (C++) static const(char)* replaceName(const(char)* path, const(char)* name)
304-
{
305-
return replaceName(path[0 .. strlen(path)], name[0 .. strlen(name)]).ptr;
306-
}
307-
308-
/// Ditto
309303
extern (D) static const(char)[] replaceName(const(char)[] path, const(char)[] name)
310304
{
311305
if (absolute(name))

0 commit comments

Comments
 (0)