diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-12-15 21:40:26 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-12-15 21:40:26 +0200 |
commit | 42015f0462e8f1b2292a48455ce740a627c14025 (patch) | |
tree | 78352fb16802115d5f16470e6aea1419c563f090 | |
parent | e60288d595ec405c5d416e5da54b516e65f8a8c5 (diff) | |
download | eclat-42015f0462e8f1b2292a48455ce740a627c14025.tar.gz eclat-42015f0462e8f1b2292a48455ce740a627c14025.tar.bz2 |
Implement map lookup functions in forlan.
* lib/forlan.c: New functions "has_map" and "lookup".
* lib/map.c (eclat_map_open): Don't issue error message
if failed to open the map. Leave that to the caller.
* src/util.c (translate_ids)
(translate_resource_ids): Issue an error message if
eclat_map_open fails.
-rw-r--r-- | lib/forlan.c | 45 | ||||
-rw-r--r-- | lib/map.c | 1 | ||||
-rw-r--r-- | src/util.c | 8 |
3 files changed, 50 insertions, 4 deletions
diff --git a/lib/forlan.c b/lib/forlan.c index 828fb09..cd5d08a 100644 --- a/lib/forlan.c +++ b/lib/forlan.c @@ -1068,7 +1068,50 @@ func_timestamp(forlan_eval_env_t env, struct grecs_list *list) env->retval.v.string = buf; } +static void +func_lookup(forlan_eval_env_t env, struct grecs_list *list) +{ + struct forlan_value *p; + const char *key; + char *mapname; + struct eclat_map *map; + int dir; + char *q; + + p = list->head->data; + + dir = eclat_map_name_split(p->v.string, &mapname, &q); + if (dir == -1) + die(EX_USAGE, "map %s: bad qualifier %s", p->v.string, q); + + map = eclat_map_lookup(mapname); + free(mapname); + + p = list->head->next->data; + key = p->v.string; + + if (map) { + if (eclat_map_open(map) == eclat_map_ok && + eclat_map_get(map, dir, key, &env->retval.v.string) + == eclat_map_ok) + return; + } else + err("no such map: %s", mapname); + env->retval.v.string = grecs_strdup(key); +} + +static void +func_has_map(forlan_eval_env_t env, struct grecs_list *list) +{ + struct forlan_value *p; + struct eclat_map *map; + + p = list->head->data; + map = eclat_map_lookup(p->v.string); + env->retval.v.num = map && eclat_map_open(map) == eclat_map_ok; +} + static struct forlan_function functab[] = { { "dump", forlan_value_void, "n", 1, 1, func_dump }, { "print", forlan_value_void, "", 1, -1, func_print }, @@ -1079,6 +1122,8 @@ static struct forlan_function functab[] = { { "exit", forlan_value_void, "s", 1, 1, func_exit }, { "empty", forlan_value_boolean, "n", 1, 1, func_empty }, { "timestamp", forlan_value_literal, "s", 1, 1, func_timestamp }, + { "lookup", forlan_value_literal, "ss", 2, 2, func_lookup }, + { "has_map",forlan_value_boolean, "s", 1, 1, func_has_map }, { NULL } }; @@ -206,7 +206,6 @@ eclat_map_open(struct eclat_map *map) } if (map->drv->map_open(map_dbg, map->data)) { - err("failed to open map %s", map->name); return eclat_map_failure; } @@ -42,10 +42,11 @@ translate_ids(int argc, char **argv, const char *mapname) map = eclat_map_lookup(realname); if (!map) die(EX_UNAVAILABLE, "no such map: %s", realname); - free(realname); if (eclat_map_open(map) != eclat_map_ok) - exit(EX_UNAVAILABLE); + die(EX_UNAVAILABLE, "failed to open map %s", realname); + + free(realname); for (i = 0; i < argc; i++) { if (!strchr(argv[i], '=')) { @@ -101,7 +102,8 @@ translate_resource_ids(int argc, char **argv) die(EX_UNAVAILABLE, "no such map: %s", ws.ws_wordv[j]); if (eclat_map_open(map) != eclat_map_ok) - exit(EX_UNAVAILABLE); + die(EX_UNAVAILABLE, + "failed to open map %s", ws.ws_wordv[j]); rc = eclat_map_get(map, MAP_DIR, p, &val); if (rc != eclat_map_ok) { die(EX_UNAVAILABLE, |