insert.c 6.5 KB

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