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