shithub: riscv

Download patch

ref: ccf5d3fb9db7e58a9b9177f41edaf045b9294f86
parent: 0dc9c8d0199db2746f2ca651911e8fa60e90e02f
author: kvik <[email protected]>
date: Wed May 6 20:10:09 EDT 2020

aux/getflags: improve flagfmt parser

This makes the flagfmt parser more robust and accepting
a looser input language — namely by allowing whitespace
around specifier fields and ignoring any empty fields.

Long flagfmts can thus be pleasingly displayed:

	flagfmt='
		a, b, c, C:cache,
		m:mtpt mountpoint,
		s:srvn srvname'

--- a/sys/man/8/getflags
+++ b/sys/man/8/getflags
@@ -71,7 +71,7 @@
 An example of the script generated:
 .IP
 .EX
-% flagfmt='e:example,x,a:arg with args'
+% flagfmt='e:example, x, a:arg with args'
 % aux/getflags -exa arg list positional stuff
 example=()
 flagx=()
@@ -87,7 +87,7 @@
 .IR leak (1):
 .IP
 .EX
-flagfmt='b:showbmp,s:acidfmt,f binary,r res,x width'
+flagfmt='b:showbmp, s:acidfmt, f binary, r res, x width'
 args='name | pid list'
 if(! ifs=() eval `{aux/getflags $*} || ~ $#* 0){
 	aux/usage
--- a/sys/src/cmd/aux/getflags.c
+++ b/sys/src/cmd/aux/getflags.c
@@ -10,17 +10,38 @@
 }
 
 char*
+skipspace(char *p)
+{
+	while(isspace(*p))
+		p++;
+	return p;
+}
+
+char*
+nextarg(char *p)
+{
+	char *s;
+
+	s = strchr(p, ',');
+	if(s == nil)
+		return p+strlen(p); /* to \0 */
+	while(*s == ',' || isspace(*s))
+		s++;
+	return s;
+}
+
+char*
 findarg(char *flags, Rune r)
 {
 	char *p;
 	Rune rr;
-	
-	for(p=flags; p!=(char*)1; p=strchr(p, ',')+1){
+
+	for(p=skipspace(flags); *p; p=nextarg(p)){
 		chartorune(&rr, p);
 		if(rr == r)
 			return p;
 	}
-	return nil;	
+	return nil;
 }
 
 char*
@@ -44,10 +65,9 @@
 	int n;
 
 	n = 1;
-	while(*p == ' ')
-		p++;
-	for(; *p && *p != ','; p++)
-		if(*p == ' ' && *(p-1) != ' ')
+	print("%s\n", p);
+	for(p=skipspace(p); *p && *p != ','; p++)
+		if(isspace(*p) && !isspace(*(p-1)))
 			n++;
 	return n;
 }
@@ -71,7 +91,7 @@
 	}
 
 	fmtfdinit(&fmt, 1, buf, sizeof buf);
-	for(p=flags; p!=(char*)1 && *p != 0; p=strchr(p, ',')+1){
+	for(p=skipspace(flags); *p; p=nextarg(p)){
 		s = e = nil;
 		if(p[1] == ':'){
 			s = p + 2;
--- a/sys/src/cmd/aux/usage.c
+++ b/sys/src/cmd/aux/usage.c
@@ -1,5 +1,6 @@
 #include <u.h>
 #include <libc.h>
+#include <ctype.h>
 
 void
 main(void)
@@ -30,9 +31,11 @@
 	if(flags[0]){
 		single = 0;
 		for(p=flags; *p; ){
+			while(isspace(*p))
+				p++;
 			p += chartorune(&r, p);
 			if(*p == ':')
-				while(*p != '\0' && *p != ',' && *p != ' ')
+				while(*p && *p != ',' && !isspace(*p))
 					p++;
 			if(*p == ',' || *p == 0){
 				if(!single){
@@ -44,7 +47,7 @@
 					p++;
 				continue;
 			}
-			while(*p == ' ')
+			while(isspace(*p))
 				p++;
 			if(single){
 				fmtprint(&fmt, "]");