 |
Changeset 2073
- Timestamp:
- 04/18/07 18:01:24
(2 years ago)
- Author:
- jcomellas
- Message:
The environment variables are now passed as an associative array to make
them compatible with tango.sys.Environment. Fixes ticket #408.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r1954 |
r2073 |
|
| 152 | 152 | static const uint DefaultStderrBufferSize = 512; |
|---|
| 153 | 153 | |
|---|
| 154 | | private char[][] _args; |
|---|
| 155 | | private char[][] _env; |
|---|
| 156 | | private char[] _workDir; |
|---|
| 157 | | private PipeConduit _stdin; |
|---|
| 158 | | private PipeConduit _stdout; |
|---|
| 159 | | private PipeConduit _stderr; |
|---|
| 160 | | private bool _running = false; |
|---|
| | 154 | private char[][] _args; |
|---|
| | 155 | private char[][char[]] _env; |
|---|
| | 156 | private char[] _workDir; |
|---|
| | 157 | private PipeConduit _stdin; |
|---|
| | 158 | private PipeConduit _stdout; |
|---|
| | 159 | private PipeConduit _stderr; |
|---|
| | 160 | private bool _running = false; |
|---|
| 161 | 161 | |
|---|
| 162 | 162 | version (Windows) |
|---|
| … | … | |
| 193 | 193 | * command = string with the process' command line; arguments that have |
|---|
| 194 | 194 | * embedded whitespace must be enclosed in inside double-quotes ("). |
|---|
| 195 | | * env = array of strings with the process' environment variables; |
|---|
| 196 | | * each element must follow the following format: |
|---|
| 197 | | * <NAME>=<VALUE> |
|---|
| | 195 | * env = associative array of strings with the process' environment |
|---|
| | 196 | * variables; the variable name must be the key of each entry. |
|---|
| 198 | 197 | * |
|---|
| 199 | 198 | * Examples: |
|---|
| 200 | 199 | * --- |
|---|
| 201 | | * char[] command = "myprogram \"first argument\" second third"; |
|---|
| 202 | | * char[][] env; |
|---|
| | 200 | * char[] command = "myprogram \"first argument\" second third"; |
|---|
| | 201 | * char[][char[]] env; |
|---|
| 203 | 202 | * |
|---|
| 204 | 203 | * // Environment variables |
|---|
| 205 | | * env ~= "MYVAR1=first"; |
|---|
| 206 | | * env ~= "MYVAR2=second"; |
|---|
| | 204 | * env["MYVAR1"] = "first"; |
|---|
| | 205 | * env["MYVAR2"] = "second"; |
|---|
| 207 | 206 | * |
|---|
| 208 | 207 | * auto p = new Process(command, env) |
|---|
| 209 | 208 | * --- |
|---|
| 210 | 209 | */ |
|---|
| 211 | | public this(char[] command, char[][] env) |
|---|
| | 210 | public this(char[] command, char[][char[]] env) |
|---|
| 212 | 211 | in |
|---|
| 213 | 212 | { |
|---|
| … | … | |
| 227 | 226 | * argument must be the process' name; the arguments can be |
|---|
| 228 | 227 | * empty. |
|---|
| 229 | | * env = array of strings with the process' environment variables; |
|---|
| 230 | | * each element must follow the following format: |
|---|
| 231 | | * <NAME>=<VALUE> |
|---|
| | 228 | * env = associative array of strings with the process' environment |
|---|
| | 229 | * variables; the variable name must be the key of each entry. |
|---|
| 232 | 230 | * |
|---|
| 233 | 231 | * Examples: |
|---|
| 234 | 232 | * --- |
|---|
| 235 | 233 | * char[][] args; |
|---|
| 236 | | * char[][] env; |
|---|
| | 234 | * char[][char[]] env; |
|---|
| 237 | 235 | * |
|---|
| 238 | 236 | * // Process name |
|---|
| … | … | |
| 244 | 242 | * |
|---|
| 245 | 243 | * // Environment variables |
|---|
| 246 | | * env ~= "MYVAR1=first"; |
|---|
| 247 | | * env ~= "MYVAR2=second"; |
|---|
| | 244 | * env["MYVAR1"] = "first"; |
|---|
| | 245 | * env["MYVAR2"] = "second"; |
|---|
| 248 | 246 | * |
|---|
| 249 | 247 | * auto p = new Process(args, env) |
|---|
| 250 | 248 | * --- |
|---|
| 251 | 249 | */ |
|---|
| 252 | | public this(char[][] args, char[][] env) |
|---|
| | 250 | public this(char[][] args, char[][char[]] env) |
|---|
| 253 | 251 | in |
|---|
| 254 | 252 | { |
|---|
| … | … | |
| 342 | 340 | |
|---|
| 343 | 341 | /** |
|---|
| 344 | | * Return an array with the process' environment variables. |
|---|
| 345 | | */ |
|---|
| 346 | | public char[][] env() |
|---|
| | 342 | * Return an associative array with the process' environment variables. |
|---|
| | 343 | */ |
|---|
| | 344 | public char[][char[]] env() |
|---|
| 347 | 345 | { |
|---|
| 348 | 346 | return _env; |
|---|
| … | … | |
| 350 | 348 | |
|---|
| 351 | 349 | /** |
|---|
| 352 | | * Set the process' environment variables from the arguments received by |
|---|
| 353 | | * the method. |
|---|
| | 350 | * Set the process' environment variables from the associative array |
|---|
| | 351 | * received by the method. |
|---|
| 354 | 352 | * |
|---|
| 355 | 353 | * Params: |
|---|
| 356 | | * env = array of string containing the environment variables for the |
|---|
| 357 | | * process. Each string must include the name and the value of each |
|---|
| 358 | | * variable in the following format: <name>=<value>. |
|---|
| | 354 | * env = associative array of strings containing the environment |
|---|
| | 355 | * variables for the process. The variable name should be the key |
|---|
| | 356 | * used for each entry. |
|---|
| 359 | 357 | * |
|---|
| 360 | 358 | * Examples: |
|---|
| 361 | 359 | * --- |
|---|
| 362 | | * p.env("VAR1=VALUE1", "VAR2=VALUE2"); |
|---|
| | 360 | * char[][char[]] env; |
|---|
| | 361 | * |
|---|
| | 362 | * env["MYVAR1"] = "first"; |
|---|
| | 363 | * env["MYVAR2"] = "second"; |
|---|
| | 364 | * |
|---|
| | 365 | * p.env = env; |
|---|
| 363 | 366 | * --- |
|---|
| 364 | 367 | */ |
|---|
| 365 | | public void env(char[][] env ...) |
|---|
| | 368 | public void env(char[][char[]] env) |
|---|
| 366 | 369 | { |
|---|
| 367 | 370 | _env = env; |
|---|
| … | … | |
| 517 | 520 | * command = string with the process' command line; arguments that have |
|---|
| 518 | 521 | * embedded whitespace must be enclosed in inside double-quotes ("). |
|---|
| 519 | | * env = array of strings with the process' environment variables; |
|---|
| 520 | | * each element must follow the following format: |
|---|
| 521 | | * <NAME>=<VALUE> |
|---|
| | 522 | * env = associative array of strings with the process' environment |
|---|
| | 523 | * variables; the variable name must be the key of each entry. |
|---|
| 522 | 524 | * |
|---|
| 523 | 525 | * Throws: |
|---|
| … | … | |
| 531 | 533 | * member, they will be replaced by the arguments supplied to the method. |
|---|
| 532 | 534 | */ |
|---|
| 533 | | public void execute(char[] command, char[][] env) |
|---|
| | 535 | public void execute(char[] command, char[][char[]] env) |
|---|
| 534 | 536 | in |
|---|
| 535 | 537 | { |
|---|
| … | … | |
| 557 | 559 | * argument must be the process' name; the arguments can be |
|---|
| 558 | 560 | * empty. |
|---|
| 559 | | * env = array of strings with the process' environment variables; |
|---|
| 560 | | * each element must follow the following format: |
|---|
| 561 | | * <NAME>=<VALUE> |
|---|
| | 561 | * env = associative array of strings with the process' environment |
|---|
| | 562 | * variables; the variable name must be the key of each entry. |
|---|
| 562 | 563 | * |
|---|
| 563 | 564 | * Throws: |
|---|
| … | … | |
| 582 | 583 | * --- |
|---|
| 583 | 584 | */ |
|---|
| 584 | | public void execute(char[][] args, char[][] env) |
|---|
| | 585 | public void execute(char[][] args, char[][char[]] env) |
|---|
| 585 | 586 | in |
|---|
| 586 | 587 | { |
|---|
| … | … | |
| 1243 | 1244 | { |
|---|
| 1244 | 1245 | /** |
|---|
| 1245 | | * Convert an array of strings to a buffer containing each string |
|---|
| 1246 | | * separated by a null character and an additional null character at |
|---|
| 1247 | | * the end of it. This is the format expected by the CreateProcess() |
|---|
| 1248 | | * Windows API. |
|---|
| | 1246 | * Convert an associative array of strings to a buffer containing a |
|---|
| | 1247 | * concatenation of "<name>=<value>" strings separated by a null |
|---|
| | 1248 | * character and with an additional null character at the end of it. |
|---|
| | 1249 | * This is the format expected by the CreateProcess() Windows API for |
|---|
| | 1250 | * the environment variables. |
|---|
| 1249 | 1251 | */ |
|---|
| 1250 | | protected static char[] toNullEndedBuffer(char[][] src) |
|---|
| 1251 | | { |
|---|
| 1252 | | char[] dest = null; |
|---|
| 1253 | | // Add space for a \0 after each string and a terminating \0 |
|---|
| 1254 | | size_t length = src.length + 1; |
|---|
| 1255 | | uint pos = 0; |
|---|
| 1256 | | |
|---|
| 1257 | | foreach(char[] str; src) |
|---|
| 1258 | | { |
|---|
| 1259 | | length += str.length; // total length of strings |
|---|
| 1260 | | } |
|---|
| 1261 | | |
|---|
| 1262 | | dest = new char[length]; |
|---|
| 1263 | | |
|---|
| 1264 | | foreach(char[] str; src) |
|---|
| 1265 | | { |
|---|
| 1266 | | dest[pos .. pos + str.length] = str; |
|---|
| 1267 | | pos += src.length; |
|---|
| 1268 | | dest[pos++] = '\0'; |
|---|
| | 1252 | protected static char[] toNullEndedBuffer(char[][char[]] src) |
|---|
| | 1253 | { |
|---|
| | 1254 | char[] dest; |
|---|
| | 1255 | |
|---|
| | 1256 | foreach (key, value; src) |
|---|
| | 1257 | { |
|---|
| | 1258 | (((dest ~= key) ~= '=') ~= value) ~ '\0'; |
|---|
| | 1259 | } |
|---|
| | 1260 | |
|---|
| | 1261 | if (dest.length > 0) |
|---|
| | 1262 | { |
|---|
| | 1263 | dest ~= '\0'; |
|---|
| 1269 | 1264 | } |
|---|
| 1270 | 1265 | |
|---|
| … | … | |
| 1281 | 1276 | */ |
|---|
| 1282 | 1277 | protected static char*[] toNullEndedArray(char[][] src) |
|---|
| 1283 | | out (result) |
|---|
| 1284 | | { |
|---|
| 1285 | | if (result !is null) |
|---|
| 1286 | | { |
|---|
| 1287 | | int i = result.length - 1; |
|---|
| 1288 | | |
|---|
| 1289 | | // Verify that the returned array has the format expected |
|---|
| 1290 | | // by execv() and execve(). |
|---|
| 1291 | | assert(result.length == src.length + 1); |
|---|
| 1292 | | assert(result[i] == null); |
|---|
| 1293 | | |
|---|
| 1294 | | while (--i >= 0) |
|---|
| 1295 | | { |
|---|
| 1296 | | assert(result[i] !is null); |
|---|
| 1297 | | assert(*(result[i] + src[i].length) == '\0'); |
|---|
| 1298 | | } |
|---|
| 1299 | | } |
|---|
| 1300 | | } |
|---|
| 1301 | | body |
|---|
| 1302 | 1278 | { |
|---|
| 1303 | 1279 | if (src !is null) |
|---|
| … | … | |
| 1320 | 1296 | return null; |
|---|
| 1321 | 1297 | } |
|---|
| | 1298 | } |
|---|
| | 1299 | |
|---|
| | 1300 | /** |
|---|
| | 1301 | * Convert an associative array of strings to an array of pointers to |
|---|
| | 1302 | * char with a terminating null character (C strings). The resulting |
|---|
| | 1303 | * array has a null pointer at the end. This is the format expected by |
|---|
| | 1304 | * the execv*() family of POSIX functions for environment variables. |
|---|
| | 1305 | */ |
|---|
| | 1306 | protected static char*[] toNullEndedArray(char[][char[]] src) |
|---|
| | 1307 | { |
|---|
| | 1308 | char*[] dest; |
|---|
| | 1309 | |
|---|
| | 1310 | foreach (key, value; src) |
|---|
| | 1311 | { |
|---|
| | 1312 | dest ~= (key ~ '=' ~ value ~ '\0').ptr; |
|---|
| | 1313 | } |
|---|
| | 1314 | |
|---|
| | 1315 | if (dest.length > 0) |
|---|
| | 1316 | { |
|---|
| | 1317 | dest ~= null; |
|---|
| | 1318 | } |
|---|
| | 1319 | return dest; |
|---|
| 1322 | 1320 | } |
|---|
| 1323 | 1321 | |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic