io.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* $Header$ */
  2. /* I/O part of em_code module.
  3. Also contains C_open, C_close
  4. */
  5. #include <alloc.h>
  6. #include <em_path.h>
  7. #include <em_arith.h>
  8. #include "insert.h"
  9. int C_ontmpfile = 0;
  10. int C_sequential = 1;
  11. Part *C_curr_part;
  12. Part *C_stable[TABSIZ];
  13. char *C_tmpdir = TMP_DIR;
  14. int (*C_outpart)(), (*C_swtout)(), (*C_swttmp)();
  15. #ifdef INCORE
  16. char *C_BASE;
  17. #endif
  18. File *C_ofp;
  19. #ifndef INCORE
  20. File *C_tfr, *C_old_ofp;
  21. char *C_tmpfile;
  22. char *strcpy(), *strcat();
  23. char *C_ibuf = 0;
  24. long C_current_out;
  25. #endif
  26. #if BUFSIZ <= 1024 && BIGMACHINE
  27. #define BUFFERSIZ 8*BUFSIZ
  28. #else
  29. #define BUFFERSIZ BUFSIZ
  30. #endif
  31. static char obuf[BUFFERSIZ];
  32. char *C_top = &obuf[BUFFERSIZ];
  33. char *C_old_top;
  34. char *C_old_opp;
  35. #ifdef INCORE
  36. char *C_current_out = obuf;
  37. #else
  38. char *C_opp = obuf;
  39. #endif
  40. C_flush() {
  41. #ifdef INCORE
  42. static unsigned int bufsiz;
  43. if (C_ontmpfile) {
  44. if (C_BASE == 0) {
  45. C_BASE = Malloc(BUFFERSIZ);
  46. bufsiz = BUFFERSIZ;
  47. C_current_out = C_BASE;
  48. }
  49. else {
  50. C_BASE = Srealloc(C_BASE, (bufsiz << 1));
  51. C_current_out = C_BASE + bufsiz;
  52. bufsiz <<= 1;
  53. }
  54. C_top = C_BASE + bufsiz;
  55. return;
  56. }
  57. #endif
  58. if (C_opp != obuf && sys_write(C_ofp, obuf, C_opp - obuf) == 0) {
  59. C_failed();
  60. }
  61. C_opp = obuf;
  62. }
  63. #ifndef INCORE
  64. #define Xputbyte(c) if (C_ontmpfile) C_current_out++; put(c)
  65. #else
  66. #define Xputbyte(c) put(c)
  67. #endif
  68. C_putbyte(c)
  69. int c;
  70. {
  71. Xputbyte(c);
  72. }
  73. #define C_putbyte Xputbyte
  74. C_init(w, p)
  75. arith w, p;
  76. {
  77. }
  78. C_open(nm)
  79. char *nm;
  80. {
  81. /* Open file "nm" for output
  82. */
  83. if (nm == 0)
  84. C_ofp = STDOUT; /* standard output */
  85. else
  86. if (sys_open(nm, OP_WRITE, &C_ofp) == 0)
  87. return 0;
  88. return 1;
  89. }
  90. C_close()
  91. {
  92. /* Finish the code-generation.
  93. */
  94. #ifndef INCORE
  95. C_flush();
  96. if (C_tmpfile) {
  97. (*C_swttmp)();
  98. sys_close(C_ofp);
  99. #else
  100. if (C_BASE) {
  101. #endif
  102. if (C_curr_part) {
  103. C_curr_part->p_parts->pp_end = C_current_out - C_BASE;
  104. }
  105. (*C_swtout)();
  106. if (! C_sequential) {
  107. (*C_outpart)(0);
  108. }
  109. #ifndef INCORE
  110. sys_close(C_tfr);
  111. sys_remove(C_tmpfile);
  112. if (C_ibuf) free(C_ibuf);
  113. #else
  114. free(C_BASE);
  115. #endif
  116. }
  117. C_flush();
  118. if (C_ofp != STDOUT)
  119. sys_close(C_ofp);
  120. C_ofp = 0;
  121. }
  122. C_busy()
  123. {
  124. return C_ofp != 0; /* true if code is being generated */
  125. }