Annotation of researchv9/cmd/sh/word.c, revision 1.1

1.1     ! root        1: /*     @(#)word.c      1.4     */
        !             2: /*
        !             3:  * UNIX shell
        !             4:  *
        !             5:  * Bell Telephone Laboratories
        !             6:  *
        !             7:  */
        !             8: 
        !             9: #include       "defs.h"
        !            10: #include       "sym.h"
        !            11: #include       <errno.h>
        !            12: 
        !            13: static int     readb();
        !            14: static int     histc();
        !            15: 
        !            16: /* ========    character handling for command lines    ========*/
        !            17: 
        !            18: 
        !            19: word()
        !            20: {
        !            21:        register char   c, d;
        !            22:        int             alpha = 1;
        !            23:        int             lbrok, rbrok;
        !            24: 
        !            25:        wdnum = 0;
        !            26:        wdset = 0;
        !            27: 
        !            28:        while (1)
        !            29:        {
        !            30:                while (c = nextc(0), space(c))          /* skipc() */
        !            31:                        ;
        !            32: 
        !            33:                if (c == COMCHAR)
        !            34:                {
        !            35:                        while ((c = readc()) != NL && c != EOF);
        !            36:                        peekc = c;
        !            37:                }
        !            38:                else
        !            39:                {
        !            40:                        break;  /* out of comment - white space loop */
        !            41:                }
        !            42:        }
        !            43:        if (!eofmeta(c))
        !            44:        {
        !            45:                struct argnod   *arg = (struct argnod *)locstak();
        !            46:                /* pushstak() starting at arg->argval[0] */
        !            47: 
        !            48:                staktop += BYTESPERWORD;
        !            49: 
        !            50:                lbrok = rbrok = 0;
        !            51:                do
        !            52:                {
        !            53:                        if (c == LITERAL)
        !            54:                        {
        !            55:                                pushstak(DQUOTE);
        !            56:                                while ((c = readc()) && c != LITERAL)
        !            57:                                {
        !            58:                                        pushstak(c | QUOTE);
        !            59:                                        if (c == NL)
        !            60:                                                chkpr();
        !            61:                                }
        !            62:                                pushstak(DQUOTE);
        !            63:                        }
        !            64:                        else
        !            65:                        {
        !            66:                                pushstak(c);
        !            67:                                if (c == '=')
        !            68:                                        wdset |= alpha;
        !            69:                                /* hack for ${} */
        !            70:                                else if (c == '$')
        !            71:                                        lbrok++;
        !            72:                                else if (lbrok)
        !            73:                                {
        !            74:                                        if (c == '{')
        !            75:                                                rbrok++;
        !            76:                                        lbrok = 0;
        !            77:                                }
        !            78:                                else if (rbrok && c == '}')
        !            79:                                        rbrok--;
        !            80:                                if (!alphanum(c))
        !            81:                                        alpha = 0;
        !            82:                                if (qotchar(c))
        !            83:                                {
        !            84:                                        d = c;
        !            85:                                        while ((pushstak(c = nextc(d)), c) && c != d)
        !            86:                                        {
        !            87:                                                if (c == NL)
        !            88:                                                        chkpr();
        !            89:                                        }
        !            90:                                }
        !            91:                        }
        !            92:                } while ((c = nextc(0), !eofmeta(c) || (lbrok && c=='{') || (rbrok && c=='}')));
        !            93:                fixstak();
        !            94:                if (!letter(arg->argval[0]))
        !            95:                        wdset = 0;
        !            96: 
        !            97:                peekn = c | MARK;
        !            98:                if (arg->argval[1] == 0 && 
        !            99:                    (d = arg->argval[0], digit(d)) && 
        !           100:                    (c == '>' || c == '<'))
        !           101:                {
        !           102:                        word();
        !           103:                        wdnum = d - '0';
        !           104:                }
        !           105:                else            /*check for reserved words*/
        !           106:                {
        !           107:                        if (reserv == FALSE || (wdval = syslook(arg->argval,reserved, no_reserved)) == 0)
        !           108:                        {
        !           109:                                wdarg = arg;
        !           110:                                wdval = 0;
        !           111:                        }
        !           112:                }
        !           113:        }
        !           114:        else if (dipchar(c))
        !           115:        {
        !           116:                if ((d = nextc(0)) == c)
        !           117:                {
        !           118:                        wdval = c | SYMREP;
        !           119:                        if (c == '<')
        !           120:                                peekn = nextc(0) | MARK;
        !           121:                }
        !           122:                else
        !           123:                {
        !           124:                        peekn = d | MARK;
        !           125:                        wdval = c;
        !           126:                }
        !           127:        }
        !           128:        else
        !           129:        {
        !           130:                if ((wdval = c) == EOF)
        !           131:                        wdval = EOFSYM;
        !           132:                if (iopend && eolchar(c))
        !           133:                {
        !           134:                        copy(iopend);
        !           135:                        iopend = 0;
        !           136:                }
        !           137:        }
        !           138:        reserv = FALSE;
        !           139:        return(wdval);
        !           140: }
        !           141: 
        !           142: skipc()
        !           143: {
        !           144:        register char c;
        !           145: 
        !           146:        while (c = nextc(0), space(c))
        !           147:                ;
        !           148:        return(c);
        !           149: }
        !           150: 
        !           151: nextc(quote)
        !           152: char   quote;
        !           153: {
        !           154:        register char   c, d;
        !           155: 
        !           156: retry:
        !           157:        if ((d = readc()) == ESCAPE)
        !           158:        {
        !           159:                if ((c = readc()) == NL)
        !           160:                {
        !           161:                        chkpr();
        !           162:                        goto retry;
        !           163:                }
        !           164:                else if (quote && c != quote && !escchar(c))
        !           165:                        peekc = c | MARK;
        !           166:                else
        !           167:                        d = c | QUOTE;
        !           168:        }
        !           169:        return(d);
        !           170: }
        !           171: readc()
        !           172: {
        !           173:        register char   c;
        !           174:        register int    len;
        !           175:        register struct fileblk *f;
        !           176: 
        !           177:        if (peekn)
        !           178:        {
        !           179:                peekc = peekn;
        !           180:                peekn = 0;
        !           181:        }
        !           182:        if (peekc)
        !           183:        {
        !           184:                c = peekc;
        !           185:                peekc = 0;
        !           186:                return(c);
        !           187:        }
        !           188:        f = standin;
        !           189: retry:
        !           190:        if (f->fnxt != f->fend)
        !           191:        {
        !           192:                if ((c = *f->fnxt++) == 0)
        !           193:                {
        !           194:                        if (f->feval)
        !           195:                        {
        !           196:                                if (estabf(*f->feval++))
        !           197:                                        c = EOF;
        !           198:                                else
        !           199:                                        c = SP;
        !           200:                        }
        !           201:                        else
        !           202:                                goto retry;     /* = c = readc(); */
        !           203:                }
        !           204:                if (standin->fstak == 0) {
        !           205:                        if (flags & readpr)
        !           206:                                prc(c);
        !           207:                        if (flags & prompt && histnod.namval.val)
        !           208:                                histc (c);
        !           209:                }
        !           210:                if (c == NL)
        !           211:                        f->flin++;
        !           212:        }
        !           213:        else if (f->feof || f->fdes < 0)
        !           214:        {
        !           215:                c = EOF;
        !           216:                f->feof++;
        !           217:        }
        !           218:        else if ((len = readb()) <= 0)
        !           219:        {
        !           220:                close(f->fdes);
        !           221:                f->fdes = -1;
        !           222:                c = EOF;
        !           223:                f->feof++;
        !           224:        }
        !           225:        else
        !           226:        {
        !           227:                f->fend = (f->fnxt = f->fbuf) + len;
        !           228:                goto retry;
        !           229:        }
        !           230:        return(c);
        !           231: }
        !           232: 
        !           233: static
        !           234: readb()
        !           235: {
        !           236:        register struct fileblk *f = standin;
        !           237:        register int    len;
        !           238: 
        !           239:        do
        !           240:        {
        !           241:                if (trapnote & SIGSET)
        !           242:                {
        !           243:                        newline();
        !           244:                        sigchk();
        !           245:                }
        !           246:                else if ((trapnote & TRAPSET) && (rwait > 0))
        !           247:                {
        !           248:                        newline();
        !           249:                        chktrap();
        !           250:                        clearup();
        !           251:                }
        !           252:        } while ((len = read(f->fdes, f->fbuf, f->fsiz)) < 0 && errno==EINTR && trapnote);
        !           253:        return(len);
        !           254: }
        !           255: 
        !           256: int histfd = 0;
        !           257: static histc (c)
        !           258: {
        !           259:        static char histbuf[256];
        !           260:        static char *histp=histbuf;
        !           261: 
        !           262:        if (histfd == 0) {
        !           263:                if ((histfd = open(histnod.namval.val, 1)) < 0 &&
        !           264:                    (histfd = creat(histnod.namval.val, 0666)) < 0)
        !           265:                        return;
        !           266:        }
        !           267:        if (histfd > 0) {
        !           268:                *histp++ = c;
        !           269:                if (c == '\n' || c == ';' || histp >= &histbuf[sizeof histbuf]) {
        !           270:                        lseek(histfd, 0L, 2);
        !           271:                        write(histfd, histbuf, histp-histbuf);
        !           272:                        histp = histbuf;
        !           273:                }
        !           274:        }
        !           275: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.