add VI_SEL_END, VI_SEL_START, SI_SELECT
[nvi.git] / common / db.c
blobe9f7eada142b55e539cd05661d3418e227a3da4b
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.23 1996/12/17 14:50:09 bostic Exp $ (Berkeley) $Date: 1996/12/17 14:50:09 $";
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 *, recno_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 *, recno_t, char **, size_t *, int *));
37 int
38 db_eget(sp, lno, pp, lenp, isemptyp)
39 SCR *sp;
40 recno_t lno; /* Line number. */
41 char **pp; /* Pointer store. */
42 size_t *lenp; /* Length store. */
43 int *isemptyp;
45 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 *, recno_t, u_int32_t, char **, size_t *));
81 int
82 db_get(sp, lno, flags, pp, lenp)
83 SCR *sp;
84 recno_t lno; /* Line number. */
85 u_int32_t flags;
86 char **pp; /* Pointer store. */
87 size_t *lenp; /* Length store. */
89 DBT data, key;
90 EXF *ep;
91 TEXT *tp;
92 recno_t l1, l2;
95 * The underlying recno stuff handles zero by returning NULL, but
96 * have to have an OOB condition for the look-aside into the input
97 * buffer anyway.
99 if (lno == 0)
100 goto err1;
102 /* Check for no underlying file. */
103 if ((ep = sp->ep) == NULL) {
104 ex_emsg(sp, NULL, EXM_NOFILEYET);
105 goto err3;
108 if (LF_ISSET(DBG_NOCACHE))
109 goto nocache;
112 * Look-aside into the TEXT buffers and see if the line we want
113 * is there.
115 if (F_ISSET(sp, SC_TINPUT)) {
116 l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
117 l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
118 if (l1 <= lno && l2 >= lno) {
119 #if defined(DEBUG) && 0
120 vtrace(sp,
121 "retrieve TEXT buffer line %lu\n", (u_long)lno);
122 #endif
123 for (tp = sp->tiq.cqh_first;
124 tp->lno != lno; tp = tp->q.cqe_next);
125 if (lenp != NULL)
126 *lenp = tp->len;
127 if (pp != NULL)
128 *pp = tp->lb;
129 return (0);
132 * Adjust the line number for the number of lines used
133 * by the text input buffers.
135 if (lno > l2)
136 lno -= l2 - l1;
139 /* Look-aside into the cache, and see if the line we want is there. */
140 if (lno == ep->c_lno) {
141 #if defined(DEBUG) && 0
142 vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
143 #endif
144 if (lenp != NULL)
145 *lenp = ep->c_len;
146 if (pp != NULL)
147 *pp = ep->c_lp;
148 return (0);
150 ep->c_lno = OOBLNO;
152 nocache:
153 /* Get the line from the underlying database. */
154 key.data = &lno;
155 key.size = sizeof(lno);
156 switch (ep->db->get(ep->db, &key, &data, 0)) {
157 case -1:
158 goto err2;
159 case 1:
160 err1: if (LF_ISSET(DBG_FATAL))
161 err2: db_err(sp, lno);
162 err3: if (lenp != NULL)
163 *lenp = 0;
164 if (pp != NULL)
165 *pp = NULL;
166 return (1);
169 /* Reset the cache. */
170 ep->c_lno = lno;
171 ep->c_len = data.size;
172 ep->c_lp = data.data;
174 #if defined(DEBUG) && 0
175 vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
176 #endif
177 if (lenp != NULL)
178 *lenp = data.size;
179 if (pp != NULL)
180 *pp = ep->c_lp;
181 return (0);
185 * db_delete --
186 * Delete a line from the file.
188 * PUBLIC: int db_delete __P((SCR *, recno_t));
191 db_delete(sp, lno)
192 SCR *sp;
193 recno_t lno;
195 DBT key;
196 EXF *ep;
198 #if defined(DEBUG) && 0
199 vtrace(sp, "delete line %lu\n", (u_long)lno);
200 #endif
201 /* Check for no underlying file. */
202 if ((ep = sp->ep) == NULL) {
203 ex_emsg(sp, NULL, EXM_NOFILEYET);
204 return (1);
207 /* Update marks, @ and global commands. */
208 if (mark_insdel(sp, LINE_DELETE, lno))
209 return (1);
210 if (ex_g_insdel(sp, LINE_DELETE, lno))
211 return (1);
213 /* Log change. */
214 log_line(sp, lno, LOG_LINE_DELETE);
216 /* Update file. */
217 key.data = &lno;
218 key.size = sizeof(lno);
219 SIGBLOCK;
220 if (ep->db->del(ep->db, &key, 0) == 1) {
221 msgq(sp, M_SYSERR,
222 "003|unable to delete line %lu", (u_long)lno);
223 return (1);
225 SIGUNBLOCK;
227 /* Flush the cache, update line count, before screen update. */
228 if (lno <= ep->c_lno)
229 ep->c_lno = OOBLNO;
230 if (ep->c_nlines != OOBLNO)
231 --ep->c_nlines;
233 /* File now modified. */
234 if (F_ISSET(ep, F_FIRSTMODIFY))
235 (void)rcv_init(sp);
236 F_SET(ep, F_MODIFIED);
238 /* Update screen. */
239 return (scr_update(sp, lno, LINE_DELETE, 1));
243 * db_append --
244 * Append a line into the file.
246 * PUBLIC: int db_append __P((SCR *, int, recno_t, char *, size_t));
249 db_append(sp, update, lno, p, len)
250 SCR *sp;
251 int update;
252 recno_t lno;
253 char *p;
254 size_t len;
256 DBT data, key;
257 EXF *ep;
258 int rval;
260 #if defined(DEBUG) && 0
261 vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
262 #endif
263 /* Check for no underlying file. */
264 if ((ep = sp->ep) == NULL) {
265 ex_emsg(sp, NULL, EXM_NOFILEYET);
266 return (1);
269 /* Update file. */
270 key.data = &lno;
271 key.size = sizeof(lno);
272 data.data = p;
273 data.size = len;
274 SIGBLOCK;
275 if (ep->db->put(ep->db, &key, &data, R_IAFTER) == -1) {
276 msgq(sp, M_SYSERR,
277 "004|unable to append to line %lu", (u_long)lno);
278 return (1);
280 SIGUNBLOCK;
282 /* Flush the cache, update line count, before screen update. */
283 if (lno < ep->c_lno)
284 ep->c_lno = OOBLNO;
285 if (ep->c_nlines != OOBLNO)
286 ++ep->c_nlines;
288 /* File now dirty. */
289 if (F_ISSET(ep, F_FIRSTMODIFY))
290 (void)rcv_init(sp);
291 F_SET(ep, F_MODIFIED);
293 /* Log change. */
294 log_line(sp, lno + 1, LOG_LINE_APPEND);
296 /* Update marks, @ and global commands. */
297 rval = 0;
298 if (mark_insdel(sp, LINE_INSERT, lno + 1))
299 rval = 1;
300 if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
301 rval = 1;
304 * Update screen.
306 * XXX
307 * Nasty hack. If multiple lines are input by the user, they aren't
308 * committed until an <ESC> is entered. The problem is the screen was
309 * updated/scrolled as each line was entered. So, when this routine
310 * is called to copy the new lines from the cut buffer into the file,
311 * it has to know not to update the screen again.
313 return (scr_update(sp, lno, LINE_APPEND, update) || rval);
317 * db_insert --
318 * Insert a line into the file.
320 * PUBLIC: int db_insert __P((SCR *, recno_t, char *, size_t));
323 db_insert(sp, lno, p, len)
324 SCR *sp;
325 recno_t lno;
326 char *p;
327 size_t len;
329 DBT data, key;
330 EXF *ep;
331 int rval;
333 #if defined(DEBUG) && 0
334 vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
335 (u_long)lno, (u_long)len, MIN(len, 20), p);
336 #endif
337 /* Check for no underlying file. */
338 if ((ep = sp->ep) == NULL) {
339 ex_emsg(sp, NULL, EXM_NOFILEYET);
340 return (1);
343 /* Update file. */
344 key.data = &lno;
345 key.size = sizeof(lno);
346 data.data = p;
347 data.size = len;
348 SIGBLOCK;
349 if (ep->db->put(ep->db, &key, &data, R_IBEFORE) == -1) {
350 msgq(sp, M_SYSERR,
351 "005|unable to insert at line %lu", (u_long)lno);
352 return (1);
354 SIGUNBLOCK;
356 /* Flush the cache, update line count, before screen update. */
357 if (lno >= ep->c_lno)
358 ep->c_lno = OOBLNO;
359 if (ep->c_nlines != OOBLNO)
360 ++ep->c_nlines;
362 /* File now dirty. */
363 if (F_ISSET(ep, F_FIRSTMODIFY))
364 (void)rcv_init(sp);
365 F_SET(ep, F_MODIFIED);
367 /* Log change. */
368 log_line(sp, lno, LOG_LINE_INSERT);
370 /* Update marks, @ and global commands. */
371 rval = 0;
372 if (mark_insdel(sp, LINE_INSERT, lno))
373 rval = 1;
374 if (ex_g_insdel(sp, LINE_INSERT, lno))
375 rval = 1;
377 /* Update screen. */
378 return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
382 * db_set --
383 * Store a line in the file.
385 * PUBLIC: int db_set __P((SCR *, recno_t, char *, size_t));
388 db_set(sp, lno, p, len)
389 SCR *sp;
390 recno_t lno;
391 char *p;
392 size_t len;
394 DBT data, key;
395 EXF *ep;
397 #if defined(DEBUG) && 0
398 vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
399 (u_long)lno, (u_long)len, MIN(len, 20), p);
400 #endif
402 /* Check for no underlying file. */
403 if ((ep = sp->ep) == NULL) {
404 ex_emsg(sp, NULL, EXM_NOFILEYET);
405 return (1);
408 /* Log before change. */
409 log_line(sp, lno, LOG_LINE_RESET_B);
411 /* Update file. */
412 key.data = &lno;
413 key.size = sizeof(lno);
414 data.data = p;
415 data.size = len;
416 SIGBLOCK;
417 if (ep->db->put(ep->db, &key, &data, 0) == -1) {
418 msgq(sp, M_SYSERR,
419 "006|unable to store line %lu", (u_long)lno);
420 return (1);
422 SIGUNBLOCK;
424 /* Flush the cache, before logging or screen update. */
425 if (lno == ep->c_lno)
426 ep->c_lno = OOBLNO;
428 /* File now dirty. */
429 if (F_ISSET(ep, F_FIRSTMODIFY))
430 (void)rcv_init(sp);
431 F_SET(ep, F_MODIFIED);
433 /* Log after change. */
434 log_line(sp, lno, LOG_LINE_RESET_F);
436 /* Update screen. */
437 return (scr_update(sp, lno, LINE_RESET, 1));
441 * db_exist --
442 * Return if a line exists.
444 * PUBLIC: int db_exist __P((SCR *, recno_t));
447 db_exist(sp, lno)
448 SCR *sp;
449 recno_t lno;
451 EXF *ep;
453 /* Check for no underlying file. */
454 if ((ep = sp->ep) == NULL) {
455 ex_emsg(sp, NULL, EXM_NOFILEYET);
456 return (1);
459 if (lno == OOBLNO)
460 return (0);
463 * Check the last-line number cache. Adjust the cached line
464 * number for the lines used by the text input buffers.
466 if (ep->c_nlines != OOBLNO)
467 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
468 ep->c_nlines + (((TEXT *)sp->tiq.cqh_last)->lno -
469 ((TEXT *)sp->tiq.cqh_first)->lno) : ep->c_nlines));
471 /* Go get the line. */
472 return (!db_get(sp, lno, 0, NULL, NULL));
476 * db_last --
477 * Return the number of lines in the file.
479 * PUBLIC: int db_last __P((SCR *, recno_t *));
482 db_last(sp, lnop)
483 SCR *sp;
484 recno_t *lnop;
486 DBT data, key;
487 EXF *ep;
488 recno_t lno;
490 /* Check for no underlying file. */
491 if ((ep = sp->ep) == NULL) {
492 ex_emsg(sp, NULL, EXM_NOFILEYET);
493 return (1);
497 * Check the last-line number cache. Adjust the cached line
498 * number for the lines used by the text input buffers.
500 if (ep->c_nlines != OOBLNO) {
501 *lnop = ep->c_nlines;
502 if (F_ISSET(sp, SC_TINPUT))
503 *lnop += ((TEXT *)sp->tiq.cqh_last)->lno -
504 ((TEXT *)sp->tiq.cqh_first)->lno;
505 return (0);
508 key.data = &lno;
509 key.size = sizeof(lno);
511 switch (ep->db->seq(ep->db, &key, &data, R_LAST)) {
512 case -1:
513 msgq(sp, M_SYSERR, "007|unable to get last line");
514 *lnop = 0;
515 return (1);
516 case 1:
517 *lnop = 0;
518 return (0);
519 default:
520 break;
523 /* Fill the cache. */
524 memcpy(&lno, key.data, sizeof(lno));
525 ep->c_nlines = ep->c_lno = lno;
526 ep->c_len = data.size;
527 ep->c_lp = data.data;
529 /* Return the value. */
530 *lnop = (F_ISSET(sp, SC_TINPUT) &&
531 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
532 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
533 return (0);
537 * db_err --
538 * Report a line error.
540 * PUBLIC: void db_err __P((SCR *, recno_t));
542 void
543 db_err(sp, lno)
544 SCR *sp;
545 recno_t lno;
547 msgq(sp, M_ERR,
548 "008|Error: unable to retrieve line %lu", (u_long)lno);
552 * scr_update --
553 * Update all of the screens that are backed by the file that
554 * just changed.
556 static int
557 scr_update(sp, lno, op, current)
558 SCR *sp;
559 recno_t lno;
560 lnop_t op;
561 int current;
563 EXF *ep;
564 SCR *tsp;
566 if (F_ISSET(sp, SC_EX))
567 return (0);
569 ep = sp->ep;
570 if (ep->refcnt != 1)
571 for (tsp = sp->gp->dq.cqh_first;
572 tsp != (void *)&sp->gp->dq; tsp = tsp->q.cqe_next)
573 if (sp != tsp && tsp->ep == ep)
574 if (vs_change(tsp, lno, op))
575 return (1);
576 return (current ? vs_change(sp, lno, op) : 0);