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.27 2015/04/03 14:17:21 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
29 static int scr_update(SCR
*, recno_t
, lnop_t
, int);
33 * Front-end to db_get, special case handling for empty files.
35 * PUBLIC: int db_eget(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(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. */
97 * The underlying recno stuff handles zero by returning NULL, but
98 * have to have an OOB condition for the look-aside into the input
104 /* Check for no underlying file. */
105 if ((ep
= sp
->ep
) == NULL
) {
106 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
110 if (LF_ISSET(DBG_NOCACHE
))
114 * Look-aside into the TEXT buffers and see if the line we want
117 if (F_ISSET(sp
, SC_TINPUT
)) {
118 l1
= ((TEXT
*)TAILQ_FIRST(sp
->tiq
))->lno
;
119 l2
= ((TEXT
*)TAILQ_LAST(sp
->tiq
, _texth
))->lno
;
120 if (l1
<= lno
&& l2
>= lno
) {
121 #if defined(DEBUG) && 0
122 TRACE(sp
, "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
124 for (tp
= TAILQ_FIRST(sp
->tiq
);
125 tp
->lno
!= lno
; tp
= TAILQ_NEXT(tp
, q
));
133 * Adjust the line number for the number of lines used
134 * by the text input buffers.
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 TRACE(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
154 /* Get the line from the underlying database. */
156 key
.size
= sizeof(lno
);
157 switch (ep
->db
->get(ep
->db
, &key
, &data
, 0)) {
161 err1
: if (LF_ISSET(DBG_FATAL
))
162 err2
: db_err(sp
, lno
);
164 err3
: if (lenp
!= NULL
)
171 if (FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
)) {
172 if (!F_ISSET(sp
, SC_CONV_ERROR
)) {
173 F_SET(sp
, SC_CONV_ERROR
);
174 msgq(sp
, M_ERR
, "324|Conversion error on line %d", lno
);
179 /* Reset the cache. */
180 if (wp
!= data
.data
) {
181 BINC_GOTOW(sp
, ep
->c_lp
, ep
->c_blen
, wlen
);
182 MEMCPY(ep
->c_lp
, wp
, wlen
);
184 ep
->c_lp
= data
.data
;
188 #if defined(DEBUG) && 0
189 TRACE(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
200 * Delete a line from the file.
202 * PUBLIC: int db_delete(SCR *, recno_t);
212 #if defined(DEBUG) && 0
213 TRACE(sp
, "delete line %lu\n", (u_long
)lno
);
215 /* Check for no underlying file. */
216 if ((ep
= sp
->ep
) == NULL
) {
217 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
221 /* Update marks, @ and global commands. */
222 if (mark_insdel(sp
, LINE_DELETE
, lno
))
224 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
228 log_line(sp
, lno
, LOG_LINE_DELETE
);
232 key
.size
= sizeof(lno
);
233 if (ep
->db
->del(ep
->db
, &key
, 0) == 1) {
235 "003|unable to delete line %lu", (u_long
)lno
);
239 /* Flush the cache, update line count, before screen update. */
240 if (lno
<= ep
->c_lno
)
242 if (ep
->c_nlines
!= OOBLNO
)
245 /* File now modified. */
246 if (F_ISSET(ep
, F_FIRSTMODIFY
))
248 F_SET(ep
, F_MODIFIED
);
251 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
256 * Append a line into the file.
258 * PUBLIC: int db_append(SCR *, int, recno_t, CHAR_T *, size_t);
274 #if defined(DEBUG) && 0
275 TRACE(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
277 /* Check for no underlying file. */
278 if ((ep
= sp
->ep
) == NULL
) {
279 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
283 INT2FILE(sp
, p
, len
, fp
, flen
);
287 key
.size
= sizeof(lno
);
290 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IAFTER
) == -1) {
292 "004|unable to append to line %lu", (u_long
)lno
);
296 /* Flush the cache, update line count, before screen update. */
299 if (ep
->c_nlines
!= OOBLNO
)
302 /* File now dirty. */
303 if (F_ISSET(ep
, F_FIRSTMODIFY
))
305 F_SET(ep
, F_MODIFIED
);
308 log_line(sp
, lno
+ 1, LOG_LINE_APPEND
);
310 /* Update marks, @ and global commands. */
312 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
314 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
321 * Nasty hack. If multiple lines are input by the user, they aren't
322 * committed until an <ESC> is entered. The problem is the screen was
323 * updated/scrolled as each line was entered. So, when this routine
324 * is called to copy the new lines from the cut buffer into the file,
325 * it has to know not to update the screen again.
327 return (scr_update(sp
, lno
, LINE_APPEND
, update
) || rval
);
332 * Insert a line into the file.
334 * PUBLIC: int db_insert(SCR *, recno_t, CHAR_T *, size_t);
349 #if defined(DEBUG) && 0
350 TRACE(sp
, "insert before %lu: len %lu {%.*s}\n",
351 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
353 /* Check for no underlying file. */
354 if ((ep
= sp
->ep
) == NULL
) {
355 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
359 INT2FILE(sp
, p
, len
, fp
, flen
);
363 key
.size
= sizeof(lno
);
366 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IBEFORE
) == -1) {
368 "005|unable to insert at line %lu", (u_long
)lno
);
372 /* Flush the cache, update line count, before screen update. */
373 if (lno
>= ep
->c_lno
)
375 if (ep
->c_nlines
!= OOBLNO
)
378 /* File now dirty. */
379 if (F_ISSET(ep
, F_FIRSTMODIFY
))
381 F_SET(ep
, F_MODIFIED
);
384 log_line(sp
, lno
, LOG_LINE_INSERT
);
386 /* Update marks, @ and global commands. */
388 if (mark_insdel(sp
, LINE_INSERT
, lno
))
390 if (ex_g_insdel(sp
, LINE_INSERT
, lno
))
394 return (scr_update(sp
, lno
, LINE_INSERT
, 1) || rval
);
399 * Store a line in the file.
401 * PUBLIC: int db_set(SCR *, recno_t, CHAR_T *, size_t);
415 #if defined(DEBUG) && 0
416 TRACE(sp
, "replace line %lu: len %lu {%.*s}\n",
417 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
419 /* Check for no underlying file. */
420 if ((ep
= sp
->ep
) == NULL
) {
421 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
425 /* Log before change. */
426 log_line(sp
, lno
, LOG_LINE_RESET_B
);
428 INT2FILE(sp
, p
, len
, fp
, flen
);
432 key
.size
= sizeof(lno
);
435 if (ep
->db
->put(ep
->db
, &key
, &data
, 0) == -1) {
437 "006|unable to store line %lu", (u_long
)lno
);
441 /* Flush the cache, before logging or screen update. */
442 if (lno
== ep
->c_lno
)
445 /* File now dirty. */
446 if (F_ISSET(ep
, F_FIRSTMODIFY
))
448 F_SET(ep
, F_MODIFIED
);
450 /* Log after change. */
451 log_line(sp
, lno
, LOG_LINE_RESET_F
);
454 return (scr_update(sp
, lno
, LINE_RESET
, 1));
459 * Return if a line exists.
461 * PUBLIC: int db_exist(SCR *, recno_t);
470 /* Check for no underlying file. */
471 if ((ep
= sp
->ep
) == NULL
) {
472 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
480 * Check the last-line number cache. Adjust the cached line
481 * number for the lines used by the text input buffers.
483 if (ep
->c_nlines
!= OOBLNO
)
484 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
485 ep
->c_nlines
+ (((TEXT
*)TAILQ_LAST(sp
->tiq
, _texth
))->lno
-
486 ((TEXT
*)TAILQ_FIRST(sp
->tiq
))->lno
) : ep
->c_nlines
));
488 /* Go get the line. */
489 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
494 * Return the number of lines in the file.
496 * PUBLIC: int db_last(SCR *, recno_t *);
509 /* Check for no underlying file. */
510 if ((ep
= sp
->ep
) == NULL
) {
511 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
516 * Check the last-line number cache. Adjust the cached line
517 * number for the lines used by the text input buffers.
519 if (ep
->c_nlines
!= OOBLNO
) {
520 *lnop
= ep
->c_nlines
;
521 if (F_ISSET(sp
, SC_TINPUT
))
522 *lnop
+= ((TEXT
*)TAILQ_LAST(sp
->tiq
, _texth
))->lno
-
523 ((TEXT
*)TAILQ_FIRST(sp
->tiq
))->lno
;
528 key
.size
= sizeof(lno
);
530 switch (ep
->db
->seq(ep
->db
, &key
, &data
, R_LAST
)) {
533 msgq(sp
, M_SYSERR
, "007|unable to get last line");
541 memcpy(&lno
, key
.data
, sizeof(lno
));
543 if (lno
!= ep
->c_lno
) {
544 FILE2INT(sp
, data
.data
, data
.size
, wp
, wlen
);
546 /* Fill the cache. */
547 if (wp
!= data
.data
) {
548 BINC_GOTOW(sp
, ep
->c_lp
, ep
->c_blen
, wlen
);
549 MEMCPY(ep
->c_lp
, wp
, wlen
);
551 ep
->c_lp
= data
.data
;
557 /* Return the value. */
558 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
559 ((TEXT
*)TAILQ_LAST(sp
->tiq
, _texth
))->lno
> lno
?
560 ((TEXT
*)TAILQ_LAST(sp
->tiq
, _texth
))->lno
: lno
);
566 * Retrieve a raw line from the database.
568 * PUBLIC: int db_rget(SCR *, recno_t, char **, size_t *);
573 recno_t lno
, /* Line number. */
574 char **pp
, /* Pointer store. */
575 size_t *lenp
) /* Length store. */
581 /* Get the line from the underlying database. */
583 key
.size
= sizeof(lno
);
584 if ((rval
= ep
->db
->get(ep
->db
, &key
, &data
, 0)) == 0)
595 * Store a raw line into the database.
597 * PUBLIC: int db_rset(SCR *, recno_t, char *, size_t);
611 key
.size
= sizeof(lno
);
614 return ep
->db
->put(ep
->db
, &key
, &data
, 0);
619 * Report a line error.
621 * PUBLIC: void db_err(SCR *, recno_t);
629 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
634 * Update all of the screens that are backed by the file that
647 if (F_ISSET(sp
, SC_EX
))
652 TAILQ_FOREACH(tsp
, sp
->gp
->dq
, q
)
653 if (sp
!= tsp
&& tsp
->ep
== ep
)
654 if (vs_change(tsp
, lno
, op
))
656 return (current
? vs_change(sp
, lno
, op
) : 0);