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) | |||
1068 | env->retval.v.string = buf; | 1068 | env->retval.v.string = buf; |
1069 | } | 1069 | } |
1070 | 1070 | ||
1071 | static void | ||
1072 | func_lookup(forlan_eval_env_t env, struct grecs_list *list) | ||
1073 | { | ||
1074 | struct forlan_value *p; | ||
1075 | const char *key; | ||
1076 | char *mapname; | ||
1077 | struct eclat_map *map; | ||
1078 | int dir; | ||
1079 | char *q; | ||
1080 | |||
1081 | p = list->head->data; | ||
1082 | |||
1083 | dir = eclat_map_name_split(p->v.string, &mapname, &q); | ||
1084 | if (dir == -1) | ||
1085 | die(EX_USAGE, "map %s: bad qualifier %s", p->v.string, q); | ||
1086 | |||
1087 | map = eclat_map_lookup(mapname); | ||
1088 | free(mapname); | ||
1089 | |||
1090 | p = list->head->next->data; | ||
1091 | key = p->v.string; | ||
1092 | |||
1093 | if (map) { | ||
1094 | if (eclat_map_open(map) == eclat_map_ok && | ||
1095 | eclat_map_get(map, dir, key, &env->retval.v.string) | ||
1096 | == eclat_map_ok) | ||
1097 | return; | ||
1098 | } else | ||
1099 | err("no such map: %s", mapname); | ||
1071 | 1100 | ||
1101 | env->retval.v.string = grecs_strdup(key); | ||
1102 | } | ||
1103 | |||
1104 | static void | ||
1105 | func_has_map(forlan_eval_env_t env, struct grecs_list *list) | ||
1106 | { | ||
1107 | struct forlan_value *p; | ||
1108 | struct eclat_map *map; | ||
1109 | |||
1110 | p = list->head->data; | ||
1111 | map = eclat_map_lookup(p->v.string); | ||
1112 | env->retval.v.num = map && eclat_map_open(map) == eclat_map_ok; | ||
1113 | } | ||
1114 | |||
1072 | static struct forlan_function functab[] = { | 1115 | static struct forlan_function functab[] = { |
1073 | { "dump", forlan_value_void, "n", 1, 1, func_dump }, | 1116 | { "dump", forlan_value_void, "n", 1, 1, func_dump }, |
1074 | { "print", forlan_value_void, "", 1, -1, func_print }, | 1117 | { "print", forlan_value_void, "", 1, -1, func_print }, |
@@ -1079,6 +1122,8 @@ static struct forlan_function functab[] = { | |||
1079 | { "exit", forlan_value_void, "s", 1, 1, func_exit }, | 1122 | { "exit", forlan_value_void, "s", 1, 1, func_exit }, |
1080 | { "empty", forlan_value_boolean, "n", 1, 1, func_empty }, | 1123 | { "empty", forlan_value_boolean, "n", 1, 1, func_empty }, |
1081 | { "timestamp", forlan_value_literal, "s", 1, 1, func_timestamp }, | 1124 | { "timestamp", forlan_value_literal, "s", 1, 1, func_timestamp }, |
1125 | { "lookup", forlan_value_literal, "ss", 2, 2, func_lookup }, | ||
1126 | { "has_map",forlan_value_boolean, "s", 1, 1, func_has_map }, | ||
1082 | { NULL } | 1127 | { NULL } |
1083 | }; | 1128 | }; |
1084 | 1129 | ||
@@ -206,7 +206,6 @@ eclat_map_open(struct eclat_map *map) | |||
206 | } | 206 | } |
207 | 207 | ||
208 | if (map->drv->map_open(map_dbg, map->data)) { | 208 | if (map->drv->map_open(map_dbg, map->data)) { |
209 | err("failed to open map %s", map->name); | ||
210 | return eclat_map_failure; | 209 | return eclat_map_failure; |
211 | } | 210 | } |
212 | 211 | ||
@@ -42,10 +42,11 @@ translate_ids(int argc, char **argv, const char *mapname) | |||
42 | map = eclat_map_lookup(realname); | 42 | map = eclat_map_lookup(realname); |
43 | if (!map) | 43 | if (!map) |
44 | die(EX_UNAVAILABLE, "no such map: %s", realname); | 44 | die(EX_UNAVAILABLE, "no such map: %s", realname); |
45 | free(realname); | ||
46 | 45 | ||
47 | if (eclat_map_open(map) != eclat_map_ok) | 46 | if (eclat_map_open(map) != eclat_map_ok) |
48 | exit(EX_UNAVAILABLE); | 47 | die(EX_UNAVAILABLE, "failed to open map %s", realname); |
48 | |||
49 | free(realname); | ||
49 | 50 | ||
50 | for (i = 0; i < argc; i++) { | 51 | for (i = 0; i < argc; i++) { |
51 | if (!strchr(argv[i], '=')) { | 52 | if (!strchr(argv[i], '=')) { |
@@ -101,7 +102,8 @@ translate_resource_ids(int argc, char **argv) | |||
101 | die(EX_UNAVAILABLE, "no such map: %s", | 102 | die(EX_UNAVAILABLE, "no such map: %s", |
102 | ws.ws_wordv[j]); | 103 | ws.ws_wordv[j]); |
103 | if (eclat_map_open(map) != eclat_map_ok) | 104 | if (eclat_map_open(map) != eclat_map_ok) |
104 | exit(EX_UNAVAILABLE); | 105 | die(EX_UNAVAILABLE, |
106 | "failed to open map %s", ws.ws_wordv[j]); | ||
105 | rc = eclat_map_get(map, MAP_DIR, p, &val); | 107 | rc = eclat_map_get(map, MAP_DIR, p, &val); |
106 | if (rc != eclat_map_ok) { | 108 | if (rc != eclat_map_ok) { |
107 | die(EX_UNAVAILABLE, | 109 | die(EX_UNAVAILABLE, |