Another CHAR_T patch.
[nvi.git] / common / db.c
blob371d50a3271ec5851967156df5c4774e2cf7c9ad
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.27 2000/07/14 14:29:15 skimo Exp $ (Berkeley) $Date: 2000/07/14 14:29:15 $";
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;
96 * The underlying recno stuff handles zero by returning NULL, but
97 * have to have an OOB condition for the look-aside into the input
98 * buffer anyway.
100 if (lno == 0)
101 goto err1;
103 /* Check for no underlying file. */
104 if ((ep = sp->ep) == NULL) {
105 ex_emsg(sp, NULL, EXM_NOFILEYET);
106 goto err3;
109 if (LF_ISSET(DBG_NOCACHE))
110 goto nocache;
113 * Look-aside into the TEXT buffers and see if the line we want
114 * is there.
116 if (F_ISSET(sp, SC_TINPUT)) {
117 l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
118 l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
119 if (l1 <= lno && l2 >= lno) {
120 #if defined(DEBUG) && 0
121 vtrace(sp,
122 "retrieve TEXT buffer line %lu\n", (u_long)lno);
123 #endif
124 for (tp = sp->tiq.cqh_first;
125 tp->lno != lno; tp = tp->q.cqe_next);
126 if (lenp != NULL)
127 *lenp = tp->len;
128 if (pp != NULL)
129 *pp = tp->lb;
130 return (0);
133 * Adjust the line number for the number of lines used
134 * by the text input buffers.
136 if (lno > l2)
137 lno -= l2 - l1;
140 /* Look-aside into the cache, and see if the line we want is there. */
141 if (lno == ep->c_lno) {
142 #if defined(DEBUG) && 0
143 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
144 #endif
145 if (lenp != NULL)
146 *lenp = ep->c_len;
147 if (pp != NULL)
148 *pp = ep->c_lp;
149 return (0);
151 ep->c_lno = OOBLNO;
153 nocache:
154 /* Get the line from the underlying database. */
155 memset(&key, 0, sizeof(key));
156 key.data = &lno;
157 key.size = sizeof(lno);
158 memset(&data, 0, sizeof(data));
159 switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
160 default:
161 goto err2;
162 case DB_NOTFOUND:
163 err1: if (LF_ISSET(DBG_FATAL))
164 err2: db_err(sp, lno);
165 err3: if (lenp != NULL)
166 *lenp = 0;
167 if (pp != NULL)
168 *pp = NULL;
169 return (1);
170 case 0:
174 /* Reset the cache. */
175 ep->c_lno = lno;
176 ep->c_len = data.size / sizeof(CHAR_T);
177 ep->c_lp = data.data;
179 #if defined(DEBUG) && 0
180 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
181 #endif
182 if (lenp != NULL)
183 *lenp = data.size / sizeof(CHAR_T);
184 if (pp != NULL)
185 *pp = ep->c_lp;
186 return (0);
190 * db_delete --
191 * Delete a line from the file.
193 * PUBLIC: int db_delete __P((SCR *, db_recno_t));
196 db_delete(sp, lno)
197 SCR *sp;
198 db_recno_t lno;
200 DBT key;
201 EXF *ep;
203 #if defined(DEBUG) && 0
204 vtrace(sp, "delete line %lu\n", (u_long)lno);
205 #endif
206 /* Check for no underlying file. */
207 if ((ep = sp->ep) == NULL) {
208 ex_emsg(sp, NULL, EXM_NOFILEYET);
209 return (1);
212 /* Update marks, @ and global commands. */
213 if (mark_insdel(sp, LINE_DELETE, lno))
214 return (1);
215 if (ex_g_insdel(sp, LINE_DELETE, lno))
216 return (1);
218 /* Log change. */
219 log_line(sp, lno, LOG_LINE_DELETE);
221 /* Update file. */
222 memset(&key, 0, sizeof(key));
223 key.data = &lno;
224 key.size = sizeof(lno);
225 if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
226 msgq(sp, M_DBERR, "003|unable to delete line %lu",
227 (u_long)lno);
228 return (1);
231 /* Flush the cache, update line count, before screen update. */
232 if (lno <= ep->c_lno)
233 ep->c_lno = OOBLNO;
234 if (ep->c_nlines != OOBLNO)
235 --ep->c_nlines;
237 /* File now modified. */
238 if (F_ISSET(ep, F_FIRSTMODIFY))
239 (void)rcv_init(sp);
240 F_SET(ep, F_MODIFIED);
242 /* Update screen. */
243 return (scr_update(sp, lno, LINE_DELETE, 1));
246 /* maybe this could be simpler
248 * DB3 behaves differently from DB1
250 * if lno != 0 just go to lno and put the new line after it
251 * if lno == 0 then if there are any record, put in front of the first
252 * otherwise just append to the end thus creating the first
253 * line
255 static int
256 append(sp, lno, p, len)
257 SCR *sp;
258 db_recno_t lno;
259 CHAR_T *p;
260 size_t len;
262 DBT data, key;
263 DBC *dbcp_put;
264 EXF *ep;
266 ep = sp->ep;
268 memset(&key, 0, sizeof(key));
269 key.data = &lno;
270 key.size = sizeof(lno);
271 memset(&data, 0, sizeof(data));
273 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp_put, 0)) != 0)
274 return 1;
276 if (lno != 0) {
277 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0)
278 goto err2;
280 data.data = p;
281 data.size = len * sizeof(CHAR_T);
282 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_AFTER)) != 0) {
283 err2:
284 (void)dbcp_put->c_close(dbcp_put);
285 return (1);
287 } else {
288 if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
289 if (sp->db_error != DB_NOTFOUND)
290 goto err2;
292 data.data = p;
293 data.size = len * sizeof(CHAR_T);
294 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
295 goto err2;
297 } else {
298 key.data = &lno;
299 key.size = sizeof(lno);
300 data.data = p;
301 data.size = len * sizeof(CHAR_T);
302 if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
303 goto err2;
308 (void)dbcp_put->c_close(dbcp_put);
310 return 0;
314 * db_append --
315 * Append a line into the file.
317 * PUBLIC: int db_append __P((SCR *, int, db_recno_t, CHAR_T *, size_t));
320 db_append(sp, update, lno, p, len)
321 SCR *sp;
322 int update;
323 db_recno_t lno;
324 CHAR_T *p;
325 size_t len;
327 EXF *ep;
328 int rval;
330 #if defined(DEBUG) && 0
331 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
332 #endif
333 /* Check for no underlying file. */
334 if ((ep = sp->ep) == NULL) {
335 ex_emsg(sp, NULL, EXM_NOFILEYET);
336 return (1);
339 /* Update file. */
340 if (append(sp, lno, p, len) != 0) {
341 msgq(sp, M_DBERR, "004|unable to append to line %lu",
342 (u_long)lno);
343 return (1);
346 /* Flush the cache, update line count, before screen update. */
347 if (lno < ep->c_lno)
348 ep->c_lno = OOBLNO;
349 if (ep->c_nlines != OOBLNO)
350 ++ep->c_nlines;
352 /* File now dirty. */
353 if (F_ISSET(ep, F_FIRSTMODIFY))
354 (void)rcv_init(sp);
355 F_SET(ep, F_MODIFIED);
357 /* Log change. */
358 log_line(sp, lno + 1, LOG_LINE_APPEND);
360 /* Update marks, @ and global commands. */
361 rval = 0;
362 if (mark_insdel(sp, LINE_INSERT, lno + 1))
363 rval = 1;
364 if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
365 rval = 1;
368 * Update screen.
370 * XXX
371 * Nasty hack. If multiple lines are input by the user, they aren't
372 * committed until an <ESC> is entered. The problem is the screen was
373 * updated/scrolled as each line was entered. So, when this routine
374 * is called to copy the new lines from the cut buffer into the file,
375 * it has to know not to update the screen again.
377 return (scr_update(sp, lno, LINE_APPEND, update) || rval);
381 * db_insert --
382 * Insert a line into the file.
384 * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
387 db_insert(sp, lno, p, len)
388 SCR *sp;
389 db_recno_t lno;
390 CHAR_T *p;
391 size_t len;
393 DBT data, key;
394 DBC *dbcp_put;
395 EXF *ep;
396 int rval;
398 #if defined(DEBUG) && 0
399 vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
400 (u_long)lno, (u_long)len, MIN(len, 20), p);
401 #endif
402 /* Check for no underlying file. */
403 if ((ep = sp->ep) == NULL) {
404 ex_emsg(sp, NULL, EXM_NOFILEYET);
405 return (1);
408 /* Update file. */
409 if (append(sp, lno - 1, p, len) != 0) {
410 msgq(sp, M_DBERR, "005|unable to insert at line %lu",
411 (u_long)lno);
412 return (1);
415 /* Flush the cache, update line count, before screen update. */
416 if (lno >= ep->c_lno)
417 ep->c_lno = OOBLNO;
418 if (ep->c_nlines != OOBLNO)
419 ++ep->c_nlines;
421 /* File now dirty. */
422 if (F_ISSET(ep, F_FIRSTMODIFY))
423 (void)rcv_init(sp);
424 F_SET(ep, F_MODIFIED);
426 /* Log change. */
427 log_line(sp, lno, LOG_LINE_INSERT);
429 /* Update marks, @ and global commands. */
430 rval = 0;
431 if (mark_insdel(sp, LINE_INSERT, lno))
432 rval = 1;
433 if (ex_g_insdel(sp, LINE_INSERT, lno))
434 rval = 1;
436 /* Update screen. */
437 return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
441 * db_set --
442 * Store a line in the file.
444 * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
447 db_set(sp, lno, p, len)
448 SCR *sp;
449 db_recno_t lno;
450 CHAR_T *p;
451 size_t len;
453 DBT data, key;
454 EXF *ep;
456 #if defined(DEBUG) && 0
457 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
458 (u_long)lno, (u_long)len, MIN(len, 20), p);
459 #endif
461 /* Check for no underlying file. */
462 if ((ep = sp->ep) == NULL) {
463 ex_emsg(sp, NULL, EXM_NOFILEYET);
464 return (1);
467 /* Log before change. */
468 log_line(sp, lno, LOG_LINE_RESET_B);
470 /* Update file. */
471 memset(&key, 0, sizeof(key));
472 key.data = &lno;
473 key.size = sizeof(lno);
474 memset(&data, 0, sizeof(data));
475 data.data = p;
476 data.size = len * sizeof(CHAR_T);
477 if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
478 msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
479 return (1);
482 /* Flush the cache, before logging or screen update. */
483 if (lno == ep->c_lno)
484 ep->c_lno = OOBLNO;
486 /* File now dirty. */
487 if (F_ISSET(ep, F_FIRSTMODIFY))
488 (void)rcv_init(sp);
489 F_SET(ep, F_MODIFIED);
491 /* Log after change. */
492 log_line(sp, lno, LOG_LINE_RESET_F);
494 /* Update screen. */
495 return (scr_update(sp, lno, LINE_RESET, 1));
499 * db_exist --
500 * Return if a line exists.
502 * PUBLIC: int db_exist __P((SCR *, db_recno_t));
505 db_exist(sp, lno)
506 SCR *sp;
507 db_recno_t lno;
509 EXF *ep;
511 /* Check for no underlying file. */
512 if ((ep = sp->ep) == NULL) {
513 ex_emsg(sp, NULL, EXM_NOFILEYET);
514 return (1);
517 if (lno == OOBLNO)
518 return (0);
521 * Check the last-line number cache. Adjust the cached line
522 * number for the lines used by the text input buffers.
524 if (ep->c_nlines != OOBLNO)
525 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
526 ep->c_nlines + (((TEXT *)sp->tiq.cqh_last)->lno -
527 ((TEXT *)sp->tiq.cqh_first)->lno) : ep->c_nlines));
529 /* Go get the line. */
530 return (!db_get(sp, lno, 0, NULL, NULL));
534 * db_last --
535 * Return the number of lines in the file.
537 * PUBLIC: int db_last __P((SCR *, db_recno_t *));
540 db_last(sp, lnop)
541 SCR *sp;
542 db_recno_t *lnop;
544 DBT data, key;
545 DBC *dbcp;
546 EXF *ep;
547 db_recno_t lno;
549 /* Check for no underlying file. */
550 if ((ep = sp->ep) == NULL) {
551 ex_emsg(sp, NULL, EXM_NOFILEYET);
552 return (1);
556 * Check the last-line number cache. Adjust the cached line
557 * number for the lines used by the text input buffers.
559 if (ep->c_nlines != OOBLNO) {
560 *lnop = ep->c_nlines;
561 if (F_ISSET(sp, SC_TINPUT))
562 *lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
563 ((TEXT *)sp->tiq.cqh_first)->lno;
564 return (0);
567 memset(&key, 0, sizeof(key));
568 key.data = &lno;
569 key.size = sizeof(lno);
570 memset(&data, 0, sizeof(data));
572 if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
573 goto err1;
574 switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
575 case DB_NOTFOUND:
576 *lnop = 0;
577 return (0);
578 default:
579 (void)dbcp->c_close(dbcp);
580 err1:
581 msgq(sp, M_DBERR, "007|unable to get last line");
582 *lnop = 0;
583 return (1);
584 case 0:
587 (void)dbcp->c_close(dbcp);
589 /* Fill the cache. */
590 memcpy(&lno, key.data, sizeof(lno));
591 ep->c_nlines = ep->c_lno = lno;
592 ep->c_len = data.size / sizeof(CHAR_T);
593 ep->c_lp = data.data;
595 /* Return the value. */
596 *lnop = (F_ISSET(sp, SC_TINPUT) &&
597 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
598 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
599 return (0);
603 * db_err --
604 * Report a line error.
606 * PUBLIC: void db_err __P((SCR *, db_recno_t));
608 void
609 db_err(sp, lno)
610 SCR *sp;
611 db_recno_t lno;
613 msgq(sp, M_ERR,
614 "008|Error: unable to retrieve line %lu", (u_long)lno);
618 * scr_update --
619 * Update all of the screens that are backed by the file that
620 * just changed.
622 static int
623 scr_update(sp, lno, op, current)
624 SCR *sp;
625 db_recno_t lno;
626 lnop_t op;
627 int current;
629 EXF *ep;
630 SCR *tsp;
631 WIN *wp;
633 if (F_ISSET(sp, SC_EX))
634 return (0);
636 /* XXXX goes outside of window */
637 ep = sp->ep;
638 if (ep->refcnt != 1)
639 for (wp = sp->gp->dq.cqh_first; wp != (void *)&sp->gp->dq;
640 wp = wp->q.cqe_next)
641 for (tsp = wp->scrq.cqh_first;
642 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
643 if (sp != tsp && tsp->ep == ep)
644 if (vs_change(tsp, lno, op))
645 return (1);
646 return (current ? vs_change(sp, lno, op) : 0);