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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
25 static int scr_update(SCR *, recno_t, lnop_t, int);
29 * Front-end to db_get, special case handling for empty files.
31 * PUBLIC: int db_eget(SCR *, recno_t, CHAR_T **, size_t *, int *);
35 recno_t lno, /* Line number. */
36 CHAR_T **pp, /* Pointer store. */
37 size_t *lenp, /* Length store. */
45 /* If the line exists, simply return it. */
46 if (!db_get(sp, lno, 0, pp, lenp))
50 * If the user asked for line 0 or line 1, i.e. the only possible
51 * line in an empty file, find the last line of the file; db_last
54 if ((lno == 0 || lno == 1) && db_last(sp, &l1))
57 /* If the file isn't empty, fail loudly. */
58 if ((lno != 0 && lno != 1) || l1 != 0) {
71 * Look in the text buffers for a line, followed by the cache, followed
74 * PUBLIC: int db_get(SCR *, recno_t, u_int32_t, CHAR_T **, size_t *);
78 recno_t lno, /* Line number. */
80 CHAR_T **pp, /* Pointer store. */
81 size_t *lenp) /* Length store. */
91 * The underlying recno stuff handles zero by returning NULL, but
92 * have to have an OOB condition for the look-aside into the input
98 /* Check for no underlying file. */
99 if ((ep = sp->ep) == NULL) {
100 ex_emsg(sp, NULL, EXM_NOFILEYET);
104 if (LF_ISSET(DBG_NOCACHE))
108 * Look-aside into the TEXT buffers and see if the line we want
111 if (F_ISSET(sp, SC_TINPUT)) {
112 l1 = ((TEXT *)TAILQ_FIRST(sp->tiq))->lno;
113 l2 = ((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno;
114 if (l1 <= lno && l2 >= lno) {
115 #if defined(DEBUG) && 0
116 TRACE(sp, "retrieve TEXT buffer line %lu\n", (u_long)lno);
118 for (tp = TAILQ_FIRST(sp->tiq);
119 tp->lno != lno; tp = TAILQ_NEXT(tp, q));
127 * Adjust the line number for the number of lines used
128 * by the text input buffers.
134 /* Look-aside into the cache, and see if the line we want is there. */
135 if (lno == ep->c_lno) {
136 #if defined(DEBUG) && 0
137 TRACE(sp, "retrieve cached line %lu\n", (u_long)lno);
148 /* Get the line from the underlying database. */
150 key.size = sizeof(lno);
151 switch (ep->db->get(ep->db, &key, &data, 0)) {
155 err1: if (LF_ISSET(DBG_FATAL))
156 err2: db_err(sp, lno);
158 err3: if (lenp != NULL)
165 if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
166 if (!F_ISSET(sp, SC_CONV_ERROR)) {
167 F_SET(sp, SC_CONV_ERROR);
168 msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
173 /* Reset the cache. */
174 if (wp != data.data) {
175 BINC_GOTOW(sp, ep->c_lp, ep->c_blen, wlen);
176 MEMCPY(ep->c_lp, wp, wlen);
178 ep->c_lp = data.data;
182 #if defined(DEBUG) && 0
183 TRACE(sp, "retrieve DB line %lu\n", (u_long)lno);
194 * Delete a line from the file.
196 * PUBLIC: int db_delete(SCR *, recno_t);
199 db_delete(SCR *sp, recno_t lno)
204 #if defined(DEBUG) && 0
205 TRACE(sp, "delete line %lu\n", (u_long)lno);
207 /* Check for no underlying file. */
208 if ((ep = sp->ep) == NULL) {
209 ex_emsg(sp, NULL, EXM_NOFILEYET);
213 /* Update marks, @ and global commands. */
214 if (mark_insdel(sp, LINE_DELETE, lno))
216 if (ex_g_insdel(sp, LINE_DELETE, lno))
220 log_line(sp, lno, LOG_LINE_DELETE);
224 key.size = sizeof(lno);
225 if (ep->db->del(ep->db, &key, 0) == 1) {
227 "003|unable to delete line %lu", (u_long)lno);
231 /* Flush the cache, update line count, before screen update. */
232 if (lno <= ep->c_lno)
234 if (ep->c_nlines != OOBLNO)
237 /* File now modified. */
238 if (F_ISSET(ep, F_FIRSTMODIFY))
240 F_SET(ep, F_MODIFIED);
243 return (scr_update(sp, lno, LINE_DELETE, 1));
248 * Append a line into the file.
250 * PUBLIC: int db_append(SCR *, int, recno_t, CHAR_T *, size_t);
253 db_append(SCR *sp, int update, recno_t lno, CHAR_T *p, size_t len)
261 #if defined(DEBUG) && 0
262 TRACE(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
264 /* Check for no underlying file. */
265 if ((ep = sp->ep) == NULL) {
266 ex_emsg(sp, NULL, EXM_NOFILEYET);
270 INT2FILE(sp, p, len, fp, flen);
274 key.size = sizeof(lno);
277 if (ep->db->put(ep->db, &key, &data, R_IAFTER) == -1) {
279 "004|unable to append to line %lu", (u_long)lno);
283 /* Flush the cache, update line count, before screen update. */
286 if (ep->c_nlines != OOBLNO)
289 /* File now dirty. */
290 if (F_ISSET(ep, F_FIRSTMODIFY))
292 F_SET(ep, F_MODIFIED);
295 log_line(sp, lno + 1, LOG_LINE_APPEND);
297 /* Update marks, @ and global commands. */
299 if (mark_insdel(sp, LINE_INSERT, lno + 1))
301 if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
308 * Nasty hack. If multiple lines are input by the user, they aren't
309 * committed until an <ESC> is entered. The problem is the screen was
310 * updated/scrolled as each line was entered. So, when this routine
311 * is called to copy the new lines from the cut buffer into the file,
312 * it has to know not to update the screen again.
314 return (scr_update(sp, lno, LINE_APPEND, update) || rval);
319 * Insert a line into the file.
321 * PUBLIC: int db_insert(SCR *, recno_t, CHAR_T *, size_t);
324 db_insert(SCR *sp, recno_t lno, CHAR_T *p, size_t len)
332 #if defined(DEBUG) && 0
333 TRACE(sp, "insert before %lu: len %lu {%.*s}\n",
334 (u_long)lno, (u_long)len, MIN(len, 20), p);
336 /* Check for no underlying file. */
337 if ((ep = sp->ep) == NULL) {
338 ex_emsg(sp, NULL, EXM_NOFILEYET);
342 INT2FILE(sp, p, len, fp, flen);
346 key.size = sizeof(lno);
349 if (ep->db->put(ep->db, &key, &data, R_IBEFORE) == -1) {
351 "005|unable to insert at line %lu", (u_long)lno);
355 /* Flush the cache, update line count, before screen update. */
356 if (lno >= ep->c_lno)
358 if (ep->c_nlines != OOBLNO)
361 /* File now dirty. */
362 if (F_ISSET(ep, F_FIRSTMODIFY))
364 F_SET(ep, F_MODIFIED);
367 log_line(sp, lno, LOG_LINE_INSERT);
369 /* Update marks, @ and global commands. */
371 if (mark_insdel(sp, LINE_INSERT, lno))
373 if (ex_g_insdel(sp, LINE_INSERT, lno))
377 return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
382 * Store a line in the file.
384 * PUBLIC: int db_set(SCR *, recno_t, CHAR_T *, size_t);
387 db_set(SCR *sp, recno_t lno, CHAR_T *p, size_t len)
394 #if defined(DEBUG) && 0
395 TRACE(sp, "replace line %lu: len %lu {%.*s}\n",
396 (u_long)lno, (u_long)len, MIN(len, 20), p);
398 /* Check for no underlying file. */
399 if ((ep = sp->ep) == NULL) {
400 ex_emsg(sp, NULL, EXM_NOFILEYET);
404 /* Log before change. */
405 log_line(sp, lno, LOG_LINE_RESET_B);
407 INT2FILE(sp, p, len, fp, flen);
411 key.size = sizeof(lno);
414 if (ep->db->put(ep->db, &key, &data, 0) == -1) {
416 "006|unable to store line %lu", (u_long)lno);
420 /* Flush the cache, before logging or screen update. */
421 if (lno == ep->c_lno)
424 /* File now dirty. */
425 if (F_ISSET(ep, F_FIRSTMODIFY))
427 F_SET(ep, F_MODIFIED);
429 /* Log after change. */
430 log_line(sp, lno, LOG_LINE_RESET_F);
433 return (scr_update(sp, lno, LINE_RESET, 1));
438 * Return if a line exists.
440 * PUBLIC: int db_exist(SCR *, recno_t);
443 db_exist(SCR *sp, recno_t lno)
447 /* Check for no underlying file. */
448 if ((ep = sp->ep) == NULL) {
449 ex_emsg(sp, NULL, EXM_NOFILEYET);
457 * Check the last-line number cache. Adjust the cached line
458 * number for the lines used by the text input buffers.
460 if (ep->c_nlines != OOBLNO)
461 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
462 ep->c_nlines + (((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno -
463 ((TEXT *)TAILQ_FIRST(sp->tiq))->lno) : ep->c_nlines));
465 /* Go get the line. */
466 return (!db_get(sp, lno, 0, NULL, NULL));
471 * Return the number of lines in the file.
473 * PUBLIC: int db_last(SCR *, recno_t *);
476 db_last(SCR *sp, recno_t *lnop)
484 /* Check for no underlying file. */
485 if ((ep = sp->ep) == NULL) {
486 ex_emsg(sp, NULL, EXM_NOFILEYET);
491 * Check the last-line number cache. Adjust the cached line
492 * number for the lines used by the text input buffers.
494 if (ep->c_nlines != OOBLNO) {
495 *lnop = ep->c_nlines;
496 if (F_ISSET(sp, SC_TINPUT))
497 *lnop += ((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno -
498 ((TEXT *)TAILQ_FIRST(sp->tiq))->lno;
503 key.size = sizeof(lno);
505 switch (ep->db->seq(ep->db, &key, &data, R_LAST)) {
508 msgq(sp, M_SYSERR, "007|unable to get last line");
516 memcpy(&lno, key.data, sizeof(lno));
518 if (lno != ep->c_lno) {
519 FILE2INT(sp, data.data, data.size, wp, wlen);
521 /* Fill the cache. */
522 if (wp != data.data) {
523 BINC_GOTOW(sp, ep->c_lp, ep->c_blen, wlen);
524 MEMCPY(ep->c_lp, wp, wlen);
526 ep->c_lp = data.data;
532 /* Return the value. */
533 *lnop = (F_ISSET(sp, SC_TINPUT) &&
534 ((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno > lno ?
535 ((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno : lno);
541 * Retrieve a raw line from the database.
543 * PUBLIC: int db_rget(SCR *, recno_t, char **, size_t *);
547 recno_t lno, /* Line number. */
548 char **pp, /* Pointer store. */
549 size_t *lenp) /* Length store. */
555 /* Get the line from the underlying database. */
557 key.size = sizeof(lno);
558 if ((rval = ep->db->get(ep->db, &key, &data, 0)) == 0)
569 * Store a raw line into the database.
571 * PUBLIC: int db_rset(SCR *, recno_t, char *, size_t);
574 db_rset(SCR *sp, recno_t lno, char *p, size_t len)
581 key.size = sizeof(lno);
584 return ep->db->put(ep->db, &key, &data, 0);
589 * Report a line error.
591 * PUBLIC: void db_err(SCR *, recno_t);
594 db_err(SCR *sp, recno_t lno)
597 "008|Error: unable to retrieve line %lu", (u_long)lno);
602 * Update all of the screens that are backed by the file that
606 scr_update(SCR *sp, recno_t lno, lnop_t op, int current)
611 if (F_ISSET(sp, SC_EX))
616 TAILQ_FOREACH(tsp, sp->gp->dq, q)
617 if (sp != tsp && tsp->ep == ep)
618 if (vs_change(tsp, lno, op))
620 return (current ? vs_change(sp, lno, op) : 0);