GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/less/less/../brac.c Lines: 0 23 0.0 %
Date: 2017-11-13 Branches: 0 24 0.0 %

Line Branch Exec Source
1
/*
2
 * Copyright (C) 1984-2012  Mark Nudelman
3
 * Modified for use with illumos by Garrett D'Amore.
4
 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
5
 *
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.
8
 *
9
 * For more information, see the README file.
10
 */
11
12
/*
13
 * Routines to perform bracket matching functions.
14
 */
15
16
#include "less.h"
17
#include "position.h"
18
19
/*
20
 * Try to match the n-th open bracket
21
 *  which appears in the top displayed line (forwdir),
22
 * or the n-th close bracket
23
 *  which appears in the bottom displayed line (!forwdir).
24
 * The characters which serve as "open bracket" and
25
 * "close bracket" are given.
26
 */
27
void
28
match_brac(int obrac, int cbrac, int forwdir, int n)
29
{
30
	int c;
31
	int nest;
32
	off_t pos;
33
	int (*chget)(void);
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);
41
	if (pos == -1 || ch_seek(pos)) {
42
		if (forwdir)
43
			error("Nothing in top line", NULL);
44
		else
45
			error("Nothing in bottom line", NULL);
46
		return;
47
	}
48
49
	/*
50
	 * Look thru the line to find the open bracket to match.
51
	 */
52
	do {
53
		if ((c = ch_forw_get()) == '\n' || c == EOI) {
54
			if (forwdir)
55
				error("No bracket in top line", NULL);
56
			else
57
				error("No bracket in bottom line", NULL);
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;
76
	while ((c = (*chget)()) != EOI) {
77
		if (c == obrac) {
78
			nest++;
79
		} else if (c == cbrac && --nest < 0) {
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
	}
89
	error("No matching bracket", NULL);
90
}