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

Annotation of src/usr.bin/less/input.c, Revision 1.9

1.1       etheisen    1: /*
1.7       shadchin    2:  * Copyright (C) 1984-2012  Mark Nudelman
1.1       etheisen    3:  *
1.4       millert     4:  * You may distribute under the terms of either the GNU General Public
                      5:  * License or the Less License, as specified in the README file.
1.1       etheisen    6:  *
1.7       shadchin    7:  * For more information, see the README file.
1.1       etheisen    8:  */
1.8       nicm        9: /*
                     10:  * Modified for use with illumos.
                     11:  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
                     12:  */
1.1       etheisen   13:
                     14: /*
1.8       nicm       15:  * High level routines dealing with getting lines of input
1.1       etheisen   16:  * from the file being viewed.
                     17:  *
                     18:  * When we speak of "lines" here, we mean PRINTABLE lines;
                     19:  * lines processed with respect to the screen width.
                     20:  * We use the term "raw line" to refer to lines simply
                     21:  * delimited by newlines; not processed with respect to screen width.
                     22:  */
                     23:
                     24: #include "less.h"
                     25:
                     26: extern int squeeze;
                     27: extern int chopline;
1.4       millert    28: extern int hshift;
                     29: extern int quit_if_one_screen;
1.6       millert    30: extern volatile sig_atomic_t sigs;
1.4       millert    31: extern int ignore_eoi;
1.5       shadchin   32: extern int status_col;
1.8       nicm       33: extern off_t start_attnpos;
                     34: extern off_t end_attnpos;
1.1       etheisen   35: extern int hilite_search;
                     36: extern int size_linebuf;
                     37:
                     38: /*
                     39:  * Get the next line.
                     40:  * A "current" position is passed and a "new" position is returned.
                     41:  * The current position is the position of the first character of
                     42:  * a line.  The new position is the position of the first character
                     43:  * of the NEXT line.  The line obtained is the line starting at curr_pos.
                     44:  */
1.8       nicm       45: off_t
                     46: forw_line(off_t curr_pos)
