Annotation of researchv10no/cmd/troff/Old/n7.c, revision 1.1.1.1

1.1       root        1: #include "tdef.h"
                      2: extern
                      3: #include "d.h"
                      4: extern
                      5: #include "v.h"
                      6: #ifdef NROFF
                      7: extern
                      8: #include "tw.h"
                      9: #endif
                     10: #include "s.h"
                     11: #ifdef NROFF
                     12: #define GETCH gettch
                     13: #endif
                     14: #ifndef NROFF
                     15: #define GETCH getch
                     16: #endif
                     17: 
                     18: /*
                     19: troff7.c
                     20: 
                     21: text
                     22: */
                     23: 
                     24: #include <sgtty.h>
                     25: #include "ext.h"
                     26: int    brflg;
                     27: 
                     28: tbreak()
                     29: {
                     30:        register pad, k;
                     31:        tchar   *i, j;
                     32:        int     res;
                     33: 
                     34:        trap = 0;
                     35:        if (nb)
                     36:                return;
                     37:        if ((dip == d) && (v.nl == -1)) {
                     38:                newline(1);
                     39:                return;
                     40:        }
                     41:        if (!nc) {
                     42:                setnel();
                     43:                if (!wch)
                     44:                        return;
                     45:                if (pendw)
                     46:                        getword(1);
                     47:                movword();
                     48:        } else if (pendw && !brflg) {
                     49:                getword(1);
                     50:                movword();
                     51:        }
                     52:        *linep = dip->nls = 0;
                     53: #ifdef NROFF
                     54:        if (dip == d)
                     55:                horiz(po);
                     56: #endif
                     57:        if (lnmod)
                     58:                donum();
                     59:        lastl = ne;
                     60:        if (brflg != 1) {
                     61:                totout = 0;
                     62:        } else if (ad) {
                     63:                if ((lastl = (ll - un)) < ne)
                     64:                        lastl = ne;
                     65:        }
                     66:        if (admod && ad && (brflg != 2)) {
                     67:                lastl = ne;
                     68:                adsp = adrem = 0;
                     69: #ifdef NROFF
                     70:                if (admod == 1)
                     71:                        un +=  quant(nel / 2, t.Adj);
                     72: #endif
                     73: #ifndef NROFF
                     74:                if (admod == 1)
                     75:                        un += nel / 2;
                     76: #endif
                     77:                else if (admod == 2)
                     78:                        un += nel;
                     79:        }
                     80:        totout++;
                     81:        brflg = 0;
                     82:        if ((lastl + un) > dip->maxl)
                     83:                dip->maxl = (lastl + un);
                     84:        horiz(un);
                     85: #ifdef NROFF
                     86:        if (adrem % t.Adj)
                     87:                res = t.Hor; 
                     88:        else 
                     89:                res = t.Adj;
                     90: #endif
                     91:        for (i = line; nc > 0; ) {
                     92:                if ((cbits(j = *i++)) == ' ') {
                     93:                        pad = 0;
                     94:                        do {
                     95:                                pad += width(j);
                     96:                                nc--;
                     97:                        } while ((cbits(j = *i++)) == ' ');
                     98:                        i--;
                     99:                        pad += adsp;
                    100:                        --nwd;
                    101:                        if (adrem) {
                    102:                                if (adrem < 0) {
                    103: #ifdef NROFF
                    104:                                        pad -= res;
                    105:                                        adrem += res;
                    106:                                } else if ((totout & 01) ||  ((adrem / res) >= (nwd))) {
                    107:                                        pad += res;
                    108:                                        adrem -= res;
                    109: #endif
                    110: #ifndef NROFF
                    111:                                        pad--;
                    112:                                        adrem++;
                    113:                                } else {
                    114:                                        pad++;
                    115:                                        adrem--;
                    116: #endif
                    117:                                }
                    118:                        }
                    119:                        pchar((tchar) WORDSP);
                    120:                        horiz(pad);
                    121:                } else {
                    122:                        pchar(j);
                    123:                        nc--;
                    124:                }
                    125:        }
                    126:        if (ic) {
                    127:                if ((k = ll - un - lastl + ics) > 0)
                    128:                        horiz(k);
                    129:                pchar(ic);
                    130:        }
                    131:        if (icf)
                    132:                icf++;
                    133:        else 
                    134:                ic = 0;
                    135:        ne = nwd = 0;
                    136:        un = in;
                    137:        setnel();
                    138:        newline(0);
                    139:        if (dip != d) {
                    140:                if (dip->dnl > dip->hnl)
                    141:                        dip->hnl = dip->dnl;
                    142:        } else {
                    143:                if (v.nl > dip->hnl)
                    144:                        dip->hnl = v.nl;
                    145:        }
                    146:        for (k = ls - 1; k > 0 && !trap; k--)
                    147:                newline(0);
                    148:        spread = 0;
                    149: }
                    150: 
                    151: donum()
                    152: {
                    153:        register i, nw;
                    154:        extern pchar();
                    155: 
                    156:        nrbits = nmbits;
                    157:        nw = width('1' | nrbits);
                    158:        if (nn) {
                    159:                nn--;
                    160:                goto d1;
                    161:        }
                    162:        if (v.ln % ndf) {
                    163:                v.ln++;
                    164: d1:
                    165:                un += nw * (3 + nms + ni);
                    166:                return;
                    167:        }
                    168:        i = 0;
                    169:        if (v.ln < 100)
                    170:                i++;
                    171:        if (v.ln < 10)
                    172:                i++;
                    173:        horiz(nw * (ni + i));
                    174:        nform = 0;
                    175:        fnumb(v.ln, pchar);
                    176:        un += nw * nms;
                    177:        v.ln++;
                    178: }
                    179: 
                    180: 
                    181: text()
                    182: {
                    183:        tchar i;
                    184:        static int      spcnt;
                    185: 
                    186:        nflush++;
                    187:        if ((dip == d) && (v.nl == -1)) {
                    188:                newline(1); 
                    189:                return;
                    190:        }
                    191:        setnel();
                    192:        if (ce || !fi) {
                    193:                nofill();
                    194:                return;
                    195:        }
                    196:        if (pendw)
                    197:                goto t4;
                    198:        if (pendt)
                    199:                if (spcnt)
                    200:                        goto t2; 
                    201:                else 
                    202:                        goto t3;
                    203:        pendt++;
                    204:        if (spcnt)
                    205:                goto t2;
                    206:        while ((cbits(i = GETCH())) == ' ')
                    207:                spcnt++;
                    208:        if (nlflg) {
                    209: t1:
                    210:                nflush = pendt = ch = spcnt = 0;
                    211:                callsp();
                    212:                return;
                    213:        }
                    214:        ch = i;
                    215:        if (spcnt) {
                    216: t2:
                    217:                tbreak();
                    218:                if (nc || wch)
                    219:                        goto rtn;
                    220:                un += spcnt * sps;
                    221:                spcnt = 0;
                    222:                setnel();
                    223:                if (trap)
                    224:                        goto rtn;
                    225:                if (nlflg)
                    226:                        goto t1;
                    227:        }
                    228: t3:
                    229:        if (spread)
                    230:                goto t5;
                    231:        if (pendw || !wch)
                    232: t4:
                    233:                if (getword(0))
                    234:                        goto t6;
                    235:        if (!movword())
                    236:                goto t3;
                    237: t5:
                    238:        if (nlflg)
                    239:                pendt = 0;
                    240:        adsp = adrem = 0;
                    241:        if (ad) {
                    242:                if (nwd == 1)
                    243:                        adsp = nel; 
                    244:                else 
                    245:                        adsp = nel / (nwd - 1);
                    246: #ifdef NROFF
                    247:                adsp = (adsp / t.Adj)*t.Adj;
                    248: #endif
                    249:                adrem = nel -adsp*(nwd-1);
                    250:        }
                    251:        brflg = 1;
                    252:        tbreak();
                    253:        spread = 0;
                    254:        if (!trap)
                    255:                goto t3;
                    256:        if (!nlflg)
                    257:                goto rtn;
                    258: t6:
                    259:        pendt = 0;
                    260:        ckul();
                    261: rtn:
                    262:        nflush = 0;
                    263: }
                    264: 
                    265: 
                    266: nofill()
                    267: {
                    268:        register j;
                    269:        tchar i;
                    270: 
                    271:        if (!pendnf) {
                    272:                over = 0;
                    273:                tbreak();
                    274:                if (trap)
                    275:                        goto rtn;
                    276:                if (nlflg) {
                    277:                        ch = nflush = 0;
                    278:                        callsp();
                    279:                        return;
                    280:                }
                    281:                adsp = adrem = 0;
                    282:                nwd = 10000;
                    283:        }
                    284:        while ((j = (cbits(i = GETCH()))) != '\n') {
                    285:                if (j == ohc)
                    286:                        continue;
                    287:                if (j == CONT) {
                    288:                        pendnf++;
                    289:                        nflush = 0;
                    290:                        flushi();
                    291:                        ckul();
                    292:                        return;
                    293:                }
                    294:                storeline(i, -1);
                    295:        }
                    296:        if (ce) {
                    297:                ce--;
                    298:                if ((i = quant(nel / 2, HOR)) > 0)
                    299:                        un += i;
                    300:        }
                    301:        if (!nc)
                    302:                storeline((tchar)FILLER, 0);
                    303:        brflg = 2;
                    304:        tbreak();
                    305:        ckul();
                    306: rtn:
                    307:        pendnf = nflush = 0;
                    308: }
                    309: 
                    310: 
                    311: callsp()
                    312: {
                    313:        register i;
                    314: 
                    315:        if (flss)
                    316:                i = flss; 
                    317:        else 
                    318:                i = lss;
                    319:        flss = 0;
                    320:        casesp(i);
                    321: }
                    322: 
                    323: 
                    324: ckul()
                    325: {
                    326:        if (ul && (--ul == 0)) {
                    327:                cu = 0;
                    328:                font = sfont;
                    329:                mchbits();
                    330:        }
                    331:        if (it && (--it == 0) && itmac)
                    332:                control(itmac, 0);
                    333: }
                    334: 
                    335: 
                    336: storeline(c, w)
                    337: tchar c;
                    338: {
                    339:        register i;
                    340: 
                    341:        if (linep >= line + lnsize - 1) {
                    342:                if (!over) {
                    343:                        flusho();
                    344:                        fprintf(stderr, "troff: Line overflow.\n");
                    345:                        over++;
                    346:                        c = LEFTHAND;
                    347:                        w = -1;
                    348:                        goto s1;
                    349:                }
                    350:                return;
                    351:        }
                    352: s1:
                    353:        if (w == -1)
                    354:                w = width(c);
                    355:        ne += w;
                    356:        nel -= w;
                    357:        *linep++ = c;
                    358:        nc++;
                    359: }
                    360: 
                    361: 
                    362: newline(a)
                    363: int    a;
                    364: {
                    365:        register i, j, nlss;
                    366:        int     opn;
                    367: 
                    368:        if (a)
                    369:                goto nl1;
                    370:        if (dip != d) {
                    371:                j = lss;
                    372:                pchar1((tchar)FLSS);
                    373:                if (flss)
                    374:                        lss = flss;
                    375:                i = lss + dip->blss;
                    376:                dip->dnl += i;
                    377:                pchar1((tchar)i);
                    378:                pchar1((tchar)'\n');
                    379:                lss = j;
                    380:                dip->blss = flss = 0;
                    381:                if (dip->alss) {
                    382:                        pchar1((tchar)FLSS);
                    383:                        pchar1((tchar)dip->alss);
                    384:                        pchar1((tchar)'\n');
                    385:                        dip->dnl += dip->alss;
                    386:                        dip->alss = 0;
                    387:                }
                    388:                if (dip->ditrap && !dip->ditf &&  (dip->dnl >= dip->ditrap) && dip->dimac)
                    389:                        if (control(dip->dimac, 0)) {
                    390:                                trap++; 
                    391:                                dip->ditf++;
                    392:                        }
                    393:                return;
                    394:        }
                    395:        j = lss;
                    396:        if (flss)
                    397:                lss = flss;
                    398:        nlss = dip->alss + dip->blss + lss;
                    399:        v.nl += nlss;
                    400: #ifndef NROFF
                    401:        if (ascii) {
                    402:                dip->alss = dip->blss = 0;
                    403:        }
                    404: #endif
                    405:        pchar1((tchar)'\n');
                    406:        flss = 0;
                    407:        lss = j;
                    408:        if (v.nl < pl)
                    409:                goto nl2;
                    410: nl1:
                    411:        ejf = dip->hnl = v.nl = 0;
                    412:        ejl = frame;
                    413:        if (donef) {
                    414:                if ((!nc && !wch) || ndone)
                    415:                        done1(0);
                    416:                ndone++;
                    417:                donef = 0;
                    418:                if (frame == stk)
                    419:                        nflush++;
                    420:        }
                    421:        opn = v.pn;
                    422:        v.pn++;
                    423:        if (npnflg) {
                    424:                v.pn = npn;
                    425:                npn = npnflg = 0;
                    426:        }
                    427: nlpn:
                    428:        if (v.pn == pfrom) {
                    429:                print++;
                    430:                pfrom = -1;
                    431:        } else if (opn == pto) {
                    432:                print = 0;
                    433:                opn = -1;
                    434:                chkpn();
                    435:                goto nlpn;
                    436:        }
                    437:        if (print)
                    438:                newpage(v.pn);  /* supposedly in a clean state so can pause */
                    439:        if (stop && print) {
                    440:                dpn++;
                    441:                if (dpn >= stop) {
                    442:                        dpn = 0;
                    443:                        dostop();
                    444:                }
                    445:        }
                    446: nl2:
                    447:        trap = 0;
                    448:        if (v.nl == 0) {
                    449:                if ((j = findn(0)) != NTRAP)
                    450:                        trap = control(mlist[j], 0);
                    451:        } else if ((i = findt(v.nl - nlss)) <= nlss) {
                    452:                if ((j = findn1(v.nl - nlss + i)) == NTRAP) {
                    453:                        flusho();
                    454:                        fprintf(stderr, "troff: Trap botch.\n");
                    455:                        done2(-5);
                    456:                }
                    457:                trap = control(mlist[j], 0);
                    458:        }
                    459: }
                    460: 
                    461: 
                    462: findn1(a)
                    463: int    a;
                    464: {
                    465:        register i, j;
                    466: 
                    467:        for (i = 0; i < NTRAP; i++) {
                    468:                if (mlist[i]) {
                    469:                        if ((j = nlist[i]) < 0)
                    470:                                j += pl;
                    471:                        if (j == a)
                    472:                                break;
                    473:                }
                    474:        }
                    475:        return(i);
                    476: }
                    477: 
                    478: 
                    479: chkpn()
                    480: {
                    481:        pto = *(pnp++);
                    482:        pfrom = pto>=0 ? pto : -pto;
                    483:        if (pto == -32767) {
                    484:                flusho();
                    485:                done1(0);
                    486:        }
                    487:        if (pto < 0) {
                    488:                pto = -pto;
                    489:                print++;
                    490:                pfrom = 0;
                    491:        }
                    492: }
                    493: 
                    494: 
                    495: findt(a)
                    496: int    a;
                    497: {
                    498:        register i, j, k;
                    499: 
                    500:        k = 32767;
                    501:        if (dip != d) {
                    502:                if (dip->dimac && ((i = dip->ditrap - a) > 0))
                    503:                        k = i;
                    504:                return(k);
                    505:        }
                    506:        for (i = 0; i < NTRAP; i++) {
                    507:                if (mlist[i]) {
                    508:                        if ((j = nlist[i]) < 0)
                    509:                                j += pl;
                    510:                        if ((j -= a)  <=  0)
                    511:                                continue;
                    512:                        if (j < k)
                    513:                                k = j;
                    514:                }
                    515:        }
                    516:        i = pl - a;
                    517:        if (k > i)
                    518:                k = i;
                    519:        return(k);
                    520: }
                    521: 
                    522: 
                    523: findt1()
                    524: {
                    525:        register i;
                    526: 
                    527:        if (dip != d)
                    528:                i = dip->dnl;
                    529:        else 
                    530:                i = v.nl;
                    531:        return(findt(i));
                    532: }
                    533: 
                    534: 
                    535: eject(a)
                    536: struct s *a;
                    537: {
                    538:        register savlss;
                    539: 
                    540:        if (dip != d)
                    541:                return;
                    542:        ejf++;
                    543:        if (a)
                    544:                ejl = a;
                    545:        else 
                    546:                ejl = frame;
                    547:        if (trap)
                    548:                return;
                    549: e1:
                    550:        savlss = lss;
                    551:        lss = findt(v.nl);
                    552:        newline(0);
                    553:        lss = savlss;
                    554:        if (v.nl && !trap)
                    555:                goto e1;
                    556: }
                    557: 
                    558: 
                    559: movword()
                    560: {
                    561:        register w;
                    562:        tchar i, *wp;
                    563:        int     savwch, hys;
                    564: 
                    565:        over = 0;
                    566:        wp = wordp;
                    567:        if (!nwd) {
                    568:                while ((cbits(i = *wp++)) == ' ') {
                    569:                        wch--;
                    570:                        wne -= width(i);
                    571:                }
                    572:                wp--;
                    573:        }
                    574:        if ((wne > nel) &&  !hyoff && hyf &&  (!nwd || (nel > 3 * sps)) &&  (!(hyf & 02) || (findt1() > lss)))
                    575:                hyphen(wp);
                    576:        savwch = wch;
                    577:        hyp = hyptr;
                    578:        nhyp = 0;
                    579:        while (*hyp && (*hyp <= wp))
                    580:                hyp++;
                    581:        while (wch) {
                    582:                if ((hyoff != 1) && (*hyp == wp)) {
                    583:                        hyp++;
                    584:                        if (!wdstart ||  ((wp > (wdstart + 1)) &&  (wp < wdend) &&  (!(hyf & 04) || (wp < (wdend - 1))) &&
                    585:                            (!(hyf & '\b') || wp > wdstart + 2))) {
                    586:                                nhyp++;
                    587:                                storeline((tchar)IMP, 0);
                    588:                        }
                    589:                }
                    590:                i = *wp++;
                    591:                w = width(i);
                    592:                wne -= w;
                    593:                wch--;
                    594:                storeline(i, w);
                    595:        }
                    596:        if (nel >= 0) {
                    597:                nwd++;
                    598:                return(0);
                    599:        }
                    600:        xbitf = 1;
                    601:        hys = width((tchar)HYPHEN);
                    602: m1:
                    603:        if (!nhyp) {
                    604:                if (!nwd)
                    605:                        goto m3;
                    606:                if (wch == savwch)
                    607:                        goto m4;
                    608:        }
                    609:        if (*--linep != IMP)
                    610:                goto m5;
                    611:        if (!(--nhyp))
                    612:                if (!nwd)
                    613:                        goto m2;
                    614:        if (nel < hys) {
                    615:                nc--;
                    616:                goto m1;
                    617:        }
                    618: m2:
                    619:        if ((i = cbits(*(linep - 1))) != '-' &&  (i != EMDASH)) {
                    620:                *linep = (*(linep - 1) & SFMASK) | HYPHEN;
                    621:                w = width(*linep);
                    622:                nel -= w;
                    623:                ne += w;
                    624:                linep++;
                    625:        }
                    626: m3:
                    627:        nwd++;
                    628: m4:
                    629:        wordp = wp;
                    630:        return(1);
                    631: m5:
                    632:        nc--;
                    633:        w = width(*linep);
                    634:        ne -= w;
                    635:        nel += w;
                    636:        wne += w;
                    637:        wch++;
                    638:        wp--;
                    639:        goto m1;
                    640: }
                    641: 
                    642: 
                    643: horiz(i)
                    644: int    i;
                    645: {
                    646:        vflag = 0;
                    647:        if (i)
                    648:                pchar(makem(i));
                    649: }
                    650: 
                    651: 
                    652: setnel()
                    653: {
                    654:        if (!nc) {
                    655:                linep = line;
                    656:                if (un1 >= 0) {
                    657:                        un = un1;
                    658:                        un1 = -1;
                    659:                }
                    660:                nel = ll - un;
                    661:                ne = adsp = adrem = 0;
                    662:        }
                    663: }
                    664: 
                    665: 
                    666: getword(x)
                    667: int    x;
                    668: {
                    669:        register j, swp;
                    670:        tchar i;
                    671:        int     noword;
                    672: 
                    673:        noword = 0;
                    674:        if (x)
                    675:                if (pendw) {
                    676:                        *pendw = 0;
                    677:                        goto rtn;
                    678:                }
                    679:        if (wordp = pendw)
                    680:                goto g1;
                    681:        hyp = hyptr;
                    682:        wordp = word;
                    683:        over = wne = wch = 0;
                    684:        hyoff = 0;
                    685:        while (1) {
                    686:                j = cbits(i = GETCH());
                    687:                if (j == '\n') {
                    688:                        wne = wch = 0;
                    689:                        noword = 1;
                    690:                        goto rtn;
                    691:                }
                    692:                if (j == ohc) {
                    693:                        hyoff = 1;
                    694:                        continue;
                    695:                }
                    696:                if (j == ' ') {
                    697:                        storeword(i, width(i));
                    698:                        continue;
                    699:                }
                    700:                break;
                    701:        }
                    702:        swp = widthp;
                    703:        storeword(' ' | chbits, -1);
                    704:        if (spflg) {
                    705:                storeword(' ' | chbits, -1);
                    706:                spflg = 0;
                    707:        }
                    708:        widthp = swp;
                    709: g0:
                    710:        if (j == CONT) {
                    711:                pendw = wordp;
                    712:                nflush = 0;
                    713:                flushi();
                    714:                return(1);
                    715:        }
                    716:        if (hyoff != 1) {
                    717:                if (j == ohc) {
                    718:                        hyoff = 2;
                    719:                        *hyp++ = wordp;
                    720:                        if (hyp > (hyptr + NHYP - 1))
                    721:                                hyp = hyptr + NHYP - 1;
                    722:                        goto g1;
                    723:                }
                    724:                if ((j == '-') ||  (j == EMDASH))
                    725:                        if (wordp > word + 1) {
                    726:                                hyoff = 2;
                    727:                                *hyp++ = wordp + 1;
                    728:                                if (hyp > (hyptr + NHYP - 1))
                    729:                                        hyp = hyptr + NHYP - 1;
                    730:                        }
                    731:        }
                    732:        storeword(i, width(i));
                    733: g1:
                    734:        j = cbits(i = GETCH());
                    735:        if (j != ' ') {
                    736:                if (j != '\n')
                    737:                        goto g0;
                    738:                j = cbits(*(wordp - 1));
                    739:                if ((j == '.') ||  (j == '!') ||  (j == '?'))
                    740:                        spflg++;
                    741:        }
                    742:        *wordp = 0;
                    743: rtn:
                    744:        wdstart = 0;
                    745:        wordp = word;
                    746:        pendw = 0;
                    747:        *hyp++ = 0;
                    748:        setnel();
                    749:        return(noword);
                    750: }
                    751: 
                    752: 
                    753: storeword(c, w)
                    754: tchar c;
                    755: int    w;
                    756: {
                    757: 
                    758:        if (wordp >= &word[WDSIZE - 1]) {
                    759:                if (!over) {
                    760:                        flusho();
                    761:                        fprintf(stderr, "troff: Word overflow.\n");
                    762:                        over++;
                    763:                        c = LEFTHAND;
                    764:                        w = -1;
                    765:                        goto s1;
                    766:                }
                    767:                return;
                    768:        }
                    769: s1:
                    770:        if (w == -1)
                    771:                w = width(c);
                    772:        wne += w;
                    773:        *wordp++ = c;
                    774:        wch++;
                    775: }
                    776: 
                    777: 
                    778: #ifdef NROFF
                    779: tchar gettch()
                    780: {
                    781:        tchar i;
                    782:        int j;
                    783: 
                    784:        i = getch();
                    785:        j = cbits(i);
                    786:        if (ismot(i) || i & ulbit)
                    787:                return(i);
                    788:        if (cu && trtab[j] == ' ') {
                    789:                setcbits(i, '_');
                    790:                i &= ~ulbit;
                    791:        }
                    792:        if (!cu && j > 32 && !iscontrol(j) && !(*t.codetab[j-32] & 0200))
                    793:                i &= ~ulbit;
                    794:        return(i);
                    795: }
                    796: 
                    797: 
                    798: #endif

unix.superglobalmegacorp.com

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