From ed4a0c0642a2411f73f7ea9aff286fdf84710b22 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sun, 28 May 2023 16:09:18 +0000 Subject: [PATCH] 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. --- sys/include/ndb.h | 1 + sys/man/2/ndb | 26 +++++++++++++++++++++++++- sys/src/libndb/mkfile | 1 + sys/src/libndb/ndbvalfmt.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 sys/src/libndb/ndbvalfmt.c diff --git a/sys/include/ndb.h b/sys/include/ndb.h index 1a9ab7742..d65ec5d2d 100644 --- a/sys/include/ndb.h +++ b/sys/include/ndb.h @@ -151,3 +151,4 @@ Ndbtuple* ndbsnext(Ndbs*, char*, char*); Ndbtuple* ndbsubstitute(Ndbtuple*, Ndbtuple*, Ndbtuple*); Ndbtuple* ndbdedup(Ndbtuple*); void ndbsetmalloctag(Ndbtuple*, uintptr); +int ndbvalfmt(Fmt*); diff --git a/sys/man/2/ndb b/sys/man/2/ndb index 4df54857d..f0ecdf0ef 100644 --- a/sys/man/2/ndb +++ b/sys/man/2/ndb @@ -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 .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 diff --git a/sys/src/libndb/mkfile b/sys/src/libndb/mkfile index 390864841..5939efc8d 100644 --- a/sys/src/libndb/mkfile +++ b/sys/src/libndb/mkfile @@ -23,6 +23,7 @@ OFILES=\ ndbreorder.$O\ ndbsubstitute.$O\ ndbdedup.$O\ + ndbvalfmt.$O\ HFILES=\ /sys/include/ndb.h\ diff --git a/sys/src/libndb/ndbvalfmt.c b/sys/src/libndb/ndbvalfmt.c new file mode 100644 index 000000000..31064edba --- /dev/null +++ b/sys/src/libndb/ndbvalfmt.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#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); +}