Annotation of researchv9/cmd/sed/sed0.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include "sed.h"
                      3: 
                      4: struct label   *labtab = ltab;
                      5: char   CGMES[] = "sed: Command garbled: %s\n";
                      6: char   TMMES[] = "sed: Too much text: %s\n";
                      7: char   LTL[]   = "sed: Label too long: %s\n";
                      8: char   AD0MES[]        = "sed: No addresses allowed: %s\n";
                      9: char   AD1MES[]        = "sed: Only one address allowed: %s\n";
                     10: char   bittab[]  = {
                     11:                1,
                     12:                2,
                     13:                4,
                     14:                8,
                     15:                16,
                     16:                32,
                     17:                64,
                     18:                128
                     19:        };
                     20: 
                     21: main(argc, argv)
                     22: char   *argv[];
                     23: {
                     24: 
                     25:        eargc = argc;
                     26:        eargv = argv;
                     27: 
                     28:        badp = &bad;
                     29:        aptr = abuf;
                     30:        hspend = holdsp;
                     31:        lab = labtab + 1;       /* 0 reserved for end-pointer */
                     32:        rep = ptrspace;
                     33:        rep->r1.ad1 = respace;
                     34:        lbend = &linebuf[LBSIZE];
                     35:        hend = &holdsp[LBSIZE];
                     36:        lcomend = &genbuf[71];
                     37:        ptrend = &ptrspace[PTRSIZE];
                     38:        reend = &respace[RESIZE];
                     39:        labend = &labtab[LABSIZE];
                     40:        lnum = 0;
                     41:        pending = 0;
                     42:        depth = 0;
                     43:        spend = linebuf;
                     44:        hspend = holdsp;
                     45:        fcode[0] = stdout;
                     46:        nfiles = 1;
                     47:        lastre = NULL;
                     48: 
                     49:        if(eargc == 1)
                     50:                exit(0);
                     51: 
                     52: 
                     53:        while (--eargc > 0 && (++eargv)[0][0] == '-')
                     54:                switch (eargv[0][1]) {
                     55: 
                     56:                case 'n':
                     57:                        nflag++;
                     58:                        continue;
                     59: 
                     60:                case 'f':
                     61:                        if(eargc-- <= 0)        exit(2);
                     62: 
                     63:                        if((fin = fopen(*++eargv, "r")) == NULL) {
                     64:                                fprintf(stderr, "sed: Cannot open pattern-file: %s\n", *eargv);
                     65:                                exit(2);
                     66:                        }
                     67: 
                     68:                        fcomp();
                     69:                        fclose(fin);
                     70:                        continue;
                     71: 
                     72:                case 'e':
                     73:                        eflag++;
                     74:                        fcomp();
                     75:                        eflag = 0;
                     76:                        continue;
                     77: 
                     78:                case 'g':
                     79:                        gflag++;
                     80:                        continue;
                     81: 
                     82:                default:
                     83:                        fprintf(stderr, "sed: Unknown flag: %c\n", eargv[0][1]);
                     84:                        continue;
                     85:                }
                     86: 
                     87: 
                     88:        if(compfl == 0) {
                     89:                eargv--;
                     90:                eargc++;
                     91:                eflag++;
                     92:                fcomp();
                     93:                eargv++;
                     94:                eargc--;
                     95:                eflag = 0;
                     96:        }
                     97: 
                     98:        if(depth) {
                     99:                fprintf(stderr, "sed: Too many {'s\n");
                    100:                exit(2);
                    101:        }
                    102: 
                    103:        labtab->address = rep;
                    104: 
                    105:        dechain();
                    106: 
                    107: /*     abort();        /*DEBUG*/
                    108: 
                    109:        if(eargc <= 0)
                    110:                execute((char *)NULL);
                    111:        else while(--eargc >= 0) {
                    112:                execute(*eargv++);
                    113:        }
                    114:        fclose(stdout);
                    115:        exit(0);
                    116: }
                    117: fcomp()
                    118: {
                    119: 
                    120:        register char   *p, *op, *tp;
                    121:        char    *address();
                    122:        union reptr     *pt, *pt1;
                    123:        int     i;
                    124:        struct label    *lpt;
                    125: 
                    126:        compfl = 1;
                    127:        op = lastre;
                    128: 
                    129:        if(rline(linebuf) < 0) {
                    130:                lastre = op;
                    131:                return;
                    132:        }
                    133:        if(*linebuf == '#') {
                    134:                if(linebuf[1] == 'n')
                    135:                        nflag = 1;
                    136:        }
                    137:        else {
                    138:                cp = linebuf;
                    139:                goto comploop;
                    140:        }
                    141: 
                    142:        for(;;) {
                    143:                if(rline(linebuf) < 0)  break;
                    144: 
                    145:                cp = linebuf;
                    146: 
                    147: comploop:
                    148: /*     fprintf(stdout, "cp: %s\n", cp);        /*DEBUG*/
                    149:                while(*cp == ' ' || *cp == '\t')        cp++;
                    150:                if(*cp == '\0' || *cp == '#')           continue;
                    151:                if(*cp == ';') {
                    152:                        cp++;
                    153:                        goto comploop;
                    154:                }
                    155: 
                    156:                p = address(rep->r1.ad1);
                    157:                if(p == badp) {
                    158:                        fprintf(stderr, CGMES, linebuf);
                    159:                        exit(2);
                    160:                }
                    161: 
                    162:                if(p == 0) {
                    163:                        p = rep->r1.ad1;
                    164:                        rep->r1.ad1 = 0;
                    165:                } else {
                    166:                        if(p == rep->r1.ad1) {
                    167:                                if(op)
                    168:                                        rep->r1.ad1 = op;
                    169:                                else {
                    170:                                        fprintf(stderr, "sed: First RE may not be null\n");
                    171:                                        exit(2);
                    172:                                }
                    173:                        }
                    174:                        op = rep->r1.ad1;
                    175:                        if(*cp == ',' || *cp == ';') {
                    176:                                cp++;
                    177:                                if((rep->r1.ad2 = p) > reend) {
                    178:                                        fprintf(stderr, TMMES, linebuf);
                    179:                                        exit(2);
                    180:                                }
                    181:                                p = address(rep->r1.ad2);
                    182:                                if(p == badp || p == 0) {
                    183:                                        fprintf(stderr, CGMES, linebuf);
                    184:                                        exit(2);
                    185:                                }
                    186:                                if(p == rep->r1.ad2)
                    187:                                        rep->r1.ad2 = op;
                    188:                                else
                    189:                                        op = rep->r1.ad2;
                    190: 
                    191:                        } else
                    192:                                rep->r1.ad2 = 0;
                    193:                }
                    194: 
                    195:                if(p > reend) {
                    196:                        fprintf(stderr, "sed: Too much text: %s\n", linebuf);
                    197:                        exit(2);
                    198:                }
                    199: 
                    200:                while(*cp == ' ' || *cp == '\t')        cp++;
                    201: 
                    202: swit:
                    203:                switch(*cp++) {
                    204: 
                    205:                        default:
                    206: /*fprintf(stderr, "cp = %d; *cp = %o\n", cp - linebuf, *cp);*/
                    207:                                fprintf(stderr, "sed: Unrecognized command: %s\n", linebuf);
                    208:                                exit(2);
                    209: 
                    210:                        case '!':
                    211:                                rep->r1.negfl = 1;
                    212:                                goto swit;
                    213: 
                    214:                        case '{':
                    215:                                rep->r1.command = BCOM;
                    216:                                rep->r1.negfl = !(rep->r1.negfl);
                    217:                                cmpend[depth++] = &rep->r2.lb1;
                    218:                                if(++rep >= ptrend) {
                    219:                                        fprintf(stderr, "sed: Too many commands: %s\n", linebuf);
                    220:                                        exit(2);
                    221:                                }
                    222:                                rep->r1.ad1 = p;
                    223:                                if(*cp == '\0') continue;
                    224: 
                    225:                                goto comploop;
                    226: 
                    227:                        case '}':
                    228:                                if(rep->r1.ad1) {
                    229:                                        fprintf(stderr, AD0MES, linebuf);
                    230:                                        exit(2);
                    231:                                }
                    232: 
                    233:                                if(--depth < 0) {
                    234:                                        fprintf(stderr, "sed: Too many }'s\n");
                    235:                                        exit(2);
                    236:                                }
                    237:                                *cmpend[depth] = rep;
                    238: 
                    239:                                rep->r1.ad1 = p;
                    240:                                if(*cp == 0)    continue;
                    241:                                goto comploop;
                    242: 
                    243:                        case '=':
                    244:                                rep->r1.command = EQCOM;
                    245:                                if(rep->r1.ad2) {
                    246:                                        fprintf(stderr, AD1MES, linebuf);
                    247:                                        exit(2);
                    248:                                }
                    249:                                break;
                    250: 
                    251:                        case ':':
                    252:                                if(rep->r1.ad1) {
                    253:                                        fprintf(stderr, AD0MES, linebuf);
                    254:                                        exit(2);
                    255:                                }
                    256: 
                    257:                                while(*cp++ == ' ');
                    258:                                cp--;
                    259: 
                    260: 
                    261:                                tp = lab->asc;
                    262:                                while((*tp++ = *cp++))
                    263:                                        if(tp >= &(lab->asc[8])) {
                    264:                                                fprintf(stderr, LTL, linebuf);
                    265:                                                exit(2);
                    266:                                        }
                    267:                                *--tp = '\0';
                    268: 
                    269:                                if(lpt = search(lab)) {
                    270:                                        if(lpt->address) {
                    271:                                                fprintf(stderr, "sed: Duplicate labels: %s\n", linebuf);
                    272:                                                exit(2);
                    273:                                        }
                    274:                                } else {
                    275:                                        lab->chain = 0;
                    276:                                        lpt = lab;
                    277:                                        if(++lab >= labend) {
                    278:                                                fprintf(stderr, "sed: Too many labels: %s\n", linebuf);
                    279:                                                exit(2);
                    280:                                        }
                    281:                                }
                    282:                                lpt->address = rep;
                    283:                                rep->r1.ad1 = p;
                    284: 
                    285:                                continue;
                    286: 
                    287:                        case 'a':
                    288:                                rep->r1.command = ACOM;
                    289:                                if(rep->r1.ad2) {
                    290:                                        fprintf(stderr, AD1MES, linebuf);
                    291:                                        exit(2);
                    292:                                }
                    293:                                if(*cp == '\\') cp++;
                    294:                                if(*cp++ != '\n') {
                    295:                                        fprintf(stderr, CGMES, linebuf);
                    296:                                        exit(2);
                    297:                                }
                    298:                                rep->r1.re1 = p;
                    299:                                p = text(rep->r1.re1);
                    300:                                break;
                    301:                        case 'c':
                    302:                                rep->r1.command = CCOM;
                    303:                                if(*cp == '\\') cp++;
                    304:                                if(*cp++ != ('\n')) {
                    305:                                        fprintf(stderr, CGMES, linebuf);
                    306:                                        exit(2);
                    307:                                }
                    308:                                rep->r1.re1 = p;
                    309:                                p = text(rep->r1.re1);
                    310:                                break;
                    311:                        case 'i':
                    312:                                rep->r1.command = ICOM;
                    313:                                if(rep->r1.ad2) {
                    314:                                        fprintf(stderr, AD1MES, linebuf);
                    315:                                        exit(2);
                    316:                                }
                    317:                                if(*cp == '\\') cp++;
                    318:                                if(*cp++ != ('\n')) {
                    319:                                        fprintf(stderr, CGMES, linebuf);
                    320:                                        exit(2);
                    321:                                }
                    322:                                rep->r1.re1 = p;
                    323:                                p = text(rep->r1.re1);
                    324:                                break;
                    325: 
                    326:                        case 'g':
                    327:                                rep->r1.command = GCOM;
                    328:                                break;
                    329: 
                    330:                        case 'G':
                    331:                                rep->r1.command = CGCOM;
                    332:                                break;
                    333: 
                    334:                        case 'h':
                    335:                                rep->r1.command = HCOM;
                    336:                                break;
                    337: 
                    338:                        case 'H':
                    339:                                rep->r1.command = CHCOM;
                    340:                                break;
                    341: 
                    342:                        case 't':
                    343:                                rep->r1.command = TCOM;
                    344:                                goto jtcommon;
                    345: 
                    346:                        case 'b':
                    347:                                rep->r1.command = BCOM;
                    348: jtcommon:
                    349:                                while(*cp++ == ' ');
                    350:                                cp--;
                    351: 
                    352:                                if(*cp == '\0') {
                    353:                                        if(pt = labtab->chain) {
                    354:                                                while(pt1 = pt->r2.lb1)
                    355:                                                        pt = pt1;
                    356:                                                pt->r2.lb1 = rep;
                    357:                                        } else
                    358:                                                labtab->chain = rep;
                    359:                                        break;
                    360:                                }
                    361:                                tp = lab->asc;
                    362:                                while((*tp++ = *cp++))
                    363:                                        if(tp >= &(lab->asc[8])) {
                    364:                                                fprintf(stderr, LTL, linebuf);
                    365:                                                exit(2);
                    366:                                        }
                    367:                                cp--;
                    368:                                *--tp = '\0';
                    369: 
                    370:                                if(lpt = search(lab)) {
                    371:                                        if(lpt->address) {
                    372:                                                rep->r2.lb1 = lpt->address;
                    373:                                        } else {
                    374:                                                pt = lpt->chain;
                    375:                                                while(pt1 = pt->r2.lb1)
                    376:                                                        pt = pt1;
                    377:                                                pt->r2.lb1 = rep;
                    378:                                        }
                    379:                                } else {
                    380:                                        lab->chain = rep;
                    381:                                        lab->address = 0;
                    382:                                        if(++lab >= labend) {
                    383:                                                fprintf(stderr, "sed: Too many labels: %s\n", linebuf);
                    384:                                                exit(2);
                    385:                                        }
                    386:                                }
                    387:                                break;
                    388: 
                    389:                        case 'n':
                    390:                                rep->r1.command = NCOM;
                    391:                                break;
                    392: 
                    393:                        case 'N':
                    394:                                rep->r1.command = CNCOM;
                    395:                                break;
                    396: 
                    397:                        case 'p':
                    398:                                rep->r1.command = PCOM;
                    399:                                break;
                    400: 
                    401:                        case 'P':
                    402:                                rep->r1.command = CPCOM;
                    403:                                break;
                    404: 
                    405:                        case 'r':
                    406:                                rep->r1.command = RCOM;
                    407:                                if(rep->r1.ad2) {
                    408:                                        fprintf(stderr, AD1MES, linebuf);
                    409:                                        exit(2);
                    410:                                }
                    411:                                if(*cp++ != ' ') {
                    412:                                        fprintf(stderr, CGMES, linebuf);
                    413:                                        exit(2);
                    414:                                }
                    415:                                rep->r1.re1 = p;
                    416:                                p = text(rep->r1.re1);
                    417:                                break;
                    418: 
                    419:                        case 'd':
                    420:                                rep->r1.command = DCOM;
                    421:                                break;
                    422: 
                    423:                        case 'D':
                    424:                                rep->r1.command = CDCOM;
                    425:                                rep->r2.lb1 = ptrspace;
                    426:                                break;
                    427: 
                    428:                        case 'q':
                    429:                                rep->r1.command = QCOM;
                    430:                                if(rep->r1.ad2) {
                    431:                                        fprintf(stderr, AD1MES, linebuf);
                    432:                                        exit(2);
                    433:                                }
                    434:                                break;
                    435: 
                    436:                        case 'l':
                    437:                                rep->r1.command = LCOM;
                    438:                                break;
                    439: 
                    440:                        case 's':
                    441:                                rep->r1.command = SCOM;
                    442:                                seof = *cp++;
                    443:                                rep->r1.re1 = p;
                    444:                                p = compile(rep->r1.re1);
                    445:                                if(p == badp) {
                    446:                                        fprintf(stderr, CGMES, linebuf);
                    447:                                        exit(2);
                    448:                                }
                    449:                                if(p == rep->r1.re1) {
                    450:                                        if(op == NULL) {
                    451:                                                fprintf(stderr, "sed: First RE may not be null.\n");
                    452:                                                exit(2);
                    453:                                        }
                    454:                                        rep->r1.re1 = op;
                    455:                                } else {
                    456:                                        op = rep->r1.re1;
                    457:                                }
                    458: 
                    459:                                if((rep->r1.rhs = p) > reend) {
                    460:                                        fprintf(stderr, TMMES, linebuf);
                    461:                                        exit(2);
                    462:                                }
                    463: 
                    464:                                if((p = compsub(rep->r1.rhs)) == badp) {
                    465:                                        fprintf(stderr, CGMES, linebuf);
                    466:                                        exit(2);
                    467:                                }
                    468:                                if(*cp == 'g') {
                    469:                                        cp++;
                    470:                                        rep->r1.gfl++;
                    471:                                } else if(gflag)
                    472:                                        rep->r1.gfl++;
                    473: 
                    474:                                if(*cp == 'p') {
                    475:                                        cp++;
                    476:                                        rep->r1.pfl = 1;
                    477:                                }
                    478: 
                    479:                                if(*cp == 'P') {
                    480:                                        cp++;
                    481:                                        rep->r1.pfl = 2;
                    482:                                }
                    483: 
                    484:                                if(*cp == 'w') {
                    485:                                        cp++;
                    486:                                        if(*cp++ !=  ' ') {
                    487:                                                fprintf(stderr, CGMES, linebuf);
                    488:                                                exit(2);
                    489:                                        }
                    490:                                        if(nfiles >= MAXFILES) {
                    491:                                                fprintf(stderr, "sed: Too many files in w commands 1 \n");
                    492:                                                exit(2);
                    493:                                        }
                    494: 
                    495:                                        text(fname[nfiles]);
                    496:                                        for(i = nfiles - 1; i >= 0; i--)
                    497:                                                if(cmp(fname[nfiles],fname[i]) == 0) {
                    498:                                                        rep->r1.fcode = fcode[i];
                    499:                                                        goto done;
                    500:                                                }
                    501:                                        if((rep->r1.fcode = fopen(fname[nfiles], "w")) == NULL) {
                    502:                                                fprintf(stderr, "sed: Cannot open %s\n", fname[nfiles]);
                    503:                                                exit(2);
                    504:                                        }
                    505:                                        fcode[nfiles++] = rep->r1.fcode;
                    506:                                }
                    507:                                break;
                    508: 
                    509:                        case 'w':
                    510:                                rep->r1.command = WCOM;
                    511:                                if(*cp++ != ' ') {
                    512:                                        fprintf(stderr, CGMES, linebuf);
                    513:                                        exit(2);
                    514:                                }
                    515:                                if(nfiles >= MAXFILES){
                    516:                                        fprintf(stderr, "sed: Too many files in w commands 2 \n");
                    517:                                        fprintf(stderr, "nfiles = %d; MAXF = %d\n", nfiles, MAXFILES);
                    518:                                        exit(2);
                    519:                                }
                    520: 
                    521:                                text(fname[nfiles]);
                    522:                                for(i = nfiles - 1; i >= 0; i--)
                    523:                                        if(cmp(fname[nfiles], fname[i]) == 0) {
                    524:                                                rep->r1.fcode = fcode[i];
                    525:                                                goto done;
                    526:                                        }
                    527: 
                    528:                                if((rep->r1.fcode = fopen(fname[nfiles], "w")) == NULL) {
                    529:                                        fprintf(stderr, "sed: Cannot create %s\n", fname[nfiles]);
                    530:                                        exit(2);
                    531:                                }
                    532:                                fcode[nfiles++] = rep->r1.fcode;
                    533:                                break;
                    534: 
                    535:                        case 'x':
                    536:                                rep->r1.command = XCOM;
                    537:                                break;
                    538: 
                    539:                        case 'y':
                    540:                                rep->r1.command = YCOM;
                    541:                                seof = *cp++;
                    542:                                rep->r1.re1 = p;
                    543:                                p = ycomp(rep->r1.re1);
                    544:                                if(p == badp) {
                    545:                                        fprintf(stderr, CGMES, linebuf);
                    546:                                        exit(2);
                    547:                                }
                    548:                                if(p > reend) {
                    549:                                        fprintf(stderr, TMMES, linebuf);
                    550:                                        exit(2);
                    551:                                }
                    552:                                break;
                    553: 
                    554:                }
                    555: done:
                    556:                if(++rep >= ptrend) {
                    557:                        fprintf(stderr, "sed: Too many commands, last: %s\n", linebuf);
                    558:                        exit(2);
                    559:                }
                    560: 
                    561:                rep->r1.ad1 = p;
                    562: 
                    563:                if(*cp++ != '\0') {
                    564:                        if(cp[-1] == ';')
                    565:                                goto comploop;
                    566:                        fprintf(stderr, CGMES, linebuf);
                    567:                        exit(2);
                    568:                }
                    569: 
                    570:        }
                    571: }
                    572: char   *
                    573: compsub(rhsbuf)
                    574: char   *rhsbuf;
                    575: {
                    576:        register char   *p, *q;
                    577: 
                    578:        p = rhsbuf;
                    579:        q = cp;
                    580:        for(;;) {
                    581:                if((*p = *q++) == '\\') {
                    582:                        *p = *q++;
                    583:                        if(*p > numbra + '0' && *p <= '9')
                    584:                                return(badp);
                    585:                        if(*p == 'n')
                    586:                                *p = '\n';
                    587:                        *p++ |= 0200;
                    588:                        continue;
                    589:                }
                    590:                if(*p == seof) {
                    591:                        *p++ = '\0';
                    592:                        cp = q;
                    593:                        return(p);
                    594:                }
                    595:                if(*p++ == '\0') {
                    596:                        return(badp);
                    597:                }
                    598: 
                    599:        }
                    600: }
                    601: 
                    602: char *
                    603: compile(expbuf)
                    604: char   *expbuf;
                    605: {
                    606:        register c;
                    607:        register char *ep, *sp;
                    608:        char    neg;
                    609:        char *lastep, *cstart;
                    610:        int cclcnt;
                    611:        int     closed;
                    612:        char    bracket[NBRA], *bracketp;
                    613: 
                    614:        if(*cp == seof) {
                    615:                cp++;
                    616:                return(expbuf);
                    617:        }
                    618: 
                    619:        ep = expbuf;
                    620:        lastep = 0;
                    621:        bracketp = bracket;
                    622:        closed = numbra = 0;
                    623:        sp = cp;
                    624:        if (*sp == '^') {
                    625:                *ep++ = 1;
                    626:                sp++;
                    627:        } else {
                    628:                *ep++ = 0;
                    629:        }
                    630:        for (;;) {
                    631:                if (ep >= reend) {
                    632:                        cp = sp;
                    633:                        return(badp);
                    634:                }
                    635:                if((c = *sp++) == seof) {
                    636:                        if(bracketp != bracket) {
                    637:                                cp = sp;
                    638:                                return(badp);
                    639:                        }
                    640:                        cp = sp;
                    641:                        *ep++ = CEOF;
                    642:                        return(ep);
                    643:                }
                    644:                if(c != '*')
                    645:                        lastep = ep;
                    646:                switch (c) {
                    647: 
                    648:                case '\\':
                    649:                        if((c = *sp++) == '(') {
                    650:                                if(numbra >= NBRA) {
                    651:                                        cp = sp;
                    652:                                        return(badp);
                    653:                                }
                    654:                                *bracketp++ = numbra;
                    655:                                *ep++ = CBRA;
                    656:                                *ep++ = numbra++;
                    657:                                continue;
                    658:                        }
                    659:                        if(c == ')') {
                    660:                                if(bracketp <= bracket) {
                    661:                                        cp = sp;
                    662:                                        return(badp);
                    663:                                }
                    664:                                *ep++ = CKET;
                    665:                                *ep++ = *--bracketp;
                    666:                                closed++;
                    667:                                continue;
                    668:                        }
                    669: 
                    670:                        if(c >= '1' && c <= '9') {
                    671:                                if((c -= '1') >= closed)
                    672:                                        return(badp);
                    673:        
                    674:                                *ep++ = CBACK;
                    675:                                *ep++ = c;
                    676:                                continue;
                    677:                        }
                    678:                        if(c == '\n') {
                    679:                                cp = sp;
                    680:                                return(badp);
                    681:                        }
                    682:                        if(c == 'n') {
                    683:                                c = '\n';
                    684:                        }
                    685:                        goto defchar;
                    686: 
                    687:                case '\0':
                    688:                case '\n':
                    689:                        cp = sp;
                    690:                        return(badp);
                    691: 
                    692:                case '.':
                    693:                        *ep++ = CDOT;
                    694:                        continue;
                    695: 
                    696:                case '*':
                    697:                        if (lastep == 0)
                    698:                                goto defchar;
                    699:                        if(*lastep == CKET) {
                    700:                                cp = sp;
                    701:                                return(badp);
                    702:                        }
                    703:                        *lastep |= STAR;
                    704:                        continue;
                    705: 
                    706:                case '$':
                    707:                        if (*sp != seof)
                    708:                                goto defchar;
                    709:                        *ep++ = CDOL;
                    710:                        continue;
                    711: 
                    712:                case '[':
                    713:                        if(&ep[17] >= reend) {
                    714:                                fprintf(stderr, "sed: RE too long: %s\n", linebuf);
                    715:                                exit(2);
                    716:                        }
                    717: 
                    718:                        *ep++ = CCL;
                    719: 
                    720:                        neg = 0;
                    721:                        if((c = *sp++) == '^') {
                    722:                                neg = 1;
                    723:                                c = *sp++;
                    724:                        }
                    725: 
                    726:                        cstart = sp;
                    727:                        do {
                    728:                                if(c == '\0') {
                    729:                                        fprintf(stderr, CGMES, linebuf);
                    730:                                        exit(2);
                    731:                                }
                    732:                                if (c=='-' && sp>cstart && *sp!=']') {
                    733:                                        for (c = sp[-2]; c<*sp; c++)
                    734:                                                ep[c>>3] |= bittab[c&07];
                    735:                                }
                    736:                                if(c == '\\') {
                    737:                                        switch(c = *sp++) {
                    738:                                                case 'n':
                    739:                                                        c = '\n';
                    740:                                                        break;
                    741:                                        }
                    742:                                }
                    743: 
                    744:                                ep[c >> 3] |= bittab[c & 07];
                    745:                        } while((c = *sp++) != ']');
                    746: 
                    747:                        if(neg)
                    748:                                for(cclcnt = 0; cclcnt < 16; cclcnt++)
                    749:                                        ep[cclcnt] ^= -1;
                    750:                        ep[0] &= 0376;
                    751: 
                    752:                        ep += 16;
                    753: 
                    754:                        continue;
                    755: 
                    756:                defchar:
                    757:                default:
                    758:                        *ep++ = CCHR;
                    759:                        *ep++ = c;
                    760:                }
                    761:        }
                    762: }
                    763: rline(lbuf)
                    764: char   *lbuf;
                    765: {
                    766:        register char   *p, *q;
                    767:        register        t;
                    768:        static char     *saveq;
                    769: 
                    770:        p = lbuf - 1;
                    771: 
                    772:        if(eflag) {
                    773:                if(eflag > 0) {
                    774:                        eflag = -1;
                    775:                        if(eargc-- <= 0)
                    776:                                exit(2);
                    777:                        q = *++eargv;
                    778:                        while(*++p = *q++) {
                    779:                                if(*p == '\\') {
                    780:                                        if((*++p = *q++) == '\0') {
                    781:                                                saveq = 0;
                    782:                                                return(-1);
                    783:                                        } else
                    784:                                                continue;
                    785:                                }
                    786:                                if(*p == '\n') {
                    787:                                        *p = '\0';
                    788:                                        saveq = q;
                    789:                                        return(1);
                    790:                                }
                    791:                        }
                    792:                        saveq = 0;
                    793:                        return(1);
                    794:                }
                    795:                if((q = saveq) == 0)    return(-1);
                    796: 
                    797:                while(*++p = *q++) {
                    798:                        if(*p == '\\') {
                    799:                                if((*++p = *q++) == '0') {
                    800:                                        saveq = 0;
                    801:                                        return(-1);
                    802:                                } else
                    803:                                        continue;
                    804:                        }
                    805:                        if(*p == '\n') {
                    806:                                *p = '\0';
                    807:                                saveq = q;
                    808:                                return(1);
                    809:                        }
                    810:                }
                    811:                saveq = 0;
                    812:                return(1);
                    813:        }
                    814: 
                    815:        while((t = getc(fin)) != EOF) {
                    816:                *++p = t;
                    817:                if(*p == '\\') {
                    818:                        t = getc(fin);
                    819:                        *++p = t;
                    820:                }
                    821:                else if(*p == '\n') {
                    822:                        *p = '\0';
                    823:                        return(1);
                    824:                }
                    825:        }
                    826:        *++p = '\0';
                    827:        return(-1);
                    828: }
                    829: 
                    830: char   *address(expbuf)
                    831: char   *expbuf;
                    832: {
                    833:        register char   *rcp;
                    834:        long    lno;
                    835: 
                    836:        if(*cp == '$') {
                    837:                cp++;
                    838:                *expbuf++ = CEND;
                    839:                *expbuf++ = CEOF;
                    840:                return(expbuf);
                    841:        }
                    842: 
                    843:        if(*cp == '/') {
                    844:                seof = '/';
                    845:                cp++;
                    846:                return(compile(expbuf));
                    847:        }
                    848: 
                    849:        rcp = cp;
                    850:        lno = 0;
                    851: 
                    852:        while(*rcp >= '0' && *rcp <= '9')
                    853:                lno = lno*10 + *rcp++ - '0';
                    854: 
                    855:        if(rcp > cp) {
                    856:                *expbuf++ = CLNUM;
                    857:                *expbuf++ = lno;
                    858:                *expbuf++ = lno >> 8;
                    859:                *expbuf++ = lno >> 16;
                    860:                *expbuf++ = lno >> 24;
                    861:                *expbuf++ = CEOF;
                    862:                cp = rcp;
                    863:                return(expbuf);
                    864:        }
                    865:        return(0);
                    866: }
                    867: cmp(a, b)
                    868: char   *a,*b;
                    869: {
                    870:        register char   *ra, *rb;
                    871: 
                    872:        ra = a - 1;
                    873:        rb = b - 1;
                    874: 
                    875:        while(*++ra == *++rb)
                    876:                if(*ra == '\0') return(0);
                    877:        return(1);
                    878: }
                    879: 
                    880: char   *text(textbuf)
                    881: char   *textbuf;
                    882: {
                    883:        register char   *p, *q;
                    884: 
                    885:        p = textbuf;
                    886:        q = cp;
                    887:        while(*q == '\t' || *q == ' ')  q++;
                    888:        for(;;) {
                    889: 
                    890:                if((*p = *q++) == '\\')
                    891:                        *p = *q++;
                    892:                if(*p == '\0') {
                    893:                        cp = --q;
                    894:                        return(++p);
                    895:                }
                    896:                if(*p == '\n') {
                    897:                        while(*q == '\t' || *q == ' ')  q++;
                    898:                }
                    899:                p++;
                    900:        }
                    901: }
                    902: 
                    903: 
                    904: struct label   *search(ptr)
                    905: struct label   *ptr;
                    906: {
                    907:        struct label    *rp;
                    908: 
                    909:        rp = labtab;
                    910:        while(rp < ptr) {
                    911:                if(cmp(rp->asc, ptr->asc) == 0)
                    912:                        return(rp);
                    913:                rp++;
                    914:        }
                    915: 
                    916:        return(0);
                    917: }
                    918: 
                    919: 
                    920: dechain()
                    921: {
                    922:        struct label    *lptr;
                    923:        union reptr     *rptr, *trptr;
                    924: 
                    925:        for(lptr = labtab; lptr < lab; lptr++) {
                    926: 
                    927:                if(lptr->address == 0) {
                    928:                        fprintf(stderr, "sed: Undefined label: %s\n", lptr->asc);
                    929:                        exit(2);
                    930:                }
                    931: 
                    932:                if(lptr->chain) {
                    933:                        rptr = lptr->chain;
                    934:                        while(trptr = rptr->r2.lb1) {
                    935:                                rptr->r2.lb1 = lptr->address;
                    936:                                rptr = trptr;
                    937:                        }
                    938:                        rptr->r2.lb1 = lptr->address;
                    939:                }
                    940:        }
                    941: }
                    942: 
                    943: char *ycomp(expbuf)
                    944: char   *expbuf;
                    945: {
                    946:        register char   c, *ep, *tsp;
                    947:        char    *sp;
                    948: 
                    949:        ep = expbuf;
                    950:        sp = cp;
                    951:        for(tsp = cp; *tsp != seof; tsp++) {
                    952:                if(*tsp == '\\')
                    953:                        tsp++;
                    954:                if(*tsp == '\n' || *tsp == '\0')
                    955:                        return(badp);
                    956:        }
                    957:        tsp++;
                    958: 
                    959:        while((c = *sp++ & 0177) != seof) {
                    960:                if(c == '\\' && *sp == 'n') {
                    961:                        sp++;
                    962:                        c = '\n';
                    963:                }
                    964:                if((ep[c] = *tsp++) == '\\' && *tsp == 'n') {
                    965:                        ep[c] = '\n';
                    966:                        tsp++;
                    967:                }
                    968:                if(ep[c] == seof || ep[c] == '\0')
                    969:                        return(badp);
                    970:        }
                    971:        if(*tsp != seof)
                    972:                return(badp);
                    973:        cp = ++tsp;
                    974: 
                    975:        for(c = 0; !(c & 0200); c++)
                    976:                if(ep[c] == 0)
                    977:                        ep[c] = c;
                    978: 
                    979:        return(ep + 0200);
                    980: }
                    981: 

unix.superglobalmegacorp.com

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