Annotation of researchv10no/cmd/gre/buffer.c, revision 1.1

1.1     ! root        1: #include       "re.h"
        !             2: #include       "lre.h"
        !             3: #include       "hdr.h"
        !             4: 
        !             5: /*#define              DEBUG           /**/
        !             6: 
        !             7: /* the following ifdef aids testing of the buffering code */
        !             8: #ifndef        BUFSIZE
        !             9: #define        BUFSIZE 50000
        !            10: #endif
        !            11: 
        !            12: #define        OVERFLOW        (BUFSIZE/10)
        !            13: /*
        !            14:        lines less than BUFSIZE are preserved. larger lines have at least
        !            15:        BUFSIZE-OVERFLOW preserved
        !            16: */
        !            17: 
        !            18: static char rbuf[BUFSIZE+1] = "\n";
        !            19: static char *buf = rbuf+1;
        !            20: static char *next, *proc;
        !            21: /* invariants:
        !            22:        valid text in buffer:   buf <= text < next
        !            23:        text to be processed:   proc <= text < next
        !            24:        buf, proc always point at beginning of lines
        !            25: */
        !            26: 
        !            27: greprd(register char **b, register char **e)
        !            28: {
        !            29:        int n;
        !            30:        int keepingsome;
        !            31:        register char *p;
        !            32: 
        !            33:        if(*b == 0)                     /* set up invariants */
        !            34:                *b = *e = next = proc = buf;
        !            35:        keepingsome = *b != *e;
        !            36: again: /* this is only used for overflowing input lines */
        !            37: #ifdef DEBUG
        !            38:        fprint(2, "%d <> %d; keep=%d (%d'%.10s'..%d) proc=%d\n", next, &buf[BUFSIZE], keepingsome, *b, *b?*b:"", *e, proc);
        !            39: #endif
        !            40:        if(next < &buf[BUFSIZE]){
        !            41:                /*
        !            42:                        next is fine but *b may not be set
        !            43:                */
        !            44:                if(!keepingsome)
        !            45:                        *b = proc;
        !            46:        } else {
        !            47:                /*
        !            48:                        find a \n so we can shift the buffer
        !            49:                */
        !            50:                if(keepingsome){
        !            51:                        for(p = *b-1; p >= buf; p--)
        !            52:                                if(*p == '\n') break;
        !            53:                        p++;
        !            54:                        /* the best new buffer start is p */
        !            55:                        if(p == buf){   /* progressing? */
        !            56:        longline:
        !            57:                                if(!longlinewarned){
        !            58:                                        EPR "%s: %s: warning: ", progname, curfile);
        !            59:                                        if(bflag)
        !            60:                                                EPR "%ld: ", nbytes/offsetunit);
        !            61:                                        if(nflag)
        !            62:                                                EPR "%ld: ", lnum);
        !            63:                                        EPR "line too long (> %d chars); text skipped\n", BUFSIZE);
        !            64:                                        longlinewarned = 1;
        !            65:                                }
        !            66:                                next -= OVERFLOW;
        !            67:                                noverflow += OVERFLOW;
        !            68:                                goto again;
        !            69:                        }
        !            70:                } else {
        !            71:                        /* not keeping any; we only have to look at unprocessed */
        !            72:                        for(p = next-1; p >= proc; p--)
        !            73:                                if(*p == '\n') break;
        !            74:                        p++;
        !            75:                        if(p == buf)
        !            76:                                goto longline;
        !            77:                        *b = p;
        !            78:                }
        !            79:                /* process any we haven't */
        !            80:                if(proc < p){
        !            81:                        (*failfn)(proc, p);
        !            82:                        proc = p;
        !            83:                }
        !            84:                /* move it! */
        !            85:                n = p-buf;
        !            86:                memcpy(buf, p, next-p);
        !            87:                proc -= n;
        !            88:                next -= n;
        !            89:                *b -= n;
        !            90:        }
        !            91:        /*
        !            92:                *b points to start of returned (saved) text
        !            93:                next points to first available text for reading
        !            94:        */
        !            95:        FLUSH;
        !            96:        if((n = read(ifd, next, &buf[BUFSIZE] - next)) <= 0){
        !            97:                if(proc < next){
        !            98:                        (*failfn)(proc, next);
        !            99:                }
        !           100:                proc = next;
        !           101:                return(n);
        !           102:        }
        !           103:        next += n;
        !           104:        *e = next;
        !           105: #ifdef DEBUG
        !           106: fprint(2, "greprd returns %d .. %d\n", *b, *e);
        !           107: #endif
        !           108:        return(1);
        !           109: }
        !           110: 
        !           111: grepmatch(register char **b, register char **e)
        !           112: {
        !           113:        char *s, *f;
        !           114:        int eoffset, n, ret = 1;
        !           115: #ifdef DEBUG
        !           116: fprint(2, "match! *b=%d@%d='%.100s' *e=%d@%d\n", **b, *b, *b, **e, *e);/**/
        !           117: #endif
        !           118:        for(s = *b; s >= proc; s--)
        !           119:                if(*s == '\n')
        !           120:                        break;
        !           121:        if(s != *b)
        !           122:                s++;
        !           123:        if(proc < s){
        !           124:                (*failfn)(proc, s);
        !           125:                proc = s;
        !           126:        }
        !           127:        f = *e;
        !           128:        for(;;){
        !           129:                for(; f < next; f++)
        !           130:                        if(*f == '\n')
        !           131:                                goto done;
        !           132:                eoffset = f-s;
        !           133:                if((n = greprd(&s, &f)) <= 0){
        !           134:                        ret = n;
        !           135:                        goto done;
        !           136:                }
        !           137:                f = s+eoffset;
        !           138:        }
        !           139: done:
        !           140:        f++;
        !           141:        if(s > f)
        !           142:                abort();
        !           143:        (*succfn)(s, f);
        !           144:        proc = *b = f;
        !           145:        *e = next;
        !           146: #ifdef DEBUG
        !           147: fprint(2, "match at '%.20s'; resuming at '%.20s'@%d\n", s, f, f);/**/
        !           148: #endif
        !           149:        return(ret);
        !           150: }
        !           151: 
        !           152: cwxrd(register char **b, register char **e)
        !           153: {
        !           154:        int n;
        !           155: 
        !           156:        n = greprd(b, e);
        !           157:        if(n > 0){
        !           158:                (*b)--;
        !           159: #ifdef DEBUG
        !           160:                fprint(2, "grepxrd returns %d .. %d\n", *b, *e);
        !           161: #endif
        !           162:        }
        !           163:        return(n);
        !           164: }
        !           165: 
        !           166: cwxmatch(register char **b, register char **e)
        !           167: {
        !           168:        char *s, *f;
        !           169:        int eoffset, n, ret = 1;
        !           170: 
        !           171: #ifdef DEBUG
        !           172: fprint(2, "cwxmatch! *b=%d@%d='%.100s' *e=%d@%d\n", **b, *b, *b, **e, *e);/**/
        !           173: #endif
        !           174:        for(s = *b; s >= proc; s--)
        !           175:                if(*s == '\n')
        !           176:                        break;
        !           177:        s++;
        !           178:        if(proc < s){
        !           179: /*
        !           180: fprint(2, "cwxfail! *b=%d@%d='%.50s' *e=%d@%d\n", **b, *b, *b, **e, *e);
        !           181: fprint(2, "s=%d, proc=%d, dbg.b=%d dbg.e=%d dbg.resume=%d\n", s, proc,dbg.b, dbg.e, dbg.resume);
        !           182: */
        !           183:                (*failfn)(proc, s);
        !           184:                proc = s;
        !           185:        }
        !           186:        f = *e - 1;
        !           187:        for(;;){
        !           188:                for(; f < next; f++)
        !           189:                        if(*f == '\n')
        !           190:                                goto done;
        !           191:                eoffset = f-s;
        !           192:                if((n = greprd(&s, &f)) <= 0){
        !           193:                        ret = n;
        !           194:                        goto done;
        !           195:                }
        !           196:                f = s+eoffset;
        !           197:        }
        !           198: done:
        !           199:        f++;
        !           200:        if(s > f)
        !           201:                abort();
        !           202:        (*succfn)(s, f);
        !           203:        proc = *b = f;
        !           204:        *e = next;
        !           205:        (*b)--;
        !           206: #ifdef DEBUG
        !           207: fprint(2, "cwxmatch at '%.20s'; resuming at '%.20s'@%d\n", s, f, f);/**/
        !           208: #endif
        !           209:        return(ret);
        !           210: }
        !           211: 
        !           212: bmxmatch(register char **b, register char **e)
        !           213: {
        !           214:        char *s, *f;
        !           215:        int eoffset, n, ret = 1;
        !           216: 
        !           217: #ifdef DEBUG
        !           218: fprint(2, "bmxmatch! *b=%d@%d='%.100s' *e=%d@%d\n", **b, *b, *b, **e, *e);/**/
        !           219: #endif
        !           220:        for(s = *b; s >= proc; s--)
        !           221:                if(*s == '\n')
        !           222:                        break;
        !           223:        s++;
        !           224:        if(proc < s){
        !           225:                (*failfn)(proc, s);
        !           226:                proc = s;
        !           227:        }
        !           228:        f = *e - 1;
        !           229:        for(;;){
        !           230:                for(; f < next; f++)
        !           231:                        if(*f == '\n')
        !           232:                                goto done;
        !           233:                eoffset = f-s;
        !           234:                if((n = greprd(&s, &f)) <= 0){
        !           235:                        ret = n;
        !           236:                        goto done;
        !           237:                }
        !           238:                f = s+eoffset;
        !           239:        }
        !           240: done:
        !           241:        f++;
        !           242:        if(s > f)
        !           243:                abort();
        !           244:        (*((*b == s)? succfn:failfn))(s, f);
        !           245:        proc = *b = f;
        !           246:        *e = next;
        !           247: #ifdef DEBUG
        !           248: fprint(2, "bmxmatch at '%.20s'; resuming at '%.20s'\n", s, f);/**/
        !           249: #endif
        !           250:        return(ret);
        !           251: }

unix.superglobalmegacorp.com

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