bridge/class-master/depend/pari/src/language/es.c
/* expand environment variables in str, return a malloc'ed buffer
 * assume no \ remain and str can be freed */
expand environment variables in str, return a malloc'ed buffer
assume no \ remain and str can be freed
expand environment variables in str return a malloc ' ed buffer assume no \ remain and str can be freed
static char *
_expand_env(char *str)
{
  long i, l, len = 0, xlen = 16, xnum = 0;
  char *s = str, *s0 = s, *env;
  char **x = (char **)pari_malloc(xlen * sizeof(char*));
  while (*s)
  {
    if (*s != '$') { s++; continue; }
    l = s - s0;
    if (l)
    {
      s0 = (char*)memcpy(pari_malloc(l+1), s0, l); s0[l] = 0;
      x[xnum++] = s0; len += l;
    }
    if (xnum > xlen - 3) 
    {
      xlen <<= 1;
      x = (char **)pari_realloc((void*)x, xlen * sizeof(char*));
    }
    s0 = ++s; 
    while (is_keyword_char(*s)) s++;
    l = s - s0;
    env = (char*)memcpy(pari_malloc(l+1), s0, l); env[l] = 0;
    s0 = os_getenv(env);
    if (!s0)
    {
      pari_warn(warner,"undefined environment variable: %s",env);
      s0 = (char*)"";
    }
    l = strlen(s0);
    if (l)
    {
      s0 = (char*)memcpy(pari_malloc(l+1), s0, l); s0[l] = 0;
      x[xnum++] = s0; len += l;
    }
    pari_free(env); s0 = s;
  }
  l = s - s0;
  if (l)
  {
    s0 = (char*)memcpy(pari_malloc(l+1), s0, l); s0[l] = 0;
    x[xnum++] = s0; len += l;
  }
  s = (char*)pari_malloc(len+1); *s = 0;
  for (i = 0; i < xnum; i++) { (void)strcat(s, x[i]); pari_free(x[i]); }
  pari_free(str); pari_free(x); return s;
}
static char * _expand_env ( char * str ) { long i , l , len = 0 , xlen = 16 , xnum = 0 ; char * s = str , * s0 = s , * env ; char * * x = ( char * * ) pari_malloc ( xlen * sizeof ( char * ) ) ; while ( * s ) { if ( * s != ' ' ) { s ++ ; continue ; } l = s - s0 ; if ( l ) { s0 = ( char * ) memcpy ( pari_malloc ( l + 1 ) , s0 , l ) ; s0 [ l ] = 0 ; x [ xnum ++ ] = s0 ; len += l ; } if ( xnum > xlen - 3 ) { xlen <<= 1 ; x = ( char * * ) pari_realloc ( ( void * ) x , xlen * sizeof ( char * ) ) ; } s0 = ++ s ; while ( is_keyword_char ( * s ) ) s ++ ; l = s - s0 ; env = ( char * ) memcpy ( pari_malloc ( l + 1 ) , s0 , l ) ; env [ l ] = 0 ; s0 = os_getenv ( env ) ; if ( ! s0 ) { pari_warn ( warner , " " , env ) ; s0 = ( char * ) " " ; } l = strlen ( s0 ) ; if ( l ) { s0 = ( char * ) memcpy ( pari_malloc ( l + 1 ) , s0 , l ) ; s0 [ l ] = 0 ; x [ xnum ++ ] = s0 ; len += l ; } pari_free ( env ) ; s0 = s ; } l = s - s0 ; if ( l ) { s0 = ( char * ) memcpy ( pari_malloc ( l + 1 ) , s0 , l ) ; s0 [ l ] = 0 ; x [ xnum ++ ] = s0 ; len += l ; } s = ( char * ) pari_malloc ( len + 1 ) ; * s = 0 ; for ( i = 0 ; i < xnum ; i ++ ) { ( void ) strcat ( s , x [ i ] ) ; pari_free ( x [ i ] ) ; } pari_free ( str ) ; pari_free ( x ) ; return s ; }
expand environment variables in str, return a malloc'ed buffer
 assume no \ remain and str can be freed
expand environment variables in str return a malloc ' ed buffer assume no \ remain and str can be freed
/* need room for possibly two more elts */ /* skip $ */