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