summaryrefslogtreecommitdiff
path: root/Build
diff options
context:
space:
mode:
authorPeter Breitenlohner <peb@mppmu.mpg.de>2010-02-15 09:27:07 +0000
committerPeter Breitenlohner <peb@mppmu.mpg.de>2010-02-15 09:27:07 +0000
commit05d7395ed18ddec8c4a339adb64708ba7a8c52d0 (patch)
treeac33c1c0c13e30a2ee53db5eb55f5768d6575ce5 /Build
parent46a755637a029e18140bc635485f25020c276ef9 (diff)
somewhat reorganize libkpathsea internals
git-svn-id: svn://tug.org/texlive/trunk@17037 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build')
-rw-r--r--Build/source/texk/kpathsea/ChangeLog13
-rw-r--r--Build/source/texk/kpathsea/hash.c35
-rw-r--r--Build/source/texk/kpathsea/hash.h2
-rw-r--r--Build/source/texk/kpathsea/progname.c6
-rw-r--r--Build/source/texk/kpathsea/tilde.c34
-rw-r--r--Build/source/texk/kpathsea/tilde.h6
-rw-r--r--Build/source/texk/kpathsea/variable.c67
7 files changed, 72 insertions, 91 deletions
diff --git a/Build/source/texk/kpathsea/ChangeLog b/Build/source/texk/kpathsea/ChangeLog
index 0ecbbcf1267..3389509816f 100644
--- a/Build/source/texk/kpathsea/ChangeLog
+++ b/Build/source/texk/kpathsea/ChangeLog
@@ -8,6 +8,19 @@
(<kpathsea/c-pathmx.h>) [__DJGPP__]: change conditional from DJGPP.
Suggestion from Javier Mugica, 13 Feb 2010 20:46:12.
+2010-02-14 Peter Breitenlohner <peb@mppmu.mpg.de>
+
+ * variable.c (kpathsea_var_value, expand): Treat strings
+ returned by getenv() and kpathsea_cnf_get() as const.
+ Replace kpathsea_var_expand() plus kpathsea_tilde_expand() by
+ kpathsea_expand() and include expand.h instead of tilde.h.
+
+ * tilde.[ch] (kpathsea_tilde_expand): Now used only in expand.c
+ by kpathsea_expand(). Declare string param as non-const.
+ (kpse_tilde_expand): Removed.
+
+ * hash.[ch]: Adjust comments and remove trailing whitespace.
+
2010-02-13 Peter Breitenlohner <peb@mppmu.mpg.de>
* xopendir.c [WIN32]: Define neither xopendir nor xclosedir.
diff --git a/Build/source/texk/kpathsea/hash.c b/Build/source/texk/kpathsea/hash.c
index f67b71bb230..b3efad0fb4c 100644
--- a/Build/source/texk/kpathsea/hash.c
+++ b/Build/source/texk/kpathsea/hash.c
@@ -41,12 +41,12 @@ static unsigned
hash (hash_table_type table, const_string key)
{
unsigned n = 0;
-
+
/* Our keys aren't often anagrams of each other, so no point in
weighting the characters. */
while (*key != 0)
n = (n + n + TRANSFORM (*key++)) % table.size;
-
+
return n;
}
@@ -55,35 +55,35 @@ static unsigned
hash_normalized (hash_table_type table, const_string key)
{
unsigned n = 0;
-
+
/* Our keys aren't often anagrams of each other, so no point in
weighting the characters. */
while (*key != 0)
n = (n + n + (*key++)) % table.size;
-
+
return n;
}
hash_table_type
-hash_create (unsigned size)
+hash_create (unsigned size)
{
/* The was "static ..." since Oct3, 1997 to work around a gcc
optimizer bug for Alpha. That particular optimization bug
should be gone by now (Mar4, 2009).
*/
- hash_table_type ret;
+ hash_table_type ret;
unsigned b;
ret.buckets = XTALLOC (size, hash_element_type *);
ret.size = size;
-
+
/* calloc's zeroes aren't necessarily NULL, so be safe. */
for (b = 0; b <ret.size; b++)
ret.buckets[b] = NULL;
-
+
return ret;
}
-/* Whether or not KEY is already in MAP, insert it and VALUE. Do not
+/* Whether or not KEY is already in TABLE, insert it and VALUE. Do not
duplicate the strings, in case they're being purposefully shared. */
void
@@ -97,7 +97,7 @@ hash_insert (hash_table_type *table,
new_elt->key = key;
new_elt->value = value;
new_elt->next = NULL;
-
+
/* Insert the new element at the end of the list. */
if (!table->buckets[n])
/* first element in bucket is a special case. */
@@ -123,7 +123,7 @@ hash_insert_normalized (hash_table_type *table,
new_elt->key = key;
new_elt->value = value;
new_elt->next = NULL;
-
+
/* Insert the new element at the end of the list. */
if (!table->buckets[n])
/* first element in bucket is a special case. */
@@ -159,8 +159,8 @@ hash_remove (hash_table_type *table, const_string key,
}
}
-/* Look up STR in MAP. Return a (dynamically-allocated) list of the
- corresponding strings or NULL if no match. */
+/* Look up KEY in TABLE, and return NULL-terminated list of all matching
+ values (not copies), in insertion order. If none, return NULL. */
string *
hash_lookup (hash_table_type table, const_string key)
@@ -169,13 +169,13 @@ hash_lookup (hash_table_type table, const_string key)
str_list_type ret;
unsigned n = hash (table, key);
ret = str_list_init ();
-
+
/* Look at everything in this bucket. */
for (p = table.buckets[n]; p != NULL; p = p->next)
if (FILESTRCASEEQ (key, p->key))
/* Cast because the general str_list_type shouldn't force const data. */
str_list_add (&ret, (string) p->value);
-
+
/* If we found anything, mark end of list with null. */
if (STR_LIST (ret))
str_list_add (&ret, NULL);
@@ -218,7 +218,7 @@ hash_print (hash_table_type table, boolean summary_only)
{
unsigned b;
unsigned total_elements = 0, total_buckets = 0;
-
+
for (b = 0; b < table.size; b++) {
hash_element_type *bucket = table.buckets[b];
@@ -241,7 +241,7 @@ hash_print (hash_table_type table, boolean summary_only)
}
}
}
-
+
fprintf (stderr,
"%u buckets, %u nonempty (%u%%); %u entries, average chain %.1f.\n",
table.size,
@@ -264,4 +264,3 @@ hash_free (hash_table_type table)
p = q;
}
}
-
diff --git a/Build/source/texk/kpathsea/hash.h b/Build/source/texk/kpathsea/hash.h
index f9702b3a794..2945df6c01c 100644
--- a/Build/source/texk/kpathsea/hash.h
+++ b/Build/source/texk/kpathsea/hash.h
@@ -59,7 +59,7 @@ extern KPSEDLL void hash_insert_normalized (hash_table_type *table,
extern KPSEDLL void hash_remove (hash_table_type *table, const_string key,
const_string value);
-/* Look up KEY in MAP, and return NULL-terminated list of all matching
+/* Look up KEY in TABLE, and return NULL-terminated list of all matching
values (not copies), in insertion order. If none, return NULL. */
extern KPSEDLL string *hash_lookup (hash_table_type table, const_string key);
diff --git a/Build/source/texk/kpathsea/progname.c b/Build/source/texk/kpathsea/progname.c
index 809b86e7704..e5cf8c937bf 100644
--- a/Build/source/texk/kpathsea/progname.c
+++ b/Build/source/texk/kpathsea/progname.c
@@ -488,11 +488,7 @@ kpathsea_set_program_name (kpathsea kpse, const_string argv0,
#if defined(WIN32)
/* Set various info about user. Among many things,
- ensure that HOME is set. If debug_paths is on,
- turn on some message if $HOME is not found. */
- if (KPATHSEA_DEBUG_P (KPSE_DEBUG_PATHS)) {
- set_home_warning();
- }
+ ensure that HOME is set. */
init_user_info();
/* redirect stderr to debug_output. Easier to send logfiles. */
diff --git a/Build/source/texk/kpathsea/tilde.c b/Build/source/texk/kpathsea/tilde.c
index 12e71434fd5..5d4c6274b0e 100644
--- a/Build/source/texk/kpathsea/tilde.c
+++ b/Build/source/texk/kpathsea/tilde.c
@@ -36,12 +36,12 @@
<pwd.h>, just return NAME. */
string
-kpathsea_tilde_expand (kpathsea kpse, const_string name)
+kpathsea_tilde_expand (kpathsea kpse, string name)
{
- const_string expansion;
+ string expansion;
const_string home;
const_string prefix;
-
+
(void)kpse; /* currenty not used */
assert (name);
@@ -55,13 +55,13 @@ kpathsea_tilde_expand (kpathsea kpse, const_string name)
} else {
prefix = "";
}
-
+
/* If no leading tilde, do nothing, and return the original string. */
if (*name != '~') {
if (*prefix)
name -= 2;
expansion = name;
-
+
/* If a bare tilde, return the home directory or `.'. (Very unlikely
that the directory name will do anyone any good, but ... */
} else if (name[1] == 0) {
@@ -70,7 +70,7 @@ kpathsea_tilde_expand (kpathsea kpse, const_string name)
home = ".";
}
expansion = concat (prefix, home);
-
+
/* If `~/', remove any trailing / or replace leading // in $HOME.
Should really check for doubled intermediate slashes, too. */
} else if (IS_DIR_SEP (name[1])) {
@@ -86,7 +86,7 @@ kpathsea_tilde_expand (kpathsea kpse, const_string name)
c++;
}
expansion = concat3 (prefix, home, name + c);
-
+
/* If `~user' or `~user/', look up user in the passwd database (but
OS/2 doesn't have this concept. */
} else {
@@ -96,11 +96,11 @@ kpathsea_tilde_expand (kpathsea kpse, const_string name)
unsigned c = 2;
while (!IS_DIR_SEP (name[c]) && name[c] != 0) /* find user name */
c++;
-
+
user = (string) xmalloc (c);
strncpy (user, name + 1, c - 1);
user[c - 1] = 0;
-
+
/* We only need the cast here for (deficient) systems
which do not declare `getpwnam' in <pwd.h>. */
p = (struct passwd *) getpwnam (user);
@@ -125,16 +125,8 @@ kpathsea_tilde_expand (kpathsea kpse, const_string name)
}
/* We may return the same thing as the original, and then we might not
be returning a malloc-ed string. Callers beware. Sorry. */
- return (string) expansion;
+ return expansion;
}
-
-#if defined (KPSE_COMPAT_API)
-string
-kpse_tilde_expand (const_string name)
-{
- return kpathsea_tilde_expand (kpse_def, name);
-}
-#endif
#ifdef TEST
@@ -142,9 +134,9 @@ void
test_expand_tilde (const_string filename)
{
string answer;
-
+
printf ("Tilde expansion of `%s':\t", filename ? filename : "(nil)");
- answer = kpse_tilde_expand (filename);
+ answer = kpathsea_tilde_expand (kpse_def, (string)filename);
puts (answer);
}
@@ -164,7 +156,7 @@ main (int argc, char **argv)
test_expand_tilde ("!!~root");
test_expand_tilde ("!!~");
test_expand_tilde ("!!foo~bar");
-
+
return 0;
}
diff --git a/Build/source/texk/kpathsea/tilde.h b/Build/source/texk/kpathsea/tilde.h
index b769892564b..de95bb31bc6 100644
--- a/Build/source/texk/kpathsea/tilde.h
+++ b/Build/source/texk/kpathsea/tilde.h
@@ -27,11 +27,7 @@
getenv ('USERPROFILE") on Windows, or with NAME's home directory,
respectively. Excise trailing slashes. FILENAME may not be null. */
-extern string kpathsea_tilde_expand (kpathsea kpse, const_string filename);
-
-#if defined (KPSE_COMPAT_API)
-extern string kpse_tilde_expand (const_string filename);
-#endif
+extern string kpathsea_tilde_expand (kpathsea kpse, string filename);
#endif /* MAKE_KPSE_DLL */
diff --git a/Build/source/texk/kpathsea/variable.c b/Build/source/texk/kpathsea/variable.c
index 4495eaa908a..535c7581b38 100644
--- a/Build/source/texk/kpathsea/variable.c
+++ b/Build/source/texk/kpathsea/variable.c
@@ -12,7 +12,7 @@
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
-
+
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, see <http://www.gnu.org/licenses/>. */
@@ -21,7 +21,7 @@
#include <kpathsea/c-ctype.h>
#include <kpathsea/cnf.h>
#include <kpathsea/fn.h>
-#include <kpathsea/tilde.h>
+#include <kpathsea/expand.h>
#include <kpathsea/variable.h>
@@ -31,44 +31,36 @@ string
kpathsea_var_value (kpathsea kpse, const_string var)
{
string vtry, ret;
+ const_string value;
assert (kpse->program_name);
/* First look for VAR.progname. */
vtry = concat3 (var, ".", kpse->program_name);
- ret = getenv (vtry);
+ value = getenv (vtry);
free (vtry);
- if (!ret || !*ret) {
+ if (!value || !*value) {
/* Now look for VAR_progname. */
vtry = concat3 (var, "_", kpse->program_name);
- ret = getenv (vtry);
+ value = getenv (vtry);
free (vtry);
}
/* Just plain VAR. */
- if (!ret || !*ret)
- ret = getenv (var);
+ if (!value || !*value)
+ value = getenv (var);
/* Not in the environment; check a config file. */
- if (!ret || !*ret)
- ret = kpathsea_cnf_get (kpse, var);
+ if (!value || !*value)
+ value = kpathsea_cnf_get (kpse, var);
/* We have a value; do variable and tilde expansion. We want to use ~
in the cnf files, to adapt nicely to Windows and to avoid extra /'s
(see tilde.c), but we also want kpsewhich -var-value=foo to not
have any literal ~ characters, so our shell scripts don't have to
worry about doing the ~ expansion. */
- if (ret) {
- string tmp = kpathsea_var_expand (kpse, ret);
- /* We don't want to free the previous value of ret here; apparently
- it's used later, somewhere, somehow. (The end result was a crash
- when making tex.fmt.) Sigh. */
- ret = kpathsea_tilde_expand (kpse, tmp);
- if (ret != tmp) {
- free (tmp);
- }
- }
+ ret = value ? kpathsea_expand (kpse, value) : NULL;
#ifdef KPSE_DEBUG
if (KPATHSEA_DEBUG_P (KPSE_DEBUG_VARS))
@@ -122,7 +114,7 @@ expanding_p (kpathsea kpse, const_string var)
if (STREQ (kpse->expansions[e].var, var))
return kpse->expansions[e].expanding;
}
-
+
return false;
}
@@ -136,12 +128,12 @@ expand (kpathsea kpse, fn_type *expansion,
const_string start, const_string end)
{
boolean ret = false;
- string value;
+ const_string value;
unsigned len = end - start + 1;
string var = (string)xmalloc (len + 1);
strncpy (var, start, len);
var[len] = 0;
-
+
if (expanding_p (kpse, var)) {
WARNING1 ("kpathsea: variable `%s' references itself (eventually)", var);
} else {
@@ -149,7 +141,7 @@ expand (kpathsea kpse, fn_type *expansion,
/* Check for an environment variable. */
value = getenv (vtry);
free (vtry);
-
+
if (!value || !*value)
value = getenv (var);
@@ -158,25 +150,18 @@ expand (kpathsea kpse, fn_type *expansion,
value = kpathsea_cnf_get (kpse, var);
if (value) {
+ string tmp;
ret = true;
expanding (kpse, var, true);
- value = kpathsea_var_expand (kpse, value);
+ tmp = kpathsea_expand (kpse, value);
expanding (kpse, var, false);
-
- { /* Do tilde expansion; see explanation above in kpse_var_value. */
- string tmp = kpathsea_tilde_expand (kpse, value);
- if (value != tmp) {
- free (value);
- value = tmp;
- }
- }
-
- fn_grow (expansion, value, strlen (value));
- free (value);
- }
- free (var);
+ fn_grow (expansion, tmp, strlen (tmp));
+ free (tmp);
+ }
}
+
+ free (var);
return ret;
}
@@ -206,7 +191,7 @@ kpathsea_var_expand (kpathsea kpse, const_string src)
string ret;
fn_type expansion;
expansion = fn_init ();
-
+
/* Copy everything but variable constructs. */
for (s = src; *s; s++) {
if (IS_VAR_START (*s)) {
@@ -247,7 +232,7 @@ kpathsea_var_expand (kpathsea kpse, const_string src)
}
} else {
- /* $<something-else>: warn, but preserve characters; again, so
+ /* $<something-else>: warn, but preserve characters; again, so
filenames containing dollar signs can be read. */
WARNING2 ("%s: Unrecognized variable construct `$%c'", src, *s);
fn_grow (&expansion, s - 1, 2); /* moved past the $ */
@@ -256,7 +241,7 @@ kpathsea_var_expand (kpathsea kpse, const_string src)
fn_1grow (&expansion, *s);
}
fn_1grow (&expansion, 0);
-
+
ret = FN_STRING (expansion);
return ret;
}
@@ -276,7 +261,7 @@ static void
test_var (string test, string right_answer)
{
string result = kpse_var_expand (test);
-
+
printf ("expansion of `%s'\t=> %s", test, result);
if (!STREQ (result, right_answer))
printf (" [should be `%s']", right_answer);