remove generated compile, depcomp, missing and mkinstalldirs
[nvi.git] / common / vi_db1.c
blob6d63c94a2f8f9f56331591a437f4997f06c2021b
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
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 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 #include <sys/stat.h>
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <string.h>
28 #include "common.h"
29 #include "../vi/vi.h"
32 * db_eget --
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 *));
37 int
38 db_eget(SCR *sp, db_recno_t lno, CHAR_T **pp, size_t *lenp, int *isemptyp)
40 /* Line number. */
41 /* Pointer store. */
42 /* Length store. */
45 db_recno_t l1;
47 if (isemptyp != NULL)
48 *isemptyp = 0;
50 /* If the line exists, simply return it. */
51 if (!db_get(sp, lno, 0, pp, lenp))
52 return (0);
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
57 * fails loudly.
59 if ((lno == 0 || lno == 1) && db_last(sp, &l1))
60 return (1);
62 /* If the file isn't empty, fail loudly. */
63 if ((lno != 0 && lno != 1) || l1 != 0) {
64 db_err(sp, lno);
65 return (1);
68 if (isemptyp != NULL)
69 *isemptyp = 1;
71 return (1);
75 * db_get --
76 * Look in the text buffers for a line, followed by the cache, followed
77 * by the database.
79 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
81 int
82 db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
84 /* Line number. */
86 /* Pointer store. */
87 /* Length store. */
89 DBT data, key;
90 EXF *ep;
91 TEXT *tp;
92 db_recno_t l1, l2;
93 CHAR_T *wp;
94 size_t wlen;
95 size_t nlen;
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
100 * buffer anyway.
102 if (lno == 0)
103 goto err1;
105 /* Check for no underlying file. */
106 if ((ep = sp->ep) == NULL) {
107 ex_emsg(sp, NULL, EXM_NOFILEYET);
108 goto err3;
111 if (LF_ISSET(DBG_NOCACHE))
112 goto nocache;
115 * Look-aside into the TEXT buffers and see if the line we want
116 * is there.
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
123 vtrace(sp,
124 "retrieve TEXT buffer line %lu\n", (u_long)lno);
125 #endif
126 for (tp = sp->tiq.cqh_first;
127 tp->lno != lno; tp = tp->q.cqe_next);
128 if (lenp != NULL)
129 *lenp = tp->len;
130 if (pp != NULL)
131 *pp = tp->lb;
132 return (0);
135 * Adjust the line number for the number of lines used
136 * by the text input buffers.
138 if (lno > l2)
139 lno -= l2 - l1;
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);
146 #endif
147 if (lenp != NULL)
148 *lenp = sp->c_len;
149 if (pp != NULL)
150 *pp = sp->c_lp;
151 return (0);
153 sp->c_lno = OOBLNO;
155 nocache:
156 nlen = 1024;
157 retry:
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. */
162 key.data = &lno;
163 key.size = sizeof(lno);
164 switch (ep->db->get(ep->db, &key, &data, 0)) {
165 case -1:
166 goto err2;
167 case 1:
168 err1: if (LF_ISSET(DBG_FATAL))
169 err2: db_err(sp, lno);
170 alloc_err:
171 err3: if (lenp != NULL)
172 *lenp = 0;
173 if (pp != NULL)
174 *pp = NULL;
175 return (1);
176 case 0:
177 if (data.size > nlen) {
178 nlen = data.size;
179 goto retry;
180 } else
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);
189 goto err3;
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);
197 sp->c_lno = lno;
198 sp->c_len = wlen;
200 #if defined(DEBUG) && 0
201 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
202 #endif
203 if (lenp != NULL)
204 *lenp = wlen;
205 if (pp != NULL)
206 *pp = sp->c_lp;
207 return (0);
211 * db_delete --
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)
219 DBT key;
220 EXF *ep;
222 #if defined(DEBUG) && 0
223 vtrace(sp, "delete line %lu\n", (u_long)lno);
224 #endif
225 /* Check for no underlying file. */
226 if ((ep = sp->ep) == NULL) {
227 ex_emsg(sp, NULL, EXM_NOFILEYET);
228 return (1);
230 if (ep->l_win && ep->l_win != sp->wp) {
231 ex_emsg(sp, NULL, EXM_LOCKED);
232 return 1;
235 /* Update marks, @ and global commands. */
236 if (mark_insdel(sp, LINE_DELETE, lno))
237 return (1);
238 if (ex_g_insdel(sp, LINE_DELETE, lno))
239 return (1);
241 /* Log change. */
242 log_line(sp, lno, LOG_LINE_DELETE_B);
244 /* Update file. */
245 key.data = &lno;
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",
253 (u_long)lno);
254 return (1);
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))
262 (void)rcv_init(sp);
263 F_SET(ep, F_MODIFIED);
265 /* Log after change. */
266 log_line(sp, lno, LOG_LINE_DELETE_F);
268 /* Update screen. */
269 return (scr_update(sp, lno, LINE_DELETE, 1));
273 * db_append --
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)
281 DBT data, key;
282 EXF *ep;
283 char *fp;
284 size_t flen;
285 int rval;
287 #if defined(DEBUG) && 0
288 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
289 #endif
290 /* Check for no underlying file. */
291 if ((ep = sp->ep) == NULL) {
292 ex_emsg(sp, NULL, EXM_NOFILEYET);
293 return (1);
295 if (ep->l_win && ep->l_win != sp->wp) {
296 ex_emsg(sp, NULL, EXM_LOCKED);
297 return 1;
300 /* Log before change. */
301 log_line(sp, lno + 1, LOG_LINE_APPEND_B);
303 INT2FILE(sp, p, len, fp, flen);
305 /* Update file. */
306 key.data = &lno;
307 key.size = sizeof(lno);
308 data.data = fp;
309 data.size = flen;
310 if (ep->db->put(ep->db, &key, &data, R_IAFTER)) {
311 msgq(sp, M_DBERR, "004|unable to append to line %lu",
312 (u_long)lno);
313 return (1);
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))
321 (void)rcv_init(sp);
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. */
328 rval = 0;
329 if (mark_insdel(sp, LINE_INSERT, lno + 1))
330 rval = 1;
331 if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
332 rval = 1;
335 * Update screen.
337 * XXX
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);
348 * db_insert --
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)
356 DBT data, key;
357 EXF *ep;
358 char *fp;
359 size_t flen;
360 int rval;
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);
365 #endif
366 /* Check for no underlying file. */
367 if ((ep = sp->ep) == NULL) {
368 ex_emsg(sp, NULL, EXM_NOFILEYET);
369 return (1);
371 if (ep->l_win && ep->l_win != sp->wp) {
372 ex_emsg(sp, NULL, EXM_LOCKED);
373 return 1;
376 /* Log before change. */
377 log_line(sp, lno, LOG_LINE_APPEND_B);
379 INT2FILE(sp, p, len, fp, flen);
381 /* Update file. */
382 key.data = &lno;
383 key.size = sizeof(lno);
384 data.data = fp;
385 data.size = flen;
386 if (ep->db->put(ep->db, &key, &data, R_IBEFORE)) {
387 msgq(sp, M_SYSERR,
388 "005|unable to insert at line %lu", (u_long)lno);
389 return (1);
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))
397 (void)rcv_init(sp);
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. */
404 rval = 0;
405 if (mark_insdel(sp, LINE_INSERT, lno))
406 rval = 1;
407 if (ex_g_insdel(sp, LINE_INSERT, lno))
408 rval = 1;
410 /* Update screen. */
411 return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
415 * db_set --
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)
423 DBT data, key;
424 EXF *ep;
425 char *fp;
426 size_t flen;
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);
431 #endif
432 /* Check for no underlying file. */
433 if ((ep = sp->ep) == NULL) {
434 ex_emsg(sp, NULL, EXM_NOFILEYET);
435 return (1);
437 if (ep->l_win && ep->l_win != sp->wp) {
438 ex_emsg(sp, NULL, EXM_LOCKED);
439 return 1;
442 /* Log before change. */
443 log_line(sp, lno, LOG_LINE_RESET_B);
445 INT2FILE(sp, p, len, fp, flen);
447 /* Update file. */
448 key.data = &lno;
449 key.size = sizeof(lno);
450 data.data = fp;
451 data.size = flen;
452 sp->db_error =
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);
459 return (1);
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))
467 (void)rcv_init(sp);
468 F_SET(ep, F_MODIFIED);
470 /* Log after change. */
471 log_line(sp, lno, LOG_LINE_RESET_F);
473 /* Update screen. */
474 return (scr_update(sp, lno, LINE_RESET, 1));
478 * db_exist --
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)
486 EXF *ep;
488 /* Check for no underlying file. */
489 if ((ep = sp->ep) == NULL) {
490 ex_emsg(sp, NULL, EXM_NOFILEYET);
491 return (1);
494 if (lno == OOBLNO)
495 return (0);
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));
511 * db_last --
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)
519 DBT data, key;
520 EXF *ep;
521 db_recno_t lno;
522 CHAR_T *wp;
523 size_t wlen;
525 /* Check for no underlying file. */
526 if ((ep = sp->ep) == NULL) {
527 ex_emsg(sp, NULL, EXM_NOFILEYET);
528 return (1);
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;
540 return (0);
543 key.data = &lno;
544 key.size = sizeof(lno);
546 sp->db_error = ep->db->seq(ep->db, &key, &data, R_LAST);
547 switch (sp->db_error) {
548 case 1:
549 *lnop = 0;
550 return (0);
551 case -1:
552 sp->db_error = errno;
553 alloc_err:
554 msgq(sp, M_DBERR, "007|unable to get last line");
555 *lnop = 0;
556 return (1);
557 case 0:
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);
569 sp->c_lno = lno;
570 sp->c_len = wlen;
572 ep->c_nlines = lno;
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);
578 return (0);
582 * db_err --
583 * Report a line error.
585 * PUBLIC: void db_err __P((SCR *, db_recno_t));
587 void
588 db_err(SCR *sp, db_recno_t lno)
590 msgq(sp, M_ERR,
591 "008|Error: unable to retrieve line %lu", (u_long)lno);
595 * scr_update --
596 * Update all of the screens that are backed by the file that
597 * just changed.
600 scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
602 EXF *ep;
603 SCR *tsp;
604 WIN *wp;
606 if (F_ISSET(sp, SC_EX))
607 return (0);
609 /* XXXX goes outside of window */
610 ep = sp->ep;
611 if (ep->refcnt != 1)
612 for (wp = sp->gp->dq.cqh_first; wp != (void *)&sp->gp->dq;
613 wp = wp->q.cqe_next)
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))
618 return (1);
619 return (current ? vs_change(sp, lno, op) : 0);
623 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
625 void
626 update_cache(SCR *sp, lnop_t op, db_recno_t lno)
628 SCR* scrp;
629 EXF *ep;
631 ep = sp->ep;
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)
640 switch (op) {
641 case LINE_INSERT:
642 case LINE_DELETE:
643 if (lno <= scrp->c_lno)
644 scrp->c_lno = OOBLNO;
645 break;
646 case LINE_RESET:
647 if (lno == scrp->c_lno)
648 scrp->c_lno = OOBLNO;
649 break;
652 if (ep->c_nlines != OOBLNO)
653 switch (op) {
654 case LINE_INSERT:
655 ++ep->c_nlines;
656 break;
657 case LINE_DELETE:
658 --ep->c_nlines;
659 break;
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);
670 return *dbp == 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)
679 RECNOINFO oinfo;
681 memset(&oinfo, 0, sizeof(RECNOINFO));
682 oinfo.bval = '\n'; /* Always set. */
683 oinfo.psize = psize;
684 oinfo.flags = R_SNAPSHOT;
685 if (rcv_name)
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);
693 if (!ep->db) {
694 msgq_str(sp,
695 M_DBERR, rcv_name == NULL ? oname : rcv_name, "%s");
697 * !!!
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 */
705 *open_err = 1;
706 return 1;
709 return 0;
712 char *
713 db_strerror(int error)
715 return error > 0 ? strerror(error) : "record not found";