gap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /* gap.c, Ait, BSD 3-Clause, Kevin Bloom, 2023-2024,
  2. Derived from: Atto January 2017
  3. Derived from: Anthony's Editor January 93
  4. */
  5. #include <sys/stat.h>
  6. #include "header.h"
  7. #include "util.h"
  8. #include "config.h"
  9. /* Enlarge gap by n chars, position of gap cannot change */
  10. int growgap(buffer_t *bp, point_t n)
  11. {
  12. char_t *new;
  13. point_t buflen, newlen, xgap, xegap;
  14. assert(bp->b_buf <= bp->b_gap);
  15. assert(bp->b_gap <= bp->b_egap);
  16. assert(bp->b_egap <= bp->b_ebuf);
  17. xgap = bp->b_gap - bp->b_buf;
  18. xegap = bp->b_egap - bp->b_buf;
  19. buflen = bp->b_ebuf - bp->b_buf;
  20. /* reduce number of reallocs by growing by a minimum amount */
  21. n = (n < MIN_GAP_EXPAND ? MIN_GAP_EXPAND : n);
  22. newlen = buflen + n * sizeof (char_t);
  23. if (buflen == 0) {
  24. if (newlen < 0 || MAX_SIZE_T < newlen)
  25. fatal("%s: Failed to allocate required memory.\n");
  26. new = (char_t*) malloc((size_t) newlen);
  27. if (new == NULL)
  28. fatal("%s: Failed to allocate required memory.\n"); /* Cannot edit a file without a buffer. */
  29. } else {
  30. if (newlen < 0 || MAX_SIZE_T < newlen) {
  31. msg("Failed to allocate required memory");
  32. return (FALSE);
  33. }
  34. new = (char_t*) realloc(bp->b_buf, (size_t) newlen);
  35. if (new == NULL) {
  36. msg("Failed to allocate required memory"); /* Report non-fatal error. */
  37. return (FALSE);
  38. }
  39. }
  40. /* Relocate pointers in new buffer and append the new
  41. * extension to the end of the gap.
  42. */
  43. bp->b_buf = new;
  44. bp->b_gap = bp->b_buf + xgap;
  45. bp->b_ebuf = bp->b_buf + buflen;
  46. bp->b_egap = bp->b_buf + newlen;
  47. while (xegap < buflen--)
  48. *--bp->b_egap = *--bp->b_ebuf;
  49. bp->b_ebuf = bp->b_buf + newlen;
  50. assert(bp->b_buf < bp->b_ebuf); /* Buffer must exist. */
  51. assert(bp->b_buf <= bp->b_gap);
  52. assert(bp->b_gap < bp->b_egap); /* Gap must grow only. */
  53. assert(bp->b_egap <= bp->b_ebuf);
  54. return (TRUE);
  55. }
  56. point_t movegap(buffer_t *bp, point_t offset)
  57. {
  58. char_t *p = ptr(bp, offset);
  59. while (p < bp->b_gap)
  60. *--bp->b_egap = *--bp->b_gap;
  61. while (bp->b_egap < p)
  62. *bp->b_gap++ = *bp->b_egap++;
  63. assert(bp->b_gap <= bp->b_egap);
  64. assert(bp->b_buf <= bp->b_gap);
  65. assert(bp->b_egap <= bp->b_ebuf);
  66. return (pos(bp, bp->b_egap));
  67. }
  68. /* Given a buffer offset, convert it to a pointer into the buffer */
  69. char_t * ptr(buffer_t *bp, register point_t offset)
  70. {
  71. if (offset < 0)
  72. return (bp->b_buf);
  73. return (bp->b_buf+offset + (bp->b_buf + offset < bp->b_gap ? 0 : bp->b_egap-bp->b_gap));
  74. }
  75. /* Given a pointer into the buffer, convert it to a buffer offset */
  76. point_t pos(buffer_t *bp, register char_t *cp)
  77. {
  78. assert(bp->b_buf <= cp && cp <= bp->b_ebuf);
  79. return (cp - bp->b_buf - (cp < bp->b_egap ? 0 : bp->b_egap - bp->b_gap));
  80. }
  81. int posix_file(char *fn)
  82. {
  83. if (fn[0] == '_')
  84. return (FALSE);
  85. for (; *fn != '\0'; ++fn) {
  86. if (!isalnum(*fn) && *fn != '.' && *fn != '_' && *fn != '-' && *fn != '/')
  87. return (FALSE);
  88. }
  89. return (TRUE);
  90. }
  91. void relocate_backup(char *fp)
  92. {
  93. char ltemp[PATH_MAX+2];
  94. strcpy(ltemp, fp);
  95. replace_all(ltemp, '/', '!');
  96. strcpy(fp, BACKUP_DIR);
  97. strcat(fp, ltemp);
  98. }
  99. /*
  100. This code is heavily based on OpenBSD's mg(1)
  101. See mg's `fileio.c` file.
  102. */
  103. int backup_file(char *fn)
  104. {
  105. int orig, backup;
  106. char bfn[PATH_MAX+2];
  107. char *tname;
  108. char buf[STRBUF_L];
  109. struct stat sb;
  110. ssize_t nread;
  111. struct timespec new_times[2];
  112. sprintf(bfn, "%s~", fn);
  113. if(BACKUP_DIR != NULL)
  114. relocate_backup(bfn);
  115. /* If we can't open the original, just don't make a back up */
  116. if (stat(fn, &sb) == -1) {
  117. msg("Can't stat %s : %s", fn, strerror(errno));
  118. return TRUE;
  119. }
  120. if((orig = open(fn, O_RDONLY)) == -1) {
  121. return TRUE;
  122. }
  123. if (asprintf(&tname, "%s.XXXXXXXXXX", bfn) == -1) {
  124. msg("Can't allocate temp file name : %s", strerror(errno));
  125. return FALSE;
  126. }
  127. backup = mkstemp(tname);
  128. if(backup == -1) {
  129. msg("Failed to open backup file: \"%s\"", bfn);
  130. return FALSE;
  131. }
  132. while ((nread = read(orig, buf, sizeof(buf))) > 0) {
  133. if (write(backup, buf, (size_t)nread) != nread) {
  134. nread = -1;
  135. break;
  136. }
  137. }
  138. (void) fchmod(backup, (sb.st_mode & 0777));
  139. new_times[0] = sb.st_atim;
  140. new_times[1] = sb.st_mtim;
  141. futimens(backup, new_times);
  142. close(orig);
  143. close(backup);
  144. if (nread == -1) {
  145. if (unlink(tname) == -1)
  146. msg("Can't unlink temp : %s", strerror(errno));
  147. } else {
  148. if (rename(tname, bfn) == -1) {
  149. msg("Can't rename temp : %s", strerror(errno));
  150. (void) unlink(tname);
  151. nread = -1;
  152. }
  153. }
  154. return (nread == -1 ? FALSE : TRUE);
  155. }
  156. int save(char *fn)
  157. {
  158. FILE *fp;
  159. point_t length;
  160. struct stat sb;
  161. // if (!posix_file(fn)) {
  162. // msg("Not a portable POSIX file name.");
  163. // return (FALSE);
  164. // }
  165. stat(fn, &sb);
  166. if (!backup_file(fn)) {
  167. // msg("Failed to backup file \"%s\".", fn);
  168. return (FALSE);
  169. }
  170. fp = fopen(fn, "w");
  171. if (fp == NULL) {
  172. msg("Failed to open file \"%s\".", fn);
  173. return (FALSE);
  174. }
  175. (void) movegap(curbp, (point_t) 0);
  176. length = (point_t) (curbp->b_ebuf - curbp->b_egap);
  177. if (fwrite(curbp->b_egap, sizeof (char), (size_t) length, fp) != length) {
  178. msg("Failed to write file \"%s\".", fn);
  179. return (FALSE);
  180. }
  181. if (fclose(fp) != 0) {
  182. msg("Failed to close file \"%s\".", fn);
  183. return (FALSE);
  184. }
  185. curbp->b_flags &= ~B_MODIFIED;
  186. if (stat(fn, &sb) < 0) {
  187. msg("Failed to find file \"%s\".", fn);
  188. return (FALSE);
  189. }
  190. if (MAX_SIZE_T < sb.st_size) {
  191. msg("File \"%s\" is too big to load.", fn);
  192. return (FALSE);
  193. }
  194. curbp->b_fmtime = sb.st_mtime;
  195. msg("File \"%s\" %ld bytes saved.", fn, pos(curbp, curbp->b_ebuf));
  196. return (TRUE);
  197. }
  198. int load_file(char *fn)
  199. {
  200. /* reset the gap, make it the whole buffer */
  201. curbp->b_gap = curbp->b_buf;
  202. curbp->b_egap = curbp->b_ebuf;
  203. top();
  204. return insert_file(fn, FALSE);
  205. }
  206. /* reads file into buffer at point */
  207. int insert_file(char *fn, int modflag)
  208. {
  209. FILE *fp;
  210. size_t len;
  211. struct stat sb;
  212. if (stat(fn, &sb) < 0) {
  213. msg("Failed to find file \"%s\".", fn);
  214. return (FALSE);
  215. }
  216. if (MAX_SIZE_T < sb.st_size) {
  217. msg("File \"%s\" is too big to load.", fn);
  218. return (FALSE);
  219. }
  220. if (curbp->b_egap - curbp->b_gap < sb.st_size * sizeof (char_t) && !growgap(curbp, sb.st_size))
  221. return (FALSE);
  222. if ((fp = fopen(fn, "r")) == NULL) {
  223. msg("Failed to open file \"%s\".", fn);
  224. return (FALSE);
  225. }
  226. curbp->b_point = movegap(curbp, curbp->b_point);
  227. // undoset();
  228. curbp->b_gap += len = fread(curbp->b_gap, sizeof (char), (size_t) sb.st_size, fp);
  229. if (fclose(fp) != 0) {
  230. msg("Failed to close file \"%s\".", fn);
  231. return (FALSE);
  232. }
  233. curbp->b_flags &= (modflag ? B_MODIFIED : ~B_MODIFIED);
  234. if(!modflag)
  235. curbp->b_fmtime = sb.st_mtime;
  236. msg("%ld bytes read.", len);
  237. return (TRUE);
  238. }
  239. /* Record a new undo */
  240. void undoset(int type, int shouldconcat)
  241. {
  242. int length = strlen((const char *)input);
  243. int ulen = 0;
  244. int npc = 0;
  245. char_t *p;
  246. undo_t *u = (undo_t *)malloc(sizeof(undo_t));
  247. assert(u != NULL);
  248. u->u_type = type;
  249. u->u_point = curbp->b_point;
  250. u->u_line = curbp->b_line;
  251. u->u_adjust = FALSE;
  252. u->u_data = NULL;
  253. switch(type) {
  254. case INSERT:
  255. if(curbp->b_undo != NULL && curbp->b_undo->u_type == INSERT && shouldconcat) {
  256. ulen = strlen((const char *)curbp->b_undo->u_data);
  257. curbp->b_undo->u_data = (char_t *)(realloc(curbp->b_undo->u_data, sizeof(char_t)*(length + ulen)+1));
  258. strncpy((char *)curbp->b_undo->u_data, (char *)curbp->b_undo->u_data, ulen);
  259. strncat((char *)curbp->b_undo->u_data, (char *)input, length);
  260. curbp->b_undo->u_data[(length + ulen)] = '\0';
  261. return;
  262. } else {
  263. u->u_data = (char_t *)(malloc(sizeof(char_t)*length+1));
  264. strncpy((char *)u->u_data, (char *)input, length);
  265. u->u_data[length] = '\0';
  266. }
  267. break;
  268. case DELETE:
  269. npc = utf8_size(*ptr(curbp,curbp->b_point));
  270. if(curbp->b_undo != NULL && curbp->b_undo->u_type == DELETE && shouldconcat) {
  271. ulen = strlen((const char *)curbp->b_undo->u_data);
  272. curbp->b_undo->u_data = (char_t *)(realloc(curbp->b_undo->u_data, sizeof(char_t)*(npc + ulen)+1));
  273. strncpy((char *)curbp->b_undo->u_data, (char *)curbp->b_undo->u_data, ulen);
  274. strncat((char *)curbp->b_undo->u_data, (const char *)ptr(curbp, curbp->b_point), npc);
  275. curbp->b_undo->u_data[npc+ulen] = '\0';
  276. return;
  277. } else {
  278. u->u_data = (char_t *)(malloc(sizeof(char_t)+npc));
  279. strncpy((char *)u->u_data, (const char *)ptr(curbp, curbp->b_point), npc);
  280. u->u_data[npc] = '\0';
  281. }
  282. break;
  283. case BACKSP:
  284. npc = prev_utf8_char_size();
  285. if(curbp->b_undo != NULL && curbp->b_undo->u_type == BACKSP && shouldconcat) {
  286. char_t *temp;
  287. curbp->b_undo->u_point -= npc;
  288. ulen = strlen((const char *)curbp->b_undo->u_data);
  289. temp = (char_t *)(malloc(sizeof(char_t)*ulen));
  290. strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
  291. /* if(curbp->b_undo->u_data != NULL) {
  292. free(curbp->b_undo->u_data);
  293. curbp->b_undo->u_data = NULL;
  294. } */
  295. curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(npc + ulen)+1));
  296. // memset(curbp->b_undo->u_data, 0, 1+ulen);
  297. strncpy((char *)curbp->b_undo->u_data, (const char *)ptr(curbp, curbp->b_undo->u_point), npc);
  298. strncat((char *)curbp->b_undo->u_data, (char *)temp, ulen);
  299. curbp->b_undo->u_data[(npc + ulen)] = '\0';
  300. free(temp);
  301. return;
  302. } else {
  303. u->u_point = curbp->b_point - npc;
  304. u->u_data = (char_t *)(malloc(sizeof(char_t)+npc));
  305. strncpy((char *)u->u_data, (const char *)ptr(curbp, u->u_point), npc);
  306. u->u_data[npc] = '\0';
  307. u->u_adjust = TRUE;
  308. }
  309. break;
  310. case CUT: {
  311. int bigger_mark = FALSE;
  312. if (curbp->b_point < curbp->b_mark) {
  313. (void) movegap(curbp, curbp->b_point);
  314. p = ptr(curbp, curbp->b_point);
  315. length = curbp->b_mark - curbp->b_point;
  316. bigger_mark = TRUE;
  317. u->u_adjust = TRUE;
  318. } else {
  319. (void) movegap(curbp, curbp->b_mark);
  320. p = ptr(curbp, curbp->b_mark);
  321. length = curbp->b_point - curbp->b_mark;
  322. u->u_point = curbp->b_mark;
  323. }
  324. u->u_data = (char_t*) malloc(sizeof(char_t)*length+1);
  325. (void) memcpy(u->u_data, p, length * sizeof (char_t));
  326. u->u_data[length] = '\0';
  327. /* Only adjust u_adjust if it's not a delete command, otherwise
  328. the line count will be off.
  329. */
  330. if(currentcommand != KBD_DELETE_CHAR)
  331. for(int i = 0; u->u_data[i] != '\0'; i++) {
  332. if(u->u_data[i] == '\n') {
  333. u->u_adjust = !bigger_mark;
  334. break;
  335. }
  336. }
  337. break;
  338. }
  339. case YANK:
  340. u->u_data = (char_t*) malloc(sizeof(char_t)*nscrap+1);
  341. (void) memcpy(u->u_data, scrap, nscrap * sizeof (char_t));
  342. u->u_data[nscrap] = '\0';
  343. break;
  344. case REPLACE:
  345. (void) movegap(curbp, curbp->b_point);
  346. p = ptr(curbp, curbp->b_point);
  347. length = curbp->b_mark - curbp->b_point;
  348. u->u_data = (char_t*) malloc(sizeof(char_t)*length+1);
  349. (void) memcpy(u->u_data, p, length * sizeof (char_t));
  350. u->u_data[length] = '\0';
  351. u->u_size = shouldconcat;
  352. break;
  353. case CLIPBOARD: {
  354. int ntemp = strlen(gtemp);
  355. u->u_data = (char_t*) malloc(sizeof(char_t)*ntemp+1);
  356. (void) memcpy(u->u_data, gtemp, ntemp * sizeof (char_t));
  357. u->u_data[ntemp] = '\0';
  358. break;
  359. }
  360. }
  361. u->u_next = curbp->b_undo;
  362. curbp->b_undo = u;
  363. curbp->b_redo = NULL;
  364. }
  365. /* Record a new redo if you've just undid or a undo if you've just redid. */
  366. void redo_or_undo_set(undo_t *up, int datalen, int isundo)
  367. {
  368. undo_t *rp = (undo_t *)malloc(sizeof(undo_t));
  369. char_t *p;
  370. int newlines = 0;
  371. assert(rp != NULL);
  372. rp->u_point = up->u_point;
  373. rp->u_line = up->u_line;
  374. if(up->u_adjust)
  375. for(int i = 0 ; up->u_data[i] != '\0'; i++) {
  376. if(up->u_data[i] == '\n')
  377. newlines++;
  378. }
  379. switch(up->u_type) {
  380. case INSERT:
  381. rp->u_type = DELETE;
  382. break;
  383. case BACKSP:
  384. rp->u_adjust = TRUE;
  385. rp->u_line -= newlines;
  386. case DELETE:
  387. rp->u_type = INSERT;
  388. break;
  389. case CUT:
  390. rp->u_type = YANK;
  391. rp->u_line -= newlines;
  392. break;
  393. case CLIPBOARD:
  394. case YANK:
  395. rp->u_type = CUT;
  396. rp->u_line += newlines;
  397. break;
  398. case REPLACE:
  399. rp->u_type = REPLACE;
  400. (void) movegap(curbp, up->u_point);
  401. p = ptr(curbp, up->u_point);
  402. rp->u_data = (char_t*) malloc(sizeof(char_t)*up->u_size+1);
  403. (void) memcpy(rp->u_data, p, up->u_size * sizeof (char_t));
  404. rp->u_data[up->u_size] = '\0';
  405. rp->u_size = datalen;
  406. break;
  407. }
  408. /* if(rp != NULL && rp->u_data != NULL && rp->u_type != REPLACE) {
  409. free(rp->u_data);
  410. rp->u_data = NULL;
  411. } */
  412. if(rp->u_type != REPLACE) {
  413. rp->u_data = (char_t *) calloc(datalen+1, sizeof(char_t));
  414. (void) memcpy(rp->u_data, up->u_data, datalen * sizeof (char_t));
  415. rp->u_data[datalen] = '\0';
  416. }
  417. /* if an undo was done, save to redo */
  418. if(isundo) {
  419. rp->u_next = curbp->b_redo;
  420. curbp->b_redo = rp;
  421. } else {
  422. rp->u_next = curbp->b_undo;
  423. curbp->b_undo = rp;
  424. }
  425. }
  426. /* Undo */
  427. void undo_or_redo(buffer_t *bp, undo_t *up, int isundo)
  428. {
  429. int n = 0;
  430. currentcommand = KBD_UNDO;
  431. if(up == NULL) {
  432. if(isundo)
  433. msg("Nothing to undo!");
  434. else
  435. msg("Nothing to redo!");
  436. return;
  437. }
  438. bp->b_point = up->u_point;
  439. bp->b_line = up->u_line;
  440. n = strlen((const char *)up->u_data);
  441. redo_or_undo_set(up, n, isundo);
  442. switch(up->u_type) {
  443. case INSERT:
  444. case YANK:
  445. case CLIPBOARD:
  446. (void) movegap(bp, bp->b_point);
  447. bp->b_egap += n;
  448. bp->b_point = pos(bp, bp->b_egap);
  449. break;
  450. case BACKSP:
  451. case DELETE:
  452. case CUT:
  453. bp->b_point = movegap(bp, bp->b_point);
  454. memcpy(bp->b_gap, up->u_data, n*sizeof(char_t));
  455. bp->b_gap += n;
  456. bp->b_point = pos(bp, bp->b_egap);
  457. break;
  458. case REPLACE:
  459. (void) movegap(bp, bp->b_point);
  460. bp->b_egap += up->u_size;
  461. bp->b_point = pos(bp, bp->b_egap);
  462. bp->b_point = movegap(bp, bp->b_point);
  463. memcpy(bp->b_gap, up->u_data, n*sizeof(char_t));
  464. bp->b_gap += n;
  465. bp->b_point = pos(bp, bp->b_egap);
  466. break;
  467. }
  468. if(up->u_adjust && isundo)
  469. bp->b_point = movegap(bp, up->u_point + n);
  470. else
  471. bp->b_point = movegap(bp, up->u_point);
  472. bp->b_flags |= B_MODIFIED;
  473. if(isundo)
  474. bp->b_undo = up->u_next;
  475. else
  476. bp->b_redo = up->u_next;
  477. if(curbp->b_point < curbp->b_page || curbp->b_point > curbp->b_epage)
  478. bp->b_reframe = 1;
  479. }
  480. void undo()
  481. {
  482. undo_or_redo(curbp, curbp->b_undo, TRUE);
  483. }
  484. /* Redo */
  485. void redo()
  486. {
  487. undo_or_redo(curbp, curbp->b_redo, FALSE);
  488. }
  489. /* find the point for start of line ln */
  490. point_t line_to_point(int ln)
  491. {
  492. point_t end_p = pos(curbp, curbp->b_ebuf);
  493. point_t p, start;
  494. for (p=0, start=0; p <= end_p; p++) {
  495. char_t *c = ptr(curbp, p);
  496. if(c == 0)
  497. break;
  498. if ( *c == '\n') {
  499. if (--ln == 0)
  500. return start;
  501. if (p + 1 <= end_p)
  502. start = p + 1;
  503. }
  504. if(!*c && ln == 1)
  505. return start;
  506. }
  507. return -1;
  508. }
  509. /* scan buffer and fill in curline and lastline */
  510. void get_line_stats(int *curline, int *lastline, buffer_t *bp)
  511. {
  512. point_t end_p = pos(bp, bp->b_ebuf);
  513. point_t p;
  514. int line;
  515. *curline = -1;
  516. for (p=0, line=0; p < end_p; p++) {
  517. line += (*(ptr(bp,p)) == '\n') ? 1 : 0;
  518. *lastline = line;
  519. if (*curline == -1 && p == bp->b_point) {
  520. *curline = (*(ptr(bp,p)) == '\n') ? line : line + 1;
  521. }
  522. }
  523. *lastline = *lastline + 1;
  524. if (bp->b_point == end_p)
  525. *curline = *lastline;
  526. }
  527. /* Return TRUE if file was modified after the current buffer's recored mtime. */
  528. int is_file_modified(char *fn)
  529. {
  530. struct stat sb;
  531. if(stat(fn, &sb) < 0) {
  532. return (FALSE);
  533. }
  534. if(sb.st_mtime != 0 && curbp->b_fmtime != sb.st_mtime) {
  535. return (TRUE);
  536. }
  537. return (FALSE);
  538. }
  539. /* Return TRUE to continue, FALSE to stop. Revert happens here. */
  540. int file_was_modified_prompt()
  541. {
  542. int current = 0, lastln = 0;
  543. const char *prompt = "This file has changed on disk; really edit (y/N/r) ?";
  544. char c = '\0';
  545. point_t p = curbp->b_point;
  546. struct stat sb;
  547. print_to_msgline(prompt);
  548. clrtoeol(prompt, MSGLINE);
  549. c = yesnomaybeso('n');
  550. if (c == 'n') {
  551. clrtoeol("", MSGLINE);
  552. return FALSE;
  553. } else if(c == 'r') {
  554. curbp->b_point = 0;
  555. load_file(curbp->b_fname);
  556. clrtoeol("", MSGLINE);
  557. curbp->b_point = p;
  558. get_line_stats(&current, &lastln, curbp);
  559. curbp->b_line = current;
  560. msg("Buffer reverted.");
  561. return FALSE;
  562. } else if(c == 'y') {
  563. clrtoeol("", MSGLINE);
  564. if (stat(curbp->b_fname, &sb) < 0) {
  565. msg("Failed to find file \"%s\".", curbp->b_fname);
  566. return (FALSE);
  567. }
  568. if (MAX_SIZE_T < sb.st_size) {
  569. msg("File \"%s\" is too big to load.", curbp->b_fname);
  570. return (FALSE);
  571. }
  572. curbp->b_fmtime = sb.st_mtime;
  573. return TRUE;
  574. }
  575. clrtoeol("", MSGLINE);
  576. return (FALSE);
  577. }