vi_db.c revision 1.5 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 1992, 1993, 1994
3 1.1 christos * The Regents of the University of California. All rights reserved.
4 1.1 christos * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 1.1 christos * Keith Bostic. All rights reserved.
6 1.1 christos *
7 1.1 christos * See the LICENSE file for redistribution information.
8 1.1 christos */
9 1.1 christos
10 1.1 christos #include "config.h"
11 1.1 christos
12 1.1 christos #ifndef lint
13 1.1 christos static const char sccsid[] = "Id: db.c,v 10.48 2002/06/08 19:32:52 skimo Exp (Berkeley) Date: 2002/06/08 19:32:52 ";
14 1.1 christos #endif /* not lint */
15 1.1 christos
16 1.1 christos #include <sys/types.h>
17 1.1 christos #include <sys/queue.h>
18 1.1 christos #include <sys/time.h>
19 1.1 christos #include <sys/stat.h>
20 1.1 christos
21 1.1 christos #include <bitstring.h>
22 1.1 christos #include <errno.h>
23 1.1 christos #include <limits.h>
24 1.1 christos #include <stdio.h>
25 1.1 christos #include <string.h>
26 1.5 christos #include <stdlib.h>
27 1.5 christos #include <unistd.h>
28 1.1 christos
29 1.1 christos #include "common.h"
30 1.5 christos #include "dbinternal.h"
31 1.1 christos #include "../vi/vi.h"
32 1.1 christos
33 1.5 christos static int append __P((SCR*, db_recno_t, const CHAR_T*, size_t, lnop_t, int));
34 1.1 christos
35 1.1 christos /*
36 1.1 christos * db_eget --
37 1.1 christos * Front-end to db_get, special case handling for empty files.
38 1.1 christos *
39 1.1 christos * PUBLIC: int db_eget __P((SCR *, db_recno_t, CHAR_T **, size_t *, int *));
40 1.1 christos */
41 1.1 christos int
42 1.1 christos db_eget(SCR *sp, db_recno_t lno, CHAR_T **pp, size_t *lenp, int *isemptyp)
43 1.1 christos
44 1.1 christos /* Line number. */
45 1.1 christos /* Pointer store. */
46 1.1 christos /* Length store. */
47 1.1 christos
48 1.1 christos {
49 1.1 christos db_recno_t l1;
50 1.1 christos
51 1.1 christos if (isemptyp != NULL)
52 1.1 christos *isemptyp = 0;
53 1.1 christos
54 1.1 christos /* If the line exists, simply return it. */
55 1.1 christos if (!db_get(sp, lno, 0, pp, lenp))
56 1.1 christos return (0);
57 1.1 christos
58 1.1 christos /*
59 1.1 christos * If the user asked for line 0 or line 1, i.e. the only possible
60 1.1 christos * line in an empty file, find the last line of the file; db_last
61 1.1 christos * fails loudly.
62 1.1 christos */
63 1.1 christos if ((lno == 0 || lno == 1) && db_last(sp, &l1))
64 1.1 christos return (1);
65 1.1 christos
66 1.1 christos /* If the file isn't empty, fail loudly. */
67 1.1 christos if ((lno != 0 && lno != 1) || l1 != 0) {
68 1.1 christos db_err(sp, lno);
69 1.1 christos return (1);
70 1.1 christos }
71 1.1 christos
72 1.1 christos if (isemptyp != NULL)
73 1.1 christos *isemptyp = 1;
74 1.1 christos
75 1.1 christos return (1);
76 1.1 christos }
77 1.1 christos
78 1.1 christos /*
79 1.1 christos * db_get --
80 1.1 christos * Look in the text buffers for a line, followed by the cache, followed
81 1.1 christos * by the database.
82 1.1 christos *
83 1.1 christos * PUBLIC: int db_get __P((SCR *, db_recno_t, u_int32_t, CHAR_T **, size_t *));
84 1.1 christos */
85 1.1 christos int
86 1.1 christos db_get(SCR *sp, db_recno_t lno, u_int32_t flags, CHAR_T **pp, size_t *lenp)
87 1.1 christos /* Line number. */ /* Pointer store. */ /* Length store. */
88 1.1 christos {
89 1.1 christos DBT data, key;
90 1.1 christos EXF *ep;
91 1.1 christos TEXT *tp;
92 1.1 christos db_recno_t l1, l2;
93 1.5 christos const CHAR_T *wp;
94 1.1 christos size_t wlen;
95 1.1 christos size_t nlen;
96 1.1 christos
97 1.1 christos /*
98 1.1 christos * The underlying recno stuff handles zero by returning NULL, but
99 1.1 christos * have to have an OOB condition for the look-aside into the input
100 1.1 christos * buffer anyway.
101 1.1 christos */
102 1.1 christos if (lno == 0)
103 1.1 christos goto err1;
104 1.1 christos
105 1.1 christos /* Check for no underlying file. */
106 1.1 christos if ((ep = sp->ep) == NULL) {
107 1.1 christos ex_emsg(sp, NULL, EXM_NOFILEYET);
108 1.1 christos goto err3;
109 1.1 christos }
110 1.1 christos
111 1.1 christos if (LF_ISSET(DBG_NOCACHE))
112 1.1 christos goto nocache;
113 1.1 christos
114 1.1 christos /*
115 1.1 christos * Look-aside into the TEXT buffers and see if the line we want
116 1.1 christos * is there.
117 1.1 christos */
118 1.1 christos if (F_ISSET(sp, SC_TINPUT)) {
119 1.3 christos l1 = TAILQ_FIRST(&sp->tiq)->lno;
120 1.3 christos l2 = TAILQ_LAST(&sp->tiq, _texth)->lno;
121 1.1 christos if (l1 <= lno && l2 >= lno) {
122 1.1 christos #if defined(DEBUG) && 0
123 1.1 christos vtrace(sp,
124 1.1 christos "retrieve TEXT buffer line %lu\n", (u_long)lno);
125 1.1 christos #endif
126 1.3 christos for (tp = TAILQ_FIRST(&sp->tiq);
127 1.3 christos tp->lno != lno; tp = TAILQ_NEXT(tp, q));
128 1.1 christos if (lenp != NULL)
129 1.1 christos *lenp = tp->len;
130 1.1 christos if (pp != NULL)
131 1.1 christos *pp = tp->lb;
132 1.1 christos return (0);
133 1.1 christos }
134 1.1 christos /*
135 1.1 christos * Adjust the line number for the number of lines used
136 1.1 christos * by the text input buffers.
137 1.1 christos */
138 1.1 christos if (lno > l2)
139 1.1 christos lno -= l2 - l1;
140 1.1 christos }
141 1.1 christos
142 1.1 christos /* Look-aside into the cache, and see if the line we want is there. */
143 1.1 christos if (lno == sp->c_lno) {
144 1.1 christos #if defined(DEBUG) && 0
145 1.1 christos vtrace(sp, "retrieve cached line %lu\n", (u_long)lno);
146 1.1 christos #endif
147 1.1 christos if (lenp != NULL)
148 1.1 christos *lenp = sp->c_len;
149 1.1 christos if (pp != NULL)
150 1.1 christos *pp = sp->c_lp;
151 1.1 christos return (0);
152 1.1 christos }
153 1.1 christos sp->c_lno = OOBLNO;
154 1.1 christos
155 1.1 christos nocache:
156 1.1 christos nlen = 1024;
157 1.1 christos retry:
158 1.1 christos /* data.size contains length in bytes */
159 1.1 christos BINC_GOTO(sp, CHAR_T, sp->c_lp, sp->c_blen, nlen);
160 1.1 christos
161 1.1 christos /* Get the line from the underlying database. */
162 1.1 christos memset(&key, 0, sizeof(key));
163 1.1 christos key.data = &lno;
164 1.1 christos key.size = sizeof(lno);
165 1.1 christos memset(&data, 0, sizeof(data));
166 1.1 christos data.data = sp->c_lp;
167 1.1 christos data.ulen = sp->c_blen;
168 1.1 christos data.flags = DB_DBT_USERMEM;
169 1.1 christos switch (ep->db->get(ep->db, NULL, &key, &data, 0)) {
170 1.1 christos case DB_BUFFER_SMALL:
171 1.1 christos nlen = data.size;
172 1.1 christos goto retry;
173 1.1 christos default:
174 1.1 christos goto err2;
175 1.1 christos case DB_NOTFOUND:
176 1.1 christos err1: if (LF_ISSET(DBG_FATAL))
177 1.1 christos err2: db_err(sp, lno);
178 1.1 christos alloc_err:
179 1.1 christos err3: if (lenp != NULL)
180 1.1 christos *lenp = 0;
181 1.1 christos if (pp != NULL)
182 1.1 christos *pp = NULL;
183 1.1 christos return (1);
184 1.1 christos case 0:
185 1.1 christos ;
186 1.1 christos }
187 1.1 christos
188 1.1 christos if (FILE2INT(sp, data.data, data.size, wp, wlen)) {
189 1.1 christos if (!F_ISSET(sp, SC_CONV_ERROR)) {
190 1.1 christos F_SET(sp, SC_CONV_ERROR);
191 1.1 christos msgq(sp, M_ERR, "324|Conversion error on line %d", lno);
192 1.1 christos }
193 1.1 christos goto err3;
194 1.1 christos }
195 1.1 christos
196 1.1 christos /* Reset the cache. */
197 1.1 christos if (wp != data.data) {
198 1.1 christos BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
199 1.1 christos MEMCPYW(sp->c_lp, wp, wlen);
200 1.1 christos }
201 1.1 christos sp->c_lno = lno;
202 1.1 christos sp->c_len = wlen;
203 1.1 christos
204 1.1 christos #if defined(DEBUG) && 0
205 1.1 christos vtrace(sp, "retrieve DB line %lu\n", (u_long)lno);
206 1.1 christos #endif
207 1.1 christos if (lenp != NULL)
208 1.1 christos *lenp = wlen;
209 1.1 christos if (pp != NULL)
210 1.1 christos *pp = sp->c_lp;
211 1.1 christos return (0);
212 1.1 christos }
213 1.1 christos
214 1.1 christos /*
215 1.1 christos * db_delete --
216 1.1 christos * Delete a line from the file.
217 1.1 christos *
218 1.1 christos * PUBLIC: int db_delete __P((SCR *, db_recno_t));
219 1.1 christos */
220 1.1 christos int
221 1.1 christos db_delete(SCR *sp, db_recno_t lno)
222 1.1 christos {
223 1.1 christos DBT key;
224 1.1 christos EXF *ep;
225 1.1 christos
226 1.1 christos #if defined(DEBUG) && 0
227 1.1 christos vtrace(sp, "delete line %lu\n", (u_long)lno);
228 1.1 christos #endif
229 1.1 christos /* Check for no underlying file. */
230 1.1 christos if ((ep = sp->ep) == NULL) {
231 1.1 christos ex_emsg(sp, NULL, EXM_NOFILEYET);
232 1.1 christos return (1);
233 1.1 christos }
234 1.1 christos if (ep->l_win && ep->l_win != sp->wp) {
235 1.1 christos ex_emsg(sp, NULL, EXM_LOCKED);
236 1.1 christos return 1;
237 1.1 christos }
238 1.1 christos
239 1.1 christos /* Update marks, @ and global commands. */
240 1.1 christos if (line_insdel(sp, LINE_DELETE, lno))
241 1.1 christos return 1;
242 1.1 christos
243 1.1 christos /* Log before change. */
244 1.1 christos log_line(sp, lno, LOG_LINE_DELETE_B);
245 1.1 christos
246 1.1 christos /* Update file. */
247 1.1 christos memset(&key, 0, sizeof(key));
248 1.1 christos key.data = &lno;
249 1.1 christos key.size = sizeof(lno);
250 1.1 christos if ((sp->db_error = ep->db->del(ep->db, NULL, &key, 0)) != 0) {
251 1.1 christos msgq(sp, M_DBERR, "003|unable to delete line %lu",
252 1.1 christos (u_long)lno);
253 1.1 christos return (1);
254 1.1 christos }
255 1.1 christos
256 1.1 christos /* Flush the cache, update line count, before screen update. */
257 1.1 christos update_cache(sp, LINE_DELETE, lno);
258 1.1 christos
259 1.1 christos /* File now modified. */
260 1.1 christos if (F_ISSET(ep, F_FIRSTMODIFY))
261 1.1 christos (void)rcv_init(sp);
262 1.1 christos F_SET(ep, F_MODIFIED);
263 1.1 christos
264 1.1 christos /* Log after change. */
265 1.1 christos log_line(sp, lno, LOG_LINE_DELETE_F);
266 1.1 christos
267 1.1 christos /* Update screen. */
268 1.1 christos return (scr_update(sp, lno, LINE_DELETE, 1));
269 1.1 christos }
270 1.1 christos
271 1.1 christos /* maybe this could be simpler
272 1.1 christos *
273 1.1 christos * DB3 behaves differently from DB1
274 1.1 christos *
275 1.1 christos * if lno != 0 just go to lno and put the new line after it
276 1.1 christos * if lno == 0 then if there are any record, put in front of the first
277 1.1 christos * otherwise just append to the end thus creating the first
278 1.1 christos * line
279 1.1 christos */
280 1.1 christos static int
281 1.5 christos append(SCR *sp, db_recno_t lno, const CHAR_T *p, size_t len, lnop_t op, int update)
282 1.1 christos {
283 1.1 christos DBT data, key;
284 1.1 christos DBC *dbcp_put;
285 1.1 christos EXF *ep;
286 1.5 christos const char *fp;
287 1.1 christos size_t flen;
288 1.1 christos int rval;
289 1.1 christos
290 1.1 christos /* Check for no underlying file. */
291 1.1 christos if ((ep = sp->ep) == NULL) {
292 1.1 christos ex_emsg(sp, NULL, EXM_NOFILEYET);
293 1.1 christos return (1);
294 1.1 christos }
295 1.1 christos if (ep->l_win && ep->l_win != sp->wp) {
296 1.1 christos ex_emsg(sp, NULL, EXM_LOCKED);
297 1.1 christos return 1;
298 1.1 christos }
299 1.1 christos
300 1.1 christos /* Log before change. */
301 1.1 christos log_line(sp, lno + 1, LOG_LINE_APPEND_B);
302 1.1 christos
303 1.1 christos /* Update file. */
304 1.1 christos memset(&key, 0, sizeof(key));
305 1.1 christos key.data = &lno;
306 1.1 christos key.size = sizeof(lno);
307 1.1 christos memset(&data, 0, sizeof(data));
308 1.1 christos
309 1.1 christos if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp_put, 0)) != 0)
310 1.1 christos return 1;
311 1.1 christos
312 1.1 christos INT2FILE(sp, p, len, fp, flen);
313 1.1 christos
314 1.1 christos if (lno != 0) {
315 1.1 christos if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_SET)) != 0)
316 1.1 christos goto err2;
317 1.1 christos
318 1.5 christos data.data = __UNCONST(fp);
319 1.1 christos data.size = flen;
320 1.1 christos if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_AFTER)) != 0) {
321 1.1 christos err2:
322 1.1 christos (void)dbcp_put->c_close(dbcp_put);
323 1.1 christos msgq(sp, M_DBERR,
324 1.1 christos (op == LINE_APPEND)
325 1.1 christos ? "004|unable to append to line %lu"
326 1.1 christos : "005|unable to insert at line %lu",
327 1.1 christos (u_long)lno);
328 1.1 christos return (1);
329 1.1 christos }
330 1.1 christos } else {
331 1.1 christos if ((sp->db_error = dbcp_put->c_get(dbcp_put, &key, &data, DB_FIRST)) != 0) {
332 1.1 christos if (sp->db_error != DB_NOTFOUND)
333 1.1 christos goto err2;
334 1.1 christos
335 1.5 christos data.data = __UNCONST(fp);
336 1.1 christos data.size = flen;
337 1.1 christos if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, DB_APPEND)) != 0) {
338 1.1 christos goto err2;
339 1.1 christos }
340 1.1 christos } else {
341 1.1 christos key.data = &lno;
342 1.1 christos key.size = sizeof(lno);
343 1.5 christos data.data = __UNCONST(fp);
344 1.1 christos data.size = flen;
345 1.1 christos if ((sp->db_error = dbcp_put->c_put(dbcp_put, &key, &data, DB_BEFORE)) != 0) {
346 1.1 christos goto err2;
347 1.1 christos }
348 1.1 christos }
349 1.1 christos }
350 1.1 christos
351 1.1 christos (void)dbcp_put->c_close(dbcp_put);
352 1.1 christos
353 1.1 christos /* Flush the cache, update line count, before screen update. */
354 1.1 christos update_cache(sp, LINE_INSERT, lno);
355 1.1 christos
356 1.1 christos /* File now dirty. */
357 1.1 christos if (F_ISSET(ep, F_FIRSTMODIFY))
358 1.1 christos (void)rcv_init(sp);
359 1.1 christos F_SET(ep, F_MODIFIED);
360 1.1 christos
361 1.1 christos /* Log after change. */
362 1.1 christos log_line(sp, lno + 1, LOG_LINE_APPEND_F);
363 1.1 christos
364 1.1 christos /* Update marks, @ and global commands. */
365 1.1 christos rval = line_insdel(sp, LINE_INSERT, lno + 1);
366 1.1 christos
367 1.1 christos /*
368 1.1 christos * Update screen.
369 1.1 christos *
370 1.1 christos * comment copied from db_append
371 1.1 christos * XXX
372 1.1 christos * Nasty hack. If multiple lines are input by the user, they aren't
373 1.1 christos * committed until an <ESC> is entered. The problem is the screen was
374 1.1 christos * updated/scrolled as each line was entered. So, when this routine
375 1.1 christos * is called to copy the new lines from the cut buffer into the file,
376 1.1 christos * it has to know not to update the screen again.
377 1.1 christos */
378 1.1 christos return (scr_update(sp, lno + 1, LINE_INSERT, update) || rval);
379 1.1 christos }
380 1.1 christos
381 1.1 christos /*
382 1.1 christos * db_append --
383 1.1 christos * Append a line into the file.
384 1.1 christos *
385 1.2 christos * PUBLIC: int db_append __P((SCR *, int, db_recno_t, const CHAR_T *, size_t));
386 1.1 christos */
387 1.1 christos int
388 1.2 christos db_append(SCR *sp, int update, db_recno_t lno, const CHAR_T *p, size_t len)
389 1.1 christos {
390 1.1 christos #if defined(DEBUG) && 0
391 1.1 christos vtrace(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p);
392 1.1 christos #endif
393 1.1 christos
394 1.1 christos /* Update file. */
395 1.1 christos return append(sp, lno, p, len, LINE_APPEND, update);
396 1.1 christos }
397 1.1 christos
398 1.1 christos /*
399 1.1 christos * db_insert --
400 1.1 christos * Insert a line into the file.
401 1.1 christos *
402 1.1 christos * PUBLIC: int db_insert __P((SCR *, db_recno_t, CHAR_T *, size_t));
403 1.1 christos */
404 1.1 christos int
405 1.1 christos db_insert(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
406 1.1 christos {
407 1.1 christos #if defined(DEBUG) && 0
408 1.1 christos vtrace(sp, "insert before %lu: len %lu {%.*s}\n",
409 1.1 christos (u_long)lno, (u_long)len, MIN(len, 20), p);
410 1.1 christos #endif
411 1.1 christos return append(sp, lno - 1, p, len, LINE_INSERT, 1);
412 1.1 christos }
413 1.1 christos
414 1.1 christos /*
415 1.1 christos * db_set --
416 1.1 christos * Store a line in the file.
417 1.1 christos *
418 1.1 christos * PUBLIC: int db_set __P((SCR *, db_recno_t, CHAR_T *, size_t));
419 1.1 christos */
420 1.1 christos int
421 1.1 christos db_set(SCR *sp, db_recno_t lno, CHAR_T *p, size_t len)
422 1.1 christos {
423 1.1 christos DBT data, key;
424 1.1 christos EXF *ep;
425 1.5 christos const char *fp;
426 1.1 christos size_t flen;
427 1.1 christos
428 1.1 christos #if defined(DEBUG) && 0
429 1.1 christos vtrace(sp, "replace line %lu: len %lu {%.*s}\n",
430 1.1 christos (u_long)lno, (u_long)len, MIN(len, 20), p);
431 1.1 christos #endif
432 1.1 christos /* Check for no underlying file. */
433 1.1 christos if ((ep = sp->ep) == NULL) {
434 1.1 christos ex_emsg(sp, NULL, EXM_NOFILEYET);
435 1.1 christos return (1);
436 1.1 christos }
437 1.1 christos if (ep->l_win && ep->l_win != sp->wp) {
438 1.1 christos ex_emsg(sp, NULL, EXM_LOCKED);
439 1.1 christos return 1;
440 1.1 christos }
441 1.1 christos
442 1.1 christos /* Log before change. */
443 1.1 christos log_line(sp, lno, LOG_LINE_RESET_B);
444 1.1 christos
445 1.1 christos INT2FILE(sp, p, len, fp, flen);
446 1.1 christos
447 1.1 christos /* Update file. */
448 1.1 christos memset(&key, 0, sizeof(key));
449 1.1 christos key.data = &lno;
450 1.1 christos key.size = sizeof(lno);
451 1.1 christos memset(&data, 0, sizeof(data));
452 1.5 christos data.data = __UNCONST(fp);
453 1.1 christos data.size = flen;
454 1.1 christos if ((sp->db_error = ep->db->put(ep->db, NULL, &key, &data, 0)) != 0) {
455 1.1 christos msgq(sp, M_DBERR, "006|unable to store line %lu", (u_long)lno);
456 1.1 christos return (1);
457 1.1 christos }
458 1.1 christos
459 1.1 christos /* Flush the cache, update line count, before screen update. */
460 1.1 christos update_cache(sp, LINE_RESET, lno);
461 1.1 christos
462 1.1 christos /* File now dirty. */
463 1.1 christos if (F_ISSET(ep, F_FIRSTMODIFY))
464 1.1 christos (void)rcv_init(sp);
465 1.1 christos F_SET(ep, F_MODIFIED);
466 1.1 christos
467 1.1 christos /* Log after change. */
468 1.1 christos log_line(sp, lno, LOG_LINE_RESET_F);
469 1.1 christos
470 1.1 christos /* Update screen. */
471 1.1 christos return (scr_update(sp, lno, LINE_RESET, 1));
472 1.1 christos }
473 1.1 christos
474 1.1 christos /*
475 1.1 christos * db_exist --
476 1.1 christos * Return if a line exists.
477 1.1 christos *
478 1.1 christos * PUBLIC: int db_exist __P((SCR *, db_recno_t));
479 1.1 christos */
480 1.1 christos int
481 1.1 christos db_exist(SCR *sp, db_recno_t lno)
482 1.1 christos {
483 1.1 christos EXF *ep;
484 1.1 christos
485 1.1 christos /* Check for no underlying file. */
486 1.1 christos if ((ep = sp->ep) == NULL) {
487 1.1 christos ex_emsg(sp, NULL, EXM_NOFILEYET);
488 1.1 christos return (1);
489 1.1 christos }
490 1.1 christos
491 1.1 christos if (lno == OOBLNO)
492 1.1 christos return (0);
493 1.1 christos
494 1.1 christos /*
495 1.1 christos * Check the last-line number cache. Adjust the cached line
496 1.1 christos * number for the lines used by the text input buffers.
497 1.1 christos */
498 1.1 christos if (ep->c_nlines != OOBLNO)
499 1.1 christos return (lno <= (F_ISSET(sp, SC_TINPUT) ?
500 1.3 christos ep->c_nlines + TAILQ_LAST(&sp->tiq, _texth)->lno -
501 1.4 christos TAILQ_FIRST(&sp->tiq)->lno : ep->c_nlines));
502 1.1 christos
503 1.1 christos /* Go get the line. */
504 1.1 christos return (!db_get(sp, lno, 0, NULL, NULL));
505 1.1 christos }
506 1.1 christos
507 1.1 christos /*
508 1.1 christos * db_last --
509 1.1 christos * Return the number of lines in the file.
510 1.1 christos *
511 1.1 christos * PUBLIC: int db_last __P((SCR *, db_recno_t *));
512 1.1 christos */
513 1.1 christos int
514 1.1 christos db_last(SCR *sp, db_recno_t *lnop)
515 1.1 christos {
516 1.1 christos DBT data, key;
517 1.1 christos DBC *dbcp;
518 1.1 christos EXF *ep;
519 1.1 christos db_recno_t lno;
520 1.5 christos const CHAR_T *wp;
521 1.1 christos size_t wlen;
522 1.1 christos
523 1.1 christos /* Check for no underlying file. */
524 1.1 christos if ((ep = sp->ep) == NULL) {
525 1.1 christos ex_emsg(sp, NULL, EXM_NOFILEYET);
526 1.1 christos return (1);
527 1.1 christos }
528 1.1 christos
529 1.1 christos /*
530 1.1 christos * Check the last-line number cache. Adjust the cached line
531 1.1 christos * number for the lines used by the text input buffers.
532 1.1 christos */
533 1.1 christos if (ep->c_nlines != OOBLNO) {
534 1.1 christos *lnop = ep->c_nlines;
535 1.1 christos if (F_ISSET(sp, SC_TINPUT))
536 1.5 christos *lnop += TAILQ_LAST(&sp->tiq, _texth)->lno -
537 1.5 christos TAILQ_FIRST(&sp->tiq)->lno;
538 1.1 christos return (0);
539 1.1 christos }
540 1.1 christos
541 1.1 christos memset(&key, 0, sizeof(key));
542 1.1 christos key.data = &lno;
543 1.1 christos key.size = sizeof(lno);
544 1.1 christos memset(&data, 0, sizeof(data));
545 1.1 christos
546 1.1 christos if ((sp->db_error = ep->db->cursor(ep->db, NULL, &dbcp, 0)) != 0)
547 1.1 christos goto err1;
548 1.1 christos switch (sp->db_error = dbcp->c_get(dbcp, &key, &data, DB_LAST)) {
549 1.1 christos case DB_NOTFOUND:
550 1.1 christos *lnop = 0;
551 1.1 christos return (0);
552 1.1 christos default:
553 1.1 christos (void)dbcp->c_close(dbcp);
554 1.1 christos alloc_err:
555 1.1 christos err1:
556 1.1 christos msgq(sp, M_DBERR, "007|unable to get last line");
557 1.1 christos *lnop = 0;
558 1.1 christos return (1);
559 1.1 christos case 0:
560 1.1 christos ;
561 1.1 christos }
562 1.1 christos
563 1.1 christos memcpy(&lno, key.data, sizeof(lno));
564 1.1 christos
565 1.1 christos if (lno != sp->c_lno) {
566 1.1 christos FILE2INT(sp, data.data, data.size, wp, wlen);
567 1.1 christos
568 1.1 christos /* Fill the cache. */
569 1.1 christos BINC_GOTOW(sp, sp->c_lp, sp->c_blen, wlen);
570 1.1 christos MEMCPYW(sp->c_lp, wp, wlen);
571 1.1 christos sp->c_lno = lno;
572 1.1 christos sp->c_len = wlen;
573 1.1 christos }
574 1.1 christos ep->c_nlines = lno;
575 1.1 christos
576 1.1 christos (void)dbcp->c_close(dbcp);
577 1.1 christos
578 1.1 christos /* Return the value. */
579 1.1 christos *lnop = (F_ISSET(sp, SC_TINPUT) &&
580 1.3 christos TAILQ_LAST(&sp->tiq, _texth)->lno > lno ?
581 1.3 christos TAILQ_LAST(&sp->tiq, _texth)->lno : lno);
582 1.1 christos return (0);
583 1.1 christos }
584 1.1 christos
585 1.1 christos /*
586 1.1 christos * db_err --
587 1.1 christos * Report a line error.
588 1.1 christos *
589 1.1 christos * PUBLIC: void db_err __P((SCR *, db_recno_t));
590 1.1 christos */
591 1.1 christos void
592 1.1 christos db_err(SCR *sp, db_recno_t lno)
593 1.1 christos {
594 1.1 christos msgq(sp, M_ERR,
595 1.1 christos "008|Error: unable to retrieve line %lu", (u_long)lno);
596 1.1 christos }
597 1.1 christos
598 1.1 christos /*
599 1.1 christos * scr_update --
600 1.1 christos * Update all of the screens that are backed by the file that
601 1.1 christos * just changed.
602 1.1 christos *
603 1.1 christos * PUBLIC: int scr_update __P((SCR *sp, db_recno_t lno,
604 1.1 christos * PUBLIC: lnop_t op, int current));
605 1.1 christos */
606 1.1 christos int
607 1.1 christos scr_update(SCR *sp, db_recno_t lno, lnop_t op, int current)
608 1.1 christos {
609 1.1 christos EXF *ep;
610 1.1 christos SCR *tsp;
611 1.1 christos WIN *wp;
612 1.1 christos
613 1.1 christos if (F_ISSET(sp, SC_EX))
614 1.1 christos return (0);
615 1.1 christos
616 1.1 christos /* XXXX goes outside of window */
617 1.1 christos ep = sp->ep;
618 1.1 christos if (ep->refcnt != 1)
619 1.3 christos TAILQ_FOREACH(wp, &sp->gp->dq, q)
620 1.3 christos TAILQ_FOREACH(tsp, &wp->scrq, q)
621 1.3 christos if (sp != tsp && tsp->ep == ep)
622 1.3 christos if (vs_change(tsp, lno, op))
623 1.3 christos return (1);
624 1.1 christos return (current ? vs_change(sp, lno, op) : 0);
625 1.1 christos }
626 1.1 christos
627 1.1 christos /*
628 1.1 christos * PUBLIC: void update_cache __P((SCR *sp, lnop_t op, db_recno_t lno));
629 1.1 christos */
630 1.1 christos void
631 1.1 christos update_cache(SCR *sp, lnop_t op, db_recno_t lno)
632 1.1 christos {
633 1.1 christos SCR* scrp;
634 1.1 christos EXF *ep;
635 1.1 christos
636 1.1 christos ep = sp->ep;
637 1.1 christos
638 1.1 christos /* Flush the cache, update line count, before screen update. */
639 1.1 christos /* The flushing is probably not needed, since it was incorrect
640 1.1 christos * for db_insert. It might be better to adjust it, like
641 1.1 christos * marks, @ and global
642 1.1 christos */
643 1.3 christos TAILQ_FOREACH(scrp, &ep->scrq, eq)
644 1.1 christos switch (op) {
645 1.1 christos case LINE_INSERT:
646 1.1 christos case LINE_DELETE:
647 1.1 christos if (lno <= scrp->c_lno)
648 1.1 christos scrp->c_lno = OOBLNO;
649 1.1 christos break;
650 1.1 christos case LINE_RESET:
651 1.1 christos if (lno == scrp->c_lno)
652 1.1 christos scrp->c_lno = OOBLNO;
653 1.1 christos break;
654 1.5 christos case LINE_APPEND:
655 1.5 christos break;
656 1.1 christos }
657 1.1 christos
658 1.1 christos if (ep->c_nlines != OOBLNO)
659 1.1 christos switch (op) {
660 1.1 christos case LINE_INSERT:
661 1.1 christos ++ep->c_nlines;
662 1.1 christos break;
663 1.1 christos case LINE_DELETE:
664 1.1 christos --ep->c_nlines;
665 1.1 christos break;
666 1.5 christos case LINE_APPEND:
667 1.5 christos case LINE_RESET:
668 1.5 christos break;
669 1.1 christos }
670 1.1 christos }
671 1.1 christos
672 1.1 christos /*
673 1.1 christos * PUBLIC: int line_insdel __P((SCR *sp, lnop_t op, db_recno_t lno));
674 1.1 christos */
675 1.1 christos int
676 1.1 christos line_insdel(SCR *sp, lnop_t op, db_recno_t lno)
677 1.1 christos {
678 1.1 christos int rval;
679 1.1 christos
680 1.1 christos /* Update marks, @ and global commands. */
681 1.1 christos rval = 0;
682 1.1 christos if (mark_insdel(sp, op, lno))
683 1.1 christos rval = 1;
684 1.1 christos if (ex_g_insdel(sp, op, lno))
685 1.1 christos rval = 1;
686 1.1 christos
687 1.1 christos return rval;
688 1.1 christos }
689 1.1 christos
690 1.1 christos #ifdef USE_DB4_LOGGING
691 1.1 christos #define VI_DB_INIT_LOG DB_INIT_LOG
692 1.1 christos #else
693 1.1 christos #define VI_DB_INIT_LOG 0
694 1.1 christos #endif
695 1.1 christos
696 1.1 christos /*
697 1.1 christos * PUBLIC: int db_setup __P((SCR *, EXF *));
698 1.1 christos */
699 1.1 christos int
700 1.1 christos db_setup(SCR *sp, EXF *ep)
701 1.1 christos {
702 1.1 christos char path[MAXPATHLEN];
703 1.1 christos int fd;
704 1.1 christos DB_ENV *env;
705 1.1 christos
706 1.1 christos (void)snprintf(path, sizeof(path), "%s/vi.XXXXXX", O_STR(sp, O_RECDIR));
707 1.1 christos if ((fd = mkstemp(path)) == -1) {
708 1.1 christos msgq(sp, M_SYSERR, "%s", path);
709 1.1 christos goto err;
710 1.1 christos }
711 1.1 christos (void)close(fd);
712 1.1 christos (void)unlink(path);
713 1.1 christos if (mkdir(path, S_IRWXU)) {
714 1.1 christos msgq(sp, M_SYSERR, "%s", path);
715 1.1 christos goto err;
716 1.1 christos }
717 1.1 christos if (db_env_create(&env, 0)) {
718 1.1 christos msgq(sp, M_ERR, "env_create");
719 1.1 christos goto err;
720 1.1 christos }
721 1.1 christos #ifdef USE_DB4_LOGGING
722 1.1 christos if ((sp->db_error = vi_db_init_recover(env))) {
723 1.1 christos msgq(sp, M_DBERR, "init_recover");
724 1.1 christos goto err;
725 1.1 christos }
726 1.1 christos if ((sp->db_error = __vi_init_recover(env))) {
727 1.1 christos msgq(sp, M_DBERR, "init_recover");
728 1.1 christos goto err;
729 1.1 christos }
730 1.1 christos #endif
731 1.1 christos if ((sp->db_error = db_env_open(env, path,
732 1.1 christos DB_PRIVATE | DB_CREATE | DB_INIT_MPOOL | VI_DB_THREAD
733 1.1 christos | VI_DB_INIT_LOG, 0)) != 0) {
734 1.1 christos msgq(sp, M_DBERR, "env->open");
735 1.1 christos goto err;
736 1.1 christos }
737 1.1 christos
738 1.1 christos if ((ep->env_path = strdup(path)) == NULL) {
739 1.1 christos msgq(sp, M_SYSERR, NULL);
740 1.1 christos (void)rmdir(path);
741 1.1 christos goto err;
742 1.1 christos }
743 1.1 christos ep->env = env;
744 1.1 christos return 0;
745 1.1 christos err:
746 1.1 christos return 1;
747 1.1 christos }
748 1.1 christos
749 1.1 christos /* Round up v to the nearest power of 2 */
750 1.1 christos static size_t round_up(size_t v)
751 1.1 christos {
752 1.1 christos ssize_t old_v = v;
753 1.1 christos
754 1.1 christos while (v) {
755 1.1 christos old_v = v;
756 1.1 christos v ^= v & -v;
757 1.1 christos }
758 1.1 christos return old_v << 1;
759 1.1 christos }
760 1.1 christos
761 1.1 christos /*
762 1.2 christos * PUBLIC: int db_msg_open __P((SCR *, const char *, DB **));
763 1.1 christos */
764 1.2 christos int db_msg_open(SCR *sp, const char *file, DB **dbp)
765 1.1 christos {
766 1.1 christos return (sp->db_error = db_create(dbp, 0, 0)) != 0 ||
767 1.1 christos (sp->db_error = (*dbp)->set_re_source(*dbp, file)) != 0 ||
768 1.1 christos (sp->db_error = db_open(*dbp, NULL, DB_RECNO, 0, 0)) != 0;
769 1.1 christos }
770 1.1 christos
771 1.1 christos /*
772 1.1 christos * PUBLIC: int db_init __P((SCR *, EXF *, char *, char *, size_t, int *));
773 1.1 christos */
774 1.1 christos int
775 1.1 christos db_init(SCR *sp, EXF *ep, char *rcv_name, char *oname, size_t psize, int *open_err)
776 1.1 christos {
777 1.1 christos if (db_setup(sp, ep))
778 1.1 christos return 1;
779 1.1 christos
780 1.1 christos /* Open a db structure. */
781 1.1 christos if ((sp->db_error = db_create(&ep->db, 0, 0)) != 0) {
782 1.1 christos msgq(sp, M_DBERR, "db_create");
783 1.1 christos return 1;
784 1.1 christos }
785 1.1 christos
786 1.1 christos ep->db->set_re_delim(ep->db, '\n'); /* Always set. */
787 1.1 christos ep->db->set_pagesize(ep->db, round_up(psize));
788 1.1 christos ep->db->set_flags(ep->db, DB_RENUMBER | DB_SNAPSHOT);
789 1.1 christos if (rcv_name == NULL)
790 1.1 christos ep->db->set_re_source(ep->db, oname);
791 1.1 christos
792 1.1 christos /*
793 1.1 christos * Don't let db use mmap when using fcntl for locking
794 1.1 christos */
795 1.1 christos #ifdef HAVE_LOCK_FCNTL
796 1.1 christos #define NOMMAPIFFCNTL DB_NOMMAP
797 1.1 christos #else
798 1.1 christos #define NOMMAPIFFCNTL 0
799 1.1 christos #endif
800 1.1 christos
801 1.1 christos #define _DB_OPEN_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
802 1.1 christos
803 1.1 christos if ((sp->db_error = db_open(ep->db, ep->rcv_path, DB_RECNO,
804 1.1 christos ((rcv_name == 0) ? DB_TRUNCATE : 0) | VI_DB_THREAD | NOMMAPIFFCNTL,
805 1.1 christos _DB_OPEN_MODE)) != 0) {
806 1.1 christos msgq_str(sp,
807 1.1 christos M_DBERR, rcv_name == NULL ? oname : rcv_name, "%s");
808 1.1 christos /*
809 1.1 christos * !!!
810 1.1 christos * Historically, vi permitted users to edit files that couldn't
811 1.1 christos * be read. This isn't useful for single files from a command
812 1.1 christos * line, but it's quite useful for "vi *.c", since you can skip
813 1.1 christos * past files that you can't read.
814 1.1 christos */
815 1.1 christos ep->db = NULL; /* Don't close it; it wasn't opened */
816 1.1 christos
817 1.1 christos *open_err = 1;
818 1.1 christos return 1;
819 1.1 christos }
820 1.1 christos
821 1.1 christos /* re_source is loaded into the database.
822 1.1 christos * Close it and reopen it in the environment.
823 1.1 christos */
824 1.1 christos if ((sp->db_error = ep->db->close(ep->db, 0))) {
825 1.1 christos msgq(sp, M_DBERR, "close");
826 1.1 christos return 1;
827 1.1 christos }
828 1.1 christos if ((sp->db_error = db_create(&ep->db, ep->env, 0)) != 0) {
829 1.1 christos msgq(sp, M_DBERR, "db_create 2");
830 1.1 christos return 1;
831 1.1 christos }
832 1.1 christos if ((sp->db_error = db_open(ep->db, ep->rcv_path, DB_RECNO,
833 1.1 christos VI_DB_THREAD | NOMMAPIFFCNTL, _DB_OPEN_MODE)) != 0) {
834 1.1 christos msgq_str(sp,
835 1.1 christos M_DBERR, ep->rcv_path, "%s");
836 1.1 christos return 1;
837 1.1 christos }
838 1.1 christos
839 1.1 christos return 0;
840 1.1 christos }
841