1 /* $OpenBSD: line.c,v 1.8 2007/05/14 12:32:29 pyr Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1992, 1993, 1994, 1995, 1996 7 * Keith Bostic. All rights reserved. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12 #include "config.h" 13 14 #ifndef lint 15 static const char sccsid[] = "@(#)line.c 10.21 (Berkeley) 9/15/96"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <sys/queue.h> 20 #include <sys/time.h> 21 22 #include <bitstring.h> 23 #include <errno.h> 24 #include <limits.h> 25 #include <stdio.h> 26 #include <string.h> 27 28 #include "common.h" 29 #include "../vi/vi.h" 30 31 static int scr_update(SCR *, recno_t, lnop_t, int); 32 33 /* 34 * db_eget -- 35 * Front-end to db_get, special case handling for empty files. 36 * 37 * PUBLIC: int db_eget(SCR *, recno_t, char **, size_t *, int *); 38 */ 39 int 40 db_eget(sp, lno, pp, lenp, isemptyp) 41 SCR *sp; 42 recno_t lno; /* Line number. */ 43 char **pp; /* Pointer store. */ 44 size_t *lenp; /* Length store. */ 45 int *isemptyp; 46 { 47 recno_t l1; 48 49 if (isemptyp != NULL) 50 *isemptyp = 0; 51 52 /* If the line exists, simply return it. */ 53 if (!db_get(sp, lno, 0, pp, lenp)) 54 return (0); 55 56 /* 57 * If the user asked for line 0 or line 1, i.e. the only possible 58 * line in an empty file, find the last line of the file; db_last 59 * fails loudly. 60 */ 61 if ((lno == 0 || lno == 1) && db_last(sp, &l1)) 62 return (1); 63 64 /* If the file isn't empty, fail loudly. */ 65 if ((lno != 0 && lno != 1) || l1 != 0) { 66 db_err(sp, lno); 67 return (1); 68 } 69 70 if (isemptyp != NULL) 71 *isemptyp = 1; 72 73 return (1); 74 } 75 76 /* 77 * db_get -- 78 * Look in the text buffers for a line, followed by the cache, followed 79 * by the database. 80 * 81 * PUBLIC: int db_get(SCR *, recno_t, u_int32_t, char **, size_t *); 82 */ 83 int 84 db_get(sp, lno, flags, pp, lenp) 85 SCR *sp; 86 recno_t lno; /* Line number. */ 87 u_int32_t flags; 88 char **pp; /* Pointer store. */ 89 size_t *lenp; /* Length store. */ 90 { 91 DBT data, key; 92 EXF *ep; 93 TEXT *tp; 94 recno_t l1, l2; 95 96 /* 97 * The underlying recno stuff handles zero by returning NULL, but 98 * have to have an OOB condition for the look-aside into the input 99 * buffer anyway. 100 */ 101 if (lno == 0) 102 goto err1; 103 104 /* Check for no underlying file. */ 105 if ((ep = sp->ep) == NULL) { 106 ex_emsg(sp, NULL, EXM_NOFILEYET); 107 goto err3; 108 } 109 110 if (LF_ISSET(DBG_NOCACHE)) 111 goto nocache; 112 113 /* 114 * Look-aside into the TEXT buffers and see if the line we want 115 * is there. 116 */ 117 if (F_ISSET(sp, SC_TINPUT)) { 118 l1 = ((TEXT *)CIRCLEQ_FIRST(&sp->tiq))->lno; 119 l2 = ((TEXT *)CIRCLEQ_LAST(&sp->tiq))->lno; 120 if (l1 <= lno && l2 >= lno) { 121 #if defined(DEBUG) && 0 122 TRACE(sp, "retrieve TEXT buffer line %lu\n", (u_long)lno); 123 #endif 124 for (tp = CIRCLEQ_FIRST(&sp->tiq); 125 tp->lno != lno; tp = CIRCLEQ_NEXT(tp, q)); 126 if (lenp != NULL) 127 *lenp = tp->len; 128 if (pp != NULL) 129 *pp = tp->lb; 130 return (0); 131 } 132 /* 133 * Adjust the line number for the number of lines used 134 * by the text input buffers. 135 */ 136 if (lno > l2) 137 lno -= l2 - l1; 138 } 139 140 /* Look-aside into the cache, and see if the line we want is there. */ 141 if (lno == ep->c_lno) { 142 #if defined(DEBUG) && 0 143 TRACE(sp, "retrieve cached line %lu\n", (u_long)lno); 144 #endif 145 if (lenp != NULL) 146 *lenp = ep->c_len; 147 if (pp != NULL) 148 *pp = ep->c_lp; 149 return (0); 150 } 151 ep->c_lno = OOBLNO; 152 153 nocache: 154 /* Get the line from the underlying database. */ 155 key.data = &lno; 156 key.size = sizeof(lno); 157 switch (ep->db->get(ep->db, &key, &data, 0)) { 158 case -1: 159 goto err2; 160 case 1: 161 err1: if (LF_ISSET(DBG_FATAL)) 162 err2: db_err(sp, lno); 163 err3: if (lenp != NULL) 164 *lenp = 0; 165 if (pp != NULL) 166 *pp = NULL; 167 return (1); 168 } 169 170 /* Reset the cache. */ 171 ep->c_lno = lno; 172 ep->c_len = data.size; 173 ep->c_lp = data.data; 174 175 #if defined(DEBUG) && 0 176 TRACE(sp, "retrieve DB line %lu\n", (u_long)lno); 177 #endif 178 if (lenp != NULL) 179 *lenp = data.size; 180 if (pp != NULL) 181 *pp = ep->c_lp; 182 return (0); 183 } 184 185 /* 186 * db_delete -- 187 * Delete a line from the file. 188 * 189 * PUBLIC: int db_delete(SCR *, recno_t); 190 */ 191 int 192 db_delete(sp, lno) 193 SCR *sp; 194 recno_t lno; 195 { 196 DBT key; 197 EXF *ep; 198 199 #if defined(DEBUG) && 0 200 TRACE(sp, "delete line %lu\n", (u_long)lno); 201 #endif 202 /* Check for no underlying file. */ 203 if ((ep = sp->ep) == NULL) { 204 ex_emsg(sp, NULL, EXM_NOFILEYET); 205 return (1); 206 } 207 208 /* Update marks, @ and global commands. */ 209 if (mark_insdel(sp, LINE_DELETE, lno)) 210 return (1); 211 if (ex_g_insdel(sp, LINE_DELETE, lno)) 212 return (1); 213 214 /* Log change. */ 215 log_line(sp, lno, LOG_LINE_DELETE); 216 217 /* Update file. */ 218 key.data = &lno; 219 key.size = sizeof(lno); 220 SIGBLOCK; 221 if (ep->db->del(ep->db, &key, 0) == 1) { 222 msgq(sp, M_SYSERR, 223 "003|unable to delete line %lu", (u_long)lno); 224 return (1); 225 } 226 SIGUNBLOCK; 227 228 /* Flush the cache, update line count, before screen update. */ 229 if (lno <= ep->c_lno) 230 ep->c_lno = OOBLNO; 231 if (ep->c_nlines != OOBLNO) 232 --ep->c_nlines; 233 234 /* File now modified. */ 235 if (F_ISSET(ep, F_FIRSTMODIFY)) 236 (void)rcv_init(sp); 237 F_SET(ep, F_MODIFIED); 238 239 /* Update screen. */ 240 return (scr_update(sp, lno, LINE_DELETE, 1)); 241 } 242 243 /* 244 * db_append -- 245 * Append a line into the file. 246 * 247 * PUBLIC: int db_append(SCR *, int, recno_t, char *, size_t); 248 */ 249 int 250 db_append(sp, update, lno, p, len) 251 SCR *sp; 252 int update; 253 recno_t lno; 254 char *p; 255 size_t len; 256 { 257 DBT data, key; 258 EXF *ep; 259 int rval; 260 261 #if defined(DEBUG) && 0 262 TRACE(sp, "append to %lu: len %u {%.*s}\n", lno, len, MIN(len, 20), p); 263 #endif 264 /* Check for no underlying file. */ 265 if ((ep = sp->ep) == NULL) { 266 ex_emsg(sp, NULL, EXM_NOFILEYET); 267 return (1); 268 } 269 270 /* Update file. */ 271 key.data = &lno; 272 key.size = sizeof(lno); 273 data.data = p; 274 data.size = len; 275 SIGBLOCK; 276 if (ep->db->put(ep->db, &key, &data, R_IAFTER) == -1) { 277 msgq(sp, M_SYSERR, 278 "004|unable to append to line %lu", (u_long)lno); 279 return (1); 280 } 281 SIGUNBLOCK; 282 283 /* Flush the cache, update line count, before screen update. */ 284 if (lno < ep->c_lno) 285 ep->c_lno = OOBLNO; 286 if (ep->c_nlines != OOBLNO) 287 ++ep->c_nlines; 288 289 /* File now dirty. */ 290 if (F_ISSET(ep, F_FIRSTMODIFY)) 291 (void)rcv_init(sp); 292 F_SET(ep, F_MODIFIED); 293 294 /* Log change. */ 295 log_line(sp, lno + 1, LOG_LINE_APPEND); 296 297 /* Update marks, @ and global commands. */ 298 rval = 0; 299 if (mark_insdel(sp, LINE_INSERT, lno + 1)) 300 rval = 1; 301 if (ex_g_insdel(sp, LINE_INSERT, lno + 1)) 302 rval = 1; 303 304 /* 305 * Update screen. 306 * 307 * XXX 308 * Nasty hack. If multiple lines are input by the user, they aren't 309 * committed until an <ESC> is entered. The problem is the screen was 310 * updated/scrolled as each line was entered. So, when this routine 311 * is called to copy the new lines from the cut buffer into the file, 312 * it has to know not to update the screen again. 313 */ 314 return (scr_update(sp, lno, LINE_APPEND, update) || rval); 315 } 316 317 /* 318 * db_insert -- 319 * Insert a line into the file. 320 * 321 * PUBLIC: int db_insert(SCR *, recno_t, char *, size_t); 322 */ 323 int 324 db_insert(sp, lno, p, len) 325 SCR *sp; 326 recno_t lno; 327 char *p; 328 size_t len; 329 { 330 DBT data, key; 331 EXF *ep; 332 int rval; 333 334 #if defined(DEBUG) && 0 335 TRACE(sp, "insert before %lu: len %lu {%.*s}\n", 336 (u_long)lno, (u_long)len, MIN(len, 20), p); 337 #endif 338 /* Check for no underlying file. */ 339 if ((ep = sp->ep) == NULL) { 340 ex_emsg(sp, NULL, EXM_NOFILEYET); 341 return (1); 342 } 343 344 /* Update file. */ 345 key.data = &lno; 346 key.size = sizeof(lno); 347 data.data = p; 348 data.size = len; 349 SIGBLOCK; 350 if (ep->db->put(ep->db, &key, &data, R_IBEFORE) == -1) { 351 msgq(sp, M_SYSERR, 352 "005|unable to insert at line %lu", (u_long)lno); 353 return (1); 354 } 355 SIGUNBLOCK; 356 357 /* Flush the cache, update line count, before screen update. */ 358 if (lno >= ep->c_lno) 359 ep->c_lno = OOBLNO; 360 if (ep->c_nlines != OOBLNO) 361 ++ep->c_nlines; 362 363 /* File now dirty. */ 364 if (F_ISSET(ep, F_FIRSTMODIFY)) 365 (void)rcv_init(sp); 366 F_SET(ep, F_MODIFIED); 367 368 /* Log change. */ 369 log_line(sp, lno, LOG_LINE_INSERT); 370 371 /* Update marks, @ and global commands. */ 372 rval = 0; 373 if (mark_insdel(sp, LINE_INSERT, lno)) 374 rval = 1; 375 if (ex_g_insdel(sp, LINE_INSERT, lno)) 376 rval = 1; 377 378 /* Update screen. */ 379 return (scr_update(sp, lno, LINE_INSERT, 1) || rval); 380 } 381 382 /* 383 * db_set -- 384 * Store a line in the file. 385 * 386 * PUBLIC: int db_set(SCR *, recno_t, char *, size_t); 387 */ 388 int 389 db_set(sp, lno, p, len) 390 SCR *sp; 391 recno_t lno; 392 char *p; 393 size_t len; 394 { 395 DBT data, key; 396 EXF *ep; 397 398 #if defined(DEBUG) && 0 399 TRACE(sp, "replace line %lu: len %lu {%.*s}\n", 400 (u_long)lno, (u_long)len, MIN(len, 20), p); 401 #endif 402 403 /* Check for no underlying file. */ 404 if ((ep = sp->ep) == NULL) { 405 ex_emsg(sp, NULL, EXM_NOFILEYET); 406 return (1); 407 } 408 409 /* Log before change. */ 410 log_line(sp, lno, LOG_LINE_RESET_B); 411 412 /* Update file. */ 413 key.data = &lno; 414 key.size = sizeof(lno); 415 data.data = p; 416 data.size = len; 417 SIGBLOCK; 418 if (ep->db->put(ep->db, &key, &data, 0) == -1) { 419 msgq(sp, M_SYSERR, 420 "006|unable to store line %lu", (u_long)lno); 421 return (1); 422 } 423 SIGUNBLOCK; 424 425 /* Flush the cache, before logging or screen update. */ 426 if (lno == ep->c_lno) 427 ep->c_lno = OOBLNO; 428 429 /* File now dirty. */ 430 if (F_ISSET(ep, F_FIRSTMODIFY)) 431 (void)rcv_init(sp); 432 F_SET(ep, F_MODIFIED); 433 434 /* Log after change. */ 435 log_line(sp, lno, LOG_LINE_RESET_F); 436 437 /* Update screen. */ 438 return (scr_update(sp, lno, LINE_RESET, 1)); 439 } 440 441 /* 442 * db_exist -- 443 * Return if a line exists. 444 * 445 * PUBLIC: int db_exist(SCR *, recno_t); 446 */ 447 int 448 db_exist(sp, lno) 449 SCR *sp; 450 recno_t lno; 451 { 452 EXF *ep; 453 454 /* Check for no underlying file. */ 455 if ((ep = sp->ep) == NULL) { 456 ex_emsg(sp, NULL, EXM_NOFILEYET); 457 return (1); 458 } 459 460 if (lno == OOBLNO) 461 return (0); 462 463 /* 464 * Check the last-line number cache. Adjust the cached line 465 * number for the lines used by the text input buffers. 466 */ 467 if (ep->c_nlines != OOBLNO) 468 return (lno <= (F_ISSET(sp, SC_TINPUT) ? 469 ep->c_nlines + (((TEXT *)CIRCLEQ_LAST(&sp->tiq))->lno - 470 ((TEXT *)CIRCLEQ_FIRST(&sp->tiq))->lno) : ep->c_nlines)); 471 472 /* Go get the line. */ 473 return (!db_get(sp, lno, 0, NULL, NULL)); 474 } 475 476 /* 477 * db_last -- 478 * Return the number of lines in the file. 479 * 480 * PUBLIC: int db_last(SCR *, recno_t *); 481 */ 482 int 483 db_last(sp, lnop) 484 SCR *sp; 485 recno_t *lnop; 486 { 487 DBT data, key; 488 EXF *ep; 489 recno_t lno; 490 491 /* Check for no underlying file. */ 492 if ((ep = sp->ep) == NULL) { 493 ex_emsg(sp, NULL, EXM_NOFILEYET); 494 return (1); 495 } 496 497 /* 498 * Check the last-line number cache. Adjust the cached line 499 * number for the lines used by the text input buffers. 500 */ 501 if (ep->c_nlines != OOBLNO) { 502 *lnop = ep->c_nlines; 503 if (F_ISSET(sp, SC_TINPUT)) 504 *lnop += ((TEXT *)CIRCLEQ_LAST(&sp->tiq))->lno - 505 ((TEXT *)CIRCLEQ_FIRST(&sp->tiq))->lno; 506 return (0); 507 } 508 509 key.data = &lno; 510 key.size = sizeof(lno); 511 512 switch (ep->db->seq(ep->db, &key, &data, R_LAST)) { 513 case -1: 514 msgq(sp, M_SYSERR, "007|unable to get last line"); 515 *lnop = 0; 516 return (1); 517 case 1: 518 *lnop = 0; 519 return (0); 520 default: 521 break; 522 } 523 524 /* Fill the cache. */ 525 memcpy(&lno, key.data, sizeof(lno)); 526 ep->c_nlines = ep->c_lno = lno; 527 ep->c_len = data.size; 528 ep->c_lp = data.data; 529 530 /* Return the value. */ 531 *lnop = (F_ISSET(sp, SC_TINPUT) && 532 ((TEXT *)CIRCLEQ_LAST(&sp->tiq))->lno > lno ? 533 ((TEXT *)CIRCLEQ_LAST(&sp->tiq))->lno : lno); 534 return (0); 535 } 536 537 /* 538 * db_err -- 539 * Report a line error. 540 * 541 * PUBLIC: void db_err(SCR *, recno_t); 542 */ 543 void 544 db_err(sp, lno) 545 SCR *sp; 546 recno_t lno; 547 { 548 msgq(sp, M_ERR, 549 "008|Error: unable to retrieve line %lu", (u_long)lno); 550 } 551 552 /* 553 * scr_update -- 554 * Update all of the screens that are backed by the file that 555 * just changed. 556 */ 557 static int 558 scr_update(sp, lno, op, current) 559 SCR *sp; 560 recno_t lno; 561 lnop_t op; 562 int current; 563 { 564 EXF *ep; 565 SCR *tsp; 566 567 if (F_ISSET(sp, SC_EX)) 568 return (0); 569 570 ep = sp->ep; 571 if (ep->refcnt != 1) 572 CIRCLEQ_FOREACH(tsp, &sp->gp->dq, q) 573 if (sp != tsp && tsp->ep == ep) 574 if (vs_change(tsp, lno, op)) 575 return (1); 576 return (current ? vs_change(sp, lno, op) : 0); 577 } 578