[BACK]Return to wwupdate.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / window

Annotation of src/usr.bin/window/wwupdate.c, Revision 1.4

1.4     ! mpech       1: /*     $OpenBSD: wwupdate.c,v 1.3 1997/02/25 00:05:13 downsj Exp $     */
1.1       deraadt     2: /*     $NetBSD: wwupdate.c,v 1.3 1995/09/28 10:36:00 tls Exp $ */
                      3:
                      4: /*
                      5:  * Copyright (c) 1983, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Edward Wang at The University of California, Berkeley.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: #if 0
                     42: static char sccsid[] = "@(#)wwupdate.c 8.1 (Berkeley) 6/6/93";
                     43: #else
1.4     ! mpech      44: static char rcsid[] = "$OpenBSD: wwupdate.c,v 1.3 1997/02/25 00:05:13 downsj Exp $";
1.1       deraadt    45: #endif
                     46: #endif /* not lint */
                     47:
                     48: #include "ww.h"
                     49: #include "tt.h"
                     50:
                     51: wwupdate1(top, bot)
                     52: {
                     53:        int i;
1.4     ! mpech      54:        int j;
1.1       deraadt    55:        char *touched;
                     56:        struct ww_update *upd;
                     57:        char check_clreos = 0;
                     58:        int scan_top, scan_bot;
                     59:
                     60:        wwnupdate++;
                     61:        {
1.4     ! mpech      62:                char *t1 = wwtouched + top, *t2 = wwtouched + bot;
        !            63:                int n;
1.1       deraadt    64:
                     65:                while (!*t1++)
                     66:                        if (t1 == t2)
                     67:                                return;
                     68:                while (!*--t2)
                     69:                        ;
                     70:                scan_top = top = t1 - wwtouched - 1;
                     71:                scan_bot = bot = t2 - wwtouched + 1;
                     72:                if (scan_bot - scan_top > 1 &&
                     73:                    (tt.tt_clreos != 0 || tt.tt_clear != 0)) {
                     74:                        int st = tt.tt_clreos != 0 ? scan_top : 0;
                     75:
                     76:                        /*
                     77:                         * t1 is one past the first touched row,
                     78:                         * t2 is on the last touched row.
                     79:                         */
                     80:                        for (t1--, n = 1; t1 < t2;)
                     81:                                if (*t1++)
                     82:                                        n++;
                     83:                        /*
                     84:                         * If we can't clreos then we try for clearing
                     85:                         * the whole screen.
                     86:                         */
                     87:                        if (check_clreos = n * 10 > (wwnrow - st) * 9) {
                     88:                                scan_top = st;
                     89:                                scan_bot = wwnrow;
                     90:                        }
                     91:                }
                     92:        }
                     93:        if (tt.tt_clreol == 0 && !check_clreos)
                     94:                goto simple;
                     95:        for (i = scan_top, touched = &wwtouched[i], upd = &wwupd[i];
                     96:             i < scan_bot;
                     97:             i++, touched++, upd++) {
1.4     ! mpech      98:                int gain = 0;
        !            99:                int best_gain = 0;
        !           100:                int best_col;
        !           101:                union ww_char *ns, *os;
1.1       deraadt   102:
                    103:                if (wwinterrupt())
                    104:                        return;
                    105:                if (!check_clreos && !*touched)
                    106:                        continue;
                    107:                wwnupdscan++;
                    108:                j = wwncol;
                    109:                ns = &wwns[i][j];
                    110:                os = &wwos[i][j];
                    111:                while (--j >= 0) {
                    112:                        /*
                    113:                         * The cost of clearing is:
                    114:                         *      ncol - nblank + X
                    115:                         * The cost of straight update is, more or less:
                    116:                         *      ncol - nsame
                    117:                         * We clear if  nblank - nsame > X
                    118:                         * X is the clreol overhead.
                    119:                         * So we make gain = nblank - nsame.
                    120:                         */
                    121:                        if ((--ns)->c_w == (--os)->c_w)
                    122:                                gain--;
                    123:                        else
                    124:                                best_gain--;
                    125:                        if (ns->c_w == ' ')
                    126:                                gain++;
                    127:                        if (gain > best_gain) {
                    128:                                best_col = j;
                    129:                                best_gain = gain;
                    130:                        }
                    131:                }
                    132:                upd->best_gain = best_gain;
                    133:                upd->best_col = best_col;
                    134:                upd->gain = gain;
                    135:        }
                    136:        if (check_clreos) {
1.4     ! mpech     137:                struct ww_update *u;
        !           138:                int gain = 0;
        !           139:                int best_gain = 0;
1.1       deraadt   140:                int best_row;
1.4     ! mpech     141:                int simple_gain = 0;
1.1       deraadt   142:                char didit = 0;
                    143:
                    144:                /*
                    145:                 * gain is the advantage of clearing all the lines.
                    146:                 * best_gain is the advantage of clearing to eos
                    147:                 * at best_row and u->best_col.
                    148:                 * simple_gain is the advantage of using only clreol.
                    149:                 * We use g > best_gain because u->best_col can be
                    150:                 * undefined when u->best_gain is 0 so we can't use it.
                    151:                 */
                    152:                for (j = scan_bot - 1, u = wwupd + j; j >= top; j--, u--) {
1.4     ! mpech     153:                        int g = gain + u->best_gain;
1.1       deraadt   154:
                    155:                        if (g > best_gain) {
                    156:                                best_gain = g;
                    157:                                best_row = j;
                    158:                        }
                    159:                        gain += u->gain;
                    160:                        if (tt.tt_clreol != 0 && u->best_gain > 4)
                    161:                                simple_gain += u->best_gain - 4;
                    162:                }
                    163:                if (tt.tt_clreos == 0) {
                    164:                        if (gain > simple_gain && gain > 4) {
                    165:                                xxclear();
                    166:                                i = top = scan_top;
                    167:                                bot = scan_bot;
                    168:                                j = 0;
                    169:                                didit = 1;
                    170:                        }
                    171:                } else
                    172:                        if (best_gain > simple_gain && best_gain > 4) {
                    173:                                i = best_row;
                    174:                                xxclreos(i, j = wwupd[i].best_col);
                    175:                                bot = scan_bot;
                    176:                                didit = 1;
                    177:                        }
                    178:                if (didit) {
                    179:                        wwnupdclreos++;
                    180:                        wwnupdclreosline += wwnrow - i;
                    181:                        u = wwupd + i;
                    182:                        while (i < scan_bot) {
1.4     ! mpech     183:                                union ww_char *os = &wwos[i][j];
1.1       deraadt   184:
                    185:                                for (j = wwncol - j; --j >= 0;)
                    186:                                        os++->c_w = ' ';
                    187:                                wwtouched[i++] |= WWU_TOUCHED;
                    188:                                u++->best_gain = 0;
                    189:                                j = 0;
                    190:                        }
                    191:                } else
                    192:                        wwnupdclreosmiss++;
                    193:        }
                    194: simple:
                    195:        for (i = top, touched = &wwtouched[i], upd = &wwupd[i]; i < bot;
                    196:             i++, touched++, upd++) {
1.4     ! mpech     197:                union ww_char *os, *ns;
1.1       deraadt   198:                char didit;
                    199:
                    200:                if (!*touched)
                    201:                        continue;
                    202:                *touched = 0;
                    203:                wwnupdline++;
                    204:                didit = 0;
                    205:                if (tt.tt_clreol != 0 && upd->best_gain > 4) {
                    206:                        wwnupdclreol++;
                    207:                        xxclreol(i, j = upd->best_col);
                    208:                        for (os = &wwos[i][j], j = wwncol - j; --j >= 0;)
                    209:                                os++->c_w = ' ';
                    210:                        didit = 1;
                    211:                }
                    212:                ns = wwns[i];
                    213:                os = wwos[i];
                    214:                for (j = 0; j < wwncol;) {
1.4     ! mpech     215:                        char *p, *q;
1.1       deraadt   216:                        char m;
                    217:                        int c;
1.4     ! mpech     218:                        int n;
1.1       deraadt   219:                        char buf[512];                  /* > wwncol */
                    220:                        union ww_char lastc;
                    221:
                    222:                        for (; j++ < wwncol && ns++->c_w == os++->c_w;)
                    223:                                ;
                    224:                        if (j > wwncol)
                    225:                                break;
                    226:                        p = buf;
                    227:                        m = ns[-1].c_m;
                    228:                        c = j - 1;
                    229:                        os[-1] = ns[-1];
                    230:                        *p++ = ns[-1].c_c;
                    231:                        n = 5;
                    232:                        q = p;
                    233:                        while (j < wwncol && ns->c_m == m) {
                    234:                                *p++ = ns->c_c;
                    235:                                if (ns->c_w == os->c_w) {
                    236:                                        if (--n <= 0)
                    237:                                                break;
                    238:                                        os++;
                    239:                                        ns++;
                    240:                                } else {
                    241:                                        n = 5;
                    242:                                        q = p;
                    243:                                        lastc = *os;
                    244:                                        *os++ = *ns++;
                    245:                                }
                    246:                                j++;
                    247:                        }
                    248:                        n = q - buf;
                    249:                        if (!wwwrap || i != wwnrow - 1 || c + n != wwncol)
                    250:                                xxwrite(i, c, buf, n, m);
                    251:                        else if (tt.tt_inschar || tt.tt_insspace) {
                    252:                                if (n > 1) {
                    253:                                        q[-2] = q[-1];
                    254:                                        n--;
                    255:                                } else
                    256:                                        c--;
                    257:                                xxwrite(i, c, buf, n, m);
                    258:                                c += n - 1;
                    259:                                if (tt.tt_inschar)
                    260:                                        xxinschar(i, c, ns[-2].c_c,
                    261:                                                ns[-2].c_m);
                    262:                                else {
                    263:                                        xxinsspace(i, c);
                    264:                                        xxwrite(i, c, &ns[-2].c_c, 1,
                    265:                                                ns[-2].c_m);
                    266:                                }
                    267:                        } else {
                    268:                                if (--n)
                    269:                                        xxwrite(i, c, buf, n, m);
                    270:                                os[-1] = lastc;
                    271:                                *touched = WWU_TOUCHED;
                    272:                        }
                    273:                        didit = 1;
                    274:                }
                    275:                if (!didit)
                    276:                        wwnupdmiss++;
                    277:        }
                    278: }