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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
22 #include "../common/common.h"
26 * Character stream routines --
27 * These routines return the file a character at a time. There are two
28 * special cases. First, the end of a line, end of a file, start of a
29 * file and empty lines are returned as special cases, and no character
30 * is returned. Second, empty lines include lines that have only white
31 * space in them, because the vi search functions don't care about white
32 * space, and this makes it easier for them to be consistent.
37 * Initialize character stream routines.
39 * PUBLIC: int cs_init(SCR *, VCS *);
42 cs_init(SCR *sp, VCS *csp)
46 if (db_eget(sp, csp->cs_lno, &csp->cs_bp, &csp->cs_len, &isempty)) {
48 msgq(sp, M_BERR, "177|Empty file");
51 if (csp->cs_len == 0 || v_isempty(csp->cs_bp, csp->cs_len)) {
53 csp->cs_flags = CS_EMP;
56 csp->cs_ch = csp->cs_bp[csp->cs_cno];
63 * Retrieve the next character.
65 * PUBLIC: int cs_next(SCR *, VCS *);
68 cs_next(SCR *sp, VCS *csp)
72 switch (csp->cs_flags) {
73 case CS_EMP: /* EMP; get next line. */
74 case CS_EOL: /* EOL; get next line. */
75 if (db_get(sp, ++csp->cs_lno, 0, &p, &csp->cs_len)) {
77 csp->cs_flags = CS_EOF;
80 if (csp->cs_len == 0 ||
81 v_isempty(csp->cs_bp, csp->cs_len)) {
83 csp->cs_flags = CS_EMP;
86 csp->cs_ch = csp->cs_bp[csp->cs_cno = 0];
91 if (csp->cs_cno == csp->cs_len - 1)
92 csp->cs_flags = CS_EOL;
94 csp->cs_ch = csp->cs_bp[++csp->cs_cno];
96 case CS_EOF: /* EOF. */
107 * If on a space, eat forward until something other than a
108 * whitespace character.
111 * Semantics of checking the current character were coded for the fword()
112 * function -- once the other word routines are converted, they may have
115 * PUBLIC: int cs_fspace(SCR *, VCS *);
118 cs_fspace(SCR *sp, VCS *csp)
120 if (csp->cs_flags != 0 || !ISBLANK(csp->cs_ch))
123 if (cs_next(sp, csp))
125 if (csp->cs_flags != 0 || !ISBLANK(csp->cs_ch))
133 * Eat forward to the next non-whitespace character.
135 * PUBLIC: int cs_fblank(SCR *, VCS *);
138 cs_fblank(SCR *sp, VCS *csp)
141 if (cs_next(sp, csp))
143 if (csp->cs_flags == CS_EOL || csp->cs_flags == CS_EMP ||
144 (csp->cs_flags == 0 && ISBLANK(csp->cs_ch)))
153 * Retrieve the previous character.
155 * PUBLIC: int cs_prev(SCR *, VCS *);
158 cs_prev(SCR *sp, VCS *csp)
160 switch (csp->cs_flags) {
161 case CS_EMP: /* EMP; get previous line. */
162 case CS_EOL: /* EOL; get previous line. */
163 if (csp->cs_lno == 1) { /* SOF. */
164 csp->cs_flags = CS_SOF;
167 if (db_get(sp, /* The line should exist. */
168 --csp->cs_lno, DBG_FATAL, &csp->cs_bp, &csp->cs_len)) {
172 if (csp->cs_len == 0 || v_isempty(csp->cs_bp, csp->cs_len)) {
174 csp->cs_flags = CS_EMP;
177 csp->cs_cno = csp->cs_len - 1;
178 csp->cs_ch = csp->cs_bp[csp->cs_cno];
181 case CS_EOF: /* EOF: get previous char. */
183 if (csp->cs_cno == 0)
184 if (csp->cs_lno == 1)
185 csp->cs_flags = CS_SOF;
187 csp->cs_flags = CS_EOL;
189 csp->cs_ch = csp->cs_bp[--csp->cs_cno];
191 case CS_SOF: /* SOF. */
202 * Eat backward to the next non-whitespace character.
204 * PUBLIC: int cs_bblank(SCR *, VCS *);
207 cs_bblank(SCR *sp, VCS *csp)
210 if (cs_prev(sp, csp))
212 if (csp->cs_flags == CS_EOL || csp->cs_flags == CS_EMP ||
213 (csp->cs_flags == 0 && ISBLANK(csp->cs_ch)))