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

Annotation of src/usr.bin/less/brac.c, Revision 1.1.1.1

1.1       etheisen    1: /*
                      2:  * Copyright (c) 1984,1985,1989,1994,1995  Mark Nudelman
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice in the documentation and/or other materials provided with
                     12:  *    the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
                     15:  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     16:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     17:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
                     18:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     19:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
                     20:  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
                     21:  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
                     23:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
                     24:  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
                     27:
                     28: /*
                     29:  * Routines to perform bracket matching functions.
                     30:  */
                     31:
                     32: #include "less.h"
                     33: #include "position.h"
                     34:
                     35: /*
                     36:  * Try to match the n-th open bracket
                     37:  *  which appears in the top displayed line (forwdir),
                     38:  * or the n-th close bracket
                     39:  *  which appears in the bottom displayed line (!forwdir).
                     40:  * The characters which serve as "open bracket" and
                     41:  * "close bracket" are given.
                     42:  */
                     43:        public void
                     44: match_brac(obrac, cbrac, forwdir, n)
                     45:        register int obrac;
                     46:        register int cbrac;
                     47:        int forwdir;
                     48:        int n;
                     49: {
                     50:        register int c;
                     51:        register int nest;
                     52:        POSITION pos;
                     53:        int (*chget)();
                     54:
                     55:        extern int ch_forw_get(), ch_back_get();
                     56:
                     57:        /*
                     58:         * Seek to the line containing the open bracket.
                     59:         * This is either the top or bottom line on the screen,
                     60:         * depending on the type of bracket.
                     61:         */
                     62:        pos = position((forwdir) ? TOP : BOTTOM);
                     63:        if (pos == NULL_POSITION || ch_seek(pos))
                     64:        {
                     65:                if (forwdir)
                     66:                        error("Nothing in top line", NULL_PARG);
                     67:                else
                     68:                        error("Nothing in bottom line", NULL_PARG);
                     69:                return;
                     70:        }
                     71:
                     72:        /*
                     73:         * Look thru the line to find the open bracket to match.
                     74:         */
                     75:        do
                     76:        {
                     77:                if ((c = ch_forw_get()) == '\n' || c == EOI)
                     78:                {
                     79:                        if (forwdir)
                     80:                                error("No bracket in top line", NULL_PARG);
                     81:                        else
                     82:                                error("No bracket in bottom line", NULL_PARG);
                     83:                        return;
                     84:                }
                     85:        } while (c != obrac || --n > 0);
                     86:
                     87:        /*
                     88:         * Position the file just "after" the open bracket
                     89:         * (in the direction in which we will be searching).
                     90:         * If searching forward, we are already after the bracket.
                     91:         * If searching backward, skip back over the open bracket.
                     92:         */
                     93:        if (!forwdir)
                     94:                (void) ch_back_get();
                     95:
                     96:        /*
                     97:         * Search the file for the matching bracket.
                     98:         */
                     99:        chget = (forwdir) ? ch_forw_get : ch_back_get;
                    100:        nest = 0;
                    101:        while ((c = (*chget)()) != EOI)
                    102:        {
                    103:                if (c == obrac)
                    104:                        nest++;
                    105:                else if (c == cbrac && --nest < 0)
                    106:                {
                    107:                        /*
                    108:                         * Found the matching bracket.
                    109:                         * If searching backward, put it on the top line.
                    110:                         * If searching forward, put it on the bottom line.
                    111:                         */
                    112:                        jump_line_loc(ch_tell(), forwdir ? -1 : 1);
                    113:                        return;
                    114:                }
                    115:        }
                    116:        error("No matching bracket", NULL_PARG);
                    117: }