summaryrefslogtreecommitdiff
path: root/src/driver
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2015-12-29 04:48:00 -0500
committermidipix <writeonce@midipix.org>2016-01-01 22:50:23 -0500
commit383aa6559ee06236b7600df8df17d7f3ab254ca3 (patch)
tree1745e2888f440a7f56d54e29ce4a37a486d9d272 /src/driver
parentd764f2cab0ed73a95f3574f7974ec53a516f3c0d (diff)
downloadapimagic-383aa6559ee06236b7600df8df17d7f3ab254ca3.tar.bz2
apimagic-383aa6559ee06236b7600df8df17d7f3ab254ca3.tar.xz
created skeleton.
Diffstat (limited to 'src/driver')
-rw-r--r--src/driver/amgc_driver_ctx.c183
-rw-r--r--src/driver/amgc_unit_ctx.c62
2 files changed, 245 insertions, 0 deletions
diff --git a/src/driver/amgc_driver_ctx.c b/src/driver/amgc_driver_ctx.c
new file mode 100644
index 0000000..65f5b30
--- /dev/null
+++ b/src/driver/amgc_driver_ctx.c
@@ -0,0 +1,183 @@
+/**********************************************************/
+/* apimagic: cparser-based API normalization utility */
+/* Copyright (C) 2015--2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
+/**********************************************************/
+
+#include <stdint.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#define ARGV_DRIVER
+
+#include <apimagic/apimagic.h>
+#include "apimagic_driver_impl.h"
+#include "argv/argv.h"
+
+extern const struct argv_option amgc_default_options[];
+
+struct amgc_driver_ctx_alloc {
+ struct argv_meta * meta;
+ struct amgc_driver_ctx_impl ctx;
+ uint64_t guard;
+ const char * units[];
+};
+
+static uint32_t amgc_argv_flags(uint32_t flags)
+{
+ uint32_t ret = 0;
+
+ if (flags & AMGC_DRIVER_VERBOSITY_NONE)
+ ret |= ARGV_VERBOSITY_NONE;
+
+ if (flags & AMGC_DRIVER_VERBOSITY_ERRORS)
+ ret |= ARGV_VERBOSITY_ERRORS;
+
+ if (flags & AMGC_DRIVER_VERBOSITY_STATUS)
+ ret |= ARGV_VERBOSITY_STATUS;
+
+ return ret;
+}
+
+static int amgc_driver_usage(
+ const char * program,
+ const char * arg,
+ const struct argv_option * options,
+ struct argv_meta * meta)
+{
+ char header[512];
+
+ snprintf(header,sizeof(header),
+ "Usage: %s [options] <file>...\n" "Options:\n",
+ program);
+
+ argv_usage(stdout,header,options,arg);
+ argv_free(meta);
+
+ return AMGC_USAGE;
+}
+
+static struct amgc_driver_ctx_impl * amgc_driver_ctx_alloc(
+ struct argv_meta * meta,
+ const struct amgc_common_ctx * cctx,
+ size_t nunits)
+{
+ struct amgc_driver_ctx_alloc * ictx;
+ size_t size;
+ struct argv_entry * entry;
+ const char ** units;
+
+ size = sizeof(struct amgc_driver_ctx_alloc);
+ size += (nunits+1)*sizeof(const char *);
+
+ if (!(ictx = calloc(size,1)))
+ return 0;
+
+ if (cctx)
+ memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx));
+
+ for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++)
+ if (!entry->fopt)
+ *units++ = entry->arg;
+
+ ictx->meta = meta;
+ ictx->ctx.ctx.units = ictx->units;
+ return &ictx->ctx;
+}
+
+static int amgc_get_driver_ctx_fail(struct argv_meta * meta)
+{
+ argv_free(meta);
+ return -1;
+}
+
+int amgc_get_driver_ctx(
+ const char ** argv,
+ const char ** envp,
+ uint32_t flags,
+ struct amgc_driver_ctx ** pctx)
+{
+ struct amgc_driver_ctx_impl * ctx;
+ struct amgc_common_ctx cctx;
+ const struct argv_option * options;
+ struct argv_meta * meta;
+ struct argv_entry * entry;
+ size_t nunits;
+ const char * program;
+
+ options = amgc_default_options;
+
+ if (!(meta = argv_get(argv,options,amgc_argv_flags(flags))))
+ return -1;
+
+ nunits = 0;
+ program = argv_program_name(argv[0]);
+ memset(&cctx,0,sizeof(cctx));
+
+ if (!argv[1] && (flags & AMGC_DRIVER_VERBOSITY_USAGE))
+ return amgc_driver_usage(program,0,options,meta);
+
+ /* get options, count units */
+ for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
+ if (entry->fopt) {
+ switch (entry->tag) {
+ case TAG_HELP:
+ if (flags & AMGC_DRIVER_VERBOSITY_USAGE)
+ return amgc_driver_usage(program,entry->arg,options,meta);
+
+ case TAG_VERSION:
+ cctx.drvflags |= AMGC_DRIVER_VERSION;
+ break;
+ }
+ } else
+ nunits++;
+ }
+
+ if (!(ctx = amgc_driver_ctx_alloc(meta,&cctx,nunits)))
+ return amgc_get_driver_ctx_fail(meta);
+
+ ctx->ctx.program = program;
+ ctx->ctx.cctx = &ctx->cctx;
+
+ *pctx = &ctx->ctx;
+ return AMGC_OK;
+}
+
+int amgc_create_driver_ctx(
+ const struct amgc_common_ctx * cctx,
+ struct amgc_driver_ctx ** pctx)
+{
+ struct argv_meta * meta;
+ struct amgc_driver_ctx_impl * ctx;
+ const char * argv[] = {"apimagic_driver",0};
+
+ if (!(meta = argv_get(argv,amgc_default_options,0)))
+ return -1;
+
+ if (!(ctx = amgc_driver_ctx_alloc(meta,cctx,0)))
+ return amgc_get_driver_ctx_fail(0);
+
+ ctx->ctx.cctx = &ctx->cctx;
+ memcpy(&ctx->cctx,cctx,sizeof(*cctx));
+ *pctx = &ctx->ctx;
+ return AMGC_OK;
+}
+
+static void amgc_free_driver_ctx_impl(struct amgc_driver_ctx_alloc * ictx)
+{
+ argv_free(ictx->meta);
+ free(ictx);
+}
+
+void amgc_free_driver_ctx(struct amgc_driver_ctx * ctx)
+{
+ struct amgc_driver_ctx_alloc * ictx;
+ uintptr_t addr;
+
+ if (ctx) {
+ addr = (uintptr_t)ctx - offsetof(struct amgc_driver_ctx_alloc,ctx);
+ addr = addr - offsetof(struct amgc_driver_ctx_impl,ctx);
+ ictx = (struct amgc_driver_ctx_alloc *)addr;
+ amgc_free_driver_ctx_impl(ictx);
+ }
+}
diff --git a/src/driver/amgc_unit_ctx.c b/src/driver/amgc_unit_ctx.c
new file mode 100644
index 0000000..d2268fd
--- /dev/null
+++ b/src/driver/amgc_unit_ctx.c
@@ -0,0 +1,62 @@
+/**********************************************************/
+/* apimagic: cparser-based API normalization utility */
+/* Copyright (C) 2015--2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
+/**********************************************************/
+
+#include <stdint.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+
+#include <apimagic/apimagic.h>
+#include "apimagic_driver_impl.h"
+
+static int amgc_free_unit_ctx_impl(struct amgc_unit_ctx_impl * ctx, int status)
+{
+ if (ctx) {
+ amgc_unmap_input(&ctx->map);
+ free(ctx);
+ }
+
+ return status;
+}
+
+int amgc_get_unit_ctx(
+ const struct amgc_driver_ctx * dctx,
+ const char * path,
+ struct amgc_unit_ctx ** pctx)
+{
+ struct amgc_unit_ctx_impl * ctx;
+
+ if (!dctx || !(ctx = calloc(sizeof(*ctx),1)))
+ return -1;
+
+ if (amgc_map_input(-1,path,PROT_READ,&ctx->map))
+ return amgc_free_unit_ctx_impl(ctx,-1);
+
+ memcpy(&ctx->cctx,dctx->cctx,
+ sizeof(ctx->cctx));
+
+ ctx->path = path;
+
+ ctx->uctx.path = &ctx->path;
+ ctx->uctx.map = &ctx->map;
+ ctx->uctx.cctx = &ctx->cctx;
+
+ *pctx = &ctx->uctx;
+ return 0;
+}
+
+void amgc_free_unit_ctx(struct amgc_unit_ctx * ctx)
+{
+ struct amgc_unit_ctx_impl * ictx;
+ uintptr_t addr;
+
+ if (ctx) {
+ addr = (uintptr_t)ctx - offsetof(struct amgc_unit_ctx_impl,uctx);
+ ictx = (struct amgc_unit_ctx_impl *)addr;
+ amgc_free_unit_ctx_impl(ictx,0);
+ }
+}