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.
13 static const char sccsid[] = "$Id: line.c,v 10.26 2011/08/12 12:36:41 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
29 static int scr_update __P((SCR *, recno_t, lnop_t, int));
33 * Front-end to db_get, special case handling for empty files.
35 * PUBLIC: int db_eget __P((SCR *, recno_t, CHAR_T **, size_t *, int *));
40 recno_t lno, /* Line number. */
41 CHAR_T **pp, /* Pointer store. */
42 size_t *lenp, /* Length store. */
50 /* If the line exists, simply return it. */
51 if (!db_get(sp, lno, 0, pp, lenp))
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
59 if ((lno == 0 || lno == 1) && db_last(sp, &l1))
62 /* If the file isn't empty, fail loudly. */
63 if ((lno != 0 && lno != 1) || l1 != 0) {
76 * Look in the text buffers for a line, followed by the cache, followed
79 * PUBLIC: int db_get __P((SCR *, recno_t, u_int32_t, CHAR_T **, size_t *));
84 recno_t lno, /* Line number. */
86 CHAR_T **pp, /* Pointer store. */
87 size_t *lenp) /* Length store. */
98 * The underlying recno stuff handles zero by returning NULL, but
99 * have to have an OOB condition for the look-aside into the input
105 /* Check for no underlying file. */
106 if ((ep = sp->ep) == NULL) {
107 ex_emsg(sp, NULL, EXM_NOFILEYET);
111 if (LF_ISSET(DBG_NOCACHE))
115 * Look-aside into the TEXT buffers and see if the line we want
118 if (F_ISSET(sp, SC_TINPUT)) {
119 l1 = ((TEXT *)TAILQ_FIRST(sp->tiq))->lno;
120 l2 = ((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno;
121 if (l1 <= lno && l2 >= lno) {
122 #if defined(DEBUG) && 0
123 TRACE(sp, "retrieve TEXT buffer line %lu\n", (u_long)lno);
125 for (tp = TAILQ_FIRST(sp->tiq);
126 tp->lno != lno; tp = TAILQ_NEXT(tp, q));
134 * Adjust the line number for the number of lines used
135 * by the text input buffers.
141 /* Look-aside into the cache, and see if the line we want is there. */
142 if (lno == ep->c_lno) {
143 #if defined(DEBUG) && 0
144 TRACE(sp, "retrieve cached line %lu\n", (u_long)lno);
157 /* Get the line from the underlying database. */
159 key.size = sizeof(lno);
160 switch (ep->db->get(ep->db, &key, &data, 0)) {
164 err1: if (LF_ISSET(DBG_FATAL))
165 err2: db_err(sp, lno);
167 err3: if (lenp != NULL)
173 if (data.size > nlen) {
179 if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
180 if (!F_ISSET(sp, SC_CONV_ERROR)) {
181 F_SET(sp, SC_CONV_ERROR);
182 msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
187 /* Reset the cache. */
188 if (wp != data.data) {
189 BINC_GOTOW(sp, ep->c_lp, ep->c_blen, wlen);
190 MEMCPY(ep->c_lp, wp, wlen);
192 ep->c_lp = data.data;
196 #if defined(DEBUG) && 0
197 TRACE(sp, "retrieve DB line %lu\n", (u_long)lno);
208 * Delete a line from the file.
210 * PUBLIC: int db_delete __P((SCR *, recno_t));
220 #if defined(DEBUG) && 0
221 TRACE(sp, "delete line %lu\n", (u_long)lno);
223 /* Check for no underlying file. */
224 if ((ep = sp->ep) == NULL) {
225 ex_emsg(sp, NULL, EXM_NOFILEYET);
229 /* Update marks, @ and global commands. */
230 if (mark_insdel(sp, LINE_DELETE, lno))
232 if (ex_g_insdel(sp, LINE_DELETE, lno))
236 log_line(sp, lno, LOG_LINE_DELETE);
240 key.size = sizeof(lno);
242 if (ep->db->del(ep->db, &key, 0) == 1) {
244 "003|unable to delete line %lu", (u_long)lno);
249 /* Flush the cache, update line count, before screen update. */
250 if (lno <= ep->c_lno)
252 if (ep->c_nlines != OOBLNO)
255 /* File now modified. */
256 if (F_ISSET(ep, F_FIRSTMODIFY))
258 F_SET(ep, F_MODIFIED);
261 return (scr_update(sp, lno, LINE_DELETE, 1));
266 * Append a line into the file.
268 * PUBLIC: int db_append __P((SCR *, int, recno_t, CHAR_T *, size_t));
284 #if defined(DEBUG) && 0
285 TRACE(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
287 /* Check for no underlying file. */
288 if ((ep = sp->ep) == NULL) {
289 ex_emsg(sp, NULL, EXM_NOFILEYET);
293 INT2FILE(sp, p, len, fp, flen);
297 key.size = sizeof(lno);
301 if (ep->db->put(ep->db, &key, &data, R_IAFTER) == -1) {
303 "004|unable to append to line %lu", (u_long)lno);
308 /* Flush the cache, update line count, before screen update. */
311 if (ep->c_nlines != OOBLNO)
314 /* File now dirty. */
315 if (F_ISSET(ep, F_FIRSTMODIFY))
317 F_SET(ep, F_MODIFIED);
320 log_line(sp, lno + 1, LOG_LINE_APPEND);
322 /* Update marks, @ and global commands. */
324 if (mark_insdel(sp, LINE_INSERT, lno + 1))
326 if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
333 * Nasty hack. If multiple lines are input by the user, they aren't
334 * committed until an <ESC> is entered. The problem is the screen was
335 * updated/scrolled as each line was entered. So, when this routine
336 * is called to copy the new lines from the cut buffer into the file,
337 * it has to know not to update the screen again.
339 return (scr_update(sp, lno, LINE_APPEND, update) || rval);
344 * Insert a line into the file.
346 * PUBLIC: int db_insert __P((SCR *, recno_t, CHAR_T *, size_t));
361 #if defined(DEBUG) && 0
362 TRACE(sp, "insert before %lu: len %lu {%.*s}\n",
363 (u_long)lno, (u_long)len, MIN(len, 20), p);
365 /* Check for no underlying file. */
366 if ((ep = sp->ep) == NULL) {
367 ex_emsg(sp, NULL, EXM_NOFILEYET);
371 INT2FILE(sp, p, len, fp, flen);
375 key.size = sizeof(lno);
379 if (ep->db->put(ep->db, &key, &data, R_IBEFORE) == -1) {
381 "005|unable to insert at line %lu", (u_long)lno);
386 /* Flush the cache, update line count, before screen update. */
387 if (lno >= ep->c_lno)
389 if (ep->c_nlines != OOBLNO)
392 /* File now dirty. */
393 if (F_ISSET(ep, F_FIRSTMODIFY))
395 F_SET(ep, F_MODIFIED);
398 log_line(sp, lno, LOG_LINE_INSERT);
400 /* Update marks, @ and global commands. */
402 if (mark_insdel(sp, LINE_INSERT, lno))
404 if (ex_g_insdel(sp, LINE_INSERT, lno))
408 return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
413 * Store a line in the file.
415 * PUBLIC: int db_set __P((SCR *, recno_t, CHAR_T *, size_t));
429 #if defined(DEBUG) && 0
430 TRACE(sp, "replace line %lu: len %lu {%.*s}\n",
431 (u_long)lno, (u_long)len, MIN(len, 20), p);
433 /* Check for no underlying file. */
434 if ((ep = sp->ep) == NULL) {
435 ex_emsg(sp, NULL, EXM_NOFILEYET);
439 /* Log before change. */
440 log_line(sp, lno, LOG_LINE_RESET_B);
442 INT2FILE(sp, p, len, fp, flen);
446 key.size = sizeof(lno);
450 if (ep->db->put(ep->db, &key, &data, 0) == -1) {
452 "006|unable to store line %lu", (u_long)lno);
457 /* Flush the cache, before logging or screen update. */
458 if (lno == ep->c_lno)
461 /* File now dirty. */
462 if (F_ISSET(ep, F_FIRSTMODIFY))
464 F_SET(ep, F_MODIFIED);
466 /* Log after change. */
467 log_line(sp, lno, LOG_LINE_RESET_F);
470 return (scr_update(sp, lno, LINE_RESET, 1));
475 * Return if a line exists.
477 * PUBLIC: int db_exist __P((SCR *, recno_t));
486 /* Check for no underlying file. */
487 if ((ep = sp->ep) == NULL) {
488 ex_emsg(sp, NULL, EXM_NOFILEYET);
496 * Check the last-line number cache. Adjust the cached line
497 * number for the lines used by the text input buffers.
499 if (ep->c_nlines != OOBLNO)
500 return (lno <= (F_ISSET(sp, SC_TINPUT) ?
501 ep->c_nlines + (((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno -
502 ((TEXT *)TAILQ_FIRST(sp->tiq))->lno) : ep->c_nlines));
504 /* Go get the line. */
505 return (!db_get(sp, lno, 0, NULL, NULL));
510 * Return the number of lines in the file.
512 * PUBLIC: int db_last __P((SCR *, recno_t *));
525 /* Check for no underlying file. */
526 if ((ep = sp->ep) == NULL) {
527 ex_emsg(sp, NULL, EXM_NOFILEYET);
532 * Check the last-line number cache. Adjust the cached line
533 * number for the lines used by the text input buffers.
535 if (ep->c_nlines != OOBLNO) {
536 *lnop = ep->c_nlines;
537 if (F_ISSET(sp, SC_TINPUT))
538 *lnop += ((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno -
539 ((TEXT *)TAILQ_FIRST(sp->tiq))->lno;
544 key.size = sizeof(lno);
546 switch (ep->db->seq(ep->db, &key, &data, R_LAST)) {
549 msgq(sp, M_SYSERR, "007|unable to get last line");
559 memcpy(&lno, key.data, sizeof(lno));
561 if (lno != ep->c_lno) {
562 FILE2INT(sp, data.data, data.size, wp, wlen);
564 /* Fill the cache. */
565 if (wp != data.data) {
566 BINC_GOTOW(sp, ep->c_lp, ep->c_blen, wlen);
567 MEMCPY(ep->c_lp, wp, wlen);
569 ep->c_lp = data.data;
575 /* Return the value. */
576 *lnop = (F_ISSET(sp, SC_TINPUT) &&
577 ((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno > lno ?
578 ((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno : lno);
584 * Retrieve a raw line from database. No cache, no conversion.
586 * PUBLIC: int db_rget __P((SCR *, recno_t, char **, size_t *));
591 recno_t lno, /* Line number. */
592 char **pp, /* Pointer store. */
593 size_t *lenp) /* Length store. */
598 /* Check for no underlying file. */
599 if ((ep = sp->ep) == NULL)
602 /* Get the line from the underlying database. */
604 key.size = sizeof(lno);
605 if (ep->db->get(ep->db, &key, &data, 0))
606 /* We do not report error, and do not ensure the size! */
618 * Store a line in the file. No log, no conversion.
620 * PUBLIC: int db_rset __P((SCR *, recno_t, char *, size_t));
632 /* Check for no underlying file. */
633 if ((ep = sp->ep) == NULL)
638 key.size = sizeof(lno);
641 if (ep->db->put(ep->db, &key, &data, 0) == -1)
642 /* We do not report error, and do not ensure the size! */
650 * Report a line error.
652 * PUBLIC: void db_err __P((SCR *, recno_t));
660 "008|Error: unable to retrieve line %lu", (u_long)lno);
665 * Update all of the screens that are backed by the file that
678 if (F_ISSET(sp, SC_EX))
683 TAILQ_FOREACH(tsp, sp->gp->dq, q)
684 if (sp != tsp && tsp->ep == ep)
685 if (vs_change(tsp, lno, op))
687 return (current ? vs_change(sp, lno, op) : 0);