[BACK]Return to var.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Diff for /src/usr.bin/make/var.c between version 1.4 and 1.5

version 1.4, 1996/09/02 16:04:22 version 1.5, 1996/11/30 21:09:07
Line 1 
Line 1 
 /*      $OpenBSD$       */  /*      $OpenBSD$       */
 /*      $NetBSD: var.c,v 1.14 1996/08/13 16:42:25 christos Exp $        */  /*      $NetBSD: var.c,v 1.15 1996/11/06 17:59:29 christos Exp $        */
   
 /*  /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.   * Copyright (c) 1988, 1989, 1990, 1993
  * Copyright (c) 1988, 1989 by Adam de Boor   *      The Regents of the University of California.  All rights reserved.
  * Copyright (c) 1989 by Berkeley Softworks   * Copyright (c) 1989 by Berkeley Softworks
  * All rights reserved.   * All rights reserved.
  *   *
Line 41 
Line 41 
   
 #ifndef lint  #ifndef lint
 #if 0  #if 0
 static char sccsid[] = "@(#)var.c       5.7 (Berkeley) 6/1/90";  static char sccsid[] = "@(#)var.c       8.3 (Berkeley) 3/19/94";
 #else  #else
 static char rcsid[] = "$OpenBSD$";  static char rcsid[] = "$OpenBSD$";
 #endif  #endif
Line 154 
Line 154 
 #define VAR_SUB_GLOBAL  1   /* Apply substitution globally */  #define VAR_SUB_GLOBAL  1   /* Apply substitution globally */
 #define VAR_MATCH_START 2   /* Match at start of word */  #define VAR_MATCH_START 2   /* Match at start of word */
 #define VAR_MATCH_END   4   /* Match at end of word */  #define VAR_MATCH_END   4   /* Match at end of word */
 #define VAR_NO_SUB      8   /* Substitution is non-global and already done */  
 } VarPattern;  } VarPattern;
   
 static int VarCmp __P((ClientData, ClientData));  static int VarCmp __P((ClientData, ClientData));
