KHTTP_TEMPLATEX(3)
π khttp_templatex(3) β Library Functions Manual, bestie edition π
okay so bestie, welcome to the khttp_templatex(3) page from the Library Functions Manual π
this one is all about khttp_templatex, khttp_templatex_buf, and khttp_templatex_fd β the template-filling glow-up for kcgi. It lives in library βlibkcgiβ. slay.
β¨ NAME β¨
khttp_templatex, khttp_templatex_buf, khttp_templatex_fd β emit filled-in templates for kcgi π
π LIBRARY π
library βlibkcgiβ β that's the squad, no cap π«Ά
π§Ύ SYNOPSIS π§Ύ
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
enum kcgi_err
khttp_templatex(const struct ktemplate *t, const char *filename,
const struct ktemplatex *x, void *arg);
enum kcgi_err
khttp_templatex_buf(const struct ktemplate *t, const char *buf,
size_t sz, const struct ktemplatex *x, void *arg);
enum kcgi_err
khttp_templatex_fd(const struct ktemplate *t, int fd,
const char *filename, const struct ktemplatex *x, void *arg);
πΈ DESCRIPTION πΈ
okay so these functions modify input by replacing keys in a template. This generalises the khttp_template(3) family of functions with generic writing functions. All functions accept a template t consisting of the following fields:
const char *const *keyβ an array of keys to be replaced in the template πsize_t keyszβ the number of keys int->keyvoid *argβ an optional argument passed tot->cbint (*cb)(size_t index, void *arg)β the callback function invoked when a key at positionindex, which is always less thant->keysz, is found int->key. The optionalargis passed the function. The function must return zero on failure, non-zero on success π
They further accept an extension x consisting of the following:
writerβ a writing function for writing input that does not match key sequences. If it is invoked and the return value is notKCGI_OK, templating stops and the return value is returned to the caller.fbkβ a fall-back function invoked if a key sequences was not found in thec->keyarray. This accepts the key sequence, length of the sequence, andarg. IfNULL, key sequences not found are passed towriter.
If t is NULL, the input is passed through to x->writer without any processing π«‘
Otherwise, the input is passed to x->writer until a key sequence is encountered matching a key in t->key. The callback t->cb is then invoked instead of printing the key sequence. If there are multiple matching keys in t->key, only one is used (which is not yet fixed). If the key sequence is not found in t->key, it is passed to x->fbk, if not NULL, or passed directly to x->writer otherwise.
bestie, quick vibe check: if there are multiple matching keys in t->key, only one is used β and which one is not yet fixed π that's literally what the page says.
The different input types are khttpx_template(), which reads input from the file filename; khttpx_template_buf(), which reads from a binary buffer buf of length sz; and khttpx_template_fd(), which reads from a file descriptor fd with optional file-name fname used only for logging purposes. (yes, those are the spellings on the page π)
π SYNTAX π
Each substring of the input beginning and ending with a pair of βatβ signs, @@key@@, is called a "key sequence". Zero-length keys @@@@ are allowed and match empty template keys. If the @@ pair is escaped with a single backslash, \@@, the backslash is removed and it's emitted as @@.
A key sequence may not contain an escaped pair: this is parsed as a backslash followed by the trailing pair.
π₯ RETURN VALUES π₯
These return an enum kcgi_err indicating the error state:
KCGI_OKβ no error occurred.KCGI_ENOMEMβ memory allocation failed.KCGI_SYSTEMβ a system call failed. For example, writing to the output stream failed, orkhttp_template()failed toopen(2)filename.KCGI_FORMβt->cbreturned 0.
If the x->writer function returns anything but KCGI_OK, the return code is passed as the return value.
π§ͺ EXAMPLES π§ͺ
The following simple example takes a buffer buf and applies the replacement template of two values, writing it to the current context req. It stores the result in the given buffer out.
static int writer(size_t idx, void *arg)
{
struct kcgi_buf *p = arg;
if (idx == 0)
kcgi_buf_puts(p, "foo-value");
else if (idx == 1)
kcgi_buf_puts(p, "bar-value");
return 1;
}
enum kcgi_err format(struct kcgi_buf *out)
{
const char *const keys[] = { "foo", "bar" };
struct ktemplate t = {
.key = keys,
.keysz = 2,
.arg = out,
.cb = writer
};
struct ktemplatex x = {
.writer = kcgi_buf_write,
.fbk = NULL
};
const char *in = "foo=@@foo@@, bar=@@bar@@";
memset(out, 0, sizeof(struct kcgi_buf));
return khttp_templatex_buf
(&t, in, strlen(in), &x, out);
}
The function will produce "foo=foo-value, bar=bar-value" β¨
π SEE ALSO π
π AUTHORS π
Written by Kristaps Dzonsons <kristaps@bsd.lv>. icon behavior fr fr π
xoxo, Debian says hiii πΈ β $Mdocdate$ β KHTTP_TEMPLATEX(3) π
π¦ where this page comes from
- alpine 3.24: kcgi-doc 1.0.1-r0 Β· ISC
- Copyright (c) 2014, 2017--2018, 2020 Kristaps Dzonsons <kristaps@bsd.lv>
- Copyright (c) 2018 Ingo Schwarze <schwarze@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms