2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
9 * @(#)getc.c 10.10 (Berkeley) 3/6/96
10 * $DragonFly: src/contrib/nvi/vi/getc.c,v 1.2 2003/06/17 04:24:04 dillon Exp $
15 #include <sys/types.h>
16 #include <sys/queue.h>
19 #include <bitstring.h>
25 #include "../common/common.h"
29 * Character stream routines --
30 * These routines return the file a character at a time. There are two
31 * special cases. First, the end of a line, end of a file, start of a
32 * file and empty lines are returned as special cases, and no character
33 * is returned. Second, empty lines include lines that have only white
34 * space in them, because the vi search functions don't care about white
35 * space, and this makes it easier for them to be consistent.
40 * Initialize character stream routines.
42 * PUBLIC: int cs_init __P((SCR *, VCS *));
51 if (db_eget(sp
, csp
->cs_lno
, &csp
->cs_bp
, &csp
->cs_len
, &isempty
)) {
53 msgq(sp
, M_BERR
, "177|Empty file");
56 if (csp
->cs_len
== 0 || v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
58 csp
->cs_flags
= CS_EMP
;
61 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
];
68 * Retrieve the next character.
70 * PUBLIC: int cs_next __P((SCR *, VCS *));
79 switch (csp
->cs_flags
) {
80 case CS_EMP
: /* EMP; get next line. */
81 case CS_EOL
: /* EOL; get next line. */
82 if (db_get(sp
, ++csp
->cs_lno
, 0, &p
, &csp
->cs_len
)) {
84 csp
->cs_flags
= CS_EOF
;
87 if (csp
->cs_len
== 0 ||
88 v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
90 csp
->cs_flags
= CS_EMP
;
93 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
= 0];
98 if (csp
->cs_cno
== csp
->cs_len
- 1)
99 csp
->cs_flags
= CS_EOL
;
101 csp
->cs_ch
= csp
->cs_bp
[++csp
->cs_cno
];
103 case CS_EOF
: /* EOF. */
114 * If on a space, eat forward until something other than a
115 * whitespace character.
118 * Semantics of checking the current character were coded for the fword()
119 * function -- once the other word routines are converted, they may have
122 * PUBLIC: int cs_fspace __P((SCR *, VCS *));
129 if (csp
->cs_flags
!= 0 || !isblank(csp
->cs_ch
))
132 if (cs_next(sp
, csp
))
134 if (csp
->cs_flags
!= 0 || !isblank(csp
->cs_ch
))
142 * Eat forward to the next non-whitespace character.
144 * PUBLIC: int cs_fblank __P((SCR *, VCS *));
152 if (cs_next(sp
, csp
))
154 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
155 csp
->cs_flags
== 0 && isblank(csp
->cs_ch
))
164 * Retrieve the previous character.
166 * PUBLIC: int cs_prev __P((SCR *, VCS *));
173 switch (csp
->cs_flags
) {
174 case CS_EMP
: /* EMP; get previous line. */
175 case CS_EOL
: /* EOL; get previous line. */
176 if (csp
->cs_lno
== 1) { /* SOF. */
177 csp
->cs_flags
= CS_SOF
;
180 if (db_get(sp
, /* The line should exist. */
181 --csp
->cs_lno
, DBG_FATAL
, &csp
->cs_bp
, &csp
->cs_len
)) {
185 if (csp
->cs_len
== 0 || v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
187 csp
->cs_flags
= CS_EMP
;
190 csp
->cs_cno
= csp
->cs_len
- 1;
191 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
];
194 case CS_EOF
: /* EOF: get previous char. */
196 if (csp
->cs_cno
== 0)
197 if (csp
->cs_lno
== 1)
198 csp
->cs_flags
= CS_SOF
;
200 csp
->cs_flags
= CS_EOL
;
202 csp
->cs_ch
= csp
->cs_bp
[--csp
->cs_cno
];
204 case CS_SOF
: /* SOF. */
215 * Eat backward to the next non-whitespace character.
217 * PUBLIC: int cs_bblank __P((SCR *, VCS *));
225 if (cs_prev(sp
, csp
))
227 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
228 csp
->cs_flags
== 0 && isblank(csp
->cs_ch
))