dvitomp fix from Akira
[mplib] / src / texk / kpathsea / kdefault.c
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 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 
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 = xstrdup (fallback);
42
43   /* Solitary or leading :?  */
44   else if (IS_ENV_SEP (*path))
45     {
46       expansion = path[1] == 0 ? xstrdup (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       for (loc = path; *loc; loc++)
63         if (IS_ENV_SEP (loc[0]) && IS_ENV_SEP (loc[1]))
64           break;
65       if (*loc)
66         { /* We have a doubled colon.  */
67           expansion = (string)xmalloc (path_length + strlen(fallback) + 1);
68
69           /* Copy stuff up to and including the first colon.  */
70           strncpy (expansion, path, loc - path + 1);
71           expansion[loc - path + 1] = 0;
72
73           /* Copy in FALLBACK, and then the rest of PATH.  */
74           strcat (expansion, fallback);
75           strcat (expansion, loc + 1);
76         }
77       else
78         { /* No doubled colon. */
79           expansion = xstrdup(path);
80         }
81     }
82   
83   return expansion;
84 }
85 \f
86 #ifdef TEST
87
88 void
89 test_expand_default (const_string path, const_string def)
90 {
91   string answer;
92   
93   printf ("Expansion of `%s':\t", path ? path : "(nil)");
94   answer = kpse_expand_default (path, def);
95   puts (answer);
96 }
97
98 int
99 main ()
100 {
101   string default_path = "default";
102
103   test_expand_default (NULL, default_path);
104   test_expand_default ("", default_path);
105   test_expand_default ("none", default_path);
106   test_expand_default (ENV_SEP_STRING, default_path);
107   test_expand_default (ENV_SEP_STRING "first", default_path);
108   test_expand_default ("last" ENV_SEP_STRING, default_path);
109   test_expand_default ("middle" ENV_SEP_STRING ENV_SEP_STRING "elddim", default_path);
110   
111   return 0;
112 }
113
114 #endif /* TEST */
115
116 \f
117 /*
118 Local variables:
119 standalone-compile-command: "gcc -g -I. -I.. -DTEST default.c kpathsea.a"
120 End:
121 */