fix use of RE_W{START,STOP} in wide char version
[nvi.git] / common / db.c
blob9fee00a47c980ae1006e65d7982035b2284ef697
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: db.c,v 10.34 2000/07/22 17:31:18 skimo Exp $ (Berkeley) $Date: 2000/07/22 17:31:18 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include "common.h"
27 #include "../vi/vi.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));
33 * db_eget --
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 *));
38 int
39 db_eget(sp, lno, pp, lenp, isemptyp)
40 SCR *sp;
41 db_recno_t lno; /* Line number. */
42 CHAR_T **pp; /* Pointer store. */
43 size_t *lenp; /* Length store. */
44 int *isemptyp;
46 db_recno_t l1;
48 if (isemptyp != NULL)
49 *isemptyp = 0;
51 /* If the line exists, simply return it. */
52 if (!db_get(sp, lno, 0, pp, lenp))
53 return (0);
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
58 * fails loudly.
60 if ((lno == 0 || lno == 1) && db_last(sp, &l1))
61 return (1);
63 /* If the file isn't empty, fail loudly. */
64 if ((lno != 0 && lno != 1) || l1 != 0) {
65 db_err(sp, lno);
66 return (1);
69 if (isemptyp != NULL)
70 *isemptyp = 1;
72 return (1);
76 * db_get --
77 * Look in the text buffers for a line, followed by the cache, followed
78 * by the database.
80 * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
82 int
83 db_get(sp, lno, flags, pp, lenp)
84 SCR *sp;
85 db_recno_t lno; /* Line number. */
86 u_int32_t flags;
87 CHAR_T **pp; /* Pointer store. */
88 size_t *lenp; /* Length store. */
90 DBT data, key;
91 EXF *ep;
92 TEXT *tp;
93 db_recno_t l1, l2;
94 CHAR_T *wp;
95 size_t wlen;
96 size_t nlen;
97 char *bp;
98 size_t blen;
100 bp = NULL;
102 * The underlying recno stuff handles zero by returning NULL, but
103 * have to have an OOB condition for the look-aside into the input
104 * buffer anyway.
106 if (lno == 0)
107 goto err1;
109 /* Check for no underlying file. */
110 if ((ep = sp->ep) == NULL) {
111 ex_emsg(sp, NULL, EXM_NOFILEYET);
112 goto err3;
115 if (LF_ISSET(DBG_NOCACHE))
116 goto nocache;
119 * Look-aside into the TEXT buffers and see if the line we want
120 * is there.
122 if (F_ISSET(sp, SC_TINPUT)) {
123 l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
124 l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
125 if (l1 <= lno && l2 >= lno) {
126 #if defined(DEBUG) && 0
127 vtrace(sp,
128 "retrieve TEXT buffer line %lu\n", (u_long)lno);
129 #endif
130 for (tp = sp->tiq.cqh_first;
131 tp->lno != lno; tp = tp->q.cqe_next);
132 if (lenp != NULL)
133 *lenp = tp->len;
134 if (pp != NULL)
135 *pp = tp->lb;
136 return (0);
139 * Adjust the line number for the number of lines used
140 * by the text input buffers.
142 if (lno > l2)
143 lno -= l2 - l1;
146 /* Look-aside into the cache, and see if the line we want is there. */
147 /* Disable cache for now */
148 if (0 && lno == ep->c_lno) {
149 #if defined(DEBUG) && 0
150 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
151 #endif
152 if (lenp != NULL)
153 *lenp = ep->c_len;
154 if (pp != NULL)
155 *pp = ep->c_lp;
156 return (0);
158 ep->c_lno = OOBLNO;
160 nocache:
161 nlen = 1024;
162 GET_SPACE_GOTO(sp, bp, blen, nlen);
163 if (0) {
164 retry: ADD_SPACE_GOTO(sp, bp, blen, nlen);
167 BINC_GOTO(sp, (char *)ep->c_lp, ep->c_blen, nlen);
170 /* Get the line from the underlying database. */
171 memset(&key, 0, sizeof(key));
172 key.data = &lno;
173 key.size = sizeof(lno);
174 memset(&data, 0, sizeof(data));
175 data.data = bp;
176 data.ulen = blen;
177 data.flags = DB_DBT_USERMEM;
178 switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
179 case ENOMEM:
180 nlen = data.size;
181 goto retry;
182 default:
183 goto err2;
184 case DB_NOTFOUND:
185 err1: if (LF_ISSET(DBG_FATAL))
186 err2: db_err(sp, lno);
187 alloc_err:
188 err3: if (lenp != NULL)
189 *lenp = 0;
190 if (pp != NULL)
191 *pp = NULL;
192 if (bp != NULL)
193 FREE_SPACE(sp, bp, blen);
194 return (1);
195 case 0:
199 FILE2INT(sp, data.data, data.size, wp, wlen);
201 /* Reset the cache. */
202 BINC_GOTOW(sp, ep->c_lp, ep->c_blen, wlen);
203 MEMCPYW(ep->c_lp, wp, wlen);
204 ep->c_lno = lno;
205 ep->c_len = wlen;
207 #if defined(DEBUG) && 0
208 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
209 #endif
210 if (lenp != NULL)
211 *lenp = wlen;
212 if (pp != NULL)
213 *pp = ep->c_lp;
214 if (bp != NULL)
215 FREE_SPACE(sp, bp, blen);
216 return (0);
220 * db_delete --
221 * Delete a line from the file.
223 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
226 db_delete(sp, lno)
227 SCR *sp;
228 db_recno_t lno;
230 DBT key;
231 EXF *ep;
233 #if defined(DEBUG) && 0
234 vtrace(sp, "delete line %lu\n", (u_long)lno);
235 #endif
236 /* Check for no underlying file. */
237 if ((ep = sp->ep) == NULL) {
238 ex_emsg(sp, NULL, EXM_NOFILEYET);
239 return (1);
241 if (ep->l_win && ep->l_win != sp->wp) {
242 ex_emsg(sp, NULL, EXM_LOCKED);
243 return 1;
246 /* Update marks, @ and global commands. */
247 if (mark_insdel(sp, LINE_DELETE, lno))
248 return (1);
249 if (ex_g_insdel(sp, LINE_DELETE, lno))
250 return (1);
252 /* Log change. */
253 log_line(sp, lno, LOG_LINE_DELETE);
255 /* Update file. */
256 memset(&key, 0, sizeof(key));
257 key.data = &lno;
258 key.size = sizeof(lno);
259 if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
260 msgq(sp, M_DBERR, "003|unable to delete line %lu",
261 (u_long)lno);
262 return (1);
265 /* Flush the cache, update line count, before screen update. */
266 if (lno <= ep->c_lno)
267 ep->c_lno = OOBLNO;
268 if (ep->c_nlines != OOBLNO)
269 --ep->c_nlines;
271 /* File now modified. */
272 if (F_ISSET(ep, F_FIRSTMODIFY))
273 (void)rcv_init(sp);
274 F_SET(ep, F_MODIFIED);
276 /* Update screen. */
277 return (scr_update(sp, lno, LINE_DELETE, 1));
280 /* maybe this could be simpler
282 * DB3 behaves differently from DB1
284 * if lno != 0 just go to lno and put the new line after it
285 * if lno == 0 then if there are any record, put in front of the first
286 * otherwise just append to the end thus creating the first
287 * line
289 static int
290 append(sp, lno, p, len)
291 SCR *sp;
292 db_recno_t lno;
293 CHAR_T *p;
294 size_t len;
296 DBT data, key;
297 DBC *dbcp_put;
298 EXF *ep;
299 char *fp;
300 size_t flen;
302 ep = sp->ep;
304 memset(&key, 0, sizeof(key));
305 key.data = &lno;
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)
310 return 1;
312 INT2FILE(sp, p, len, fp, flen);
314 if (lno != 0) {
315 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0)
316 goto err2;
318 data.data = fp;
319 data.size = flen;
320 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_AFTER)) != 0) {
321 err2:
322 (void)dbcp_put->c_close(dbcp_put);
323 return (1);
325 } else {
326 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
327 if (sp->db_error != DB_NOTFOUND)
328 goto err2;
330 data.data = fp;
331 data.size = flen;
332 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
333 goto err2;
335 } else {
336 key.data = &lno;
337 key.size = sizeof(lno);
338 data.data = fp;
339 data.size = flen;
340 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
341 goto err2;
346 (void)dbcp_put->c_close(dbcp_put);
348 return 0;
352 * db_append --
353 * Append a line into the file.
355 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
358 db_append(sp, update, lno, p, len)
359 SCR *sp;
360 int update;
361 db_recno_t lno;
362 CHAR_T *p;
363 size_t len;
365 EXF *ep;
366 int rval;
368 #if defined(DEBUG) && 0
369 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
370 #endif
371 /* Check for no underlying file. */
372 if ((ep = sp->ep) == NULL) {
373 ex_emsg(sp, NULL, EXM_NOFILEYET);
374 return (1);
376 if (ep->l_win && ep->l_win != sp->wp) {
377 ex_emsg(sp, NULL, EXM_LOCKED);
378 return 1;
381 /* Update file. */
382 if (append(sp, lno, p, len) != 0) {
383 msgq(sp, M_DBERR, "004|unable to append to line %lu",
384 (u_long)lno);
385 return (1);
388 /* Flush the cache, update line count, before screen update. */
389 if (lno < ep->c_lno)
390 ep->c_lno = OOBLNO;
391 if (ep->c_nlines != OOBLNO)
392 ++ep->c_nlines;
394 /* File now dirty. */
395 if (F_ISSET(ep, F_FIRSTMODIFY))
396 (void)rcv_init(sp);
397 F_SET(ep, F_MODIFIED);
399 /* Log change. */
400 log_line(sp, lno + 1, LOG_LINE_APPEND);
402 /* Update marks, @ and global commands. */
403 rval = 0;
404 if (mark_insdel(sp, LINE_INSERT, lno + 1))
405 rval = 1;
406 if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
407 rval = 1;
410 * Update screen.
412 * XXX
413 * Nasty hack. If multiple lines are input by the user, they aren't
414 * committed until an <ESC> is entered. The problem is the screen was
415 * updated/scrolled as each line was entered. So, when this routine
416 * is called to copy the new lines from the cut buffer into the file,
417 * it has to know not to update the screen again.
419 return (scr_update(sp, lno, LINE_APPEND, update) || rval);
423 * db_insert --
424 * Insert a line into the file.
426 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
429 db_insert(sp, lno, p, len)
430 SCR *sp;
431 db_recno_t lno;
432 CHAR_T *p;
433 size_t len;
435 DBT data, key;
436 DBC *dbcp_put;
437 EXF *ep;
438 int rval;
440 #if defined(DEBUG) && 0
441 vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
442 (u_long)lno, (u_long)len, MIN(len, 20), p);
443 #endif
444 /* Check for no underlying file. */
445 if ((ep = sp->ep) == NULL) {
446 ex_emsg(sp, NULL, EXM_NOFILEYET);
447 return (1);
449 if (ep->l_win && ep->l_win != sp->wp) {
450 ex_emsg(sp, NULL, EXM_LOCKED);
451 return 1;
454 /* Update file. */
455 if (append(sp, lno - 1, p, len) != 0) {
456 msgq(sp, M_DBERR, "005|unable to insert at line %lu",
457 (u_long)lno);
458 return (1);
461 /* Flush the cache, update line count, before screen update. */
462 if (lno >= ep->c_lno)
463 ep->c_lno = OOBLNO;
464 if (ep->c_nlines != OOBLNO)
465 ++ep->c_nlines;
467 /* File now dirty. */
468 if (F_ISSET(ep, F_FIRSTMODIFY))
469 (void)rcv_init(sp);
470 F_SET(ep, F_MODIFIED);
472 /* Log change. */
473 log_line(sp, lno, LOG_LINE_INSERT);
475 /* Update marks, @ and global commands. */
476 rval = 0;
477 if (mark_insdel(sp, LINE_INSERT, lno))
478 rval = 1;
479 if (ex_g_insdel(sp, LINE_INSERT, lno))
480 rval = 1;
482 /* Update screen. */
483 return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
487 * db_set --
488 * Store a line in the file.
490 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
493 db_set(sp, lno, p, len)
494 SCR *sp;
495 db_recno_t lno;
496 CHAR_T *p;
497 size_t len;
499 DBT data, key;
500 EXF *ep;
501 char *fp;
502 size_t flen;
504 #if defined(DEBUG) && 0
505 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
506 (u_long)lno, (u_long)len, MIN(len, 20), p);
507 #endif
508 /* Check for no underlying file. */
509 if ((ep = sp->ep) == NULL) {
510 ex_emsg(sp, NULL, EXM_NOFILEYET);
511 return (1);
513 if (ep->l_win && ep->l_win != sp->wp) {
514 ex_emsg(sp, NULL, EXM_LOCKED);
515 return 1;
518 /* Log before change. */
519 log_line(sp, lno, LOG_LINE_RESET_B);
521 INT2FILE(sp, p, len, fp, flen);
523 /* Update file. */
524 memset(&key, 0, sizeof(key));
525 key.data = &lno;
526 key.size = sizeof(lno);
527 memset(&data, 0, sizeof(data));
528 data.data = fp;
529 data.size = flen;
530 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
531 msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
532 return (1);
535 /* Flush the cache, before logging or screen update. */
536 if (lno == ep->c_lno)
537 ep->c_lno = OOBLNO;
539 /* File now dirty. */
540 if (F_ISSET(ep, F_FIRSTMODIFY))
541 (void)rcv_init(sp);
542 F_SET(ep, F_MODIFIED);
544 /* Log after change. */
545 log_line(sp, lno, LOG_LINE_RESET_F);
547 /* Update screen. */
548 return (scr_update(sp, lno, LINE_RESET, 1));
552 * db_exist --
553 * Return if a line exists.
555 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
558 db_exist(sp, lno)
559 SCR *sp;
560 db_recno_t lno;
562 EXF *ep;
564 /* Check for no underlying file. */
565 if ((ep = sp->ep) == NULL) {
566 ex_emsg(sp, NULL, EXM_NOFILEYET);
567 return (1);
570 if (lno == OOBLNO)
571 return (0);
574 * Check the last-line number cache. Adjust the cached line
575 * number for the lines used by the text input buffers.
577 if (ep->c_nlines != OOBLNO)
578 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
579 ep->c_nlines + (((TEXT *)sp->tiq.cqh_last)->lno -
580 ((TEXT *)sp->tiq.cqh_first)->lno) : ep->c_nlines));
582 /* Go get the line. */
583 return (!db_get(sp, lno, 0, NULL, NULL));
587 * db_last --
588 * Return the number of lines in the file.
590 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
593 db_last(sp, lnop)
594 SCR *sp;
595 db_recno_t *lnop;
597 DBT data, key;
598 DBC *dbcp;
599 EXF *ep;
600 db_recno_t lno;
601 CHAR_T *wp;
602 size_t wlen;
604 /* Check for no underlying file. */
605 if ((ep = sp->ep) == NULL) {
606 ex_emsg(sp, NULL, EXM_NOFILEYET);
607 return (1);
611 * Check the last-line number cache. Adjust the cached line
612 * number for the lines used by the text input buffers.
614 if (ep->c_nlines != OOBLNO) {
615 *lnop = ep->c_nlines;
616 if (F_ISSET(sp, SC_TINPUT))
617 *lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
618 ((TEXT *)sp->tiq.cqh_first)->lno;
619 return (0);
622 memset(&key, 0, sizeof(key));
623 key.data = &lno;
624 key.size = sizeof(lno);
625 memset(&data, 0, sizeof(data));
627 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
628 goto err1;
629 switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
630 case DB_NOTFOUND:
631 *lnop = 0;
632 return (0);
633 default:
634 (void)dbcp->c_close(dbcp);
635 alloc_err:
636 err1:
637 msgq(sp, M_DBERR, "007|unable to get last line");
638 *lnop = 0;
639 return (1);
640 case 0:
643 (void)dbcp->c_close(dbcp);
645 FILE2INT(sp, data.data, data.size, wp, wlen);
647 /* Fill the cache. */
648 BINC_GOTOW(sp, ep->c_lp, ep->c_blen, wlen);
649 MEMCPYW(ep->c_lp, wp, wlen);
650 memcpy(&lno, key.data, sizeof(lno));
651 ep->c_nlines = ep->c_lno = lno;
652 ep->c_len = wlen;
654 /* Return the value. */
655 *lnop = (F_ISSET(sp, SC_TINPUT) &&
656 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
657 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
658 return (0);
662 * db_err --
663 * Report a line error.
665 * PUBLIC: void db_err __P((SCR *, db_recno_t));
667 void
668 db_err(sp, lno)
669 SCR *sp;
670 db_recno_t lno;
672 msgq(sp, M_ERR,
673 "008|Error: unable to retrieve line %lu", (u_long)lno);
677 * scr_update --
678 * Update all of the screens that are backed by the file that
679 * just changed.
681 static int
682 scr_update(sp, lno, op, current)
683 SCR *sp;
684 db_recno_t lno;
685 lnop_t op;
686 int current;
688 EXF *ep;
689 SCR *tsp;
690 WIN *wp;
692 if (F_ISSET(sp, SC_EX))
693 return (0);
695 /* XXXX goes outside of window */
696 ep = sp->ep;
697 if (ep->refcnt != 1)
698 for (wp = sp->gp->dq.cqh_first; wp != (void *)&sp->gp->dq;
699 wp = wp->q.cqe_next)
700 for (tsp = wp->scrq.cqh_first;
701 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
702 if (sp != tsp && tsp->ep == ep)
703 if (vs_change(tsp, lno, op))
704 return (1);
705 return (current ? vs_change(sp, lno, op) : 0);