|
NAMEkhttp_templatex ,
khttp_templatex_buf ,
khttp_templatex_fd —
emit filled-in templates for kcgi
LIBRARYlibrary “libkcgi”SYNOPSIS#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
#include <kcgi.h>
enum kcgi_err
enum kcgi_err
enum kcgi_err
DESCRIPTIONModify 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:
They further accept an extension x consisting of the following:
If t is Otherwise, the input is passed to
x->writer until a key sequence in 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
The different input types are
SYNTAXEach 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 VALUESThese return an enum kcgi_err indicating the error state:
If the x->writer function returns
anything but EXAMPLESThe 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 ALSOkcgi(3), khttp_body(3), khttp_parse(3), khttp_template(3), khttp_write(3)AUTHORSWritten by Kristaps Dzonsons <kristaps@bsd.lv>.
Visit the GSP FreeBSD Man Page Interface. |