|
| 1 | +module dmd.env; |
| 2 | + |
| 3 | +import core.stdc.string; |
| 4 | +import core.sys.posix.stdlib; |
| 5 | +import dmd.globals; |
| 6 | +import dmd.root.array; |
| 7 | +import dmd.root.rmem; |
| 8 | +import dmd.utils; |
| 9 | + |
| 10 | +version (Windows) |
| 11 | + private extern (C) int putenv(const char*) nothrow; |
| 12 | + |
| 13 | +/** |
| 14 | +Construct a variable from `name` and `value` and put it in the environment while saving |
| 15 | +the current value of the environment variable. |
| 16 | +Returns: |
| 17 | + 0 on success, non-zero on failure |
| 18 | +*/ |
| 19 | +int putenvRestorable(const(char)[] name, const(char)[] value) nothrow |
| 20 | +{ |
| 21 | + auto var = LocalEnvVar.xmalloc(name, value); |
| 22 | + const result = var.putenvRestorable(); |
| 23 | + var.xfree(result); |
| 24 | + return result; |
| 25 | +} |
| 26 | + |
| 27 | +/// Holds a `VAR=value` string that can be put into the global environment. |
| 28 | +struct LocalEnvVar |
| 29 | +{ |
| 30 | + string nameValueCStr; // The argument passed to `putenv`. |
| 31 | + private size_t equalsIndex; // index of '=' in nameValueCStr. |
| 32 | + |
| 33 | + /// The name of the variable |
| 34 | + auto name() const { return nameValueCStr[0 .. equalsIndex]; } |
| 35 | + |
| 36 | + /// The value of the variable |
| 37 | + auto value() const { return nameValueCStr[equalsIndex + 1 .. $]; } |
| 38 | + |
| 39 | + /** |
| 40 | + Put this variable in the environment while saving the current value of the |
| 41 | + environment variable. |
| 42 | + Returns: |
| 43 | + 0 on success, non-zero on failure |
| 44 | + */ |
| 45 | + int putenvRestorable() const nothrow |
| 46 | + { |
| 47 | + RestorableEnv.save(name); |
| 48 | + return .putenv(cast(char*)nameValueCStr.ptr); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + Allocate a new variable via xmalloc that can be promoted to the global environment. |
| 53 | + Params: |
| 54 | + name = name of the variable |
| 55 | + value = value of the variable |
| 56 | + Returns: |
| 57 | + a newly allocated variable that can be promoted to the global environment |
| 58 | + */ |
| 59 | + static LocalEnvVar xmalloc(const(char)[] name, const(char)[] value) nothrow |
| 60 | + { |
| 61 | + const length = name.length + 1 + value.length; |
| 62 | + auto str = (cast(char*)mem.xmalloc(length + 1))[0 .. length]; |
| 63 | + str[0 .. name.length] = name[]; |
| 64 | + str[name.length] = '='; |
| 65 | + str[name.length + 1 .. length] = value[]; |
| 66 | + str.ptr[length] = '\0'; |
| 67 | + return LocalEnvVar(cast(string)str, name.length); |
| 68 | + } |
| 69 | + |
| 70 | + private void xfree(int putenvResult) const nothrow |
| 71 | + { |
| 72 | + bool doFree; |
| 73 | + version (Windows) |
| 74 | + doFree = true; |
| 75 | + else |
| 76 | + { |
| 77 | + // on posix, when putenv succeeds ownership of the memory is transferred |
| 78 | + // to the global environment |
| 79 | + doFree = (putenvResult != 0); |
| 80 | + } |
| 81 | + if (doFree) |
| 82 | + mem.xfree(cast(void*)nameValueCStr.ptr); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/// Provides save/restore functionality for environment variables. |
| 87 | +struct RestorableEnv |
| 88 | +{ |
| 89 | + /// Holds the original values of environment variables when they are overwritten. |
| 90 | + private __gshared Array!LocalEnvVar originalVars; |
| 91 | + |
| 92 | + /// Restore the original environment. |
| 93 | + static void restore() |
| 94 | + { |
| 95 | + foreach (var; originalVars) |
| 96 | + { |
| 97 | + if (0 != putenv(cast(char*)var.nameValueCStr)) |
| 98 | + assert(0, "putenv failed"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// Save the environment variable `name` if not saved already. |
| 103 | + static void save(const(char)[] name) nothrow |
| 104 | + { |
| 105 | + foreach (var; originalVars) |
| 106 | + { |
| 107 | + if (name == var.name) |
| 108 | + return; // already saved |
| 109 | + } |
| 110 | + originalVars.push(LocalEnvVar.xmalloc(name, |
| 111 | + name.toCStringThen!(n => getenv(n.ptr)).toDString)); |
| 112 | + } |
| 113 | +} |
0 commit comments