1 /* $NetBSD: getc.c,v 1.2 2013/11/22 15:52:06 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.
14 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 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
27 #include "../common/common.h"
31 * Character stream routines --
32 * These routines return the file a character at a time. There are two
33 * special cases. First, the end of a line, end of a file, start of a
34 * file and empty lines are returned as special cases, and no character
35 * is returned. Second, empty lines include lines that have only white
36 * space in them, because the vi search functions don't care about white
37 * space, and this makes it easier for them to be consistent.
42 * Initialize character stream routines.
44 * PUBLIC: int cs_init __P((SCR *, VCS *));
47 cs_init(SCR
*sp
, VCS
*csp
)
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 *));
73 cs_next(SCR
*sp
, VCS
*csp
)
77 switch (csp
->cs_flags
) {
78 case CS_EMP
: /* EMP; get next line. */
79 case CS_EOL
: /* EOL; get next line. */
80 if (db_get(sp
, ++csp
->cs_lno
, 0, &p
, &csp
->cs_len
)) {
82 csp
->cs_flags
= CS_EOF
;
85 if (csp
->cs_len
== 0 ||
86 v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
88 csp
->cs_flags
= CS_EMP
;
91 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
= 0];
96 if (csp
->cs_cno
== csp
->cs_len
- 1)
97 csp
->cs_flags
= CS_EOL
;
99 csp
->cs_ch
= csp
->cs_bp
[++csp
->cs_cno
];
101 case CS_EOF
: /* EOF. */
112 * If on a space, eat forward until something other than a
113 * whitespace character.
116 * Semantics of checking the current character were coded for the fword()
117 * function -- once the other word routines are converted, they may have
120 * PUBLIC: int cs_fspace __P((SCR *, VCS *));
123 cs_fspace(SCR
*sp
, VCS
*csp
)
125 if (csp
->cs_flags
!= 0 || !ISBLANK2(csp
->cs_ch
))
128 if (cs_next(sp
, csp
))
130 if (csp
->cs_flags
!= 0 || !ISBLANK2(csp
->cs_ch
))
138 * Eat forward to the next non-whitespace character.
140 * PUBLIC: int cs_fblank __P((SCR *, VCS *));
143 cs_fblank(SCR
*sp
, VCS
*csp
)
146 if (cs_next(sp
, csp
))
148 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
149 (csp
->cs_flags
== 0 && ISBLANK2(csp
->cs_ch
)))
158 * Retrieve the previous character.
160 * PUBLIC: int cs_prev __P((SCR *, VCS *));
163 cs_prev(SCR
*sp
, VCS
*csp
)
165 switch (csp
->cs_flags
) {
166 case CS_EMP
: /* EMP; get previous line. */
167 case CS_EOL
: /* EOL; get previous line. */
168 if (csp
->cs_lno
== 1) { /* SOF. */
169 csp
->cs_flags
= CS_SOF
;
172 if (db_get(sp
, /* The line should exist. */
173 --csp
->cs_lno
, DBG_FATAL
, &csp
->cs_bp
, &csp
->cs_len
)) {
177 if (csp
->cs_len
== 0 || v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
179 csp
->cs_flags
= CS_EMP
;
182 csp
->cs_cno
= csp
->cs_len
- 1;
183 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
];
186 case CS_EOF
: /* EOF: get previous char. */
188 if (csp
->cs_cno
== 0)
189 if (csp
->cs_lno
== 1)
190 csp
->cs_flags
= CS_SOF
;
192 csp
->cs_flags
= CS_EOL
;
194 csp
->cs_ch
= csp
->cs_bp
[--csp
->cs_cno
];
196 case CS_SOF
: /* SOF. */
207 * Eat backward to the next non-whitespace character.
209 * PUBLIC: int cs_bblank __P((SCR *, VCS *));
212 cs_bblank(SCR
*sp
, VCS
*csp
)
215 if (cs_prev(sp
, csp
))
217 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
218 (csp
->cs_flags
== 0 && ISBLANK2(csp
->cs_ch
)))