commit 94e1f2a4388f674331b2a2f988fb815133d2ab6b from: Russ Cox date: Sun Jul 20 08:15:41 2008 UTC rc: add subscript sequences (Erik Quanstrom) commit - 78a779a3834cf39d7c0bcd93a15824b29df947a3 commit + 94e1f2a4388f674331b2a2f988fb815133d2ab6b blob - e35d625864541053d8e21d9fb07ba2007141cc3c blob + 3f9eb0563ce67e21317b7d2d4213c16f9216e803 --- man/man1/rc.1 +++ 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 blob - c54c0d89e03e33ee1bf9610a335d3ea9ab32a0ed blob + ebb46b0de71a151b3948ce83e0d77ad9cc2bfa17 --- src/cmd/rc/exec.c +++ src/cmd/rc/exec.c @@ -686,21 +686,51 @@ Xqdol(void) } 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); - 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