Annotation of 41BSD/lib/libcurses/cr_put.c, revision 1.1

1.1     ! root        1: # include      "curses.ext"
        !             2: # include      "cr_ex.h"
        !             3: 
        !             4: # define       HARDTABS        8
        !             5: 
        !             6: extern char    *tgoto();
        !             7: int            plodput();
        !             8: 
        !             9: /*
        !            10:  * Terminal driving and line formatting routines.
        !            11:  * Basic motion optimizations are done here as well
        !            12:  * as formatting of lines (printing of control characters,
        !            13:  * line numbering and the like).
        !            14:  */
        !            15: 
        !            16: /*
        !            17:  * Sync the position of the output cursor.
        !            18:  * Most work here is rounding for terminal boundaries getting the
        !            19:  * column position implied by wraparound or the lack thereof and
        !            20:  * rolling up the screen to get destline on the screen.
        !            21:  */
        !            22: 
        !            23: static int     outcol, outline, destcol, destline, plodcnt;
        !            24: 
        !            25: mvcur(ly, lx, y, x)
        !            26: int    ly, lx, y, x; {
        !            27: 
        !            28: #ifdef DEBUG
        !            29:        fprintf(outf, "MVCUR: moving cursor from (%d,%d) to (%d,%d)\n", ly, lx, y, x);
        !            30: #endif
        !            31:        destcol = x;
        !            32:        destline = y;
        !            33:        outcol = lx;
        !            34:        outline = ly;
        !            35:        fgoto();
        !            36: }
        !            37: 
        !            38: fgoto() {
        !            39: 
        !            40:        reg char        *cgp;
        !            41:        reg int         l, c;
        !            42: 
        !            43:        if (destcol > COLS - 1) {
        !            44:                destline += destcol / COLS;
        !            45:                destcol %= COLS;
        !            46:        }
        !            47:        if (outcol > COLS - 1) {
        !            48:                l = (outcol + 1) / COLS;
        !            49:                outline += l;
        !            50:                outcol %= COLS;
        !            51:                if (AM == 0) {
        !            52:                        while (l > 0) {
        !            53:                                if (pfast)
        !            54:                                        putchar('\r');
        !            55:                                putchar('\n');
        !            56:                                l--;
        !            57:                        }
        !            58:                        outcol = 0;
        !            59:                }
        !            60:                if (outline > LINES - 1) {
        !            61:                        destline -= outline - (LINES - 1);
        !            62:                        outline = LINES - 1;
        !            63:                }
        !            64:        }
        !            65:        if (destline > LINES - 1) {
        !            66:                l = destline;
        !            67:                destline = LINES - 1;
        !            68:                if (outline < LINES - 1) {
        !            69:                        c = destcol;
        !            70:                        if (pfast == 0 && !CA)
        !            71:                                destcol = 0;
        !            72:                        fgoto();
        !            73:                        destcol = c;
        !            74:                }
        !            75:                while (l > LINES - 1) {
        !            76:                        putchar('\n');
        !            77:                        l--;
        !            78:                        if (pfast == 0)
        !            79:                                outcol = 0;
        !            80:                }
        !            81:        }
        !            82:        if (destline < outline && !(CA || UP != NULL))
        !            83:                destline = outline;
        !            84:        cgp = tgoto(CM, destcol, destline);
        !            85:        if (CA)
        !            86:                if (plod(strlen(cgp)) > 0)
        !            87:                        plod(0);
        !            88:                else
        !            89:                        tputs(cgp, 0, _putchar);
        !            90:        else
        !            91:                plod(0);
        !            92:        outline = destline;
        !            93:        outcol = destcol;
        !            94: }
        !            95: 
        !            96: char
        !            97: _putchar(c)
        !            98: reg char       c; {
        !            99: 
        !           100:        putchar(c);
        !           101: #ifdef DEBUG
        !           102:        fprintf(outf, "_PUTCHAR(%s)\n", unctrl(c));
        !           103: #endif
        !           104: }
        !           105: 
        !           106: extern bool    plodflg;
        !           107: extern int     plodcnt;
        !           108: 
        !           109: plod(cnt)
        !           110: int    cnt; {
        !           111: 
        !           112:        reg int         i, j, k;
        !           113:        reg int         soutcol, soutline;
        !           114: 
        !           115:        plodcnt = plodflg = cnt;
        !           116:        soutcol = outcol;
        !           117:        soutline = outline;
        !           118:        if (HO) {
        !           119:                if (GT)
        !           120:                        i = (destcol / HARDTABS) + (destcol % HARDTABS);
        !           121:                else
        !           122:                        i = destcol;
        !           123:         if (destcol >= outcol) {
        !           124:                 j = destcol / HARDTABS - outcol / HARDTABS;
        !           125:                 if (GT && j)
        !           126:                         j += destcol % HARDTABS;
        !           127:                else
        !           128:                        j = destcol - outcol;
        !           129:         } else
        !           130:                        if (outcol - destcol <= i && (BS || BC))
        !           131:                                i = j = outcol - destcol;
        !           132:                        else
        !           133:                                j = i + 1;
        !           134:                k = outline - destline;
        !           135:                if (k < 0)
        !           136:                        k = -k;
        !           137:                j += k;
        !           138:                if (i + destline < j) {
        !           139:                        tputs(HO, 0, plodput);
        !           140:                        outcol = outline = 0;
        !           141:                } else if (LL) {
        !           142:                        k = (LINES - 1) - destline;
        !           143:                        if (i + k + 2 < j) {
        !           144:                                tputs(LL, 0, plodput);
        !           145:                                outcol = 0;
        !           146:                                outline = LINES - 1;
        !           147:                        }
        !           148:                }
        !           149:        }
        !           150:        if (GT)
        !           151:         i = destcol % HARDTABS + destcol / HARDTABS;
        !           152:        else
        !           153:                i = destcol;
        !           154: /*
        !           155:        if (BT && outcol > destcol && (j = (((outcol+7) & ~7) - destcol - 1) >> 3)) {
        !           156:                j *= (k = strlen(BT));
        !           157:                if ((k += (destcol&7)) > 4)
        !           158:                        j += 8 - (destcol&7);
        !           159:                else
        !           160:                        j += k;
        !           161:        } else
        !           162: */
        !           163:                j = outcol - destcol;
        !           164:        /*
        !           165:         * If we will later need a \n which will turn into a \r\n by
        !           166:         * the system or the terminal, then don't bother to try to \r.
        !           167:         */
        !           168:        if ((NONL || !pfast) && outline < destline)
        !           169:                goto dontcr;
        !           170:        /*
        !           171:         * If the terminal will do a \r\n and there isn't room for it,
        !           172:         * then we can't afford a \r.
        !           173:         */
        !           174:        if (NC && outline >= destline)
        !           175:                goto dontcr;
        !           176:        /*
        !           177:         * If it will be cheaper, or if we can't back up, then send
        !           178:         * a return preliminarily.
        !           179:         */
        !           180:        if (j > i + 1 || outcol > destcol && !BS && !BC) {
        !           181:                plodput('\r');
        !           182:                if (NC) {
        !           183:                        plodput('\n');
        !           184:                        outline++;
        !           185:                }
        !           186:                outcol = 0;
        !           187:        }
        !           188: dontcr:
        !           189:        while (outline < destline) {
        !           190:                outline++;
        !           191:                plodput('\n');
        !           192:                if (plodcnt < 0)
        !           193:                        goto out;
        !           194:                if (NONL || pfast == 0)
        !           195:                        outcol = 0;
        !           196:        }
        !           197:        if (BT)
        !           198:                k = strlen(BT);
        !           199:        while (outcol > destcol) {
        !           200:                if (plodcnt < 0)
        !           201:                        goto out;
        !           202: /*
        !           203:                if (BT && outcol - destcol > 4+k) {
        !           204:                        tputs(BT, 0, plodput);
        !           205:                        outcol--;
        !           206:                        outcol &= ~7;
        !           207:                        continue;
        !           208:                }
        !           209: */
        !           210:                outcol--;
        !           211:                if (BC)
        !           212:                        tputs(BC, 0, plodput);
        !           213:                else
        !           214:                        plodput('\b');
        !           215:        }
        !           216:        while (outline > destline) {
        !           217:                outline--;
        !           218:                tputs(UP, 0, plodput);
        !           219:                if (plodcnt < 0)
        !           220:                        goto out;
        !           221:        }
        !           222:        if (GT && destcol - outcol > 1) {
        !           223:         for (;;) {
        !           224:                 i = (outcol / HARDTABS + 1) * HARDTABS;
        !           225:                 if (i > destcol)
        !           226:                         break;
        !           227:                        if (TA)
        !           228:                                tputs(TA, 0, plodput);
        !           229:                        else
        !           230:                                plodput('\t');
        !           231:                        outcol = i;
        !           232:                }
        !           233:                if (destcol - outcol > 4 && i < COLS && (BC || BS)) {
        !           234:                        if (TA)
        !           235:                                tputs(TA, 0, plodput);
        !           236:                        else
        !           237:                                plodput('\t');
        !           238:                        outcol = i;
        !           239:                        while (outcol > destcol) {
        !           240:                                outcol--;
        !           241:                                if (BC)
        !           242:                                        tputs(BC, 0, plodput);
        !           243:                                else
        !           244:                                        plodput('\b');
        !           245:                        }
        !           246:                }
        !           247:        }
        !           248:        while (outcol < destcol) {
        !           249:                if (ND)
        !           250:                        tputs(ND, 0, plodput);
        !           251:                else
        !           252:                        plodput(' ');
        !           253:                outcol++;
        !           254:                if (plodcnt < 0)
        !           255:                        goto out;
        !           256:        }
        !           257: out:
        !           258:        if (plodflg) {
        !           259:                outcol = soutcol;
        !           260:                outline = soutline;
        !           261:        }
        !           262:        return(plodcnt);
        !           263: }
        !           264: 
        !           265: /*
        !           266:  * Move (slowly) to destination.
        !           267:  * Hard thing here is using home cursor on really deficient terminals.
        !           268:  * Otherwise just use cursor motions, hacking use of tabs and overtabbing
        !           269:  * and backspace.
        !           270:  */
        !           271: 
        !           272: static bool    plodflg;
        !           273: 
        !           274: plodput(c)
        !           275: reg char       c; {
        !           276: 
        !           277:        if (plodflg)
        !           278:                plodcnt--;
        !           279:        else {
        !           280:                putchar(c);
        !           281: #ifdef DEBUG
        !           282:                fprintf(outf, "PLODPUT(%s)\n", unctrl(c));
        !           283: #endif
        !           284:        }
        !           285: }
        !           286: 
        !           287: /*
        !           288:  * Put with padding
        !           289:  */
        !           290: putpad(cp)
        !           291: reg char       *cp; {
        !           292: 
        !           293:        fflush(stdout);
        !           294: #ifdef DEBUG
        !           295:        fprintf(outf, "PUTPAD: _puts(\"%s\")\n", cp);
        !           296: #endif
        !           297:        _puts(cp);
        !           298: }

unix.superglobalmegacorp.com

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