1.1       etheisen   47: {
1.8       nicm       48:        off_t base_pos;
                     49:        off_t new_pos;
1.9     ! tedu       50:        int c;
1.1       etheisen   51:        int blankline;
                     52:        int endline;
1.5       shadchin   53:        int backchars;
1.1       etheisen   54:
1.5       shadchin   55: get_forw_line:
1.8       nicm       56:        if (curr_pos == -1) {
1.1       etheisen   57:                null_line();
1.8       nicm       58:                return (-1);
1.1       etheisen   59:        }
1.5       shadchin   60:        if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
1.4       millert    61:                /*
                     62:                 * If we are ignoring EOI (command F), only prepare
                     63:                 * one line ahead, to avoid getting stuck waiting for
                     64:                 * slow data without displaying the data we already have.
                     65:                 * If we're not ignoring EOI, we *could* do the same, but
                     66:                 * for efficiency we prepare several lines ahead at once.
                     67:                 */
1.8       nicm       68:                prep_hilite(curr_pos, curr_pos + 3*size_linebuf,
                     69:                    ignore_eoi ? 1 : -1);
                     70:        if (ch_seek(curr_pos)) {
1.1       etheisen   71:                null_line();
1.8       nicm       72:                return (-1);
1.1       etheisen   73:        }
                     74:
1.5       shadchin   75:        /*
                     76:         * Step back to the beginning of the line.
                     77:         */
                     78:        base_pos = curr_pos;
1.8       nicm       79:        for (;;) {
                     80:                if (ABORT_SIGS()) {
1.5       shadchin   81:                        null_line();
1.8       nicm       82:                        return (-1);
1.5       shadchin   83:                }
                     84:                c = ch_back_get();
                     85:                if (c == EOI)
                     86:                        break;
1.8       nicm       87:                if (c == '\n') {
1.5       shadchin   88:                        (void) ch_forw_get();
                     89:                        break;
                     90:                }
                     91:                --base_pos;
                     92:        }
                     93:
                     94:        /*
                     95:         * Read forward again to the position we should start at.
                     96:         */
1.8       nicm       97:        prewind();
1.5       shadchin   98:        plinenum(base_pos);
                     99:        (void) ch_seek(base_pos);
                    100:        new_pos = base_pos;
1.8       nicm      101:        while (new_pos < curr_pos) {
                    102:                if (ABORT_SIGS()) {
1.5       shadchin  103:                        null_line();
1.8       nicm      104:                        return (-1);
1.5       shadchin  105:                }
                    106:                c = ch_forw_get();
                    107:                backchars = pappend(c, new_pos);
                    108:                new_pos++;
1.8       nicm      109:                if (backchars > 0) {
1.5       shadchin  110:                        pshift_all();
                    111:                        new_pos -= backchars;
                    112:                        while (--backchars >= 0)
                    113:                                (void) ch_back_get();
                    114:                }
                    115:        }
                    116:        (void) pflushmbc();
                    117:        pshift_all();
1.1       etheisen  118:
1.5       shadchin  119:        /*
                    120:         * Read the first character to display.
                    121:         */
1.1       etheisen  122:        c = ch_forw_get();
1.8       nicm      123:        if (c == EOI) {
1.1       etheisen  124:                null_line();
1.8       nicm      125:                return (-1);
1.1       etheisen  126:        }
                    127:        blankline = (c == '\n' || c == '\r');
                    128:
1.5       shadchin  129:        /*
                    130:         * Read each character in the line and append to the line buffer.
                    131:         */
1.8       nicm      132:        for (;;) {
                    133:                if (ABORT_SIGS()) {
1.1       etheisen  134:                        null_line();
1.8       nicm      135:                        return (-1);
1.1       etheisen  136:                }
1.8       nicm      137:                if (c == '\n' || c == EOI) {
1.1       etheisen  138:                        /*
                    139:                         * End of the line.
                    140:                         */
1.5       shadchin  141:                        backchars = pflushmbc();
1.1       etheisen  142:                        new_pos = ch_tell();
1.8       nicm      143:                        if (backchars > 0 && !chopline && hshift == 0) {
1.5       shadchin  144:                                new_pos -= backchars + 1;
                    145:                                endline = FALSE;
                    146:                        } else
                    147:                                endline = TRUE;
1.1       etheisen  148:                        break;
                    149:                }
1.5       shadchin  150:                if (c != '\r')
                    151:                        blankline = 0;
1.1       etheisen  152:
                    153:                /*
                    154:                 * Append the char to the line and get the next char.
                    155:                 */
1.5       shadchin  156:                backchars = pappend(c, ch_tell()-1);
1.8       nicm      157:                if (backchars > 0) {
1.1       etheisen  158:                        /*
                    159:                         * The char won't fit in the line; the line
                    160:                         * is too long to print in the screen width.
                    161:                         * End the line here.
                    162:                         */
1.8       nicm      163:                        if (chopline || hshift > 0) {
                    164:                                do {
                    165:                                        if (ABORT_SIGS()) {
1.5       shadchin  166:                                                null_line();
1.8       nicm      167:                                                return (-1);
1.5       shadchin  168:                                        }
1.1       etheisen  169:                                        c = ch_forw_get();
                    170:                                } while (c != '\n' && c != EOI);
                    171:                                new_pos = ch_tell();
1.4       millert   172:                                endline = TRUE;
                    173:                                quit_if_one_screen = FALSE;
1.8       nicm      174:                        } else {
1.5       shadchin  175:                                new_pos = ch_tell() - backchars;
1.4       millert   176:                                endline = FALSE;
1.1       etheisen  177:                        }
                    178:                        break;
                    179:                }
                    180:                c = ch_forw_get();
                    181:        }
1.5       shadchin  182:
                    183:        pdone(endline, 1);
                    184:
1.8       nicm      185:        if (is_filtered(base_pos)) {
1.5       shadchin  186:                /*
                    187:                 * We don't want to display this line.
                    188:                 * Get the next line.
                    189:                 */
                    190:                curr_pos = new_pos;
                    191:                goto get_forw_line;
                    192:        }
                    193:
                    194:        if (status_col && is_hilited(base_pos, ch_tell()-1, 1, NULL))
                    195:                set_status_col('*');
1.1       etheisen  196:
1.8       nicm      197:        if (squeeze && blankline) {
1.1       etheisen  198:                /*
                    199:                 * This line is blank.
                    200:                 * Skip down to the last contiguous blank line
                    201:                 * and pretend it is the one which we are returning.
                    202:                 */
                    203:                while ((c = ch_forw_get()) == '\n' || c == '\r')
1.8       nicm      204:                        if (ABORT_SIGS()) {
1.1       etheisen  205:                                null_line();
1.8       nicm      206:                                return (-1);
1.1       etheisen  207:                        }
                    208:                if (c != EOI)
                    209:                        (void) ch_back_get();
                    210:                new_pos = ch_tell();
                    211:        }
                    212:
                    213:        return (new_pos);
                    214: }
                    215:
                    216: /*
                    217:  * Get the previous line.
                    218:  * A "current" position is passed and a "new" position is returned.
                    219:  * The current position is the position of the first character of
                    220:  * a line.  The new position is the position of the first character
                    221:  * of the PREVIOUS line.  The line obtained is the one starting at new_pos.
                    222:  */
