Blame src/internal/treebnf_driver_impl.h

02e59c
/**************************************************************/
02e59c
/*  treebnf: a tree oriented bnf library                      */
02e59c
/*  Copyright (C) 2024  SysDeer Technologies, LLC             */
02e59c
/*  Released under GPLv2 and GPLv3; see COPYING.TREEBNF.      */
02e59c
/**************************************************************/
02e59c
02e59c
#ifndef TREEBNF_DRIVER_IMPL_H
02e59c
#define TREEBNF_DRIVER_IMPL_H
02e59c
02e59c
#include <stdint.h>
02e59c
#include <dirent.h>
02e59c
#include <stdio.h>
02e59c
#include <sys/stat.h>
02e59c
#include <sys/types.h>
02e59c
02e59c
#include <treebnf/treebnf.h>
02e59c
#include "argv/argv.h"
02e59c
02e59c
#define TBNF_OPTV_ELEMENTS 128
02e59c
02e59c
extern const struct argv_option tbnf_default_options[];
02e59c
02e59c
enum app_tags {
02e59c
	TAG_HELP,
02e59c
	TAG_VERSION,
02e59c
};
02e59c
02e59c
struct tbnf_driver_ctx_impl {
02e59c
	struct tbnf_common_ctx        cctx;
02e59c
	struct tbnf_driver_ctx        ctx;
02e59c
	struct tbnf_fd_ctx            fdctx;
02e59c
	int                           fdtmpin;
02e59c
	const struct tbnf_unit_ctx *  euctx;
02e59c
	const char *                  eunit;
02e59c
	struct tbnf_error_info **     errinfp;
02e59c
	struct tbnf_error_info **     erricap;
02e59c
	struct tbnf_error_info *      erriptr[64];
02e59c
	struct tbnf_error_info        erribuf[64];
02e59c
};
02e59c
02e59c
struct tbnf_unit_ctx_impl {
02e59c
	const char *		path;
02e59c
	struct tbnf_unit_ctx	uctx;
02e59c
	struct tbnf_raw_input   map;
02e59c
	struct stat		st;
02e59c
};
02e59c
02e59c
02e59c
static inline struct tbnf_driver_ctx_impl * tbnf_get_driver_ictx(
02e59c
	const struct tbnf_driver_ctx * dctx)
02e59c
{
02e59c
        uintptr_t addr;
02e59c
02e59c
        if (dctx) {
02e59c
                addr = (uintptr_t)dctx - offsetof(struct tbnf_driver_ctx_impl,ctx);
02e59c
                return (struct tbnf_driver_ctx_impl *)addr;
02e59c
        }
02e59c
02e59c
        return 0;
02e59c
}
02e59c
02e59c
static inline struct tbnf_unit_ctx_impl * tbnf_get_unit_ictx(
02e59c
	const struct tbnf_unit_ctx * uctx)
02e59c
{
02e59c
	struct tbnf_unit_ctx_impl *	ictx;
02e59c
	uintptr_t			addr;
02e59c
02e59c
	addr = (uintptr_t)uctx - offsetof(struct tbnf_unit_ctx_impl,uctx);
02e59c
	ictx = (struct tbnf_unit_ctx_impl *)addr;
02e59c
	return ictx;
02e59c
}
02e59c
02e59c
static inline void tbnf_driver_set_ectx(
02e59c
	const struct tbnf_driver_ctx * dctx,
02e59c
	const struct tbnf_unit_ctx *   uctx,
02e59c
	const char *                 unit)
02e59c
{
02e59c
	struct tbnf_driver_ctx_impl *  ictx;
02e59c
02e59c
	ictx        = tbnf_get_driver_ictx(dctx);
02e59c
	ictx->euctx = uctx;
02e59c
	ictx->eunit = unit;
02e59c
}
02e59c
02e59c
static inline int tbnf_driver_fdin(const struct tbnf_driver_ctx * dctx)
02e59c
{
02e59c
	struct tbnf_fd_ctx fdctx;
02e59c
	tbnf_lib_get_driver_fdctx(dctx,&fdctx);
02e59c
	return fdctx.fdin;
02e59c
}
02e59c
02e59c
static inline int tbnf_driver_fdout(const struct tbnf_driver_ctx * dctx)
02e59c
{
02e59c
	struct tbnf_fd_ctx fdctx;
02e59c
	tbnf_lib_get_driver_fdctx(dctx,&fdctx);
02e59c
	return fdctx.fdout;
02e59c
}
02e59c
02e59c
static inline int tbnf_driver_fderr(const struct tbnf_driver_ctx * dctx)
02e59c
{
02e59c
	struct tbnf_fd_ctx fdctx;
02e59c
	tbnf_lib_get_driver_fdctx(dctx,&fdctx);
02e59c
	return fdctx.fderr;
02e59c
}
02e59c
02e59c
static inline int tbnf_driver_fdlog(const struct tbnf_driver_ctx * dctx)
02e59c
{
02e59c
	struct tbnf_fd_ctx fdctx;
02e59c
	tbnf_lib_get_driver_fdctx(dctx,&fdctx);
02e59c
	return fdctx.fdlog;
02e59c
}
02e59c
02e59c
static inline int tbnf_driver_fdcwd(const struct tbnf_driver_ctx * dctx)
02e59c
{
02e59c
	struct tbnf_fd_ctx fdctx;
02e59c
	tbnf_lib_get_driver_fdctx(dctx,&fdctx);
02e59c
	return fdctx.fdcwd;
02e59c
}
02e59c
02e59c
static inline int tbnf_driver_fddst(const struct tbnf_driver_ctx * dctx)
02e59c
{
02e59c
	struct tbnf_fd_ctx fdctx;
02e59c
	tbnf_lib_get_driver_fdctx(dctx,&fdctx);
02e59c
	return fdctx.fddst;
02e59c
}
02e59c
02e59c
#endif