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* ndbsubstitute(Ndbtuple*, Ndbtuple*, Ndbtuple*);
Ndbtuple* ndbdedup(Ndbtuple*); Ndbtuple* ndbdedup(Ndbtuple*);
void ndbsetmalloctag(Ndbtuple*, uintptr); void ndbsetmalloctag(Ndbtuple*, uintptr);
int ndbvalfmt(Fmt*);

View file

@ -1,6 +1,6 @@
.TH NDB 2 .TH NDB 2
.SH NAME .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 .SH SYNOPSIS
.B #include <u.h> .B #include <u.h>
.br .br
@ -94,6 +94,9 @@ Ndbtuple* ndbdedup(Ndbtuple *t)
.PP .PP
.B .B
void ndbsetmalloctag(Ndbtuple *t, uintptr tag) void ndbsetmalloctag(Ndbtuple *t, uintptr tag)
.PP
.B
int ndbvalfmt(Fmt*)
.SH DESCRIPTION .SH DESCRIPTION
These routines are used by network administrative programs to search These routines are used by network administrative programs to search
the network database. the network database.
@ -523,6 +526,27 @@ of each tuple in the list
.I t .I t
to to
.IR tag . .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 .SH FILES
.BR /lib/ndb " directory of network database files .BR /lib/ndb " directory of network database files
.SH SOURCE .SH SOURCE

View file

@ -23,6 +23,7 @@ OFILES=\
ndbreorder.$O\ ndbreorder.$O\
ndbsubstitute.$O\ ndbsubstitute.$O\
ndbdedup.$O\ ndbdedup.$O\
ndbvalfmt.$O\
HFILES=\ HFILES=\
/sys/include/ndb.h\ /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);
}