From 94e1f2a4388f674331b2a2f988fb815133d2ab6b Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Sun, 20 Jul 2008 04:15:41 -0400 Subject: rc: add subscript sequences (Erik Quanstrom) --- man/man1/rc.1 | 5 +++++ src/cmd/rc/exec.c | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/man/man1/rc.1 b/man/man1/rc.1 index e35d6258..3f9eb056 100644 --- a/man/man1/rc.1 +++ b/man/man1/rc.1 @@ -208,6 +208,11 @@ If is followed by a parenthesized list of subscripts, the value substituted is a list composed of the requested elements (origin 1). The parenthesis must follow the variable name with no spaces. +Subscripts can also take the form +.IB m - n +or +.IB m - +to indicate a sequence of elements. Assignments to variables are described below. .HP .BI $# argument diff --git a/src/cmd/rc/exec.c b/src/cmd/rc/exec.c index c54c0d89..ebb46b0d 100644 --- a/src/cmd/rc/exec.c +++ b/src/cmd/rc/exec.c @@ -685,22 +685,52 @@ Xqdol(void) efree(s); } +word* +copynwords(word *a, word *tail, int n) +{ + word *v, **end; + + v = 0; + end = &v; + while(n-- > 0){ + *end = newword(a->word, 0); + end = &(*end)->next; + a = a->next; + } + *end = tail; + return v; +} + word* subwords(word *val, int len, word *sub, word *a) { - int n; + int n, m; char *s; if(!sub) return a; a = subwords(val, len, sub->next, a); s = sub->word; deglob(s); + m = 0; n = 0; - while('0'<=*s && *s<='9') n = n*10+ *s++ -'0'; - if(n<1 || lenlen || m<0) return a; - for(;n!=1;--n) val = val->next; - return newword(val->word, a); + if(n+m>len) + m = len-n; + while(--n > 0) + val = val->next; + return copynwords(val, a, m+1); } void -- cgit v1.2.3