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.27 2000/07/14 14:29:15 skimo Exp $ (Berkeley) $Date: 2000/07/14 14:29:15 $";
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. */
96 * The underlying recno stuff handles zero by returning NULL, but
97 * have to have an OOB condition for the look-aside into the input
103 /* Check for no underlying file. */
104 if ((ep
= sp
->ep
) == NULL
) {
105 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
109 if (LF_ISSET(DBG_NOCACHE
))
113 * Look-aside into the TEXT buffers and see if the line we want
116 if (F_ISSET(sp
, SC_TINPUT
)) {
117 l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
118 l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
119 if (l1
<= lno
&& l2
>= lno
) {
120 #if defined(DEBUG) && 0
122 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
124 for (tp
= sp
->tiq
.cqh_first
;
125 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
133 * Adjust the line number for the number of lines used
134 * by the text input buffers.
140 /* Look-aside into the cache, and see if the line we want is there. */
141 if (lno
== ep
->c_lno
) {
142 #if defined(DEBUG) && 0
143 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
154 /* Get the line from the underlying database. */
155 memset(&key
, 0, sizeof(key
));
157 key
.size
= sizeof(lno
);
158 memset(&data
, 0, sizeof(data
));
159 switch (ep
->db
->get(ep
->db
, NULL
, &key
, &data
, 0)) {
163 err1
: if (LF_ISSET(DBG_FATAL
))
164 err2
: db_err(sp
, lno
);
165 err3
: if (lenp
!= NULL
)
174 /* Reset the cache. */
176 ep
->c_len
= data
.size
/ sizeof(CHAR_T
);
177 ep
->c_lp
= data
.data
;
179 #if defined(DEBUG) && 0
180 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
183 *lenp
= data
.size
/ sizeof(CHAR_T
);
191 * Delete a line from the file.
193 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
203 #if defined(DEBUG) && 0
204 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
206 /* Check for no underlying file. */
207 if ((ep
= sp
->ep
) == NULL
) {
208 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
212 /* Update marks, @ and global commands. */
213 if (mark_insdel(sp
, LINE_DELETE
, lno
))
215 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
219 log_line(sp
, lno
, LOG_LINE_DELETE
);
222 memset(&key
, 0, sizeof(key
));
224 key
.size
= sizeof(lno
);
225 if ((sp
->db_error
= ep
->db
->del(ep
->db
, NULL
, &key
, 0)) != 0) {
226 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
231 /* Flush the cache, update line count, before screen update. */
232 if (lno
<= ep
->c_lno
)
234 if (ep
->c_nlines
!= OOBLNO
)
237 /* File now modified. */
238 if (F_ISSET(ep
, F_FIRSTMODIFY
))
240 F_SET(ep
, F_MODIFIED
);
243 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
246 /* maybe this could be simpler
248 * DB3 behaves differently from DB1
250 * if lno != 0 just go to lno and put the new line after it
251 * if lno == 0 then if there are any record, put in front of the first
252 * otherwise just append to the end thus creating the first
256 append(sp
, lno
, p
, len
)
268 memset(&key
, 0, sizeof(key
));
270 key
.size
= sizeof(lno
);
271 memset(&data
, 0, sizeof(data
));
273 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp_put
, 0)) != 0)
277 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_SET
)) != 0)
281 data
.size
= len
* sizeof(CHAR_T
);
282 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_AFTER
)) != 0) {
284 (void)dbcp_put
->c_close(dbcp_put
);
288 if ((sp
->db_error
= dbcp_put
->c_get(dbcp_put
, &key
, &data
, DB_FIRST
)) != 0) {
289 if (sp
->db_error
!= DB_NOTFOUND
)
293 data
.size
= len
* sizeof(CHAR_T
);
294 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, DB_APPEND
)) != 0) {
299 key
.size
= sizeof(lno
);
301 data
.size
= len
* sizeof(CHAR_T
);
302 if ((sp
->db_error
= dbcp_put
->c_put(dbcp_put
, &key
, &data
, DB_BEFORE
)) != 0) {
308 (void)dbcp_put
->c_close(dbcp_put
);
315 * Append a line into the file.
317 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
320 db_append(sp
, update
, lno
, p
, len
)
330 #if defined(DEBUG) && 0
331 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
333 /* Check for no underlying file. */
334 if ((ep
= sp
->ep
) == NULL
) {
335 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
340 if (append(sp
, lno
, p
, len
) != 0) {
341 msgq(sp
, M_DBERR
, "004|unable to append to line %lu",
346 /* Flush the cache, update line count, before screen update. */
349 if (ep
->c_nlines
!= OOBLNO
)
352 /* File now dirty. */
353 if (F_ISSET(ep
, F_FIRSTMODIFY
))
355 F_SET(ep
, F_MODIFIED
);
358 log_line(sp
, lno
+ 1, LOG_LINE_APPEND
);
360 /* Update marks, @ and global commands. */
362 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
364 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
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
, LINE_APPEND
, update
) || rval
);
382 * Insert a line into the file.
384 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
387 db_insert(sp
, lno
, p
, len
)
398 #if defined(DEBUG) && 0
399 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
400 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
402 /* Check for no underlying file. */
403 if ((ep
= sp
->ep
) == NULL
) {
404 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
409 if (append(sp
, lno
- 1, p
, len
) != 0) {
410 msgq(sp
, M_DBERR
, "005|unable to insert at line %lu",
415 /* Flush the cache, update line count, before screen update. */
416 if (lno
>= ep
->c_lno
)
418 if (ep
->c_nlines
!= OOBLNO
)
421 /* File now dirty. */
422 if (F_ISSET(ep
, F_FIRSTMODIFY
))
424 F_SET(ep
, F_MODIFIED
);
427 log_line(sp
, lno
, LOG_LINE_INSERT
);
429 /* Update marks, @ and global commands. */
431 if (mark_insdel(sp
, LINE_INSERT
, lno
))
433 if (ex_g_insdel(sp
, LINE_INSERT
, lno
))
437 return (scr_update(sp
, lno
, LINE_INSERT
, 1) || rval
);
442 * Store a line in the file.
444 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
447 db_set(sp
, lno
, p
, len
)
456 #if defined(DEBUG) && 0
457 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
458 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
461 /* Check for no underlying file. */
462 if ((ep
= sp
->ep
) == NULL
) {
463 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
467 /* Log before change. */
468 log_line(sp
, lno
, LOG_LINE_RESET_B
);
471 memset(&key
, 0, sizeof(key
));
473 key
.size
= sizeof(lno
);
474 memset(&data
, 0, sizeof(data
));
476 data
.size
= len
* sizeof(CHAR_T
);
477 if ((sp
->db_error
= ep
->db
->put(ep
->db
, NULL
, &key
, &data
, 0)) != 0) {
478 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
482 /* Flush the cache, before logging or screen update. */
483 if (lno
== ep
->c_lno
)
486 /* File now dirty. */
487 if (F_ISSET(ep
, F_FIRSTMODIFY
))
489 F_SET(ep
, F_MODIFIED
);
491 /* Log after change. */
492 log_line(sp
, lno
, LOG_LINE_RESET_F
);
495 return (scr_update(sp
, lno
, LINE_RESET
, 1));
500 * Return if a line exists.
502 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
511 /* Check for no underlying file. */
512 if ((ep
= sp
->ep
) == NULL
) {
513 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
521 * Check the last-line number cache. Adjust the cached line
522 * number for the lines used by the text input buffers.
524 if (ep
->c_nlines
!= OOBLNO
)
525 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
526 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
527 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
529 /* Go get the line. */
530 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
535 * Return the number of lines in the file.
537 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
549 /* Check for no underlying file. */
550 if ((ep
= sp
->ep
) == NULL
) {
551 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
556 * Check the last-line number cache. Adjust the cached line
557 * number for the lines used by the text input buffers.
559 if (ep
->c_nlines
!= OOBLNO
) {
560 *lnop
= ep
->c_nlines
;
561 if (F_ISSET(sp
, SC_TINPUT
))
562 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
563 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
567 memset(&key
, 0, sizeof(key
));
569 key
.size
= sizeof(lno
);
570 memset(&data
, 0, sizeof(data
));
572 if ((sp
->db_error
= ep
->db
->cursor(ep
->db
, NULL
, &dbcp
, 0)) != 0)
574 switch (sp
->db_error
= dbcp
->c_get(dbcp
, &key
, &data
, DB_LAST
)) {
579 (void)dbcp
->c_close(dbcp
);
581 msgq(sp
, M_DBERR
, "007|unable to get last line");
587 (void)dbcp
->c_close(dbcp
);
589 /* Fill the cache. */
590 memcpy(&lno
, key
.data
, sizeof(lno
));
591 ep
->c_nlines
= ep
->c_lno
= lno
;
592 ep
->c_len
= data
.size
/ sizeof(CHAR_T
);
593 ep
->c_lp
= data
.data
;
595 /* Return the value. */
596 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
597 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
598 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
604 * Report a line error.
606 * PUBLIC: void db_err __P((SCR *, db_recno_t));
614 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
619 * Update all of the screens that are backed by the file that
623 scr_update(sp
, lno
, op
, current
)
633 if (F_ISSET(sp
, SC_EX
))
636 /* XXXX goes outside of window */
639 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
641 for (tsp
= wp
->scrq
.cqh_first
;
642 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
643 if (sp
!= tsp
&& tsp
->ep
== ep
)
644 if (vs_change(tsp
, lno
, op
))
646 return (current
? vs_change(sp
, lno
, op
) : 0);