etc/services - sync with NetBSD-8
[minix.git] / external / bsd / less / dist / brac.c
blobd741157b5ddaa08c39123965ee25881f99ca9027
1 /* $NetBSD: brac.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
3 /*
4 * Copyright (C) 1984-2012 Mark Nudelman
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.
9 * For more information, see the README file.
14 * Routines to perform bracket matching functions.
17 #include "less.h"
18 #include "position.h"
21 * Try to match the n-th open bracket
22 * which appears in the top displayed line (forwdir),
23 * or the n-th close bracket
24 * which appears in the bottom displayed line (!forwdir).
25 * The characters which serve as "open bracket" and
26 * "close bracket" are given.
28 public void
29 match_brac(obrac, cbrac, forwdir, n)
30 register int obrac;
31 register int cbrac;
32 int forwdir;
33 int n;
35 register int c;
36 register int nest;
37 POSITION pos;
38 int (*chget) __P((void));
41 * Seek to the line containing the open bracket.
42 * This is either the top or bottom line on the screen,
43 * depending on the type of bracket.
45 pos = position((forwdir) ? TOP : BOTTOM);
46 if (pos == NULL_POSITION || ch_seek(pos))
48 if (forwdir)
49 error("Nothing in top line", NULL_PARG);
50 else
51 error("Nothing in bottom line", NULL_PARG);
52 return;
56 * Look thru the line to find the open bracket to match.
60 if ((c = ch_forw_get()) == '\n' || c == EOI)
62 if (forwdir)
63 error("No bracket in top line", NULL_PARG);
64 else
65 error("No bracket in bottom line", NULL_PARG);
66 return;
68 } while (c != obrac || --n > 0);
71 * Position the file just "after" the open bracket
72 * (in the direction in which we will be searching).
73 * If searching forward, we are already after the bracket.
74 * If searching backward, skip back over the open bracket.
76 if (!forwdir)
77 (void) ch_back_get();
80 * Search the file for the matching bracket.
82 chget = (forwdir) ? ch_forw_get : ch_back_get;
83 nest = 0;
84 while ((c = (*chget)()) != EOI)
86 if (c == obrac)
87 nest++;
88 else if (c == cbrac && --nest < 0)
91 * Found the matching bracket.
92 * If searching backward, put it on the top line.
93 * If searching forward, put it on the bottom line.
95 jump_line_loc(ch_tell(), forwdir ? -1 : 1);
96 return;
99 error("No matching bracket", NULL_PARG);