cmdriver/src/CMDriver.cxx
//---------------------------------------------------------------------------------------------
// Expands all environment variables in the given command line specification.
// Function will modify source buffer. We suppose that this buffer has enough space.
//
// Returns:  0 - all right, source buffer contains processed command line;
//          !0 - error, source buffer contains error description.
//---------------------------------------------------------------------------------------------
Expands all environment variables in the given command line specification.
Function will modify source buffer. We suppose that this buffer has enough space.

all right, source buffer contains processed command line;
0 - error, source buffer contains error description.
Expands all environment variables in the given command line specification . Function will modify source buffer . We suppose that this buffer has enough space . all right source buffer contains processed command line ; 0 - error source buffer contains error description .
int expandEnvVariables(char *commandLine) {
    int len = strlen(commandLine);
    int count = 0;
    for(int i = 0; i < len; i++) {
        if(commandLine[i] == '$') {
            count++;
        }
    }
    if(count == 0) {
        return 0;
    }
    int tmp_sz = len + count * 1024;
    char *expanded = new char [tmp_sz];
    memset(expanded, 0, tmp_sz);
    char var_name[1024];
    char *var_value = NULL;
    char *prev = commandLine;
    char *ptr = NULL;
    int retCode = 0;
    while(prev != NULL) {
        ptr = strchr(prev, '$');
        if(ptr == NULL) {
            strcat(expanded, prev);
            break;
        } else {
            if(ptr[1] == '{') {
                char *close_brace = strchr(ptr + 2, '}');
                if(close_brace == NULL) {
                    strcpy(expanded,
                           "Command line specification contains reference " \
                           "to environment variable, which starts from '${', " \
                           "but doesn't contain closing curly brace '}'.");
                    retCode = -1;
                    break;
                }
                tmp_sz = close_brace - ptr - 2;
                strncpy(var_name, ptr + 2, tmp_sz);
                var_name[tmp_sz] = 0;
                var_value = getenv(var_name);
                if(var_value == NULL) {
                    sprintf(expanded,
                            "Command line specification contains reference " \
                            "to undefined environment variable '%s'.",
                            var_name);
                    retCode = -1;
                    break;
                }
                if(ptr != prev) {
                    tmp_sz = ptr - prev;
                    strncat(expanded, prev, tmp_sz);
                }
                strcat(expanded, var_value);
                prev = close_brace + 1;
            } else {
                char *close_slash = strpbrk(ptr + 1, "\\/");
                if(close_slash == NULL) {
                    strcpy(var_name, ptr + 1);
                } else {
                    tmp_sz = close_slash - ptr - 1;
                    strncpy(var_name, ptr + 1, tmp_sz);
                    var_name[tmp_sz] = 0;
                }
                var_value = getenv(var_name);
                if(var_value == NULL) {
                    sprintf(expanded,
                            "Command line specification contains reference " \
                            "to undefined environment variable '%s'. " \
                            "M.b. program coudln't extract correctly variable name. " \
                            "It is better to use '${VARNAME}' notation.",
                            var_name);
                    retCode = -1;
                    break;
                }
                if(ptr != prev) {
                    tmp_sz = ptr - prev;
                    strncat(expanded, prev, tmp_sz);
                }
                strcat(expanded, var_value);
                prev = close_slash;
            } 
        } 
    } 
    strcpy(commandLine, expanded);
    delete [] expanded;
    expanded = NULL;
    return retCode;
}
int expandEnvVariables ( char * commandLine ) { int len = strlen ( commandLine ) ; int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( commandLine [ i ] == ' ' ) { count ++ ; } } if ( count == 0 ) { return 0 ; } int tmp_sz = len + count * 1024 ; char * expanded = new char [ tmp_sz ] ; memset ( expanded , 0 , tmp_sz ) ; char var_name [ 1024 ] ; char * var_value = NULL ; char * prev = commandLine ; char * ptr = NULL ; int retCode = 0 ; while ( prev != NULL ) { ptr = strchr ( prev , ' ' ) ; if ( ptr == NULL ) { strcat ( expanded , prev ) ; break ; } else { if ( ptr [ 1 ] == ' ' ) { char * close_brace = strchr ( ptr + 2 , ' ' ) ; if ( close_brace == NULL ) { strcpy ( expanded , " " " " " " ) ; retCode = -1 ; break ; } tmp_sz = close_brace - ptr - 2 ; strncpy ( var_name , ptr + 2 , tmp_sz ) ; var_name [ tmp_sz ] = 0 ; var_value = getenv ( var_name ) ; if ( var_value == NULL ) { sprintf ( expanded , " " " " , var_name ) ; retCode = -1 ; break ; } if ( ptr != prev ) { tmp_sz = ptr - prev ; strncat ( expanded , prev , tmp_sz ) ; } strcat ( expanded , var_value ) ; prev = close_brace + 1 ; } else { char * close_slash = strpbrk ( ptr + 1 , " \\ " ) ; if ( close_slash == NULL ) { strcpy ( var_name , ptr + 1 ) ; } else { tmp_sz = close_slash - ptr - 1 ; strncpy ( var_name , ptr + 1 , tmp_sz ) ; var_name [ tmp_sz ] = 0 ; } var_value = getenv ( var_name ) ; if ( var_value == NULL ) { sprintf ( expanded , " " " " " " " " , var_name ) ; retCode = -1 ; break ; } if ( ptr != prev ) { tmp_sz = ptr - prev ; strncat ( expanded , prev , tmp_sz ) ; } strcat ( expanded , var_value ) ; prev = close_slash ; } } } strcpy ( commandLine , expanded ) ; delete [ ] expanded ; expanded = NULL ; return retCode ; }
Expands all environment variables in the given command line specification.
Expands all environment variables in the given command line specification .
// Calculate number of references to env. variables, i.e. number of dollar signs. // Nothing to do. Source buffer doesn't contain any env. variables. // Allocate temporary memory for transformation. // Go through source command line specification, find env. variables, and replace them. // Find next reference to env. variable. // Copy the rest part of source command line. // Extract name of env. variable. // We have "${VARNAME}" notation here. // Find closing brace. // Get env. variable name. // Get env. variable value. // Check if variable was found. // M.b. we have some part before reference to env. variable. // Put env. variable value to destination buffer. // Go ahead. // We have "$VARNAME" notation here. // Find end of env. variable. We suppose that it is either '/' or '\'. // Get env. variable name. // Get env. variable value. // Check if variable was found. // M.b. we have some part before reference to env. variable. // Put env. variable value to destination buffer. // Go ahead. //if ptr[1] //if ptr == NULL //while