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 1993, 1994, 1996, 2008, 2009, 2011 Karl Berry.
7    Copyright 2002, 2005 Olaf Weber.
8 
9    This library is free software; you can redistribute it and/or
10    modify it under the terms of the GNU Lesser General Public
11    License as published by the Free Software Foundation; either
12    version 2.1 of the License, or (at your option) any later version.
13 
14    This library is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    Lesser General Public License for more details.
18 
19    You should have received a copy of the GNU Lesser General Public License
20    along with this library; if not, see <http://www.gnu.org/licenses/>.  */
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
kpathsea_expand_default(kpathsea kpse,const_string path,const_string fallback)32 kpathsea_expand_default (kpathsea kpse, const_string path,
33                          const_string fallback)
34 {
35   unsigned path_length;
36   string expansion;
37   (void)kpse; /* currenty not used */
38 
39   /* The default path better not be null.  */
40   assert (fallback);
41 
42   if (path == NULL)
43     expansion = xstrdup (fallback);
44 
45   /* Solitary or leading :?  */
46   else if (IS_ENV_SEP (*path))
47     {
48       expansion = path[1] == 0 ? xstrdup (fallback) : concat (fallback, path);
49     }
50 
51   /* Sorry about the assignment in the middle of the expression, but
52      conventions were made to be flouted and all that.  I don't see the
53      point of calling strlen twice or complicating the logic just to
54      avoid the assignment (especially now that I've pointed it out at
55      such great length).  */
56   else if (path[(path_length = strlen (path)) - 1] == ENV_SEP)
57     expansion = concat (path, fallback);
58 
59   /* OK, not leading or trailing.  Check for doubled.  */
60   else
61     {
62       const_string loc;
63 
64       for (loc = path; *loc; loc++)
65         if (IS_ENV_SEP (loc[0]) && IS_ENV_SEP (loc[1]))
66           break;
67       if (*loc)
68         { /* We have a doubled colon.  */
69           expansion = (string)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       else
80         { /* No doubled colon. */
81           expansion = xstrdup(path);
82         }
83     }
84 
85   return expansion;
86 }
87 
88 #ifdef TEST
89 
90 void
test_expand_default(const_string path,const_string def)91 test_expand_default (const_string path, const_string def)
92 {
93   string answer;
94 
95   printf ("Expansion of `%s':\t", path ? path : "(nil)");
96   answer = kpathsea_expand_default (kpse_def, path, def);
97   puts (answer);
98 }
99 
100 int
main()101 main ()
102 {
103   string default_path = "default";
104 
105   test_expand_default (NULL, default_path);
106   test_expand_default ("", default_path);
107   test_expand_default ("none", default_path);
108   test_expand_default (ENV_SEP_STRING, default_path);
109   test_expand_default (ENV_SEP_STRING "first", default_path);
110   test_expand_default ("last" ENV_SEP_STRING, default_path);
111   test_expand_default ("middle" ENV_SEP_STRING ENV_SEP_STRING "elddim",
112                        default_path);
113 
114   return 0;
115 }
116 
117 #endif /* TEST */
118 
119 
120 /*
121 Local variables:
122 standalone-compile-command: "gcc -g -I. -I.. -DTEST kdefault.c kpathsea.a"
123 End:
124 */
125