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
[] = "@(#)line.c 10.21 (Berkeley) 9/15/96";
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 **, size_t *, int *));
38 db_eget(sp
, lno
, pp
, lenp
, isemptyp
)
40 recno_t lno
; /* Line number. */
41 char **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 **, size_t *));
82 db_get(sp
, lno
, flags
, pp
, lenp
)
84 recno_t lno
; /* Line number. */
86 char **pp
; /* Pointer store. */
87 size_t *lenp
; /* Length store. */
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
102 /* Check for no underlying file. */
103 if ((ep
= sp
->ep
) == NULL
) {
104 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
108 if (LF_ISSET(DBG_NOCACHE
))
112 * Look-aside into the TEXT buffers and see if the line we want
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 TRACE(sp
, "retrieve TEXT buffer line %lu\n", (u_long
)lno
);
122 for (tp
= sp
->tiq
.cqh_first
;
123 tp
->lno
!= lno
; tp
= tp
->q
.cqe_next
);
131 * Adjust the line number for the number of lines used
132 * by the text input buffers.
138 /* Look-aside into the cache, and see if the line we want is there. */
139 if (lno
== ep
->c_lno
) {
140 #if defined(DEBUG) && 0
141 TRACE(sp
, "retrieve cached line %lu\n", (u_long
)lno
);
152 /* Get the line from the underlying database. */
154 key
.size
= sizeof(lno
);
155 switch (ep
->db
->get(ep
->db
, &key
, &data
, 0)) {
159 err1
: if (LF_ISSET(DBG_FATAL
))
160 err2
: db_err(sp
, lno
);
161 err3
: if (lenp
!= NULL
)
168 /* Reset the cache. */
170 ep
->c_len
= data
.size
;
171 ep
->c_lp
= data
.data
;
173 #if defined(DEBUG) && 0
174 TRACE(sp
, "retrieve DB line %lu\n", (u_long
)lno
);
185 * Delete a line from the file.
187 * PUBLIC: int db_delete __P((SCR *, recno_t));
197 #if defined(DEBUG) && 0
198 TRACE(sp
, "delete line %lu\n", (u_long
)lno
);
200 /* Check for no underlying file. */
201 if ((ep
= sp
->ep
) == NULL
) {
202 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
206 /* Update marks, @ and global commands. */
207 if (mark_insdel(sp
, LINE_DELETE
, lno
))
209 if (ex_g_insdel(sp
, LINE_DELETE
, lno
))
213 log_line(sp
, lno
, LOG_LINE_DELETE
);
217 key
.size
= sizeof(lno
);
219 if (ep
->db
->del(ep
->db
, &key
, 0) == 1) {
221 "003|unable to delete line %lu", (u_long
)lno
);
226 /* Flush the cache, update line count, before screen update. */
227 if (lno
<= ep
->c_lno
)
229 if (ep
->c_nlines
!= OOBLNO
)
232 /* File now modified. */
233 if (F_ISSET(ep
, F_FIRSTMODIFY
))
235 F_SET(ep
, F_MODIFIED
);
238 return (scr_update(sp
, lno
, LINE_DELETE
, 1));
243 * Append a line into the file.
245 * PUBLIC: int db_append __P((SCR *, int, recno_t, char *, size_t));
248 db_append(sp
, update
, lno
, p
, len
)
259 #if defined(DEBUG) && 0
260 TRACE(sp
, "append to %lu: len %u {%.*s}\n", lno
, len
, MIN(len
, 20), p
);
262 /* Check for no underlying file. */
263 if ((ep
= sp
->ep
) == NULL
) {
264 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
270 key
.size
= sizeof(lno
);
274 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IAFTER
) == -1) {
276 "004|unable to append to line %lu", (u_long
)lno
);
281 /* Flush the cache, update line count, before screen update. */
284 if (ep
->c_nlines
!= OOBLNO
)
287 /* File now dirty. */
288 if (F_ISSET(ep
, F_FIRSTMODIFY
))
290 F_SET(ep
, F_MODIFIED
);
293 log_line(sp
, lno
+ 1, LOG_LINE_APPEND
);
295 /* Update marks, @ and global commands. */
297 if (mark_insdel(sp
, LINE_INSERT
, lno
+ 1))
299 if (ex_g_insdel(sp
, LINE_INSERT
, lno
+ 1))
306 * Nasty hack. If multiple lines are input by the user, they aren't
307 * committed until an <ESC> is entered. The problem is the screen was
308 * updated/scrolled as each line was entered. So, when this routine
309 * is called to copy the new lines from the cut buffer into the file,
310 * it has to know not to update the screen again.
312 return (scr_update(sp
, lno
, LINE_APPEND
, update
) || rval
);
317 * Insert a line into the file.
319 * PUBLIC: int db_insert __P((SCR *, recno_t, char *, size_t));
322 db_insert(sp
, lno
, p
, 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
);
344 key
.size
= sizeof(lno
);
348 if (ep
->db
->put(ep
->db
, &key
, &data
, R_IBEFORE
) == -1) {
350 "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 __P((SCR *, recno_t, char *, size_t));
387 db_set(sp
, lno
, p
, len
)
396 #if defined(DEBUG) && 0
397 TRACE(sp
, "replace line %lu: len %lu {%.*s}\n",
398 (u_long
)lno
, (u_long
)len
, MIN(len
, 20), p
);
401 /* Check for no underlying file. */
402 if ((ep
= sp
->ep
) == NULL
) {
403 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
407 /* Log before change. */
408 log_line(sp
, lno
, LOG_LINE_RESET_B
);
412 key
.size
= sizeof(lno
);
416 if (ep
->db
->put(ep
->db
, &key
, &data
, 0) == -1) {
418 "006|unable to store line %lu", (u_long
)lno
);
423 /* Flush the cache, before logging or screen update. */
424 if (lno
== ep
->c_lno
)
427 /* File now dirty. */
428 if (F_ISSET(ep
, F_FIRSTMODIFY
))
430 F_SET(ep
, F_MODIFIED
);
432 /* Log after change. */
433 log_line(sp
, lno
, LOG_LINE_RESET_F
);
436 return (scr_update(sp
, lno
, LINE_RESET
, 1));
441 * Return if a line exists.
443 * PUBLIC: int db_exist __P((SCR *, recno_t));
452 /* Check for no underlying file. */
453 if ((ep
= sp
->ep
) == NULL
) {
454 ex_emsg(sp
, NULL
, EXM_NOFILEYET
);
462 * Check the last-line number cache. Adjust the cached line
463 * number for the lines used by the text input buffers.
465 if (ep
->c_nlines
!= OOBLNO
)
466 return (lno
<= (F_ISSET(sp
, SC_TINPUT
) ?
467 ep
->c_nlines
+ (((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
468 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
) : ep
->c_nlines
));
470 /* Go get the line. */
471 return (!db_get(sp
, lno
, 0, NULL
, NULL
));
476 * Return the number of lines in the file.
478 * PUBLIC: int db_last __P((SCR *, recno_t *));
489 /* Check for no underlying file. */
490 if ((ep
= sp
->ep
) == NULL
) {
491 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 *lnop
= ep
->c_nlines
;
501 if (F_ISSET(sp
, SC_TINPUT
))
502 *lnop
+= ((TEXT
*)sp
->tiq
.cqh_last
)->lno
-
503 ((TEXT
*)sp
->tiq
.cqh_first
)->lno
;
508 key
.size
= sizeof(lno
);
510 switch (ep
->db
->seq(ep
->db
, &key
, &data
, R_LAST
)) {
512 msgq(sp
, M_SYSERR
, "007|unable to get last line");
522 /* Fill the cache. */
523 memcpy(&lno
, key
.data
, sizeof(lno
));
524 ep
->c_nlines
= ep
->c_lno
= lno
;
525 ep
->c_len
= data
.size
;
526 ep
->c_lp
= data
.data
;
528 /* Return the value. */
529 *lnop
= (F_ISSET(sp
, SC_TINPUT
) &&
530 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
> lno
?
531 ((TEXT
*)sp
->tiq
.cqh_last
)->lno
: lno
);
537 * Report a line error.
539 * PUBLIC: void db_err __P((SCR *, recno_t));
547 "008|Error: unable to retrieve line %lu", (u_long
)lno
);
552 * Update all of the screens that are backed by the file that
556 scr_update(sp
, lno
, op
, current
)
565 if (F_ISSET(sp
, SC_EX
))
570 for (tsp
= sp
->gp
->dq
.cqh_first
;
571 tsp
!= (void *)&sp
->gp
->dq
; tsp
= tsp
->q
.cqe_next
)
572 if (sp
!= tsp
&& tsp
->ep
== ep
)
573 if (vs_change(tsp
, lno
, op
))
575 return (current
? vs_change(sp
, lno
, op
) : 0);