|
|
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 |
#include <stdint.h>
|
|
|
02e59c |
#include <stddef.h>
|
|
|
02e59c |
#include <stdlib.h>
|
|
|
02e59c |
#include <string.h>
|
|
|
02e59c |
#include <sys/mman.h>
|
|
|
02e59c |
|
|
|
02e59c |
#include <treebnf/treebnf.h>
|
|
|
02e59c |
#include "treebnf_driver_impl.h"
|
|
|
02e59c |
#include "treebnf_errinfo_impl.h"
|
|
|
02e59c |
#include "treebnf_tmpfile_impl.h"
|
|
|
02e59c |
|
|
|
02e59c |
static int tbnf_free_unit_ctx_impl(struct tbnf_unit_ctx_impl * ctx, int ret)
|
|
|
02e59c |
{
|
|
|
02e59c |
if (ctx) {
|
|
|
02e59c |
tbnf_lib_unmap_raw_input(&ctx->map);
|
|
|
02e59c |
free(ctx);
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
return ret;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
static int tbnf_stdin_to_tmp(const struct tbnf_driver_ctx * dctx)
|
|
|
02e59c |
{
|
|
|
02e59c |
struct tbnf_driver_ctx_impl * ictx;
|
|
|
02e59c |
int fdtmp;
|
|
|
02e59c |
|
|
|
02e59c |
ssize_t ret;
|
|
|
02e59c |
ssize_t cnt;
|
|
|
02e59c |
char * ch;
|
|
|
02e59c |
char buf[4096];
|
|
|
02e59c |
|
|
|
02e59c |
ictx = tbnf_get_driver_ictx(dctx);
|
|
|
02e59c |
|
|
|
02e59c |
if (ictx->fdtmpin >= 0)
|
|
|
02e59c |
return dup(ictx->fdtmpin);
|
|
|
02e59c |
|
|
|
02e59c |
if ((fdtmp = tbnf_tmpfile()) < 0)
|
|
|
02e59c |
return -1;
|
|
|
02e59c |
|
|
|
02e59c |
if ((ictx->fdtmpin = dup(fdtmp)) < 0) {
|
|
|
02e59c |
close(fdtmp);
|
|
|
02e59c |
return -1;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
for (;;) {
|
|
|
02e59c |
ret = read(0,buf,sizeof(buf)-1);
|
|
|
02e59c |
|
|
|
02e59c |
while ((ret < 0) && (errno == EINTR))
|
|
|
02e59c |
ret = read(0,buf,sizeof(buf)-1);
|
|
|
02e59c |
|
|
|
02e59c |
if (ret < 0) {
|
|
|
02e59c |
close(fdtmp);
|
|
|
02e59c |
return -1;
|
|
|
02e59c |
|
|
|
02e59c |
} else if (ret == 0) {
|
|
|
02e59c |
return fdtmp;
|
|
|
02e59c |
|
|
|
02e59c |
} else {
|
|
|
02e59c |
ch = buf;
|
|
|
02e59c |
cnt = ret;
|
|
|
02e59c |
|
|
|
02e59c |
for (; cnt; ) {
|
|
|
02e59c |
ret = write(fdtmp,ch,cnt);
|
|
|
02e59c |
|
|
|
02e59c |
while ((ret < 0) && (errno == EINTR))
|
|
|
02e59c |
ret = write(fdtmp,ch,cnt);
|
|
|
02e59c |
|
|
|
02e59c |
if (ret < 0) {
|
|
|
02e59c |
close(fdtmp);
|
|
|
02e59c |
return -1;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
ch += ret;
|
|
|
02e59c |
cnt -= ret;
|
|
|
02e59c |
}
|
|
|
02e59c |
}
|
|
|
02e59c |
}
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
int tbnf_lib_get_unit_ctx(
|
|
|
02e59c |
const struct tbnf_driver_ctx * dctx,
|
|
|
02e59c |
const char * path,
|
|
|
02e59c |
struct tbnf_unit_ctx ** pctx)
|
|
|
02e59c |
{
|
|
|
02e59c |
struct tbnf_unit_ctx_impl * ctx;
|
|
|
02e59c |
int prot;
|
|
|
02e59c |
int fd;
|
|
|
02e59c |
|
|
|
02e59c |
if (!dctx)
|
|
|
02e59c |
return TBNF_CUSTOM_ERROR(
|
|
|
02e59c |
dctx,TBNF_ERR_NULL_CONTEXT);
|
|
|
02e59c |
|
|
|
02e59c |
else if (!(ctx = calloc(1,sizeof(*ctx))))
|
|
|
02e59c |
return TBNF_BUFFER_ERROR(dctx);
|
|
|
02e59c |
|
|
|
02e59c |
tbnf_driver_set_ectx(
|
|
|
02e59c |
dctx,0,path);
|
|
|
02e59c |
|
|
|
02e59c |
prot = PROT_READ;
|
|
|
02e59c |
|
|
|
02e59c |
if (strcmp(path,"-"))
|
|
|
02e59c |
fd = -1;
|
|
|
02e59c |
|
|
|
02e59c |
else if ((fd = tbnf_stdin_to_tmp(dctx)) < 0)
|
|
|
02e59c |
return tbnf_free_unit_ctx_impl(
|
|
|
02e59c |
ctx,TBNF_FILE_ERROR(dctx));
|
|
|
02e59c |
|
|
|
02e59c |
if (tbnf_lib_map_raw_input(dctx,fd,path,prot,&ctx->map))
|
|
|
02e59c |
return tbnf_free_unit_ctx_impl(
|
|
|
02e59c |
ctx,TBNF_NESTED_ERROR(dctx));
|
|
|
02e59c |
|
|
|
02e59c |
if (fd >= 0)
|
|
|
02e59c |
close(fd);
|
|
|
02e59c |
|
|
|
02e59c |
ctx->path = path;
|
|
|
02e59c |
ctx->uctx.path = &ctx->path;
|
|
|
02e59c |
ctx->uctx.map = &ctx->map;
|
|
|
02e59c |
|
|
|
02e59c |
*pctx = &ctx->uctx;
|
|
|
02e59c |
return 0;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
void tbnf_lib_free_unit_ctx(struct tbnf_unit_ctx * ctx)
|
|
|
02e59c |
{
|
|
|
02e59c |
struct tbnf_unit_ctx_impl * ictx;
|
|
|
02e59c |
uintptr_t addr;
|
|
|
02e59c |
|
|
|
02e59c |
if (ctx) {
|
|
|
02e59c |
addr = (uintptr_t)ctx - offsetof(struct tbnf_unit_ctx_impl,uctx);
|
|
|
02e59c |
ictx = (struct tbnf_unit_ctx_impl *)addr;
|
|
|
02e59c |
tbnf_free_unit_ctx_impl(ictx,0);
|
|
|
02e59c |
}
|
|
|
02e59c |
}
|