ports.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* classes: h_files */
  2. #ifndef PORTSH
  3. #define PORTSH
  4. /* Copyright (C) 1995,1996,1997,1998,1999, 2000, 2002 Free Software Foundation, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this software; see the file COPYING. If not, write to
  18. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307 USA
  20. *
  21. * As a special exception, the Free Software Foundation gives permission
  22. * for additional uses of the text contained in its release of GUILE.
  23. *
  24. * The exception is that, if you link the GUILE library with other files
  25. * to produce an executable, this does not by itself cause the
  26. * resulting executable to be covered by the GNU General Public License.
  27. * Your use of that executable is in no way restricted on account of
  28. * linking the GUILE library code into it.
  29. *
  30. * This exception does not however invalidate any other reasons why
  31. * the executable file might be covered by the GNU General Public License.
  32. *
  33. * This exception applies only to the code released by the
  34. * Free Software Foundation under the name GUILE. If you copy
  35. * code from other Free Software Foundation releases into a copy of
  36. * GUILE, as the General Public License permits, the exception does
  37. * not apply to the code that you add in this way. To avoid misleading
  38. * anyone as to the status of such modified files, you must delete
  39. * this exception notice from them.
  40. *
  41. * If you write modifications of your own for GUILE, it is your choice
  42. * whether to permit this exception to apply to your modifications.
  43. * If you do not wish that, delete this exception notice. */
  44. #include "libguile/__scm.h"
  45. #include "libguile/print.h"
  46. #include "libguile/struct.h"
  47. /* Not sure if this is a good idea. We need it for off_t. */
  48. #include <sys/types.h>
  49. #define SCM_INITIAL_PUTBACK_BUF_SIZE 4
  50. /* values for the rw_active flag. */
  51. enum scm_port_rw_active {
  52. SCM_PORT_NEITHER = 0,
  53. SCM_PORT_READ = 1,
  54. SCM_PORT_WRITE = 2
  55. };
  56. /* C representation of a Scheme port. */
  57. typedef struct
  58. {
  59. SCM port; /* Link back to the port object. */
  60. int entry; /* Index in port table. */
  61. int revealed; /* 0 not revealed, > 1 revealed.
  62. * Revealed ports do not get GC'd.
  63. */
  64. /* data for the underlying port implementation as a raw C value. */
  65. scm_bits_t stream;
  66. SCM file_name; /* debugging support. */
  67. int line_number; /* debugging support. */
  68. int column_number; /* debugging support. */
  69. /* port buffers. the buffer(s) are set up for all ports.
  70. in the case of string ports, the buffer is the string itself.
  71. in the case of unbuffered file ports, the buffer is a
  72. single char: shortbuf. */
  73. /* this buffer is filled from read_buf to read_end using the ptob
  74. buffer_fill. then input requests are taken from read_pos until
  75. it reaches read_end. */
  76. unsigned char *read_buf; /* buffer start. */
  77. const unsigned char *read_pos;/* the next unread char. */
  78. unsigned char *read_end; /* pointer to last buffered char + 1. */
  79. off_t read_buf_size; /* size of the buffer. */
  80. /* when chars are put back into the buffer, e.g., using peek-char or
  81. unread-string, the read-buffer pointers are switched to cbuf.
  82. the original pointers are saved here and restored when the put-back
  83. chars have been consumed. */
  84. unsigned char *saved_read_buf;
  85. const unsigned char *saved_read_pos;
  86. unsigned char *saved_read_end;
  87. off_t saved_read_buf_size;
  88. /* write requests are saved into this buffer at write_pos until it
  89. reaches write_buf + write_buf_size, then the ptob flush is
  90. called. */
  91. unsigned char *write_buf; /* buffer start. */
  92. unsigned char *write_pos; /* pointer to last buffered char + 1. */
  93. unsigned char *write_end; /* pointer to end of buffer + 1. */
  94. off_t write_buf_size; /* size of the buffer. */
  95. unsigned char shortbuf; /* buffer for "unbuffered" streams. */
  96. int rw_random; /* true if the port is random access.
  97. implies that the buffers must be
  98. flushed before switching between
  99. reading and writing, seeking, etc. */
  100. enum scm_port_rw_active rw_active; /* for random access ports,
  101. indicates which of the buffers
  102. is currently in use. can be
  103. SCM_PORT_WRITE, SCM_PORT_READ,
  104. or SCM_PORT_NEITHER. */
  105. /* a buffer for un-read chars and strings. */
  106. unsigned char *putback_buf;
  107. int putback_buf_size; /* allocated size of putback_buf. */
  108. } scm_port;
  109. extern scm_port **scm_port_table;
  110. extern int scm_port_table_size; /* Number of ports in scm_port_table. */
  111. #define SCM_EOF_OBJECT_P(x) (SCM_EQ_P ((x), SCM_EOF_VAL))
  112. /* PORT FLAGS
  113. * A set of flags characterizes a port.
  114. * Note that we reserve the bits 1 << 24 and above for use by the
  115. * routines in the port's scm_ptobfuns structure.
  116. */
  117. #define SCM_OPN (1L<<16) /* Is the port open? */
  118. #define SCM_RDNG (2L<<16) /* Is it a readable port? */
  119. #define SCM_WRTNG (4L<<16) /* Is it writable? */
  120. #define SCM_BUF0 (8L<<16) /* Is it unbuffered? */
  121. #define SCM_BUFLINE (64L<<16) /* Is it line-buffered? */
  122. #define SCM_PORTP(x) (SCM_NIMP(x) && (SCM_TYP7(x)==scm_tc7_port))
  123. #define SCM_OPPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_OPN) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN)))
  124. #define SCM_OPINPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_OPN | SCM_RDNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_RDNG)))
  125. #define SCM_OPOUTPORTP(x) (SCM_NIMP(x) && (((0x7f | SCM_OPN | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_OPN | SCM_WRTNG)))
  126. #define SCM_INPUT_PORT_P(x) \
  127. (SCM_NIMP(x) \
  128. && (((0x7f | SCM_RDNG) & SCM_CELL_WORD_0(x)) == (scm_tc7_port | SCM_RDNG)))
  129. #define SCM_OUTPUT_PORT_P(x) \
  130. (SCM_NIMP(x) \
  131. && (((0x7f | SCM_WRTNG) & SCM_CELL_WORD_0(x))==(scm_tc7_port | SCM_WRTNG)))
  132. #define SCM_OPENP(x) (SCM_NIMP(x) && (SCM_OPN & SCM_CELL_WORD_0 (x)))
  133. #define SCM_CLOSEDP(x) (!SCM_OPENP(x))
  134. #define SCM_PTAB_ENTRY(x) ((scm_port *) SCM_CELL_WORD_1 (x))
  135. #define SCM_SETPTAB_ENTRY(x,ent) (SCM_SET_CELL_WORD_1 ((x), (scm_bits_t) (ent)))
  136. #define SCM_STREAM(x) (SCM_PTAB_ENTRY(x)->stream)
  137. #define SCM_SETSTREAM(x,s) (SCM_PTAB_ENTRY(x)->stream = (scm_bits_t) (s))
  138. #define SCM_FILENAME(x) (SCM_PTAB_ENTRY(x)->file_name)
  139. #define SCM_LINUM(x) (SCM_PTAB_ENTRY(x)->line_number)
  140. #define SCM_COL(x) (SCM_PTAB_ENTRY(x)->column_number)
  141. #define SCM_REVEALED(x) (SCM_PTAB_ENTRY(x)->revealed)
  142. #define SCM_SETREVEALED(x,s) (SCM_PTAB_ENTRY(x)->revealed = (s))
  143. #define SCM_INCLINE(port) {SCM_LINUM (port) += 1; SCM_COL (port) = 0;}
  144. #define SCM_INCCOL(port) {SCM_COL (port) += 1;}
  145. #define SCM_TABCOL(port) {SCM_COL (port) += 8 - SCM_COL (port) % 8;}
  146. /* port-type description. */
  147. typedef struct scm_ptob_descriptor
  148. {
  149. char *name;
  150. SCM (*mark) (SCM);
  151. scm_sizet (*free) (SCM);
  152. int (*print) (SCM exp, SCM port, scm_print_state *pstate);
  153. SCM (*equalp) (SCM, SCM);
  154. int (*close) (SCM port);
  155. void (*write) (SCM port, const void *data, size_t size);
  156. void (*flush) (SCM port);
  157. void (*end_input) (SCM port, int offset);
  158. int (*fill_input) (SCM port);
  159. int (*input_waiting) (SCM port);
  160. off_t (*seek) (SCM port, off_t OFFSET, int WHENCE);
  161. void (*truncate) (SCM port, off_t length);
  162. } scm_ptob_descriptor;
  163. #define SCM_TC2PTOBNUM(x) (0x0ff & ((x) >> 8))
  164. #define SCM_PTOBNUM(x) (SCM_TC2PTOBNUM (SCM_CELL_TYPE (x)))
  165. /* SCM_PTOBNAME can be 0 if name is missing */
  166. #define SCM_PTOBNAME(ptobnum) scm_ptobs[ptobnum].name
  167. extern scm_ptob_descriptor *scm_ptobs;
  168. extern int scm_numptob;
  169. extern int scm_port_table_room;
  170. extern SCM scm_markstream (SCM ptr);
  171. extern long scm_make_port_type (char *name,
  172. int (*fill_input) (SCM port),
  173. void (*write) (SCM port, const void *data,
  174. size_t size));
  175. extern void scm_set_port_mark (long tc, SCM (*mark) (SCM));
  176. extern void scm_set_port_free (long tc, scm_sizet (*free) (SCM));
  177. extern void scm_set_port_print (long tc,
  178. int (*print) (SCM exp,
  179. SCM port,
  180. scm_print_state *pstate));
  181. extern void scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM));
  182. extern void scm_set_port_close (long tc, int (*close) (SCM));
  183. extern void scm_set_port_flush (long tc,
  184. void (*flush) (SCM port));
  185. extern void scm_set_port_end_input (long tc,
  186. void (*end_input) (SCM port,
  187. int offset));
  188. extern void scm_set_port_seek (long tc,
  189. off_t (*seek) (SCM port,
  190. off_t OFFSET,
  191. int WHENCE));
  192. extern void scm_set_port_truncate (long tc,
  193. void (*truncate) (SCM port,
  194. off_t length));
  195. extern void scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM));
  196. extern SCM scm_char_ready_p (SCM port);
  197. extern SCM scm_drain_input (SCM port);
  198. extern SCM scm_current_input_port (void);
  199. extern SCM scm_current_output_port (void);
  200. extern SCM scm_current_error_port (void);
  201. extern SCM scm_current_load_port (void);
  202. extern SCM scm_set_current_input_port (SCM port);
  203. extern SCM scm_set_current_output_port (SCM port);
  204. extern SCM scm_set_current_error_port (SCM port);
  205. extern scm_port * scm_add_to_port_table (SCM port);
  206. extern void scm_remove_from_port_table (SCM port);
  207. extern void scm_grow_port_cbuf (SCM port, size_t requested);
  208. extern SCM scm_pt_size (void);
  209. extern SCM scm_pt_member (SCM member);
  210. extern void scm_port_non_buffer (scm_port *pt);
  211. extern int scm_revealed_count (SCM port);
  212. extern SCM scm_port_revealed (SCM port);
  213. extern SCM scm_set_port_revealed_x (SCM port, SCM rcount);
  214. extern long scm_mode_bits (char *modes);
  215. extern SCM scm_port_mode (SCM port);
  216. extern SCM scm_close_input_port (SCM port);
  217. extern SCM scm_close_output_port (SCM port);
  218. extern SCM scm_close_port (SCM port);
  219. extern SCM scm_close_all_ports_except (SCM ports);
  220. extern SCM scm_input_port_p (SCM x);
  221. extern SCM scm_output_port_p (SCM x);
  222. extern SCM scm_port_closed_p (SCM port);
  223. extern SCM scm_eof_object_p (SCM x);
  224. extern SCM scm_force_output (SCM port);
  225. extern SCM scm_flush_all_ports (void);
  226. extern SCM scm_read_char (SCM port);
  227. extern void scm_putc (char c, SCM port);
  228. extern void scm_puts (const char *str_data, SCM port);
  229. extern void scm_lfwrite (const char *ptr, scm_sizet size, SCM port);
  230. extern void scm_flush (SCM port);
  231. extern void scm_end_input (SCM port);
  232. extern int scm_fill_input (SCM port);
  233. extern int scm_getc (SCM port);
  234. extern void scm_ungetc (int c, SCM port);
  235. extern void scm_ungets (const char *s, int n, SCM port);
  236. extern SCM scm_peek_char (SCM port);
  237. extern SCM scm_unread_char (SCM cobj, SCM port);
  238. extern SCM scm_unread_string (SCM str, SCM port);
  239. extern SCM scm_seek (SCM object, SCM offset, SCM whence);
  240. extern SCM scm_truncate_file (SCM object, SCM length);
  241. extern SCM scm_port_line (SCM port);
  242. extern SCM scm_set_port_line_x (SCM port, SCM line);
  243. extern SCM scm_port_column (SCM port);
  244. extern SCM scm_set_port_column_x (SCM port, SCM line);
  245. extern SCM scm_port_filename (SCM port);
  246. extern SCM scm_set_port_filename_x (SCM port, SCM filename);
  247. extern int scm_port_print (SCM exp, SCM port, scm_print_state *);
  248. extern void scm_print_port_mode (SCM exp, SCM port);
  249. extern void scm_ports_prehistory (void);
  250. extern SCM scm_void_port (char * mode_str);
  251. extern SCM scm_sys_make_void_port (SCM mode);
  252. extern void scm_init_ports (void);
  253. #ifdef GUILE_DEBUG
  254. extern SCM scm_pt_size (void);
  255. extern SCM scm_pt_member (SCM member);
  256. #endif /* GUILE_DEBUG */
  257. #if (SCM_DEBUG_DEPRECATED == 0)
  258. /* #define SCM_CRDY (32L<<16) obsolete, for pushed back characters */
  259. #define SCM_INPORTP(x) SCM_INPUT_PORT_P (x)
  260. #define SCM_OUTPORTP(x) SCM_OUTPUT_PORT_P (x)
  261. #endif /* SCM_DEBUG_DEPRECATED == 0 */
  262. #endif /* PORTSH */
  263. /*
  264. Local Variables:
  265. c-file-style: "gnu"
  266. End:
  267. */