libndb: add ndbvalfmt() formatter

Some attributes such as vendor and txt require double-quoting
to be parsable by libndb. Provide the fmtinstall(2) formatter
ndbvalfmt for programs outputting ndb(6) format.
This commit is contained in:
cinap_lenrek 2023-05-28 16:09:18 +00:00
parent 5aa902c953
commit ed4a0c0642
4 changed files with 55 additions and 1 deletions

View file

@ -151,3 +151,4 @@ Ndbtuple* ndbsnext(Ndbs*, char*, char*);
Ndbtuple* ndbsubstitute(Ndbtuple*, Ndbtuple*, Ndbtuple*);
Ndbtuple* ndbdedup(Ndbtuple*);
void ndbsetmalloctag(Ndbtuple*, uintptr);
int ndbvalfmt(Fmt*);

View file

@ -1,6 +1,6 @@
.TH NDB 2
.SH NAME
ndbopen, ndbcat, ndbchanged, ndbclose, ndbreopen, ndbsearch, ndbsnext, ndbgetvalue, ndbfree, ipattr, mkptrname, ndbgetipaddr, ndbipinfo, csipinfo, ndbhash, ndbparse, csgetvalue, ndbfindattr, dnsquery, ndbdiscard, ndbconcatenate, ndbreorder, ndbsubstitute, ndbdedup \- network database
ndbopen, ndbcat, ndbchanged, ndbclose, ndbreopen, ndbsearch, ndbsnext, ndbgetvalue, ndbfree, ipattr, mkptrname, ndbgetipaddr, ndbipinfo, csipinfo, ndbhash, ndbparse, csgetvalue, ndbfindattr, dnsquery, ndbdiscard, ndbconcatenate, ndbreorder, ndbsubstitute, ndbdedup, ndbsetmalloctag, ndbvalfmt \- network database
.SH SYNOPSIS
.B #include <u.h>
.br
@ -94,6 +94,9 @@ Ndbtuple* ndbdedup(Ndbtuple *t)
.PP
.B
void ndbsetmalloctag(Ndbtuple *t, uintptr tag)
.PP
.B
int ndbvalfmt(Fmt*)
.SH DESCRIPTION
These routines are used by network administrative programs to search
the network database.
@ -523,6 +526,27 @@ of each tuple in the list
.I t
to
.IR tag .
.PP
.I Ndbvalfmt
formats a
.B char*
string
of a
.I Ndbtuple
val,
adding " quoting if necessary.
It is typically enabled by calling:
.IP
.EX
fmtinstall('$', ndbvalfmt);
.EE
.PP
And then used like:
.IP
.EX
Ndbtuple *t = ...
print("%s=%$", t->attr, t->val);
.EE
.SH FILES
.BR /lib/ndb " directory of network database files
.SH SOURCE

View file

@ -23,6 +23,7 @@ OFILES=\
ndbreorder.$O\
ndbsubstitute.$O\
ndbdedup.$O\
ndbvalfmt.$O\
HFILES=\
/sys/include/ndb.h\

View file

@ -0,0 +1,28 @@
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ndb.h>
#include "ndbhf.h"
static int
needquote(char *s)
{
int c;
while((c = *s++) != '\0'){
if(ISWHITE(c) || c == '#')
return 1;
}
return 0;
}
int
ndbvalfmt(Fmt *f)
{
char *s = va_arg(f->args, char*);
if(s == nil)
s = "";
if(needquote(s))
return fmtprint(f, "\"%s\"", s);
return fmtstrcpy(f, s);
}