summaryrefslogtreecommitdiff
path: root/sys/src/cmd/acid
diff options
context:
space:
mode:
authoraiju <devnull@localhost>2017-06-12 19:24:32 +0000
committeraiju <devnull@localhost>2017-06-12 19:24:32 +0000
commit0f653d0f293fbcc8fb8e6673b49743f8f1bee25a (patch)
treef3459ea55f6cbe96331536e2330bb2d5d01c4d15 /sys/src/cmd/acid
parentcd1f44b5c0443119be845ad9558c079cfe0c5bb0 (diff)
acid: add getfields() builtin
Diffstat (limited to 'sys/src/cmd/acid')
-rw-r--r--sys/src/cmd/acid/builtin.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/sys/src/cmd/acid/builtin.c b/sys/src/cmd/acid/builtin.c
index e36a5dbdd..bfcb19d4a 100644
--- a/sys/src/cmd/acid/builtin.c
+++ b/sys/src/cmd/acid/builtin.c
@@ -42,6 +42,7 @@ void regexp(Node*, Node*);
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 @@ struct Btab
"fnbound", funcbound,
"fmt", fmt,
"follow", follow,
+ "getfields", dogetfields,
"itoa", cvtitoa,
"kill", kill,
"match", match,
@@ -1314,3 +1316,38 @@ void dofmtsize(Node *r, Node *args)
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);
+}