* Deprecated scm_makfromstr and added scm_mem2string as a replacement.
[bpt/guile.git] / libguile / ports.c
index 9bc8168..49902d7 100644 (file)
@@ -86,7 +86,7 @@
  * Indexes into this table are used when generating type
  * tags for smobjects (if you know a tag you can get an index and conversely).
  */
-scm_ptob_descriptor_t *scm_ptobs;
+scm_t_ptob_descriptor *scm_ptobs;
 long scm_numptob;
 
 /* GC marker for a port with stream of SCM type.  */
@@ -108,16 +108,16 @@ scm_markstream (SCM ptr)
  */
 
 static void
-flush_port_default (SCM port)
+flush_port_default (SCM port SCM_UNUSED)
 {
 }
 
 static void
-end_input_default (SCM port, int offset)
+end_input_default (SCM port SCM_UNUSED, int offset SCM_UNUSED)
 {
 }
 
-scm_bits_t
+scm_t_bits
 scm_make_port_type (char *name,
                    int (*fill_input) (SCM port),
                    void (*write) (SCM port, const void *data, size_t size))
@@ -128,10 +128,10 @@ scm_make_port_type (char *name,
   SCM_DEFER_INTS;
   SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs,
                                       (1 + scm_numptob)
-                                      * sizeof (scm_ptob_descriptor_t)));
+                                      * sizeof (scm_t_ptob_descriptor)));
   if (tmp)
     {
-      scm_ptobs = (scm_ptob_descriptor_t *) tmp;
+      scm_ptobs = (scm_t_ptob_descriptor *) tmp;
 
       scm_ptobs[scm_numptob].name = name;
       scm_ptobs[scm_numptob].mark = 0;
@@ -246,7 +246,7 @@ SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0,
            "interactive port that has no ready characters.}")
 #define FUNC_NAME s_scm_char_ready_p
 {
-  scm_port_t *pt;
+  scm_t_port *pt;
 
   if (SCM_UNBNDP (port))
     port = scm_cur_inp;
@@ -264,7 +264,7 @@ SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0,
     return SCM_BOOL_T;
   else
     {
-      scm_ptob_descriptor_t *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
+      scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
       
       if (ptob->input_waiting)
        return SCM_BOOL(ptob->input_waiting (port));
@@ -278,7 +278,7 @@ SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0,
    into memory starting at dest.  returns the number of chars moved.  */
 size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len)
 {
-  scm_port_t *pt = SCM_PTAB_ENTRY (port);
+  scm_t_port *pt = SCM_PTAB_ENTRY (port);
   size_t chars_read = 0;
   size_t from_buf = min (pt->read_end - pt->read_pos, read_len);
 
@@ -313,7 +313,7 @@ SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0,
 #define FUNC_NAME s_scm_drain_input
 {
   SCM result;
-  scm_port_t *pt = SCM_PTAB_ENTRY (port);
+  scm_t_port *pt = SCM_PTAB_ENTRY (port);
   long count;
 
   SCM_VALIDATE_OPINPORT (1,port);
@@ -422,35 +422,35 @@ SCM_DEFINE (scm_set_current_error_port, "set-current-error-port", 1, 0, 0,
 \f
 /* The port table --- an array of pointers to ports.  */
 
-scm_port_t **scm_port_table;
+scm_t_port **scm_t_portable;
 
-long scm_port_table_size = 0;  /* Number of ports in scm_port_table.  */
-long scm_port_table_room = 20; /* Size of the array.  */
+long scm_t_portable_size = 0;  /* Number of ports in scm_t_portable.  */
+long scm_t_portable_room = 20; /* Size of the array.  */
 
 /* Add a port to the table.  */
 
-scm_port_t *
+scm_t_port *
 scm_add_to_port_table (SCM port)
 #define FUNC_NAME "scm_add_to_port_table"
 {
-  scm_port_t *entry;
+  scm_t_port *entry;
 
-  if (scm_port_table_size == scm_port_table_room)
+  if (scm_t_portable_size == scm_t_portable_room)
     {
       /* initial malloc is in gc.c.  this doesn't use scm_must_malloc etc.,
         since it can never be freed during gc.  */
-      void *newt = realloc ((char *) scm_port_table,
-                           (size_t) (sizeof (scm_port_t *)
-                                        * scm_port_table_room * 2));
+      void *newt = realloc ((char *) scm_t_portable,
+                           (size_t) (sizeof (scm_t_port *)
+                                        * scm_t_portable_room * 2));
       if (newt == NULL)
        scm_memory_error ("scm_add_to_port_table");
-      scm_port_table = (scm_port_t **) newt;
-      scm_port_table_room *= 2;
+      scm_t_portable = (scm_t_port **) newt;
+      scm_t_portable_room *= 2;
     }
-  entry = (scm_port_t *) scm_must_malloc (sizeof (scm_port_t), FUNC_NAME);
+  entry = (scm_t_port *) scm_must_malloc (sizeof (scm_t_port), FUNC_NAME);
 
   entry->port = port;
-  entry->entry = scm_port_table_size;
+  entry->entry = scm_t_portable_size;
   entry->revealed = 0;
   entry->stream = 0;
   entry->file_name = SCM_BOOL_F;
@@ -461,8 +461,8 @@ scm_add_to_port_table (SCM port)
   entry->rw_active = SCM_PORT_NEITHER;
   entry->rw_random = 0;
 
-  scm_port_table[scm_port_table_size] = entry;
-  scm_port_table_size++;
+  scm_t_portable[scm_t_portable_size] = entry;
+  scm_t_portable_size++;
 
   return entry;
 }
@@ -474,23 +474,23 @@ void
 scm_remove_from_port_table (SCM port)
 #define FUNC_NAME "scm_remove_from_port_table"
 {
-  scm_port_t *p = SCM_PTAB_ENTRY (port);
+  scm_t_port *p = SCM_PTAB_ENTRY (port);
   long i = p->entry;
 
-  if (i >= scm_port_table_size)
+  if (i >= scm_t_portable_size)
     SCM_MISC_ERROR ("Port not in table: ~S", SCM_LIST1 (port));
   if (p->putback_buf)
     scm_must_free (p->putback_buf);
   scm_must_free (p);
   /* Since we have just freed slot i we can shrink the table by moving
      the last entry to that slot... */
-  if (i < scm_port_table_size - 1)
+  if (i < scm_t_portable_size - 1)
     {
-      scm_port_table[i] = scm_port_table[scm_port_table_size - 1];
-      scm_port_table[i]->entry = i;
+      scm_t_portable[i] = scm_t_portable[scm_t_portable_size - 1];
+      scm_t_portable[i]->entry = i;
     }
   SCM_SETPTAB_ENTRY (port, 0);
-  scm_port_table_size--;
+  scm_t_portable_size--;
 }
 #undef FUNC_NAME
 
@@ -504,7 +504,7 @@ SCM_DEFINE (scm_pt_size, "pt-size", 0, 0, 0,
            "is only included in @code{--enable-guile-debug} builds.")
 #define FUNC_NAME s_scm_pt_size
 {
-  return SCM_MAKINUM (scm_port_table_size);
+  return SCM_MAKINUM (scm_t_portable_size);
 }
 #undef FUNC_NAME
 
@@ -517,16 +517,16 @@ SCM_DEFINE (scm_pt_member, "pt-member", 1, 0, 0,
 {
   long i;
   SCM_VALIDATE_INUM_COPY (1,index,i);
-  if (i < 0 || i >= scm_port_table_size)
+  if (i < 0 || i >= scm_t_portable_size)
     return SCM_BOOL_F;
   else
-    return scm_port_table[i]->port;
+    return scm_t_portable[i]->port;
 }
 #undef FUNC_NAME
 #endif
 
 void
-scm_port_non_buffer (scm_port_t *pt)
+scm_port_non_buffer (scm_t_port *pt)
 {
   pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
   pt->write_buf = pt->write_pos = &pt->shortbuf;
@@ -627,7 +627,7 @@ SCM_DEFINE (scm_port_mode, "port-mode", 1, 0, 0,
     strcpy (modes, "w");
   if (SCM_CELL_WORD_0 (port) & SCM_BUF0)
     strcat (modes, "0");
-  return scm_makfromstr (modes, strlen (modes), 0);
+  return scm_mem2string (modes, strlen (modes));
 }
 #undef FUNC_NAME
 
@@ -664,7 +664,7 @@ SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0,
     rv = 0;
   scm_remove_from_port_table (port);
   SCM_CLR_PORT_OPEN_FLAG (port);
-  return SCM_NEGATE_BOOL (rv < 0);
+  return SCM_BOOL (rv >= 0);
 }
 #undef FUNC_NAME
 
@@ -725,8 +725,8 @@ SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0,
   SCM_DEFER_INTS;
   scm_block_gc++;
   ports = SCM_EOL;
-  for (i = 0; i < scm_port_table_size; i++)
-    ports = scm_cons (scm_port_table[i]->port, ports);
+  for (i = 0; i < scm_t_portable_size; i++)
+    ports = scm_cons (scm_t_portable[i]->port, ports);
   scm_block_gc--;
   SCM_ALLOW_INTS;
 
@@ -754,13 +754,13 @@ SCM_DEFINE (scm_close_all_ports_except, "close-all-ports-except", 0, 0, 1,
 {
   long i = 0;
   SCM_VALIDATE_REST_ARGUMENT (ports);
-  while (i < scm_port_table_size)
+  while (i < scm_t_portable_size)
     {
-      SCM thisport = scm_port_table[i]->port;
+      SCM thisport = scm_t_portable[i]->port;
       int found = 0;
       SCM ports_ptr = ports;
 
-      while (SCM_NNULLP (ports_ptr))
+      while (!SCM_NULLP (ports_ptr))
        {
          SCM port = SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr));
          if (i == 0)
@@ -791,9 +791,7 @@ SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0,
            "@code{port?}.")
 #define FUNC_NAME s_scm_input_port_p
 {
-  if (SCM_IMP (x))
-    return SCM_BOOL_F;
-  return SCM_BOOL(SCM_INPUT_PORT_P (x));
+  return SCM_BOOL (SCM_INPUT_PORT_P (x));
 }
 #undef FUNC_NAME
 
@@ -804,11 +802,8 @@ SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0,
            "@code{port?}.")
 #define FUNC_NAME s_scm_output_port_p
 {
-  if (SCM_IMP (x))
-    return SCM_BOOL_F;
-  if (SCM_PORT_WITH_PS_P (x))
-    x = SCM_PORT_WITH_PS_PORT (x);
-  return SCM_BOOL(SCM_OUTPUT_PORT_P (x));
+  SCM_COERCE_OUTPORT (x);
+  return SCM_BOOL (SCM_OUTPUT_PORT_P (x));
 }
 #undef FUNC_NAME
 
@@ -830,7 +825,7 @@ SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0,
 #define FUNC_NAME s_scm_port_closed_p
 {
   SCM_VALIDATE_PORT (1,port);
-  return SCM_NEGATE_BOOL(SCM_OPPORTP (port));
+  return SCM_BOOL (!SCM_OPPORTP (port));
 }
 #undef FUNC_NAME
 
@@ -874,10 +869,10 @@ SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0,
 {
   size_t i;
 
-  for (i = 0; i < scm_port_table_size; i++)
+  for (i = 0; i < scm_t_portable_size; i++)
     {
-      if (SCM_OPOUTPORTP (scm_port_table[i]->port))
-       scm_flush (scm_port_table[i]->port);
+      if (SCM_OPOUTPORTP (scm_t_portable[i]->port))
+       scm_flush (scm_t_portable[i]->port);
     }
   return SCM_UNSPECIFIED;
 }
@@ -907,7 +902,7 @@ SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0,
 int
 scm_fill_input (SCM port)
 {
-  scm_port_t *pt = SCM_PTAB_ENTRY (port);
+  scm_t_port *pt = SCM_PTAB_ENTRY (port);
 
   if (pt->read_buf == pt->putback_buf)
     {
@@ -926,7 +921,7 @@ int
 scm_getc (SCM port)
 {
   int c;
-  scm_port_t *pt = SCM_PTAB_ENTRY (port);
+  scm_t_port *pt = SCM_PTAB_ENTRY (port);
 
   if (pt->rw_active == SCM_PORT_WRITE)
     {
@@ -975,23 +970,32 @@ scm_puts (const char *s, SCM port)
 
 /* scm_lfwrite
  *
- * Currently, this function has an identical implementation to
- * scm_c_write.  We could have turned it into a macro expanding into a
- * call to scm_c_write.  However, the implementation is small and
- * might differ in the future.
- */
+ * This function differs from scm_c_write; it updates port line and
+ * column. */
 
 void 
 scm_lfwrite (const char *ptr, size_t size, SCM port)
 {
-  scm_port_t *pt = SCM_PTAB_ENTRY (port);
-  scm_ptob_descriptor_t *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
+  scm_t_port *pt = SCM_PTAB_ENTRY (port);
+  scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
 
   if (pt->rw_active == SCM_PORT_READ)
     scm_end_input (port);
 
   ptob->write (port, ptr, size);
 
+  for (; size; ptr++, size--) {
+    if (*ptr == '\n') {
+      SCM_INCLINE(port);
+    }
+    else if (*ptr == '\t') {
+      SCM_TABCOL(port);
+    }
+    else {
+      SCM_INCCOL(port);
+    }
+  }
+
   if (pt->rw_random)
     pt->rw_active = SCM_PORT_WRITE;
 }
@@ -1007,7 +1011,7 @@ scm_lfwrite (const char *ptr, size_t size, SCM port)
 size_t
 scm_c_read (SCM port, void *buffer, size_t size)
 {
-  scm_port_t *pt = SCM_PTAB_ENTRY (port);
+  scm_t_port *pt = SCM_PTAB_ENTRY (port);
   size_t n_read = 0, n_available;
 
   if (pt->rw_active == SCM_PORT_WRITE)
@@ -1060,8 +1064,8 @@ scm_c_read (SCM port, void *buffer, size_t size)
 void 
 scm_c_write (SCM port, const void *ptr, size_t size)
 {
-  scm_port_t *pt = SCM_PTAB_ENTRY (port);
-  scm_ptob_descriptor_t *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
+  scm_t_port *pt = SCM_PTAB_ENTRY (port);
+  scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
 
   if (pt->rw_active == SCM_PORT_READ)
     scm_end_input (port);
@@ -1083,7 +1087,7 @@ void
 scm_end_input (SCM port)
 {
   long offset;
-  scm_port_t *pt = SCM_PTAB_ENTRY (port);
+  scm_t_port *pt = SCM_PTAB_ENTRY (port);
 
   if (pt->read_buf == pt->putback_buf)
     {
@@ -1106,7 +1110,7 @@ void
 scm_ungetc (int c, SCM port)
 #define FUNC_NAME "scm_ungetc"
 {
-  scm_port_t *pt = SCM_PTAB_ENTRY (port);
+  scm_t_port *pt = SCM_PTAB_ENTRY (port);
 
   if (pt->read_buf == pt->putback_buf)
     /* already using the put-back buffer.  */
@@ -1302,7 +1306,7 @@ SCM_DEFINE (scm_seek, "seek", 3, 0, 0,
     SCM_OUT_OF_RANGE (3, whence);
   if (SCM_OPPORTP (fd_port))
     {
-      scm_ptob_descriptor_t *ptob = scm_ptobs + SCM_PTOBNUM (fd_port);
+      scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (fd_port);
 
       if (!ptob->seek)
        SCM_MISC_ERROR ("port is not seekable", 
@@ -1355,8 +1359,8 @@ SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0,
     }
   else if (SCM_OPOUTPORTP (object))
     {
-      scm_port_t *pt = SCM_PTAB_ENTRY (object);
-      scm_ptob_descriptor_t *ptob = scm_ptobs + SCM_PTOBNUM (object);
+      scm_t_port *pt = SCM_PTAB_ENTRY (object);
+      scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (object);
       
       if (!ptob->truncate)
        SCM_MISC_ERROR ("port is not truncatable", SCM_EOL);
@@ -1487,7 +1491,7 @@ scm_print_port_mode (SCM exp, SCM port)
 }
 
 int
-scm_port_print (SCM exp, SCM port, scm_print_state *pstate)
+scm_port_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
 {
   char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
   if (!type)
@@ -1505,22 +1509,24 @@ void
 scm_ports_prehistory ()
 {
   scm_numptob = 0;
-  scm_ptobs = (scm_ptob_descriptor_t *) malloc (sizeof (scm_ptob_descriptor_t));
+  scm_ptobs = (scm_t_ptob_descriptor *) malloc (sizeof (scm_t_ptob_descriptor));
 }
 
 \f
 
 /* Void ports.   */
 
-scm_bits_t scm_tc16_void_port = 0;
+scm_t_bits scm_tc16_void_port = 0;
 
-static int fill_input_void_port (SCM port)
+static int fill_input_void_port (SCM port SCM_UNUSED)
 {
   return EOF;
 }
 
 static void
-write_void_port (SCM port, const void *data, size_t size)
+write_void_port (SCM port SCM_UNUSED,
+                const void *data SCM_UNUSED,
+                size_t size SCM_UNUSED)
 {
 }
 
@@ -1529,7 +1535,7 @@ scm_void_port (char *mode_str)
 {
   int mode_bits;
   SCM answer;
-  scm_port_t * pt;
+  scm_t_port * pt;
 
   SCM_NEWCELL (answer);
   SCM_DEFER_INTS;