PageRenderTime 396ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/nvi2/common/line.c

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