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

1.1     ! root        1: #include       <ctype.h>
        !             2: #include       "re.h"
        !             3: #include       "lre.h"
        !             4: #include       "hdr.h"
        !             5: 
        !             6: static int addwords(char*, re_cw*, unsigned char**, unsigned char**);
        !             7: 
        !             8: void
        !             9: dofgrep(char *input, char *finput, unsigned char *map, PROCFN *pprocfn, void **pat, RDFN *prdfn, MATCHFN *pmatchfn)
        !            10: {
        !            11:        unsigned char *lb, *le;
        !            12:        int nwords, k;
        !            13:        re_cw *cw;
        !            14: 
        !            15:        *pat = (void *)(cw = re_cwinit(map));
        !            16:        if(finput){
        !            17:                nwords = addwords(finput, cw, &lb, &le);
        !            18:        } else {
        !            19:                register unsigned char *s, *e;
        !            20: 
        !            21:                nwords = 0;
        !            22:                s = (unsigned char *)input;
        !            23:                while(*s){
        !            24:                        char ch;
        !            25: 
        !            26:                        for(e = s; *e && (*e != '\n'); e++)
        !            27:                                ;
        !            28:                        ch = *e;
        !            29:                        if(xflag){
        !            30:                                s[-1] = '\n';
        !            31:                                *e = '\n';
        !            32:                                re_cwadd(cw, (lb = s)-1, (le = e)+1);
        !            33:                                *e = ch;
        !            34:                        } else
        !            35:                                re_cwadd(cw, lb = s, le = e);
        !            36:                        s = *e? (e+1):e;
        !            37:                        nwords++;
        !            38:                }
        !            39:                if(nwords == 1){
        !            40:                        s = (unsigned char *)malloc(1 + (k = le-lb));
        !            41:                        if (!s){
        !            42:                                re_error("malloc failure");
        !            43:                                cw->seenerror = 1;
        !            44:                                return;
        !            45:                        }
        !            46:                        memmove((char *)s, (char *)lb, k);
        !            47:                        lb = s;
        !            48:                        le = s+k;
        !            49:                        if(xflag)
        !            50:                                *le++ = '\n';
        !            51:                }
        !            52:        }
        !            53:        if(nwords == 1){
        !            54:                *pat = (void *)re_bmcomp((char *)lb, (char *)le, map);
        !            55:                *pprocfn = re_bmexec;
        !            56:                *prdfn = greprd;
        !            57:                *pmatchfn = xflag ? bmxmatch : grepmatch;
        !            58:        } else {
        !            59:                re_cwcomp(cw);
        !            60:                *pprocfn = re_cwexec;
        !            61:                *prdfn = xflag? cwxrd : greprd;
        !            62:                *pmatchfn = xflag ? cwxmatch : grepmatch;
        !            63:        }
        !            64:        if(sflag || lflag || Lflag || oneflag){
        !            65:                if(vflag == 0)
        !            66:                        succfn = oneshot, failfn = count_m;
        !            67:                else
        !            68:                        succfn = count, failfn = oneshot;
        !            69:        } else if(cflag){
        !            70:                if(vflag == 0)
        !            71:                        succfn = inc, failfn = null;
        !            72:                else
        !            73:                        succfn = null, failfn = inc_m;
        !            74:        } else if(vflag){
        !            75:                if(bflag||nflag)
        !            76:                        succfn = count;
        !            77:                else
        !            78:                        succfn = null;
        !            79:                failfn = pr_m;
        !            80:        } else {
        !            81:                succfn = pr;
        !            82:                if(bflag||nflag)
        !            83:                        failfn = count_m;
        !            84:                else
        !            85:                        failfn = null;
        !            86:        }
        !            87: }
        !            88: 
        !            89: static
        !            90: addwords(char *file, re_cw *pat, unsigned char **b, unsigned char **e)
        !            91: {
        !            92:        unsigned char rbuf[MAXLINE+2];
        !            93:        unsigned char *buf = rbuf+1;
        !            94:        int fd;
        !            95:        int nwords = 0, eof, k;
        !            96:        register unsigned char *nl, *nn, *end;
        !            97: 
        !            98:        if((fd = open(file, 0)) < 0){
        !            99:                perror(file);
        !           100:                exit(2);
        !           101:        }
        !           102:        rbuf[0] = '\n';
        !           103:        nl = end = buf+1;
        !           104:        eof = 0;
        !           105:        for(;;){
        !           106:                if((nn = (unsigned char *)memchr((char *)nl, '\n', end-nl)) == 0){
        !           107:                        if(nl == buf){
        !           108:                                EPR "line too long in %s\n", file);
        !           109:                                exit(2);
        !           110:                        }
        !           111:                        memmove((char *)buf, (char *)nl, end-nl);
        !           112:                        end -= nl-buf;
        !           113:                        nl = buf;
        !           114:                        k = read(fd, (char *)end, &buf[MAXLINE]-end);
        !           115:                        if(k > 0){
        !           116:                                end += k;
        !           117:                                continue;
        !           118:                        } else if(k == 0){
        !           119:                                if(nl == end)   /* clean read to end of file */
        !           120:                                        break;
        !           121:                                eof = 1;
        !           122:                                *end++ = '\n';
        !           123:                                continue;
        !           124:                        } else {
        !           125:                                perror(file);
        !           126:                                exit(2);
        !           127:                        }
        !           128:                }
        !           129:                if(xflag)
        !           130:                        re_cwadd(pat, nl-1, nn+1);
        !           131:                else
        !           132:                        re_cwadd(pat, nl, nn);
        !           133:                nl = nn+1;
        !           134:                nwords++;
        !           135:                if(eof)
        !           136:                        break;
        !           137:        }
        !           138:        close(fd);
        !           139:        if(nwords == 1){
        !           140:                k = strlen((char *)buf)-1;
        !           141:                *b = (unsigned char *)malloc(k+1);
        !           142:                if (!*b){
        !           143:                        re_error("malloc failed");
        !           144:                        pat->seenerror = 1;
        !           145:                        return 0;
        !           146:                }
        !           147:                memmove((char *)*b, (char *)buf, k);
        !           148:                if(xflag)
        !           149:                        (*b)[k++] = '\n';
        !           150:                *e = *b + k;
        !           151:        }
        !           152:        return(nwords);
        !           153: }

unix.superglobalmegacorp.com

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