gap.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. curbp->b_eundo += sizeof(undo_t);
  215. // if(pos(curbp, curbp->b_gap) == 0)
  216. (void) movegap(curbp, curbp->b_point);
  217. curbp->b_eundo->u_point = curbp->b_point;
  218. curbp->b_eundo->u_gap = curbp->b_gap - curbp->b_buf;
  219. curbp->b_eundo->u_egap = curbp->b_egap - curbp->b_buf;
  220. (void) movegap(curbp, (point_t) 0);
  221. length = (point_t) (curbp->b_ebuf - curbp->b_egap);
  222. curbp->b_eundo->u_size = length;
  223. curbp->b_eundo->u_scrap = malloc(sizeof(char_t)*curbp->b_eundo->u_size);
  224. memcpy(curbp->b_eundo->u_scrap, curbp->b_egap, curbp->b_eundo->u_size);
  225. (void) movegap(curbp, curbp->b_eundo->u_point);
  226. curbp->b_gap = curbp->b_eundo->u_gap + curbp->b_buf;
  227. curbp->b_egap = curbp->b_eundo->u_egap + curbp->b_buf;
  228. }
  229. /* Undo */
  230. void undo()
  231. {
  232. if(curbp->b_eundo == curbp->b_undo) {
  233. msg("Nothing to undo!");
  234. return;
  235. }
  236. curbp->b_gap = curbp->b_buf + curbp->b_eundo->u_gap;
  237. curbp->b_egap = curbp->b_buf + curbp->b_eundo->u_egap;
  238. (void) movegap(curbp, (point_t) 0);
  239. memcpy(curbp->b_egap, curbp->b_eundo->u_scrap, curbp->b_eundo->u_size);
  240. curbp->b_point = movegap(curbp, curbp->b_eundo->u_point);
  241. curbp->b_flags |= B_MODIFIED;
  242. curbp->b_eundo -= sizeof(undo_t);
  243. }
  244. /* find the point for start of line ln */
  245. point_t line_to_point(int ln)
  246. {
  247. point_t end_p = pos(curbp, curbp->b_ebuf);
  248. point_t p, start;
  249. for (p=0, start=0; p <= end_p; p++) {
  250. char_t *c = ptr(curbp, p);
  251. if(c == 0)
  252. break;
  253. if ( *c == '\n') {
  254. if (--ln == 0)
  255. return start;
  256. if (p + 1 <= end_p)
  257. start = p + 1;
  258. }
  259. if(!*c && ln == 1)
  260. return start;
  261. }
  262. return -1;
  263. }
  264. /* scan buffer and fill in curline and lastline */
  265. void get_line_stats(int *curline, int *lastline, buffer_t *bp)
  266. {
  267. point_t end_p = pos(bp, bp->b_ebuf);
  268. point_t p;
  269. int line;
  270. *curline = -1;
  271. for (p=0, line=0; p < end_p; p++) {
  272. line += (*(ptr(bp,p)) == '\n') ? 1 : 0;
  273. *lastline = line;
  274. if (*curline == -1 && p == bp->b_point) {
  275. *curline = (*(ptr(bp,p)) == '\n') ? line : line + 1;
  276. }
  277. }
  278. *lastline = *lastline + 1;
  279. if (bp->b_point == end_p)
  280. *curline = *lastline;
  281. }
  282. /* Return TRUE if file was modified after the current buffer's recored mtime. */
  283. int is_file_modified(char *fn)
  284. {
  285. struct stat sb;
  286. if(stat(fn, &sb) < 0) {
  287. return (FALSE);
  288. }
  289. if(sb.st_mtime != 0 && curbp->b_fmtime != sb.st_mtime) {
  290. return (TRUE);
  291. }
  292. return (FALSE);
  293. }
  294. /* Return TRUE to continue, FALSE to stop. Revert happens here. */
  295. int file_was_modified_prompt()
  296. {
  297. const char *prompt = "This file has changed on disk; really edit (y/N/r) ?";
  298. char c = '\0';
  299. point_t p = curbp->b_point;
  300. struct stat sb;
  301. print_to_msgline(prompt);
  302. clrtoeol(prompt, MSGLINE);
  303. c = yesnomaybeso('n');
  304. if (c == 'n') {
  305. clrtoeol("", MSGLINE);
  306. return FALSE;
  307. } else if(c == 'r') {
  308. curbp->b_point = 0;
  309. load_file(curbp->b_fname);
  310. clrtoeol("", MSGLINE);
  311. curbp->b_point = p;
  312. msg("Buffer reverted.");
  313. return FALSE;
  314. } else if(c == 'y') {
  315. clrtoeol("", MSGLINE);
  316. if (stat(curbp->b_fname, &sb) < 0) {
  317. msg("Failed to find file \"%s\".", curbp->b_fname);
  318. return (FALSE);
  319. }
  320. if (MAX_SIZE_T < sb.st_size) {
  321. msg("File \"%s\" is too big to load.", curbp->b_fname);
  322. return (FALSE);
  323. }
  324. curbp->b_fmtime = sb.st_mtime;
  325. return TRUE;
  326. }
  327. clrtoeol("", MSGLINE);
  328. return (FALSE);
  329. }