1248871Sjoel/*-
2248842Ssbruno * Copyright (c) 1992, 1993, 1994
3248842Ssbruno *	The Regents of the University of California.  All rights reserved.
4248842Ssbruno * Copyright (c) 1992, 1993, 1994, 1995, 1996
5248842Ssbruno *	Keith Bostic.  All rights reserved.
6248842Ssbruno *
7248842Ssbruno * See the LICENSE file for redistribution information.
8248842Ssbruno */
9248842Ssbruno
10248842Ssbruno#include "config.h"
11248842Ssbruno
12248842Ssbruno#ifndef lint
13248842Ssbrunostatic const char sccsid[] = "$Id: line.c,v 10.27 2015/04/03 14:17:21 zy Exp $";
14248842Ssbruno#endif /* not lint */
15248842Ssbruno
16248842Ssbruno#include <sys/types.h>
17248842Ssbruno#include <sys/queue.h>
18248842Ssbruno#include <sys/time.h>
19248842Ssbruno
20248842Ssbruno#include <bitstring.h>
21248842Ssbruno#include <errno.h>
22248842Ssbruno#include <limits.h>
23248842Ssbruno#include <stdio.h>
24248842Ssbruno#include <string.h>
25248842Ssbruno
26248842Ssbruno#include "common.h"
27248842Ssbruno#include "../vi/vi.h"
28248842Ssbruno
29248842Ssbrunostatic int scr_update(SCR *, recno_t, lnop_t, int);
30248842Ssbruno
31248842Ssbruno/*
32248842Ssbruno * db_eget --
33248842Ssbruno *	Front-end to db_get, special case handling for empty files.
34248842Ssbruno *
35248842Ssbruno * PUBLIC: int db_eget(SCR *, recno_t, CHAR_T **, size_t *, int *);
36248842Ssbruno */
37248842Ssbrunoint
38248842Ssbrunodb_eget(
39248842Ssbruno	SCR *sp,
40248842Ssbruno	recno_t lno,				/* Line number. */
41248842Ssbruno	CHAR_T **pp,				/* Pointer store. */
42248842Ssbruno	size_t *lenp,				/* Length store. */
43248842Ssbruno	int *isemptyp)
44248842Ssbruno{
45248842Ssbruno	recno_t l1;
46248842Ssbruno
47248842Ssbruno	if (isemptyp != NULL)
48248842Ssbruno		*isemptyp = 0;
49248842Ssbruno
50248842Ssbruno	/* If the line exists, simply return it. */
51248842Ssbruno	if (!db_get(sp, lno, 0, pp, lenp))
52248842Ssbruno		return (0);
53248842Ssbruno
54248842Ssbruno	/*
55248842Ssbruno	 * If the user asked for line 0 or line 1, i.e. the only possible
56248842Ssbruno	 * line in an empty file, find the last line of the file; db_last
57248842Ssbruno	 * fails loudly.
58248842Ssbruno	 */
59248842Ssbruno	if ((lno == 0 || lno == 1) && db_last(sp, &l1))
60248842Ssbruno		return (1);
61248842Ssbruno
62248842Ssbruno	/* If the file isn't empty, fail loudly. */
63248842Ssbruno	if ((lno != 0 && lno != 1) || l1 != 0) {
64248842Ssbruno		db_err(sp, lno);
65248842Ssbruno		return (1);
66248842Ssbruno	}
67248842Ssbruno
68248842Ssbruno	if (isemptyp != NULL)
69248842Ssbruno		*isemptyp = 1;
70248842Ssbruno
71248842Ssbruno	return (1);
72248842Ssbruno}
73248842Ssbruno
74248842Ssbruno/*
75248842Ssbruno * db_get --
76248842Ssbruno *	Look in the text buffers for a line, followed by the cache, followed
77248842Ssbruno *	by the database.
78248842Ssbruno *
79248842Ssbruno * PUBLIC: int db_get(SCR *, recno_t, u_int32_t, CHAR_T **, size_t *);
80248842Ssbruno */
81248842Ssbrunoint
82248842Ssbrunodb_get(
83248842Ssbruno	SCR *sp,
84248842Ssbruno	recno_t lno,				/* Line number. */
85248842Ssbruno	u_int32_t flags,
86248842Ssbruno	CHAR_T **pp,				/* Pointer store. */
87248842Ssbruno	size_t *lenp)				/* Length store. */
88248842Ssbruno{
89248842Ssbruno	DBT data, key;
90248842Ssbruno	EXF *ep;
91248842Ssbruno	TEXT *tp;
92248842Ssbruno	recno_t l1, l2;
93248842Ssbruno	CHAR_T *wp;
94248842Ssbruno	size_t wlen;
95248842Ssbruno
96248842Ssbruno	/*
97248842Ssbruno	 * The underlying recno stuff handles zero by returning NULL, but
98248842Ssbruno	 * have to have an OOB condition for the look-aside into the input
99248842Ssbruno	 * buffer anyway.
100248842Ssbruno	 */
101248842Ssbruno	if (lno == 0)
102248842Ssbruno		goto err1;
103248842Ssbruno
104248842Ssbruno	/* Check for no underlying file. */
105248842Ssbruno	if ((ep = sp->ep) == NULL) {
106248842Ssbruno		ex_emsg(sp, NULL, EXM_NOFILEYET);
107248842Ssbruno		goto err3;
108248842Ssbruno	}
109248842Ssbruno
110248842Ssbruno	if (LF_ISSET(DBG_NOCACHE))
111248842Ssbruno		goto nocache;
112248842Ssbruno
113248842Ssbruno	/*
114248842Ssbruno	 * Look-aside into the TEXT buffers and see if the line we want
115248842Ssbruno	 * is there.
116248842Ssbruno	 */
117248842Ssbruno	if (F_ISSET(sp, SC_TINPUT)) {
118248842Ssbruno		l1 = ((TEXT *)TAILQ_FIRST(sp->tiq))->lno;
119248842Ssbruno		l2 = ((TEXT *)TAILQ_LAST(sp->tiq, _texth))->lno;
120248842Ssbruno		if (l1 <= lno && l2 >= lno) {
121248842Ssbruno#if defined(DEBUG) && 0
122248842Ssbruno	TRACE(sp, "retrieve TEXT buffer line %lu\n", (u_long)lno);
123248842Ssbruno#endif
124248842Ssbruno			for (tp = TAILQ_FIRST(sp->tiq);
125248842Ssbruno			    tp->lno != lno; tp = TAILQ_NEXT(tp, q));
126248842Ssbruno			if (lenp != NULL)
127248842Ssbruno				*lenp = tp->len;
128248842Ssbruno			if (pp != NULL)
129248842Ssbruno				*pp = tp->lb;
130248842Ssbruno			return (0);
131248842Ssbruno		}
132248842Ssbruno		/*
133248842Ssbruno		 * Adjust the line number for the number of lines used
134248842Ssbruno		 * by the text input buffers.
135248842Ssbruno		 */
136248842Ssbruno		if (lno > l2)
137248842Ssbruno			lno -= l2 - l1;
138248842Ssbruno	}
139248842Ssbruno
140248842Ssbruno	/* Look-aside into the cache, and see if the line we want is there. */
141248842Ssbruno	if (lno == ep->c_lno) {
142248842Ssbruno#if defined(DEBUG) && 0
143248842Ssbruno	TRACE(sp, "retrieve cached line %lu\n", (u_long)lno);
144248871Sjoel#endif
145248842Ssbruno		if (lenp != NULL)
146248842Ssbruno			*lenp = ep->c_len;
147248842Ssbruno		if (pp != NULL)
148248842Ssbruno			*pp = ep->c_lp;
149248842Ssbruno		return (0);
150248842Ssbruno	}
151248842Ssbruno	ep->c_lno = OOBLNO;
152248842Ssbruno
153248842Ssbrunonocache:
154248842Ssbruno	/* Get the line from the underlying database. */
155248842Ssbruno	key.data = &lno;
156248842Ssbruno	key.size = sizeof(lno);
157248842Ssbruno	switch (ep->db->get(ep->db, &key, &data, 0)) {
158248842Ssbruno	case -1:
159248842Ssbruno		goto err2;
160248842Ssbruno	case 1:
161248842Ssbrunoerr1:		if (LF_ISSET(DBG_FATAL))
162248842Ssbrunoerr2:			db_err(sp, lno);
163248842Ssbrunoalloc_err:
164248842Ssbrunoerr3:		if (lenp != NULL)
165248842Ssbruno			*lenp = 0;
166248842Ssbruno		if (pp != NULL)
167248842Ssbruno			*pp = NULL;
168248842Ssbruno		return (1);
169248842Ssbruno	}
170248842Ssbruno
171248842Ssbruno	if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
172248842Ssbruno		if (!F_ISSET(sp, SC_CONV_ERROR)) {
173248842Ssbruno			F_SET(sp, SC_CONV_ERROR);
174248842Ssbruno			msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
175248842Ssbruno		}
176248842Ssbruno		goto err3;
177248842Ssbruno	}
178248842Ssbruno
179248842Ssbruno	/* Reset the cache. */
180248842Ssbruno	if (wp != data.data) {
181248842Ssbruno		BINC_GOTOW(sp, ep->c_lp, ep->c_blen, wlen);
182248842Ssbruno		MEMCPY(ep->c_lp, wp, wlen);
183248842Ssbruno	} else
184248842Ssbruno		ep->c_lp = data.data;
185248842Ssbruno	ep->c_lno = lno;
186248842Ssbruno	ep->c_len = wlen;
187248842Ssbruno
188248842Ssbruno#if defined(DEBUG) && 0
189248842Ssbruno	TRACE(sp, "retrieve DB line %lu\n", (u_long)lno);
190248842Ssbruno#endif
191248842Ssbruno	if (lenp != NULL)
192248842Ssbruno		*lenp = wlen;
193248842Ssbruno	if (pp != NULL)
194248842Ssbruno		*pp = ep->c_lp;
195248842Ssbruno	return (0);
196248842Ssbruno}
197248842Ssbruno
198248842Ssbruno/*
199248842Ssbruno * db_delete --
200248842Ssbruno *	Delete a line from the file.
201248842Ssbruno *
202248842Ssbruno * PUBLIC: int db_delete(SCR *, recno_t);
203248842Ssbruno */
204248842Ssbrunoint
205248842Ssbrunodb_delete(
206248842Ssbruno	SCR *sp,
207248842Ssbruno	recno_t lno)
208248842Ssbruno{
209248842Ssbruno	DBT key;
210248842Ssbruno	EXF *ep;
211248842Ssbruno
212248842Ssbruno#if defined(DEBUG) && 0
213248842Ssbruno	TRACE(sp, "delete line %lu\n", (u_long)lno);
214248842Ssbruno#endif
215248842Ssbruno	/* Check for no underlying file. */
216248842Ssbruno	if ((ep = sp->ep) == NULL) {
217248842Ssbruno		ex_emsg(sp, NULL, EXM_NOFILEYET);
218248842Ssbruno		return (1);
219248842Ssbruno	}
220248842Ssbruno
221248842Ssbruno	/* Update marks, @ and global commands. */
222248842Ssbruno	if (mark_insdel(sp, LINE_DELETE, lno))
223248842Ssbruno		return (1);
224248842Ssbruno	if (ex_g_insdel(sp, LINE_DELETE, lno))
225248842Ssbruno		return (1);
226248842Ssbruno
227248842Ssbruno	/* Log change. */
228248842Ssbruno	log_line(sp, lno, LOG_LINE_DELETE);
229248842Ssbruno
230248842Ssbruno	/* Update file. */
231248842Ssbruno	key.data = &lno;
232248842Ssbruno	key.size = sizeof(lno);
233248842Ssbruno	if (ep->db->del(ep->db, &key, 0) == 1) {
234248871Sjoel		msgq(sp, M_SYSERR,
235248842Ssbruno		    "003|unable to delete line %lu", (u_long)lno);
236248842Ssbruno		return (1);
237248842Ssbruno	}
238
239	/* Flush the cache, update line count, before screen update. */
240	if (lno <= ep->c_lno)
241		ep->c_lno = OOBLNO;
242	if (ep->c_nlines != OOBLNO)
243		--ep->c_nlines;
244
245	/* File now modified. */
246	if (F_ISSET(ep, F_FIRSTMODIFY))
247		(void)rcv_init(sp);
248	F_SET(ep, F_MODIFIED);
249
250	/* Update screen. */
251	return (scr_update(sp, lno, LINE_DELETE, 1));
252}
253
254/*
255 * db_append --
256 *	Append a line into the file.
257 *
258 * PUBLIC: int db_append(SCR *, int, recno_t, CHAR_T *, size_t);
259 */
260int
261db_append(
262	SCR *sp,
263	int update,
264	recno_t lno,
265	CHAR_T *p,
266	size_t len)
267{
268	DBT data, key;
269	EXF *ep;
270	char *fp;
271	size_t flen;
272	int rval;
273
274#if defined(DEBUG) && 0
275	TRACE(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
276#endif
277	/* Check for no underlying file. */
278	if ((ep = sp->ep) == NULL) {
279		ex_emsg(sp, NULL, EXM_NOFILEYET);
280		return (1);
281	}
282
283	INT2FILE(sp, p, len, fp, flen);
284
285	/* Update file. */
286	key.data = &lno;
287	key.size = sizeof(lno);
288	data.data = fp;
289	data.size = flen;
290	if (ep->db->put(ep->db, &key, &data, R_IAFTER) == -1) {
291		msgq(sp, M_SYSERR,
292		    "004|unable to append to line %lu", (u_long)lno);
293		return (1);
294	}
295
296	/* Flush the cache, update line count, before screen update. */
297	if (lno < ep->c_lno)
298		ep->c_lno = OOBLNO;
299	if (ep->c_nlines != OOBLNO)
300		++ep->c_nlines;
301
302	/* File now dirty. */
303	if (F_ISSET(ep, F_FIRSTMODIFY))
304		(void)rcv_init(sp);
305	F_SET(ep, F_MODIFIED);
306
307	/* Log change. */
308	log_line(sp, lno + 1, LOG_LINE_APPEND);
309
310	/* Update marks, @ and global commands. */
311	rval = 0;
312	if (mark_insdel(sp, LINE_INSERT, lno + 1))
313		rval = 1;
314	if (ex_g_insdel(sp, LINE_INSERT, lno + 1))
315		rval = 1;
316
317	/*
318	 * Update screen.
319	 *
320	 * XXX
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.
326	 */
327	return (scr_update(sp, lno, LINE_APPEND, update) || rval);
328}
329
330/*
331 * db_insert --
332 *	Insert a line into the file.
333 *
334 * PUBLIC: int db_insert(SCR *, recno_t, CHAR_T *, size_t);
335 */
336int
337db_insert(
338	SCR *sp,
339	recno_t lno,
340	CHAR_T *p,
341	size_t len)
342{
343	DBT data, key;
344	EXF *ep;
345	char *fp;
346	size_t flen;
347	int rval;
348
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);
352#endif
353	/* Check for no underlying file. */
354	if ((ep = sp->ep) == NULL) {
355		ex_emsg(sp, NULL, EXM_NOFILEYET);
356		return (1);
357	}
358
359	INT2FILE(sp, p, len, fp, flen);
360
361	/* Update file. */
362	key.data = &lno;
363	key.size = sizeof(lno);
364	data.data = fp;
365	data.size = flen;
366	if (ep->db->put(ep->db, &key, &data, R_IBEFORE) == -1) {
367		msgq(sp, M_SYSERR,
368		    "005|unable to insert at line %lu", (u_long)lno);
369		return (1);
370	}
371
372	/* Flush the cache, update line count, before screen update. */
373	if (lno >= ep->c_lno)
374		ep->c_lno = OOBLNO;
375	if (ep->c_nlines != OOBLNO)
376		++ep->c_nlines;
377
378	/* File now dirty. */
379	if (F_ISSET(ep, F_FIRSTMODIFY))
380		(void)rcv_init(sp);
381	F_SET(ep, F_MODIFIED);
382
383	/* Log change. */
384	log_line(sp, lno, LOG_LINE_INSERT);
385
386	/* Update marks, @ and global commands. */
387	rval = 0;
388	if (mark_insdel(sp, LINE_INSERT, lno))
389		rval = 1;
390	if (ex_g_insdel(sp, LINE_INSERT, lno))
391		rval = 1;
392
393	/* Update screen. */
394	return (scr_update(sp, lno, LINE_INSERT, 1) || rval);
395}
396
397/*
398 * db_set --
399 *	Store a line in the file.
400 *
401 * PUBLIC: int db_set(SCR *, recno_t, CHAR_T *, size_t);
402 */
403int
404db_set(
405	SCR *sp,
406	recno_t lno,
407	CHAR_T *p,
408	size_t len)
409{
410	DBT data, key;
411	EXF *ep;
412	char *fp;
413	size_t flen;
414
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);
418#endif
419	/* Check for no underlying file. */
420	if ((ep = sp->ep) == NULL) {
421		ex_emsg(sp, NULL, EXM_NOFILEYET);
422		return (1);
423	}
424
425	/* Log before change. */
426	log_line(sp, lno, LOG_LINE_RESET_B);
427
428	INT2FILE(sp, p, len, fp, flen);
429
430	/* Update file. */
431	key.data = &lno;
432	key.size = sizeof(lno);
433	data.data = fp;
434	data.size = flen;
435	if (ep->db->put(ep->db, &key, &data, 0) == -1) {
436		msgq(sp, M_SYSERR,
437		    "006|unable to store line %lu", (u_long)lno);
438		return (1);
439	}
440
441	/* Flush the cache, before logging or screen update. */
442	if (lno == ep->c_lno)
443		ep->c_lno = OOBLNO;
444
445	/* File now dirty. */
446	if (F_ISSET(ep, F_FIRSTMODIFY))
447		(void)rcv_init(sp);
448	F_SET(ep, F_MODIFIED);
449
450	/* Log after change. */
451	log_line(sp, lno, LOG_LINE_RESET_F);
452
453	/* Update screen. */
454	return (scr_update(sp, lno, LINE_RESET, 1));
455}
456
457/*
458 * db_exist --
459 *	Return if a line exists.
460 *
461 * PUBLIC: int db_exist(SCR *, recno_t);
462 */
463int
464db_exist(
465	SCR *sp,
466	recno_t lno)
467{
468	EXF *ep;
469
470	/* Check for no underlying file. */
471	if ((ep = sp->ep) == NULL) {
472		ex_emsg(sp, NULL, EXM_NOFILEYET);
473		return (1);
474	}
475
476	if (lno == OOBLNO)
477		return (0);
478
479	/*
480	 * Check the last-line number cache.  Adjust the cached line
481	 * number for the lines used by the text input buffers.
482	 */
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));
487
488	/* Go get the line. */
489	return (!db_get(sp, lno, 0, NULL, NULL));
490}
491
492/*
493 * db_last --
494 *	Return the number of lines in the file.
495 *
496 * PUBLIC: int db_last(SCR *, recno_t *);
497 */
498int
499db_last(
500	SCR *sp,
501	recno_t *lnop)
502{
503	DBT data, key;
504	EXF *ep;
505	recno_t lno;
506	CHAR_T *wp;
507	size_t wlen;
508
509	/* Check for no underlying file. */
510	if ((ep = sp->ep) == NULL) {
511		ex_emsg(sp, NULL, EXM_NOFILEYET);
512		return (1);
513	}
514
515	/*
516	 * Check the last-line number cache.  Adjust the cached line
517	 * number for the lines used by the text input buffers.
518	 */
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;
524		return (0);
525	}
526
527	key.data = &lno;
528	key.size = sizeof(lno);
529
530	switch (ep->db->seq(ep->db, &key, &data, R_LAST)) {
531	case -1:
532alloc_err:
533		msgq(sp, M_SYSERR, "007|unable to get last line");
534		*lnop = 0;
535		return (1);
536	case 1:
537		*lnop = 0;
538		return (0);
539	}
540
541	memcpy(&lno, key.data, sizeof(lno));
542
543	if (lno != ep->c_lno) {
544		FILE2INT(sp, data.data, data.size, wp, wlen);
545
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);
550		} else
551			ep->c_lp = data.data;
552		ep->c_lno = lno;
553		ep->c_len = wlen;
554	}
555	ep->c_nlines = lno;
556
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);
561	return (0);
562}
563
564/*
565 * db_rget --
566 *	Retrieve a raw line from the database.
567 *
568 * PUBLIC: int db_rget(SCR *, recno_t, char **, size_t *);
569 */
570int
571db_rget(
572	SCR *sp,
573	recno_t lno,				/* Line number. */
574	char **pp,				/* Pointer store. */
575	size_t *lenp)				/* Length store. */
576{
577	DBT data, key;
578	EXF *ep = sp->ep;
579	int rval;
580
581	/* Get the line from the underlying database. */
582	key.data = &lno;
583	key.size = sizeof(lno);
584	if ((rval = ep->db->get(ep->db, &key, &data, 0)) == 0)
585	{
586		*lenp = data.size;
587		*pp = data.data;
588	}
589
590	return (rval);
591}
592
593/*
594 * db_rset --
595 *	Store a raw line into the database.
596 *
597 * PUBLIC: int db_rset(SCR *, recno_t, char *, size_t);
598 */
599int
600db_rset(
601	SCR *sp,
602	recno_t lno,
603	char *p,
604	size_t len)
605{
606	DBT data, key;
607	EXF *ep = sp->ep;
608
609	/* Update file. */
610	key.data = &lno;
611	key.size = sizeof(lno);
612	data.data = p;
613	data.size = len;
614	return ep->db->put(ep->db, &key, &data, 0);
615}
616
617/*
618 * db_err --
619 *	Report a line error.
620 *
621 * PUBLIC: void db_err(SCR *, recno_t);
622 */
623void
624db_err(
625	SCR *sp,
626	recno_t lno)
627{
628	msgq(sp, M_ERR,
629	    "008|Error: unable to retrieve line %lu", (u_long)lno);
630}
631
632/*
633 * scr_update --
634 *	Update all of the screens that are backed by the file that
635 *	just changed.
636 */
637static int
638scr_update(
639	SCR *sp,
640	recno_t lno,
641	lnop_t op,
642	int current)
643{
644	EXF *ep;
645	SCR *tsp;
646
647	if (F_ISSET(sp, SC_EX))
648		return (0);
649
650	ep = sp->ep;
651	if (ep->refcnt != 1)
652		TAILQ_FOREACH(tsp, sp->gp->dq, q)
653			if (sp != tsp && tsp->ep == ep)
654				if (vs_change(tsp, lno, op))
655					return (1);
656	return (current ? vs_change(sp, lno, op) : 0);
657}
658