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