summaryrefslogtreecommitdiff
path: root/usr.sbin/bgpctl/json.c
blob: 8f72be0bcbba8d263485a2c18be5f7800ea0e4a0 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*	$OpenBSD: json.c,v 1.5 2023/04/26 19:14:54 claudio Exp $ */

/*
 * Copyright (c) 2020 Claudio Jeker <claudio@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <err.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "json.h"

#define JSON_MAX_STACK	16

enum json_type {
	NONE,
	START,
	ARRAY,
	OBJECT
};

static struct json_stack {
	const char	*name;
	unsigned int	count;
	enum json_type	type;
} stack[JSON_MAX_STACK];

static char indent[JSON_MAX_STACK + 1];
static int level;
static FILE *jsonfh;

static void
do_comma_indent(void)
{
	if (stack[level].count++ > 0)
		fprintf(jsonfh, ",\n");
	fprintf(jsonfh, "\t%.*s", level, indent);
}

static void
do_name(const char *name)
{
	if (stack[level].type == ARRAY)
		return;
	fprintf(jsonfh, "\"%s\": ", name);
}

static int
do_find(enum json_type type, const char *name)
{
	int i;

	for (i = level; i > 0; i--)
		if (type == stack[i].type &&
		    strcmp(name, stack[i].name) == 0)
			return i;

	/* not found */
	return -1;
}

void
json_do_start(FILE *fh)
{
	memset(indent, '\t', JSON_MAX_STACK);
	memset(stack, 0, sizeof(stack));
	level = 0;
	stack[level].type = START;
	jsonfh = fh;

	fprintf(jsonfh, "{\n");
}

void
json_do_finish(void)
{
	while (level > 0)
		json_do_end();
	fprintf(jsonfh, "\n}\n");
}

void
json_do_array(const char *name)
{
	int i, l;

	if ((l = do_find(ARRAY, name)) > 0) {
		/* array already in use, close element and move on */
		for (i = level - l; i > 0; i--)
			json_do_end();
		return;
	}
	/* Do not stack arrays, while allowed this is not needed */
	if (stack[level].type == ARRAY)
		json_do_end();

	do_comma_indent();
	do_name(name);
	fprintf(jsonfh, "[\n");

	if (++level >= JSON_MAX_STACK)
		errx(1, "json stack too deep");

	stack[level].name = name;
	stack[level].type = ARRAY;
	stack[level].count = 0;
}

void
json_do_object(const char *name)
{
	int i, l;

	if ((l = do_find(OBJECT, name)) > 0) {
		/* roll back to that object and close it */
		for (i = level - l; i >= 0; i--)
			json_do_end();
	}

	do_comma_indent();
	do_name(name);
	fprintf(jsonfh, "{\n");

	if (++level >= JSON_MAX_STACK)
		errx(1, "json stack too deep");

	stack[level].name = name;
	stack[level].type = OBJECT;
	stack[level].count = 0;
}

void
json_do_end(void)
{
	if (stack[level].type == ARRAY)
		fprintf(jsonfh, "\n%.*s]", level, indent);
	else if (stack[level].type == OBJECT)
		fprintf(jsonfh, "\n%.*s}", level, indent);
	else
		errx(1, "json bad stack state");

	stack[level].name = NULL;
	stack[level].type = NONE;
	stack[level].count = 0;

	if (level-- <= 0)
		errx(1, "json stack underflow");

	stack[level].count++;
}

void
json_do_printf(const char *name, const char *fmt, ...)
{
	va_list ap;

	do_comma_indent();

	do_name(name);
	fprintf(jsonfh, "\"");
	va_start(ap, fmt);
	vfprintf(jsonfh, fmt, ap);
	va_end(ap);
	fprintf(jsonfh, "\"");
}

void
json_do_hexdump(const char *name, void *buf, size_t len)
{
	uint8_t *data = buf;
	size_t i;

	do_comma_indent();
	do_name(name);
	fprintf(jsonfh, "\"");
	for (i = 0; i < len; i++)
		fprintf(jsonfh, "%02x", *(data + i));
	fprintf(jsonfh, "\"");
}

void
json_do_bool(const char *name, int v)
{
	do_comma_indent();
	do_name(name);
	if (v)
		fprintf(jsonfh, "true");
	else
		fprintf(jsonfh, "false");
}

void
json_do_uint(const char *name, unsigned long long v)
{
	do_comma_indent();
	do_name(name);
	fprintf(jsonfh, "%llu", v);
}

void
json_do_int(const char *name, long long v)
{
	do_comma_indent();
	do_name(name);
	fprintf(jsonfh, "%lld", v);
}

void
json_do_double(const char *name, double v)
{
	do_comma_indent();
	do_name(name);
	fprintf(jsonfh, "%f", v);
}