more flushing
[nvi.git] / common / db.c
blobfb0452cd9d500cfd1f70a93e84a906ab8a056251
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: db.c,v 10.42 2001/10/11 19:19:03 skimo Exp $ (Berkeley) $Date: 2001/10/11 19:19:03 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include "common.h"
27 #include "../vi/vi.h"
29 static int scr_update __P((SCR *, db_recno_t, lnop_t, int));
30 static int append __P((SCR*, db_recno_t, CHAR_T*, size_t));
33 * db_eget --
34 * Front-end to db_get, special case handling for empty files.
36 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
38 int
39 db_eget(SCR *sp, db_recno_t lno, CHAR_T **pp, size_t *lenp, int *isemptyp)
41 /* Line number. */
42 /* Pointer store. */
43 /* Length store. */
46 db_recno_t l1;
48 if (isemptyp != NULL)
49 *isemptyp = 0;
51 /* If the line exists, simply return it. */
52 if (!db_get(sp, lno, 0, pp, lenp))
53 return (0);
56 * If the user asked for line 0 or line 1, i.e. the only possible
57 * line in an empty file, find the last line of the file; db_last
58 * fails loudly.
60 if ((lno == 0 || lno == 1) && db_last(sp, &l1))
61 return (1);
63 /* If the file isn't empty, fail loudly. */
64 if ((lno != 0 && lno != 1) || l1 != 0) {
65 db_err(sp, lno);
66 return (1);
69 if (isemptyp != NULL)
70 *isemptyp = 1;
72 return (1);
76 * db_get --
77 * Look in the text buffers for a line, followed by the cache, followed
78 * by the database.
80 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
82 int
83 db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
84 /* Line number. */ /* Pointer store. */ /* Length store. */
86 DBT data, key;
87 EXF *ep;
88 TEXT *tp;
89 db_recno_t l1, l2;
90 CHAR_T *wp;
91 size_t wlen;
92 size_t nlen;
95 * The underlying recno stuff handles zero by returning NULL, but
96 * have to have an OOB condition for the look-aside into the input
97 * buffer anyway.
99 if (lno == 0)
100 goto err1;
102 /* Check for no underlying file. */
103 if ((ep = sp->ep) == NULL) {
104 ex_emsg(sp, NULL, EXM_NOFILEYET);
105 goto err3;
108 if (LF_ISSET(DBG_NOCACHE))
109 goto nocache;
112 * Look-aside into the TEXT buffers and see if the line we want
113 * is there.
115 if (F_ISSET(sp, SC_TINPUT)) {
116 l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
117 l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
118 if (l1 <= lno && l2 >= lno) {
119 #if defined(DEBUG) && 0
120 vtrace(sp,
121 "retrieve TEXT buffer line %lu\n", (u_long)lno);
122 #endif
123 for (tp = sp->tiq.cqh_first;
124 tp->lno != lno; tp = tp->q.cqe_next);
125 if (lenp != NULL)
126 *lenp = tp->len;
127 if (pp != NULL)
128 *pp = tp->lb;
129 return (0);
132 * Adjust the line number for the number of lines used
133 * by the text input buffers.
135 if (lno > l2)
136 lno -= l2 - l1;
139 /* Look-aside into the cache, and see if the line we want is there. */
141 * Line cache will not work if different screens view the same
142 * file with different encodings.
143 * Multiple threads accessing the same cache can be a problem as
144 * well.
145 * So, line cache is (temporarily) disabled.
147 if (lno == sp->c_lno) {
148 #if defined(DEBUG) && 0
149 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
150 #endif
151 if (lenp != NULL)
152 *lenp = sp->c_len;
153 if (pp != NULL)
154 *pp = sp->c_lp;
155 return (0);
157 sp->c_lno = OOBLNO;
159 nocache:
160 nlen = 1024;
161 retry:
162 /* data.size contains length in bytes */
163 BINC_GOTO(sp, (char *)sp->c_lp, sp->c_blen, nlen);
165 /* Get the line from the underlying database. */
166 memset(&key, 0, sizeof(key));
167 key.data = &lno;
168 key.size = sizeof(lno);
169 memset(&data, 0, sizeof(data));
170 data.data = sp->c_lp;
171 data.ulen = sp->c_blen;
172 data.flags = DB_DBT_USERMEM;
173 switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
174 case ENOMEM:
175 nlen = data.size;
176 goto retry;
177 default:
178 goto err2;
179 case DB_NOTFOUND:
180 err1: if (LF_ISSET(DBG_FATAL))
181 err2: db_err(sp, lno);
182 alloc_err:
183 err3: if (lenp != NULL)
184 *lenp = 0;
185 if (pp != NULL)
186 *pp = NULL;
187 return (1);
188 case 0:
192 if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
193 if (!F_ISSET(sp, SC_CONV_ERROR)) {
194 F_SET(sp, SC_CONV_ERROR);
195 msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
197 goto err3;
200 /* Reset the cache. */
201 if (wp != data.data) {
202 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
203 MEMCPYW(sp->c_lp, wp, wlen);
205 sp->c_lno = lno;
206 sp->c_len = wlen;
208 #if defined(DEBUG) && 0
209 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
210 #endif
211 if (lenp != NULL)
212 *lenp = wlen;
213 if (pp != NULL)
214 *pp = sp->c_lp;
215 return (0);
219 * db_delete --
220 * Delete a line from the file.
222 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
225 db_delete(SCR *sp, db_recno_t lno)
227 DBT key;
228 EXF *ep;
229 SCR* scrp;
231 #if defined(DEBUG) && 0
232 vtrace(sp, "delete line %lu\n", (u_long)lno);
233 #endif
234 /* Check for no underlying file. */
235 if ((ep = sp->ep) == NULL) {
236 ex_emsg(sp, NULL, EXM_NOFILEYET);
237 return (1);
239 if (ep->l_win && ep->l_win != sp->wp) {
240 ex_emsg(sp, NULL, EXM_LOCKED);
241 return 1;
244 /* Update marks, @ and global commands. */
245 if (mark_insdel(sp, LINE_DELETE, lno))
246 return (1);
247 if (ex_g_insdel(sp, LINE_DELETE, lno))
248 return (1);
250 /* Log change. */
251 log_line(sp, lno, LOG_LINE_DELETE);
253 /* Update file. */
254 memset(&key, 0, sizeof(key));
255 key.data = &lno;
256 key.size = sizeof(lno);
257 if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
258 msgq(sp, M_DBERR, "003|unable to delete line %lu",
259 (u_long)lno);
260 return (1);
263 /* Flush the cache, update line count, before screen update. */
264 for (scrp = ep->scrq.cqh_first; scrp != (void *)&ep->scrq;
265 scrp = scrp->eq.cqe_next)
266 if (lno <= scrp->c_lno)
267 scrp->c_lno = OOBLNO;
268 if (ep->c_nlines != OOBLNO)
269 --ep->c_nlines;
271 /* File now modified. */
272 if (F_ISSET(ep, F_FIRSTMODIFY))
273 (void)rcv_init(sp);
274 F_SET(ep, F_MODIFIED);
276 /* Update screen. */
277 return (scr_update(sp, lno, LINE_DELETE, 1));
280 /* maybe this could be simpler
282 * DB3 behaves differently from DB1
284 * if lno != 0 just go to lno and put the new line after it
285 * if lno == 0 then if there are any record, put in front of the first
286 * otherwise just append to the end thus creating the first
287 * line
289 static int
290 append(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
292 DBT data, key;
293 DBC *dbcp_put;
294 EXF *ep;
295 char *fp;
296 size_t flen;
298 ep = sp->ep;
300 memset(&key, 0, sizeof(key));
301 key.data = &lno;
302 key.size = sizeof(lno);
303 memset(&data, 0, sizeof(data));
305 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp_put, 0)) != 0)
306 return 1;
308 INT2FILE(sp, p, len, fp, flen);
310 if (lno != 0) {
311 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0)
312 goto err2;
314 data.data = fp;
315 data.size = flen;
316 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_AFTER)) != 0) {
317 err2:
318 (void)dbcp_put->c_close(dbcp_put);
319 return (1);
321 } else {
322 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
323 if (sp->db_error != DB_NOTFOUND)
324 goto err2;
326 data.data = fp;
327 data.size = flen;
328 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
329 goto err2;
331 } else {
332 key.data = &lno;
333 key.size = sizeof(lno);
334 data.data = fp;
335 data.size = flen;
336 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
337 goto err2;
342 (void)dbcp_put->c_close(dbcp_put);
344 return 0;
348 * db_append --
349 * Append a line into the file.
351 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
354 db_append(SCR *sp, int update, db_recno_t lno, CHAR_T *p, size_t len)
356 EXF *ep;
357 int rval;
358 SCR* scrp;
360 #if defined(DEBUG) && 0
361 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
362 #endif
363 /* Check for no underlying file. */
364 if ((ep = sp->ep) == NULL) {
365 ex_emsg(sp, NULL, EXM_NOFILEYET);
366 return (1);
368 if (ep->l_win && ep->l_win != sp->wp) {
369 ex_emsg(sp, NULL, EXM_LOCKED);
370 return 1;
373 /* Update file. */
374 if (append(sp, lno, p, len) != 0) {
375 msgq(sp, M_DBERR, "004|unable to append to line %lu",
376 (u_long)lno);
377 return (1);
380 /* Flush the cache, update line count, before screen update. */
381 for (scrp = ep->scrq.cqh_first; scrp != (void *)&ep->scrq;
382 scrp = scrp->eq.cqe_next)
383 if (lno < scrp->c_lno)
384 scrp->c_lno = OOBLNO;
385 if (ep->c_nlines != OOBLNO)
386 ++ep->c_nlines;
388 /* File now dirty. */
389 if (F_ISSET(ep, F_FIRSTMODIFY))
390 (void)rcv_init(sp);
391 F_SET(ep, F_MODIFIED);
393 /* Log change. */
394 log_line(sp, lno + 1, LOG_LINE_APPEND);
396 /* Update marks, @ and global commands. */
397 rval = 0;
398 if (mark_insdel(sp, LINE_INSERT, lno + 1))
399 rval = 1;
400 if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
401 rval = 1;
404 * Update screen.
406 * XXX
407 * Nasty hack. If multiple lines are input by the user, they aren't
408 * committed until an <ESC> is entered. The problem is the screen was
409 * updated/scrolled as each line was entered. So, when this routine
410 * is called to copy the new lines from the cut buffer into the file,
411 * it has to know not to update the screen again.
413 return (scr_update(sp, lno, LINE_APPEND, update) || rval);
417 * db_insert --
418 * Insert a line into the file.
420 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
423 db_insert(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
425 DBT data, key;
426 DBC *dbcp_put;
427 EXF *ep;
428 int rval;
429 SCR* scrp;
431 #if defined(DEBUG) && 0
432 vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
433 (u_long)lno, (u_long)len, MIN(len, 20), p);
434 #endif
435 /* Check for no underlying file. */
436 if ((ep = sp->ep) == NULL) {
437 ex_emsg(sp, NULL, EXM_NOFILEYET);
438 return (1);
440 if (ep->l_win && ep->l_win != sp->wp) {
441 ex_emsg(sp, NULL, EXM_LOCKED);
442 return 1;
445 /* Update file. */
446 if (append(sp, lno - 1, p, len) != 0) {
447 msgq(sp, M_DBERR, "005|unable to insert at line %lu",
448 (u_long)lno);
449 return (1);
452 /* Flush the cache, update line count, before screen update. */
453 for (scrp = ep->scrq.cqh_first; scrp != (void *)&ep->scrq;
454 scrp = scrp->eq.cqe_next)
455 if (lno >= scrp->c_lno)
456 scrp->c_lno = OOBLNO;
457 if (ep->c_nlines != OOBLNO)
458 ++ep->c_nlines;
460 /* File now dirty. */
461 if (F_ISSET(ep, F_FIRSTMODIFY))
462 (void)rcv_init(sp);
463 F_SET(ep, F_MODIFIED);
465 /* Log change. */
466 log_line(sp, lno, LOG_LINE_INSERT);
468 /* Update marks, @ and global commands. */
469 rval = 0;
470 if (mark_insdel(sp, LINE_INSERT, lno))
471 rval = 1;
472 if (ex_g_insdel(sp, LINE_INSERT, lno))
473 rval = 1;
475 /* Update screen. */
476 return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
480 * db_set --
481 * Store a line in the file.
483 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
486 db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
488 DBT data, key;
489 EXF *ep;
490 char *fp;
491 size_t flen;
492 SCR* scrp;
494 #if defined(DEBUG) && 0
495 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
496 (u_long)lno, (u_long)len, MIN(len, 20), p);
497 #endif
498 /* Check for no underlying file. */
499 if ((ep = sp->ep) == NULL) {
500 ex_emsg(sp, NULL, EXM_NOFILEYET);
501 return (1);
503 if (ep->l_win && ep->l_win != sp->wp) {
504 ex_emsg(sp, NULL, EXM_LOCKED);
505 return 1;
508 /* Log before change. */
509 log_line(sp, lno, LOG_LINE_RESET_B);
511 INT2FILE(sp, p, len, fp, flen);
513 /* Update file. */
514 memset(&key, 0, sizeof(key));
515 key.data = &lno;
516 key.size = sizeof(lno);
517 memset(&data, 0, sizeof(data));
518 data.data = fp;
519 data.size = flen;
520 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
521 msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
522 return (1);
525 /* Flush the cache, before logging or screen update. */
526 for (scrp = ep->scrq.cqh_first; scrp != (void *)&ep->scrq;
527 scrp = scrp->eq.cqe_next)
528 if (lno == scrp->c_lno)
529 scrp->c_lno = OOBLNO;
531 /* File now dirty. */
532 if (F_ISSET(ep, F_FIRSTMODIFY))
533 (void)rcv_init(sp);
534 F_SET(ep, F_MODIFIED);
536 /* Log after change. */
537 log_line(sp, lno, LOG_LINE_RESET_F);
539 /* Update screen. */
540 return (scr_update(sp, lno, LINE_RESET, 1));
544 * db_exist --
545 * Return if a line exists.
547 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
550 db_exist(SCR *sp, db_recno_t lno)
552 EXF *ep;
554 /* Check for no underlying file. */
555 if ((ep = sp->ep) == NULL) {
556 ex_emsg(sp, NULL, EXM_NOFILEYET);
557 return (1);
560 if (lno == OOBLNO)
561 return (0);
564 * Check the last-line number cache. Adjust the cached line
565 * number for the lines used by the text input buffers.
567 if (ep->c_nlines != OOBLNO)
568 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
569 ep->c_nlines + (((TEXT *)sp->tiq.cqh_last)->lno -
570 ((TEXT *)sp->tiq.cqh_first)->lno) : ep->c_nlines));
572 /* Go get the line. */
573 return (!db_get(sp, lno, 0, NULL, NULL));
577 * db_last --
578 * Return the number of lines in the file.
580 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
583 db_last(SCR *sp, db_recno_t *lnop)
585 DBT data, key;
586 DBC *dbcp;
587 EXF *ep;
588 db_recno_t lno;
589 CHAR_T *wp;
590 size_t wlen;
592 /* Check for no underlying file. */
593 if ((ep = sp->ep) == NULL) {
594 ex_emsg(sp, NULL, EXM_NOFILEYET);
595 return (1);
599 * Check the last-line number cache. Adjust the cached line
600 * number for the lines used by the text input buffers.
602 if (ep->c_nlines != OOBLNO) {
603 *lnop = ep->c_nlines;
604 if (F_ISSET(sp, SC_TINPUT))
605 *lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
606 ((TEXT *)sp->tiq.cqh_first)->lno;
607 return (0);
610 memset(&key, 0, sizeof(key));
611 key.data = &lno;
612 key.size = sizeof(lno);
613 memset(&data, 0, sizeof(data));
615 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
616 goto err1;
617 switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
618 case DB_NOTFOUND:
619 *lnop = 0;
620 return (0);
621 default:
622 (void)dbcp->c_close(dbcp);
623 alloc_err:
624 err1:
625 msgq(sp, M_DBERR, "007|unable to get last line");
626 *lnop = 0;
627 return (1);
628 case 0:
632 memcpy(&lno, key.data, sizeof(lno));
634 if (lno != sp->c_lno) {
635 FILE2INT(sp, data.data, data.size, wp, wlen);
637 /* Fill the cache. */
638 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
639 MEMCPYW(sp->c_lp, wp, wlen);
640 sp->c_lno = lno;
641 sp->c_len = wlen;
643 ep->c_nlines = lno;
645 (void)dbcp->c_close(dbcp);
647 /* Return the value. */
648 *lnop = (F_ISSET(sp, SC_TINPUT) &&
649 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
650 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
651 return (0);
655 * db_err --
656 * Report a line error.
658 * PUBLIC: void db_err __P((SCR *, db_recno_t));
660 void
661 db_err(SCR *sp, db_recno_t lno)
663 msgq(sp, M_ERR,
664 "008|Error: unable to retrieve line %lu", (u_long)lno);
668 * scr_update --
669 * Update all of the screens that are backed by the file that
670 * just changed.
672 static int
673 scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
675 EXF *ep;
676 SCR *tsp;
677 WIN *wp;
679 if (F_ISSET(sp, SC_EX))
680 return (0);
682 /* XXXX goes outside of window */
683 ep = sp->ep;
684 if (ep->refcnt != 1)
685 for (wp = sp->gp->dq.cqh_first; wp != (void *)&sp->gp->dq;
686 wp = wp->q.cqe_next)
687 for (tsp = wp->scrq.cqh_first;
688 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
689 if (sp != tsp && tsp->ep == ep)
690 if (vs_change(tsp, lno, op))
691 return (1);
692 return (current ? vs_change(sp, lno, op) : 0);