ports.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* classes: h_files */
  2. #ifndef SCM_PORTS_H
  3. #define SCM_PORTS_H
  4. /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libguile/__scm.h"
  21. #include "libguile/print.h"
  22. #include "libguile/struct.h"
  23. #include "libguile/threads.h"
  24. /* Not sure if this is a good idea. We need it for off_t. */
  25. #include <sys/types.h>
  26. #define SCM_INITIAL_PUTBACK_BUF_SIZE 4
  27. /* values for the rw_active flag. */
  28. typedef enum scm_t_port_rw_active {
  29. SCM_PORT_NEITHER = 0,
  30. SCM_PORT_READ = 1,
  31. SCM_PORT_WRITE = 2
  32. } scm_t_port_rw_active;
  33. /* C representation of a Scheme port. */
  34. typedef struct
  35. {
  36. SCM port; /* Link back to the port object. */
  37. long entry; /* Index in port table. */
  38. int revealed; /* 0 not revealed, > 1 revealed.
  39. * Revealed ports do not get GC'd.
  40. */
  41. /* data for the underlying port implementation as a raw C value. */
  42. scm_t_bits stream;
  43. SCM file_name; /* debugging support. */
  44. long line_number; /* debugging support. */
  45. int column_number; /* debugging support. */
  46. /* port buffers. the buffer(s) are set up for all ports.
  47. in the case of string ports, the buffer is the string itself.
  48. in the case of unbuffered file ports, the buffer is a
  49. single char: shortbuf. */
  50. /* this buffer is filled from read_buf to read_end using the ptob
  51. buffer_fill. then input requests are taken from read_pos until
  52. it reaches read_end. */
  53. unsigned char *read_buf; /* buffer start. */
  54. const unsigned char *read_pos;/* the next unread char. */
  55. unsigned char *read_end; /* pointer to last buffered char + 1. */
  56. off_t read_buf_size; /* size of the buffer. */
  57. /* when chars are put back into the buffer, e.g., using peek-char or
  58. unread-string, the read-buffer pointers are switched to cbuf.
  59. the original pointers are saved here and restored when the put-back
  60. chars have been consumed. */
  61. unsigned char *saved_read_buf;
  62. const unsigned char *saved_read_pos;
  63. unsigned char *saved_read_end;
  64. off_t saved_read_buf_size;
  65. /* write requests are saved into this buffer at write_pos until it
  66. reaches write_buf + write_buf_size, then the ptob flush is
  67. called. */
  68. unsigned char *write_buf; /* buffer start. */
  69. unsigned char *write_pos; /* pointer to last buffered char + 1. */
  70. unsigned char *write_end; /* pointer to end of buffer + 1. */
  71. off_t write_buf_size; /* size of the buffer. */
  72. unsigned char shortbuf; /* buffer for "unbuffered" streams. */
  73. int rw_random; /* true if the port is random access.
  74. implies that the buffers must be
  75. flushed before switching between
  76. reading and writing, seeking, etc. */
  77. scm_t_port_rw_active rw_active; /* for random access ports,
  78. indicates which of the buffers
  79. is currently in use. can be
  80. SCM_PORT_WRITE, SCM_PORT_READ,
  81. or SCM_PORT_NEITHER. */
  82. /* a buffer for un-read chars and strings. */
  83. unsigned char *putback_buf;
  84. size_t putback_buf_size; /* allocated size of putback_buf. */
  85. } scm_t_port;
  86. SCM_API scm_t_port **scm_i_port_table;
  87. SCM_API long scm_i_port_table_size; /* Number of ports in scm_i_port_table. */
  88. SCM_API scm_i_pthread_mutex_t scm_i_port_table_mutex;
  89. #define SCM_READ_BUFFER_EMPTY_P(c_port) (c_port->read_pos >= c_port->read_end)
  90. #define SCM_EOF_OBJECT_P(x) (scm_is_eq ((x), SCM_EOF_VAL))
  91. /* PORT FLAGS
  92. * A set of flags characterizes a port.
  93. * Note that we reserve the bits 1 << 24 and above for use by the
  94. * routines in the port's scm_ptobfuns structure.
  95. */
  96. #define SCM_OPN (1L<<16) /* Is the port open? */
  97. #define SCM_RDNG (2L<<16) /* Is it a readable port? */
  98. #define SCM_WRTNG (4L<<16) /* Is it writable? */
  99. #define SCM_BUF0 (8L<<16) /* Is it unbuffered? */
  100. #define SCM_BUFLINE (64L<<16) /* Is it line-buffered? */
  101. #define SCM_PORTP(x) (!SCM_IMP (x) && (SCM_TYP7 (x) == scm_tc7_port))
  102. #define SCM_OPPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN)))
  103. #define SCM_OPINPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN | SCM_RDNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG)))
  104. #define SCM_OPOUTPORTP(x) (!SCM_IMP(x) && (((0x7f | SCM_OPN | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG)))
  105. #define SCM_INPUT_PORT_P(x) \
  106. (!SCM_IMP(x) \
  107. && (((0x7f | SCM_RDNG) & SCM_CELL_WORD_0(x)) == (scm_tc7_port | SCM_RDNG)))
  108. #define SCM_OUTPUT_PORT_P(x) \
  109. (!SCM_IMP(x) \
  110. && (((0x7f | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_WRTNG)))
  111. #define SCM_OPENP(x) (!SCM_IMP(x) && (SCM_OPN & SCM_CELL_WORD_0 (x)))
  112. #define SCM_CLOSEDP(x) (!SCM_OPENP(x))
  113. #define SCM_CLR_PORT_OPEN_FLAG(p) \
  114. SCM_SET_CELL_WORD_0 ((p), SCM_CELL_WORD_0 (p) & ~SCM_OPN)
  115. #define SCM_PTAB_ENTRY(x) ((scm_t_port *) SCM_CELL_WORD_1 (x))
  116. #define SCM_SETPTAB_ENTRY(x, ent) (SCM_SET_CELL_WORD_1 ((x), (scm_t_bits) (ent)))
  117. #define SCM_STREAM(x) (SCM_PTAB_ENTRY(x)->stream)
  118. #define SCM_SETSTREAM(x, s) (SCM_PTAB_ENTRY(x)->stream = (scm_t_bits) (s))
  119. #define SCM_FILENAME(x) (SCM_PTAB_ENTRY(x)->file_name)
  120. #define SCM_SET_FILENAME(x, n) (SCM_PTAB_ENTRY(x)->file_name = (n))
  121. #define SCM_LINUM(x) (SCM_PTAB_ENTRY(x)->line_number)
  122. #define SCM_COL(x) (SCM_PTAB_ENTRY(x)->column_number)
  123. #define SCM_REVEALED(x) (SCM_PTAB_ENTRY(x)->revealed)
  124. #define SCM_SETREVEALED(x, s) (SCM_PTAB_ENTRY(x)->revealed = (s))
  125. #define SCM_INCLINE(port) {SCM_LINUM (port) += 1; SCM_COL (port) = 0;}
  126. #define SCM_ZEROCOL(port) {SCM_COL (port) = 0;}
  127. #define SCM_INCCOL(port) {SCM_COL (port) += 1;}
  128. #define SCM_DECCOL(port) {if (SCM_COL (port) > 0) SCM_COL (port) -= 1;}
  129. #define SCM_TABCOL(port) {SCM_COL (port) += 8 - SCM_COL (port) % 8;}
  130. /* port-type description. */
  131. typedef struct scm_t_ptob_descriptor
  132. {
  133. char *name;
  134. SCM (*mark) (SCM);
  135. size_t (*free) (SCM);
  136. int (*print) (SCM exp, SCM port, scm_print_state *pstate);
  137. SCM (*equalp) (SCM, SCM);
  138. int (*close) (SCM port);
  139. void (*write) (SCM port, const void *data, size_t size);
  140. void (*flush) (SCM port);
  141. void (*end_input) (SCM port, int offset);
  142. int (*fill_input) (SCM port);
  143. int (*input_waiting) (SCM port);
  144. off_t (*seek) (SCM port, off_t OFFSET, int WHENCE);
  145. void (*truncate) (SCM port, off_t length);
  146. } scm_t_ptob_descriptor;
  147. #define SCM_TC2PTOBNUM(x) (0x0ff & ((x) >> 8))
  148. #define SCM_PTOBNUM(x) (SCM_TC2PTOBNUM (SCM_CELL_TYPE (x)))
  149. /* SCM_PTOBNAME can be 0 if name is missing */
  150. #define SCM_PTOBNAME(ptobnum) scm_ptobs[ptobnum].name
  151. SCM_API scm_t_ptob_descriptor *scm_ptobs;
  152. SCM_API long scm_numptob;
  153. SCM_API long scm_i_port_table_room;
  154. SCM_API SCM scm_markstream (SCM ptr);
  155. SCM_API scm_t_bits scm_make_port_type (char *name,
  156. int (*fill_input) (SCM port),
  157. void (*write) (SCM port,
  158. const void *data,
  159. size_t size));
  160. SCM_API void scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM));
  161. SCM_API void scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM));
  162. SCM_API void scm_set_port_print (scm_t_bits tc,
  163. int (*print) (SCM exp,
  164. SCM port,
  165. scm_print_state *pstate));
  166. SCM_API void scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
  167. SCM_API void scm_set_port_close (scm_t_bits tc, int (*close) (SCM));
  168. SCM_API void scm_set_port_flush (scm_t_bits tc,
  169. void (*flush) (SCM port));
  170. SCM_API void scm_set_port_end_input (scm_t_bits tc,
  171. void (*end_input) (SCM port,
  172. int offset));
  173. SCM_API void scm_set_port_seek (scm_t_bits tc,
  174. off_t (*seek) (SCM port,
  175. off_t OFFSET,
  176. int WHENCE));
  177. SCM_API void scm_set_port_truncate (scm_t_bits tc,
  178. void (*truncate) (SCM port,
  179. off_t length));
  180. SCM_API void scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM));
  181. SCM_API SCM scm_char_ready_p (SCM port);
  182. size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len);
  183. SCM_API SCM scm_drain_input (SCM port);
  184. SCM_API SCM scm_current_input_port (void);
  185. SCM_API SCM scm_current_output_port (void);
  186. SCM_API SCM scm_current_error_port (void);
  187. SCM_API SCM scm_current_load_port (void);
  188. SCM_API SCM scm_set_current_input_port (SCM port);
  189. SCM_API SCM scm_set_current_output_port (SCM port);
  190. SCM_API SCM scm_set_current_error_port (SCM port);
  191. SCM_API void scm_dynwind_current_input_port (SCM port);
  192. SCM_API void scm_dynwind_current_output_port (SCM port);
  193. SCM_API void scm_dynwind_current_error_port (SCM port);
  194. SCM_API SCM scm_new_port_table_entry (scm_t_bits tag);
  195. SCM_API void scm_remove_from_port_table (SCM port);
  196. SCM_API void scm_grow_port_cbuf (SCM port, size_t requested);
  197. SCM_API SCM scm_pt_size (void);
  198. SCM_API SCM scm_pt_member (SCM member);
  199. SCM_API void scm_port_non_buffer (scm_t_port *pt);
  200. SCM_API int scm_revealed_count (SCM port);
  201. SCM_API SCM scm_port_revealed (SCM port);
  202. SCM_API SCM scm_set_port_revealed_x (SCM port, SCM rcount);
  203. SCM_API long scm_mode_bits (char *modes);
  204. SCM_API SCM scm_port_mode (SCM port);
  205. SCM_API SCM scm_close_input_port (SCM port);
  206. SCM_API SCM scm_close_output_port (SCM port);
  207. SCM_API SCM scm_close_port (SCM port);
  208. SCM_API SCM scm_port_for_each (SCM proc);
  209. SCM_API void scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data);
  210. SCM_API SCM scm_input_port_p (SCM x);
  211. SCM_API SCM scm_output_port_p (SCM x);
  212. SCM_API SCM scm_port_p (SCM x);
  213. SCM_API SCM scm_port_closed_p (SCM port);
  214. SCM_API SCM scm_eof_object_p (SCM x);
  215. SCM_API SCM scm_force_output (SCM port);
  216. SCM_API SCM scm_flush_all_ports (void);
  217. SCM_API SCM scm_read_char (SCM port);
  218. SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
  219. SCM_API void scm_c_write (SCM port, const void *buffer, size_t size);
  220. SCM_API void scm_lfwrite (const char *ptr, size_t size, SCM port);
  221. SCM_API void scm_flush (SCM port);
  222. SCM_API void scm_end_input (SCM port);
  223. SCM_API int scm_fill_input (SCM port);
  224. SCM_API void scm_ungetc (int c, SCM port);
  225. SCM_API void scm_ungets (const char *s, int n, SCM port);
  226. SCM_API SCM scm_peek_char (SCM port);
  227. SCM_API SCM scm_unread_char (SCM cobj, SCM port);
  228. SCM_API SCM scm_unread_string (SCM str, SCM port);
  229. SCM_API SCM scm_seek (SCM object, SCM offset, SCM whence);
  230. SCM_API SCM scm_truncate_file (SCM object, SCM length);
  231. SCM_API SCM scm_port_line (SCM port);
  232. SCM_API SCM scm_set_port_line_x (SCM port, SCM line);
  233. SCM_API SCM scm_port_column (SCM port);
  234. SCM_API SCM scm_set_port_column_x (SCM port, SCM line);
  235. SCM_API SCM scm_port_filename (SCM port);
  236. SCM_API SCM scm_set_port_filename_x (SCM port, SCM filename);
  237. SCM_API int scm_port_print (SCM exp, SCM port, scm_print_state *);
  238. SCM_API void scm_print_port_mode (SCM exp, SCM port);
  239. SCM_API void scm_ports_prehistory (void);
  240. SCM_API SCM scm_void_port (char * mode_str);
  241. SCM_API SCM scm_sys_make_void_port (SCM mode);
  242. SCM_API void scm_init_ports (void);
  243. #if SCM_ENABLE_DEPRECATED==1
  244. SCM_API scm_t_port * scm_add_to_port_table (SCM port);
  245. #endif
  246. #ifdef GUILE_DEBUG
  247. SCM_API SCM scm_pt_size (void);
  248. SCM_API SCM scm_pt_member (SCM member);
  249. #endif /* GUILE_DEBUG */
  250. /* internal */
  251. SCM_API long scm_i_mode_bits (SCM modes);
  252. SCM_API void scm_i_dynwind_current_load_port (SCM port);
  253. #endif /* SCM_PORTS_H */
  254. /*
  255. Local Variables:
  256. c-file-style: "gnu"
  257. End:
  258. */