insert.c 6.5 KB

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