xref: /minix3/external/bsd/less/dist/brac.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: brac.c,v 1.4 2013/09/04 19:44:21 tron Exp $	*/
2f7cf2976SLionel Sambuc 
3f7cf2976SLionel Sambuc /*
4*84d9c625SLionel Sambuc  * Copyright (C) 1984-2012  Mark Nudelman
5f7cf2976SLionel Sambuc  *
6f7cf2976SLionel Sambuc  * You may distribute under the terms of either the GNU General Public
7f7cf2976SLionel Sambuc  * License or the Less License, as specified in the README file.
8f7cf2976SLionel Sambuc  *
9*84d9c625SLionel Sambuc  * For more information, see the README file.
10f7cf2976SLionel Sambuc  */
11f7cf2976SLionel Sambuc 
12f7cf2976SLionel Sambuc 
13f7cf2976SLionel Sambuc /*
14f7cf2976SLionel Sambuc  * Routines to perform bracket matching functions.
15f7cf2976SLionel Sambuc  */
16f7cf2976SLionel Sambuc 
17f7cf2976SLionel Sambuc #include "less.h"
18f7cf2976SLionel Sambuc #include "position.h"
19f7cf2976SLionel Sambuc 
20f7cf2976SLionel Sambuc /*
21f7cf2976SLionel Sambuc  * Try to match the n-th open bracket
22f7cf2976SLionel Sambuc  *  which appears in the top displayed line (forwdir),
23f7cf2976SLionel Sambuc  * or the n-th close bracket
24f7cf2976SLionel Sambuc  *  which appears in the bottom displayed line (!forwdir).
25f7cf2976SLionel Sambuc  * The characters which serve as "open bracket" and
26f7cf2976SLionel Sambuc  * "close bracket" are given.
27f7cf2976SLionel Sambuc  */
28f7cf2976SLionel Sambuc 	public void
match_brac(obrac,cbrac,forwdir,n)29f7cf2976SLionel Sambuc match_brac(obrac, cbrac, forwdir, n)
30f7cf2976SLionel Sambuc 	register int obrac;
31f7cf2976SLionel Sambuc 	register int cbrac;
32f7cf2976SLionel Sambuc 	int forwdir;
33f7cf2976SLionel Sambuc 	int n;
34f7cf2976SLionel Sambuc {
35f7cf2976SLionel Sambuc 	register int c;
36f7cf2976SLionel Sambuc 	register int nest;
37f7cf2976SLionel Sambuc 	POSITION pos;
38f7cf2976SLionel Sambuc 	int (*chget) __P((void));
39f7cf2976SLionel Sambuc 
40f7cf2976SLionel Sambuc 	/*
41f7cf2976SLionel Sambuc 	 * Seek to the line containing the open bracket.
42f7cf2976SLionel Sambuc 	 * This is either the top or bottom line on the screen,
43f7cf2976SLionel Sambuc 	 * depending on the type of bracket.
44f7cf2976SLionel Sambuc 	 */
45f7cf2976SLionel Sambuc 	pos = position((forwdir) ? TOP : BOTTOM);
46f7cf2976SLionel Sambuc 	if (pos == NULL_POSITION || ch_seek(pos))
47f7cf2976SLionel Sambuc 	{
48f7cf2976SLionel Sambuc 		if (forwdir)
49f7cf2976SLionel Sambuc 			error("Nothing in top line", NULL_PARG);
50f7cf2976SLionel Sambuc 		else
51f7cf2976SLionel Sambuc 			error("Nothing in bottom line", NULL_PARG);
52f7cf2976SLionel Sambuc 		return;
53f7cf2976SLionel Sambuc 	}
54f7cf2976SLionel Sambuc 
55f7cf2976SLionel Sambuc 	/*
56f7cf2976SLionel Sambuc 	 * Look thru the line to find the open bracket to match.
57f7cf2976SLionel Sambuc 	 */
58f7cf2976SLionel Sambuc 	do
59f7cf2976SLionel Sambuc 	{
60f7cf2976SLionel Sambuc 		if ((c = ch_forw_get()) == '\n' || c == EOI)
61f7cf2976SLionel Sambuc 		{
62f7cf2976SLionel Sambuc 			if (forwdir)
63f7cf2976SLionel Sambuc 				error("No bracket in top line", NULL_PARG);
64f7cf2976SLionel Sambuc 			else
65f7cf2976SLionel Sambuc 				error("No bracket in bottom line", NULL_PARG);
66f7cf2976SLionel Sambuc 			return;
67f7cf2976SLionel Sambuc 		}
68f7cf2976SLionel Sambuc 	} while (c != obrac || --n > 0);
69f7cf2976SLionel Sambuc 
70f7cf2976SLionel Sambuc 	/*
71f7cf2976SLionel Sambuc 	 * Position the file just "after" the open bracket
72f7cf2976SLionel Sambuc 	 * (in the direction in which we will be searching).
73f7cf2976SLionel Sambuc 	 * If searching forward, we are already after the bracket.
74f7cf2976SLionel Sambuc 	 * If searching backward, skip back over the open bracket.
75f7cf2976SLionel Sambuc 	 */
76f7cf2976SLionel Sambuc 	if (!forwdir)
77f7cf2976SLionel Sambuc 		(void) ch_back_get();
78f7cf2976SLionel Sambuc 
79f7cf2976SLionel Sambuc 	/*
80f7cf2976SLionel Sambuc 	 * Search the file for the matching bracket.
81f7cf2976SLionel Sambuc 	 */
82f7cf2976SLionel Sambuc 	chget = (forwdir) ? ch_forw_get : ch_back_get;
83f7cf2976SLionel Sambuc 	nest = 0;
84f7cf2976SLionel Sambuc 	while ((c = (*chget)()) != EOI)
85f7cf2976SLionel Sambuc 	{
86f7cf2976SLionel Sambuc 		if (c == obrac)
87f7cf2976SLionel Sambuc 			nest++;
88f7cf2976SLionel Sambuc 		else if (c == cbrac && --nest < 0)
89f7cf2976SLionel Sambuc 		{
90f7cf2976SLionel Sambuc 			/*
91f7cf2976SLionel Sambuc 			 * Found the matching bracket.
92f7cf2976SLionel Sambuc 			 * If searching backward, put it on the top line.
93f7cf2976SLionel Sambuc 			 * If searching forward, put it on the bottom line.
94f7cf2976SLionel Sambuc 			 */
95f7cf2976SLionel Sambuc 			jump_line_loc(ch_tell(), forwdir ? -1 : 1);
96f7cf2976SLionel Sambuc 			return;
97f7cf2976SLionel Sambuc 		}
98f7cf2976SLionel Sambuc 	}
99f7cf2976SLionel Sambuc 	error("No matching bracket", NULL_PARG);
100f7cf2976SLionel Sambuc }
101