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

unix.superglobalmegacorp.com

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