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.
13 static const char sccsid
[] = "$Id: db.c,v 10.46 2002/03/09 12:48:22 skimo Exp $ (Berkeley) $Date: 2002/03/09 12:48:22 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
29 static int append
__P((SCR
*, db_recno_t
, CHAR_T
*, size_t, lnop_t
, int));
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 *));
38 db_eget(SCR
*sp
, db_recno_t lno
, CHAR_T
**pp
, size_t *lenp
, int *isemptyp
)
50 /* If the line exists, simply return it. */
51 if (!db_get(sp
, lno
, 0, pp
, lenp
))
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
59 if ((lno
== 0 || lno
== 1) && db_last(sp
, &l1
))
62 /* If the file isn't empty, fail loudly. */
63 if ((lno
!= 0 && lno
!= 1) || l1
!= 0) {
76 * Look in the text buffers for a line, followed by the cache, followed
79 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
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. */
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
101 /* Check for no underlying file. */
102 if ((ep
= sp
->ep
) == NULL
) {
103 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
107 if (LF_ISSET(DBG_NOCACHE
))
111 * Look-aside into the TEXT buffers and see if the line we want
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
120 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
122 for (tp
= sp
->tiq
.cqh_first
;
123 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
131 * Adjust the line number for the number of lines used
132 * by the text input buffers.
138 /* Look-aside into the cache, and see if the line we want is there. */
140 * Line cache will not work if different screens view the same
141 * file with different encodings.
142 * Multiple threads accessing the same cache can be a problem as
144 * So, line cache is (temporarily) disabled.
146 if (lno
== sp
->c_lno
) {
147 #if defined(DEBUG) && 0
148 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
161 /* data.size contains length in bytes */
162 BINC_GOTO(sp
, (char *)sp
->c_lp
, sp
->c_blen
, nlen
);
164 /* Get the line from the underlying database. */
165 memset(&key
, 0, sizeof(key
));
167 key
.size
= sizeof(lno
);
168 memset(&data
, 0, sizeof(data
));
169 data
.data
= sp
->c_lp
;
170 data
.ulen
= sp
->c_blen
;
171 data
.flags
= DB_DBT_USERMEM
;
172 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
179 err1
: if (LF_ISSET(DBG_FATAL
))
180 err2
: db_err(sp
, lno
);
182 err3
: if (lenp
!= NULL
)
191 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
192 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
193 F_SET(sp
, SC_CONV_ERROR
);
194 msgq(sp
, M_ERR
, "324|Conversion error on line %d", lno
);
199 /* Reset the cache. */
200 if (wp
!= data
.data
) {
201 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
202 MEMCPYW(sp
->c_lp
, wp
, wlen
);
207 #if defined(DEBUG) && 0
208 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
219 * Delete a line from the file.
221 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
224 db_delete(SCR
*sp
, db_recno_t lno
)
229 #if defined(DEBUG) && 0
230 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
232 /* Check for no underlying file. */
233 if ((ep
= sp
->ep
) == NULL
) {
234 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
237 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
238 ex_emsg(sp
, NULL
, EXM_LOCKED
);
242 /* Update cache, marks, @ and global commands. */
243 if (line_insdel(sp
, LINE_DELETE
, lno
))
246 /* Log before change. */
247 log_line(sp
, lno
, LOG_LINE_DELETE_B
);
250 memset(&key
, 0, sizeof(key
));
252 key
.size
= sizeof(lno
);
253 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
254 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
259 /* File now modified. */
260 if (F_ISSET(ep
, F_FIRSTMODIFY
))
262 F_SET(ep
, F_MODIFIED
);
264 /* Log after change. */
265 log_line(sp
, lno
, LOG_LINE_DELETE_F
);
268 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
271 /* maybe this could be simpler
273 * DB3 behaves differently from DB1
275 * if lno != 0 just go to lno and put the new line after it
276 * if lno == 0 then if there are any record, put in front of the first
277 * otherwise just append to the end thus creating the first
281 append(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
, lnop_t op
, int update
)
290 /* Check for no underlying file. */
291 if ((ep
= sp
->ep
) == NULL
) {
292 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
295 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
296 ex_emsg(sp
, NULL
, EXM_LOCKED
);
300 /* Log before change. */
301 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_B
);
304 memset(&key
, 0, sizeof(key
));
306 key
.size
= sizeof(lno
);
307 memset(&data
, 0, sizeof(data
));
309 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
312 INT2FILE(sp
, p
, len
, fp
, flen
);
315 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
320 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
322 (void)dbcp_put
->c_close(dbcp_put
);
325 ? "004|unable to append to line %lu"
326 : "005|unable to insert at line %lu",
331 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
332 if (sp
->db_error
!= DB_NOTFOUND
)
337 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
342 key
.size
= sizeof(lno
);
345 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
351 (void)dbcp_put
->c_close(dbcp_put
);
353 /* File now dirty. */
354 if (F_ISSET(ep
, F_FIRSTMODIFY
))
356 F_SET(ep
, F_MODIFIED
);
358 /* Update cache, marks, @ and global commands. */
359 rval
= line_insdel(sp
, LINE_INSERT
, lno
+ 1);
361 /* Log after change. */
362 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_F
);
367 * comment copied from db_append
369 * Nasty hack. If multiple lines are input by the user, they aren't
370 * committed until an <ESC> is entered. The problem is the screen was
371 * updated/scrolled as each line was entered. So, when this routine
372 * is called to copy the new lines from the cut buffer into the file,
373 * it has to know not to update the screen again.
375 return (scr_update(sp
, lno
+ 1, LINE_INSERT
, update
) || rval
);
380 * Append a line into the file.
382 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
385 db_append(SCR
*sp
, int update
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
387 #if defined(DEBUG) && 0
388 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
392 return append(sp
, lno
, p
, len
, LINE_APPEND
, update
);
397 * Insert a line into the file.
399 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
402 db_insert(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
404 #if defined(DEBUG) && 0
405 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
406 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
408 return append(sp
, lno
- 1, p
, len
, LINE_INSERT
, 1);
413 * Store a line in the file.
415 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
418 db_set(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
426 #if defined(DEBUG) && 0
427 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
428 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
430 /* Check for no underlying file. */
431 if ((ep
= sp
->ep
) == NULL
) {
432 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
435 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
436 ex_emsg(sp
, NULL
, EXM_LOCKED
);
440 /* Log before change. */
441 log_line(sp
, lno
, LOG_LINE_RESET_B
);
443 INT2FILE(sp
, p
, len
, fp
, flen
);
446 memset(&key
, 0, sizeof(key
));
448 key
.size
= sizeof(lno
);
449 memset(&data
, 0, sizeof(data
));
452 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
453 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
457 /* File now dirty. */
458 if (F_ISSET(ep
, F_FIRSTMODIFY
))
460 F_SET(ep
, F_MODIFIED
);
462 /* Update cache, marks, @ and global commands. */
463 rval
= line_insdel(sp
, LINE_RESET
, lno
);
465 /* Log after change. */
466 log_line(sp
, lno
, LOG_LINE_RESET_F
);
469 return (scr_update(sp
, lno
, LINE_RESET
, 1) || rval
);
474 * Return if a line exists.
476 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
479 db_exist(SCR
*sp
, db_recno_t lno
)
483 /* Check for no underlying file. */
484 if ((ep
= sp
->ep
) == NULL
) {
485 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
493 * Check the last-line number cache. Adjust the cached line
494 * number for the lines used by the text input buffers.
496 if (ep
->c_nlines
!= OOBLNO
)
497 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
498 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
499 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
501 /* Go get the line. */
502 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
507 * Return the number of lines in the file.
509 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
512 db_last(SCR
*sp
, db_recno_t
*lnop
)
521 /* Check for no underlying file. */
522 if ((ep
= sp
->ep
) == NULL
) {
523 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
528 * Check the last-line number cache. Adjust the cached line
529 * number for the lines used by the text input buffers.
531 if (ep
->c_nlines
!= OOBLNO
) {
532 *lnop
= ep
->c_nlines
;
533 if (F_ISSET(sp
, SC_TINPUT
))
534 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
535 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
539 memset(&key
, 0, sizeof(key
));
541 key
.size
= sizeof(lno
);
542 memset(&data
, 0, sizeof(data
));
544 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
546 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
551 (void)dbcp
->c_close(dbcp
);
554 msgq(sp
, M_DBERR
, "007|unable to get last line");
561 memcpy(&lno
, key
.data
, sizeof(lno
));
563 if (lno
!= sp
->c_lno
) {
564 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
566 /* Fill the cache. */
567 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
568 MEMCPYW(sp
->c_lp
, wp
, wlen
);
574 (void)dbcp
->c_close(dbcp
);
576 /* Return the value. */
577 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
578 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
579 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
585 * Report a line error.
587 * PUBLIC: void db_err __P((SCR *, db_recno_t));
590 db_err(SCR
*sp
, db_recno_t lno
)
593 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
598 * Update all of the screens that are backed by the file that
601 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
602 * PUBLIC: lnop_t op, int current));
605 scr_update(SCR
*sp
, db_recno_t lno
, lnop_t op
, int current
)
611 if (F_ISSET(sp
, SC_EX
))
614 /* XXXX goes outside of window */
617 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
619 for (tsp
= wp
->scrq
.cqh_first
;
620 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
621 if (sp
!= tsp
&& tsp
->ep
== ep
)
622 if (vs_change(tsp
, lno
, op
))
624 return (current
? vs_change(sp
, lno
, op
) : 0);
628 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
631 line_insdel(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
;
658 if (ep
->c_nlines
!= OOBLNO
)
668 /* Update marks, @ and global commands. */
670 if (mark_insdel(sp
, op
, lno
))
672 if (ex_g_insdel(sp
, op
, lno
))