1 /* $NetBSD: getc.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: getc.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
26 #include <bitstring.h>
32 #include "../common/common.h"
36 * Character stream routines --
37 * These routines return the file a character at a time. There are two
38 * special cases. First, the end of a line, end of a file, start of a
39 * file and empty lines are returned as special cases, and no character
40 * is returned. Second, empty lines include lines that have only white
41 * space in them, because the vi search functions don't care about white
42 * space, and this makes it easier for them to be consistent.
47 * Initialize character stream routines.
49 * PUBLIC: int cs_init __P((SCR *, VCS *));
52 cs_init(SCR
*sp
, VCS
*csp
)
56 if (db_eget(sp
, csp
->cs_lno
, &csp
->cs_bp
, &csp
->cs_len
, &isempty
)) {
58 msgq(sp
, M_BERR
, "177|Empty file");
61 if (csp
->cs_len
== 0 || v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
63 csp
->cs_flags
= CS_EMP
;
66 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
];
73 * Retrieve the next character.
75 * PUBLIC: int cs_next __P((SCR *, VCS *));
78 cs_next(SCR
*sp
, VCS
*csp
)
82 switch (csp
->cs_flags
) {
83 case CS_EMP
: /* EMP; get next line. */
84 case CS_EOL
: /* EOL; get next line. */
85 if (db_get(sp
, ++csp
->cs_lno
, 0, &p
, &csp
->cs_len
)) {
87 csp
->cs_flags
= CS_EOF
;
90 if (csp
->cs_len
== 0 ||
91 v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
93 csp
->cs_flags
= CS_EMP
;
96 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
= 0];
101 if (csp
->cs_cno
== csp
->cs_len
- 1)
102 csp
->cs_flags
= CS_EOL
;
104 csp
->cs_ch
= csp
->cs_bp
[++csp
->cs_cno
];
106 case CS_EOF
: /* EOF. */
117 * If on a space, eat forward until something other than a
118 * whitespace character.
121 * Semantics of checking the current character were coded for the fword()
122 * function -- once the other word routines are converted, they may have
125 * PUBLIC: int cs_fspace __P((SCR *, VCS *));
128 cs_fspace(SCR
*sp
, VCS
*csp
)
130 if (csp
->cs_flags
!= 0 || !ISBLANK2(csp
->cs_ch
))
133 if (cs_next(sp
, csp
))
135 if (csp
->cs_flags
!= 0 || !ISBLANK2(csp
->cs_ch
))
143 * Eat forward to the next non-whitespace character.
145 * PUBLIC: int cs_fblank __P((SCR *, VCS *));
148 cs_fblank(SCR
*sp
, VCS
*csp
)
151 if (cs_next(sp
, csp
))
153 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
154 (csp
->cs_flags
== 0 && ISBLANK2(csp
->cs_ch
)))
163 * Retrieve the previous character.
165 * PUBLIC: int cs_prev __P((SCR *, VCS *));
168 cs_prev(SCR
*sp
, VCS
*csp
)
170 switch (csp
->cs_flags
) {
171 case CS_EMP
: /* EMP; get previous line. */
172 case CS_EOL
: /* EOL; get previous line. */
173 if (csp
->cs_lno
== 1) { /* SOF. */
174 csp
->cs_flags
= CS_SOF
;
177 if (db_get(sp
, /* The line should exist. */
178 --csp
->cs_lno
, DBG_FATAL
, &csp
->cs_bp
, &csp
->cs_len
)) {
182 if (csp
->cs_len
== 0 || v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
184 csp
->cs_flags
= CS_EMP
;
187 csp
->cs_cno
= csp
->cs_len
- 1;
188 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
];
191 case CS_EOF
: /* EOF: get previous char. */
193 if (csp
->cs_cno
== 0)
194 if (csp
->cs_lno
== 1)
195 csp
->cs_flags
= CS_SOF
;
197 csp
->cs_flags
= CS_EOL
;
199 csp
->cs_ch
= csp
->cs_bp
[--csp
->cs_cno
];
201 case CS_SOF
: /* SOF. */
212 * Eat backward to the next non-whitespace character.
214 * PUBLIC: int cs_bblank __P((SCR *, VCS *));
217 cs_bblank(SCR
*sp
, VCS
*csp
)
220 if (cs_prev(sp
, csp
))
222 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
223 (csp
->cs_flags
== 0 && ISBLANK2(csp
->cs_ch
)))