83f8578be4de69f27b6e1032dc665adc9be813c1
[nvi.git] / common / db.c
blob83f8578be4de69f27b6e1032dc665adc9be813c1
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.48 2002/06/08 19:32:52 skimo Exp $ (Berkeley) $Date: 2002/06/08 19:32:52 $";
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 append __P((SCR*, db_recno_t, CHAR_T*, size_t, lnop_t, int));
32 * db_eget --
33 * Front-end to db_get, special case handling for empty files.
35 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
37 int
38 db_eget(SCR *sp, db_recno_t lno, CHAR_T **pp, size_t *lenp, int *isemptyp)
40 /* Line number. */
41 /* Pointer store. */
42 /* Length store. */
45 db_recno_t l1;
47 if (isemptyp != NULL)
48 *isemptyp = 0;
50 /* If the line exists, simply return it. */
51 if (!db_get(sp, lno, 0, pp, lenp))
52 return (0);
55 * If the user asked for line 0 or line 1, i.e. the only possible
56 * line in an empty file, find the last line of the file; db_last
57 * fails loudly.
59 if ((lno == 0 || lno == 1) && db_last(sp, &l1))
60 return (1);
62 /* If the file isn't empty, fail loudly. */
63 if ((lno != 0 && lno != 1) || l1 != 0) {
64 db_err(sp, lno);
65 return (1);
68 if (isemptyp != NULL)
69 *isemptyp = 1;
71 return (1);
75 * db_get --
76 * Look in the text buffers for a line, followed by the cache, followed
77 * by the database.
79 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
81 int
82 db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
83 /* Line number. */ /* Pointer store. */ /* Length store. */
85 DBT data, key;
86 EXF *ep;
87 TEXT *tp;
88 db_recno_t l1, l2;
89 CHAR_T *wp;
90 size_t wlen;
91 size_t nlen;
94 * The underlying recno stuff handles zero by returning NULL, but
95 * have to have an OOB condition for the look-aside into the input
96 * buffer anyway.
98 if (lno == 0)
99 goto err1;
101 /* Check for no underlying file. */
102 if ((ep = sp->ep) == NULL) {
103 ex_emsg(sp, NULL, EXM_NOFILEYET);
104 goto err3;
107 if (LF_ISSET(DBG_NOCACHE))
108 goto nocache;
111 * Look-aside into the TEXT buffers and see if the line we want
112 * is there.
114 if (F_ISSET(sp, SC_TINPUT)) {
115 l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
116 l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
117 if (l1 <= lno && l2 >= lno) {
118 #if defined(DEBUG) && 0
119 vtrace(sp,
120 "retrieve TEXT buffer line %lu\n", (u_long)lno);
121 #endif
122 for (tp = sp->tiq.cqh_first;
123 tp->lno != lno; tp = tp->q.cqe_next);
124 if (lenp != NULL)
125 *lenp = tp->len;
126 if (pp != NULL)
127 *pp = tp->lb;
128 return (0);
131 * Adjust the line number for the number of lines used
132 * by the text input buffers.
134 if (lno > l2)
135 lno -= l2 - l1;
138 /* Look-aside into the cache, and see if the line we want is there. */
139 if (lno == sp->c_lno) {
140 #if defined(DEBUG) && 0
141 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
142 #endif
143 if (lenp != NULL)
144 *lenp = sp->c_len;
145 if (pp != NULL)
146 *pp = sp->c_lp;
147 return (0);
149 sp->c_lno = OOBLNO;
151 nocache:
152 nlen = 1024;
153 retry:
154 /* data.size contains length in bytes */
155 BINC_GOTO(sp, CHAR_T, sp->c_lp, sp->c_blen, nlen);
157 /* Get the line from the underlying database. */
158 memset(&key, 0, sizeof(key));
159 key.data = &lno;
160 key.size = sizeof(lno);
161 memset(&data, 0, sizeof(data));
162 data.data = sp->c_lp;
163 data.ulen = sp->c_blen;
164 data.flags = DB_DBT_USERMEM;
165 switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
166 case DB_BUFFER_SMALL:
167 nlen = data.size;
168 goto retry;
169 default:
170 goto err2;
171 case DB_NOTFOUND:
172 err1: if (LF_ISSET(DBG_FATAL))
173 err2: db_err(sp, lno);
174 alloc_err:
175 err3: if (lenp != NULL)
176 *lenp = 0;
177 if (pp != NULL)
178 *pp = NULL;
179 return (1);
180 case 0:
184 if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
185 if (!F_ISSET(sp, SC_CONV_ERROR)) {
186 F_SET(sp, SC_CONV_ERROR);
187 msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
189 goto err3;
192 /* Reset the cache. */
193 if (wp != data.data) {
194 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
195 MEMCPYW(sp->c_lp, wp, wlen);
197 sp->c_lno = lno;
198 sp->c_len = wlen;
200 #if defined(DEBUG) && 0
201 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
202 #endif
203 if (lenp != NULL)
204 *lenp = wlen;
205 if (pp != NULL)
206 *pp = sp->c_lp;
207 return (0);
211 * db_delete --
212 * Delete a line from the file.
214 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
217 db_delete(SCR *sp, db_recno_t lno)
219 DBT key;
220 EXF *ep;
222 #if defined(DEBUG) && 0
223 vtrace(sp, "delete line %lu\n", (u_long)lno);
224 #endif
225 /* Check for no underlying file. */
226 if ((ep = sp->ep) == NULL) {
227 ex_emsg(sp, NULL, EXM_NOFILEYET);
228 return (1);
230 if (ep->l_win && ep->l_win != sp->wp) {
231 ex_emsg(sp, NULL, EXM_LOCKED);
232 return 1;
235 /* Update marks, @ and global commands. */
236 if (line_insdel(sp, LINE_DELETE, lno))
237 return 1;
239 /* Log before change. */
240 log_line(sp, lno, LOG_LINE_DELETE_B);
242 /* Update file. */
243 memset(&key, 0, sizeof(key));
244 key.data = &lno;
245 key.size = sizeof(lno);
246 if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
247 msgq(sp, M_DBERR, "003|unable to delete line %lu",
248 (u_long)lno);
249 return (1);
252 /* Flush the cache, update line count, before screen update. */
253 update_cache(sp, LINE_DELETE, lno);
255 /* File now modified. */
256 if (F_ISSET(ep, F_FIRSTMODIFY))
257 (void)rcv_init(sp);
258 F_SET(ep, F_MODIFIED);
260 /* Log after change. */
261 log_line(sp, lno, LOG_LINE_DELETE_F);
263 /* Update screen. */
264 return (scr_update(sp, lno, LINE_DELETE, 1));
267 /* maybe this could be simpler
269 * DB3 behaves differently from DB1
271 * if lno != 0 just go to lno and put the new line after it
272 * if lno == 0 then if there are any record, put in front of the first
273 * otherwise just append to the end thus creating the first
274 * line
276 static int
277 append(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len, lnop_t op, int update)
279 DBT data, key;
280 DBC *dbcp_put;
281 EXF *ep;
282 char *fp;
283 size_t flen;
284 int rval;
286 /* Check for no underlying file. */
287 if ((ep = sp->ep) == NULL) {
288 ex_emsg(sp, NULL, EXM_NOFILEYET);
289 return (1);
291 if (ep->l_win && ep->l_win != sp->wp) {
292 ex_emsg(sp, NULL, EXM_LOCKED);
293 return 1;
296 /* Log before change. */
297 log_line(sp, lno + 1, LOG_LINE_APPEND_B);
299 /* Update file. */
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 msgq(sp, M_DBERR,
320 (op == LINE_APPEND)
321 ? "004|unable to append to line %lu"
322 : "005|unable to insert at line %lu",
323 (u_long)lno);
324 return (1);
326 } else {
327 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
328 if (sp->db_error != DB_NOTFOUND)
329 goto err2;
331 data.data = fp;
332 data.size = flen;
333 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
334 goto err2;
336 } else {
337 key.data = &lno;
338 key.size = sizeof(lno);
339 data.data = fp;
340 data.size = flen;
341 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
342 goto err2;
347 (void)dbcp_put->c_close(dbcp_put);
349 /* Flush the cache, update line count, before screen update. */
350 update_cache(sp, LINE_INSERT, lno);
352 /* File now dirty. */
353 if (F_ISSET(ep, F_FIRSTMODIFY))
354 (void)rcv_init(sp);
355 F_SET(ep, F_MODIFIED);
357 /* Log after change. */
358 log_line(sp, lno + 1, LOG_LINE_APPEND_F);
360 /* Update marks, @ and global commands. */
361 rval = line_insdel(sp, LINE_INSERT, lno + 1);
364 * Update screen.
366 * comment copied from db_append
367 * XXX
368 * Nasty hack. If multiple lines are input by the user, they aren't
369 * committed until an <ESC> is entered. The problem is the screen was
370 * updated/scrolled as each line was entered. So, when this routine
371 * is called to copy the new lines from the cut buffer into the file,
372 * it has to know not to update the screen again.
374 return (scr_update(sp, lno + 1, LINE_INSERT, update) || rval);
378 * db_append --
379 * Append a line into the file.
381 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
384 db_append(SCR *sp, int update, db_recno_t lno, CHAR_T *p, size_t len)
386 #if defined(DEBUG) && 0
387 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
388 #endif
390 /* Update file. */
391 return append(sp, lno, p, len, LINE_APPEND, update);
395 * db_insert --
396 * Insert a line into the file.
398 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
401 db_insert(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
403 #if defined(DEBUG) && 0
404 vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
405 (u_long)lno, (u_long)len, MIN(len, 20), p);
406 #endif
407 return append(sp, lno - 1, p, len, LINE_INSERT, 1);
411 * db_set --
412 * Store a line in the file.
414 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
417 db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
419 DBT data, key;
420 EXF *ep;
421 char *fp;
422 size_t flen;
424 #if defined(DEBUG) && 0
425 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
426 (u_long)lno, (u_long)len, MIN(len, 20), p);
427 #endif
428 /* Check for no underlying file. */
429 if ((ep = sp->ep) == NULL) {
430 ex_emsg(sp, NULL, EXM_NOFILEYET);
431 return (1);
433 if (ep->l_win && ep->l_win != sp->wp) {
434 ex_emsg(sp, NULL, EXM_LOCKED);
435 return 1;
438 /* Log before change. */
439 log_line(sp, lno, LOG_LINE_RESET_B);
441 INT2FILE(sp, p, len, fp, flen);
443 /* Update file. */
444 memset(&key, 0, sizeof(key));
445 key.data = &lno;
446 key.size = sizeof(lno);
447 memset(&data, 0, sizeof(data));
448 data.data = fp;
449 data.size = flen;
450 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
451 msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
452 return (1);
455 /* Flush the cache, update line count, before screen update. */
456 update_cache(sp, LINE_RESET, lno);
458 /* File now dirty. */
459 if (F_ISSET(ep, F_FIRSTMODIFY))
460 (void)rcv_init(sp);
461 F_SET(ep, F_MODIFIED);
463 /* Log after change. */
464 log_line(sp, lno, LOG_LINE_RESET_F);
466 /* Update screen. */
467 return (scr_update(sp, lno, LINE_RESET, 1));
471 * db_exist --
472 * Return if a line exists.
474 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
477 db_exist(SCR *sp, db_recno_t lno)
479 EXF *ep;
481 /* Check for no underlying file. */
482 if ((ep = sp->ep) == NULL) {
483 ex_emsg(sp, NULL, EXM_NOFILEYET);
484 return (1);
487 if (lno == OOBLNO)
488 return (0);
491 * Check the last-line number cache. Adjust the cached line
492 * number for the lines used by the text input buffers.
494 if (ep->c_nlines != OOBLNO)
495 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
496 ep->c_nlines + (((TEXT *)sp->tiq.cqh_last)->lno -
497 ((TEXT *)sp->tiq.cqh_first)->lno) : ep->c_nlines));
499 /* Go get the line. */
500 return (!db_get(sp, lno, 0, NULL, NULL));
504 * db_last --
505 * Return the number of lines in the file.
507 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
510 db_last(SCR *sp, db_recno_t *lnop)
512 DBT data, key;
513 DBC *dbcp;
514 EXF *ep;
515 db_recno_t lno;
516 CHAR_T *wp;
517 size_t wlen;
519 /* Check for no underlying file. */
520 if ((ep = sp->ep) == NULL) {
521 ex_emsg(sp, NULL, EXM_NOFILEYET);
522 return (1);
526 * Check the last-line number cache. Adjust the cached line
527 * number for the lines used by the text input buffers.
529 if (ep->c_nlines != OOBLNO) {
530 *lnop = ep->c_nlines;
531 if (F_ISSET(sp, SC_TINPUT))
532 *lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
533 ((TEXT *)sp->tiq.cqh_first)->lno;
534 return (0);
537 memset(&key, 0, sizeof(key));
538 key.data = &lno;
539 key.size = sizeof(lno);
540 memset(&data, 0, sizeof(data));
542 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
543 goto err1;
544 switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
545 case DB_NOTFOUND:
546 *lnop = 0;
547 return (0);
548 default:
549 (void)dbcp->c_close(dbcp);
550 alloc_err:
551 err1:
552 msgq(sp, M_DBERR, "007|unable to get last line");
553 *lnop = 0;
554 return (1);
555 case 0:
559 memcpy(&lno, key.data, sizeof(lno));
561 if (lno != sp->c_lno) {
562 FILE2INT(sp, data.data, data.size, wp, wlen);
564 /* Fill the cache. */
565 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
566 MEMCPYW(sp->c_lp, wp, wlen);
567 sp->c_lno = lno;
568 sp->c_len = wlen;
570 ep->c_nlines = lno;
572 (void)dbcp->c_close(dbcp);
574 /* Return the value. */
575 *lnop = (F_ISSET(sp, SC_TINPUT) &&
576 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
577 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
578 return (0);
582 * db_err --
583 * Report a line error.
585 * PUBLIC: void db_err __P((SCR *, db_recno_t));
587 void
588 db_err(SCR *sp, db_recno_t lno)
590 msgq(sp, M_ERR,
591 "008|Error: unable to retrieve line %lu", (u_long)lno);
595 * scr_update --
596 * Update all of the screens that are backed by the file that
597 * just changed.
599 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
600 * PUBLIC: lnop_t op, int current));
603 scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
605 EXF *ep;
606 SCR *tsp;
607 WIN *wp;
609 if (F_ISSET(sp, SC_EX))
610 return (0);
612 /* XXXX goes outside of window */
613 ep = sp->ep;
614 if (ep->refcnt != 1)
615 for (wp = sp->gp->dq.cqh_first; wp != (void *)&sp->gp->dq;
616 wp = wp->q.cqe_next)
617 for (tsp = wp->scrq.cqh_first;
618 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
619 if (sp != tsp && tsp->ep == ep)
620 if (vs_change(tsp, lno, op))
621 return (1);
622 return (current ? vs_change(sp, lno, op) : 0);
626 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
628 void
629 update_cache(SCR *sp, lnop_t op, db_recno_t lno)
631 SCR* scrp;
632 EXF *ep;
634 ep = sp->ep;
636 /* Flush the cache, update line count, before screen update. */
637 /* The flushing is probably not needed, since it was incorrect
638 * for db_insert. It might be better to adjust it, like
639 * marks, @ and global
641 for (scrp = ep->scrq.cqh_first; scrp != (void *)&ep->scrq;
642 scrp = scrp->eq.cqe_next)
643 switch (op) {
644 case LINE_INSERT:
645 case LINE_DELETE:
646 if (lno <= scrp->c_lno)
647 scrp->c_lno = OOBLNO;
648 break;
649 case LINE_RESET:
650 if (lno == scrp->c_lno)
651 scrp->c_lno = OOBLNO;
652 break;
655 if (ep->c_nlines != OOBLNO)
656 switch (op) {
657 case LINE_INSERT:
658 ++ep->c_nlines;
659 break;
660 case LINE_DELETE:
661 --ep->c_nlines;
662 break;
667 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
670 line_insdel(SCR *sp, lnop_t op, db_recno_t lno)
672 int rval;
674 /* Update marks, @ and global commands. */
675 rval = 0;
676 if (mark_insdel(sp, op, lno))
677 rval = 1;
678 if (ex_g_insdel(sp, op, lno))
679 rval = 1;
681 return rval;