revert 4a2ac0623c3dabb2c8b9d38c27b837dcb2c7fe4e
[bpt/guile.git] / libguile / ports.h
index 796ccf8..6a669b6 100644 (file)
@@ -3,33 +3,34 @@
 #ifndef SCM_PORTS_H
 #define SCM_PORTS_H
 
-/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004 Free Software Foundation, Inc.
+/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004,
+ *   2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
  */
 
 \f
 
 #include "libguile/__scm.h"
 
+#include <unistd.h>
 #include "libguile/print.h"
 #include "libguile/struct.h"
 #include "libguile/threads.h"
-
-/* Not sure if this is a good idea.  We need it for off_t.  */
-#include <sys/types.h>
+#include "libguile/strings.h"
 
 \f
 
@@ -47,7 +48,6 @@ typedef enum scm_t_port_rw_active {
 typedef struct 
 {
   SCM port;                    /* Link back to the port object.  */
-  long entry;                  /* Index in port table. */
   int revealed;                        /* 0 not revealed, > 1 revealed.
                                 * Revealed ports do not get GC'd.
                                 */
@@ -58,6 +58,10 @@ typedef struct
   long line_number;            /* debugging support.  */
   int column_number;           /* debugging support.  */
 
+  /* Character encoding support  */
+  char *encoding;
+  scm_t_string_failed_conversion_handler ilseq_handler;
+
   /* port buffers.  the buffer(s) are set up for all ports.  
      in the case of string ports, the buffer is the string itself.
      in the case of unbuffered file ports, the buffer is a
@@ -70,7 +74,7 @@ typedef struct
   unsigned char *read_buf;     /* buffer start.  */
   const unsigned char *read_pos;/* the next unread char.  */
   unsigned char *read_end;      /* pointer to last buffered char + 1.  */
-  off_t read_buf_size;         /* size of the buffer.  */
+  scm_t_off read_buf_size;             /* size of the buffer.  */
 
   /* when chars are put back into the buffer, e.g., using peek-char or
      unread-string, the read-buffer pointers are switched to cbuf.
@@ -79,7 +83,7 @@ typedef struct
   unsigned char *saved_read_buf;
   const unsigned char *saved_read_pos;
   unsigned char *saved_read_end;
-  off_t saved_read_buf_size;
+  scm_t_off saved_read_buf_size;
 
   /* write requests are saved into this buffer at write_pos until it
      reaches write_buf + write_buf_size, then the ptob flush is
@@ -88,7 +92,7 @@ typedef struct
   unsigned char *write_buf;     /* buffer start.  */
   unsigned char *write_pos;     /* pointer to last buffered char + 1.  */
   unsigned char *write_end;     /* pointer to end of buffer + 1.  */
-  off_t write_buf_size;                /* size of the buffer.  */
+  scm_t_off write_buf_size;            /* size of the buffer.  */
 
   unsigned char shortbuf;       /* buffer for "unbuffered" streams.  */
 
@@ -107,17 +111,22 @@ typedef struct
   /* a buffer for un-read chars and strings.  */
   unsigned char *putback_buf;
   size_t putback_buf_size;        /* allocated size of putback_buf.  */
+
+  /* input/output iconv conversion descriptors */
+  void *input_cd;
+  void *output_cd;
 } scm_t_port;
 
-SCM_API scm_t_port **scm_i_port_table;
-SCM_API long scm_i_port_table_size; /* Number of ports in scm_i_port_table.  */
-SCM_API scm_t_mutex scm_i_port_table_mutex;
+
+SCM_INTERNAL scm_i_pthread_mutex_t scm_i_port_table_mutex;
+SCM_INTERNAL SCM scm_i_port_weak_hash;
+
 
 #define SCM_READ_BUFFER_EMPTY_P(c_port) (c_port->read_pos >= c_port->read_end)
 
 \f
 
-#define SCM_EOF_OBJECT_P(x) (SCM_EQ_P ((x), SCM_EOF_VAL))
+#define SCM_EOF_OBJECT_P(x) (scm_is_eq ((x), SCM_EOF_VAL))
 
 /* PORT FLAGS
  * A set of flags characterizes a port.
@@ -156,9 +165,14 @@ SCM_API scm_t_mutex scm_i_port_table_mutex;
 #define SCM_REVEALED(x)           (SCM_PTAB_ENTRY(x)->revealed)
 #define SCM_SETREVEALED(x, s)      (SCM_PTAB_ENTRY(x)->revealed = (s))
 
-#define SCM_INCLINE(port)      {SCM_LINUM (port) += 1; SCM_COL (port) = 0;}
-#define SCM_INCCOL(port)       {SCM_COL (port) += 1;}
-#define SCM_TABCOL(port)       {SCM_COL (port) += 8 - SCM_COL (port) % 8;}
+#define SCM_INCLINE(port)      do {SCM_LINUM (port) += 1; SCM_COL (port) = 0;} while (0)
+#define SCM_ZEROCOL(port)      do {SCM_COL (port) = 0;} while (0)
+#define SCM_INCCOL(port)       do {SCM_COL (port) += 1;} while (0)
+#define SCM_DECCOL(port)       do {if (SCM_COL (port) > 0) SCM_COL (port) -= 1;} while (0)
+#define SCM_TABCOL(port)       do {SCM_COL (port) += 8 - SCM_COL (port) % 8;} while (0)
+
+/* Maximum number of port types.  */
+#define SCM_I_MAX_PORT_TYPE_COUNT  256
 
 \f
 
@@ -179,8 +193,8 @@ typedef struct scm_t_ptob_descriptor
   int (*fill_input) (SCM port);
   int (*input_waiting) (SCM port);
 
-  off_t (*seek) (SCM port, off_t OFFSET, int WHENCE);
-  void (*truncate) (SCM port, off_t length);
+  scm_t_off (*seek) (SCM port, scm_t_off OFFSET, int WHENCE);
+  void (*truncate) (SCM port, scm_t_off length);
 
 } scm_t_ptob_descriptor;
 
