Back to index

tetex-bin  3.0
kdefault.c
Go to the documentation of this file.
00001 /* kdefault.c: Expand extra colons.
00002    (This is not named default.c because then the OSF/1 make tries to
00003    make a program `default' from it, since we have a target `default';
00004    and OSF/1 make doesn't understand .PHONY.)
00005 
00006 Copyright (C) 1993, 94, 96 Karl Berry.
00007 
00008 This library is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU Library General Public
00010 License as published by the Free Software Foundation; either
00011 version 2 of the License, or (at your option) any later version.
00012 
00013 This library is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 Library General Public License for more details.
00017 
00018 You should have received a copy of the GNU Library General Public
00019 License along with this library; if not, write to the Free Software
00020 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00021 
00022 #include <kpathsea/config.h>
00023 
00024 #include <kpathsea/c-pathch.h>
00025 #include <kpathsea/default.h>
00026 
00027 
00028 /* Check for leading colon first, then trailing, then doubled, since
00029    that is fastest.  Usually it will be leading or trailing.  */
00030 
00031 string 
00032 kpse_expand_default P2C(const_string, path,  const_string, fallback)
00033 {
00034   unsigned path_length;
00035   string expansion;
00036   
00037   /* The default path better not be null.  */
00038   assert (fallback);
00039   
00040   if (path == NULL)
00041     expansion = xstrdup (fallback);
00042 
00043   /* Solitary or leading :?  */
00044   else if (IS_ENV_SEP (*path))
00045     {
00046       expansion = path[1] == 0 ? xstrdup (fallback) : concat (fallback, path);
00047     }
00048 
00049   /* Sorry about the assignment in the middle of the expression, but
00050      conventions were made to be flouted and all that.  I don't see the
00051      point of calling strlen twice or complicating the logic just to
00052      avoid the assignment (especially now that I've pointed it out at
00053      such great length).  */
00054   else if (path[(path_length = strlen (path)) - 1] == ENV_SEP)
00055     expansion = concat (path, fallback);
00056 
00057   /* OK, not leading or trailing.  Check for doubled.  */
00058   else
00059     {
00060       const_string loc;
00061 
00062       for (loc = path; *loc; loc++)
00063         if (IS_ENV_SEP (loc[0]) && IS_ENV_SEP (loc[1]))
00064           break;
00065       if (*loc)
00066         { /* We have a doubled colon.  */
00067           expansion = (string)xmalloc (path_length + strlen(fallback) + 1);
00068 
00069           /* Copy stuff up to and including the first colon.  */
00070           strncpy (expansion, path, loc - path + 1);
00071           expansion[loc - path + 1] = 0;
00072 
00073           /* Copy in FALLBACK, and then the rest of PATH.  */
00074           strcat (expansion, fallback);
00075           strcat (expansion, loc + 1);
00076         }
00077       else
00078         { /* No doubled colon. */
00079           expansion = xstrdup(path);
00080         }
00081     }
00082   
00083   return expansion;
00084 }
00085 
00086 #ifdef TEST
00087 
00088 void
00089 test_expand_default (const_string path, const_string def)
00090 {
00091   string answer;
00092   
00093   printf ("Expansion of `%s':\t", path ? path : "(nil)");
00094   answer = kpse_expand_default (path, def);
00095   puts (answer);
00096 }
00097 
00098 int
00099 main ()
00100 {
00101   string default_path = "default";
00102 
00103   test_expand_default (NULL, default_path);
00104   test_expand_default ("", default_path);
00105   test_expand_default ("none", default_path);
00106   test_expand_default (ENV_SEP_STRING, default_path);
00107   test_expand_default (ENV_SEP_STRING "first", default_path);
00108   test_expand_default ("last" ENV_SEP_STRING, default_path);
00109   test_expand_default ("middle" ENV_SEP_STRING ENV_SEP_STRING "elddim", default_path);
00110   
00111   return 0;
00112 }
00113 
00114 #endif /* TEST */
00115 
00116 
00117 /*
00118 Local variables:
00119 standalone-compile-command: "gcc -g -I. -I.. -DTEST default.c kpathsea.a"
00120 End:
00121 */