Line 280 
Line 279 
   
         if ((env = getenv (name)) != NULL) {          if ((env = getenv (name)) != NULL) {
             int         len;              int         len;
   
             v = (Var *) emalloc(sizeof(Var));              v = (Var *) emalloc(sizeof(Var));
             v->name = estrdup(name);              v->name = estrdup(name);
   
             len = strlen(env);              len = strlen(env);
   
             v->val = Buf_Init(len);              v->val = Buf_Init(len);
             Buf_AddBytes(v->val, len, (Byte *)env);              Buf_AddBytes(v->val, len, (Byte *)env);
   
             v->flags = VAR_FROM_ENV;              v->flags = VAR_FROM_ENV;
             return (v);              return (v);
         } else if (checkEnvFirst && (flags & FIND_GLOBAL) &&          } else if (checkEnvFirst && (flags & FIND_GLOBAL) &&
Line 756 
Line 755 
  * VarMatch --   * VarMatch --
  *      Place the word in the buffer if it matches the given pattern.   *      Place the word in the buffer if it matches the given pattern.
  *      Callback function for VarModify to implement the :M modifier.   *      Callback function for VarModify to implement the :M modifier.
  *   *
  * Results:   * Results:
  *      TRUE if a space should be placed in the buffer before the next   *      TRUE if a space should be placed in the buffer before the next
  *      word.   *      word.
Line 792 
Line 791 
  *      Place the word in the buffer if it matches the given pattern.   *      Place the word in the buffer if it matches the given pattern.
  *      Callback function for VarModify to implement the System V %   *      Callback function for VarModify to implement the System V %
  *      modifiers.   *      modifiers.
  *   *
  * Results:   * Results:
  *      TRUE if a space should be placed in the buffer before the next   *      TRUE if a space should be placed in the buffer before the next
  *      word.   *      word.
Line 835 
Line 834 
  * VarNoMatch --   * VarNoMatch --
  *      Place the word in the buffer if it doesn't match the given pattern.   *      Place the word in the buffer if it doesn't match the given pattern.
  *      Callback function for VarModify to implement the :N modifier.   *      Callback function for VarModify to implement the :N modifier.
  *   *
  * Results:   * Results:
  *      TRUE if a space should be placed in the buffer before the next   *      TRUE if a space should be placed in the buffer before the next
  *      word.   *      word.
Line 892 
Line 891 
     VarPattern  *pattern = (VarPattern *) patternp;      VarPattern  *pattern = (VarPattern *) patternp;
   
     wordLen = strlen(word);      wordLen = strlen(word);
     if ((pattern->flags & VAR_NO_SUB) == 0) {      if (1) { /* substitute in each word of the variable */
         /*          /*
          * Still substituting -- break it down into simple anchored cases           * Break substitution down into simple anchored cases
          * and if none of them fits, perform the general substitution case.           * and if none of them fits, perform the general substitution case.
          */           */
         if ((pattern->flags & VAR_MATCH_START) &&          if ((pattern->flags & VAR_MATCH_START) &&
Line 976 
Line 975 
              * Pattern is unanchored: search for the pattern in the word using               * Pattern is unanchored: search for the pattern in the word using
              * String_FindSubstring, copying unmatched portions and the               * String_FindSubstring, copying unmatched portions and the
              * right-hand-side for each match found, handling non-global               * right-hand-side for each match found, handling non-global
              * subsititutions correctly, etc. When the loop is done, any               * substitutions correctly, etc. When the loop is done, any
              * remaining part of the word (word and wordLen are adjusted               * remaining part of the word (word and wordLen are adjusted
              * accordingly through the loop) is copied straight into the               * accordingly through the loop) is copied straight into the
              * buffer.               * buffer.
Line 999 
Line 998 
                     Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs);                      Buf_AddBytes(buf, pattern->rightLen, (Byte *)pattern->rhs);
                     wordLen -= (cp - word) + pattern->leftLen;                      wordLen -= (cp - word) + pattern->leftLen;
                     word = cp + pattern->leftLen;                      word = cp + pattern->leftLen;
                     if (wordLen == 0) {                      if (wordLen == 0 || (pattern->flags & VAR_SUB_GLOBAL) == 0){
                         done = TRUE;                          done = TRUE;
                     }                      }
                     if ((pattern->flags & VAR_SUB_GLOBAL) == 0) {  
                         done = TRUE;  
                         pattern->flags |= VAR_NO_SUB;  
                     }  
                 } else {                  } else {
                     done = TRUE;                      done = TRUE;
                 }                  }
Line 1024 
Line 1019 
             return ((Buf_Size(buf) != origSize) || addSpace);              return ((Buf_Size(buf) != origSize) || addSpace);
         }          }
         /*          /*
          * Common code for anchored substitutions: if performed a substitution           * Common code for anchored substitutions:
          * and it's not supposed to be global, mark the pattern as requiring           * addSpace was set TRUE if characters were added to the buffer.
          * no more substitutions. addSpace was set TRUE if characters were  
          * added to the buffer.  
          */           */
         if ((pattern->flags & VAR_SUB_GLOBAL) == 0) {  
             pattern->flags |= VAR_NO_SUB;  
         }  
         return (addSpace);          return (addSpace);
     }      }
  nosub:   nosub:
Line 1077 
Line 1067 
   
     for (i = 1; i < ac; i++)      for (i = 1; i < ac; i++)
         addSpace = (*modProc)(av[i], addSpace, buf, datum);          addSpace = (*modProc)(av[i], addSpace, buf, datum);
   
     Buf_AddByte (buf, '\0');      Buf_AddByte (buf, '\0');
     str = (char *)Buf_GetAll (buf, (int *)NULL);      str = (char *)Buf_GetAll (buf, (int *)NULL);
     Buf_Destroy (buf, FALSE);      Buf_Destroy (buf, FALSE);
