insert.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* $Id$ */
  2. /* Implementation of C_insertpart, C_beginpart, and C_endpart.
  3. Basic methodology: place the parts either in memory or on a temporary
  4. file, in the order received, and remember this order. Then, in a second
  5. "pass", write out the code.
  6. An optimization is possible: as long as the order in which the parts
  7. are received corresponds to the order in which they must be written,
  8. they can be written immediately.
  9. */
  10. #include <em_path.h>
  11. #include <alloc.h>
  12. #include "insert.h"
  13. char *C_tmpdir = TMP_DIR;
  14. static Part *C_stable[TABSIZ];
  15. static char *C_old_top;
  16. static char *C_old_opp;
  17. #ifndef INCORE
  18. static File *C_old_ofp;
  19. static int getbyte(long b)
  20. {
  21. /* Get the byte at offset "b" from the start of the
  22. temporary file, and try to do so in an efficient way.
  23. */
  24. static long start_core, curr_pos;
  25. if (b < start_core || b >= curr_pos) {
  26. /* the block wanted is not in core, so get it */
  27. long nb = (b & ~(BUFSIZ - 1));
  28. int n;
  29. C_flush();
  30. if (nb != curr_pos) {
  31. if (sys_seek(C_tfr, nb, 0, &curr_pos) == 0) {
  32. C_failed();
  33. }
  34. }
  35. if (! C_ibuf) {
  36. C_ibuf = Malloc(BUFSIZ);
  37. }
  38. if (sys_read(C_tfr, C_ibuf, BUFSIZ, &n) == 0) {
  39. C_failed();
  40. }
  41. curr_pos += n;
  42. start_core = nb;
  43. }
  44. return C_ibuf[(int) (b - start_core)];
  45. }
  46. #endif
  47. static void C_out_parts(PartOfPart *pp);
  48. static Part *C_findpart(int part);
  49. static void outpart(int id)
  50. {
  51. /* Output part "id", if present.
  52. */
  53. Part *p = C_findpart(id);
  54. if (p) {
  55. C_out_parts(p->p_parts);
  56. p->p_parts = 0;
  57. }
  58. }
  59. static void C_out_parts(PartOfPart *pp)
  60. {
  61. /* Output the list of chunks started by "pp".
  62. The list is build in reverse order, so this routine is
  63. recursive.
  64. */
  65. PartOfPart *prev = 0, *next;
  66. while (pp) {
  67. next = pp->pp_next;
  68. pp->pp_next = prev;
  69. prev = pp;
  70. pp = next;
  71. }
  72. pp = prev;
  73. while (pp) {
  74. if (pp->pp_type == INSERT) {
  75. (*C_outpart)(pp->pp_id);
  76. }
  77. else {
  78. /* copy the chunk to output */
  79. #ifdef INCORE
  80. register char *s = C_BASE + pp->pp_begin;
  81. char *se = C_BASE + pp->pp_end;
  82. while (s < se) {
  83. put(*s++);
  84. }
  85. #else
  86. register long b = pp->pp_begin;
  87. while (b < pp->pp_end) {
  88. put(getbyte(b++));
  89. }
  90. #endif
  91. }
  92. prev = pp;
  93. pp = pp->pp_next;
  94. free((char *) prev);
  95. }
  96. }
  97. static Part *C_findpart(int part)
  98. {
  99. /* Look for part "part" in the table.
  100. Return 0 if not present,
  101. */
  102. register Part *p = C_stable[part % TABSIZ];
  103. while (p && p->p_id != part) {
  104. p = p->p_next;
  105. }
  106. return p;
  107. }
  108. static void swttmp()
  109. {
  110. #ifndef INCORE
  111. if (C_tmpfile == 0) {
  112. static char tmpbuf[64];
  113. register char *p = tmpbuf;
  114. strcpy(p, C_tmpdir);
  115. strcat(p, "/CodeXXXXXX");
  116. C_tmpfile = mktemp(p);
  117. if (! sys_open(p, OP_WRITE, &C_old_ofp)) {
  118. C_failed();
  119. }
  120. if (! sys_open(p, OP_READ, &C_tfr)) {
  121. C_failed();
  122. }
  123. }
  124. if (! C_ontmpfile) {
  125. File *p = C_ofp;
  126. C_flush();
  127. C_ofp = C_old_ofp;
  128. C_old_ofp = p;
  129. C_ontmpfile = 1;
  130. }
  131. #else
  132. if (! C_ontmpfile) {
  133. char *p;
  134. p = C_opp;
  135. C_opp = C_old_opp;
  136. C_old_opp = p;
  137. p = C_top;
  138. C_top = C_old_top;
  139. C_old_top = p;
  140. C_ontmpfile = 1;
  141. }
  142. #endif
  143. }
  144. static void swtout()
  145. {
  146. #ifndef INCORE
  147. if (C_ontmpfile) {
  148. File *p = C_ofp;
  149. C_flush();
  150. C_ofp = C_old_ofp;
  151. C_old_ofp = p;
  152. C_ontmpfile = 0;
  153. }
  154. #else
  155. if (C_ontmpfile) {
  156. char *p;
  157. p = C_opp;
  158. C_opp = C_old_opp;
  159. C_old_opp = p;
  160. p = C_top;
  161. C_top = C_old_top;
  162. C_old_top = p;
  163. C_ontmpfile = 0;
  164. }
  165. #endif
  166. }
  167. static int available(int part)
  168. {
  169. /* See if part "part", and all the parts it consists of,
  170. are available. Return 1 if they are, 0 otherwize
  171. */
  172. register Part *p = C_findpart(part);
  173. register PartOfPart *pp;
  174. int retval = 1;
  175. if (p == 0) return 0;
  176. if (p->p_flags & BUSY) {
  177. /* recursive call ends up here, and this just should
  178. not happen. It is an error of the programmer using
  179. this module.
  180. */
  181. C_internal_error();
  182. }
  183. p->p_flags |= BUSY;
  184. pp = p->p_parts;
  185. while (pp) {
  186. if (pp->pp_type == INSERT && ! available(pp->pp_id)) {
  187. retval = 0;
  188. break;
  189. }
  190. else pp = pp->pp_next;
  191. }
  192. p->p_flags &= ~BUSY;
  193. return retval;
  194. }
  195. static Part *mkpart(int part)
  196. {
  197. /* Create a Part structure with id "part", and return a
  198. pointer to it, after checking that is does not exist
  199. already.
  200. */
  201. register Part *p = C_findpart(part);
  202. register int index = part % TABSIZ;
  203. if (p != 0) {
  204. /* multiple defined part ... */
  205. C_internal_error();
  206. }
  207. p = (Part *) Malloc(sizeof(Part));
  208. p->p_id = part;
  209. p->p_next = C_stable[index];
  210. C_stable[index] = p;
  211. p->p_parts = 0;
  212. p->p_flags = 0;
  213. p->p_prevpart = 0;
  214. return p;
  215. }
  216. static void end_partofpart(Part *p)
  217. {
  218. /* End the current chunk of part *p.
  219. */
  220. if (p) {
  221. register PartOfPart *pp = p->p_parts;
  222. pp->pp_end = C_current_out - C_BASE;
  223. if (pp->pp_begin == pp->pp_end) {
  224. /* nothing in this chunk, so give it back */
  225. p->p_parts = pp->pp_next;
  226. free((char *) pp);
  227. }
  228. }
  229. }
  230. static void resume(Part *p)
  231. {
  232. /* Resume part "p", by creating a new PartOfPart structure
  233. for it.
  234. */
  235. register PartOfPart *pp = (PartOfPart *) Malloc(sizeof(PartOfPart));
  236. swttmp();
  237. C_curr_part = p;
  238. pp->pp_next = p->p_parts;
  239. p->p_parts = pp;
  240. pp->pp_type = TEXT;
  241. pp->pp_begin = C_current_out - C_BASE;
  242. }
  243. void C_insertpart(int part)
  244. {
  245. /* Insert part "part" in the current part. If C_sequential is
  246. still set and the part to be inserted is available now,
  247. just write it out.
  248. */
  249. register Part *p;
  250. register PartOfPart *pp;
  251. C_outpart = outpart;
  252. C_swttmp = swttmp;
  253. C_swtout = swtout;
  254. if (C_sequential && available(part)) {
  255. outpart(part);
  256. return;
  257. }
  258. if (C_sequential) {
  259. /* stop the sequential stuff, by creating a part */
  260. C_sequential = 0;
  261. p = mkpart(0);
  262. C_curr_part = p;
  263. }
  264. else {
  265. p = C_curr_part;
  266. end_partofpart(p);
  267. }
  268. /* Now, add the insertion of "part" to the current part. */
  269. pp = (PartOfPart *) Malloc(sizeof(PartOfPart));
  270. pp->pp_next = p->p_parts;
  271. p->p_parts = pp;
  272. pp->pp_type = INSERT;
  273. pp->pp_id = part;
  274. resume(p);
  275. }
  276. void C_beginpart(int part)
  277. {
  278. /* Now follows the definition for part "part".
  279. Suspend the current part, and add part "part" to the
  280. table.
  281. */
  282. register Part *p = mkpart(part);
  283. C_outpart = outpart;
  284. C_swttmp = swttmp;
  285. C_swtout = swtout;
  286. end_partofpart(C_curr_part);
  287. p->p_prevpart = C_curr_part;
  288. resume(p);
  289. }
  290. void C_endpart(int part)
  291. {
  292. /* End the current part. The parameter "part" is just there
  293. for the checking. Do we really need it ???
  294. */
  295. register Part *p = C_curr_part;
  296. if (p->p_id != part) {
  297. /* illegal C_endpart ... */
  298. C_internal_error();
  299. }
  300. end_partofpart(p);
  301. if (p->p_prevpart) resume(p->p_prevpart);
  302. else {
  303. C_curr_part = 0;
  304. swtout();
  305. }
  306. }