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: db1.c,v 10.1 2002/03/09 12:53:57 skimo Exp $ (Berkeley) $Date: 2002/03/09 12:53:57 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
21 #include <bitstring.h>
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
)
98 * The underlying recno stuff handles zero by returning NULL, but
99 * have to have an OOB condition for the look-aside into the input
105 /* Check for no underlying file. */
106 if ((ep
= sp
->ep
) == NULL
) {
107 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
111 if (LF_ISSET(DBG_NOCACHE
))
115 * Look-aside into the TEXT buffers and see if the line we want
118 if (F_ISSET(sp
, SC_TINPUT
)) {
119 l1
= ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
120 l2
= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
;
121 if (l1
<= lno
&& l2
>= lno
) {
122 #if defined(DEBUG) && 0
124 "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
126 for (tp
= sp
->tiq
.cqh_first
;
127 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
135 * Adjust the line number for the number of lines used
136 * by the text input buffers.
142 /* Look-aside into the cache, and see if the line we want is there. */
143 if (lno
== sp
->c_lno
) {
144 #if defined(DEBUG) && 0
145 vtrace(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
158 /* data.size contains length in bytes */
159 BINC_GOTO(sp
, CHAR_T
, sp
->c_lp
, sp
->c_blen
, nlen
);
161 /* Get the line from the underlying database. */
163 key
.size
= sizeof(lno
);
164 switch (ep
->db
->get(ep
->db
, &key
, &data
, 0)) {
168 err1
: if (LF_ISSET(DBG_FATAL
))
169 err2
: db_err(sp
, lno
);
171 err3
: if (lenp
!= NULL
)
177 if (data
.size
> nlen
) {
181 memcpy(sp
->c_lp
, data
.data
, nlen
);
184 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
185 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
186 F_SET(sp
, SC_CONV_ERROR
);
187 msgq(sp
, M_ERR
, "324|Conversion error on line %d", lno
);
192 /* Reset the cache. */
193 if (wp
!= data
.data
) {
194 BINC_GOTOW(sp
, sp
->c_lp
, sp
->c_blen
, wlen
);
195 MEMCPYW(sp
->c_lp
, wp
, wlen
);
200 #if defined(DEBUG) && 0
201 vtrace(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
212 * Delete a line from the file.
214 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
217 db_delete(SCR
*sp
, db_recno_t lno
)
222 #if defined(DEBUG) && 0
223 vtrace(sp
, "delete line %lu\n", (u_long
)lno
);
225 /* Check for no underlying file. */
226 if ((ep
= sp
->ep
) == NULL
) {
227 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
230 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
231 ex_emsg(sp
, NULL
, EXM_LOCKED
);
235 /* Update marks, @ and global commands. */
236 if (mark_insdel(sp
, LINE_DELETE
, lno
))
238 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
242 log_line(sp
, lno
, LOG_LINE_DELETE_B
);
246 key
.size
= sizeof(lno
);
247 sp
->db_error
= ep
->db
->del(ep
->db
, &key
, 0);
248 if (sp
->db_error
!= 0) {
249 if (sp
->db_error
== -1)
250 sp
->db_error
= errno
;
252 msgq(sp
, M_DBERR
, "003|unable to delete line %lu",
257 /* Flush the cache, update line count, before screen update. */
258 update_cache(sp
, LINE_DELETE
, lno
);
260 /* File now modified. */
261 if (F_ISSET(ep
, F_FIRSTMODIFY
))
263 F_SET(ep
, F_MODIFIED
);
265 /* Log after change. */
266 log_line(sp
, lno
, LOG_LINE_DELETE_F
);
269 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
274 * Append a line into the file.
276 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
279 db_append(SCR
*sp
, int update
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
287 #if defined(DEBUG) && 0
288 vtrace(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
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
);
303 INT2FILE(sp
, p
, len
, fp
, flen
);
307 key
.size
= sizeof(lno
);
310 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IAFTER
)) {
311 msgq(sp
, M_DBERR
, "004|unable to append to line %lu",
316 /* Flush the cache, update line count, before screen update. */
317 update_cache(sp
, LINE_INSERT
, lno
);
319 /* File now dirty. */
320 if (F_ISSET(ep
, F_FIRSTMODIFY
))
322 F_SET(ep
, F_MODIFIED
);
324 /* Log after change. */
325 log_line(sp
, lno
+ 1, LOG_LINE_APPEND_F
);
327 /* Update marks, @ and global commands. */
329 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
331 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
338 * Nasty hack. If multiple lines are input by the user, they aren't
339 * committed until an <ESC> is entered. The problem is the screen was
340 * updated/scrolled as each line was entered. So, when this routine
341 * is called to copy the new lines from the cut buffer into the file,
342 * it has to know not to update the screen again.
344 return (scr_update(sp
, lno
, LINE_APPEND
, update
) || rval
);
349 * Insert a line into the file.
351 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
354 db_insert(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
362 #if defined(DEBUG) && 0
363 vtrace(sp
, "insert before %lu: len %lu {%.*s}\n",
364 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
366 /* Check for no underlying file. */
367 if ((ep
= sp
->ep
) == NULL
) {
368 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
371 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
372 ex_emsg(sp
, NULL
, EXM_LOCKED
);
376 /* Log before change. */
377 log_line(sp
, lno
, LOG_LINE_APPEND_B
);
379 INT2FILE(sp
, p
, len
, fp
, flen
);
383 key
.size
= sizeof(lno
);
386 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IBEFORE
)) {
388 "005|unable to insert at line %lu", (u_long
)lno
);
392 /* Flush the cache, update line count, before screen update. */
393 update_cache(sp
, LINE_INSERT
, lno
);
395 /* File now dirty. */
396 if (F_ISSET(ep
, F_FIRSTMODIFY
))
398 F_SET(ep
, F_MODIFIED
);
400 /* Log after change. */
401 log_line(sp
, lno
, LOG_LINE_APPEND_F
);
403 /* Update marks, @ and global commands. */
405 if (mark_insdel(sp
, LINE_INSERT
, lno
))
407 if (ex_g_insdel(sp
, LINE_INSERT
, lno
))
411 return (scr_update(sp
, lno
, LINE_INSERT
, 1) || rval
);
416 * Store a line in the file.
418 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
421 db_set(SCR
*sp
, db_recno_t lno
, CHAR_T
*p
, size_t len
)
428 #if defined(DEBUG) && 0
429 vtrace(sp
, "replace line %lu: len %lu {%.*s}\n",
430 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
432 /* Check for no underlying file. */
433 if ((ep
= sp
->ep
) == NULL
) {
434 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
437 if (ep
->l_win
&& ep
->l_win
!= sp
->wp
) {
438 ex_emsg(sp
, NULL
, EXM_LOCKED
);
442 /* Log before change. */
443 log_line(sp
, lno
, LOG_LINE_RESET_B
);
445 INT2FILE(sp
, p
, len
, fp
, flen
);
449 key
.size
= sizeof(lno
);
453 ep
->db
->put(ep
->db
, &key
, &data
, 0);
454 if (sp
->db_error
!= 0) {
455 if (sp
->db_error
== -1)
456 sp
->db_error
= errno
;
458 msgq(sp
, M_DBERR
, "006|unable to store line %lu", (u_long
)lno
);
462 /* Flush the cache, before logging or screen update. */
463 update_cache(sp
, LINE_RESET
, lno
);
465 /* File now dirty. */
466 if (F_ISSET(ep
, F_FIRSTMODIFY
))
468 F_SET(ep
, F_MODIFIED
);
470 /* Log after change. */
471 log_line(sp
, lno
, LOG_LINE_RESET_F
);
474 return (scr_update(sp
, lno
, LINE_RESET
, 1));
479 * Return if a line exists.
481 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
484 db_exist(SCR
*sp
, db_recno_t lno
)
488 /* Check for no underlying file. */
489 if ((ep
= sp
->ep
) == NULL
) {
490 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
498 * Check the last-line number cache. Adjust the cached line
499 * number for the lines used by the text input buffers.
501 if (ep
->c_nlines
!= OOBLNO
)
502 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
503 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
504 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
506 /* Go get the line. */
507 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
512 * Return the number of lines in the file.
514 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
517 db_last(SCR
*sp
, db_recno_t
*lnop
)
525 /* Check for no underlying file. */
526 if ((ep
= sp
->ep
) == NULL
) {
527 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
532 * Check the last-line number cache. Adjust the cached line
533 * number for the lines used by the text input buffers.
535 if (ep
->c_nlines
!= OOBLNO
) {
536 *lnop
= ep
->c_nlines
;
537 if (F_ISSET(sp
, SC_TINPUT
))
538 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
539 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
544 key
.size
= sizeof(lno
);
546 sp
->db_error
= ep
->db
->seq(ep
->db
, &key
, &data
, R_LAST
);
547 switch (sp
->db_error
) {
552 sp
->db_error
= errno
;
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 /* Return the value. */
575 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
576 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
577 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
583 * Report a line error.
585 * PUBLIC: void db_err __P((SCR *, db_recno_t));
588 db_err(SCR
*sp
, db_recno_t lno
)
591 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
596 * Update all of the screens that are backed by the file that
600 scr_update(SCR
*sp
, db_recno_t lno
, lnop_t op
, int current
)
606 if (F_ISSET(sp
, SC_EX
))
609 /* XXXX goes outside of window */
612 for (wp
= sp
->gp
->dq
.cqh_first
; wp
!= (void *)&sp
->gp
->dq
;
614 for (tsp
= wp
->scrq
.cqh_first
;
615 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
616 if (sp
!= tsp
&& tsp
->ep
== ep
)
617 if (vs_change(tsp
, lno
, op
))
619 return (current
? vs_change(sp
, lno
, op
) : 0);
623 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
626 update_cache(SCR
*sp
, lnop_t op
, db_recno_t lno
)
633 /* Flush the cache, update line count, before screen update. */
634 /* The flushing is probably not needed, since it was incorrect
635 * for db_insert. It might be better to adjust it, like
636 * marks, @ and global
638 for (scrp
= ep
->scrq
.cqh_first
; scrp
!= (void *)&ep
->scrq
;
639 scrp
= scrp
->eq
.cqe_next
)
643 if (lno
<= scrp
->c_lno
)
644 scrp
->c_lno
= OOBLNO
;
647 if (lno
== scrp
->c_lno
)
648 scrp
->c_lno
= OOBLNO
;
652 if (ep
->c_nlines
!= OOBLNO
)
664 * PUBLIC: int db_msg_open __P((SCR *, char *, DB **));
666 int db_msg_open(SCR
*sp
, char *file
, DB
**dbp
)
668 *dbp
= dbopen(file
, O_NONBLOCK
| O_RDONLY
, 0, DB_RECNO
, NULL
);
674 * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
677 db_init(SCR
*sp
, EXF
*ep
, char *rcv_name
, char *oname
, size_t psize
, int *open_err
)
681 memset(&oinfo
, 0, sizeof(RECNOINFO
));
682 oinfo
.bval
= '\n'; /* Always set. */
684 oinfo
.flags
= R_SNAPSHOT
;
686 oinfo
.bfname
= ep
->rcv_path
;
688 #define _DB_OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
690 ep
->db
= dbopen(rcv_name
== NULL
? oname
: NULL
,
691 O_NONBLOCK
| O_RDONLY
, _DB_OPEN_MODE
, DB_RECNO
, &oinfo
);
695 M_DBERR
, rcv_name
== NULL
? oname
: rcv_name
, "%s");
698 * Historically, vi permitted users to edit files that couldn't
699 * be read. This isn't useful for single files from a command
700 * line, but it's quite useful for "vi *.c", since you can skip
701 * past files that you can't read.
703 ep
->db
= NULL
; /* Don't close it; it wasn't opened */
713 db_strerror(int error
)
715 return error
> 0 ? strerror(error
) : "record not found";