git: plan9front

Download patch

ref: 146b4dd7973fef0efd9b561b2a8eb61242ca1eec
parent: 022d758cf959641219001de170ba701844e06eba
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Mar 22 13:59:46 EDT 2025

ndb/cs: quote attribute value for csgetvalue() and csipinfo()

Some attribute values such as vendor= can contain spaces
and must be quoted.

To parse the !query commands in ndb/cs, use tokenize() instead
of getfields().

This handles "attr='val with spaces'" -> "attr=val with spaces",
removing the quotes from the output as neccessary.

--- a/sys/src/cmd/ndb/cs.c
+++ b/sys/src/cmd/ndb/cs.c
@@ -1184,7 +1184,7 @@
 	char *field[12];
 	int i, n;
 
-	n = getfields(p, field, 12, 1, " ");
+	n = getfields(p, field, nelem(field), 1, " ");
 	for(i = 0; i < n; i++){
 		for(np = network; np->net != nil; np++){
 			if(strcmp(field[i], np->net) != 0)
@@ -1845,7 +1845,12 @@
 	Ndbtuple *t;
 	Ndbs s;
 
-	n = getfields(query, attr, nelem(attr), 1, " ");
+	/*
+	 * this was using getfields(), but some attribute values
+	 * such as vendor can contain spaces, so need to
+	 * be quoted like: vendor='value with spaces'
+	 */
+	n = tokenize(query, attr, nelem(attr));
 	if(n == 0)
 		return "bad query";
 
--- a/sys/src/libndb/csgetval.c
+++ b/sys/src/libndb/csgetval.c
@@ -29,7 +29,9 @@
 	if(fd < 0)
 		return nil;
 
-	snprint(line, sizeof(line), "!%s=%s %s=*", attr, val, rattr);
+	quotefmtinstall();	/* just in case */
+
+	snprint(line, sizeof(line), "!%s=%q %s=*", attr, val, rattr);
 	if(write(fd, line, strlen(line)) < 0){
 		close(fd);
 		return nil;
--- a/sys/src/libndb/csipinfo.c
+++ b/sys/src/libndb/csipinfo.c
@@ -27,8 +27,10 @@
 	if(fd < 0)
 		return nil;
 
+	quotefmtinstall();	/* just in case */
+
 	e = line + sizeof(line);
-	p = seprint(line, e, "!ipinfo %s=%s", attr, val);
+	p = seprint(line, e, "!ipinfo %s=%q", attr, val);
 	for(i = 0; i < n; i++){
 		if(*list == nil)
 			break;
--