summaryrefslogtreecommitdiffhomepage
path: root/src/driver
diff options
context:
space:
mode:
Diffstat (limited to 'src/driver')
-rw-r--r--src/driver/tbnf_amain.c97
-rw-r--r--src/driver/tbnf_driver_ctx.c254
-rw-r--r--src/driver/tbnf_map_input.c66
-rw-r--r--src/driver/tbnf_unit_ctx.c139
4 files changed, 556 insertions, 0 deletions
diff --git a/src/driver/tbnf_amain.c b/src/driver/tbnf_amain.c
new file mode 100644
index 0000000..62e66bc
--- /dev/null
+++ b/src/driver/tbnf_amain.c
@@ -0,0 +1,97 @@
+/**************************************************************/
+/* treebnf: a tree oriented bnf library */
+/* Copyright (C) 2024 SysDeer Technologies, LLC */
+/* Released under GPLv2 and GPLv3; see COPYING.TREEBNF. */
+/**************************************************************/
+
+#include <stdio.h>
+#include <unistd.h>
+#include <treebnf/treebnf.h>
+#include "treebnf_driver_impl.h"
+#include "treebnf_dprintf_impl.h"
+
+#ifndef TBNF_DRIVER_FLAGS
+#define TBNF_DRIVER_FLAGS TBNF_DRIVER_VERBOSITY_ERRORS \
+ | TBNF_DRIVER_VERBOSITY_USAGE
+#endif
+
+static const char vermsg[] = "%s%s%s (https://git.foss21.org/tulips/treebnf): "
+ "version %s%d.%d.%d%s.\n"
+ "[commit reference: %s%s%s]\n";
+
+static const char * const tbnf_ver_color[6] = {
+ "\x1b[1m\x1b[35m","\x1b[0m",
+ "\x1b[1m\x1b[32m","\x1b[0m",
+ "\x1b[1m\x1b[34m","\x1b[0m"
+};
+
+static const char * const tbnf_ver_plain[6] = {
+ "","",
+ "","",
+ "",""
+};
+
+static ssize_t tbnf_version(struct tbnf_driver_ctx * dctx, int fdout)
+{
+ const struct tbnf_source_version * verinfo;
+ const char * const * verclr;
+
+ verinfo = tbnf_api_source_version();
+ verclr = isatty(fdout) ? tbnf_ver_color : tbnf_ver_plain;
+
+ return tbnf_dprintf(
+ fdout,vermsg,
+ verclr[0],dctx->program,verclr[1],
+ verclr[2],verinfo->major,verinfo->minor,
+ verinfo->revision,verclr[3],
+ verclr[4],verinfo->commit,verclr[5]);
+}
+
+static void tbnf_perform_unit_actions(
+ const struct tbnf_driver_ctx * dctx,
+ struct tbnf_unit_ctx * uctx)
+{
+ uint64_t flags = dctx->cctx->fmtflags;
+
+ (void)dctx;
+ (void)uctx;
+ (void)flags;
+}
+
+static int tbnf_exit(struct tbnf_driver_ctx * dctx, int ret)
+{
+ tbnf_output_error_vector(dctx);
+ tbnf_lib_free_driver_ctx(dctx);
+ return ret;
+}
+
+int tbnf_main(char ** argv, char ** envp, const struct tbnf_fd_ctx * fdctx)
+{
+ int ret;
+ int fdout;
+ uint64_t flags;
+ struct tbnf_driver_ctx * dctx;
+ struct tbnf_unit_ctx * uctx;
+ const char ** unit;
+
+ flags = TBNF_DRIVER_FLAGS;
+ fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;
+
+ if ((ret = tbnf_lib_get_driver_ctx(argv,envp,flags,fdctx,&dctx)))
+ return (ret == TBNF_USAGE)
+ ? !argv || !argv[0] || !argv[1]
+ : TBNF_ERROR;
+
+ if (dctx->cctx->drvflags & TBNF_DRIVER_VERSION)
+ if ((tbnf_version(dctx,fdout)) < 0)
+ return tbnf_exit(dctx,TBNF_ERROR);
+
+ for (unit=dctx->units; *unit; unit++) {
+ if (!(tbnf_lib_get_unit_ctx(dctx,*unit,&uctx))) {
+ tbnf_perform_unit_actions(dctx,uctx);
+ tbnf_lib_free_unit_ctx(uctx);
+ }
+ }
+
+ return tbnf_exit(dctx,dctx->errv[0] ? TBNF_ERROR : TBNF_OK);
+}
diff --git a/src/driver/tbnf_driver_ctx.c b/src/driver/tbnf_driver_ctx.c
new file mode 100644
index 0000000..8d4b5e5
--- /dev/null
+++ b/src/driver/tbnf_driver_ctx.c
@@ -0,0 +1,254 @@
+/**************************************************************/
+/* treebnf: a tree oriented bnf library */
+/* Copyright (C) 2024 SysDeer Technologies, LLC */
+/* Released under GPLv2 and GPLv3; see COPYING.TREEBNF. */
+/**************************************************************/
+
+#include <stdint.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#define ARGV_DRIVER
+
+#include <treebnf/treebnf.h>
+#include "treebnf_version.h"
+#include "treebnf_driver_impl.h"
+#include "argv/argv.h"
+
+/* package info */
+static const struct tbnf_source_version tbnf_src_version = {
+ TBNF_TAG_VER_MAJOR,
+ TBNF_TAG_VER_MINOR,
+ TBNF_TAG_VER_PATCH,
+ TREEBNF_GIT_VERSION
+};
+
+/* default fd context */
+static const struct tbnf_fd_ctx tbnf_default_fdctx = {
+ .fdin = STDIN_FILENO,
+ .fdout = STDOUT_FILENO,
+ .fderr = STDERR_FILENO,
+ .fdcwd = AT_FDCWD,
+ .fddst = AT_FDCWD,
+ .fdlog = (-1),
+};
+
+struct tbnf_driver_ctx_alloc {
+ struct argv_meta * meta;
+ struct tbnf_driver_ctx_impl ctx;
+ uint64_t guard;
+ const char * units[];
+};
+
+static uint32_t tbnf_argv_flags(uint32_t flags)
+{
+ uint32_t ret = ARGV_CLONE_VECTOR;
+
+ if (flags & TBNF_DRIVER_VERBOSITY_NONE)
+ ret |= ARGV_VERBOSITY_NONE;
+
+ if (flags & TBNF_DRIVER_VERBOSITY_ERRORS)
+ ret |= ARGV_VERBOSITY_ERRORS;
+
+ if (flags & TBNF_DRIVER_VERBOSITY_STATUS)
+ ret |= ARGV_VERBOSITY_STATUS;
+
+ return ret;
+}
+
+static int tbnf_driver_usage(
+ int fdout,
+ const char * program,
+ const char * arg,
+ const struct argv_option ** optv,
+ struct argv_meta * meta)
+{
+ char header[512];
+
+ snprintf(header,sizeof(header),
+ "Usage: %s [options] <file>...\n" "Options:\n",
+ program);
+
+ argv_usage(fdout,header,optv,arg);
+ argv_free(meta);
+
+ return TBNF_USAGE;
+}
+
+static struct tbnf_driver_ctx_impl * tbnf_driver_ctx_alloc(
+ struct argv_meta * meta,
+ const struct tbnf_fd_ctx * fdctx,
+ const struct tbnf_common_ctx * cctx,
+ size_t nunits)
+{
+ struct tbnf_driver_ctx_alloc * ictx;
+ size_t size;
+ struct argv_entry * entry;
+ const char ** units;
+ int elements;
+
+ size = sizeof(struct tbnf_driver_ctx_alloc);
+ size += (nunits+1)*sizeof(const char *);
+
+ if (!(ictx = calloc(1,size)))
+ return 0;
+
+ memcpy(&ictx->ctx.fdctx,fdctx,sizeof(*fdctx));
+ memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx));
+
+ elements = sizeof(ictx->ctx.erribuf) / sizeof(*ictx->ctx.erribuf);
+
+ ictx->ctx.errinfp = &ictx->ctx.erriptr[0];
+ ictx->ctx.erricap = &ictx->ctx.erriptr[--elements];
+
+ ictx->meta = meta;
+
+ for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++)
+ if (!entry->fopt)
+ *units++ = entry->arg;
+
+ ictx->ctx.fdtmpin = (-1);
+ ictx->ctx.ctx.units = ictx->units;
+ ictx->ctx.ctx.errv = ictx->ctx.errinfp;
+ return &ictx->ctx;
+}
+
+static int tbnf_get_driver_ctx_fail(struct argv_meta * meta)
+{
+ argv_free(meta);
+ return -1;
+}
+
+int tbnf_lib_get_driver_ctx(
+ char ** argv,
+ char ** envp,
+ uint64_t flags,
+ const struct tbnf_fd_ctx * fdctx,
+ struct tbnf_driver_ctx ** pctx)
+{
+ struct tbnf_driver_ctx_impl * ctx;
+ struct tbnf_common_ctx cctx;
+ const struct argv_option * optv[TBNF_OPTV_ELEMENTS];
+ struct argv_meta * meta;
+ struct argv_entry * entry;
+ size_t nunits;
+ const char * program;
+
+ (void)envp;
+
+ if (!fdctx)
+ fdctx = &tbnf_default_fdctx;
+
+ argv_optv_init(tbnf_default_options,optv);
+
+ if (!(meta = argv_get(
+ argv,optv,
+ tbnf_argv_flags(flags),
+ fdctx->fderr)))
+ return -1;
+
+ nunits = 0;
+ program = argv_program_name(argv[0]);
+ memset(&cctx,0,sizeof(cctx));
+ cctx.drvflags = flags;
+
+ if (!argv[1] && (flags & TBNF_DRIVER_VERBOSITY_USAGE))
+ return tbnf_driver_usage(
+ fdctx->fderr,
+ program,
+ 0,optv,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 & TBNF_DRIVER_VERBOSITY_USAGE)
+ return tbnf_driver_usage(
+ fdctx->fdout,
+ program,entry->arg,
+ optv,meta);
+ break;
+
+ case TAG_VERSION:
+ cctx.drvflags |= TBNF_DRIVER_VERSION;
+ break;
+ }
+ } else {
+ nunits++;
+ }
+ }
+
+ if (!(ctx = tbnf_driver_ctx_alloc(meta,fdctx,&cctx,nunits)))
+ return tbnf_get_driver_ctx_fail(meta);
+
+ ctx->ctx.program = program;
+ ctx->ctx.cctx = &ctx->cctx;
+
+ *pctx = &ctx->ctx;
+
+ return TBNF_OK;
+}
+
+static void tbnf_free_driver_ctx_impl(struct tbnf_driver_ctx_alloc * ictx)
+{
+ if (ictx->ctx.fdtmpin)
+ close(ictx->ctx.fdtmpin);
+
+ argv_free(ictx->meta);
+ free(ictx);
+}
+
+void tbnf_lib_free_driver_ctx(struct tbnf_driver_ctx * ctx)
+{
+ struct tbnf_driver_ctx_alloc * ictx;
+ uintptr_t addr;
+
+ if (ctx) {
+ addr = (uintptr_t)ctx - offsetof(struct tbnf_driver_ctx_impl,ctx);
+ addr = addr - offsetof(struct tbnf_driver_ctx_alloc,ctx);
+ ictx = (struct tbnf_driver_ctx_alloc *)addr;
+ tbnf_free_driver_ctx_impl(ictx);
+ }
+}
+
+const struct tbnf_source_version * tbnf_api_source_version(void)
+{
+ return &tbnf_src_version;
+}
+
+int tbnf_lib_get_driver_fdctx(
+ const struct tbnf_driver_ctx * dctx,
+ struct tbnf_fd_ctx * fdctx)
+{
+ struct tbnf_driver_ctx_impl * ictx;
+
+ ictx = tbnf_get_driver_ictx(dctx);
+
+ fdctx->fdin = ictx->fdctx.fdin;
+ fdctx->fdout = ictx->fdctx.fdout;
+ fdctx->fderr = ictx->fdctx.fderr;
+ fdctx->fdlog = ictx->fdctx.fdlog;
+ fdctx->fdcwd = ictx->fdctx.fdcwd;
+ fdctx->fddst = ictx->fdctx.fddst;
+
+ return 0;
+}
+
+int tbnf_lib_set_driver_fdctx(
+ struct tbnf_driver_ctx * dctx,
+ const struct tbnf_fd_ctx * fdctx)
+{
+ struct tbnf_driver_ctx_impl * ictx;
+
+ ictx = tbnf_get_driver_ictx(dctx);
+
+ ictx->fdctx.fdin = fdctx->fdin;
+ ictx->fdctx.fdout = fdctx->fdout;
+ ictx->fdctx.fderr = fdctx->fderr;
+ ictx->fdctx.fdlog = fdctx->fdlog;
+ ictx->fdctx.fdcwd = fdctx->fdcwd;
+ ictx->fdctx.fddst = fdctx->fddst;
+
+ return 0;
+}
diff --git a/src/driver/tbnf_map_input.c b/src/driver/tbnf_map_input.c
new file mode 100644
index 0000000..629672a
--- /dev/null
+++ b/src/driver/tbnf_map_input.c
@@ -0,0 +1,66 @@
+/**************************************************************/
+/* treebnf: a tree oriented bnf library */
+/* Copyright (C) 2024 SysDeer Technologies, LLC */
+/* Released under GPLv2 and GPLv3; see COPYING.TREEBNF. */
+/**************************************************************/
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <treebnf/treebnf.h>
+#include "treebnf_driver_impl.h"
+#include "treebnf_errinfo_impl.h"
+
+int tbnf_lib_map_raw_input(
+ const struct tbnf_driver_ctx * dctx,
+ int fd,
+ const char * path,
+ int prot,
+ struct tbnf_raw_input * map)
+{
+ int ret;
+ struct stat st;
+ bool fnew;
+ int fdcwd;
+
+ fdcwd = tbnf_driver_fdcwd(dctx);
+
+ if ((fnew = (fd < 0)))
+ fd = openat(fdcwd,path,O_RDONLY | O_CLOEXEC);
+
+ if (fd < 0)
+ return TBNF_SYSTEM_ERROR(dctx,path);
+
+ if ((ret = fstat(fd,&st) < 0) && fnew)
+ close(fd);
+
+ else if ((st.st_size == 0) && fnew)
+ close(fd);
+
+ if (ret < 0)
+ return TBNF_SYSTEM_ERROR(dctx,path);
+
+ else if (st.st_size == 0)
+ return TBNF_CUSTOM_ERROR(
+ dctx,TBNF_ERR_IMAGE_SIZE_ZERO);
+
+ map->map_size = st.st_size;
+ map->map_addr = mmap(0,map->map_size,prot,MAP_PRIVATE,fd,0);
+
+ if (fnew)
+ close(fd);
+
+ return (map->map_addr == MAP_FAILED)
+ ? TBNF_SYSTEM_ERROR(dctx,0)
+ : 0;
+}
+
+int tbnf_lib_unmap_raw_input(struct tbnf_raw_input * map)
+{
+ return munmap(map->map_addr,map->map_size);
+}
diff --git a/src/driver/tbnf_unit_ctx.c b/src/driver/tbnf_unit_ctx.c
new file mode 100644
index 0000000..fedcbaa
--- /dev/null
+++ b/src/driver/tbnf_unit_ctx.c
@@ -0,0 +1,139 @@
+/**************************************************************/
+/* treebnf: a tree oriented bnf library */
+/* Copyright (C) 2024 SysDeer Technologies, LLC */
+/* Released under GPLv2 and GPLv3; see COPYING.TREEBNF. */
+/**************************************************************/
+
+#include <stdint.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+
+#include <treebnf/treebnf.h>
+#include "treebnf_driver_impl.h"
+#include "treebnf_errinfo_impl.h"
+#include "treebnf_tmpfile_impl.h"
+
+static int tbnf_free_unit_ctx_impl(struct tbnf_unit_ctx_impl * ctx, int ret)
+{
+ if (ctx) {
+ tbnf_lib_unmap_raw_input(&ctx->map);
+ free(ctx);
+ }
+
+ return ret;
+}
+
+static int tbnf_stdin_to_tmp(const struct tbnf_driver_ctx * dctx)
+{
+ struct tbnf_driver_ctx_impl * ictx;
+ int fdtmp;
+
+ ssize_t ret;
+ ssize_t cnt;
+ char * ch;
+ char buf[4096];
+
+ ictx = tbnf_get_driver_ictx(dctx);
+
+ if (ictx->fdtmpin >= 0)
+ return dup(ictx->fdtmpin);
+
+ if ((fdtmp = tbnf_tmpfile()) < 0)
+ return -1;
+
+ if ((ictx->fdtmpin = dup(fdtmp)) < 0) {
+ close(fdtmp);
+ return -1;
+ }
+
+ for (;;) {
+ ret = read(0,buf,sizeof(buf)-1);
+
+ while ((ret < 0) && (errno == EINTR))
+ ret = read(0,buf,sizeof(buf)-1);
+
+ if (ret < 0) {
+ close(fdtmp);
+ return -1;
+
+ } else if (ret == 0) {
+ return fdtmp;
+
+ } else {
+ ch = buf;
+ cnt = ret;
+
+ for (; cnt; ) {
+ ret = write(fdtmp,ch,cnt);
+
+ while ((ret < 0) && (errno == EINTR))
+ ret = write(fdtmp,ch,cnt);
+
+ if (ret < 0) {
+ close(fdtmp);
+ return -1;
+ }
+
+ ch += ret;
+ cnt -= ret;
+ }
+ }
+ }
+}
+
+int tbnf_lib_get_unit_ctx(
+ const struct tbnf_driver_ctx * dctx,
+ const char * path,
+ struct tbnf_unit_ctx ** pctx)
+{
+ struct tbnf_unit_ctx_impl * ctx;
+ int prot;
+ int fd;
+
+ if (!dctx)
+ return TBNF_CUSTOM_ERROR(
+ dctx,TBNF_ERR_NULL_CONTEXT);
+
+ else if (!(ctx = calloc(1,sizeof(*ctx))))
+ return TBNF_BUFFER_ERROR(dctx);
+
+ tbnf_driver_set_ectx(
+ dctx,0,path);
+
+ prot = PROT_READ;
+
+ if (strcmp(path,"-"))
+ fd = -1;
+
+ else if ((fd = tbnf_stdin_to_tmp(dctx)) < 0)
+ return tbnf_free_unit_ctx_impl(
+ ctx,TBNF_FILE_ERROR(dctx));
+
+ if (tbnf_lib_map_raw_input(dctx,fd,path,prot,&ctx->map))
+ return tbnf_free_unit_ctx_impl(
+ ctx,TBNF_NESTED_ERROR(dctx));
+
+ if (fd >= 0)
+ close(fd);
+
+ ctx->path = path;
+ ctx->uctx.path = &ctx->path;
+ ctx->uctx.map = &ctx->map;
+
+ *pctx = &ctx->uctx;
+ return 0;
+}
+
+void tbnf_lib_free_unit_ctx(struct tbnf_unit_ctx * ctx)
+{
+ struct tbnf_unit_ctx_impl * ictx;
+ uintptr_t addr;
+
+ if (ctx) {
+ addr = (uintptr_t)ctx - offsetof(struct tbnf_unit_ctx_impl,uctx);
+ ictx = (struct tbnf_unit_ctx_impl *)addr;
+ tbnf_free_unit_ctx_impl(ictx,0);
+ }
+}