align CHAR_T string in log
[nvi.git] / common / db.c
blob5c7dc92c90b5df609b7a770e2903a78d9d98e054
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.37 2001/05/10 19:32:47 skimo Exp $ (Berkeley) $Date: 2001/05/10 19:32:47 $";
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;
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
101 * buffer anyway.
103 if (lno == 0)
104 goto err1;
106 /* Check for no underlying file. */
107 if ((ep = sp->ep) == NULL) {
108 ex_emsg(sp, NULL, EXM_NOFILEYET);
109 goto err3;
112 if (LF_ISSET(DBG_NOCACHE))
113 goto nocache;
116 * Look-aside into the TEXT buffers and see if the line we want
117 * is there.
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
124 vtrace(sp,
125 "retrieve TEXT buffer line %lu\n", (u_long)lno);
126 #endif
127 for (tp = sp->tiq.cqh_first;
128 tp->lno != lno; tp = tp->q.cqe_next);
129 if (lenp != NULL)
130 *lenp = tp->len;
131 if (pp != NULL)
132 *pp = tp->lb;
133 return (0);
136 * Adjust the line number for the number of lines used
137 * by the text input buffers.
139 if (lno > l2)
140 lno -= l2 - l1;
143 /* Look-aside into the cache, and see if the line we want is there. */
145 * Line cache will not work if different screens view the same
146 * file with different encodings.
147 * Multiple threads accessing the same cache can be a problem as
148 * well.
149 * So, line cache is (temporarily) disabled.
151 if (0 && lno == ep->c_lno) {
152 #if defined(DEBUG) && 0
153 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
154 #endif
155 if (lenp != NULL)
156 *lenp = ep->c_len;
157 if (pp != NULL)
158 *pp = ep->c_lp;
159 return (0);
161 ep->c_lno = OOBLNO;
163 nocache:
164 nlen = 1024;
165 retry:
166 /* data.size contains length in bytes */
167 BINC_GOTO(sp, (char *)ep->c_lp, ep->c_blen, nlen);
169 /* Get the line from the underlying database. */
170 memset(&key, 0, sizeof(key));
171 key.data = &lno;
172 key.size = sizeof(lno);
173 memset(&data, 0, sizeof(data));
174 data.data = ep->c_lp;
175 data.ulen = ep->c_blen;
176 data.flags = DB_DBT_USERMEM;
177 switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
178 case ENOMEM:
179 nlen = data.size;
180 goto retry;
181 default:
182 goto err2;
183 case DB_NOTFOUND:
184 err1: if (LF_ISSET(DBG_FATAL))
185 err2: db_err(sp, lno);
186 alloc_err:
187 err3: if (lenp != NULL)
188 *lenp = 0;
189 if (pp != NULL)
190 *pp = NULL;
191 return (1);
192 case 0:
196 if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
197 if (!F_ISSET(sp, SC_CONV_ERROR)) {
198 F_SET(sp, SC_CONV_ERROR);
199 msgq(sp, M_ERR, "321|Conversion error on line %d", lno);
201 goto err3;
204 /* Reset the cache. */
205 if (wp != data.data) {
206 BINC_GOTOW(sp, ep->c_lp, ep->c_blen, wlen);
207 MEMCPYW(ep->c_lp, wp, wlen);
209 ep->c_lno = lno;
210 ep->c_len = wlen;
212 #if defined(DEBUG) && 0
213 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
214 #endif
215 if (lenp != NULL)
216 *lenp = wlen;
217 if (pp != NULL)
218 *pp = ep->c_lp;
219 return (0);
223 * db_delete --
224 * Delete a line from the file.
226 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
229 db_delete(sp, lno)
230 SCR *sp;
231 db_recno_t lno;
233 DBT key;
234 EXF *ep;
236 #if defined(DEBUG) && 0
237 vtrace(sp, "delete line %lu\n", (u_long)lno);
238 #endif
239 /* Check for no underlying file. */
240 if ((ep = sp->ep) == NULL) {
241 ex_emsg(sp, NULL, EXM_NOFILEYET);
242 return (1);
244 if (ep->l_win && ep->l_win != sp->wp) {
245 ex_emsg(sp, NULL, EXM_LOCKED);
246 return 1;
249 /* Update marks, @ and global commands. */
250 if (mark_insdel(sp, LINE_DELETE, lno))
251 return (1);
252 if (ex_g_insdel(sp, LINE_DELETE, lno))
253 return (1);
255 /* Log change. */
256 log_line(sp, lno, LOG_LINE_DELETE);
258 /* Update file. */
259 memset(&key, 0, sizeof(key));
260 key.data = &lno;
261 key.size = sizeof(lno);
262 if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
263 msgq(sp, M_DBERR, "003|unable to delete line %lu",
264 (u_long)lno);
265 return (1);
268 /* Flush the cache, update line count, before screen update. */
269 if (lno <= ep->c_lno)
270 ep->c_lno = OOBLNO;
271 if (ep->c_nlines != OOBLNO)
272 --ep->c_nlines;
274 /* File now modified. */
275 if (F_ISSET(ep, F_FIRSTMODIFY))
276 (void)rcv_init(sp);
277 F_SET(ep, F_MODIFIED);
279 /* Update screen. */
280 return (scr_update(sp, lno, LINE_DELETE, 1));
283 /* maybe this could be simpler
285 * DB3 behaves differently from DB1
287 * if lno != 0 just go to lno and put the new line after it
288 * if lno == 0 then if there are any record, put in front of the first
289 * otherwise just append to the end thus creating the first
290 * line
292 static int
293 append(sp, lno, p, len)
294 SCR *sp;
295 db_recno_t lno;
296 CHAR_T *p;
297 size_t len;
299 DBT data, key;
300 DBC *dbcp_put;
301 EXF *ep;
302 char *fp;
303 size_t flen;
305 ep = sp->ep;
307 memset(&key, 0, sizeof(key));
308 key.data = &lno;
309 key.size = sizeof(lno);
310 memset(&data, 0, sizeof(data));
312 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp_put, 0)) != 0)
313 return 1;
315 INT2FILE(sp, p, len, fp, flen);
317 if (lno != 0) {
318 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0)
319 goto err2;
321 data.data = fp;
322 data.size = flen;
323 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_AFTER)) != 0) {
324 err2:
325 (void)dbcp_put->c_close(dbcp_put);
326 return (1);
328 } else {
329 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
330 if (sp->db_error != DB_NOTFOUND)
331 goto err2;
333 data.data = fp;
334 data.size = flen;
335 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
336 goto err2;
338 } else {
339 key.data = &lno;
340 key.size = sizeof(lno);
341 data.data = fp;
342 data.size = flen;
343 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
344 goto err2;
349 (void)dbcp_put->c_close(dbcp_put);
351 return 0;
355 * db_append --
356 * Append a line into the file.
358 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
361 db_append(sp, update, lno, p, len)
362 SCR *sp;
363 int update;
364 db_recno_t lno;
365 CHAR_T *p;
366 size_t len;
368 EXF *ep;
369 int rval;
371 #if defined(DEBUG) && 0
372 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
373 #endif
374 /* Check for no underlying file. */
375 if ((ep = sp->ep) == NULL) {
376 ex_emsg(sp, NULL, EXM_NOFILEYET);
377 return (1);
379 if (ep->l_win && ep->l_win != sp->wp) {
380 ex_emsg(sp, NULL, EXM_LOCKED);
381 return 1;
384 /* Update file. */
385 if (append(sp, lno, p, len) != 0) {
386 msgq(sp, M_DBERR, "004|unable to append to line %lu",
387 (u_long)lno);
388 return (1);
391 /* Flush the cache, update line count, before screen update. */
392 if (lno < ep->c_lno)
393 ep->c_lno = OOBLNO;
394 if (ep->c_nlines != OOBLNO)
395 ++ep->c_nlines;
397 /* File now dirty. */
398 if (F_ISSET(ep, F_FIRSTMODIFY))
399 (void)rcv_init(sp);
400 F_SET(ep, F_MODIFIED);
402 /* Log change. */
403 log_line(sp, lno + 1, LOG_LINE_APPEND);
405 /* Update marks, @ and global commands. */
406 rval = 0;
407 if (mark_insdel(sp, LINE_INSERT, lno + 1))
408 rval = 1;
409 if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
410 rval = 1;
413 * Update screen.
415 * XXX
416 * Nasty hack. If multiple lines are input by the user, they aren't
417 * committed until an <ESC> is entered. The problem is the screen was
418 * updated/scrolled as each line was entered. So, when this routine
419 * is called to copy the new lines from the cut buffer into the file,
420 * it has to know not to update the screen again.
422 return (scr_update(sp, lno, LINE_APPEND, update) || rval);
426 * db_insert --
427 * Insert a line into the file.
429 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
432 db_insert(sp, lno, p, len)
433 SCR *sp;
434 db_recno_t lno;
435 CHAR_T *p;
436 size_t len;
438 DBT data, key;
439 DBC *dbcp_put;
440 EXF *ep;
441 int rval;
443 #if defined(DEBUG) && 0
444 vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
445 (u_long)lno, (u_long)len, MIN(len, 20), p);
446 #endif
447 /* Check for no underlying file. */
448 if ((ep = sp->ep) == NULL) {
449 ex_emsg(sp, NULL, EXM_NOFILEYET);
450 return (1);
452 if (ep->l_win && ep->l_win != sp->wp) {
453 ex_emsg(sp, NULL, EXM_LOCKED);
454 return 1;
457 /* Update file. */
458 if (append(sp, lno - 1, p, len) != 0) {
459 msgq(sp, M_DBERR, "005|unable to insert at line %lu",
460 (u_long)lno);
461 return (1);
464 /* Flush the cache, update line count, before screen update. */
465 if (lno >= ep->c_lno)
466 ep->c_lno = OOBLNO;
467 if (ep->c_nlines != OOBLNO)
468 ++ep->c_nlines;
470 /* File now dirty. */
471 if (F_ISSET(ep, F_FIRSTMODIFY))
472 (void)rcv_init(sp);
473 F_SET(ep, F_MODIFIED);
475 /* Log change. */
476 log_line(sp, lno, LOG_LINE_INSERT);
478 /* Update marks, @ and global commands. */
479 rval = 0;
480 if (mark_insdel(sp, LINE_INSERT, lno))
481 rval = 1;
482 if (ex_g_insdel(sp, LINE_INSERT, lno))
483 rval = 1;
485 /* Update screen. */
486 return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
490 * db_set --
491 * Store a line in the file.
493 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
496 db_set(sp, lno, p, len)
497 SCR *sp;
498 db_recno_t lno;
499 CHAR_T *p;
500 size_t len;
502 DBT data, key;
503 EXF *ep;
504 char *fp;
505 size_t flen;
507 #if defined(DEBUG) && 0
508 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
509 (u_long)lno, (u_long)len, MIN(len, 20), p);
510 #endif
511 /* Check for no underlying file. */
512 if ((ep = sp->ep) == NULL) {
513 ex_emsg(sp, NULL, EXM_NOFILEYET);
514 return (1);
516 if (ep->l_win && ep->l_win != sp->wp) {
517 ex_emsg(sp, NULL, EXM_LOCKED);
518 return 1;
521 /* Log before change. */
522 log_line(sp, lno, LOG_LINE_RESET_B);
524 INT2FILE(sp, p, len, fp, flen);
526 /* Update file. */
527 memset(&key, 0, sizeof(key));
528 key.data = &lno;
529 key.size = sizeof(lno);
530 memset(&data, 0, sizeof(data));
531 data.data = fp;
532 data.size = flen;
533 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
534 msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
535 return (1);
538 /* Flush the cache, before logging or screen update. */
539 if (lno == ep->c_lno)
540 ep->c_lno = OOBLNO;
542 /* File now dirty. */
543 if (F_ISSET(ep, F_FIRSTMODIFY))
544 (void)rcv_init(sp);
545 F_SET(ep, F_MODIFIED);
547 /* Log after change. */
548 log_line(sp, lno, LOG_LINE_RESET_F);
550 /* Update screen. */
551 return (scr_update(sp, lno, LINE_RESET, 1));
555 * db_exist --
556 * Return if a line exists.
558 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
561 db_exist(sp, lno)
562 SCR *sp;
563 db_recno_t lno;
565 EXF *ep;
567 /* Check for no underlying file. */
568 if ((ep = sp->ep) == NULL) {
569 ex_emsg(sp, NULL, EXM_NOFILEYET);
570 return (1);
573 if (lno == OOBLNO)
574 return (0);
577 * Check the last-line number cache. Adjust the cached line
578 * number for the lines used by the text input buffers.
580 if (ep->c_nlines != OOBLNO)
581 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
582 ep->c_nlines + (((TEXT *)sp->tiq.cqh_last)->lno -
583 ((TEXT *)sp->tiq.cqh_first)->lno) : ep->c_nlines));
585 /* Go get the line. */
586 return (!db_get(sp, lno, 0, NULL, NULL));
590 * db_last --
591 * Return the number of lines in the file.
593 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
596 db_last(sp, lnop)
597 SCR *sp;
598 db_recno_t *lnop;
600 DBT data, key;
601 DBC *dbcp;
602 EXF *ep;
603 db_recno_t lno;
604 CHAR_T *wp;
605 size_t wlen;
607 /* Check for no underlying file. */
608 if ((ep = sp->ep) == NULL) {
609 ex_emsg(sp, NULL, EXM_NOFILEYET);
610 return (1);
614 * Check the last-line number cache. Adjust the cached line
615 * number for the lines used by the text input buffers.
617 if (ep->c_nlines != OOBLNO) {
618 *lnop = ep->c_nlines;
619 if (F_ISSET(sp, SC_TINPUT))
620 *lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
621 ((TEXT *)sp->tiq.cqh_first)->lno;
622 return (0);
625 memset(&key, 0, sizeof(key));
626 key.data = &lno;
627 key.size = sizeof(lno);
628 memset(&data, 0, sizeof(data));
630 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
631 goto err1;
632 switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
633 case DB_NOTFOUND:
634 *lnop = 0;
635 return (0);
636 default:
637 (void)dbcp->c_close(dbcp);
638 alloc_err:
639 err1:
640 msgq(sp, M_DBERR, "007|unable to get last line");
641 *lnop = 0;
642 return (1);
643 case 0:
647 memcpy(&lno, key.data, sizeof(lno));
649 if (lno != ep->c_lno) {
650 FILE2INT(sp, data.data, data.size, wp, wlen);
652 /* Fill the cache. */
653 if (wp != data.data) {
654 BINC_GOTOW(sp, ep->c_lp, ep->c_blen, wlen);
655 MEMCPYW(ep->c_lp, wp, wlen);
657 ep->c_lno = lno;
658 ep->c_len = wlen;
660 ep->c_nlines = lno;
662 (void)dbcp->c_close(dbcp);
664 /* Return the value. */
665 *lnop = (F_ISSET(sp, SC_TINPUT) &&
666 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
667 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
668 return (0);
672 * db_err --
673 * Report a line error.
675 * PUBLIC: void db_err __P((SCR *, db_recno_t));
677 void
678 db_err(sp, lno)
679 SCR *sp;
680 db_recno_t lno;
682 msgq(sp, M_ERR,
683 "008|Error: unable to retrieve line %lu", (u_long)lno);
687 * scr_update --
688 * Update all of the screens that are backed by the file that
689 * just changed.
691 static int
692 scr_update(sp, lno, op, current)
693 SCR *sp;
694 db_recno_t lno;
695 lnop_t op;
696 int current;
698 EXF *ep;
699 SCR *tsp;
700 WIN *wp;
702 if (F_ISSET(sp, SC_EX))
703 return (0);
705 /* XXXX goes outside of window */
706 ep = sp->ep;
707 if (ep->refcnt != 1)
708 for (wp = sp->gp->dq.cqh_first; wp != (void *)&sp->gp->dq;
709 wp = wp->q.cqe_next)
710 for (tsp = wp->scrq.cqh_first;
711 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
712 if (sp != tsp && tsp->ep == ep)
713 if (vs_change(tsp, lno, op))
714 return (1);
715 return (current ? vs_change(sp, lno, op) : 0);