avoid casts in left hand sides of assignments.
[nvi.git] / common / db.c
blobdcaa08e1dbfc618c7bca52653833cf0dc36161cc
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.48 2002/06/08 19:32:52 skimo Exp $ (Berkeley) $Date: 2002/06/08 19:32:52 $";
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 append __P((SCR*, db_recno_t, CHAR_T*, size_t, lnop_t, int));
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)
83 /* Line number. */ /* Pointer store. */ /* Length store. */
85 DBT data, key;
86 EXF *ep;
87 TEXT *tp;
88 db_recno_t l1, l2;
89 CHAR_T *wp;
90 size_t wlen;
91 size_t nlen;
94 * The underlying recno stuff handles zero by returning NULL, but
95 * have to have an OOB condition for the look-aside into the input
96 * buffer anyway.
98 if (lno == 0)
99 goto err1;
101 /* Check for no underlying file. */
102 if ((ep = sp->ep) == NULL) {
103 ex_emsg(sp, NULL, EXM_NOFILEYET);
104 goto err3;
107 if (LF_ISSET(DBG_NOCACHE))
108 goto nocache;
111 * Look-aside into the TEXT buffers and see if the line we want
112 * is there.
114 if (F_ISSET(sp, SC_TINPUT)) {
115 l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
116 l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
117 if (l1 <= lno && l2 >= lno) {
118 #if defined(DEBUG) && 0
119 vtrace(sp,
120 "retrieve TEXT buffer line %lu\n", (u_long)lno);
121 #endif
122 for (tp = sp->tiq.cqh_first;
123 tp->lno != lno; tp = tp->q.cqe_next);
124 if (lenp != NULL)
125 *lenp = tp->len;
126 if (pp != NULL)
127 *pp = tp->lb;
128 return (0);
131 * Adjust the line number for the number of lines used
132 * by the text input buffers.
134 if (lno > l2)
135 lno -= l2 - l1;
138 /* Look-aside into the cache, and see if the line we want is there. */
140 * Line cache will not work if different screens view the same
141 * file with different encodings.
142 * Multiple threads accessing the same cache can be a problem as
143 * well.
144 * So, line cache is (temporarily) disabled.
146 if (lno == sp->c_lno) {
147 #if defined(DEBUG) && 0
148 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
149 #endif
150 if (lenp != NULL)
151 *lenp = sp->c_len;
152 if (pp != NULL)
153 *pp = sp->c_lp;
154 return (0);
156 sp->c_lno = OOBLNO;
158 nocache:
159 nlen = 1024;
160 retry:
161 /* data.size contains length in bytes */
162 BINC_GOTO(sp, CHAR_T, sp->c_lp, sp->c_blen, nlen);
164 /* Get the line from the underlying database. */
165 memset(&key, 0, sizeof(key));
166 key.data = &lno;
167 key.size = sizeof(lno);
168 memset(&data, 0, sizeof(data));
169 data.data = sp->c_lp;
170 data.ulen = sp->c_blen;
171 data.flags = DB_DBT_USERMEM;
172 switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
173 case DB_BUFFER_SMALL:
174 nlen = data.size;
175 goto retry;
176 default:
177 goto err2;
178 case DB_NOTFOUND:
179 err1: if (LF_ISSET(DBG_FATAL))
180 err2: db_err(sp, lno);
181 alloc_err:
182 err3: if (lenp != NULL)
183 *lenp = 0;
184 if (pp != NULL)
185 *pp = NULL;
186 return (1);
187 case 0:
191 if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
192 if (!F_ISSET(sp, SC_CONV_ERROR)) {
193 F_SET(sp, SC_CONV_ERROR);
194 msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
196 goto err3;
199 /* Reset the cache. */
200 if (wp != data.data) {
201 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
202 MEMCPYW(sp->c_lp, wp, wlen);
204 sp->c_lno = lno;
205 sp->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 = sp->c_lp;
214 return (0);
218 * db_delete --
219 * Delete a line from the file.
221 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
224 db_delete(SCR *sp, db_recno_t lno)
226 DBT key;
227 EXF *ep;
229 #if defined(DEBUG) && 0
230 vtrace(sp, "delete line %lu\n", (u_long)lno);
231 #endif
232 /* Check for no underlying file. */
233 if ((ep = sp->ep) == NULL) {
234 ex_emsg(sp, NULL, EXM_NOFILEYET);
235 return (1);
237 if (ep->l_win && ep->l_win != sp->wp) {
238 ex_emsg(sp, NULL, EXM_LOCKED);
239 return 1;
242 /* Update marks, @ and global commands. */
243 if (line_insdel(sp, LINE_DELETE, lno))
244 return 1;
246 /* Log before change. */
247 log_line(sp, lno, LOG_LINE_DELETE_B);
249 /* Update file. */
250 memset(&key, 0, sizeof(key));
251 key.data = &lno;
252 key.size = sizeof(lno);
253 if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
254 msgq(sp, M_DBERR, "003|unable to delete line %lu",
255 (u_long)lno);
256 return (1);
259 /* Flush the cache, update line count, before screen update. */
260 update_cache(sp, LINE_DELETE, lno);
262 /* File now modified. */
263 if (F_ISSET(ep, F_FIRSTMODIFY))
264 (void)rcv_init(sp);
265 F_SET(ep, F_MODIFIED);
267 /* Log after change. */
268 log_line(sp, lno, LOG_LINE_DELETE_F);
270 /* Update screen. */
271 return (scr_update(sp, lno, LINE_DELETE, 1));
274 /* maybe this could be simpler
276 * DB3 behaves differently from DB1
278 * if lno != 0 just go to lno and put the new line after it
279 * if lno == 0 then if there are any record, put in front of the first
280 * otherwise just append to the end thus creating the first
281 * line
283 static int
284 append(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len, lnop_t op, int update)
286 DBT data, key;
287 DBC *dbcp_put;
288 EXF *ep;
289 char *fp;
290 size_t flen;
291 int rval;
293 /* Check for no underlying file. */
294 if ((ep = sp->ep) == NULL) {
295 ex_emsg(sp, NULL, EXM_NOFILEYET);
296 return (1);
298 if (ep->l_win && ep->l_win != sp->wp) {
299 ex_emsg(sp, NULL, EXM_LOCKED);
300 return 1;
303 /* Log before change. */
304 log_line(sp, lno + 1, LOG_LINE_APPEND_B);
306 /* Update file. */
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 msgq(sp, M_DBERR,
327 (op == LINE_APPEND)
328 ? "004|unable to append to line %lu"
329 : "005|unable to insert at line %lu",
330 (u_long)lno);
331 return (1);
333 } else {
334 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
335 if (sp->db_error != DB_NOTFOUND)
336 goto err2;
338 data.data = fp;
339 data.size = flen;
340 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
341 goto err2;
343 } else {
344 key.data = &lno;
345 key.size = sizeof(lno);
346 data.data = fp;
347 data.size = flen;
348 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
349 goto err2;
354 (void)dbcp_put->c_close(dbcp_put);
356 /* Flush the cache, update line count, before screen update. */
357 update_cache(sp, LINE_INSERT, lno);
359 /* File now dirty. */
360 if (F_ISSET(ep, F_FIRSTMODIFY))
361 (void)rcv_init(sp);
362 F_SET(ep, F_MODIFIED);
364 /* Log after change. */
365 log_line(sp, lno + 1, LOG_LINE_APPEND_F);
367 /* Update marks, @ and global commands. */
368 rval = line_insdel(sp, LINE_INSERT, lno + 1);
371 * Update screen.
373 * comment copied from db_append
374 * XXX
375 * Nasty hack. If multiple lines are input by the user, they aren't
376 * committed until an <ESC> is entered. The problem is the screen was
377 * updated/scrolled as each line was entered. So, when this routine
378 * is called to copy the new lines from the cut buffer into the file,
379 * it has to know not to update the screen again.
381 return (scr_update(sp, lno + 1, LINE_INSERT, update) || rval);
385 * db_append --
386 * Append a line into the file.
388 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
391 db_append(SCR *sp, int update, db_recno_t lno, CHAR_T *p, size_t len)
393 #if defined(DEBUG) && 0
394 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
395 #endif
397 /* Update file. */
398 return append(sp, lno, p, len, LINE_APPEND, update);
402 * db_insert --
403 * Insert a line into the file.
405 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
408 db_insert(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
410 #if defined(DEBUG) && 0
411 vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
412 (u_long)lno, (u_long)len, MIN(len, 20), p);
413 #endif
414 return append(sp, lno - 1, p, len, LINE_INSERT, 1);
418 * db_set --
419 * Store a line in the file.
421 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
424 db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
426 DBT data, key;
427 EXF *ep;
428 char *fp;
429 size_t flen;
431 #if defined(DEBUG) && 0
432 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
433 (u_long)lno, (u_long)len, MIN(len, 20), p);
434 #endif
435 /* Check for no underlying file. */
436 if ((ep = sp->ep) == NULL) {
437 ex_emsg(sp, NULL, EXM_NOFILEYET);
438 return (1);
440 if (ep->l_win && ep->l_win != sp->wp) {
441 ex_emsg(sp, NULL, EXM_LOCKED);
442 return 1;
445 /* Log before change. */
446 log_line(sp, lno, LOG_LINE_RESET_B);
448 INT2FILE(sp, p, len, fp, flen);
450 /* Update file. */
451 memset(&key, 0, sizeof(key));
452 key.data = &lno;
453 key.size = sizeof(lno);
454 memset(&data, 0, sizeof(data));
455 data.data = fp;
456 data.size = flen;
457 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
458 msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
459 return (1);
462 /* Flush the cache, update line count, before 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 DBC *dbcp;
521 EXF *ep;
522 db_recno_t lno;
523 CHAR_T *wp;
524 size_t wlen;
526 /* Check for no underlying file. */
527 if ((ep = sp->ep) == NULL) {
528 ex_emsg(sp, NULL, EXM_NOFILEYET);
529 return (1);
533 * Check the last-line number cache. Adjust the cached line
534 * number for the lines used by the text input buffers.
536 if (ep->c_nlines != OOBLNO) {
537 *lnop = ep->c_nlines;
538 if (F_ISSET(sp, SC_TINPUT))
539 *lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
540 ((TEXT *)sp->tiq.cqh_first)->lno;
541 return (0);
544 memset(&key, 0, sizeof(key));
545 key.data = &lno;
546 key.size = sizeof(lno);
547 memset(&data, 0, sizeof(data));
549 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
550 goto err1;
551 switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
552 case DB_NOTFOUND:
553 *lnop = 0;
554 return (0);
555 default:
556 (void)dbcp->c_close(dbcp);
557 alloc_err:
558 err1:
559 msgq(sp, M_DBERR, "007|unable to get last line");
560 *lnop = 0;
561 return (1);
562 case 0:
566 memcpy(&lno, key.data, sizeof(lno));
568 if (lno != sp->c_lno) {
569 FILE2INT(sp, data.data, data.size, wp, wlen);
571 /* Fill the cache. */
572 BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
573 MEMCPYW(sp->c_lp, wp, wlen);
574 sp->c_lno = lno;
575 sp->c_len = wlen;
577 ep->c_nlines = lno;
579 (void)dbcp->c_close(dbcp);
581 /* Return the value. */
582 *lnop = (F_ISSET(sp, SC_TINPUT) &&
583 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
584 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
585 return (0);
589 * db_err --
590 * Report a line error.
592 * PUBLIC: void db_err __P((SCR *, db_recno_t));
594 void
595 db_err(SCR *sp, db_recno_t lno)
597 msgq(sp, M_ERR,
598 "008|Error: unable to retrieve line %lu", (u_long)lno);
602 * scr_update --
603 * Update all of the screens that are backed by the file that
604 * just changed.
606 * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
607 * PUBLIC: lnop_t op, int current));
610 scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
612 EXF *ep;
613 SCR *tsp;
614 WIN *wp;
616 if (F_ISSET(sp, SC_EX))
617 return (0);
619 /* XXXX goes outside of window */
620 ep = sp->ep;
621 if (ep->refcnt != 1)
622 for (wp = sp->gp->dq.cqh_first; wp != (void *)&sp->gp->dq;
623 wp = wp->q.cqe_next)
624 for (tsp = wp->scrq.cqh_first;
625 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
626 if (sp != tsp && tsp->ep == ep)
627 if (vs_change(tsp, lno, op))
628 return (1);
629 return (current ? vs_change(sp, lno, op) : 0);
633 * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
635 void
636 update_cache(SCR *sp, lnop_t op, db_recno_t lno)
638 SCR* scrp;
639 EXF *ep;
641 ep = sp->ep;
643 /* Flush the cache, update line count, before screen update. */
644 /* The flushing is probably not needed, since it was incorrect
645 * for db_insert. It might be better to adjust it, like
646 * marks, @ and global
648 for (scrp = ep->scrq.cqh_first; scrp != (void *)&ep->scrq;
649 scrp = scrp->eq.cqe_next)
650 switch (op) {
651 case LINE_INSERT:
652 case LINE_DELETE:
653 if (lno <= scrp->c_lno)
654 scrp->c_lno = OOBLNO;
655 break;
656 case LINE_RESET:
657 if (lno == scrp->c_lno)
658 scrp->c_lno = OOBLNO;
659 break;
662 if (ep->c_nlines != OOBLNO)
663 switch (op) {
664 case LINE_INSERT:
665 ++ep->c_nlines;
666 break;
667 case LINE_DELETE:
668 --ep->c_nlines;
669 break;
674 * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
677 line_insdel(SCR *sp, lnop_t op, db_recno_t lno)
679 int rval;
681 /* Update marks, @ and global commands. */
682 rval = 0;
683 if (mark_insdel(sp, op, lno))
684 rval = 1;
685 if (ex_g_insdel(sp, op, lno))
686 rval = 1;
688 return rval;