shithub: riscv

Download patch

ref: 0f653d0f293fbcc8fb8e6673b49743f8f1bee25a
parent: cd1f44b5c0443119be845ad9558c079cfe0c5bb0
author: aiju <devnull@localhost>
date: Mon Jun 12 15:24:32 EDT 2017

acid: add getfields() builtin

--- a/sys/src/cmd/acid/builtin.c
+++ b/sys/src/cmd/acid/builtin.c
@@ -42,6 +42,7 @@
 void	dosysr1(Node*, Node*);
 void	fmtof(Node*, Node*) ;
 void	dofmtsize(Node*, Node*) ;
+void	dogetfields(Node*, Node*);
 
 typedef struct Btab Btab;
 struct Btab
@@ -60,6 +61,7 @@
 	"fnbound",	funcbound,
 	"fmt",		fmt,
 	"follow",	follow,
+	"getfields",	dogetfields,
 	"itoa",		cvtitoa,
 	"kill",		kill,
 	"match",	match,
@@ -1313,4 +1315,39 @@
 	r->type = TINT ;
 	r->ival = fmtsize(&v) ;
 	r->fmt = 'D';
+}
+
+void
+dogetfields(Node  *r, Node *args)
+{
+	Node *av[Maxarg], nstr, ndelim, nmultif;
+	char *buf;
+	char *f[128];
+	int rc, i;
+	List *l, **lp;
+	
+	na = 0;
+	flatten(av, args);
+	if(na != 3)
+		error("getfields(str, delims, multiflag): arg count");
+	expr(av[0], &nstr);
+	expr(av[1], &ndelim);
+	expr(av[2], &nmultif);
+	if(nstr.type != TSTRING || ndelim.type != TSTRING)
+		error("getfields(str, delims, multiflag): arg type");
+	buf = strdup(nstr.string->string);
+	if(buf == nil)
+		fatal("out of memory");
+	rc = getfields(buf, f, nelem(f), bool(&nmultif), ndelim.string->string);
+	lp = &r->l;
+	for(i = 0; i < rc; i++){
+		l = al(TSTRING);
+		l->fmt = 's';
+		l->string = strnode(f[i]);
+		*lp = l;
+		lp = &l->next;
+	}
+	r->op = OCONST;
+	r->type = TLIST;
+	free(buf);
 }