io.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <stdarg.h>
  7. #include <string.h>
  8. #include <windows.h>
  9. #include "io.h"
  10. #include "scheme48.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(TRUE) {
  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(TRUE) {
  122. clearerr(port);
  123. if (errno != EINTR)
  124. return errno;
  125. else if (putc(ch, port) != EOF)
  126. return 0; }
  127. }
  128. long
  129. ps_write_integer(long n, FILE *port)
  130. {
  131. int status;
  132. static long write_integer(unsigned long n, FILE *port);
  133. if (n == 0) {
  134. WRITE_CHAR('0', port, status);
  135. return status; }
  136. else if (n > 0)
  137. return write_integer(n, port);
  138. else {
  139. WRITE_CHAR('-', port, status);
  140. if (status == 0)
  141. return write_integer(- n, port);
  142. else
  143. return status; }
  144. }
  145. static long
  146. write_integer(unsigned long n, FILE *port)
  147. {
  148. char ch;
  149. long status;
  150. if (n == 0)
  151. status = 0;
  152. else {
  153. status = write_integer(n / 10, port);
  154. if (status == 0) {
  155. ch = (char) ((n % 10) + '0');
  156. WRITE_CHAR(ch, port,status); } }
  157. return status;
  158. }
  159. long
  160. ps_write_string(char *string, FILE *port)
  161. {
  162. while (TRUE) {
  163. if (EOF != fputs(string, port))
  164. return (0);
  165. clearerr(port);
  166. if (errno != EINTR)
  167. return (errno);
  168. }
  169. }
  170. long
  171. ps_read_block(FILE *port, char *buffer, long count, psbool *eofp, long *status)
  172. {
  173. int got = 0;
  174. psbool errorp;
  175. while(TRUE) {
  176. got += fread(buffer, sizeof(char), count - got, port);
  177. if (got == count) {
  178. *eofp = PSFALSE;
  179. *status = NO_ERRORS;
  180. return got;}
  181. else if (ferror(port) && errno == EINTR)
  182. clearerr(port);
  183. else {
  184. *eofp = feof(port);
  185. errorp = ferror(port);
  186. clearerr(port);
  187. if (errorp)
  188. *status = errno;
  189. else
  190. *status = NO_ERRORS;
  191. return got;} };
  192. }
  193. long
  194. ps_write_block(FILE *port, char *buffer, long count)
  195. {
  196. int sent = 0;
  197. while(TRUE) {
  198. sent += fwrite(buffer, sizeof(char), count - sent, port);
  199. if (sent == count)
  200. return NO_ERRORS;
  201. else if (ferror(port) && errno == EINTR)
  202. clearerr(port);
  203. else {
  204. clearerr(port);
  205. return errno; } }
  206. }
  207. void
  208. ps_error(char *message, long count, ...)
  209. {
  210. va_list ap;
  211. extern long opcode_count;
  212. va_start(ap, count);
  213. fputs(message, stderr);
  214. for(; count > 0; --count)
  215. fprintf(stderr, " %ld", va_arg(ap, long));
  216. putc('\n', stderr);
  217. exit(-1);
  218. }
  219. static FILE *
  220. ps_really_open_file(char *filename, long *status, char *mode)
  221. {
  222. #define FILE_NAME_SIZE 1024
  223. char filename_temp[FILE_NAME_SIZE];
  224. char *expanded;
  225. extern char *s48_expand_file_name(char *, char *, int);
  226. FILE *new;
  227. expanded = s48_expand_file_name(filename, filename_temp, FILE_NAME_SIZE);
  228. if (expanded == NULL) {
  229. *status = 4711; /* #### has to be something */
  230. return NULL; }
  231. new = fopen(expanded, mode);
  232. if (new == NULL) {
  233. *status = errno;
  234. return NULL; }
  235. *status = NO_ERRORS;
  236. return new;
  237. }
  238. FILE *
  239. ps_open_input_file(char *name, long *status)
  240. {
  241. return ps_really_open_file(name, status, "rb");
  242. }
  243. FILE *
  244. ps_open_output_file(char *name, long *status)
  245. {
  246. return ps_really_open_file(name, status, "wb");
  247. }
  248. long
  249. ps_close(FILE *stream)
  250. {
  251. int status;
  252. status = fclose(stream);
  253. if (0 == status)
  254. return 0;
  255. else
  256. return errno;
  257. }
  258. char*
  259. s48_get_os_string_encoding(void)
  260. {
  261. /* really UTF-8of16, but that doesn't matter at the Scheme level */
  262. return "UTF-8";
  263. }