Line 1128 
Line 1118 
                                  * expanding it in a non-local context. This                                   * expanding it in a non-local context. This
                                  * is done to support dynamic sources. The                                   * is done to support dynamic sources. The
                                  * result is just the invocation, unaltered */                                   * result is just the invocation, unaltered */
   
     *freePtr = FALSE;      *freePtr = FALSE;
     dynamic = FALSE;      dynamic = FALSE;
     start = str;      start = str;
   
     if (str[1] != '(' && str[1] != '{') {      if (str[1] != '(' && str[1] != '{') {
         /*          /*
          * If it's not bounded by braces of some sort, life is much simpler.           * If it's not bounded by braces of some sort, life is much simpler.
Line 1147 
Line 1137 
         v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);          v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
         if (v == (Var *)NIL) {          if (v == (Var *)NIL) {
             *lengthPtr = 2;              *lengthPtr = 2;
   
             if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {              if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {
                 /*                  /*
                  * If substituting a local variable in a non-local context,                   * If substituting a local variable in a non-local context,
Line 1205 
Line 1195 
             return (var_Error);              return (var_Error);
         }          }
         *tstr = '\0';          *tstr = '\0';
   
         v = VarFind (str + 2, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);          v = VarFind (str + 2, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
         if ((v == (Var *)NIL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&          if ((v == (Var *)NIL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&
             ((tstr-str) == 4) && (str[3] == 'F' || str[3] == 'D'))              ((tstr-str) == 4) && (str[3] == 'F' || str[3] == 'D'))
Line 1231 
Line 1221 
                     vname[0] = str[2];                      vname[0] = str[2];
                     vname[1] = '\0';                      vname[1] = '\0';
                     v = VarFind(vname, ctxt, 0);                      v = VarFind(vname, ctxt, 0);
   
                     if (v != (Var *)NIL) {                      if (v != (Var *)NIL) {
                         /*                          /*
                          * No need for nested expansion or anything, as we're                           * No need for nested expansion or anything, as we're
Line 1239 
Line 1229 
                          * but nested invocations in them...                           * but nested invocations in them...
                          */                           */
                         val = (char *)Buf_GetAll(v->val, (int *)NULL);                          val = (char *)Buf_GetAll(v->val, (int *)NULL);
   
                         if (str[3] == 'D') {                          if (str[3] == 'D') {
                             val = VarModify(val, VarHead, (ClientData)0);                              val = VarModify(val, VarHead, (ClientData)0);
                         } else {                          } else {
Line 1258 
Line 1248 
                 }                  }
             }              }
         }          }
   
         if (v == (Var *)NIL) {          if (v == (Var *)NIL) {
             if ((((tstr-str) == 3) ||              if ((((tstr-str) == 3) ||
                  ((((tstr-str) == 4) && (str[3] == 'F' ||                   ((((tstr-str) == 4) && (str[3] == 'F' ||
Line 1287 
Line 1277 
                        ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))                         ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
             {              {
                 int     len;                  int     len;
   
                 len = (tstr-str) - 3;                  len = (tstr-str) - 3;
                 if ((strncmp(str+2, ".TARGET", len) == 0) ||                  if ((strncmp(str+2, ".TARGET", len) == 0) ||
                     (strncmp(str+2, ".ARCHIVE", len) == 0) ||                      (strncmp(str+2, ".ARCHIVE", len) == 0) ||
Line 1297 
Line 1287 
                     dynamic = TRUE;                      dynamic = TRUE;
                 }                  }
             }              }
   
             if (!haveModifier) {              if (!haveModifier) {
                 /*                  /*
                  * No modifiers -- have specification length so we can return                   * No modifiers -- have specification length so we can return
Line 1347 
Line 1337 
         str = Var_Subst(NULL, str, ctxt, err);          str = Var_Subst(NULL, str, ctxt, err);
         *freePtr = TRUE;          *freePtr = TRUE;
     }      }
   
     v->flags &= ~VAR_IN_USE;      v->flags &= ~VAR_IN_USE;
   
     /*      /*
      * Now we need to apply any modifiers the user wants applied.       * Now we need to apply any modifiers the user wants applied.
      * These are:       * These are:
Line 1375 
Line 1365 
         while (*tstr != endc) {          while (*tstr != endc) {
             char        *newStr;    /* New value to return */              char        *newStr;    /* New value to return */
             char        termc;      /* Character which terminated scan */              char        termc;      /* Character which terminated scan */
   
             if (DEBUG(VAR)) {              if (DEBUG(VAR)) {
                 printf("Applying :%c to \"%s\"\n", *tstr, str);                  printf("Applying :%c to \"%s\"\n", *tstr, str);
             }              }
Line 1452 
Line 1442 
                     }                      }
   
                     buf = Buf_Init(0);                      buf = Buf_Init(0);
   
                     /*                      /*
                      * Pass through the lhs looking for 1) escaped delimiters,                       * Pass through the lhs looking for 1) escaped delimiters,
                      * '$'s and backslashes (place the escaped character in                       * '$'s and backslashes (place the escaped character in
Line 1478 
Line 1468 
                                 char        *cp2;                                  char        *cp2;
                                 int         len;                                  int         len;
                                 Boolean     freeIt;                                  Boolean     freeIt;
   
                                 cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);                                  cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);
                                 Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2);                                  Buf_AddBytes(buf, strlen(cp2), (Byte *)cp2);
                                 if (freeIt) {                                  if (freeIt) {
Line 1498 
Line 1488 
                     }                      }
   
                     Buf_AddByte(buf, (Byte)'\0');                      Buf_AddByte(buf, (Byte)'\0');
   
                     /*                      /*
                      * If lhs didn't end with the delimiter, complain and                       * If lhs didn't end with the delimiter, complain and
                      * return NULL                       * return NULL
Line 1533 
Line 1523 
                      * it right here) and 3) expand any variable substitutions.                       * it right here) and 3) expand any variable substitutions.
                      */                       */
                     buf = Buf_Init(0);                      buf = Buf_Init(0);
   
                     tstr = cp + 1;                      tstr = cp + 1;
                     for (cp = tstr; *cp != '\0' && *cp != delim; cp++) {                      for (cp = tstr; *cp != '\0' && *cp != delim; cp++) {
                         if ((*cp == '\\') &&                          if ((*cp == '\\') &&
Line 1564 
Line 1554 
                     }                      }
   
                     Buf_AddByte(buf, (Byte)'\0');                      Buf_AddByte(buf, (Byte)'\0');
   
                     /*                      /*
                      * If didn't end in delimiter character, complain                       * If didn't end in delimiter character, complain
                      */                       */
Line 1658 
Line 1648 
                      */                       */
                     VarPattern      pattern;                      VarPattern      pattern;
                     Boolean         eqFound;                      Boolean         eqFound;
   
                     pattern.flags = 0;                      pattern.flags = 0;
                     eqFound = FALSE;                      eqFound = FALSE;
                     /*                      /*
Line 1681 
Line 1671 
                             cp++;                              cp++;
                     }                      }
                     if (*cp == endc && eqFound) {                      if (*cp == endc && eqFound) {
   
                         /*                          /*
                          * Now we break this sucker into the lhs and                           * Now we break this sucker into the lhs and
                          * rhs. We must null terminate them of course.                           * rhs. We must null terminate them of course.
Line 1691 
Line 1681 
                         pattern.lhs = tstr;                          pattern.lhs = tstr;
                         pattern.leftLen = cp - tstr;                          pattern.leftLen = cp - tstr;
                         *cp++ = '\0';                          *cp++ = '\0';
   
                         pattern.rhs = cp;                          pattern.rhs = cp;
                         cnt = 1;                          cnt = 1;
                         while (cnt) {                          while (cnt) {
Line 1704 
Line 1694 
                         }                          }
                         pattern.rightLen = cp - pattern.rhs;                          pattern.rightLen = cp - pattern.rhs;
                         *cp = '\0';                          *cp = '\0';
   
                         /*                          /*
                          * SYSV modifications happen through the whole                           * SYSV modifications happen through the whole
                          * string. Note the pattern is anchored at the end.                           * string. Note the pattern is anchored at the end.
Line 1724 
Line 1714 
                         Error ("Unknown modifier '%c'\n", *tstr);                          Error ("Unknown modifier '%c'\n", *tstr);
                         for (cp = tstr+1;                          for (cp = tstr+1;
                              *cp != ':' && *cp != endc && *cp != '\0';                               *cp != ':' && *cp != endc && *cp != '\0';
                              cp++)                               cp++)
                                  continue;                                   continue;
                         termc = *cp;                          termc = *cp;
                         newStr = var_Error;                          newStr = var_Error;
Line 1734 
Line 1724 
             if (DEBUG(VAR)) {              if (DEBUG(VAR)) {
                 printf("Result is \"%s\"\n", newStr);                  printf("Result is \"%s\"\n", newStr);
             }              }
   
             if (*freePtr) {              if (*freePtr) {
                 free (str);                  free (str);
             }              }
Line 1758 
Line 1748 
         *lengthPtr = tstr - start + 1;          *lengthPtr = tstr - start + 1;
         *tstr = endc;          *tstr = endc;
     }      }
   
     if (v->flags & VAR_FROM_ENV) {      if (v->flags & VAR_FROM_ENV) {
         Boolean   destroy = FALSE;          Boolean   destroy = FALSE;
   
         if (str != (char *)Buf_GetAll(v->val, (int *)NULL)) {          if (str != (char *)Buf_GetAll(v->val, (int *)NULL)) {
             destroy = TRUE;              destroy = TRUE;
         } else {          } else {
Line 1868 
Line 1858 
                         /*                          /*
                          * Scan up to the end of the variable name.                           * Scan up to the end of the variable name.
                          */                           */
                         for (p = &str[2]; *p &&                          for (p = &str[2]; *p &&
                              *p != ':' && *p != ')' && *p != '}'; p++)                               *p != ':' && *p != ')' && *p != '}'; p++)
                             if (*p == '$')                              if (*p == '$')
                                 break;                                  break;
                         /*                          /*
                          * A variable inside the variable. We cannot expand                           * A variable inside the variable. We cannot expand
Line 1882 
Line 1872 
                             str = p;                              str = p;
                             continue;                              continue;
                         }                          }
   
                         if (strncmp(var, str + 2, p - str - 2) != 0 ||                          if (strncmp(var, str + 2, p - str - 2) != 0 ||
                             var[p - str - 2] != '\0') {                              var[p - str - 2] != '\0') {
                             /*                              /*
                              * Not the variable we want to expand, scan                               * Not the variable we want to expand, scan
                              * until the next variable                               * until the next variable
                              */                               */
                             for (;*p != '$' && *p != '\0'; p++)                              for (;*p != '$' && *p != '\0'; p++)
                                 continue;                                  continue;
                             Buf_AddBytes(buf, p - str, (Byte *) str);                              Buf_AddBytes(buf, p - str, (Byte *) str);
                             str = p;                              str = p;
Line 1903 
Line 1893 
                 if (!expand)                  if (!expand)
                     continue;                      continue;
             }              }
   
             val = Var_Parse (str, ctxt, undefErr, &length, &doFree);              val = Var_Parse (str, ctxt, undefErr, &length, &doFree);
   
             /*              /*
Line 1943 
Line 1933 
                  * advance the string pointer.                   * advance the string pointer.
                  */                   */
                 str += length;                  str += length;
   
                 /*                  /*
                  * Copy all the characters from the variable value straight                   * Copy all the characters from the variable value straight
                  * into the new string.                   * into the new string.
Line 1955 
Line 1945 
             }              }
         }          }
     }      }
   
     Buf_AddByte (buf, '\0');      Buf_AddByte (buf, '\0');
     str = (char *)Buf_GetAll (buf, (int *)NULL);      str = (char *)Buf_GetAll (buf, (int *)NULL);
     Buf_Destroy (buf, FALSE);      Buf_Destroy (buf, FALSE);
Line 2014 
Line 2004 
  *      None   *      None
  *   *
  * Side Effects:   * Side Effects:
  *      The VAR_CMD and VAR_GLOBAL contexts are created   *      The VAR_CMD and VAR_GLOBAL contexts are created
  *-----------------------------------------------------------------------   *-----------------------------------------------------------------------
  */   */
 void  void
Line 2032 
Line 2022 
 {  {
     Lst_Destroy(allVars, VarDelete);      Lst_Destroy(allVars, VarDelete);
 }  }
   
   
 /****************** PRINT DEBUGGING INFO *****************/  /****************** PRINT DEBUGGING INFO *****************/
 static int  static int

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5