Annotation of 41BSD/cmd/troff/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: extern int cbuf[];
        !            17: extern int *cp;
        !            18: extern int ch;
        !            19: extern int chbits;
        !            20: extern int dfact;
        !            21: extern int vflag;
        !            22: extern int pts;
        !            23: extern int fc;
        !            24: extern int padc;
        !            25: extern int tabtab[];
        !            26: extern int nlflg;
        !            27: extern int lss;
        !            28: extern int tabch, ldrch;
        !            29: extern int tabc, dotc;
        !            30: extern int nchar, rchar;
        !            31: extern int xxx;
        !            32: 
        !            33: setz(){
        !            34:        register i;
        !            35: 
        !            36:        if(!((i = getch()) & MOT))i |= ZBIT;
        !            37:        return(i);
        !            38: }
        !            39: setline(){
        !            40:        register *i, length, c;
        !            41:        int w, cnt, delim, rem, temp;
        !            42: 
        !            43:        if((delim = getch()) & MOT)return;
        !            44:                else delim &= CMASK;
        !            45:        vflag = 0;
        !            46:        dfact = EM;
        !            47:        length = quant(atoi(),HOR);
        !            48:        dfact = 1;
        !            49:        if(!length){
        !            50:                eat(delim);
        !            51:                return;
        !            52:        }
        !            53: s0:
        !            54:        if(((c = getch()) & CMASK) == delim){
        !            55:                ch = c;
        !            56:                c = 0204 | chbits;
        !            57:        }else if((c & CMASK) == FILLER)goto s0;
        !            58:        w = width(c);
        !            59:        i = cbuf;
        !            60:        if(length < 0){
        !            61:                *i++ = makem(length);
        !            62:                length = -length;
        !            63:        }
        !            64:        if(!(cnt = length/w)){
        !            65:                *i++ = makem(-(temp = ((w-length)/2)));
        !            66:                *i++ = c;
        !            67:                *i++ = makem(-(w - length - temp));
        !            68:                goto s1;
        !            69:        }
        !            70:        if(rem = length%w){
        !            71:                switch(c & CMASK){
        !            72:                        case 0204: /*rule*/
        !            73:                        case 0224: /*underrule*/
        !            74:                        case 0276: /*root en*/
        !            75:                                *i++ = c | ZBIT;
        !            76:                        default:
        !            77:                                *i++ = makem(rem);
        !            78:                }
        !            79:        }
        !            80:        if(cnt){
        !            81:                *i++ = RPT;
        !            82:                *i++ = cnt;
        !            83:                *i++ = c;
        !            84:        }
        !            85: s1:
        !            86:        *i++ = 0;
        !            87:        eat(delim);
        !            88:        cp = cbuf;
        !            89: }
        !            90: eat(c)
        !            91: int c;
        !            92: {
        !            93:        register i;
        !            94: 
        !            95:        while(((i = getch() & CMASK) != c) &&
        !            96:                (i != '\n'));
        !            97:        return(i);
        !            98: }
        !            99: setov(){
        !           100:        register i, j, k;
        !           101:        int *p, delim, o[NOV], w[NOV];
        !           102: 
        !           103:        if((delim = getch()) & MOT)return;
        !           104:                else delim &= CMASK;
        !           105:        for(k=0; (k<NOV) && ((j=(i = getch()) & CMASK) != delim) &&
        !           106:                (j != '\n'); k++){
        !           107:                        o[k] = i;
        !           108:                        w[k] = width(i);
        !           109:        }
        !           110:        o[k] = w[k] = 0;
        !           111:        if(o[0])for(j=1; j;){
        !           112:                j = 0;
        !           113:                for(k=1; o[k] ; k++){
        !           114:                        if(w[k-1] < w[k]){
        !           115:                                j++;
        !           116:                                i = w[k];
        !           117:                                w[k] = w[k-1];
        !           118:                                w[k-1] = i;
        !           119:                                i = o[k];
        !           120:                                o[k] = o[k-1];
        !           121:                                o[k-1] = i;
        !           122:                        }
        !           123:                }
        !           124:        }else return;
        !           125:        p = cbuf;
        !           126:        for(k=0; o[k]; k++){
        !           127:                *p++ = o[k];
        !           128:                *p++ = makem(-((w[k]+w[k+1])/2));
        !           129:        }
        !           130:        *p++ = makem(w[0]/2);
        !           131:        *p = 0;
        !           132:        cp = cbuf;
        !           133: }
        !           134: setbra(){
        !           135:        register i, *j, k;
        !           136:        int cnt, delim, dwn;
        !           137: 
        !           138:        if((delim = getch()) & MOT)return;
        !           139:                else delim &= CMASK;
        !           140:        j = cbuf + 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 = (i = getch()) & CMASK) != delim) && (k != '\n') &&
        !           149:                (j <= (cbuf+NC-4))){
        !           150:                *j++ = i | ZBIT;
        !           151:                *j++ = dwn;
        !           152:                cnt++;
        !           153:        }
        !           154:        if(--cnt < 0)return;
        !           155:                else if (!cnt){
        !           156:                        ch = *(j-2);
        !           157:                        return;
        !           158:        }
        !           159:        *j = 0;
        !           160: #ifdef NROFF
        !           161:        *--j = *cbuf = (cnt*t.Halfline) | MOT | NMOT | VMOT;
        !           162: #endif
        !           163: #ifndef NROFF
        !           164:        *--j = *cbuf = (cnt*EM)/2 | MOT | NMOT | VMOT;
        !           165: #endif
        !           166:        *--j &= ~ZBIT;
        !           167:        cp = cbuf;
        !           168: }
        !           169: setvline(){
        !           170:        register i, c, *k;
        !           171:        int cnt, neg, rem, ver, delim;
        !           172: 
        !           173:        if((delim = getch()) & MOT)return;
        !           174:                else delim &= CMASK;
        !           175:        dfact = lss;
        !           176:        vflag++;
        !           177:        i = quant(atoi(),VERT);
        !           178:        dfact = 1;
        !           179:        if(!i){
        !           180:                eat(delim);
        !           181:                vflag = 0;
        !           182:                return;
        !           183:        }
        !           184:        if(((c = getch()) & CMASK) == delim){
        !           185:                c = 0337 | chbits;      /*default box rule*/
        !           186:        }else getch();
        !           187:        c |= ZBIT;
        !           188:        neg = 0;
        !           189:        if(i < 0){
        !           190:                i = -i;
        !           191:                neg = NMOT;
        !           192:        }
        !           193: #ifdef NROFF
        !           194:        ver = 2*t.Halfline;
        !           195: #endif
        !           196: #ifndef NROFF
        !           197:        ver = EM;
        !           198: #endif
        !           199:        cnt = i/ver;
        !           200:        rem = makem(i%ver) | neg;
        !           201:        ver = makem(ver) | neg;
        !           202:        k = cbuf;
        !           203:        if(!neg)*k++ = ver;
        !           204:        if(rem & ~MOTV){
        !           205:                *k++ = c;
        !           206:                *k++ = rem;
        !           207:        }
        !           208:        while((k < (cbuf+NC-3)) && cnt--){
        !           209:                *k++ = c;
        !           210:                *k++ = ver;
        !           211:        }
        !           212:        *(k-2) &= ~ZBIT;
        !           213:        if(!neg)k--;
        !           214:        *k = 0;
        !           215:        cp = cbuf;
        !           216:        vflag = 0;
        !           217: }
        !           218: casefc(){
        !           219:        register i;
        !           220: 
        !           221:        fc = IMP;
        !           222:        padc = ' ';
        !           223:        if(skip() ||
        !           224:           ((i = getch()) & MOT) ||
        !           225:           ((i &= CMASK) == '\n'))return;
        !           226:        fc = i;
        !           227:        if(skip() || (ch & MOT) || ((ch &= CMASK) == fc))return;
        !           228:        padc = ch;
        !           229: }
        !           230: setfield(x)
        !           231: int x;
        !           232: {
        !           233:        register i, j, *fp;
        !           234:        int length, ws, npad, temp, type;
        !           235:        int **pp, *padptr[NPP];
        !           236:        static int fbuf[FBUFSZ];
        !           237:        int savfc, savtc, savlc;
        !           238: 
        !           239:        if(x == tabch) rchar = tabc | chbits;
        !           240:        else if(x ==  ldrch) rchar = dotc | chbits;
        !           241:        temp = npad = ws = 0;
        !           242:        savfc = fc; savtc = tabch; savlc = ldrch;
        !           243:        tabch = ldrch = fc = IMP;
        !           244:        for(j=0;;j++){
        !           245:                if((tabtab[j] & TMASK)== 0){
        !           246:                        if(x==savfc)prstr("Zero field width.\n");
        !           247:                        j = 0;
        !           248:                        goto rtn;
        !           249:                }
        !           250:                v.hp = sumhp(); /* XXX */
        !           251:                if((length = ((tabtab[j] & TMASK) - v.hp)) > 0 )break;
        !           252:        }
        !           253:        type = tabtab[j] & (~TMASK);
        !           254:        fp = fbuf;
        !           255:        pp = padptr;
        !           256:        if(x == savfc){while(1){
        !           257:                if(((j = (i = getch()) & CMASK)) == padc){
        !           258:                        npad++;
        !           259:                        *pp++ = fp;
        !           260:                        if(pp > (padptr + NPP - 1))break;
        !           261:                        goto s1;
        !           262:                }else if(j == savfc) break;
        !           263:                        else if(j == '\n'){
        !           264:                                temp = j;
        !           265:                                nlflg = 0;
        !           266:                                break;
        !           267:                        }
        !           268:                ws += width(i);
        !           269:        s1:
        !           270:                *fp++ = i;
        !           271:                if(fp > (fbuf + FBUFSZ -3))break;
        !           272:        }
        !           273:        if(!npad){
        !           274:                npad++;
        !           275:                *pp++ = fp;
        !           276:                *fp++ = 0;
        !           277:        }
        !           278:        *fp++ = temp;
        !           279:        *fp++ = 0;
        !           280:        temp = i = (j = length-ws)/npad;
        !           281:        i = (i/HOR)*HOR;
        !           282:        if((j -= i*npad) <0)j = -j;
        !           283:        i = makem(i);
        !           284:        if(temp <0)i |= NMOT;
        !           285:        for(;npad > 0; npad--){
        !           286:                *(*--pp) = i;
        !           287:                if(j){
        !           288:                        j -= HOR;
        !           289:                        (*(*pp)) += HOR;
        !           290:                }
        !           291:        }
        !           292:        cp = fbuf;
        !           293:        j = 0;
        !           294:        }else if(type == 0){
        !           295:        /*plain tab or leader*/
        !           296:                if((j = width(rchar)) == 0)nchar = 0;
        !           297:                else{
        !           298:                        nchar = length /j;
        !           299:                        length %= j;
        !           300:                }
        !           301:                if(length)j = length | MOT;
        !           302:                else j = getch0();
        !           303:        }else{
        !           304:        /*center tab*/
        !           305:        /*right tab*/
        !           306:                while(((j = (i = getch()) & CMASK) != savtc) &&
        !           307:                        (j != '\n') && (j != savlc)){
        !           308:                        ws += width(i);
        !           309:                        *fp++ = i;
        !           310:                        if(fp > (fbuf +FBUFSZ - 3)) break;
        !           311:                }
        !           312:                *fp++ = i;
        !           313:                *fp++ = 0;
        !           314:                if(type == RTAB)length -= ws;
        !           315:                else length -= ws/2; /*CTAB*/
        !           316:                if(((j = width(rchar)) == 0) || (length <= 0))nchar = 0;
        !           317:                else{
        !           318:                        nchar = length/j;
        !           319:                        length %= j;
        !           320:                }
        !           321:                length = (length/HOR)*HOR;
        !           322:                j = makem(length);
        !           323:                cp = fbuf;
        !           324:                nlflg = 0;
        !           325:        }
        !           326: rtn:
        !           327:        fc = savfc; tabch = savtc; ldrch = savlc;
        !           328:        return(j);
        !           329: }

unix.superglobalmegacorp.com

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