1.8       nicm      223: off_t
                    224: back_line(off_t curr_pos)
1.1       etheisen  225: {
1.8       nicm      226:        off_t new_pos, begin_new_pos, base_pos;
1.1       etheisen  227:        int c;
                    228:        int endline;
1.5       shadchin  229:        int backchars;
1.1       etheisen  230:
1.5       shadchin  231: get_back_line:
1.8       nicm      232:        if (curr_pos == -1 || curr_pos <= ch_zero()) {
1.1       etheisen  233:                null_line();
1.8       nicm      234:                return (-1);
1.1       etheisen  235:        }
1.5       shadchin  236:        if (hilite_search == OPT_ONPLUS || is_filtering() || status_col)
1.8       nicm      237:                prep_hilite((curr_pos < 3*size_linebuf) ?
                    238:                    0 : curr_pos - 3*size_linebuf, curr_pos, -1);
                    239:        if (ch_seek(curr_pos-1)) {
1.1       etheisen  240:                null_line();
1.8       nicm      241:                return (-1);
1.1       etheisen  242:        }
                    243:
1.8       nicm      244:        if (squeeze) {
1.1       etheisen  245:                /*
                    246:                 * Find out if the "current" line was blank.
                    247:                 */
1.8       nicm      248:                (void) ch_forw_get();   /* Skip the newline */
                    249:                c = ch_forw_get();      /* First char of "current" line */
                    250:                (void) ch_back_get();   /* Restore our position */
1.1       etheisen  251:                (void) ch_back_get();
                    252:
1.8       nicm      253:                if (c == '\n' || c == '\r') {
1.1       etheisen  254:                        /*
                    255:                         * The "current" line was blank.
                    256:                         * Skip over any preceding blank lines,
                    257:                         * since we skipped them in forw_line().
                    258:                         */
                    259:                        while ((c = ch_back_get()) == '\n' || c == '\r')
1.8       nicm      260:                                if (ABORT_SIGS()) {
1.1       etheisen  261:                                        null_line();
1.8       nicm      262:                                        return (-1);
1.1       etheisen  263:                                }
1.8       nicm      264:                        if (c == EOI) {
1.1       etheisen  265:                                null_line();
1.8       nicm      266:                                return (-1);
1.1       etheisen  267:                        }
                    268:                        (void) ch_forw_get();
                    269:                }
                    270:        }
                    271:
                    272:        /*
                    273:         * Scan backwards until we hit the beginning of the line.
                    274:         */
1.8       nicm      275:        for (;;) {
                    276:                if (ABORT_SIGS()) {
1.1       etheisen  277:                        null_line();
1.8       nicm      278:                        return (-1);
1.1       etheisen  279:                }
                    280:                c = ch_back_get();
1.8       nicm      281:                if (c == '\n') {
1.1       etheisen  282:                        /*
                    283:                         * This is the newline ending the previous line.
                    284:                         * We have hit the beginning of the line.
                    285:                         */
1.5       shadchin  286:                        base_pos = ch_tell() + 1;
1.1       etheisen  287:                        break;
                    288:                }
1.8       nicm      289:                if (c == EOI) {
1.1       etheisen  290:                        /*
                    291:                         * We have hit the beginning of the file.
                    292:                         * This must be the first line in the file.
                    293:                         * This must, of course, be the beginning of the line.
                    294:                         */
1.5       shadchin  295:                        base_pos = ch_tell();
1.1       etheisen  296:                        break;
                    297:                }
                    298:        }
                    299:
                    300:        /*
                    301:         * Now scan forwards from the beginning of this line.
                    302:         * We keep discarding "printable lines" (based on screen width)
                    303:         * until we reach the curr_pos.
                    304:         *
                    305:         * {{ This algorithm is pretty inefficient if the lines
1.8       nicm      306:         *    are much longer than the screen width,
1.1       etheisen  307:         *    but I don't know of any better way. }}
                    308:         */
1.5       shadchin  309:        new_pos = base_pos;
1.8       nicm      310:        if (ch_seek(new_pos)) {
1.1       etheisen  311:                null_line();
1.8       nicm      312:                return (-1);
1.1       etheisen  313:        }
1.4       millert   314:        endline = FALSE;
1.5       shadchin  315:        prewind();
                    316:        plinenum(new_pos);
1.8       nicm      317: loop:
1.1       etheisen  318:        begin_new_pos = new_pos;
                    319:        (void) ch_seek(new_pos);
                    320:
1.8       nicm      321:        do {
1.1       etheisen  322:                c = ch_forw_get();
1.8       nicm      323:                if (c == EOI || ABORT_SIGS()) {
1.1       etheisen  324:                        null_line();
1.8       nicm      325:                        return (-1);
1.1       etheisen  326:                }
                    327:                new_pos++;
1.8       nicm      328:                if (c == '\n') {
1.5       shadchin  329:                        backchars = pflushmbc();
1.8       nicm      330:                        if (backchars > 0 && !chopline && hshift == 0) {
1.5       shadchin  331:                                backchars++;
                    332:                                goto shift;
                    333:                        }
1.4       millert   334:                        endline = TRUE;
1.1       etheisen  335:                        break;
                    336:                }
1.5       shadchin  337:                backchars = pappend(c, ch_tell()-1);
1.8       nicm      338:                if (backchars > 0) {
1.1       etheisen  339:                        /*
                    340:                         * Got a full printable line, but we haven't
                    341:                         * reached our curr_pos yet.  Discard the line
                    342:                         * and start a new one.
                    343:                         */
1.8       nicm      344:                        if (chopline || hshift > 0) {
1.4       millert   345:                                endline = TRUE;
                    346:                                quit_if_one_screen = FALSE;
1.1       etheisen  347:                                break;
                    348:                        }
1.5       shadchin  349:                shift:
                    350:                        pshift_all();
1.8       nicm      351:                        while (backchars-- > 0) {
1.5       shadchin  352:                                (void) ch_back_get();
                    353:                                new_pos--;
                    354:                        }
1.1       etheisen  355:                        goto loop;
                    356:                }
                    357:        } while (new_pos < curr_pos);
                    358:
1.5       shadchin  359:        pdone(endline, 0);
                    360:
1.8       nicm      361:        if (is_filtered(base_pos)) {
1.5       shadchin  362:                /*
                    363:                 * We don't want to display this line.
                    364:                 * Get the previous line.
                    365:                 */
                    366:                curr_pos = begin_new_pos;
                    367:                goto get_back_line;
                    368:        }
                    369:
1.8       nicm      370:        if (status_col && curr_pos > 0 &&
                    371:            is_hilited(base_pos, curr_pos-1, 1, NULL))
1.5       shadchin  372:                set_status_col('*');
1.1       etheisen  373:
                    374:        return (begin_new_pos);
1.4       millert   375: }
                    376:
                    377: /*
                    378:  * Set attnpos.
                    379:  */
1.8       nicm      380: void
                    381: set_attnpos(off_t pos)
1.4       millert   382: {
                    383:        int c;
                    384:
1.8       nicm      385:        if (pos != -1) {
1.4       millert   386:                if (ch_seek(pos))
                    387:                        return;
1.8       nicm      388:                for (;;) {
1.4       millert   389:                        c = ch_forw_get();
                    390:                        if (c == EOI)
                    391:                                return;
                    392:                        if (c != '\n' && c != '\r')
                    393:                                break;
                    394:                        pos++;
                    395:                }
                    396:        }
                    397:        start_attnpos = pos;
1.8       nicm      398:        for (;;) {
1.4       millert   399:                c = ch_forw_get();
                    400:                pos++;
                    401:                if (c == EOI || c == '\n' || c == '\r')
                    402:                        break;
                    403:        }
                    404:        end_attnpos = pos;
1.1       etheisen  405: }