@@ -191,9 +205,11 @@ typedef struct scm_t_ptob_descriptor
 
 \f
 
+/* Hey you!  Yes you, reading the header file!  We're going to deprecate
+   scm_ptobs in 2.2, so please don't write any new code that uses it.
+   Thanks.  */
 SCM_API scm_t_ptob_descriptor *scm_ptobs;
 SCM_API long scm_numptob;
-SCM_API long scm_i_port_table_room;
 
 \f
 
@@ -203,28 +219,28 @@ SCM_API scm_t_bits scm_make_port_type (char *name,
                                       void (*write) (SCM port, 
                                                      const void *data,
                                                      size_t size));
-SCM_API void scm_set_port_mark (long tc, SCM (*mark) (SCM));
-SCM_API void scm_set_port_free (long tc, size_t (*free) (SCM));
-SCM_API void scm_set_port_print (long tc,
+SCM_API void scm_set_port_mark (scm_t_bits tc, SCM (*mark) (SCM));
+SCM_API void scm_set_port_free (scm_t_bits tc, size_t (*free) (SCM));
+SCM_API void scm_set_port_print (scm_t_bits tc,
                                 int (*print) (SCM exp,
                                               SCM port,
                                               scm_print_state *pstate));
-SCM_API void scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM));
-SCM_API void scm_set_port_close (long tc, int (*close) (SCM));
+SCM_API void scm_set_port_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM));
+SCM_API void scm_set_port_close (scm_t_bits tc, int (*close) (SCM));
 
-SCM_API void scm_set_port_flush (long tc, 
+SCM_API void scm_set_port_flush (scm_t_bits tc, 
                                 void (*flush) (SCM port));
-SCM_API void scm_set_port_end_input (long tc,
+SCM_API void scm_set_port_end_input (scm_t_bits tc,
                                     void (*end_input) (SCM port,
                                                        int offset));
-SCM_API void scm_set_port_seek (long tc,
-                               off_t (*seek) (SCM port,
-                                              off_t OFFSET,
-                                              int WHENCE));
-SCM_API void scm_set_port_truncate (long tc,
+SCM_API void scm_set_port_seek (scm_t_bits tc,
+                               scm_t_off (*seek) (SCM port,
+                                                  scm_t_off OFFSET,
+                                                  int WHENCE));
+SCM_API void scm_set_port_truncate (scm_t_bits tc,
                                    void (*truncate) (SCM port,
-                                                     off_t length));
-SCM_API void scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM));
+                                                     scm_t_off length));
+SCM_API void scm_set_port_input_waiting (scm_t_bits tc, int (*input_waiting) (SCM));
 SCM_API SCM scm_char_ready_p (SCM port);
 size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len);
 SCM_API SCM scm_drain_input (SCM port);
@@ -235,11 +251,10 @@ SCM_API SCM scm_current_load_port (void);
 SCM_API SCM scm_set_current_input_port (SCM port);
 SCM_API SCM scm_set_current_output_port (SCM port);
 SCM_API SCM scm_set_current_error_port (SCM port);
