Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 2073

Show
Ignore:
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
  • trunk/tango/sys/Process.d

    r1954 r2073  
    152152    static const uint DefaultStderrBufferSize   = 512; 
    153153 
    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; 
    161161 
    162162    version (Windows) 
     
    193193     * command  = string with the process' command line; arguments that have 
    194194     *            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. 
    198197     * 
    199198     * Examples: 
    200199     * --- 
    201      * char[]   command = "myprogram \"first argument\" second third"; 
    202      * char[][] env; 
     200     * char[] command = "myprogram \"first argument\" second third"; 
     201     * char[][char[]] env; 
    203202     * 
    204203     * // Environment variables 
    205      * env ~= "MYVAR1=first"; 
    206      * env ~= "MYVAR2=second"; 
     204     * env["MYVAR1"] = "first"; 
     205     * env["MYVAR2"] = "second"; 
    207206     * 
    208207     * auto p = new Process(command, env) 
    209208     * --- 
    210209     */ 
    211     public this(char[] command, char[][] env) 
     210    public this(char[] command, char[][char[]] env) 
    212211    in 
    213212    { 
     
    227226     *            argument must be the process' name; the arguments can be 
    228227     *            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. 
    232230     * 
    233231     * Examples: 
    234232     * --- 
    235233     * char[][] args; 
    236      * char[][] env; 
     234     * char[][char[]] env; 
    237235     * 
    238236     * // Process name 
     
    244242     * 
    245243     * // Environment variables 
    246      * env ~= "MYVAR1=first"; 
    247      * env ~= "MYVAR2=second"; 
     244     * env["MYVAR1"] = "first"; 
     245     * env["MYVAR2"] = "second"; 
    248246     * 
    249247     * auto p = new Process(args, env) 
    250248     * --- 
    251249     */ 
    252     public this(char[][] args, char[][] env) 
     250    public this(char[][] args, char[][char[]] env) 
    253251    in 
    254252    { 
     
    342340 
    343341    /** 
    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() 
    347345    { 
    348346        return _env; 
     
    350348 
    351349    /** 
    352      * Set the process' environment variables from the arguments received b
    353      * the method. 
     350     * Set the process' environment variables from the associative arra
     351     * received by the method. 
    354352     * 
    355353     * 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
    359357     * 
    360358     * Examples: 
    361359     * --- 
    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; 
    363366     * --- 
    364367     */ 
    365     public void env(char[][] env ...
     368    public void env(char[][char[]] env
    366369    { 
    367370        _env = env; 
     
    517520     * command  = string with the process' command line; arguments that have 
    518521     *            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. 
    522524     * 
    523525     * Throws: 
     
    531533     * member, they will be replaced by the arguments supplied to the method. 
    532534     */ 
    533     public void execute(char[] command, char[][] env) 
     535    public void execute(char[] command, char[][char[]] env) 
    534536    in 
    535537    { 
     
    557559     *            argument must be the process' name; the arguments can be 
    558560     *            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. 
    562563     * 
    563564     * Throws: 
     
    582583     * --- 
    583584     */ 
    584     public void execute(char[][] args, char[][] env) 
     585    public void execute(char[][] args, char[][char[]] env) 
    585586    in 
    586587    { 
     
    12431244    { 
    12441245        /** 
    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. 
    12491251         */ 
    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'; 
    12691264            } 
    12701265 
     
    12811276         */ 
    12821277        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 
    13021278        { 
    13031279            if (src !is null) 
     
    13201296                return null; 
    13211297            } 
     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; 
    13221320        } 
    13231321