lint
[nvi.git] / common / db.c
blobc39ea07e489cf8179bd99c8649de1c809304ecbb
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: db.c,v 8.18 1993/11/22 17:26:52 bostic Exp $ (Berkeley) $Date: 1993/11/22 17:26:52 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <errno.h>
15 #include <string.h>
17 #include "vi.h"
19 static inline int scr_update __P((SCR *, EXF *, recno_t, enum operation, int));
22 * file_gline --
23 * Look in the text buffers for a line; if it's not there
24 * call file_rline to retrieve it from the database.
26 char *
27 file_gline(sp, ep, lno, lenp)
28 SCR *sp;
29 EXF *ep;
30 recno_t lno; /* Line number. */
31 size_t *lenp; /* Length store. */
33 TEXT *tp;
34 recno_t l1, l2;
37 * The underlying recno stuff handles zero by returning NULL, but
38 * have to have an oob condition for the look-aside into the input
39 * buffer anyway.
41 if (lno == 0)
42 return (NULL);
45 * Look-aside into the TEXT buffers and see if the line we want
46 * is there.
48 if (F_ISSET(sp, S_INPUT)) {
49 l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
50 l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
51 if (l1 <= lno && l2 >= lno) {
52 for (tp = sp->tiq.cqh_first;
53 tp->lno != lno; tp = tp->q.cqe_next);
54 if (lenp)
55 *lenp = tp->len;
56 return (tp->lb);
59 * Adjust the line number for the number of lines used
60 * by the text input buffers.
62 if (lno > l2)
63 lno -= l2 - l1;
65 return (file_rline(sp, ep, lno, lenp));
69 * file_rline --
70 * Look in the cache for a line; if it's not there retrieve
71 * it from the file.
73 char *
74 file_rline(sp, ep, lno, lenp)
75 SCR *sp;
76 EXF *ep;
77 recno_t lno; /* Line number. */
78 size_t *lenp; /* Length store. */
80 DBT data, key;
82 /* Check the cache. */
83 if (lno == ep->c_lno) {
84 if (lenp)
85 *lenp = ep->c_len;
86 return (ep->c_lp);
88 ep->c_lno = OOBLNO;
90 /* Get the line from the underlying database. */
91 key.data = &lno;
92 key.size = sizeof(lno);
93 switch (ep->db->get(ep->db, &key, &data, 0)) {
94 case -1:
95 msgq(sp, M_ERR,
96 "Error: %s/%d: unable to get line %u: %s.",
97 tail(__FILE__), __LINE__, lno, strerror(errno));
98 /* FALLTHROUGH */
99 case 1:
100 return (NULL);
101 /* NOTREACHED */
103 if (lenp)
104 *lenp = data.size;
106 /* Fill the cache. */
107 ep->c_lno = lno;
108 ep->c_len = data.size;
109 ep->c_lp = data.data;
111 return (data.data);
115 * file_dline --
116 * Delete a line from the file.
119 file_dline(sp, ep, lno)
120 SCR *sp;
121 EXF *ep;
122 recno_t lno;
124 DBT key;
126 #if defined(DEBUG) && 0
127 TRACE(sp, "delete line %lu\n", lno);
128 #endif
129 /* Delete any marks in the line. */
130 mark_delete(sp, ep, lno);
132 /* Log change. */
133 log_line(sp, ep, lno, LOG_LINE_DELETE);
135 /* Update file. */
136 key.data = &lno;
137 key.size = sizeof(lno);
138 if (ep->db->del(ep->db, &key, 0) == 1) {
139 msgq(sp, M_ERR,
140 "Error: %s/%d: unable to delete line %u: %s.",
141 tail(__FILE__), __LINE__, lno, strerror(errno));
142 return (1);
145 /* Flush the cache, update line count, before screen update. */
146 if (lno <= ep->c_lno)
147 ep->c_lno = OOBLNO;
148 if (ep->c_nlines != OOBLNO)
149 --ep->c_nlines;
151 /* File now dirty. */
152 if (F_ISSET(ep, F_FIRSTMODIFY))
153 (void)rcv_init(sp, ep);
154 F_SET(ep, F_MODIFIED);
156 /* Update screen. */
157 return (scr_update(sp, ep, lno, LINE_DELETE, 1));
161 * file_aline --
162 * Append a line into the file.
165 file_aline(sp, ep, update, lno, p, len)
166 SCR *sp;
167 EXF *ep;
168 int update;
169 recno_t lno;
170 char *p;
171 size_t len;
173 DBT data, key;
174 recno_t lline;
176 #if defined(DEBUG) && 0
177 TRACE(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
178 #endif
180 * Very nasty special case. The historic vi code displays a single
181 * space (or a '$' if the list option is set) for the first line in
182 * an "empty" file. If we "insert" a line, that line gets scrolled
183 * down, not repainted, so it's incorrect when we refresh the the
184 * screen. This is really hard to find and fix in the vi code -- the
185 * text input functions detect it explicitly and don't insert a new
186 * line. The hack here is to repaint the screen if we're appending
187 * to an empty file.
189 if (lno == 0) {
190 if (file_lline(sp, ep, &lline))
191 return (1);
192 if (lline == 0)
193 F_SET(sp, S_REDRAW);
196 /* Update file. */
197 key.data = &lno;
198 key.size = sizeof(lno);
199 data.data = p;
200 data.size = len;
201 if (ep->db->put(ep->db, &key, &data, R_IAFTER) == -1) {
202 msgq(sp, M_ERR,
203 "Error: %s/%d: unable to append to line %u: %s.",
204 tail(__FILE__), __LINE__, lno, strerror(errno));
205 return (1);
208 /* Flush the cache, update line count, before screen update. */
209 if (lno < ep->c_lno)
210 ep->c_lno = OOBLNO;
211 if (ep->c_nlines != OOBLNO)
212 ++ep->c_nlines;
214 /* File now dirty. */
215 if (F_ISSET(ep, F_FIRSTMODIFY))
216 (void)rcv_init(sp, ep);
217 F_SET(ep, F_MODIFIED);
219 /* Log change. */
220 log_line(sp, ep, lno + 1, LOG_LINE_APPEND);
222 /* Push any marks above the line. */
223 mark_insert(sp, ep, lno + 1);
226 * Update screen.
228 * XXX
229 * Nasty hack. If multiple lines are input by the user, they aren't
230 * committed until an <ESC> is entered. The problem is the screen was
231 * updated/scrolled as each line was entered. So, when this routine
232 * is called to copy the new lines from the cut buffer into the file,
233 * it has to know not to update the screen again.
235 return (scr_update(sp, ep, lno, LINE_APPEND, update));
239 * file_iline --
240 * Insert a line into the file.
243 file_iline(sp, ep, lno, p, len)
244 SCR *sp;
245 EXF *ep;
246 recno_t lno;
247 char *p;
248 size_t len;
250 DBT data, key;
251 recno_t lline;
253 #if defined(DEBUG) && 0
254 TRACE(sp,
255 "insert before %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
256 #endif
258 /* Very nasty special case. See comment in file_aline(). */
259 if (lno == 1) {
260 if (file_lline(sp, ep, &lline))
261 return (1);
262 if (lline == 0)
263 F_SET(sp, S_REDRAW);
266 /* Update file. */
267 key.data = &lno;
268 key.size = sizeof(lno);
269 data.data = p;
270 data.size = len;
271 if (ep->db->put(ep->db, &key, &data, R_IBEFORE) == -1) {
272 msgq(sp, M_ERR,
273 "Error: %s/%d: unable to insert at line %u: %s.",
274 tail(__FILE__), __LINE__, lno, strerror(errno));
275 return (1);
278 /* Flush the cache, update line count, before screen update. */
279 if (lno >= ep->c_lno)
280 ep->c_lno = OOBLNO;
281 if (ep->c_nlines != OOBLNO)
282 ++ep->c_nlines;
284 /* File now dirty. */
285 if (F_ISSET(ep, F_FIRSTMODIFY))
286 (void)rcv_init(sp, ep);
287 F_SET(ep, F_MODIFIED);
289 /* Log change. */
290 log_line(sp, ep, lno, LOG_LINE_INSERT);
292 /* Push any marks above the line. */
293 mark_insert(sp, ep, lno);
295 /* Update screen. */
296 return (scr_update(sp, ep, lno, LINE_INSERT, 1));
300 * file_sline --
301 * Store a line in the file.
304 file_sline(sp, ep, lno, p, len)
305 SCR *sp;
306 EXF *ep;
307 recno_t lno;
308 char *p;
309 size_t len;
311 DBT data, key;
313 #if defined(DEBUG) && 0
314 TRACE(sp,
315 "replace line %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
316 #endif
317 /* Log before change. */
318 log_line(sp, ep, lno, LOG_LINE_RESET_B);
320 /* Update file. */
321 key.data = &lno;
322 key.size = sizeof(lno);
323 data.data = p;
324 data.size = len;
325 if (ep->db->put(ep->db, &key, &data, 0) == -1) {
326 msgq(sp, M_ERR,
327 "Error: %s/%d: unable to store line %u: %s.",
328 tail(__FILE__), __LINE__, lno, strerror(errno));
329 return (1);
332 /* Flush the cache, before logging or screen update. */
333 if (lno == ep->c_lno)
334 ep->c_lno = OOBLNO;
336 /* File now dirty. */
337 if (F_ISSET(ep, F_FIRSTMODIFY))
338 (void)rcv_init(sp, ep);
339 F_SET(ep, F_MODIFIED);
341 /* Log after change. */
342 log_line(sp, ep, lno, LOG_LINE_RESET_F);
344 /* Update screen. */
345 return (scr_update(sp, ep, lno, LINE_RESET, 1));
349 * file_lline --
350 * Return the number of lines in the file.
353 file_lline(sp, ep, lnop)
354 SCR *sp;
355 EXF *ep;
356 recno_t *lnop;
358 DBT data, key;
359 recno_t lno;
361 /* Check the cache. */
362 if (ep->c_nlines != OOBLNO) {
363 *lnop = (F_ISSET(sp, S_INPUT) &&
364 ((TEXT *)sp->tiq.cqh_last)->lno > ep->c_nlines ?
365 ((TEXT *)sp->tiq.cqh_last)->lno : ep->c_nlines);
366 return (0);
369 key.data = &lno;
370 key.size = sizeof(lno);
372 switch (ep->db->seq(ep->db, &key, &data, R_LAST)) {
373 case -1:
374 msgq(sp, M_ERR,
375 "Error: %s/%d: unable to get last line: %s.",
376 tail(__FILE__), __LINE__, strerror(errno));
377 *lnop = 0;
378 return (1);
379 case 1:
380 lno = 0;
381 break;
382 default:
383 memmove(&lno, key.data, sizeof(lno));
384 break;
387 /* Fill the cache. */
388 ep->c_nlines = ep->c_lno = lno;
389 ep->c_len = data.size;
390 ep->c_lp = data.data;
392 *lnop = (F_ISSET(sp, S_INPUT) &&
393 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
394 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
395 return (0);
399 * scr_update --
400 * Update all of the screens that are backed by the file that
401 * just changed.
403 static inline int
404 scr_update(sp, ep, lno, op, current)
405 SCR *sp;
406 EXF *ep;
407 recno_t lno;
408 enum operation op;
409 int current;
411 SCR *tsp;
413 if (ep->refcnt != 1)
414 for (tsp = sp->gp->dq.cqh_first;
415 tsp != (void *)&sp->gp->dq; tsp = tsp->q.cqe_next)
416 if (sp != tsp && tsp->ep == ep)
417 (void)sp->s_change(tsp, ep, lno, op);
418 return (current && sp->s_change(sp, ep, lno, op));