db_last doesn't get the line directly into the line buffer,
[nvi.git] / common / db.c
blobe50820ad7e004ed59be093295a2d76946f8966b8
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.41 2001/09/11 20:01:03 skimo Exp $ (Berkeley) $Date: 2001/09/11 20:01: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;
230 #if defined(DEBUG) && 0
231 vtrace(sp, "delete line %lu\n", (u_long)lno);
232 #endif
233 /* Check for no underlying file. */
234 if ((ep = sp->ep) == NULL) {
235 ex_emsg(sp, NULL, EXM_NOFILEYET);
236 return (1);
238 if (ep->l_win && ep->l_win != sp->wp) {
239 ex_emsg(sp, NULL, EXM_LOCKED);
240 return 1;
243 /* Update marks, @ and global commands. */
244 if (mark_insdel(sp, LINE_DELETE, lno))
245 return (1);
246 if (ex_g_insdel(sp, LINE_DELETE, lno))
247 return (1);
249 /* Log change. */
250 log_line(sp, lno, LOG_LINE_DELETE);
252 /* Update file. */
253 memset(&key, 0, sizeof(key));
254 key.data = &lno;
255 key.size = sizeof(lno);
256 if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
257 msgq(sp, M_DBERR, "003|unable to delete line %lu",
258 (u_long)lno);
259 return (1);
262 /* Flush the cache, update line count, before screen update. */
263 if (lno <= sp->c_lno)
264 sp->c_lno = OOBLNO;
265 if (ep->c_nlines != OOBLNO)
266 --ep->c_nlines;
268 /* File now modified. */
269 if (F_ISSET(ep, F_FIRSTMODIFY))
270 (void)rcv_init(sp);
271 F_SET(ep, F_MODIFIED);
273 /* Update screen. */
274 return (scr_update(sp, lno, LINE_DELETE, 1));
277 /* maybe this could be simpler
279 * DB3 behaves differently from DB1
281 * if lno != 0 just go to lno and put the new line after it
282 * if lno == 0 then if there are any record, put in front of the first
283 * otherwise just append to the end thus creating the first
284 * line
286 static int
287 append(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
289 DBT data, key;
290 DBC *dbcp_put;
291 EXF *ep;
292 char *fp;
293 size_t flen;
295 ep = sp->ep;
297 memset(&key, 0, sizeof(key));
298 key.data = &lno;
299 key.size = sizeof(lno);
300 memset(&data, 0, sizeof(data));
302 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp_put, 0)) != 0)
303 return 1;
305 INT2FILE(sp, p, len, fp, flen);
307 if (lno != 0) {
308 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0)
309 goto err2;
311 data.data = fp;
312 data.size = flen;
313 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_AFTER)) != 0) {
314 err2:
315 (void)dbcp_put->c_close(dbcp_put);
316 return (1);
318 } else {
319 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
320 if (sp->db_error != DB_NOTFOUND)
321 goto err2;
323 data.data = fp;
324 data.size = flen;
325 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
326 goto err2;
328 } else {
329 key.data = &lno;
330 key.size = sizeof(lno);
331 data.data = fp;
332 data.size = flen;
333 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
334 goto err2;
339 (void)dbcp_put->c_close(dbcp_put);
341 return 0;
345 * db_append --
346 * Append a line into the file.
348 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
351 db_append(SCR *sp, int update, db_recno_t lno, CHAR_T *p, size_t len)
353 EXF *ep;
354 int rval;
356 #if defined(DEBUG) && 0
357 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
358 #endif
359 /* Check for no underlying file. */
360 if ((ep = sp->ep) == NULL) {
361 ex_emsg(sp, NULL, EXM_NOFILEYET);
362 return (1);
364 if (ep->l_win && ep->l_win != sp->wp) {
365 ex_emsg(sp, NULL, EXM_LOCKED);
366 return 1;
369 /* Update file. */
370 if (append(sp, lno, p, len) != 0) {
371 msgq(sp, M_DBERR, "004|unable to append to line %lu",
372 (u_long)lno);
373 return (1);
376 /* Flush the cache, update line count, before screen update. */
377 if (lno < sp->c_lno)
378 sp->c_lno = OOBLNO;
379 if (ep->c_nlines != OOBLNO)
380 ++ep->c_nlines;
382 /* File now dirty. */
383 if (F_ISSET(ep, F_FIRSTMODIFY))
384 (void)rcv_init(sp);
385 F_SET(ep, F_MODIFIED);
387 /* Log change. */
388 log_line(sp, lno + 1, LOG_LINE_APPEND);
390 /* Update marks, @ and global commands. */
391 rval = 0;
392 if (mark_insdel(sp, LINE_INSERT, lno + 1))
393 rval = 1;
394 if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
395 rval = 1;
398 * Update screen.
400 * XXX
401 * Nasty hack. If multiple lines are input by the user, they aren't
402 * committed until an <ESC> is entered. The problem is the screen was
403 * updated/scrolled as each line was entered. So, when this routine
404 * is called to copy the new lines from the cut buffer into the file,
405 * it has to know not to update the screen again.
407 return (scr_update(sp, lno, LINE_APPEND, update) || rval);
411 * db_insert --
412 * Insert a line into the file.
414 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
417 db_insert(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
419 DBT data, key;
420 DBC *dbcp_put;
421 EXF *ep;
422 int rval;
424 #if defined(DEBUG) && 0
425 vtrace(sp, "insert before %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 /* Update file. */
439 if (append(sp, lno - 1, p, len) != 0) {
440 msgq(sp, M_DBERR, "005|unable to insert at line %lu",
441 (u_long)lno);
442 return (1);
445 /* Flush the cache, update line count, before screen update. */
446 if (lno >= sp->c_lno)
447 sp->c_lno = OOBLNO;
448 if (ep->c_nlines != OOBLNO)
449 ++ep->c_nlines;
451 /* File now dirty. */
452 if (F_ISSET(ep, F_FIRSTMODIFY))
453 (void)rcv_init(sp);
454 F_SET(ep, F_MODIFIED);
456 /* Log change. */
457 log_line(sp, lno, LOG_LINE_INSERT);
459 /* Update marks, @ and global commands. */
460 rval = 0;
461 if (mark_insdel(sp, LINE_INSERT, lno))
462 rval = 1;
463 if (ex_g_insdel(sp, LINE_INSERT, lno))
464 rval = 1;
466 /* Update screen. */
467 return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
471 * db_set --
472 * Store a line in the file.
474 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
477 db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
479 DBT data, key;
480 EXF *ep;
481 char *fp;
482 size_t flen;
483 SCR* scrp;
485 #if defined(DEBUG) && 0
486 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
487 (u_long)lno, (u_long)len, MIN(len, 20), p);
488 #endif
489 /* Check for no underlying file. */
490 if ((ep = sp->ep) == NULL) {
491 ex_emsg(sp, NULL, EXM_NOFILEYET);
492 return (1);
494 if (ep->l_win && ep->l_win != sp->wp) {
495 ex_emsg(sp, NULL, EXM_LOCKED);
496 return 1;
499 /* Log before change. */
500 log_line(sp, lno, LOG_LINE_RESET_B);
502 INT2FILE(sp, p, len, fp, flen);
504 /* Update file. */
505 memset(&key, 0, sizeof(key));
506 key.data = &lno;
507 key.size = sizeof(lno);
508 memset(&data, 0, sizeof(data));
509 data.data = fp;
510 data.size = flen;
511 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
512 msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
513 return (1);
516 /* Flush the cache, before logging or screen update. */
517 for (scrp = ep->scrq.cqh_first; scrp != (void *)&ep->scrq;
518 scrp = scrp->eq.cqe_next)
519 if (lno == scrp->c_lno)
520 scrp->c_lno = OOBLNO;
522 /* File now dirty. */
523 if (F_ISSET(ep, F_FIRSTMODIFY))
524 (void)rcv_init(sp);
525 F_SET(ep, F_MODIFIED);
527 /* Log after change. */
528 log_line(sp, lno, LOG_LINE_RESET_F);
530 /* Update screen. */
531 return (scr_update(sp, lno, LINE_RESET, 1));
535 * db_exist --
536 * Return if a line exists.
538 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
541 db_exist(SCR *sp, db_recno_t lno)
543 EXF *ep;
545 /* Check for no underlying file. */
546 if ((ep = sp->ep) == NULL) {
547 ex_emsg(sp, NULL, EXM_NOFILEYET);
548 return (1);
551 if (lno == OOBLNO)
552 return (0);
555 * Check the last-line number cache. Adjust the cached line
556 * number for the lines used by the text input buffers.
558 if (ep->c_nlines != OOBLNO)
559 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
560 ep->c_nlines + (((TEXT *)sp->tiq.cqh_last)->lno -
561 ((TEXT *)sp->tiq.cqh_first)->lno) : ep->c_nlines));
563 /* Go get the line. */
564 return (!db_get(sp, lno, 0, NULL, NULL));
568 * db_last --
569 * Return the number of lines in the file.
571 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
574 db_last(SCR *sp, db_recno_t *lnop)
576 DBT data, key;
577 DBC *dbcp;
578 EXF *ep;
579 db_recno_t lno;
580 CHAR_T *wp;
581 size_t wlen;
583 /* Check for no underlying file. */
584 if ((ep = sp->ep) == NULL) {
585 ex_emsg(sp, NULL, EXM_NOFILEYET);
586 return (1);
590 * Check the last-line number cache. Adjust the cached line
591 * number for the lines used by the text input buffers.
593 if (ep->c_nlines != OOBLNO) {
594 *lnop = ep->c_nlines;
595 if (F_ISSET(sp, SC_TINPUT))
596 *lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
597 ((TEXT *)sp->tiq.cqh_first)->lno;
598 return (0);
601 memset(&key, 0, sizeof(key));
602 key.data = &lno;
603 key.size = sizeof(lno);
604 memset(&data, 0, sizeof(data));
606 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
607 goto err1;
608 switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
609 case DB_NOTFOUND:
610 *lnop = 0;
611 return (0);
612 default:
613 (void)dbcp->c_close(dbcp);
614 alloc_err:
615 err1:
616 msgq(sp, M_DBERR, "007|unable to get last line");
617 *lnop = 0;
618 return (1);
619 case 0:
623 memcpy(&lno, key.data, sizeof(lno));
625 if (lno != sp->c_lno) {
626 FILE2INT(sp, data.data, data.size, wp, wlen);
628 /* Fill the cache. */
629 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
630 MEMCPYW(sp->c_lp, wp, wlen);
631 sp->c_lno = lno;
632 sp->c_len = wlen;
634 ep->c_nlines = lno;
636 (void)dbcp->c_close(dbcp);
638 /* Return the value. */
639 *lnop = (F_ISSET(sp, SC_TINPUT) &&
640 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
641 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
642 return (0);
646 * db_err --
647 * Report a line error.
649 * PUBLIC: void db_err __P((SCR *, db_recno_t));
651 void
652 db_err(SCR *sp, db_recno_t lno)
654 msgq(sp, M_ERR,
655 "008|Error: unable to retrieve line %lu", (u_long)lno);
659 * scr_update --
660 * Update all of the screens that are backed by the file that
661 * just changed.
663 static int
664 scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
666 EXF *ep;
667 SCR *tsp;
668 WIN *wp;
670 if (F_ISSET(sp, SC_EX))
671 return (0);
673 /* XXXX goes outside of window */
674 ep = sp->ep;
675 if (ep->refcnt != 1)
676 for (wp = sp->gp->dq.cqh_first; wp != (void *)&sp->gp->dq;
677 wp = wp->q.cqe_next)
678 for (tsp = wp->scrq.cqh_first;
679 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
680 if (sp != tsp && tsp->ep == ep)
681 if (vs_change(tsp, lno, op))
682 return (1);
683 return (current ? vs_change(sp, lno, op) : 0);