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[] = "@(#)getc.c 10.10 (Berkeley) 3/6/96";
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 __P((SCR *, VCS *));
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 *));
80 switch (csp->cs_flags) {
81 case CS_EMP: /* EMP; get next line. */
82 case CS_EOL: /* EOL; get next line. */
83 if (db_get(sp, ++csp->cs_lno, 0, &p, &csp->cs_len)) {
85 csp->cs_flags = CS_EOF;
88 if (csp->cs_len == 0 ||
89 v_isempty(csp->cs_bp, csp->cs_len)) {
91 csp->cs_flags = CS_EMP;
94 csp->cs_ch = csp->cs_bp[csp->cs_cno = 0];
99 if (csp->cs_cno == csp->cs_len - 1)
100 csp->cs_flags = CS_EOL;
102 csp->cs_ch = csp->cs_bp[++csp->cs_cno];
104 case CS_EOF: /* EOF. */
115 * If on a space, eat forward until something other than a
116 * whitespace character.
119 * Semantics of checking the current character were coded for the fword()
120 * function -- once the other word routines are converted, they may have
123 * PUBLIC: int cs_fspace __P((SCR *, VCS *));
130 if (csp->cs_flags != 0 || !isblank(csp->cs_ch))
133 if (cs_next(sp, csp))
135 if (csp->cs_flags != 0 || !isblank(csp->cs_ch))
143 * Eat forward to the next non-whitespace character.
145 * PUBLIC: int cs_fblank __P((SCR *, VCS *));
153 if (cs_next(sp, csp))
155 if (csp->cs_flags == CS_EOL || csp->cs_flags == CS_EMP ||
156 csp->cs_flags == 0 && isblank(csp->cs_ch))
165 * Retrieve the previous character.
167 * PUBLIC: int cs_prev __P((SCR *, VCS *));
174 switch (csp->cs_flags) {
175 case CS_EMP: /* EMP; get previous line. */
176 case CS_EOL: /* EOL; get previous line. */
177 if (csp->cs_lno == 1) { /* SOF. */
178 csp->cs_flags = CS_SOF;
181 if (db_get(sp, /* The line should exist. */
182 --csp->cs_lno, DBG_FATAL, &csp->cs_bp, &csp->cs_len)) {
186 if (csp->cs_len == 0 || v_isempty(csp->cs_bp, csp->cs_len)) {
188 csp->cs_flags = CS_EMP;
191 csp->cs_cno = csp->cs_len - 1;
192 csp->cs_ch = csp->cs_bp[csp->cs_cno];
195 case CS_EOF: /* EOF: get previous char. */
197 if (csp->cs_cno == 0)
198 if (csp->cs_lno == 1)
199 csp->cs_flags = CS_SOF;
201 csp->cs_flags = CS_EOL;
203 csp->cs_ch = csp->cs_bp[--csp->cs_cno];
205 case CS_SOF: /* SOF. */
216 * Eat backward to the next non-whitespace character.
218 * PUBLIC: int cs_bblank __P((SCR *, VCS *));
226 if (cs_prev(sp, csp))
228 if (csp->cs_flags == CS_EOL || csp->cs_flags == CS_EMP ||
229 csp->cs_flags == 0 && isblank(csp->cs_ch))