2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: getc.c,v 8.6 1993/10/26 17:22:58 bostic Exp $ (Berkeley) $Date: 1993/10/26 17:22:58 $";
12 #include <sys/types.h>
20 * Character stream routines --
21 * These routines return the file a character at a time. There are two
22 * special cases. First, the end of a line, end of a file, start of a
23 * file and empty lines are returned as special cases, and no character
24 * is returned. Second, empty lines include lines that have only white
25 * space in them, because the vi search functions don't care about white
26 * space, and this makes it easier for them to be consistent.
31 * Initialize character stream routines.
42 file_gline(sp
, ep
, csp
->cs_lno
, &csp
->cs_len
)) == NULL
) {
43 if (file_lline(sp
, ep
, &lno
))
46 msgq(sp
, M_BERR
, "Empty file.");
48 GETLINE_ERR(sp
, csp
->cs_lno
);
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.
73 switch (csp
->cs_flags
) {
74 case CS_EMP
: /* EMP; get next line. */
75 case CS_EOL
: /* EOL; get next line. */
76 slno
= csp
->cs_lno
; /* Save current line. */
78 file_gline(sp
, ep
, ++csp
->cs_lno
, &csp
->cs_len
)) == NULL
) {
80 if (file_lline(sp
, ep
, &slno
))
82 if (slno
> csp
->cs_lno
) {
83 GETLINE_ERR(sp
, csp
->cs_lno
);
86 csp
->cs_flags
= CS_EOF
;
87 } else if (csp
->cs_len
== 0 ||
88 v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
90 csp
->cs_flags
= CS_EMP
;
93 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
= 0];
97 if (csp
->cs_cno
== csp
->cs_len
- 1)
98 csp
->cs_flags
= CS_EOL
;
100 csp
->cs_ch
= csp
->cs_bp
[++csp
->cs_cno
];
102 case CS_EOF
: /* EOF; only returned once. */
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
121 cs_fspace(sp
, ep
, csp
)
126 if (csp
->cs_flags
!= 0 || !isblank(csp
->cs_ch
))
129 if (cs_next(sp
, ep
, csp
))
131 if (csp
->cs_flags
!= 0 || !isblank(csp
->cs_ch
))
139 * Eat forward to the next non-whitespace character.
142 cs_fblank(sp
, ep
, csp
)
148 if (cs_next(sp
, ep
, csp
))
150 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
151 csp
->cs_flags
== 0 && isblank(csp
->cs_ch
))
160 * Retrieve the previous character.
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 slno
= csp
->cs_lno
; /* Save current line. */
178 if ((csp
->cs_bp
= /* Line should exist. */
179 file_gline(sp
, ep
, --csp
->cs_lno
, &csp
->cs_len
)) == NULL
) {
180 GETLINE_ERR(sp
, csp
->cs_lno
);
184 if (csp
->cs_len
== 0 || v_isempty(csp
->cs_bp
, csp
->cs_len
)) {
186 csp
->cs_flags
= CS_EMP
;
189 csp
->cs_cno
= csp
->cs_len
- 1;
190 csp
->cs_ch
= csp
->cs_bp
[csp
->cs_cno
];
194 if (csp
->cs_cno
== 0)
195 csp
->cs_flags
= CS_EOL
;
197 csp
->cs_ch
= csp
->cs_bp
[--csp
->cs_cno
];
199 case CS_SOF
: /* SOF; only returned once. */
209 * Eat backward to the next non-whitespace character.
212 cs_bblank(sp
, ep
, csp
)
218 if (cs_prev(sp
, ep
, csp
))
220 if (csp
->cs_flags
== CS_EOL
|| csp
->cs_flags
== CS_EMP
||
221 csp
->cs_flags
== 0 && isblank(csp
->cs_ch
))