home *** CD-ROM | disk | FTP | other *** search
/ Der Mediaplex Sampler - Die 6 von Plex / 6_v_plex.zip / 6_v_plex / DISK5 / DOS_50 / PVIC.ZIP / ENVEVAL.C < prev    next >
C/C++ Source or Header  |  1993-04-21  |  3KB  |  116 lines

  1. /*
  2.  *      Evaluate a string, expanding environment variables
  3.  *      where encountered.
  4.  *      We'll use the UNIX convention for representing environment
  5.  *      variables: $xxx, where xxx is the shortest string that
  6.  *      matches some environment variable.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "pvic.h"
  12. #include "locdefs.h"
  13.  
  14. char    *getenv();
  15.  
  16. int
  17. eval_environment (s, len)
  18.   char *s;
  19.   int   len;
  20. /*------------------------------------------------------------------
  21.  *  s=  Pointer to buffer, currently containing string.  It will be
  22.  *      expanded in-place in the buffer.
  23.  *  len=Maximum allowable length of the buffer.  (In this version, we
  24.  *      use a static buffer of 256 bytes internally.)
  25.  *
  26.  * RETURNS:
  27.  *       0      on success.
  28.  *      -1      on failure.  In this case, s may contain a partially
  29.  *                      converted string, but it won't contain a partial
  30.  *                      string.  It will be the FULL string, with as
  31.  *                      many substitutions as we could find.
  32.  */
  33.  
  34. {
  35. #define LEN     256
  36.     char    buf [LEN];
  37.     char    *s1, *s2;
  38.     char    *b1;
  39.     int     done=0;
  40.  
  41.     if (len > LEN)
  42.         return (-1);
  43.  
  44.     s1 = s;
  45.  
  46.     /*  Check for '$', and expand when we find one.  */
  47.     while (!done) {
  48.         if ((s1 = get_pointer_to_chr_in_string (s1, '$')) == NULL)
  49.             done = 1;
  50.         else {
  51.             /*
  52.              *  Here's where the real work gets done.
  53.              *  We'll find the env.var., and convert
  54.              *  it into buf, then copy back into s
  55.              *  and continue.
  56.              */
  57.             char    c;
  58.             int     need, got;
  59.  
  60.             /* Test successively longer strings, to see
  61.              * if they're env.vars.
  62.              */
  63.             for (s2=++s1+1; ; s2++) {
  64.                 c = *s2;        /* save it */
  65.                 *s2 = '\0';
  66.                 b1 = getenv (s1);
  67.                 *s2 = c;                /* restore it */
  68.                 if (b1)                 /* found it */
  69.                     break;
  70.                 if (!*s2)               /* nothing to try */
  71.                     goto Failed;
  72.             }
  73.             --s1;   /* Back to the '$' */
  74.  
  75.             /* OK, we've found one (between s1 & s2,
  76.              * non-inclusive).  Its value is in b1.
  77.              * Do the substitution into bufp,
  78.              * and copy back into s.
  79.              */
  80.             need = strlen(b1) + strlen(s2) + 1;
  81.             got  = len - (s1-s);
  82.             if (need > got)
  83.                 goto Failed;
  84.             strcpy (buf, b1);
  85.             strcat (buf, s2);
  86.             strcpy (s1, buf);
  87.         }
  88.     }
  89.  
  90.     /*  If we get here, the converted value is in s  */
  91.     return (0);
  92.  
  93.    Failed:
  94.     return (-1);
  95. }
  96.  
  97.  
  98. /* #define SAMPLE */
  99. #ifdef SAMPLE  /***************************************************/
  100.  
  101. main (int argc, char **argv)
  102. {
  103.     int     i, ret;
  104.  
  105.     for (i=1; i<argc; i++) {
  106.         printf ("Convert  %s  to", argv [i]);
  107.         ret = eval_environment (argv [i], 80);
  108.         printf ("  %s", argv [i]);
  109.         if (ret)
  110.             printf ("  -  Failed");
  111.         putchar ('\n');
  112.     }
  113. }
  114.  
  115. #endif
  116.