io.c 5.6 KB

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