summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2019-10-30 21:26:53 +0000
committerVincent Sanders <vince@kyllikki.org>2019-10-30 21:33:27 +0000
commit4eb06ad2cf2b7025974618be943dea9718d09319 (patch)
tree8f926311aca3505f901bb58ea8ed5106b9ef95db /utils
parent4b0c3f0efef2239dd5d62a9e73eeec4037c056a4 (diff)
downloadnetsurf-4eb06ad2cf2b7025974618be943dea9718d09319.tar.gz
netsurf-4eb06ad2cf2b7025974618be943dea9718d09319.tar.bz2
move the fallback text for about handler into messages handler
Diffstat (limited to 'utils')
-rw-r--r--utils/hashtable.c6
-rw-r--r--utils/messages.c92
-rw-r--r--utils/messages.h16
3 files changed, 85 insertions, 29 deletions
diff --git a/utils/hashtable.c b/utils/hashtable.c
index 4935d6b3f..aa162cbc4 100644
--- a/utils/hashtable.c
+++ b/utils/hashtable.c
@@ -347,10 +347,12 @@ const char *hash_get(struct hash_table *ht, const char *key)
h = hash_string_fnv(key, &key_length);
c = h % ht->nchains;
- for (e = ht->chain[c]; e; e = e->next)
+ for (e = ht->chain[c]; e; e = e->next) {
if ((key_length == e->key_length) &&
- (memcmp(key, e->pairing, key_length) == 0))
+ (memcmp(key, e->pairing, key_length) == 0)) {
return e->pairing + key_length + 1;
+ }
+ }
return NULL;
}
diff --git a/utils/messages.c b/utils/messages.c
index 197d45ea6..5525e18cc 100644
--- a/utils/messages.c
+++ b/utils/messages.c
@@ -42,11 +42,69 @@
/** Messages are stored in a fixed-size hash table. */
#define HASH_SIZE 101
-/** The hash table used to store the standard Messages file for the old API */
+/**
+ * The hash table used to store the standard Messages file for the old API
+ */
static struct hash_table *messages_hash = NULL;
/**
+ * Create a message context
+ *
+ * generate a message context populated with english fallbacks for
+ * some formatted messages.
+ */
+static struct hash_table *messages_create_ctx(int hash_size)
+{
+ struct hash_table *nctx;
+ const struct {
+ const char *key;
+ const char *value;
+ } fallback[] = {
+ { "LoginDescription",
+ "The site %s is requesting your username and password. "
+ "The realm is \"%s\""},
+ { "PrivacyDescription",
+ "A privacy error occurred while communicating with %s this "
+ "may be a site configuration error or an attempt to steal "
+ "private information (passwords, messages or credit cards)"},
+ { "TimeoutDescription",
+ "A connection to %s could not be established. The site may "
+ "be temporarily unavailable or too busy to respond."},
+ { "FetchErrorDescription",
+ "An error occurred when connecting to %s"},
+ { NULL, NULL}
+ };
+ nctx = hash_create(hash_size);
+
+ if (nctx != NULL) {
+ int floop;
+ for (floop = 0; fallback[floop].key != NULL; floop++) {
+ hash_add(nctx,
+ fallback[floop].key,
+ fallback[floop].value);
+ }
+ }
+
+ return nctx;
+}
+
+/**
+ * Free memory used by a messages hash.
+ * The context will not be valid after this function returns.
+ *
+ * \param ctx context of messages file to free
+ */
+static void messages_destroy_ctx(struct hash_table *ctx)
+{
+ if (ctx == NULL)
+ return;
+
+ hash_destroy(ctx);
+}
+
+
+/**
* Read keys and values from messages file.
*
* \param path pathname of messages file
@@ -66,7 +124,7 @@ static nserror messages_load_ctx(const char *path, struct hash_table **ctx)
return hash_add_file(*ctx, path);
}
- nctx = hash_create(HASH_SIZE);
+ nctx = messages_create_ctx(HASH_SIZE);
if (nctx == NULL) {
NSLOG(netsurf, INFO,
"Unable to create hash table for messages file %s",
@@ -115,21 +173,6 @@ messages_get_ctx(const char *key, struct hash_table *ctx)
}
-/**
- * Free memory used by a messages hash.
- * The context will not be valid after this function returns.
- *
- * \param ctx context of messages file to free
- */
-static void messages_destroy_ctx(struct hash_table *ctx)
-{
- if (ctx == NULL)
- return;
-
- hash_destroy(ctx);
-}
-
-
/* exported interface documented in messages.h */
nserror messages_add_from_file(const char *path)
{
@@ -148,7 +191,7 @@ nserror messages_add_from_inline(const uint8_t *data, size_t size)
{
/* ensure the hash table is initialised */
if (messages_hash == NULL) {
- messages_hash = hash_create(HASH_SIZE);
+ messages_hash = messages_create_ctx(HASH_SIZE);
}
if (messages_hash == NULL) {
NSLOG(netsurf, INFO, "Unable to create hash table");
@@ -157,6 +200,7 @@ nserror messages_add_from_inline(const uint8_t *data, size_t size)
return hash_add_inline(messages_hash, data, size);
}
+
/* exported interface documented in messages.h */
char *messages_get_buff(const char *key, ...)
{
@@ -165,7 +209,17 @@ char *messages_get_buff(const char *key, ...)
int buff_len = 0;
va_list ap;
- msg_fmt = messages_get_ctx(key, messages_hash);
+ assert(key != NULL);
+
+ if (messages_hash == NULL) {
+ return NULL;
+ }
+
+ msg_fmt = hash_get(messages_hash, key);
+
+ if (msg_fmt == NULL) {
+ return NULL;
+ }
va_start(ap, key);
buff_len = vsnprintf(buff, buff_len, msg_fmt, ap);
diff --git a/utils/messages.h b/utils/messages.h
index 635d6e8e4..5da35e4ad 100644
--- a/utils/messages.h
+++ b/utils/messages.h
@@ -16,7 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-/** \file
+/**
+ * \file
* Localised message support (interface).
*
* The messages module loads a file of keys and associated strings, and
@@ -30,8 +31,8 @@
* file table. Use the _ctx versions of the functions to do this.
*/
-#ifndef _NETSURF_UTILS_MESSAGES_H_
-#define _NETSURF_UTILS_MESSAGES_H_
+#ifndef NETSURF_UTILS_MESSAGES_H_
+#define NETSURF_UTILS_MESSAGES_H_
#include <stdint.h>
@@ -90,13 +91,12 @@ const char *messages_get_sslcode(ssl_cert_err code);
/**
* Formatted message from a key in the global message hash.
*
- * \param key key of message
+ * \param key key of message
* \param ... message parameters
- * \return buffer containing formatted message text or NULL if memory
- * is unavailable. The caller owns the returned buffer and is
- * responsible for freeing it.
+ * \return buffer containing formatted message text or NULL if key is
+ * unavailable or memory allocation failed. The caller owns the
+ * returned buffer and is responsible for freeing it.
*/
-
char *messages_get_buff(const char *key, ...);
/**