summaryrefslogtreecommitdiffhomepage
path: root/src/driver/tbnf_unit_ctx.c
blob: fedcbaa4d40f9645104602cd8c4cb632e30f7ea5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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);
	}
}