1 /* $NetBSD: getc.c,v 1.1.1.2 2008/05/18 14:31:40 aymeric Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "Id: getc.c,v 10.12 2001/06/25 15:19:30 skimo Exp (Berkeley) Date: 2001/06/25 15:19:30";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
28 #include "../common/common.h"
32 * Character stream routines --
33 * These routines return the file a character at a time. There are two
34 * special cases. First, the end of a line, end of a file, start of a
35 * file and empty lines are returned as special cases, and no character
36 * is returned. Second, empty lines include lines that have only white
37 * space in them, because the vi search functions don't care about white
38 * space, and this makes it easier for them to be consistent.
43 * Initialize character stream routines.
45 * PUBLIC: int cs_init __P((SCR *, VCS *));
48 cs_init(SCR
*sp
, VCS
*csp
)
52 if (db_eget(sp
, csp
->cs_lno
, &csp
->cs_bp
, &csp
->cs_len
, &isempty
)) {
54 msgq(sp
, M_BERR
, "177|Empty file");
57 if (csp
->cs_len
== 0 || v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
59 csp
->cs_flags
= CS_EMP
;
62 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
];
69 * Retrieve the next character.
71 * PUBLIC: int cs_next __P((SCR *, VCS *));
74 cs_next(SCR
*sp
, VCS
*csp
)
78 switch (csp
->cs_flags
) {
79 case CS_EMP
: /* EMP; get next line. */
80 case CS_EOL
: /* EOL; get next line. */
81 if (db_get(sp
, ++csp
->cs_lno
, 0, &p
, &csp
->cs_len
)) {
83 csp
->cs_flags
= CS_EOF
;
86 if (csp
->cs_len
== 0 ||
87 v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
89 csp
->cs_flags
= CS_EMP
;
92 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
= 0];
97 if (csp
->cs_cno
== csp
->cs_len
- 1)
98 csp
->cs_flags
= CS_EOL
;
100 csp
->cs_ch
= csp
->cs_bp
[++csp
->cs_cno
];
102 case CS_EOF
: /* EOF. */
113 * If on a space, eat forward until something other than a
114 * whitespace character.
117 * Semantics of checking the current character were coded for the fword()
118 * function -- once the other word routines are converted, they may have
121 * PUBLIC: int cs_fspace __P((SCR *, VCS *));
124 cs_fspace(SCR
*sp
, VCS
*csp
)
126 if (csp
->cs_flags
!= 0 || !isblank(csp
->cs_ch
))
129 if (cs_next(sp
, csp
))
131 if (csp
->cs_flags
!= 0 || !isblank(csp
->cs_ch
))
139 * Eat forward to the next non-whitespace character.
141 * PUBLIC: int cs_fblank __P((SCR *, VCS *));
144 cs_fblank(SCR
*sp
, VCS
*csp
)
147 if (cs_next(sp
, csp
))
149 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
150 (csp
->cs_flags
== 0 && isblank(csp
->cs_ch
)))
159 * Retrieve the previous character.
161 * PUBLIC: int cs_prev __P((SCR *, VCS *));
164 cs_prev(SCR
*sp
, VCS
*csp
)
166 switch (csp
->cs_flags
) {
167 case CS_EMP
: /* EMP; get previous line. */
168 case CS_EOL
: /* EOL; get previous line. */
169 if (csp
->cs_lno
== 1) { /* SOF. */
170 csp
->cs_flags
= CS_SOF
;
173 if (db_get(sp
, /* The line should exist. */
174 --csp
->cs_lno
, DBG_FATAL
, &csp
->cs_bp
, &csp
->cs_len
)) {
178 if (csp
->cs_len
== 0 || v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
180 csp
->cs_flags
= CS_EMP
;
183 csp
->cs_cno
= csp
->cs_len
- 1;
184 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
];
187 case CS_EOF
: /* EOF: get previous char. */
189 if (csp
->cs_cno
== 0)
190 if (csp
->cs_lno
== 1)
191 csp
->cs_flags
= CS_SOF
;
193 csp
->cs_flags
= CS_EOL
;
195 csp
->cs_ch
= csp
->cs_bp
[--csp
->cs_cno
];
197 case CS_SOF
: /* SOF. */
208 * Eat backward to the next non-whitespace character.
210 * PUBLIC: int cs_bblank __P((SCR *, VCS *));
213 cs_bblank(SCR
*sp
, VCS
*csp
)
216 if (cs_prev(sp
, csp
))
218 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
219 (csp
->cs_flags
== 0 && isblank(csp
->cs_ch
)))