Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / vi / getc.c
blob6aef39bfe9dc72efdef47df3ae0f005369c5ef7e
1 /* $NetBSD: getc.c,v 1.1.1.2 2008/05/18 14:31:40 aymeric Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "../common/common.h"
29 #include "vi.h"
32 * Character stream routines --
33 * These routines return the file a character at a time. There are two
34 * special cases. First, the end of a line, end of a file, start of a
35 * file and empty lines are returned as special cases, and no character
36 * is returned. Second, empty lines include lines that have only white
37 * space in them, because the vi search functions don't care about white
38 * space, and this makes it easier for them to be consistent.
42 * cs_init --
43 * Initialize character stream routines.
45 * PUBLIC: int cs_init __P((SCR *, VCS *));
47 int
48 cs_init(SCR *sp, VCS *csp)
50 int isempty;
52 if (db_eget(sp, csp->cs_lno, &csp->cs_bp, &csp->cs_len, &isempty)) {
53 if (isempty)
54 msgq(sp, M_BERR, "177|Empty file");
55 return (1);
57 if (csp->cs_len == 0 || v_isempty(csp->cs_bp, csp->cs_len)) {
58 csp->cs_cno = 0;
59 csp->cs_flags = CS_EMP;
60 } else {
61 csp->cs_flags = 0;
62 csp->cs_ch = csp->cs_bp[csp->cs_cno];
64 return (0);
68 * cs_next --
69 * Retrieve the next character.
71 * PUBLIC: int cs_next __P((SCR *, VCS *));
73 int
74 cs_next(SCR *sp, VCS *csp)
76 CHAR_T *p;
78 switch (csp->cs_flags) {
79 case CS_EMP: /* EMP; get next line. */
80 case CS_EOL: /* EOL; get next line. */
81 if (db_get(sp, ++csp->cs_lno, 0, &p, &csp->cs_len)) {
82 --csp->cs_lno;
83 csp->cs_flags = CS_EOF;
84 } else {
85 csp->cs_bp = p;
86 if (csp->cs_len == 0 ||
87 v_isempty(csp->cs_bp, csp->cs_len)) {
88 csp->cs_cno = 0;
89 csp->cs_flags = CS_EMP;
90 } else {
91 csp->cs_flags = 0;
92 csp->cs_ch = csp->cs_bp[csp->cs_cno = 0];
95 break;
96 case 0:
97 if (csp->cs_cno == csp->cs_len - 1)
98 csp->cs_flags = CS_EOL;
99 else
100 csp->cs_ch = csp->cs_bp[++csp->cs_cno];
101 break;
102 case CS_EOF: /* EOF. */
103 break;
104 default:
105 abort();
106 /* NOTREACHED */
108 return (0);
112 * cs_fspace --
113 * If on a space, eat forward until something other than a
114 * whitespace character.
116 * XXX
117 * Semantics of checking the current character were coded for the fword()
118 * function -- once the other word routines are converted, they may have
119 * to change.
121 * PUBLIC: int cs_fspace __P((SCR *, VCS *));
124 cs_fspace(SCR *sp, VCS *csp)
126 if (csp->cs_flags != 0 || !isblank(csp->cs_ch))
127 return (0);
128 for (;;) {
129 if (cs_next(sp, csp))
130 return (1);
131 if (csp->cs_flags != 0 || !isblank(csp->cs_ch))
132 break;
134 return (0);
138 * cs_fblank --
139 * Eat forward to the next non-whitespace character.
141 * PUBLIC: int cs_fblank __P((SCR *, VCS *));
144 cs_fblank(SCR *sp, VCS *csp)
146 for (;;) {
147 if (cs_next(sp, csp))
148 return (1);
149 if (csp->cs_flags == CS_EOL || csp->cs_flags == CS_EMP ||
150 (csp->cs_flags == 0 && isblank(csp->cs_ch)))
151 continue;
152 break;
154 return (0);
158 * cs_prev --
159 * Retrieve the previous character.
161 * PUBLIC: int cs_prev __P((SCR *, VCS *));
164 cs_prev(SCR *sp, VCS *csp)
166 switch (csp->cs_flags) {
167 case CS_EMP: /* EMP; get previous line. */
168 case CS_EOL: /* EOL; get previous line. */
169 if (csp->cs_lno == 1) { /* SOF. */
170 csp->cs_flags = CS_SOF;
171 break;
173 if (db_get(sp, /* The line should exist. */
174 --csp->cs_lno, DBG_FATAL, &csp->cs_bp, &csp->cs_len)) {
175 ++csp->cs_lno;
176 return (1);
178 if (csp->cs_len == 0 || v_isempty(csp->cs_bp, csp->cs_len)) {
179 csp->cs_cno = 0;
180 csp->cs_flags = CS_EMP;
181 } else {
182 csp->cs_flags = 0;
183 csp->cs_cno = csp->cs_len - 1;
184 csp->cs_ch = csp->cs_bp[csp->cs_cno];
186 break;
187 case CS_EOF: /* EOF: get previous char. */
188 case 0:
189 if (csp->cs_cno == 0)
190 if (csp->cs_lno == 1)
191 csp->cs_flags = CS_SOF;
192 else
193 csp->cs_flags = CS_EOL;
194 else
195 csp->cs_ch = csp->cs_bp[--csp->cs_cno];
196 break;
197 case CS_SOF: /* SOF. */
198 break;
199 default:
200 abort();
201 /* NOTREACHED */
203 return (0);
207 * cs_bblank --
208 * Eat backward to the next non-whitespace character.
210 * PUBLIC: int cs_bblank __P((SCR *, VCS *));
213 cs_bblank(SCR *sp, VCS *csp)
215 for (;;) {
216 if (cs_prev(sp, csp))
217 return (1);
218 if (csp->cs_flags == CS_EOL || csp->cs_flags == CS_EMP ||
219 (csp->cs_flags == 0 && isblank(csp->cs_ch)))
220 continue;
221 break;
223 return (0);