shithub: riscv

Download patch

ref: fc8df2c9ce66ed92b126146598ebc0af30e475b7
parent: ed4a0c0642a2411f73f7ea9aff286fdf84710b22
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun May 28 12:10:53 EDT 2023

ndb/cs: Use ndbvalfmt() for quoting !ipinfo replies

--- a/sys/src/cmd/ndb/cs.c
+++ b/sys/src/cmd/ndb/cs.c
@@ -6,7 +6,6 @@
 #include <ctype.h>
 #include <ndb.h>
 #include <ip.h>
-#include <String.h>
 
 enum
 {
@@ -187,6 +186,22 @@
 Network *netlist;		/* networks ordered by preference */
 Network *last;
 
+#pragma varargck type "$" char*
+#pragma varargck type "N" Ndbtuple*
+
+static int
+ndblinefmt(Fmt *f)
+{
+	Ndbtuple *t;
+
+	for(t = va_arg(f->args, Ndbtuple*); t != nil; t = t->entry) {
+		fmtprint(f, "%s=%$ ", t->attr, t->val);
+		if(t->line != t->entry)
+			break;
+	}
+	return 0;
+}
+
 static void
 nstrcpy(char *to, char *from, int len)
 {
@@ -241,6 +256,9 @@
 	fmtinstall('M', eipfmt);
 	fmtinstall('F', fcallfmt);
 
+	fmtinstall('$', ndbvalfmt);
+	fmtinstall('N', ndblinefmt);
+
 	ndbinit();
 	netinit(0);
 
@@ -1775,23 +1793,21 @@
 void
 qreply(Mfile *mf, Ndbtuple *t)
 {
-	Ndbtuple *nt;
-	String *s;
+	while(mf->nreply < Nreply && t != nil) {
+		char *line = smprint("%N", t);
+		if(line == nil)
+			break;
+		mf->reply[mf->nreply] = line;
+		mf->replylen[mf->nreply++] = strlen(line);
 
-	s = s_new();
-	for(nt = t; mf->nreply < Nreply && nt != nil; nt = nt->entry){
-		s_append(s, nt->attr);
-		s_append(s, "=");
-		s_append(s, nt->val);
-
-		if(nt->line != nt->entry){
-			mf->replylen[mf->nreply] = s_len(s);
-			mf->reply[mf->nreply++] = estrdup(s_to_c(s));
-			s_restart(s);
-		} else
-			s_append(s, " ");
+		/* skip to next line */
+		do {
+			Ndbtuple *l = t->line;
+			t = t->entry;
+			if(t != l)
+				break;
+		} while(t != nil);
 	}
-	s_free(s);
 }
 
 enum