diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-10-07 20:12:34 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-10-07 20:12:34 +0300 |
commit | 1fbcbdb432e945aa3f63be5b9170e94a0c9ef6c3 (patch) | |
tree | 209822ff7ef7766b94a714438473dcf8f12c4e1f /lib/filemap.c | |
parent | cabb35e68d34bcccaa39a223eaa037b335c81558 (diff) | |
download | eclat-1fbcbdb432e945aa3f63be5b9170e94a0c9ef6c3.tar.gz eclat-1fbcbdb432e945aa3f63be5b9170e94a0c9ef6c3.tar.bz2 |
Implement resource identifier mapping.
Resource identifier mapping permits to use more customized identifiers
instead of the Amazon resource identifiers. For instance, you can use
hostnames instead of the instance IDs, etc.
* lib/Makefile.am: Add new files.
* lib/filemap.c: New file.
* lib/map.c: New file.
* lib/libeclat.h (eclat_map_drv,eclat_map): New structs.
(eclat_map_status): New enum.
(eclat_map_init,eclat_map_lookup,eclat_map_config)
(eclat_map_free,eclat_map_open,eclat_map_close)
(eclat_map_get,eclat_map_strerror,eclat_map_drv_register)
(eclat_map_foreach,eclat_map_free_all)
(eclat_get_string_node): New protos.
(eclat_map_drv_file): New extern.
* src/accfile.c (access_file_lookup): Fix memory leak.
* src/cmdline.opt: New option: --translate (-x)
* src/config.c (eclat_kw) <map>: New block statement.
(config_finish): Parse map configs.
* src/asscaddr.c: Translate key.
* src/dscrinstattr.c: Likewise.
* src/dscrinsts.c: Likewise.
* src/dscrinststat.c: Likewise.
* src/dscrsecgrps.c: Likewise.
* src/getconout.c: Likewise.
* src/startinst.c: Likewise.
* src/eclat.c (main): Initialize maps and free them when no
more needed.
* src/eclat.h (translate_option): New extern.
(translate_ids): New proto.
* src/util.c (translate_option): New variable.
(translate_ids): New function.
Diffstat (limited to 'lib/filemap.c')
-rw-r--r-- | lib/filemap.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/lib/filemap.c b/lib/filemap.c new file mode 100644 index 0000000..bced0d4 --- /dev/null +++ b/lib/filemap.c @@ -0,0 +1,140 @@ +/* This file is part of Eclat. + Copyright (C) 2012 Sergey Poznyakoff. + + Eclat is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Eclat is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Eclat. If not, see <http://www.gnu.org/licenses/>. */ + +#include "libeclat.h" +#include <errno.h> + +struct filemap { + char *name; + FILE *fp; + struct grecs_locus locus; +}; + +static int +filemap_config(int dbg, struct grecs_node *node, void *data) +{ + struct filemap *filemap, **return_filemap = data; + const char *filename; + struct grecs_node *p; + + if (eclat_get_string_node(node, "file", 0, &p)) + return eclat_map_failure; + filemap = grecs_malloc(sizeof(*filemap)); + filemap->name = grecs_strdup(p->v.value->v.string); + filemap->locus = p->locus; + *return_filemap = filemap; + return eclat_map_ok; +} + +static void +filemap_free(int dbg, void *data) +{ + struct filemap *filemap = data; + free(filemap->name); + free(filemap); +} + +static int +filemap_open(int dbg, void *data) +{ + struct filemap *filemap = data; + + filemap->fp = fopen(filemap->name, "r"); + if (!filemap->fp) { + grecs_error(&filemap->locus, errno, "cannot open file"); + return eclat_map_failure; + } + return eclat_map_ok; +} + + +static int +filemap_close(int dbg, void *data) +{ + struct filemap *filemap = data; + fclose(filemap->fp); + return eclat_map_ok; +} + +static void +skipline(FILE *fp) +{ + int c; + + while ((c = getc(fp)) != EOF && c != '\n') + ; +} + +static int +filemap_get(int dbg, void *data, const char *key, char **return_value) +{ + struct filemap *filemap = data; + FILE *fp = filemap->fp; + int line = 0; + int rc, c; + const char *p; + + rewind(fp); + while ((c = getc(fp)) != EOF) { + line++; + while (c != EOF && (c == ' ' || c == '\t')) + c = getc(fp); + if (c == '\n') + continue; + if (c == '#') { + skipline(fp); + continue; + } + if (c == EOF) + break; + + for (p = key; c != EOF && c != ':' && *p == c; p++) + c = getc(fp); + if (c == EOF) + break; + if (c == '\n') + continue; + if (c == ':') + break; + skipline(fp); + } + + if (c == ':') { + struct grecs_txtacc *acc; + + debug(dbg, 2, ("%s:%d: found key", filemap->name, line)); + acc = grecs_txtacc_create(); + while ((c = getc(fp)) != EOF && c != '\n') + grecs_txtacc_grow_char(acc, c); + grecs_txtacc_grow_char(acc, 0); + *return_value = grecs_txtacc_finish(acc, 1); + grecs_txtacc_free(acc); + rc = eclat_map_ok; + } else + rc = eclat_map_not_found; + + return rc; +} + +struct eclat_map_drv eclat_map_drv_file = { + "file", + filemap_config, + filemap_open, + filemap_close, + filemap_get, + filemap_free +}; + |