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.
13 static const char sccsid
[] = "$Id: getc.c,v 10.13 2011/12/27 00:49:31 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
26 #include "../common/common.h"
30 * Character stream routines --
31 * These routines return the file a character at a time. There are two
32 * special cases. First, the end of a line, end of a file, start of a
33 * file and empty lines are returned as special cases, and no character
34 * is returned. Second, empty lines include lines that have only white
35 * space in them, because the vi search functions don't care about white
36 * space, and this makes it easier for them to be consistent.
41 * Initialize character stream routines.
43 * PUBLIC: int cs_init(SCR *, VCS *);
46 cs_init(SCR
*sp
, VCS
*csp
)
50 if (db_eget(sp
, csp
->cs_lno
, &csp
->cs_bp
, &csp
->cs_len
, &isempty
)) {
52 msgq(sp
, M_BERR
, "177|Empty file");
55 if (csp
->cs_len
== 0 || v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
57 csp
->cs_flags
= CS_EMP
;
60 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
];
67 * Retrieve the next character.
69 * PUBLIC: int cs_next(SCR *, VCS *);
72 cs_next(SCR
*sp
, VCS
*csp
)
76 switch (csp
->cs_flags
) {
77 case CS_EMP
: /* EMP; get next line. */
78 case CS_EOL
: /* EOL; get next line. */
79 if (db_get(sp
, ++csp
->cs_lno
, 0, &p
, &csp
->cs_len
)) {
81 csp
->cs_flags
= CS_EOF
;
84 if (csp
->cs_len
== 0 ||
85 v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
87 csp
->cs_flags
= CS_EMP
;
90 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
= 0];
95 if (csp
->cs_cno
== csp
->cs_len
- 1)
96 csp
->cs_flags
= CS_EOL
;
98 csp
->cs_ch
= csp
->cs_bp
[++csp
->cs_cno
];
100 case CS_EOF
: /* EOF. */
111 * If on a space, eat forward until something other than a
112 * whitespace character.
115 * Semantics of checking the current character were coded for the fword()
116 * function -- once the other word routines are converted, they may have
119 * PUBLIC: int cs_fspace(SCR *, VCS *);
122 cs_fspace(SCR
*sp
, VCS
*csp
)
124 if (csp
->cs_flags
!= 0 || !ISBLANK(csp
->cs_ch
))
127 if (cs_next(sp
, csp
))
129 if (csp
->cs_flags
!= 0 || !ISBLANK(csp
->cs_ch
))
137 * Eat forward to the next non-whitespace character.
139 * PUBLIC: int cs_fblank(SCR *, VCS *);
142 cs_fblank(SCR
*sp
, VCS
*csp
)
145 if (cs_next(sp
, csp
))
147 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
148 (csp
->cs_flags
== 0 && ISBLANK(csp
->cs_ch
)))
157 * Retrieve the previous character.
159 * PUBLIC: int cs_prev(SCR *, VCS *);
162 cs_prev(SCR
*sp
, VCS
*csp
)
164 switch (csp
->cs_flags
) {
165 case CS_EMP
: /* EMP; get previous line. */
166 case CS_EOL
: /* EOL; get previous line. */
167 if (csp
->cs_lno
== 1) { /* SOF. */
168 csp
->cs_flags
= CS_SOF
;
171 if (db_get(sp
, /* The line should exist. */
172 --csp
->cs_lno
, DBG_FATAL
, &csp
->cs_bp
, &csp
->cs_len
)) {
176 if (csp
->cs_len
== 0 || v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
178 csp
->cs_flags
= CS_EMP
;
181 csp
->cs_cno
= csp
->cs_len
- 1;
182 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
];
185 case CS_EOF
: /* EOF: get previous char. */
187 if (csp
->cs_cno
== 0)
188 if (csp
->cs_lno
== 1)
189 csp
->cs_flags
= CS_SOF
;
191 csp
->cs_flags
= CS_EOL
;
193 csp
->cs_ch
= csp
->cs_bp
[--csp
->cs_cno
];
195 case CS_SOF
: /* SOF. */
206 * Eat backward to the next non-whitespace character.
208 * PUBLIC: int cs_bblank(SCR *, VCS *);
211 cs_bblank(SCR
*sp
, VCS
*csp
)
214 if (cs_prev(sp
, csp
))
216 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
217 (csp
->cs_flags
== 0 && ISBLANK(csp
->cs_ch
)))