ORT_LANG_JSON(3) Library Functions Manual ORT_LANG_JSON(3)

ort_lang_c_sourcegenerate JSON representation of openradtool configuration

library “libort-lang-json”

#include <sys/queue.h>
#include <stdio.h>
#include <ort.h>
#include <ort-lang-json.h>

int
ort_lang_json(const struct ort_lang_json *args, const struct config *cfg, FILE *f);

Outputs a JSON file representing the parsed configuration cfg to f with the parameters in args. The values in args are as follows:

unsigned int flags
If set to ORT_LANG_JSON_FRAGMENT, do not generate squiggly braces around the JSON content.

The output is not pretty-printed in any way: JSON tokens are only space separated.

To compile and link a simple file sample.c using this function, use pkg-config(1):

% cc `pkg-config --cflags ort-lang-json` -c -o sample.o sample.c
% cc -o sample sample.o `pkg-config --libs ort-lang-json`

Returns zero on failure, non-zero on success. Failure only occurs with memory allocation errors or when writing to f.

ort-json.ts
Typescript definition of the output. This is usually installed in /usr/local/share/openradtool, but may be elsewhere.

The following emits a configuration as read from standard input to JSON as written to standard output.

struct config *cfg;
struct ort_lang_json args;

memset(&args, 0, sizeof(struct ort_lang_json));

if ((cfg = ort_config_alloc()) == NULL)
  err(1, NULL);
if (!ort_parse_file(cfg, stdin, "<stdin>"))
  errx(1, "ort_parse_file");
if (!ort_parse_close(cfg))
  errx(1, "ort_parse_close");
if (!ort_lang_json(&args, cfg, stdout))
  err(1, "ort_lang_json");

ort_config_free(cfg);

Alternatively, this could be converted into a string:

char *buf = NULL;
size_t bufsz = 0;

/* Read and parse configuration as above... */

if ((stream = open_memstream(&buf, &bufsz)) == NULL)
  err(1, "open_memstream");
if (!ort_lang_json(&args, cfg, stream))
  err(1, "ort_lang_json");
fclose(stream);

/* Do something with buf of size bufsz. */

free(buf);

pkg-config(1), ort(3)

October 25, 2021 OpenBSD 6.7