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