io.c 5.7 KB

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