summaryrefslogtreecommitdiff
path: root/utils/messages.c
diff options
context:
space:
mode:
authorJohn Mark Bell <jmb@netsurf-browser.org>2006-10-12 14:00:40 +0000
committerJohn Mark Bell <jmb@netsurf-browser.org>2006-10-12 14:00:40 +0000
commit2caa96dcc97b7d34c5186214a247874b3355071b (patch)
treef8544dc5f2f4c48c344673f6aa6721b72da65b7b /utils/messages.c
parent50c303284b670449e67b4c2bce36050eac8cc229 (diff)
downloadnetsurf-2caa96dcc97b7d34c5186214a247874b3355071b.tar.gz
netsurf-2caa96dcc97b7d34c5186214a247874b3355071b.tar.bz2
Fix attempts to call die() before messages_hash exists:
1) Make hash_* more robust in the face of bad parameters 2) Make messages_* more robust in the face of bad parameters 3) Tidy up gui_init such that localised messages are loaded at the earliest opportunity svn path=/trunk/netsurf/; revision=2998
Diffstat (limited to 'utils/messages.c')
-rw-r--r--utils/messages.c33
1 files changed, 24 insertions, 9 deletions
diff --git a/utils/messages.c b/utils/messages.c
index a40c2b3bf..e3a8d5c90 100644
--- a/utils/messages.c
+++ b/utils/messages.c
@@ -41,9 +41,11 @@ struct hash_table *messages_load_ctx(const char *path, struct hash_table *ctx)
{
char s[400];
FILE *fp;
-
+
+ assert(path != NULL);
+
ctx = (ctx != NULL) ? ctx : hash_create(HASH_SIZE);
-
+
if (ctx == NULL) {
LOG(("Unable to create hash table for messages file %s", path));
return NULL;
@@ -70,7 +72,7 @@ struct hash_table *messages_load_ctx(const char *path, struct hash_table *ctx)
continue;
*colon = 0; /* terminate key */
value = colon + 1;
-
+
if (hash_add(ctx, s, value) == false) {
LOG(("Unable to add %s:%s to hash table of %s",
s, value, path));
@@ -80,7 +82,7 @@ struct hash_table *messages_load_ctx(const char *path, struct hash_table *ctx)
}
fclose(fp);
-
+
return ctx;
}
@@ -99,15 +101,18 @@ void messages_load(const char *path)
{
struct hash_table *m;
char s[400];
-
+
+ assert(path != NULL);
+
m = messages_load_ctx(path, messages_hash);
if (m == NULL) {
LOG(("Unable to open Messages file '%s'. Possible reason: %s",
path, strerror(errno)));
- snprintf(s, 400, "Unable to open Messages file '%s'.", path);
+ snprintf(s, sizeof s,
+ "Unable to open Messages file '%s'.", path);
die(s);
}
-
+
messages_hash = m;
}
@@ -121,8 +126,18 @@ void messages_load(const char *path)
const char *messages_get_ctx(const char *key, struct hash_table *ctx)
{
- const char *r = hash_get(ctx, key);
-
+ const char *r;
+
+ assert(key != NULL);
+
+ /* If we're called with no context, it's nicer to return the
+ * key rather than explode - this allows attempts to get messages
+ * before messages_hash is set up to fail gracefully, for example */
+ if (ctx == NULL)
+ return key;
+
+ r = hash_get(ctx, key);
+
return r ? r : key;
}