io.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. *
  3. * Authors: Richard Kelsey, Jonathan Rees, Mike Sperber, Ivan Shmakov
  4. */
  5. /* NB: sysdep.h must come first in order for LFS to be enabled
  6. properly */
  7. #include "sysdep.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11. #include <stdarg.h>
  12. #include <string.h>
  13. #include "io.h"
  14. #include "scheme48.h"
  15. #include "unix.h"
  16. #include "c-mods.h"
  17. /* read a character while ignoring interrupts */
  18. #define READ_CHAR(PORT,RESULT) \
  19. { \
  20. FILE * TTport = PORT; \
  21. int TTchar; \
  22. if (EOF == (TTchar = getc(TTport))) \
  23. RESULT = s48_read_char(TTport); \
  24. else \
  25. RESULT = TTchar; \
  26. }
  27. /*
  28. Helper procedure for the READ_CHAR macro. If the getc was interrupted
  29. we clear the error bit and try again.
  30. */
  31. int
  32. s48_read_char(FILE *port)
  33. {
  34. int result;
  35. while(1) {
  36. if (ferror(port) && errno == EINTR) {
  37. clearerr(port);
  38. result = getc(port);
  39. if (EOF != result)
  40. return result; }
  41. else
  42. return EOF; }
  43. }
  44. /* called when getc(port) returned EOF */
  45. char
  46. ps_read_char(FILE *port, psbool *eofp, long *status, psbool peekp)
  47. {
  48. psbool errorp;
  49. int result;
  50. result = s48_read_char(port); /* read past any interruptions */
  51. if (result != EOF) {
  52. if (peekp)
  53. ungetc(result, port);
  54. *eofp = PSFALSE;
  55. *status = NO_ERRORS;
  56. return result; }
  57. else {
  58. errorp = ferror(port);
  59. clearerr(port);
  60. if (errorp) {
  61. *eofp = PSFALSE;
  62. *status = errno;
  63. return 0; }
  64. else {
  65. *eofp = PSTRUE;
  66. *status = NO_ERRORS;
  67. return 0; } }
  68. }
  69. long
  70. ps_read_integer(FILE *port, psbool *eofp, long *status)
  71. {
  72. long result;
  73. int ch;
  74. psbool negate;
  75. psbool errorp;
  76. /* eat whitespace */
  77. do { READ_CHAR(port, ch); }
  78. while (ch == ' ' || ch == '\t' || ch == '\n');
  79. /* read optional sign */
  80. if (ch == '-') {
  81. negate = PSTRUE;
  82. READ_CHAR(port, ch); }
  83. else
  84. negate = PSFALSE;
  85. if (ch < '0' || '9' < ch) {
  86. if (ch != EOF) {
  87. *eofp = PSFALSE;
  88. *status = EINVAL; } /* has to be something */
  89. else {
  90. errorp = ferror(port);
  91. clearerr(port);
  92. if (errorp) {
  93. *eofp = PSFALSE;
  94. *status = errno; }
  95. else {
  96. *eofp = PSTRUE;
  97. *status = 0; } }
  98. result = 0; }
  99. else {
  100. result = ch - '0';
  101. while(1) {
  102. READ_CHAR(port, ch);
  103. if (ch < '0' || '9' < ch)
  104. break;
  105. result = (10 * result) + (ch - '0'); }
  106. if (ch != EOF)
  107. ungetc(ch, port);
  108. *eofp = PSFALSE;
  109. *status = 0; }
  110. return (negate ? -result : result);
  111. }
  112. /* write a character regardless of interrupts */
  113. #define WRITE_CHAR(CH,PORT,RESULT) \
  114. { \
  115. char TTch = CH; \
  116. FILE * TTport = PORT; \
  117. if (putc(TTch, TTport) != EOF) \
  118. RESULT = 0; \
  119. else \
  120. RESULT = ps_write_char(TTch, TTport); \
  121. }
  122. /* called when putc(char, port) returned EOF */
  123. long
  124. ps_write_char(char ch, FILE *port)
  125. {
  126. while(1) {
  127. clearerr(port);
  128. if (errno != EINTR)
  129. return errno;
  130. else if (putc(ch, port) != EOF)
  131. return 0; }
  132. }
  133. static long
  134. write_integer(unsigned long n, FILE *port)
  135. {
  136. char ch;
  137. long status;
  138. if (n == 0)
  139. status = 0;
  140. else {
  141. status = write_integer(n / 10, port);
  142. if (status == 0) {
  143. ch = (n % 10) + '0';
  144. WRITE_CHAR(ch, port,status); } }
  145. return status;
  146. }
  147. long
  148. ps_write_integer(long n, FILE *port)
  149. {
  150. int status;
  151. if (n == 0) {
  152. WRITE_CHAR('0', port, status);
  153. return status; }
  154. else if (n > 0)
  155. return write_integer(n, port);
  156. else {
  157. WRITE_CHAR('-', port, status);
  158. if (status == 0)
  159. return write_integer(- n, port);
  160. else
  161. return status; }
  162. }
  163. long
  164. ps_write_string(char *string, FILE *port)
  165. {
  166. while (1) {
  167. if (EOF != fputs(string, port))
  168. return (0);
  169. clearerr(port);
  170. if (errno != EINTR)
  171. return (errno);
  172. }
  173. }
  174. long
  175. ps_read_block(FILE *port, char *buffer, long count, psbool *eofp, long *status)
  176. {
  177. int got = 0;
  178. psbool errorp;
  179. while(1) {
  180. got += fread(buffer, sizeof(char), count - got, port);
  181. if (got == count) {
  182. *eofp = PSFALSE;
  183. *status = NO_ERRORS;
  184. return got;}
  185. else if (ferror(port) && errno == EINTR)
  186. clearerr(port);
  187. else {
  188. *eofp = feof(port);
  189. errorp = ferror(port);
  190. clearerr(port);
  191. if (errorp)
  192. *status = errno;
  193. else
  194. *status = NO_ERRORS;
  195. return got;} };
  196. }
  197. long
  198. ps_write_block(FILE *port, char *buffer, long count)
  199. {
  200. int sent = 0;
  201. while(1) {
  202. sent += fwrite(buffer, sizeof(char), count - sent, port);
  203. if (sent == count)
  204. return NO_ERRORS;
  205. else if (ferror(port) && errno == EINTR)
  206. clearerr(port);
  207. else {
  208. clearerr(port);
  209. return errno; } }
  210. }
  211. void
  212. ps_error(char *message, long count, ...)
  213. {
  214. va_list ap;
  215. extern long opcode_count;
  216. va_start(ap, count);
  217. fputs(message, stderr);
  218. for(; count > 0; --count)
  219. fprintf(stderr, " %ld", va_arg(ap, long));
  220. putc('\n', stderr);
  221. exit(-1);
  222. }
  223. static FILE *
  224. ps_really_open_file(char *filename, long *status, char *mode)
  225. {
  226. #define FILE_NAME_SIZE 1024
  227. char filename_temp[FILE_NAME_SIZE];
  228. char *expanded;
  229. extern char *s48_expand_file_name(char *, char *, int);
  230. FILE *new_file;
  231. expanded = s48_expand_file_name(filename, filename_temp, FILE_NAME_SIZE);
  232. if (expanded == NULL) {
  233. *status = EDOM; /* has to be something */
  234. return NULL; }
  235. RETRY_NULL(new_file, fopen(expanded, mode));
  236. if (new_file == NULL) {
  237. *status = errno;
  238. return NULL; }
  239. *status = NO_ERRORS;
  240. return new_file;
  241. }
  242. FILE *
  243. ps_open_input_file(char *name, long *status)
  244. {
  245. return ps_really_open_file(name, status, "r");
  246. }
  247. FILE *
  248. ps_open_output_file(char *name, long *status)
  249. {
  250. return ps_really_open_file(name, status, "w");
  251. }
  252. long
  253. ps_close(FILE *stream)
  254. {
  255. int status;
  256. RETRY_NEG(status, fclose(stream));
  257. if (0 == status)
  258. return 0;
  259. else
  260. return errno;
  261. }