VirtualBox

Changeset 903 in kBuild for trunk/src/gmakenew/vpath.c


Ignore:
Timestamp:
May 23, 2007 5:31:19 AM (18 years ago)
Author:
bird
Message:

Merged with the 2007-05-23 CVS. Added rsort and fixed a couple of windows build issues.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gmakenew/vpath.c

    r527 r903  
    3030  {
    3131    struct vpath *next; /* Pointer to next struct in the linked list.  */
    32     char *pattern;      /* The pattern to match.  */
    33     char *percent;      /* Pointer into `pattern' where the `%' is.  */
     32    const char *pattern;/* The pattern to match.  */
     33    const char *percent;/* Pointer into `pattern' where the `%' is.  */
    3434    unsigned int patlen;/* Length of the pattern.  */
    35     char **searchpath;  /* Null-terminated list of directories.  */
     35    const char **searchpath; /* Null-terminated list of directories.  */
    3636    unsigned int maxlen;/* Maximum length of any entry in the list.  */
    3737  };
     
    5050
    5151
    52 static int selective_vpath_search PARAMS ((struct vpath *path, char **file, FILE_TIMESTAMP *mtime_ptr));
    53 
    54 /* Reverse the chain of selective VPATH lists so they
    55    will be searched in the order given in the makefiles
    56    and construct the list from the VPATH variable.  */
     52
     53/* Reverse the chain of selective VPATH lists so they will be searched in the
     54   order given in the makefiles and construct the list from the VPATH
     55   variable.  */
    5756
    5857void
     
    9796      /* Save the list of vpaths.  */
    9897      struct vpath *save_vpaths = vpaths;
     98      char gp[] = "%";
    9999
    100100      /* Empty `vpaths' so the new one will have no next, and `vpaths'
     
    103103
    104104      /* Parse P.  */
    105       construct_vpath_list ("%", p);
     105      construct_vpath_list (gp, p);
    106106
    107107      /* Store the created path as the general path,
     
    133133      /* Save the list of vpaths.  */
    134134      struct vpath *save_vpaths = vpaths;
     135      char gp[] = "%";
    135136
    136137      /* Empty `vpaths' so the new one will have no next, and `vpaths'
     
    139140
    140141      /* Parse P.  */
    141       construct_vpath_list ("%", p);
     142      construct_vpath_list (gp, p);
    142143
    143144      /* Store the created path as the GPATH,
     
    171172construct_vpath_list (char *pattern, char *dirpath)
    172173{
    173   register unsigned int elem;
    174   register char *p;
    175   register char **vpath;
    176   register unsigned int maxvpath;
     174  unsigned int elem;
     175  char *p;
     176  const char **vpath;
     177  unsigned int maxvpath;
    177178  unsigned int maxelem;
    178   char *percent = NULL;
     179  const char *percent = NULL;
    179180
    180181  if (pattern != 0)
    181     {
    182       pattern = xstrdup (pattern);
    183       percent = find_percent (pattern);
    184     }
     182    percent = find_percent (pattern);
    185183
    186184  if (dirpath == 0)
    187185    {
    188186      /* Remove matching listings.  */
    189       register struct vpath *path, *lastpath;
     187      struct vpath *path, *lastpath;
    190188
    191189      lastpath = 0;
     
    207205
    208206              /* Free its unused storage.  */
    209               free (path->pattern);
    210               free ((char *) path->searchpath);
    211               free ((char *) path);
     207              free (path->searchpath);
     208              free (path);
    212209            }
    213210          else
     
    217214        }
    218215
    219       if (pattern != 0)
    220         free (pattern);
    221216      return;
    222217    }
     
    225220    convert_vpath_to_windows32(dirpath, ';');
    226221#endif
     222
     223  /* Skip over any initial separators and blanks.  */
     224  while (*dirpath == PATH_SEPARATOR_CHAR || isblank ((unsigned char)*dirpath))
     225    ++dirpath;
    227226
    228227  /* Figure out the maximum number of VPATH entries and put it in
     
    236235      ++maxelem;
    237236
    238   vpath = (char **) xmalloc (maxelem * sizeof (char *));
     237  vpath = xmalloc (maxelem * sizeof (const char *));
    239238  maxvpath = 0;
    240239
    241   /* Skip over any initial separators and blanks.  */
     240  elem = 0;
    242241  p = dirpath;
    243   while (*p == PATH_SEPARATOR_CHAR || isblank ((unsigned char)*p))
    244     ++p;
    245 
    246   elem = 0;
    247242  while (*p != '\0')
    248243    {
     
    266261        --len;
    267262
     263      /* Put the directory on the vpath list.  */
    268264      if (len > 1 || *v != '.')
    269265        {
    270           v = savestring (v, len);
    271 
    272           /* Verify that the directory actually exists.  */
    273 
    274           if (dir_file_exists_p (v, ""))
    275             {
    276               /* It does.  Put it in the list.  */
    277               vpath[elem++] = dir_name (v);
    278               free (v);
    279               if (len > maxvpath)
    280                 maxvpath = len;
    281             }
    282           else
    283             /* The directory does not exist.  Omit from the list.  */
    284             free (v);
     266          vpath[elem++] = dir_name (strcache_add_len (v, len));
     267          if (len > maxvpath)
     268            maxvpath = len;
    285269        }
    286270
     
    297281         Usually this is maxelem - 1.  If not, shrink down.  */
    298282      if (elem < (maxelem - 1))
    299         vpath = (char **) xrealloc ((char *) vpath,
    300                                     (elem + 1) * sizeof (char *));
     283        vpath = xrealloc (vpath, (elem+1) * sizeof (const char *));
    301284
    302285      /* Put the nil-pointer terminator on the end of the VPATH list.  */
    303       vpath[elem] = 0;
     286      vpath[elem] = NULL;
    304287
    305288      /* Construct the vpath structure and put it into the linked list.  */
    306       path = (struct vpath *) xmalloc (sizeof (struct vpath));
     289      path = xmalloc (sizeof (struct vpath));
    307290      path->searchpath = vpath;
    308291      path->maxlen = maxvpath;
     
    311294
    312295      /* Set up the members.  */
    313       path->pattern = pattern;
    314       path->percent = percent;
     296      path->pattern = strcache_add (pattern);
    315297      path->patlen = strlen (pattern);
     298      path->percent = percent ? path->pattern + (percent - pattern) : 0;
    316299    }
    317300  else
    318     {
    319       /* There were no entries, so free whatever space we allocated.  */
    320       free ((char *) vpath);
    321       if (pattern != 0)
    322         free (pattern);
    323     }
     301    /* There were no entries, so free whatever space we allocated.  */
     302    free (vpath);
    324303}
    325304
     
    329308
    330309int
    331 gpath_search (char *file, unsigned int len)
     310gpath_search (const char *file, unsigned int len)
    332311{
    333   char **gp;
     312  const char **gp;
    334313
    335314  if (gpaths && (len <= gpaths->maxlen))
     
    342321
    343322
    344 /* Search the VPATH list whose pattern matches *FILE for a directory
    345    where the name pointed to by FILE exists.  If it is found, we set *FILE to
    346    the newly malloc'd name of the existing file, *MTIME_PTR (if MTIME_PTR is
    347    not NULL) to its modtime (or zero if no stat call was done), and return 1.
    348    Otherwise we return 0.  */
    349 
    350 int
    351 vpath_search (char **file, FILE_TIMESTAMP *mtime_ptr)
    352 {
    353   register struct vpath *v;
    354 
    355   /* If there are no VPATH entries or FILENAME starts at the root,
    356      there is nothing we can do.  */
    357 
    358   if (**file == '/'
    359 #ifdef HAVE_DOS_PATHS
    360       || **file == '\\'
    361       || (*file)[1] == ':'
    362 #endif
    363       || (vpaths == 0 && general_vpath == 0))
    364     return 0;
    365 
    366   for (v = vpaths; v != 0; v = v->next)
    367     if (pattern_matches (v->pattern, v->percent, *file))
    368       if (selective_vpath_search (v, file, mtime_ptr))
    369         return 1;
    370 
    371   if (general_vpath != 0
    372       && selective_vpath_search (general_vpath, file, mtime_ptr))
    373     return 1;
    374 
    375   return 0;
    376 }
    377 
    378 
    379 /* Search the given VPATH list for a directory where the name pointed
    380    to by FILE exists.  If it is found, we set *FILE to the newly malloc'd
    381    name of the existing file, *MTIME_PTR (if MTIME_PTR is not NULL) to
    382    its modtime (or zero if no stat call was done), and we return 1.
    383    Otherwise we return 0.  */
    384 
    385 static int
    386 selective_vpath_search (struct vpath *path, char **file,
     323
     324/* Search the given VPATH list for a directory where the name pointed to by
     325   FILE exists.  If it is found, we return a cached name of the existing file
     326   and set *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or zero if no
     327   stat call was done).  Otherwise we return NULL.  */
     328
     329static const char *
     330selective_vpath_search (struct vpath *path, const char *file,
    387331                        FILE_TIMESTAMP *mtime_ptr)
    388332{
    389333  int not_target;
    390   char *name, *n;
    391   char *filename;
    392   register char **vpath = path->searchpath;
     334  char *name;
     335  const char *n;
     336  const char *filename;
     337  const char **vpath = path->searchpath;
    393338  unsigned int maxvpath = path->maxlen;
    394   register unsigned int i;
     339  unsigned int i;
    395340  unsigned int flen, vlen, name_dplen;
    396341  int exists = 0;
     
    400345     files that don't exist but are mentioned in a makefile.  */
    401346  {
    402     struct file *f = lookup_file (*file);
     347    struct file *f = lookup_file (file);
    403348    not_target = f == 0 || !f->is_target;
    404349  }
    405350
    406   flen = strlen (*file);
     351  flen = strlen (file);
    407352
    408353  /* Split *FILE into a directory prefix and a name-within-directory.
    409      NAME_DPLEN gets the length of the prefix; FILENAME gets the
    410      pointer to the name-within-directory and FLEN is its length.  */
    411 
    412   n = strrchr (*file, '/');
     354     NAME_DPLEN gets the length of the prefix; FILENAME gets the pointer to
     355     the name-within-directory and FLEN is its length.  */
     356
     357  n = strrchr (file, '/');
    413358#ifdef HAVE_DOS_PATHS
    414359  /* We need the rightmost slash or backslash.  */
    415360  {
    416     char *bslash = strrchr(*file, '\\');
     361    const char *bslash = strrchr(file, '\\');
    417362    if (!n || bslash > n)
    418363      n = bslash;
    419364  }
    420365#endif
    421   name_dplen = n != 0 ? n - *file : 0;
    422   filename = name_dplen > 0 ? n + 1 : *file;
     366  name_dplen = n != 0 ? n - file : 0;
     367  filename = name_dplen > 0 ? n + 1 : file;
    423368  if (name_dplen > 0)
    424369    flen -= name_dplen + 1;
    425370
    426   /* Allocate enough space for the biggest VPATH entry,
    427      a slash, the directory prefix that came with *FILE,
    428      another slash (although this one may not always be
    429      necessary), the filename, and a null terminator.  */
    430   name = (char *) xmalloc (maxvpath + 1 + name_dplen + 1 + flen + 1);
     371  /* Get enough space for the biggest VPATH entry, a slash, the directory
     372     prefix that came with FILE, another slash (although this one may not
     373     always be necessary), the filename, and a null terminator.  */
     374  name = alloca (maxvpath + 1 + name_dplen + 1 + flen + 1);
    431375
    432376  /* Try each VPATH entry.  */
     
    434378    {
    435379      int exists_in_cache = 0;
    436 
    437       n = name;
    438 
    439       /* Put the next VPATH entry into NAME at N and increment N past it.  */
     380      char *p;
     381
     382      p = name;
     383
     384      /* Put the next VPATH entry into NAME at P and increment P past it.  */
    440385      vlen = strlen (vpath[i]);
    441       bcopy (vpath[i], n, vlen);
    442       n += vlen;
     386      memcpy (p, vpath[i], vlen);
     387      p += vlen;
    443388
    444389      /* Add the directory prefix already in *FILE.  */
     
    446391        {
    447392#ifndef VMS
    448           *n++ = '/';
    449 #endif
    450           bcopy (*file, n, name_dplen);
    451           n += name_dplen;
     393          *p++ = '/';
     394#endif
     395          memcpy (p, file, name_dplen);
     396          p += name_dplen;
    452397        }
    453398
    454399#ifdef HAVE_DOS_PATHS
    455400      /* Cause the next if to treat backslash and slash alike.  */
    456       if (n != name && n[-1] == '\\' )
    457         n[-1] = '/';
     401      if (p != name && p[-1] == '\\' )
     402        p[-1] = '/';
    458403#endif
    459404      /* Now add the name-within-directory at the end of NAME.  */
    460405#ifndef VMS
    461       if (n != name && n[-1] != '/')
     406      if (p != name && p[-1] != '/')
    462407        {
    463           *n = '/';
    464           bcopy (filename, n + 1, flen + 1);
     408          *p = '/';
     409          memcpy (p + 1, filename, flen + 1);
    465410        }
    466411      else
    467412#endif
    468         bcopy (filename, n, flen + 1);
     413        memcpy (p, filename, flen + 1);
    469414
    470415      /* Check if the file is mentioned in a makefile.  If *FILE is not
     
    513458          /* Clobber a null into the name at the last slash.
    514459             Now NAME is the name of the directory to look in.  */
    515           *n = '\0';
     460          *p = '\0';
    516461
    517462          /* We know the directory is in the hash table now because either
     
    534479#ifndef VMS
    535480          /* Put the slash back in NAME.  */
    536           *n = '/';
     481          *p = '/';
    537482#endif
    538483
     
    557502
    558503          /* We have found a file.
    559              Store the name we found into *FILE for the caller.  */
    560 
    561           *file = savestring (name, (n + 1 - name) + flen);
    562 
    563           /* If we get here and mtime_ptr hasn't been set, record
     504             If we get here and mtime_ptr hasn't been set, record
    564505             UNKNOWN_MTIME to indicate this.  */
    565506          if (mtime_ptr != 0)
    566507            *mtime_ptr = UNKNOWN_MTIME;
    567508
    568           free (name);
    569           return 1;
     509          /* Store the name we found and return it.  */
     510
     511          return strcache_add_len (name, (p + 1 - name) + flen);
    570512        }
    571513    }
    572514
    573   free (name);
     515  return 0;
     516}
     517
     518
     519/* Search the VPATH list whose pattern matches FILE for a directory where FILE
     520   exists.  If it is found, return the cached name of an existing file, and
     521   set *MTIME_PTR (if MTIME_PTR is not NULL) to its modtime (or zero if no
     522   stat call was done).  Otherwise we return 0.  */
     523
     524const char *
     525vpath_search (const char *file, FILE_TIMESTAMP *mtime_ptr)
     526{
     527  struct vpath *v;
     528
     529  /* If there are no VPATH entries or FILENAME starts at the root,
     530     there is nothing we can do.  */
     531
     532  if (file[0] == '/'
     533#ifdef HAVE_DOS_PATHS
     534      || file[0] == '\\' || file[1] == ':'
     535#endif
     536      || (vpaths == 0 && general_vpath == 0))
     537    return 0;
     538
     539  for (v = vpaths; v != 0; v = v->next)
     540    if (pattern_matches (v->pattern, v->percent, file))
     541      {
     542        const char *p = selective_vpath_search (v, file, mtime_ptr);
     543        if (p)
     544          return p;
     545      }
     546
     547  if (general_vpath != 0)
     548    {
     549      const char *p = selective_vpath_search (general_vpath, file, mtime_ptr);
     550      if (p)
     551        return p;
     552    }
     553
    574554  return 0;
    575555}
     
    581561print_vpath_data_base (void)
    582562{
    583   register unsigned int nvpaths;
    584   register struct vpath *v;
     563  unsigned int nvpaths;
     564  struct vpath *v;
    585565
    586566  puts (_("\n# VPATH Search Paths\n"));
     
    609589  else
    610590    {
    611       register char **path = general_vpath->searchpath;
    612       register unsigned int i;
     591      const char **path = general_vpath->searchpath;
     592      unsigned int i;
    613593
    614594      fputs (_("\n# General (`VPATH' variable) search path:\n# "), stdout);
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette