Annotation of researchv8dc/cmd/troff/n9.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

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