1 /* $NetBSD: db.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
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.
15 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";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
32 static int append
__P((SCR
*, db_recno_t
, const CHAR_T
*, size_t, lnop_t
, int));
36 * Front-end to db_get, special case handling for empty files.
38 * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
41 db_eget(SCR
*sp
, db_recno_t lno
, CHAR_T
**pp
, size_t *lenp
, int *isemptyp
)
53 /* If the line exists, simply return it. */
54 if (!db_get(sp
, lno
, 0, pp
, lenp
))
58 * If the user asked for line 0 or line 1, i.e. the only possible
59 * line in an empty file, find the last line of the file; db_last
62 if ((lno
== 0 || lno
== 1) && db_last(sp
, &l1
))
65 /* If the file isn't empty, fail loudly. */
66 if ((lno
!= 0 && lno
!= 1) || l1
!= 0) {
79 * Look in the text buffers for a line, followed by the cache, followed
82 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
85 db_get(SCR
*sp
, db_recno_t lno
, u_int32_t flags
, CHAR_T
**pp
, size_t *lenp
)
86 /* Line number. */ /* Pointer store. */ /* Length store. */
97 * The underlying recno stuff handles zero by returning NULL, but
98 * have to have an OOB condition for the look-aside into the input
104 /* Check for no underlying file. */
105 if ((ep
= sp
->ep
) == NULL
) {
106 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
110 if (LF_ISSET(DBG_NOCACHE
))
114 * Look-aside into the TEXT buffers and see if the line we want
117 if (F_ISSET(sp
, SC_TINPUT
)) {
118 l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
119 l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
120 if (l1
<= lno
&& l2
>= lno
) {
121 #if defined(DEBUG) && 0
123 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
125 for (tp
= sp
->tiq
.cqh_first
;
126 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
134 * Adjust the line number for the number of lines used
135 * by the text input buffers.
141 /* Look-aside into the cache, and see if the line we want is there. */
142 if (lno
== sp
->c_lno
) {
143 #if defined(DEBUG) && 0
144 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
157 /* data.size contains length in bytes */
158 BINC_GOTO(sp
, CHAR_T
, sp
->c_lp
, sp
->c_blen
, nlen
);
160 /* Get the line from the underlying database. */
161 memset(&key
, 0, sizeof(key
));
163 key
.size
= sizeof(lno
);
164 memset(&data
, 0, sizeof(data
));
165 data
.data
= sp
->c_lp
;
166 data
.ulen
= sp
->c_blen
;
167 data
.flags
= DB_DBT_USERMEM
;
168 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
169 case DB_BUFFER_SMALL
:
175 err1
: if (LF_ISSET(DBG_FATAL
))
176 err2
: db_err(sp
, lno
);
178 err3
: if (lenp
!= NULL
)
187 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
188 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
189 F_SET(sp
, SC_CONV_ERROR
);
190 msgq(sp
, M_ERR
, "324|Conversion error on line %d", lno
);
195 /* Reset the cache. */
196 if (wp
!= data
.data
) {
197 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
198 MEMCPYW(sp
->c_lp
, wp
, wlen
);
203 #if defined(DEBUG) && 0
204 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
215 * Delete a line from the file.
217 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
220 db_delete(SCR
*sp
, db_recno_t lno
)
225 #if defined(DEBUG) && 0
226 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
228 /* Check for no underlying file. */
229 if ((ep
= sp
->ep
) == NULL
) {
230 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
233 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
234 ex_emsg(sp
, NULL
, EXM_LOCKED
);
238 /* Update marks, @ and global commands. */
239 if (line_insdel(sp
, LINE_DELETE
, lno
))
242 /* Log before change. */
243 log_line(sp
, lno
, LOG_LINE_DELETE_B
);
246 memset(&key
, 0, sizeof(key
));
248 key
.size
= sizeof(lno
);
249 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
250 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
255 /* Flush the cache, update line count, before screen update. */
256 update_cache(sp
, LINE_DELETE
, lno
);
258 /* File now modified. */
259 if (F_ISSET(ep
, F_FIRSTMODIFY
))
261 F_SET(ep
, F_MODIFIED
);
263 /* Log after change. */
264 log_line(sp
, lno
, LOG_LINE_DELETE_F
);
267 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
270 /* maybe this could be simpler
272 * DB3 behaves differently from DB1
274 * if lno != 0 just go to lno and put the new line after it
275 * if lno == 0 then if there are any record, put in front of the first
276 * otherwise just append to the end thus creating the first
280 append(SCR
*sp
, db_recno_t lno
, const CHAR_T
*p
, size_t len
, lnop_t op
, int update
)
289 /* Check for no underlying file. */
290 if ((ep
= sp
->ep
) == NULL
) {
291 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
294 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
295 ex_emsg(sp
, NULL
, EXM_LOCKED
);
299 /* Log before change. */
300 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_B
);
303 memset(&key
, 0, sizeof(key
));
305 key
.size
= sizeof(lno
);
306 memset(&data
, 0, sizeof(data
));
308 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
311 INT2FILE(sp
, p
, len
, fp
, flen
);
314 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
317 data
.data
= __UNCONST(fp
);
319 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
321 (void)dbcp_put
->c_close(dbcp_put
);
324 ? "004|unable to append to line %lu"
325 : "005|unable to insert at line %lu",
330 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
331 if (sp
->db_error
!= DB_NOTFOUND
)
334 data
.data
= __UNCONST(fp
);
336 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
341 key
.size
= sizeof(lno
);
342 data
.data
= __UNCONST(fp
);
344 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
350 (void)dbcp_put
->c_close(dbcp_put
);
352 /* Flush the cache, update line count, before screen update. */
353 update_cache(sp
, LINE_INSERT
, lno
);
355 /* File now dirty. */
356 if (F_ISSET(ep
, F_FIRSTMODIFY
))
358 F_SET(ep
, F_MODIFIED
);
360 /* Log after change. */
361 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_F
);
363 /* Update marks, @ and global commands. */
364 rval
= line_insdel(sp
, LINE_INSERT
, lno
+ 1);
369 * comment copied from db_append
371 * Nasty hack. If multiple lines are input by the user, they aren't
372 * committed until an <ESC> is entered. The problem is the screen was
373 * updated/scrolled as each line was entered. So, when this routine
374 * is called to copy the new lines from the cut buffer into the file,
375 * it has to know not to update the screen again.
377 return (scr_update(sp
, lno
+ 1, LINE_INSERT
, update
) || rval
);
382 * Append a line into the file.
384 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
387 db_append(SCR
*sp
, int update
, db_recno_t lno
, const CHAR_T
*p
, size_t len
)
389 #if defined(DEBUG) && 0
390 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
394 return append(sp
, lno
, p
, len
, LINE_APPEND
, update
);
399 * Insert a line into the file.
401 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
404 db_insert(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
406 #if defined(DEBUG) && 0
407 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
408 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
410 return append(sp
, lno
- 1, p
, len
, LINE_INSERT
, 1);
415 * Store a line in the file.
417 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
420 db_set(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
427 #if defined(DEBUG) && 0
428 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
429 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
431 /* Check for no underlying file. */
432 if ((ep
= sp
->ep
) == NULL
) {
433 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
436 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
437 ex_emsg(sp
, NULL
, EXM_LOCKED
);
441 /* Log before change. */
442 log_line(sp
, lno
, LOG_LINE_RESET_B
);
444 INT2FILE(sp
, p
, len
, fp
, flen
);
447 memset(&key
, 0, sizeof(key
));
449 key
.size
= sizeof(lno
);
450 memset(&data
, 0, sizeof(data
));
451 data
.data
= __UNCONST(fp
);
453 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
454 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
458 /* Flush the cache, update line count, before screen update. */
459 update_cache(sp
, LINE_RESET
, lno
);
461 /* File now dirty. */
462 if (F_ISSET(ep
, F_FIRSTMODIFY
))
464 F_SET(ep
, F_MODIFIED
);
466 /* Log after change. */
467 log_line(sp
, lno
, LOG_LINE_RESET_F
);
470 return (scr_update(sp
, lno
, LINE_RESET
, 1));
475 * Return if a line exists.
477 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
480 db_exist(SCR
*sp
, db_recno_t lno
)
484 /* Check for no underlying file. */
485 if ((ep
= sp
->ep
) == NULL
) {
486 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
494 * Check the last-line number cache. Adjust the cached line
495 * number for the lines used by the text input buffers.
497 if (ep
->c_nlines
!= OOBLNO
)
498 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
499 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
500 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
502 /* Go get the line. */
503 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
508 * Return the number of lines in the file.
510 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
513 db_last(SCR
*sp
, db_recno_t
*lnop
)
522 /* Check for no underlying file. */
523 if ((ep
= sp
->ep
) == NULL
) {
524 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
529 * Check the last-line number cache. Adjust the cached line
530 * number for the lines used by the text input buffers.
532 if (ep
->c_nlines
!= OOBLNO
) {
533 *lnop
= ep
->c_nlines
;
534 if (F_ISSET(sp
, SC_TINPUT
))
535 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
536 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
540 memset(&key
, 0, sizeof(key
));
542 key
.size
= sizeof(lno
);
543 memset(&data
, 0, sizeof(data
));
545 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
547 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
552 (void)dbcp
->c_close(dbcp
);
555 msgq(sp
, M_DBERR
, "007|unable to get last line");
562 memcpy(&lno
, key
.data
, sizeof(lno
));
564 if (lno
!= sp
->c_lno
) {
565 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
567 /* Fill the cache. */
568 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
569 MEMCPYW(sp
->c_lp
, wp
, wlen
);
575 (void)dbcp
->c_close(dbcp
);
577 /* Return the value. */
578 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
579 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
580 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
586 * Report a line error.
588 * PUBLIC: void db_err __P((SCR *, db_recno_t));
591 db_err(SCR
*sp
, db_recno_t lno
)
594 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
599 * Update all of the screens that are backed by the file that
602 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
603 * PUBLIC: lnop_t op, int current));
606 scr_update(SCR
*sp
, db_recno_t lno
, lnop_t op
, int current
)
612 if (F_ISSET(sp
, SC_EX
))
615 /* XXXX goes outside of window */
618 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
620 for (tsp
= wp
->scrq
.cqh_first
;
621 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
622 if (sp
!= tsp
&& tsp
->ep
== ep
)
623 if (vs_change(tsp
, lno
, op
))
625 return (current
? vs_change(sp
, lno
, op
) : 0);
629 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
632 update_cache(SCR
*sp
, lnop_t op
, db_recno_t lno
)
639 /* Flush the cache, update line count, before screen update. */
640 /* The flushing is probably not needed, since it was incorrect
641 * for db_insert. It might be better to adjust it, like
642 * marks, @ and global
644 for (scrp
= ep
->scrq
.cqh_first
; scrp
!= (void *)&ep
->scrq
;
645 scrp
= scrp
->eq
.cqe_next
)
649 if (lno
<= scrp
->c_lno
)
650 scrp
->c_lno
= OOBLNO
;
653 if (lno
== scrp
->c_lno
)
654 scrp
->c_lno
= OOBLNO
;
660 if (ep
->c_nlines
!= OOBLNO
)
675 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
678 line_insdel(SCR
*sp
, lnop_t op
, db_recno_t lno
)
682 /* Update marks, @ and global commands. */
684 if (mark_insdel(sp
, op
, lno
))
686 if (ex_g_insdel(sp
, op
, lno
))