gap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* gap.c, Ait, BSD 3-Clause, Kevin Bloom, 2023,
  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. /* Enlarge gap by n chars, position of gap cannot change */
  9. int growgap(buffer_t *bp, point_t n)
  10. {
  11. char_t *new;
  12. point_t buflen, newlen, xgap, xegap;
  13. assert(bp->b_buf <= bp->b_gap);
  14. assert(bp->b_gap <= bp->b_egap);
  15. assert(bp->b_egap <= bp->b_ebuf);
  16. xgap = bp->b_gap - bp->b_buf;
  17. xegap = bp->b_egap - bp->b_buf;
  18. buflen = bp->b_ebuf - bp->b_buf;
  19. /* reduce number of reallocs by growing by a minimum amount */
  20. n = (n < MIN_GAP_EXPAND ? MIN_GAP_EXPAND : n);
  21. newlen = buflen + n * sizeof (char_t);
  22. if (buflen == 0) {
  23. if (newlen < 0 || MAX_SIZE_T < newlen)
  24. fatal("%s: Failed to allocate required memory.\n");
  25. new = (char_t*) malloc((size_t) newlen);
  26. if (new == NULL)
  27. fatal("%s: Failed to allocate required memory.\n"); /* Cannot edit a file without a buffer. */
  28. } else {
  29. if (newlen < 0 || MAX_SIZE_T < newlen) {
  30. msg("Failed to allocate required memory");
  31. return (FALSE);
  32. }
  33. new = (char_t*) realloc(bp->b_buf, (size_t) newlen);
  34. if (new == NULL) {
  35. msg("Failed to allocate required memory"); /* Report non-fatal error. */
  36. return (FALSE);
  37. }
  38. }
  39. /* Relocate pointers in new buffer and append the new
  40. * extension to the end of the gap.
  41. */
  42. bp->b_buf = new;
  43. bp->b_gap = bp->b_buf + xgap;
  44. bp->b_ebuf = bp->b_buf + buflen;
  45. bp->b_egap = bp->b_buf + newlen;
  46. while (xegap < buflen--)
  47. *--bp->b_egap = *--bp->b_ebuf;
  48. bp->b_ebuf = bp->b_buf + newlen;
  49. assert(bp->b_buf < bp->b_ebuf); /* Buffer must exist. */
  50. assert(bp->b_buf <= bp->b_gap);
  51. assert(bp->b_gap < bp->b_egap); /* Gap must grow only. */
  52. assert(bp->b_egap <= bp->b_ebuf);
  53. return (TRUE);
  54. }
  55. point_t movegap(buffer_t *bp, point_t offset)
  56. {
  57. char_t *p = ptr(bp, offset);
  58. while (p < bp->b_gap)
  59. *--bp->b_egap = *--bp->b_gap;
  60. while (bp->b_egap < p)
  61. *bp->b_gap++ = *bp->b_egap++;
  62. assert(bp->b_gap <= bp->b_egap);
  63. assert(bp->b_buf <= bp->b_gap);
  64. assert(bp->b_egap <= bp->b_ebuf);
  65. return (pos(bp, bp->b_egap));
  66. }
  67. /* Given a buffer offset, convert it to a pointer into the buffer */
  68. char_t * ptr(buffer_t *bp, register point_t offset)
  69. {
  70. if (offset < 0)
  71. return (bp->b_buf);
  72. return (bp->b_buf+offset + (bp->b_buf + offset < bp->b_gap ? 0 : bp->b_egap-bp->b_gap));
  73. }
  74. /* Given a pointer into the buffer, convert it to a buffer offset */
  75. point_t pos(buffer_t *bp, register char_t *cp)
  76. {
  77. assert(bp->b_buf <= cp && cp <= bp->b_ebuf);
  78. return (cp - bp->b_buf - (cp < bp->b_egap ? 0 : bp->b_egap - bp->b_gap));
  79. }
  80. int posix_file(char *fn)
  81. {
  82. if (fn[0] == '_')
  83. return (FALSE);
  84. for (; *fn != '\0'; ++fn) {
  85. if (!isalnum(*fn) && *fn != '.' && *fn != '_' && *fn != '-' && *fn != '/')
  86. return (FALSE);
  87. }
  88. return (TRUE);
  89. }
  90. void relocate_backup(char *fp)
  91. {
  92. char ltemp[NAME_MAX+2];
  93. strcpy(ltemp, fp);
  94. replace_all(ltemp, '/', '!');
  95. strcpy(fp, BACKUP_DIR);
  96. strcat(fp, ltemp);
  97. }
  98. /*
  99. TODO: make the location of backup files an option. Either put them in the dir
  100. of the file or in `backup_dir`.
  101. */
  102. int backup_file(char *fn)
  103. {
  104. FILE *orig, *backup;
  105. char bfn[NAME_MAX+2];
  106. char ch;
  107. strcpy(bfn, fn);
  108. if(BACKUP_DIR != NULL)
  109. relocate_backup(bfn);
  110. strcat(bfn, "~");
  111. // printf("%s", bfn);
  112. orig = fopen(fn, "r");
  113. if(orig == NULL) {
  114. msg("Failed to open original file \"%s\".", fn);
  115. return -1;
  116. }
  117. backup = fopen(bfn, "w");
  118. if(backup == NULL) {
  119. msg("Failed to open backup file.");
  120. return -1;
  121. }
  122. while((ch = fgetc(orig)) != EOF)
  123. fputc(ch, backup);
  124. fclose(orig);
  125. fclose(backup);
  126. return 1;
  127. }
  128. int save(char *fn)
  129. {
  130. FILE *fp;
  131. point_t length;
  132. struct stat sb;
  133. // if (!posix_file(fn)) {
  134. // msg("Not a portable POSIX file name.");
  135. // return (FALSE);
  136. // }
  137. stat(fn, &sb);
  138. if (!backup_file(fn)) {
  139. msg("Failed to backup file \"%s\".", fn);
  140. return (FALSE);
  141. }
  142. fp = fopen(fn, "w");
  143. if (fp == NULL) {
  144. msg("Failed to open file \"%s\".", fn);
  145. return (FALSE);
  146. }
  147. (void) movegap(curbp, (point_t) 0);
  148. length = (point_t) (curbp->b_ebuf - curbp->b_egap);
  149. if (fwrite(curbp->b_egap, sizeof (char), (size_t) length, fp) != length) {
  150. msg("Failed to write file \"%s\".", fn);
  151. return (FALSE);
  152. }
  153. if (fclose(fp) != 0) {
  154. msg("Failed to close file \"%s\".", fn);
  155. return (FALSE);
  156. }
  157. curbp->b_flags &= ~B_MODIFIED;
  158. if (stat(fn, &sb) < 0) {
  159. msg("Failed to find file \"%s\".", fn);
  160. return (FALSE);
  161. }
  162. if (MAX_SIZE_T < sb.st_size) {
  163. msg("File \"%s\" is too big to load.", fn);
  164. return (FALSE);
  165. }
  166. curbp->b_fmtime = sb.st_mtime;
  167. msg("File \"%s\" %ld bytes saved.", fn, pos(curbp, curbp->b_ebuf));
  168. return (TRUE);
  169. }
  170. int load_file(char *fn)
  171. {
  172. /* reset the gap, make it the whole buffer */
  173. curbp->b_gap = curbp->b_buf;
  174. curbp->b_egap = curbp->b_ebuf;
  175. top();
  176. return insert_file(fn, FALSE);
  177. }
  178. /* reads file into buffer at point */
  179. int insert_file(char *fn, int modflag)
  180. {
  181. FILE *fp;
  182. size_t len;
  183. struct stat sb;
  184. if (stat(fn, &sb) < 0) {
  185. msg("Failed to find file \"%s\".", fn);
  186. return (FALSE);
  187. }
  188. if (MAX_SIZE_T < sb.st_size) {
  189. msg("File \"%s\" is too big to load.", fn);
  190. return (FALSE);
  191. }
  192. if (curbp->b_egap - curbp->b_gap < sb.st_size * sizeof (char_t) && !growgap(curbp, sb.st_size))
  193. return (FALSE);
  194. if ((fp = fopen(fn, "r")) == NULL) {
  195. msg("Failed to open file \"%s\".", fn);
  196. return (FALSE);
  197. }
  198. curbp->b_point = movegap(curbp, curbp->b_point);
  199. // undoset();
  200. curbp->b_gap += len = fread(curbp->b_gap, sizeof (char), (size_t) sb.st_size, fp);
  201. if (fclose(fp) != 0) {
  202. msg("Failed to close file \"%s\".", fn);
  203. return (FALSE);
  204. }
  205. curbp->b_flags &= (modflag ? B_MODIFIED : ~B_MODIFIED);
  206. curbp->b_fmtime = sb.st_mtime;
  207. msg("%ld bytes read.", len);
  208. return (TRUE);
  209. }
  210. /* Record a new undo location */
  211. void undoset()
  212. {
  213. point_t length;
  214. if(curbp->b_undo == NULL) {
  215. curbp->b_undo = realloc(curbp->b_undo, sizeof(undo_t));
  216. curbp->b_eundo = curbp->b_undo;
  217. }
  218. curbp->b_eundo += sizeof(undo_t);
  219. // if(pos(curbp, curbp->b_gap) == 0)
  220. (void) movegap(curbp, curbp->b_point);
  221. curbp->b_eundo->u_line = curbp->b_line;
  222. curbp->b_eundo->u_point = curbp->b_point;
  223. curbp->b_eundo->u_gap = curbp->b_gap - curbp->b_buf;
  224. curbp->b_eundo->u_egap = curbp->b_egap - curbp->b_buf;
  225. (void) movegap(curbp, (point_t) 0);
  226. length = (point_t) (curbp->b_ebuf - curbp->b_egap);
  227. curbp->b_eundo->u_size = length;
  228. curbp->b_eundo->u_scrap = malloc(sizeof(char_t)*curbp->b_eundo->u_size);
  229. memcpy(curbp->b_eundo->u_scrap, curbp->b_egap, curbp->b_eundo->u_size);
  230. (void) movegap(curbp, curbp->b_eundo->u_point);
  231. curbp->b_gap = curbp->b_eundo->u_gap + curbp->b_buf;
  232. curbp->b_egap = curbp->b_eundo->u_egap + curbp->b_buf;
  233. }
  234. void redoset()
  235. {
  236. point_t length;
  237. if(curbp->b_redo == NULL) {
  238. curbp->b_redo = realloc(curbp->b_redo, sizeof(undo_t));
  239. curbp->b_eredo = curbp->b_redo;
  240. }
  241. curbp->b_eredo += sizeof(undo_t);
  242. (void) movegap(curbp, curbp->b_point);
  243. curbp->b_eredo->u_line = curbp->b_line;
  244. curbp->b_eredo->u_point = curbp->b_point;
  245. curbp->b_eredo->u_gap = curbp->b_gap - curbp->b_buf;
  246. curbp->b_eredo->u_egap = curbp->b_egap - curbp->b_buf;
  247. (void) movegap(curbp, (point_t) 0);
  248. length = (point_t) (curbp->b_ebuf - curbp->b_egap);
  249. curbp->b_eredo->u_size = length;
  250. curbp->b_eredo->u_scrap = malloc(sizeof(char_t)*curbp->b_eredo->u_size);
  251. memcpy(curbp->b_eredo->u_scrap, curbp->b_egap, curbp->b_eredo->u_size);
  252. (void) movegap(curbp, curbp->b_eredo->u_point);
  253. curbp->b_gap = curbp->b_eredo->u_gap + curbp->b_buf;
  254. curbp->b_egap = curbp->b_eredo->u_egap + curbp->b_buf;
  255. }
  256. /* Undo */
  257. void undo()
  258. {
  259. if(curbp->b_eundo == curbp->b_undo) {
  260. msg("Nothing to undo!");
  261. return;
  262. }
  263. redoset();
  264. curbp->b_gap = curbp->b_buf + curbp->b_eundo->u_gap;
  265. curbp->b_egap = curbp->b_buf + curbp->b_eundo->u_egap;
  266. (void) movegap(curbp, (point_t) 0);
  267. memcpy(curbp->b_egap, curbp->b_eundo->u_scrap, curbp->b_eundo->u_size);
  268. curbp->b_point = movegap(curbp, curbp->b_eundo->u_point);
  269. curbp->b_line = curbp->b_eundo->u_line;
  270. curbp->b_flags |= B_MODIFIED;
  271. curbp->b_eundo -= sizeof(undo_t);
  272. }
  273. /* Redo */
  274. void redo()
  275. {
  276. if(curbp->b_eredo == curbp->b_redo) {
  277. msg("Nothing to redo!");
  278. return;
  279. }
  280. undoset();
  281. curbp->b_gap = curbp->b_buf + curbp->b_eredo->u_gap;
  282. curbp->b_egap = curbp->b_buf + curbp->b_eredo->u_egap;
  283. (void) movegap(curbp, (point_t) 0);
  284. memcpy(curbp->b_egap, curbp->b_eredo->u_scrap, curbp->b_eredo->u_size);
  285. curbp->b_point = movegap(curbp, curbp->b_eredo->u_point);
  286. curbp->b_line = curbp->b_eredo->u_line;
  287. curbp->b_flags |= B_MODIFIED;
  288. curbp->b_eredo -= sizeof(undo_t);
  289. }
  290. /* find the point for start of line ln */
  291. point_t line_to_point(int ln)
  292. {
  293. point_t end_p = pos(curbp, curbp->b_ebuf);
  294. point_t p, start;
  295. for (p=0, start=0; p <= end_p; p++) {
  296. char_t *c = ptr(curbp, p);
  297. if(c == 0)
  298. break;
  299. if ( *c == '\n') {
  300. if (--ln == 0)
  301. return start;
  302. if (p + 1 <= end_p)
  303. start = p + 1;
  304. }
  305. if(!*c && ln == 1)
  306. return start;
  307. }
  308. return -1;
  309. }
  310. /* scan buffer and fill in curline and lastline */
  311. void get_line_stats(int *curline, int *lastline, buffer_t *bp)
  312. {
  313. point_t end_p = pos(bp, bp->b_ebuf);
  314. point_t p;
  315. int line;
  316. *curline = -1;
  317. for (p=0, line=0; p < end_p; p++) {
  318. line += (*(ptr(bp,p)) == '\n') ? 1 : 0;
  319. *lastline = line;
  320. if (*curline == -1 && p == bp->b_point) {
  321. *curline = (*(ptr(bp,p)) == '\n') ? line : line + 1;
  322. }
  323. }
  324. *lastline = *lastline + 1;
  325. if (bp->b_point == end_p)
  326. *curline = *lastline;
  327. }
  328. /* Return TRUE if file was modified after the current buffer's recored mtime. */
  329. int is_file_modified(char *fn)
  330. {
  331. struct stat sb;
  332. if(stat(fn, &sb) < 0) {
  333. return (FALSE);
  334. }
  335. if(sb.st_mtime != 0 && curbp->b_fmtime != sb.st_mtime) {
  336. return (TRUE);
  337. }
  338. return (FALSE);
  339. }
  340. /* Return TRUE to continue, FALSE to stop. Revert happens here. */
  341. int file_was_modified_prompt()
  342. {
  343. const char *prompt = "This file has changed on disk; really edit (y/N/r) ?";
  344. char c = '\0';
  345. point_t p = curbp->b_point;
  346. struct stat sb;
  347. int current = 0, lastln = 0;
  348. print_to_msgline(prompt);
  349. clrtoeol(prompt, MSGLINE);
  350. c = yesnomaybeso('n');
  351. if (c == 'n') {
  352. clrtoeol("", MSGLINE);
  353. return FALSE;
  354. } else if(c == 'r') {
  355. curbp->b_point = 0;
  356. load_file(curbp->b_fname);
  357. clrtoeol("", MSGLINE);
  358. get_line_stats(&current, &lastln, curbp);
  359. curbp->b_point = p;
  360. curbp->b_line = current;
  361. msg("Buffer reverted.");
  362. return FALSE;
  363. } else if(c == 'y') {
  364. clrtoeol("", MSGLINE);
  365. if (stat(curbp->b_fname, &sb) < 0) {
  366. msg("Failed to find file \"%s\".", curbp->b_fname);
  367. return (FALSE);
  368. }
  369. if (MAX_SIZE_T < sb.st_size) {
  370. msg("File \"%s\" is too big to load.", curbp->b_fname);
  371. return (FALSE);
  372. }
  373. curbp->b_fmtime = sb.st_mtime;
  374. return TRUE;
  375. }
  376. clrtoeol("", MSGLINE);
  377. return (FALSE);
  378. }