Annotation of researchv10no/cmd/troff/Old/n9.c, revision 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: /*
        !            11: troff9.c
        !            12: 
        !            13: misc functions
        !            14: */
        !            15: 
        !            16: #include <sgtty.h>
        !            17: #include "ext.h"
        !            18: 
        !            19: tchar setz()
        !            20: {
        !            21:        tchar i;
        !            22: 
        !            23:        if (!ismot(i = getch()))
        !            24:                i |= ZBIT;
        !            25:        return(i);
        !            26: }
        !            27: 
        !            28: 
        !            29: setline()
        !            30: {
        !            31:        register tchar *i;
        !            32:        tchar c;
        !            33:        int     length;
        !            34:        int     w, cnt, delim, rem, temp;
        !            35: 
        !            36:        if (ismot(c = getch()))
        !            37:                return;
        !            38:        delim = cbits(c);
        !            39:        vflag = 0;
        !            40:        dfact = EM;
        !            41:        length = quant(atoi(), HOR);
        !            42:        dfact = 1;
        !            43:        if (!length) {
        !            44:                eat(delim);
        !            45:                return;
        !            46:        }
        !            47: s0:
        !            48:        if ((cbits(c = getch())) == delim) {
        !            49:                ch = c;
        !            50:                c = RULE | chbits;
        !            51:        } else if (cbits(c) == FILLER)
        !            52:                goto s0;
        !            53:        w = width(c);
        !            54:        i = cbuf;
        !            55:        if (length < 0) {
        !            56:                *i++ = makem(length);
        !            57:                length = -length;
        !            58:        }
        !            59:        if (!(cnt = length / w)) {
        !            60:                *i++ = makem(-(temp = ((w - length) / 2)));
        !            61:                *i++ = c;
        !            62:                *i++ = makem(-(w - length - temp));
        !            63:                goto s1;
        !            64:        }
        !            65:        if (rem = length % w) {
        !            66:                if (cbits(c) == RULE || cbits(c) == UNDERLINE || cbits(c) == ROOTEN)
        !            67:                        *i++ = c | ZBIT;
        !            68:                *i++ = makem(rem);
        !            69:        }
        !            70:        if (cnt) {
        !            71:                *i++ = RPT;
        !            72:                *i++ = cnt;
        !            73:                *i++ = c;
        !            74:        }
        !            75: s1:
        !            76:        *i++ = 0;
        !            77:        eat(delim);
        !            78:        cp = cbuf;
        !            79: }
        !            80: 
        !            81: 
        !            82: eat(c)
        !            83: int    c;
        !            84: {
        !            85:        register i;
        !            86: 
        !            87:        while ((i = cbits(getch())) != c &&  (i != '\n'))
        !            88:                ;
        !            89:        return(i);
        !            90: }
        !            91: 
        !            92: 
        !            93: setov()
        !            94: {
        !            95:        register j, k;
        !            96:        tchar i, *p, o[NOV];
        !            97:        int     delim, w[NOV];
        !            98: 
        !            99:        if (ismot(i = getch()))
        !           100:                return;
        !           101:        delim = cbits(i);
        !           102:        for (k = 0; (k < NOV) && ((j = cbits(i = getch())) != delim) &&  (j != '\n'); k++) {
        !           103:                o[k] = i;
        !           104:                w[k] = width(i);
        !           105:        }
        !           106:        o[k] = w[k] = 0;
        !           107:        if (o[0])
        !           108:                for (j = 1; j; ) {
        !           109:                        j = 0;
        !           110:                        for (k = 1; o[k] ; k++) {
        !           111:                                if (w[k-1] < w[k]) {
        !           112:                                        j++;
        !           113:                                        i = w[k];
        !           114:                                        w[k] = w[k-1];
        !           115:                                        w[k-1] = i;
        !           116:                                        i = o[k];
        !           117:                                        o[k] = o[k-1];
        !           118:                                        o[k-1] = i;
        !           119:                                }
        !           120:                        }
        !           121:                }
        !           122:        else 
        !           123:                return;
        !           124:        p = cbuf;
        !           125:        for (k = 0; o[k]; k++) {
        !           126:                *p++ = o[k];
        !           127:                *p++ = makem(-((w[k] + w[k+1]) / 2));
        !           128:        }
        !           129:        *p++ = makem(w[0] / 2);
        !           130:        *p = 0;
        !           131:        cp = cbuf;
        !           132: }
        !           133: 
        !           134: 
        !           135: setbra()
        !           136: {
        !           137:        register k;
        !           138:        tchar i, *j, dwn;
        !           139:        int     cnt, delim;
        !           140: 
        !           141:        if (ismot(i = getch()))
        !           142:                return;
        !           143:        delim = cbits(i);
        !           144:        j = cbuf + 1;
        !           145:        cnt = 0;
        !           146: #ifdef NROFF
        !           147:        dwn = (2 * t.Halfline) | MOT | VMOT;
        !           148: #endif
        !           149: #ifndef NROFF
        !           150:        dwn = EM | MOT | VMOT;
        !           151: #endif
        !           152:        while (((k = cbits(i = getch())) != delim) && (k != '\n') &&  (j <= (cbuf + NC - 4))) {
        !           153:                *j++ = i | ZBIT;
        !           154:                *j++ = dwn;
        !           155:                cnt++;
        !           156:        }
        !           157:        if (--cnt < 0)
        !           158:                return;
        !           159:        else if (!cnt) {
        !           160:                ch = *(j - 2);
        !           161:                return;
        !           162:        }
        !           163:        *j = 0;
        !           164: #ifdef NROFF
        !           165:        *--j = *cbuf = (cnt * t.Halfline) | MOT | NMOT | VMOT;
        !           166: #endif
        !           167: #ifndef NROFF
        !           168:        *--j = *cbuf = (cnt * EM) / 2 | MOT | NMOT | VMOT;
        !           169: #endif
        !           170:        *--j &= ~ZBIT;
        !           171:        cp = cbuf;
        !           172: }
        !           173: 
        !           174: 
        !           175: setvline()
        !           176: {
        !           177:        register i;
        !           178:        tchar c, *k, rem, ver, neg;
        !           179:        int     cnt, delim, v;
        !           180: 
        !           181:        if (ismot(c = getch()))
        !           182:                return;
        !           183:        delim = cbits(c);
        !           184:        dfact = lss;
        !           185:        vflag++;
        !           186:        i = quant(atoi(), VERT);
        !           187:        dfact = 1;
        !           188:        if (!i) {
        !           189:                eat(delim);
        !           190:                vflag = 0;
        !           191:                return;
        !           192:        }
        !           193:        if ((cbits(c = getch())) == delim) {
        !           194:                c = BOXRULE | chbits;   /*default box rule*/
        !           195:        } else 
        !           196:                getch();
        !           197:        c |= ZBIT;
        !           198:        neg = 0;
        !           199:        if (i < 0) {
        !           200:                i = -i;
        !           201:                neg = NMOT;
        !           202:        }
        !           203: #ifdef NROFF
        !           204:        v = 2 * t.Halfline;
        !           205: #endif
        !           206: #ifndef NROFF
        !           207:        v = EM;
        !           208: #endif
        !           209:        cnt = i / v;
        !           210:        rem = makem(i % v) | neg;
        !           211:        ver = makem(v) | neg;
        !           212:        k = cbuf;
        !           213:        if (!neg)
        !           214:                *k++ = ver;
        !           215:        if (absmot(rem) != 0) {
        !           216:                *k++ = c;
        !           217:                *k++ = rem;
        !           218:        }
        !           219:        while ((k < (cbuf + NC - 3)) && cnt--) {
        !           220:                *k++ = c;
        !           221:                *k++ = ver;
        !           222:        }
        !           223:        *(k - 2) &= ~ZBIT;
        !           224:        if (!neg)
        !           225:                k--;
        !           226:        *k = 0;
        !           227:        cp = cbuf;
        !           228:        vflag = 0;
        !           229: }
        !           230: 
        !           231: 
        !           232: setdraw()      /* generate internal cookies for a drawing function */
        !           233: {
        !           234:        int i, j, k, dx[100], dy[100], delim, type, temp;
        !           235:        tchar c;
        !           236:        /* input is \D'f x y x y ... c' (or at least it had better be) */
        !           237:        /* this does drawing function f with character c and the */
        !           238:        /* specified x,y pairs interpreted as appropriate */
        !           239: 
        !           240:        /* l x y:       line from here by x,y */
        !           241:        /* c x:         circle of diameter x, left side here */
        !           242:        /* e x y:       ellipse of diameters x,y, left side here */
        !           243:        /* a x y r:     arc to x,y with radius r (ccw) */
        !           244:        /* ~ x y ...:   wiggly line */
        !           245: 
        !           246:        if (ismot(c = getch()))
        !           247:                return;
        !           248:        delim = cbits(c);
        !           249:        type = cbits(getch());
        !           250:        for (i = 0; i < 50 ; i++) {
        !           251:                c = getch();
        !           252:                if (cbits(c) == delim)
        !           253:                        break;
        !           254:        /* ought to pick up optional drawing character */
        !           255:                if (cbits(c) != ' ')
        !           256:                        ch = c;
        !           257:                vflag = 0;
        !           258:                dfact = EM;
        !           259:                dx[i] = quant(atoi(), HOR);
        !           260:                if (dx[i] > MAXMOT)
        !           261:                        dx[i] = MAXMOT;
        !           262:                else if (dx[i] < -MAXMOT)
        !           263:                        dx[i] = -MAXMOT;
        !           264:                if (cbits((c = getch())) == delim) {    /* spacer */
        !           265:                        dy[i++] = 0;
        !           266:                        break;
        !           267:                }
        !           268:                vflag = 1;
        !           269:                dfact = lss;
        !           270:                dy[i] = quant(atoi(), VERT);
        !           271:                if (dy[i] > MAXMOT)
        !           272:                        dy[i] = MAXMOT;
        !           273:                else if (dy[i] < -MAXMOT)
        !           274:                        dy[i] = -MAXMOT;
        !           275:        }
        !           276:        dfact = 1;
        !           277:        vflag = 0;
        !           278: #ifndef NROFF
        !           279:        cbuf[0] = DRAWFCN | chbits | ZBIT;
        !           280:        cbuf[1] = type | chbits | ZBIT;
        !           281:        cbuf[2] = '.' | chbits | ZBIT;  /* indicates to use default drawing character */
        !           282:        for (k = 0, j = 3; k < i; k++) {
        !           283:                cbuf[j++] = MOT | ((dx[k] >= 0) ? dx[k] : (NMOT | -dx[k]));
        !           284:                cbuf[j++] = MOT | VMOT | ((dy[k] >= 0) ? dy[k] : (NMOT | -dy[k]));
        !           285:        }
        !           286:        if (type == DRAWELLIPSE) {
        !           287:                cbuf[5] = cbuf[4] | NMOT;       /* so the net vertical is zero */
        !           288:                j = 6;
        !           289:        }
        !           290:        cbuf[j++] = '.' | chbits | ZBIT;        /* marks end for ptout */
        !           291:        cbuf[j] = 0;
        !           292:        cp = cbuf;
        !           293: #endif
        !           294: }
        !           295: 
        !           296: 
        !           297: casefc()
        !           298: {
        !           299:        register i;
        !           300:        tchar j;
        !           301: 
        !           302:        fc = IMP;
        !           303:        padc = ' ';
        !           304:        if (skip() || ismot(j = getch()) || (i = cbits(j)) == '\n')
        !           305:                return;
        !           306:        fc = i;
        !           307:        if (skip() || ismot(ch) || (ch = cbits(ch)) == fc)
        !           308:                return;
        !           309:        padc = ch;
        !           310: }
        !           311: 
        !           312: 
        !           313: tchar setfield(x)
        !           314: int    x;
        !           315: {
        !           316:        tchar ii, jj, *fp;
        !           317:        register i, j;
        !           318:        int     length, ws, npad, temp, type;
        !           319:        tchar * *pp, *padptr[NPP];
        !           320:        static tchar fbuf[FBUFSZ];
        !           321:        int     savfc, savtc, savlc;
        !           322: 
        !           323:        if (x == tabch) 
        !           324:                rchar = tabc | chbits;
        !           325:        else if (x ==  ldrch) 
        !           326:                rchar = dotc | chbits;
        !           327:        temp = npad = ws = 0;
        !           328:        savfc = fc;
        !           329:        savtc = tabch;
        !           330:        savlc = ldrch;
        !           331:        tabch = ldrch = fc = IMP;
        !           332:        for (j = 0; ; j++) {
        !           333:                if ((tabtab[j] & TMASK) == 0) {
        !           334:                        if (x == savfc)
        !           335:                                fprintf(stderr, "troff: zero field width.\n");
        !           336:                        jj = 0;
        !           337:                        goto rtn;
        !           338:                }
        !           339:                v.hp = sumhp(); /* XXX */
        !           340:                if ((length = ((tabtab[j] & TMASK) - v.hp)) > 0 )
        !           341:                        break;
        !           342:        }
        !           343:        type = tabtab[j] & (~TMASK);
        !           344:        fp = fbuf;
        !           345:        pp = padptr;
        !           346:        if (x == savfc) {
        !           347:                while (1) {
        !           348:                        if (((j = cbits(ii = getch()))) == padc) {
        !           349:                                npad++;
        !           350:                                *pp++ = fp;
        !           351:                                if (pp > (padptr + NPP - 1))
        !           352:                                        break;
        !           353:                                goto s1;
        !           354:                        } else if (j == savfc) 
        !           355:                                break;
        !           356:                        else if (j == '\n') {
        !           357:                                temp = j;
        !           358:                                nlflg = 0;
        !           359:                                break;
        !           360:                        }
        !           361:                        ws += width(ii);
        !           362: s1:
        !           363:                        *fp++ = ii;
        !           364:                        if (fp > (fbuf + FBUFSZ - 3))
        !           365:                                break;
        !           366:                }
        !           367:                if (!npad) {
        !           368:                        npad++;
        !           369:                        *pp++ = fp;
        !           370:                        *fp++ = 0;
        !           371:                }
        !           372:                *fp++ = temp;
        !           373:                *fp++ = 0;
        !           374:                temp = i = (j = length - ws) / npad;
        !           375:                i = (i / HOR) * HOR;
        !           376:                if ((j -= i * npad) < 0)
        !           377:                        j = -j;
        !           378:                ii = makem(i);
        !           379:                if (temp < 0)
        !           380:                        ii |= NMOT;
        !           381:                for (; npad > 0; npad--) {
        !           382:                        *(*--pp) = ii;
        !           383:                        if (j) {
        !           384:                                j -= HOR;
        !           385:                                (*(*pp)) += HOR;
        !           386:                        }
        !           387:                }
        !           388:                cp = fbuf;
        !           389:                jj = 0;
        !           390:        } else if (type == 0) {
        !           391:                /*plain tab or leader*/
        !           392:                if ((j = width(rchar)) == 0)
        !           393:                        nchar = 0;
        !           394:                else {
        !           395:                        nchar = length / j;
        !           396:                        length %= j;
        !           397:                }
        !           398:                if (length)
        !           399:                        jj = length | MOT;
        !           400:                else 
        !           401:                        jj = getch0();
        !           402:        } else {
        !           403:                /*center tab*/
        !           404:                /*right tab*/
        !           405:                while (((j = cbits(ii = getch())) != savtc) &&  (j != '\n') && (j != savlc)) {
        !           406:                        ws += width(ii);
        !           407:                        *fp++ = ii;
        !           408:                        if (fp > (fbuf + FBUFSZ - 3)) 
        !           409:                                break;
        !           410:                }
        !           411:                *fp++ = ii;
        !           412:                *fp++ = 0;
        !           413:                if (type == RTAB)
        !           414:                        length -= ws;
        !           415:                else 
        !           416:                        length -= ws / 2; /*CTAB*/
        !           417:                if (((j = width(rchar)) == 0) || (length <= 0))
        !           418:                        nchar = 0;
        !           419:                else {
        !           420:                        nchar = length / j;
        !           421:                        length %= j;
        !           422:                }
        !           423:                length = (length / HOR) * HOR;
        !           424:                jj = makem(length);
        !           425:                cp = fbuf;
        !           426:                nlflg = 0;
        !           427:        }
        !           428: rtn:
        !           429:        fc = savfc;
        !           430:        tabch = savtc;
        !           431:        ldrch = savlc;
        !           432:        return(jj);
        !           433: }
        !           434: 
        !           435: 

unix.superglobalmegacorp.com

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