📄 dol.c
字号:
} if (ffile == 0) stderror(ERR_DOLZERO); fixDolMod(); setDolp(ffile); goto eatbrac; } if (bitset) stderror(ERR_DOLQUEST); vp = adrof(STRargv); if (vp == 0) { vp = &nulargv; goto eatmod; } break; } if (!alnum(c)) stderror(ERR_VARALNUM); for (;;) { *np++ = c; c = DgetC(0); if (!alnum(c)) break; if (np >= &name[MAXVARLEN]) stderror(ERR_VARTOOLONG); } *np++ = 0; unDredc(c); vp = adrof(name); } if (bitset) { dolp = (vp || getenv(short2str(name))) ? STR1 : STR0; goto eatbrac; } if (vp == 0) { np = str2short(getenv(short2str(name))); if (np) { fixDolMod(); setDolp(np); goto eatbrac; } udvar(name); /* NOTREACHED */ } c = DgetC(0); upb = blklen(vp->vec); if (dimen == 0 && subscr == 0 && c == '[') { np = name; for (;;) { c = DgetC(DODOL); /* Allow $ expand within [ ] */ if (c == ']') break; if (c == '\n' || c == DEOF) stderror(ERR_INCBR); if (np >= &name[sizeof(name) / sizeof(Char) - 2]) stderror(ERR_VARTOOLONG); *np++ = c; } *np = 0, np = name; if (dolp || dolcnt) /* $ exp must end before ] */ stderror(ERR_EXPORD); if (!*np) stderror(ERR_SYNTAX); if (Isdigit(*np)) { int i; for (i = 0; Isdigit(*np); i = i * 10 + *np++ - '0') continue; if ((i < 0 || i > upb) && !any("-*", *np)) { dolerror(vp->v_name); return; } lwb = i; if (!*np) upb = lwb, np = STRstar; } if (*np == '*') np++; else if (*np != '-') stderror(ERR_MISSING, '-'); else { register int i = upb; np++; if (Isdigit(*np)) { i = 0; while (Isdigit(*np)) i = i * 10 + *np++ - '0'; if (i < 0 || i > upb) { dolerror(vp->v_name); return; } } if (i < lwb) upb = lwb - 1; else upb = i; } if (lwb == 0) { if (upb != 0) { dolerror(vp->v_name); return; } upb = -1; } if (*np) stderror(ERR_SYNTAX); } else { if (subscr > 0) if (subscr > upb) lwb = 1, upb = 0; else lwb = upb = subscr; unDredc(c); } if (dimen) { Char *cp = putn(upb - lwb + 1); addla(cp); xfree((ptr_t) cp); } else {eatmod: fixDolMod(); dolnxt = &vp->vec[lwb - 1]; dolcnt = upb - lwb + 1; }eatbrac: if (sc == '{') { c = Dredc(); if (c != '}') stderror(ERR_MISSING, '}'); }}static voidfixDolMod(){ register int c; c = DgetC(0); if (c == ':') { do { c = DgetC(0), dolmcnt = 1, dolwcnt = 1; if (c == 'g' || c == 'a') { if (c == 'g') dolmcnt = 10000; else dolwcnt = 10000; c = DgetC(0); } if ((c == 'g' && dolmcnt != 10000) || (c == 'a' && dolwcnt != 10000)) { if (c == 'g') dolmcnt = 10000; else dolwcnt = 10000; c = DgetC(0); } if (c == 's') { /* [eichin:19910926.0755EST] */ int delimcnt = 2; int delim = DgetC(0); dolmod[dolnmod++] = c; dolmod[dolnmod++] = delim; if (!delim || letter(delim) || Isdigit(delim) || any(" \t\n", delim)) { seterror(ERR_BADSUBST); break; } while ((c = DgetC(0)) != (-1)) { dolmod[dolnmod++] = c; if(c == delim) delimcnt--; if(!delimcnt) break; } if(delimcnt) { seterror(ERR_BADSUBST); break; } continue; } if (!any("htrqxes", c)) stderror(ERR_BADMOD, c); dolmod[dolnmod++] = c; if (c == 'q') dolmcnt = 10000; } while ((c = DgetC(0)) == ':'); unDredc(c); } else unDredc(c);}static voidsetDolp(cp) register Char *cp;{ register Char *dp; int i; if (dolnmod == 0 || dolmcnt == 0) { dolp = cp; return; } dp = cp = Strsave(cp); for (i = 0; i < dolnmod; i++) { /* handle s// [eichin:19910926.0510EST] */ if(dolmod[i] == 's') { int delim; Char *lhsub, *rhsub, *np; size_t lhlen = 0, rhlen = 0; int didmod = 0; delim = dolmod[++i]; if (!delim || letter(delim) || Isdigit(delim) || any(" \t\n", delim)) { seterror(ERR_BADSUBST); break; } lhsub = &dolmod[++i]; while(dolmod[i] != delim && dolmod[++i]) { lhlen++; } dolmod[i] = 0; rhsub = &dolmod[++i]; while(dolmod[i] != delim && dolmod[++i]) { rhlen++; } dolmod[i] = 0; do { dp = Strstr(cp, lhsub); if (dp) { np = (Char *) xmalloc((size_t) ((Strlen(cp) + 1 - lhlen + rhlen) * sizeof(Char))); (void) Strncpy(np, cp, dp - cp); (void) Strcpy(np + (dp - cp), rhsub); (void) Strcpy(np + (dp - cp) + rhlen, dp + lhlen); xfree((ptr_t) cp); dp = cp = np; didmod = 1; } else { /* should this do a seterror? */ break; } } while (dolwcnt == 10000); /* * restore dolmod for additional words */ dolmod[i] = rhsub[-1] = delim; if (didmod) dolmcnt--; else break; } else { int didmod = 0; do { if ((dp = domod(cp, dolmod[i]))) { didmod = 1; if (Strcmp(cp, dp) == 0) { xfree((ptr_t) cp); cp = dp; break; } else { xfree((ptr_t) cp); cp = dp; } } else break; } while (dolwcnt == 10000); dp = cp; if (didmod) dolmcnt--; else break; } } if (dp) { addla(dp); xfree((ptr_t) dp); } else addla(cp); dolp = STRNULL; if (seterr) stderror(ERR_OLD);}static voidunDredc(c) int c;{ Dpeekrd = c;}static intDredc(){ register int c; if ((c = Dpeekrd) != '\0') { Dpeekrd = 0; return (c); } if (Dcp && (c = *Dcp++)) return (c & (QUOTE | TRIM)); if (*Dvp == 0) { Dcp = 0; return (DEOF); } Dcp = *Dvp++; return (' ');}static voidDtestq(c) register int c;{ if (cmap(c, QUOTES)) gflag = 1;}/* * Form a shell temporary file (in unit 0) from the words * of the shell input up to EOF or a line the same as "term". * Unit 0 should have been closed before this call. */void/*ARGSUSED*/heredoc(term) Char *term;{ register int c; Char *Dv[2]; Char obuf[BUFSIZ], lbuf[BUFSIZ], mbuf[BUFSIZ]; int ocnt, lcnt, mcnt; register Char *lbp, *obp, *mbp; Char **vp; bool quoted; char *tmp; if (creat(tmp = short2str(shtemp), 0600) < 0) stderror(ERR_SYSTEM, tmp, strerror(errno)); (void) close(0); if (open(tmp, O_RDWR) < 0) { int oerrno = errno; (void) unlink(tmp); errno = oerrno; stderror(ERR_SYSTEM, tmp, strerror(errno)); } (void) unlink(tmp); /* 0 0 inode! */ Dv[0] = term; Dv[1] = NULL; gflag = 0; trim(Dv); rscan(Dv, Dtestq); quoted = gflag; ocnt = BUFSIZ; obp = obuf; for (;;) { /* * Read up a line */ lbp = lbuf; lcnt = BUFSIZ - 4; for (;;) { c = readc(1); /* 1 -> Want EOF returns */ if (c < 0 || c == '\n') break; if ((c &= TRIM) != '\0') { *lbp++ = c; if (--lcnt < 0) { setname("<<"); stderror(ERR_NAME | ERR_OVERFLOW); } } } *lbp = 0; /* * Check for EOF or compare to terminator -- before expansion */ if (c < 0 || eq(lbuf, term)) { (void) write(0, short2str(obuf), (size_t) (BUFSIZ - ocnt)); (void) lseek(0, 0l, L_SET); return; } /* * If term was quoted or -n just pass it on */ if (quoted || noexec) { *lbp++ = '\n'; *lbp = 0; for (lbp = lbuf; (c = *lbp++) != '\0';) { *obp++ = c; if (--ocnt == 0) { (void) write(0, short2str(obuf), BUFSIZ); obp = obuf; ocnt = BUFSIZ; } } continue; } /* * Term wasn't quoted so variable and then command expand the input * line */ Dcp = lbuf; Dvp = Dv + 1; mbp = mbuf; mcnt = BUFSIZ - 4; for (;;) { c = DgetC(DODOL); if (c == DEOF) break; if ((c &= TRIM) == 0) continue; /* \ quotes \ $ ` here */ if (c == '\\') { c = DgetC(0); if (!any("$\\`", c)) unDgetC(c | QUOTE), c = '\\'; else c |= QUOTE; } *mbp++ = c; if (--mcnt == 0) { setname("<<"); stderror(ERR_NAME | ERR_OVERFLOW); } } *mbp++ = 0; /* * If any ` in line do command substitution */ mbp = mbuf; if (any(short2str(mbp), '`')) { /* * 1 arg to dobackp causes substitution to be literal. Words are * broken only at newlines so that all blanks and tabs are * preserved. Blank lines (null words) are not discarded. */ vp = dobackp(mbuf, 1); } else /* Setup trivial vector similar to return of dobackp */ Dv[0] = mbp, Dv[1] = NULL, vp = Dv; /* * Resurrect the words from the command substitution each separated by * a newline. Note that the last newline of a command substitution * will have been discarded, but we put a newline after the last word * because this represents the newline after the last input line! */ for (; *vp; vp++) { for (mbp = *vp; *mbp; mbp++) { *obp++ = *mbp & TRIM; if (--ocnt == 0) { (void) write(0, short2str(obuf), BUFSIZ); obp = obuf; ocnt = BUFSIZ; } } *obp++ = '\n'; if (--ocnt == 0) { (void) write(0, short2str(obuf), BUFSIZ); obp = obuf; ocnt = BUFSIZ; } } if (pargv) blkfree(pargv), pargv = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -