Annotation of src/usr.bin/less/brac.c, Revision 1.7
1.1 etheisen 1: /*
1.6 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.6 shadchin 7: * For more information, see the README file.
1.1 etheisen 8: */
1.7 ! nicm 9: /*
! 10: * Modified for use with illumos.
! 11: * Copyright 2014 Garrett D'Amore <garrett@damore.org>
! 12: */
1.1 etheisen 13:
14: /*
15: * Routines to perform bracket matching functions.
16: */
17:
18: #include "less.h"
19: #include "position.h"
20:
21: /*
1.7 ! nicm 22: * Try to match the n-th open bracket
1.1 etheisen 23: * which appears in the top displayed line (forwdir),
1.7 ! nicm 24: * or the n-th close bracket
1.1 etheisen 25: * which appears in the bottom displayed line (!forwdir).
1.7 ! nicm 26: * The characters which serve as "open bracket" and
1.1 etheisen 27: * "close bracket" are given.
28: */
1.7 ! nicm 29: void
! 30: match_brac(int obrac, int cbrac, int forwdir, int n)
1.1 etheisen 31: {
1.7 ! nicm 32: int c;
! 33: int nest;
! 34: off_t pos;
! 35: int (*chget)(void);
1.1 etheisen 36:
37: /*
38: * Seek to the line containing the open bracket.
39: * This is either the top or bottom line on the screen,
40: * depending on the type of bracket.
41: */
42: pos = position((forwdir) ? TOP : BOTTOM);
1.7 ! nicm 43: if (pos == -1 || ch_seek(pos)) {
1.1 etheisen 44: if (forwdir)
45: error("Nothing in top line", NULL_PARG);
46: else
47: error("Nothing in bottom line", NULL_PARG);
48: return;
49: }
50:
51: /*
52: * Look thru the line to find the open bracket to match.
53: */
1.7 ! nicm 54: do {
! 55: if ((c = ch_forw_get()) == '\n' || c == EOI) {
1.1 etheisen 56: if (forwdir)
57: error("No bracket in top line", NULL_PARG);
58: else
59: error("No bracket in bottom line", NULL_PARG);
60: return;
61: }
62: } while (c != obrac || --n > 0);
63:
64: /*
65: * Position the file just "after" the open bracket
66: * (in the direction in which we will be searching).
67: * If searching forward, we are already after the bracket.
68: * If searching backward, skip back over the open bracket.
69: */
70: if (!forwdir)
71: (void) ch_back_get();
72:
73: /*
74: * Search the file for the matching bracket.
75: */
76: chget = (forwdir) ? ch_forw_get : ch_back_get;
77: nest = 0;
1.7 ! nicm 78: while ((c = (*chget)()) != EOI) {
! 79: if (c == obrac) {
1.1 etheisen 80: nest++;
1.7 ! nicm 81: } else if (c == cbrac && --nest < 0) {
1.1 etheisen 82: /*
83: * Found the matching bracket.
84: * If searching backward, put it on the top line.
85: * If searching forward, put it on the bottom line.
86: */
87: jump_line_loc(ch_tell(), forwdir ? -1 : 1);
88: return;
89: }
90: }
91: error("No matching bracket", NULL_PARG);
92: }