-SCM_API void scm_with_current_input_port (SCM port);
-SCM_API void scm_with_current_output_port (SCM port);
-SCM_API void scm_with_current_error_port (SCM port);
+SCM_API void scm_dynwind_current_input_port (SCM port);
+SCM_API void scm_dynwind_current_output_port (SCM port);
+SCM_API void scm_dynwind_current_error_port (SCM port);
 SCM_API SCM scm_new_port_table_entry (scm_t_bits tag);
-SCM_API void scm_remove_from_port_table (SCM port);
 SCM_API void scm_grow_port_cbuf (SCM port, size_t requested);
 SCM_API SCM scm_pt_size (void);
 SCM_API SCM scm_pt_member (SCM member);
@@ -262,16 +277,17 @@ SCM_API SCM scm_eof_object_p (SCM x);
 SCM_API SCM scm_force_output (SCM port);
 SCM_API SCM scm_flush_all_ports (void);
 SCM_API SCM scm_read_char (SCM port);
-SCM_API void scm_putc (char c, SCM port);
-SCM_API void scm_puts (const char *str_data, SCM port);
+SCM_API scm_t_wchar scm_getc (SCM port);
 SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
 SCM_API void scm_c_write (SCM port, const void *buffer, size_t size);
 SCM_API void scm_lfwrite (const char *ptr, size_t size, SCM port);
+SCM_INTERNAL void scm_lfwrite_substr (SCM str, size_t start, size_t end,
+                                     SCM port);
 SCM_API void scm_flush (SCM port);
 SCM_API void scm_end_input (SCM port);
 SCM_API int scm_fill_input (SCM port);
-SCM_API int scm_getc (SCM port);
-SCM_API void scm_ungetc (int c, SCM port);
+SCM_INTERNAL void scm_unget_byte (int c, SCM port); 
+SCM_API void scm_ungetc (scm_t_wchar c, SCM port);
 SCM_API void scm_ungets (const char *s, int n, SCM port);
 SCM_API SCM scm_peek_char (SCM port);
 SCM_API SCM scm_unread_char (SCM cobj, SCM port);
@@ -284,16 +300,24 @@ SCM_API SCM scm_port_column (SCM port);
 SCM_API SCM scm_set_port_column_x (SCM port, SCM line);
 SCM_API SCM scm_port_filename (SCM port);
 SCM_API SCM scm_set_port_filename_x (SCM port, SCM filename);
+SCM_INTERNAL const char *scm_i_default_port_encoding (void);
+SCM_INTERNAL void scm_i_set_default_port_encoding (const char *);
+SCM_INTERNAL void scm_i_set_port_encoding_x (SCM port, const char *str);
+SCM_API SCM scm_port_encoding (SCM port);
+SCM_API SCM scm_set_port_encoding_x (SCM port, SCM encoding);
+SCM_INTERNAL scm_t_string_failed_conversion_handler scm_i_get_conversion_strategy (SCM port);
+SCM_INTERNAL void scm_i_set_conversion_strategy_x (SCM port, 
+                                                  scm_t_string_failed_conversion_handler h);
+SCM_API SCM scm_port_conversion_strategy (SCM port);
+SCM_API SCM scm_set_port_conversion_strategy_x (SCM port, SCM behavior);
 SCM_API int scm_port_print (SCM exp, SCM port, scm_print_state *);
 SCM_API void scm_print_port_mode (SCM exp, SCM port);
-SCM_API void scm_ports_prehistory (void);
 SCM_API SCM scm_void_port (char * mode_str);
 SCM_API SCM scm_sys_make_void_port (SCM mode);
-SCM_API void scm_init_ports (void);
-
+SCM_INTERNAL void scm_init_ports (void);
 
 #if SCM_ENABLE_DEPRECATED==1
-SCM_API scm_t_port * scm_add_to_port_table (SCM port);
+SCM_DEPRECATED scm_t_port * scm_add_to_port_table (SCM port);
 #endif
 
 #ifdef GUILE_DEBUG
@@ -301,6 +325,12 @@ SCM_API SCM scm_pt_size (void);
 SCM_API SCM scm_pt_member (SCM member);
 #endif /* GUILE_DEBUG */
 
+/* internal */
+
+SCM_INTERNAL long scm_i_mode_bits (SCM modes);
+SCM_INTERNAL void scm_i_dynwind_current_load_port (SCM port);
+
+
 #endif  /* SCM_PORTS_H */
 
 /*