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.31 2000/07/21 22:09:28 skimo Exp $ (Berkeley) $Date: 2000/07/21 22:09:28 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.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));
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 *));
39 db_eget(sp
, lno
, pp
, lenp
, isemptyp
)
41 db_recno_t lno
; /* Line number. */
42 CHAR_T
**pp
; /* Pointer store. */
43 size_t *lenp
; /* Length store. */
51 /* If the line exists, simply return it. */
52 if (!db_get(sp
, lno
, 0, pp
, lenp
))
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
60 if ((lno
== 0 || lno
== 1) && db_last(sp
, &l1
))
63 /* If the file isn't empty, fail loudly. */
64 if ((lno
!= 0 && lno
!= 1) || l1
!= 0) {
77 * Look in the text buffers for a line, followed by the cache, followed
80 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
83 db_get(sp
, lno
, flags
, pp
, lenp
)
85 db_recno_t lno
; /* Line number. */
87 CHAR_T
**pp
; /* Pointer store. */
88 size_t *lenp
; /* Length store. */
99 * The underlying recno stuff handles zero by returning NULL, but
100 * have to have an OOB condition for the look-aside into the input
106 /* Check for no underlying file. */
107 if ((ep
= sp
->ep
) == NULL
) {
108 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
112 if (LF_ISSET(DBG_NOCACHE
))
116 * Look-aside into the TEXT buffers and see if the line we want
119 if (F_ISSET(sp
, SC_TINPUT
)) {
120 l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
121 l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
122 if (l1
<= lno
&& l2
>= lno
) {
123 #if defined(DEBUG) && 0
125 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
127 for (tp
= sp
->tiq
.cqh_first
;
128 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
136 * Adjust the line number for the number of lines used
137 * by the text input buffers.
143 /* Look-aside into the cache, and see if the line we want is there. */
144 /* Disable cache for now */
145 if (0 && lno
== ep
->c_lno
) {
146 #if defined(DEBUG) && 0
147 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
160 BINC_GOTO(sp
, (char *)ep
->c_lp
, ep
->c_blen
, nlen
);
162 /* Get the line from the underlying database. */
163 memset(&key
, 0, sizeof(key
));
165 key
.size
= sizeof(lno
);
166 memset(&data
, 0, sizeof(data
));
167 data
.data
= ep
->c_lp
;
168 data
.ulen
= ep
->c_blen
;
169 data
.flags
= DB_DBT_USERMEM
;
170 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
177 err1
: if (LF_ISSET(DBG_FATAL
))
178 err2
: db_err(sp
, lno
);
180 err3
: if (lenp
!= NULL
)
189 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
191 /* Reset the cache. */
192 BINC_GOTOW(sp
, ep
->c_lp
, ep
->c_blen
, wlen
);
193 MEMCPYW(ep
->c_lp
, wp
, wlen
);
197 #if defined(DEBUG) && 0
198 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
209 * Delete a line from the file.
211 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
221 #if defined(DEBUG) && 0
222 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
224 /* Check for no underlying file. */
225 if ((ep
= sp
->ep
) == NULL
) {
226 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
230 /* Update marks, @ and global commands. */
231 if (mark_insdel(sp
, LINE_DELETE
, lno
))
233 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
237 log_line(sp
, lno
, LOG_LINE_DELETE
);
240 memset(&key
, 0, sizeof(key
));
242 key
.size
= sizeof(lno
);
243 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
244 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
249 /* Flush the cache, update line count, before screen update. */
250 if (lno
<= ep
->c_lno
)
252 if (ep
->c_nlines
!= OOBLNO
)
255 /* File now modified. */
256 if (F_ISSET(ep
, F_FIRSTMODIFY
))
258 F_SET(ep
, F_MODIFIED
);
261 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
264 /* maybe this could be simpler
266 * DB3 behaves differently from DB1
268 * if lno != 0 just go to lno and put the new line after it
269 * if lno == 0 then if there are any record, put in front of the first
270 * otherwise just append to the end thus creating the first
274 append(sp
, lno
, p
, len
)
288 memset(&key
, 0, sizeof(key
));
290 key
.size
= sizeof(lno
);
291 memset(&data
, 0, sizeof(data
));
293 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
296 INT2FILE(sp
, p
, len
, fp
, flen
);
299 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
304 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
306 (void)dbcp_put
->c_close(dbcp_put
);
310 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
311 if (sp
->db_error
!= DB_NOTFOUND
)
316 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
321 key
.size
= sizeof(lno
);
324 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
330 (void)dbcp_put
->c_close(dbcp_put
);
337 * Append a line into the file.
339 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
342 db_append(sp
, update
, lno
, p
, len
)
352 #if defined(DEBUG) && 0
353 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
355 /* Check for no underlying file. */
356 if ((ep
= sp
->ep
) == NULL
) {
357 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
362 if (append(sp
, lno
, p
, len
) != 0) {
363 msgq(sp
, M_DBERR
, "004|unable to append to line %lu",
368 /* Flush the cache, update line count, before screen update. */
371 if (ep
->c_nlines
!= OOBLNO
)
374 /* File now dirty. */
375 if (F_ISSET(ep
, F_FIRSTMODIFY
))
377 F_SET(ep
, F_MODIFIED
);
380 log_line(sp
, lno
+ 1, LOG_LINE_APPEND
);
382 /* Update marks, @ and global commands. */
384 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
386 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
393 * Nasty hack. If multiple lines are input by the user, they aren't
394 * committed until an <ESC> is entered. The problem is the screen was
395 * updated/scrolled as each line was entered. So, when this routine
396 * is called to copy the new lines from the cut buffer into the file,
397 * it has to know not to update the screen again.
399 return (scr_update(sp
, lno
, LINE_APPEND
, update
) || rval
);
404 * Insert a line into the file.
406 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
409 db_insert(sp
, lno
, p
, len
)
420 #if defined(DEBUG) && 0
421 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
422 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
424 /* Check for no underlying file. */
425 if ((ep
= sp
->ep
) == NULL
) {
426 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
431 if (append(sp
, lno
- 1, p
, len
) != 0) {
432 msgq(sp
, M_DBERR
, "005|unable to insert at line %lu",
437 /* Flush the cache, update line count, before screen update. */
438 if (lno
>= ep
->c_lno
)
440 if (ep
->c_nlines
!= OOBLNO
)
443 /* File now dirty. */
444 if (F_ISSET(ep
, F_FIRSTMODIFY
))
446 F_SET(ep
, F_MODIFIED
);
449 log_line(sp
, lno
, LOG_LINE_INSERT
);
451 /* Update marks, @ and global commands. */
453 if (mark_insdel(sp
, LINE_INSERT
, lno
))
455 if (ex_g_insdel(sp
, LINE_INSERT
, lno
))
459 return (scr_update(sp
, lno
, LINE_INSERT
, 1) || rval
);
464 * Store a line in the file.
466 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
469 db_set(sp
, lno
, p
, len
)
480 #if defined(DEBUG) && 0
481 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
482 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
484 /* Check for no underlying file. */
485 if ((ep
= sp
->ep
) == NULL
) {
486 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
490 /* Log before change. */
491 log_line(sp
, lno
, LOG_LINE_RESET_B
);
493 INT2FILE(sp
, p
, len
, fp
, flen
);
496 memset(&key
, 0, sizeof(key
));
498 key
.size
= sizeof(lno
);
499 memset(&data
, 0, sizeof(data
));
502 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
503 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
507 /* Flush the cache, before logging or screen update. */
508 if (lno
== ep
->c_lno
)
511 /* File now dirty. */
512 if (F_ISSET(ep
, F_FIRSTMODIFY
))
514 F_SET(ep
, F_MODIFIED
);
516 /* Log after change. */
517 log_line(sp
, lno
, LOG_LINE_RESET_F
);
520 return (scr_update(sp
, lno
, LINE_RESET
, 1));
525 * Return if a line exists.
527 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
536 /* Check for no underlying file. */
537 if ((ep
= sp
->ep
) == NULL
) {
538 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
546 * Check the last-line number cache. Adjust the cached line
547 * number for the lines used by the text input buffers.
549 if (ep
->c_nlines
!= OOBLNO
)
550 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
551 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
552 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
554 /* Go get the line. */
555 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
560 * Return the number of lines in the file.
562 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
576 /* Check for no underlying file. */
577 if ((ep
= sp
->ep
) == NULL
) {
578 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
583 * Check the last-line number cache. Adjust the cached line
584 * number for the lines used by the text input buffers.
586 if (ep
->c_nlines
!= OOBLNO
) {
587 *lnop
= ep
->c_nlines
;
588 if (F_ISSET(sp
, SC_TINPUT
))
589 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
590 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
594 memset(&key
, 0, sizeof(key
));
596 key
.size
= sizeof(lno
);
597 memset(&data
, 0, sizeof(data
));
599 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
601 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
606 (void)dbcp
->c_close(dbcp
);
609 msgq(sp
, M_DBERR
, "007|unable to get last line");
615 (void)dbcp
->c_close(dbcp
);
617 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
619 /* Fill the cache. */
620 BINC_GOTOW(sp
, ep
->c_lp
, ep
->c_blen
, wlen
);
621 MEMCPYW(ep
->c_lp
, wp
, wlen
);
622 memcpy(&lno
, key
.data
, sizeof(lno
));
623 ep
->c_nlines
= ep
->c_lno
= lno
;
626 /* Return the value. */
627 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
628 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
629 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
635 * Report a line error.
637 * PUBLIC: void db_err __P((SCR *, db_recno_t));
645 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
650 * Update all of the screens that are backed by the file that
654 scr_update(sp
, lno
, op
, current
)
664 if (F_ISSET(sp
, SC_EX
))
667 /* XXXX goes outside of window */
670 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
672 for (tsp
= wp
->scrq
.cqh_first
;
673 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
674 if (sp
!= tsp
&& tsp
->ep
== ep
)
675 if (vs_change(tsp
, lno
, op
))
677 return (current
? vs_change(sp
, lno
, op
) : 0);