gap.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. // if (!posix_file(fn)) {
  133. // msg("Not a portable POSIX file name.");
  134. // return (FALSE);
  135. // }
  136. if (!backup_file(fn)) {
  137. msg("Failed to backup file \"%s\".", fn);
  138. return (FALSE);
  139. }
  140. fp = fopen(fn, "w");
  141. if (fp == NULL) {
  142. msg("Failed to open file \"%s\".", fn);
  143. return (FALSE);
  144. }
  145. (void) movegap(curbp, (point_t) 0);
  146. length = (point_t) (curbp->b_ebuf - curbp->b_egap);
  147. if (fwrite(curbp->b_egap, sizeof (char), (size_t) length, fp) != length) {
  148. msg("Failed to write file \"%s\".", fn);
  149. return (FALSE);
  150. }
  151. if (fclose(fp) != 0) {
  152. msg("Failed to close file \"%s\".", fn);
  153. return (FALSE);
  154. }
  155. curbp->b_flags &= ~B_MODIFIED;
  156. msg("File \"%s\" %ld bytes saved.", fn, pos(curbp, curbp->b_ebuf));
  157. return (TRUE);
  158. }
  159. int load_file(char *fn)
  160. {
  161. /* reset the gap, make it the whole buffer */
  162. curbp->b_gap = curbp->b_buf;
  163. curbp->b_egap = curbp->b_ebuf;
  164. top();
  165. return insert_file(fn, FALSE);
  166. }
  167. /* reads file into buffer at point */
  168. int insert_file(char *fn, int modflag)
  169. {
  170. FILE *fp;
  171. size_t len;
  172. struct stat sb;
  173. if (stat(fn, &sb) < 0) {
  174. msg("Failed to find file \"%s\".", fn);
  175. return (FALSE);
  176. }
  177. if (MAX_SIZE_T < sb.st_size) {
  178. msg("File \"%s\" is too big to load.", fn);
  179. return (FALSE);
  180. }
  181. if (curbp->b_egap - curbp->b_gap < sb.st_size * sizeof (char_t) && !growgap(curbp, sb.st_size))
  182. return (FALSE);
  183. if ((fp = fopen(fn, "r")) == NULL) {
  184. msg("Failed to open file \"%s\".", fn);
  185. return (FALSE);
  186. }
  187. curbp->b_point = movegap(curbp, curbp->b_point);
  188. // undoset();
  189. curbp->b_gap += len = fread(curbp->b_gap, sizeof (char), (size_t) sb.st_size, fp);
  190. if (fclose(fp) != 0) {
  191. msg("Failed to close file \"%s\".", fn);
  192. return (FALSE);
  193. }
  194. curbp->b_flags &= (modflag ? B_MODIFIED : ~B_MODIFIED);
  195. msg("File \"%s\" %ld bytes read.", fn, len);
  196. return (TRUE);
  197. }
  198. /* Record a new undo location */
  199. void undoset()
  200. {
  201. point_t length;
  202. curbp->b_eundo++;
  203. // if(pos(curbp, curbp->b_gap) == 0)
  204. (void) movegap(curbp, curbp->b_point);
  205. curbp->b_eundo->u_point = curbp->b_point;
  206. curbp->b_eundo->u_gap = curbp->b_gap - curbp->b_buf;
  207. curbp->b_eundo->u_egap = curbp->b_egap - curbp->b_buf;
  208. (void) movegap(curbp, (point_t) 0);
  209. length = (point_t) (curbp->b_ebuf - curbp->b_egap);
  210. curbp->b_eundo->u_size = length;
  211. curbp->b_eundo->u_scrap = malloc(sizeof(char_t)*curbp->b_eundo->u_size);
  212. memcpy(curbp->b_eundo->u_scrap, curbp->b_egap, curbp->b_eundo->u_size);
  213. (void) movegap(curbp, curbp->b_eundo->u_point);
  214. curbp->b_gap = curbp->b_eundo->u_gap + curbp->b_buf;
  215. curbp->b_egap = curbp->b_eundo->u_egap + curbp->b_buf;
  216. }
  217. /* Undo */
  218. void undo()
  219. {
  220. if(curbp->b_eundo == curbp->b_undo) {
  221. msg("Nothing to undo!");
  222. return;
  223. }
  224. curbp->b_gap = curbp->b_buf + curbp->b_eundo->u_gap;
  225. curbp->b_egap = curbp->b_buf + curbp->b_eundo->u_egap;
  226. (void) movegap(curbp, (point_t) 0);
  227. memcpy(curbp->b_egap, curbp->b_eundo->u_scrap, curbp->b_eundo->u_size);
  228. curbp->b_point = movegap(curbp, curbp->b_eundo->u_point);
  229. curbp->b_flags |= B_MODIFIED;
  230. curbp->b_eundo--;
  231. }
  232. /* find the point for start of line ln */
  233. point_t line_to_point(int ln)
  234. {
  235. point_t end_p = pos(curbp, curbp->b_ebuf);
  236. point_t p, start;
  237. for (p=0, start=0; p <= end_p; p++) {
  238. char_t c = *(ptr(curbp, p));
  239. if ( c == '\n') {
  240. if (--ln == 0)
  241. return start;
  242. if (p + 1 <= end_p)
  243. start = p + 1;
  244. }
  245. if(!c && ln == 1)
  246. return start;
  247. }
  248. return -1;
  249. }
  250. /* scan buffer and fill in curline and lastline */
  251. void get_line_stats(int *curline, int *lastline, buffer_t *bp)
  252. {
  253. point_t end_p = pos(bp, bp->b_ebuf);
  254. point_t p;
  255. int line;
  256. *curline = -1;
  257. for (p=0, line=0; p < end_p; p++) {
  258. line += (*(ptr(bp,p)) == '\n') ? 1 : 0;
  259. *lastline = line;
  260. if (*curline == -1 && p == bp->b_point) {
  261. *curline = (*(ptr(bp,p)) == '\n') ? line : line + 1;
  262. }
  263. }
  264. *lastline = *lastline + 1;
  265. if (bp->b_point == end_p)
  266. *curline = *lastline;
  267. }