home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / kpathsea / kdefault.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-28  |  3.4 KB  |  121 lines

  1. /* kdefault.c: Expand extra colons.
  2.    (This is not named default.c because then the OSF/1 make tries to
  3.    make a program `default' from it, since we have a target `default';
  4.    and OSF/1 make doesn't understand .PHONY.)
  5.  
  6. Copyright (C) 1993, 94 Karl Berry.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <kpathsea/config.h>
  23.  
  24. #include <kpathsea/c-pathch.h>
  25. #include <kpathsea/default.h>
  26.  
  27.  
  28. /* Check for leading colon first, then trailing, then doubled, since
  29.    that is fastest.  Usually it will be leading or trailing.  */
  30.  
  31. string 
  32. kpse_expand_default P2C(const_string, path,  const_string, fallback)
  33. {
  34.   unsigned path_length;
  35.   string expansion;
  36.   
  37.   /* The default path better not be null.  */
  38.   assert (fallback);
  39.   
  40.   if (path == NULL)
  41.     expansion = (string) fallback;
  42.  
  43.   /* Solitary or leading :?  */
  44.   else if (IS_ENV_SEP (*path))
  45.     {
  46.       expansion = path[1] == 0 ? (string) fallback : concat (fallback, path);
  47.     }
  48.  
  49.   /* Sorry about the assignment in the middle of the expression, but
  50.      conventions were made to be flouted and all that.  I don't see the
  51.      point of calling strlen twice or complicating the logic just to
  52.      avoid the assignment (especially now that I've pointed it out at
  53.      such great length).  */
  54.   else if (path[(path_length = strlen (path)) - 1] == ENV_SEP)
  55.     expansion = concat (path, fallback);
  56.  
  57.   /* OK, not leading or trailing.  Check for doubled.  */
  58.   else
  59.     {
  60.       const_string loc;
  61.  
  62.       /* What we'll return if we find none.  */
  63.       expansion = (string) path;
  64.  
  65.       for (loc = path; *loc && expansion == path; loc++)
  66.         {
  67.           if (IS_ENV_SEP (loc[0]) && IS_ENV_SEP (loc[1]))
  68.             { /* We have a doubled colon.  */
  69.               expansion = xmalloc (path_length + strlen (fallback) + 1);
  70.               
  71.               /* Copy stuff up to and including the first colon.  */
  72.               strncpy (expansion, path, loc - path + 1);
  73.               expansion[loc - path + 1] = 0;
  74.               
  75.               /* Copy in FALLBACK, and then the rest of PATH.  */
  76.               strcat (expansion, fallback);
  77.               strcat (expansion, loc + 1);
  78.             }
  79.         }
  80.     }
  81.   
  82.   return expansion;
  83. }
  84.  
  85. #ifdef TEST
  86.  
  87. void
  88. test_expand_default (const_string path, const_string def)
  89. {
  90.   string answer;
  91.   
  92.   printf ("Expansion of `%s':\t", path ? path : "(null)");
  93.   answer = kpse_expand_default (path, def);
  94.   puts (answer);
  95. }
  96.  
  97. int
  98. main ()
  99. {
  100.   string default_path = "default";
  101.  
  102.   test_expand_default (NULL, default_path);
  103.   test_expand_default ("", default_path);
  104.   test_expand_default ("none", default_path);
  105.   test_expand_default (":", default_path);
  106.   test_expand_default (":first", default_path);
  107.   test_expand_default ("last:", default_path);
  108.   test_expand_default ("middle::elddim", default_path);
  109.   
  110.   return 0;
  111. }
  112.  
  113. #endif /* TEST */
  114.  
  115.  
  116. /*
  117. Local variables:
  118. standalone-compile-command: "gcc -g -I. -I.. -DTEST default.c kpathsea.a"
  119. End:
  120. */
  121.