add NEXINIT as alternative to EXINIT
[nvi.git] / common / db.c
blobf4948ff6cff8ca90b997a0e12cce157584e71db7
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: db.c,v 8.19 1993/12/27 16:50:37 bostic Exp $ (Berkeley) $Date: 1993/12/27 16:50:37 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <errno.h>
15 #include <string.h>
17 #include "vi.h"
19 static inline int scr_update __P((SCR *, EXF *, recno_t, enum operation, int));
22 * file_gline --
23 * Look in the text buffers for a line; if it's not there
24 * call file_rline to retrieve it from the database.
26 char *
27 file_gline(sp, ep, lno, lenp)
28 SCR *sp;
29 EXF *ep;
30 recno_t lno; /* Line number. */
31 size_t *lenp; /* Length store. */
33 TEXT *tp;
34 recno_t l1, l2;
37 * The underlying recno stuff handles zero by returning NULL, but
38 * have to have an oob condition for the look-aside into the input
39 * buffer anyway.
41 if (lno == 0)
42 return (NULL);
45 * Look-aside into the TEXT buffers and see if the line we want
46 * is there.
48 if (F_ISSET(sp, S_INPUT)) {
49 l1 = ((TEXT *)sp->tiq.cqh_first)->lno;
50 l2 = ((TEXT *)sp->tiq.cqh_last)->lno;
51 if (l1 <= lno && l2 >= lno) {
52 for (tp = sp->tiq.cqh_first;
53 tp->lno != lno; tp = tp->q.cqe_next);
54 if (lenp)
55 *lenp = tp->len;
56 return (tp->lb);
59 * Adjust the line number for the number of lines used
60 * by the text input buffers.
62 if (lno > l2)
63 lno -= l2 - l1;
65 return (file_rline(sp, ep, lno, lenp));
69 * file_rline --
70 * Look in the cache for a line; if it's not there retrieve
71 * it from the file.
73 char *
74 file_rline(sp, ep, lno, lenp)
75 SCR *sp;
76 EXF *ep;
77 recno_t lno; /* Line number. */
78 size_t *lenp; /* Length store. */
80 DBT data, key;
82 /* Check the cache. */
83 if (lno == ep->c_lno) {
84 if (lenp)
85 *lenp = ep->c_len;
86 return (ep->c_lp);
88 ep->c_lno = OOBLNO;
90 /* Get the line from the underlying database. */
91 key.data = &lno;
92 key.size = sizeof(lno);
93 switch (ep->db->get(ep->db, &key, &data, 0)) {
94 case -1:
95 msgq(sp, M_ERR,
96 "Error: %s/%d: unable to get line %u: %s.",
97 tail(__FILE__), __LINE__, lno, strerror(errno));
98 /* FALLTHROUGH */
99 case 1:
100 return (NULL);
101 /* NOTREACHED */
103 if (lenp)
104 *lenp = data.size;
106 /* Fill the cache. */
107 ep->c_lno = lno;
108 ep->c_len = data.size;
109 ep->c_lp = data.data;
111 return (data.data);
115 * file_dline --
116 * Delete a line from the file.
119 file_dline(sp, ep, lno)
120 SCR *sp;
121 EXF *ep;
122 recno_t lno;
124 DBT key;
126 #if defined(DEBUG) && 0
127 TRACE(sp, "delete line %lu\n", lno);
128 #endif
130 * XXX
132 * Marks and global commands have to know when lines are
133 * inserted or deleted.
135 mark_insdel(sp, ep, LINE_DELETE, lno);
136 global_insdel(sp, ep, LINE_DELETE, lno);
138 /* Log change. */
139 log_line(sp, ep, lno, LOG_LINE_DELETE);
141 /* Update file. */
142 key.data = &lno;
143 key.size = sizeof(lno);
144 if (ep->db->del(ep->db, &key, 0) == 1) {
145 msgq(sp, M_ERR,
146 "Error: %s/%d: unable to delete line %u: %s.",
147 tail(__FILE__), __LINE__, lno, strerror(errno));
148 return (1);
151 /* Flush the cache, update line count, before screen update. */
152 if (lno <= ep->c_lno)
153 ep->c_lno = OOBLNO;
154 if (ep->c_nlines != OOBLNO)
155 --ep->c_nlines;
157 /* File now dirty. */
158 if (F_ISSET(ep, F_FIRSTMODIFY))
159 (void)rcv_init(sp, ep);
160 F_SET(ep, F_MODIFIED);
162 /* Update screen. */
163 return (scr_update(sp, ep, lno, LINE_DELETE, 1));
167 * file_aline --
168 * Append a line into the file.
171 file_aline(sp, ep, update, lno, p, len)
172 SCR *sp;
173 EXF *ep;
174 int update;
175 recno_t lno;
176 char *p;
177 size_t len;
179 DBT data, key;
180 recno_t lline;
182 #if defined(DEBUG) && 0
183 TRACE(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
184 #endif
186 * Very nasty special case. The historic vi code displays a single
187 * space (or a '$' if the list option is set) for the first line in
188 * an "empty" file. If we "insert" a line, that line gets scrolled
189 * down, not repainted, so it's incorrect when we refresh the the
190 * screen. This is really hard to find and fix in the vi code -- the
191 * text input functions detect it explicitly and don't insert a new
192 * line. The hack here is to repaint the screen if we're appending
193 * to an empty file.
195 if (lno == 0) {
196 if (file_lline(sp, ep, &lline))
197 return (1);
198 if (lline == 0)
199 F_SET(sp, S_REDRAW);
202 /* Update file. */
203 key.data = &lno;
204 key.size = sizeof(lno);
205 data.data = p;
206 data.size = len;
207 if (ep->db->put(ep->db, &key, &data, R_IAFTER) == -1) {
208 msgq(sp, M_ERR,
209 "Error: %s/%d: unable to append to line %u: %s.",
210 tail(__FILE__), __LINE__, lno, strerror(errno));
211 return (1);
214 /* Flush the cache, update line count, before screen update. */
215 if (lno < ep->c_lno)
216 ep->c_lno = OOBLNO;
217 if (ep->c_nlines != OOBLNO)
218 ++ep->c_nlines;
220 /* File now dirty. */
221 if (F_ISSET(ep, F_FIRSTMODIFY))
222 (void)rcv_init(sp, ep);
223 F_SET(ep, F_MODIFIED);
225 /* Log change. */
226 log_line(sp, ep, lno + 1, LOG_LINE_APPEND);
229 * XXX
231 * Marks and global commands have to know when lines are
232 * inserted or deleted.
234 mark_insdel(sp, ep, LINE_INSERT, lno + 1);
235 global_insdel(sp, ep, LINE_INSERT, lno + 1);
238 * Update screen.
240 * XXX
241 * Nasty hack. If multiple lines are input by the user, they aren't
242 * committed until an <ESC> is entered. The problem is the screen was
243 * updated/scrolled as each line was entered. So, when this routine
244 * is called to copy the new lines from the cut buffer into the file,
245 * it has to know not to update the screen again.
247 return (scr_update(sp, ep, lno, LINE_APPEND, update));
251 * file_iline --
252 * Insert a line into the file.
255 file_iline(sp, ep, lno, p, len)
256 SCR *sp;
257 EXF *ep;
258 recno_t lno;
259 char *p;
260 size_t len;
262 DBT data, key;
263 recno_t lline;
265 #if defined(DEBUG) && 0
266 TRACE(sp,
267 "insert before %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
268 #endif
270 /* Very nasty special case. See comment in file_aline(). */
271 if (lno == 1) {
272 if (file_lline(sp, ep, &lline))
273 return (1);
274 if (lline == 0)
275 F_SET(sp, S_REDRAW);
278 /* Update file. */
279 key.data = &lno;
280 key.size = sizeof(lno);
281 data.data = p;
282 data.size = len;
283 if (ep->db->put(ep->db, &key, &data, R_IBEFORE) == -1) {
284 msgq(sp, M_ERR,
285 "Error: %s/%d: unable to insert at line %u: %s.",
286 tail(__FILE__), __LINE__, lno, strerror(errno));
287 return (1);
290 /* Flush the cache, update line count, before screen update. */
291 if (lno >= ep->c_lno)
292 ep->c_lno = OOBLNO;
293 if (ep->c_nlines != OOBLNO)
294 ++ep->c_nlines;
296 /* File now dirty. */
297 if (F_ISSET(ep, F_FIRSTMODIFY))
298 (void)rcv_init(sp, ep);
299 F_SET(ep, F_MODIFIED);
301 /* Log change. */
302 log_line(sp, ep, lno, LOG_LINE_INSERT);
305 * XXX
307 * Marks and global commands have to know when lines are
308 * inserted or deleted.
310 mark_insdel(sp, ep, LINE_INSERT, lno);
311 global_insdel(sp, ep, LINE_INSERT, lno);
313 /* Update screen. */
314 return (scr_update(sp, ep, lno, LINE_INSERT, 1));
318 * file_sline --
319 * Store a line in the file.
322 file_sline(sp, ep, lno, p, len)
323 SCR *sp;
324 EXF *ep;
325 recno_t lno;
326 char *p;
327 size_t len;
329 DBT data, key;
331 #if defined(DEBUG) && 0
332 TRACE(sp,
333 "replace line %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
334 #endif
335 /* Log before change. */
336 log_line(sp, ep, lno, LOG_LINE_RESET_B);
338 /* Update file. */
339 key.data = &lno;
340 key.size = sizeof(lno);
341 data.data = p;
342 data.size = len;
343 if (ep->db->put(ep->db, &key, &data, 0) == -1) {
344 msgq(sp, M_ERR,
345 "Error: %s/%d: unable to store line %u: %s.",
346 tail(__FILE__), __LINE__, lno, strerror(errno));
347 return (1);
350 /* Flush the cache, before logging or screen update. */
351 if (lno == ep->c_lno)
352 ep->c_lno = OOBLNO;
354 /* File now dirty. */
355 if (F_ISSET(ep, F_FIRSTMODIFY))
356 (void)rcv_init(sp, ep);
357 F_SET(ep, F_MODIFIED);
359 /* Log after change. */
360 log_line(sp, ep, lno, LOG_LINE_RESET_F);
362 /* Update screen. */
363 return (scr_update(sp, ep, lno, LINE_RESET, 1));
367 * file_lline --
368 * Return the number of lines in the file.
371 file_lline(sp, ep, lnop)
372 SCR *sp;
373 EXF *ep;
374 recno_t *lnop;
376 DBT data, key;
377 recno_t lno;
379 /* Check the cache. */
380 if (ep->c_nlines != OOBLNO) {
381 *lnop = (F_ISSET(sp, S_INPUT) &&
382 ((TEXT *)sp->tiq.cqh_last)->lno > ep->c_nlines ?
383 ((TEXT *)sp->tiq.cqh_last)->lno : ep->c_nlines);
384 return (0);
387 key.data = &lno;
388 key.size = sizeof(lno);
390 switch (ep->db->seq(ep->db, &key, &data, R_LAST)) {
391 case -1:
392 msgq(sp, M_ERR,
393 "Error: %s/%d: unable to get last line: %s.",
394 tail(__FILE__), __LINE__, strerror(errno));
395 *lnop = 0;
396 return (1);
397 case 1:
398 lno = 0;
399 break;
400 default:
401 memmove(&lno, key.data, sizeof(lno));
402 break;
405 /* Fill the cache. */
406 ep->c_nlines = ep->c_lno = lno;
407 ep->c_len = data.size;
408 ep->c_lp = data.data;
410 *lnop = (F_ISSET(sp, S_INPUT) &&
411 ((TEXT *)sp->tiq.cqh_last)->lno > lno ?
412 ((TEXT *)sp->tiq.cqh_last)->lno : lno);
413 return (0);
417 * scr_update --
418 * Update all of the screens that are backed by the file that
419 * just changed.
421 static inline int
422 scr_update(sp, ep, lno, op, current)
423 SCR *sp;
424 EXF *ep;
425 recno_t lno;
426 enum operation op;
427 int current;
429 SCR *tsp;
431 if (ep->refcnt != 1)
432 for (tsp = sp->gp->dq.cqh_first;
433 tsp != (void *)&sp->gp->dq; tsp = tsp->q.cqe_next)
434 if (sp != tsp && tsp->ep == ep)
435 (void)sp->s_change(tsp, ep, lno, op);
436 return (current && sp->s_change(sp, ep, lno, op));