Centralize the creation of port objects based on stdio FILE * in
authorJim Blandy <jimb@red-bean.com>
Fri, 9 Oct 1998 12:46:31 +0000 (12:46 +0000)
committerJim Blandy <jimb@red-bean.com>
Fri, 9 Oct 1998 12:46:31 +0000 (12:46 +0000)
fports.c; don't just throw them together anywhere.
* fports.c (scm_stdio_to_port): Make NAME a SCM value, which is
what the rest of Guile wants.  Don't set the revealed count;
that's only appropriate for stdin, stdout, stderr.
(scm_standard_stream_to_port): This function does set the revealed
count.
* init.c (scm_init_standard_ports): Use scm_standard_stream_to_port,
not scm_stdio_to_port.
* filesys.c (scm_open): Call scm_stdio_to_port; don't write it out.
* fports.c (scm_open_file): Same.
* posix.c (scm_pipe): Same.
* socket.c (scm_sock_fd_to_port): Same.
* ioext.c (scm_fdopen): Same.
(scm_freopen): Moved from here to...
* fports.c (scm_freopen): ... here.  This is really something that
munges the internals of an fport, so it should go here.
* fports.h (scm_stdio_to_port): Adjust prototype.
(scm_standard_stream_to_port, scm_freopen): New protoypes.
* ioext.h (scm_freopen): Prototype removed.

libguile/init.c
libguile/ioext.c
libguile/ioext.h
libguile/posix.c
libguile/socket.c

index 2430455..5858269 100644 (file)
@@ -264,11 +264,12 @@ scm_init_standard_ports ()
      and scsh, read stdin unbuffered.  Applications that can tolerate
      buffered input on stdin can reset \ex{(current-input-port)} to
      block buffering for higher performance.  */
-  scm_def_inp = scm_stdio_to_port (stdin, 
+  scm_def_inp
+    = scm_standard_stream_to_port (stdin, 
                                   (isatty (fileno (stdin)) ? "r0" : "r"),
                                   "standard input");
-  scm_def_outp = scm_stdio_to_port (stdout, "w", "standard output");
-  scm_def_errp = scm_stdio_to_port (stderr, "w", "standard error");
+  scm_def_outp = scm_standard_stream_to_port (stdout, "w", "standard output");
+  scm_def_errp = scm_standard_stream_to_port (stderr, "w", "standard error");
 
   scm_cur_inp = scm_def_inp;
   scm_cur_outp = scm_def_outp;
index 394359b..64ad562 100644 (file)
@@ -260,47 +260,6 @@ scm_fseek (object, offset, whence)
   return SCM_UNSPECIFIED;
 }
 
-SCM_PROC (s_freopen, "freopen", 3, 0, 0, scm_freopen);
-
-SCM 
-scm_freopen (filename, modes, port)
-     SCM filename;
-     SCM modes;
-     SCM port;
-{
-  FILE *f;
-  SCM_ASSERT (SCM_NIMP (filename) && SCM_ROSTRINGP (filename), filename,
-             SCM_ARG1, s_freopen);
-  SCM_ASSERT (SCM_NIMP (modes) && SCM_ROSTRINGP (modes), modes, SCM_ARG2,
-             s_freopen);
-
-  SCM_COERCE_SUBSTR (filename);
-  SCM_COERCE_SUBSTR (modes);
-  port = SCM_COERCE_OUTPORT (port);
-  SCM_DEFER_INTS;
-  SCM_ASSERT (SCM_NIMP (port) && SCM_FPORTP (port), port, SCM_ARG3, s_freopen);
-  SCM_SYSCALL (f = freopen (SCM_ROCHARS (filename), SCM_ROCHARS (modes),
-                           (FILE *)SCM_STREAM (port)));
-  if (!f)
-    {
-      SCM p;
-      p = port;
-      port = SCM_MAKINUM (errno);
-      SCM_SETAND_CAR (p, ~SCM_OPN);
-      scm_remove_from_port_table (p);
-    }
-  else
-    {
-      SCM_SETCAR (port, scm_tc16_fport | scm_mode_bits (SCM_ROCHARS (modes)));
-      SCM_SETSTREAM (port, (SCM)f);
-      SCM_SETCAR (port, scm_tc16_fport | scm_mode_bits (SCM_ROCHARS (modes)));
-      if (SCM_BUF0 & SCM_CAR (port))
-       scm_setbuf0 (port);
-    }
-  SCM_ALLOW_INTS;
-  return port;
-}
-
 SCM_PROC (s_redirect_port, "redirect-port", 2, 0, 0, scm_redirect_port);
 
 SCM 
@@ -419,23 +378,16 @@ scm_fdopen (fdes, modes)
 {
   FILE *f;
   SCM port;
-  struct scm_port_table * pt;
 
   SCM_ASSERT (SCM_INUMP (fdes), fdes, SCM_ARG1, s_fdopen);
   SCM_ASSERT (SCM_NIMP (modes) && SCM_ROSTRINGP (modes), modes, SCM_ARG2,
              s_fdopen);
   SCM_COERCE_SUBSTR (modes);
-  SCM_NEWCELL (port);
   SCM_DEFER_INTS;
   f = fdopen (SCM_INUM (fdes), SCM_ROCHARS (modes));
   if (f == NULL)
     scm_syserror (s_fdopen);
-  pt = scm_add_to_port_table (port);
-  SCM_SETPTAB_ENTRY (port, pt);
-  SCM_SETCAR (port, scm_tc16_fport | scm_mode_bits (SCM_ROCHARS (modes)));
-  SCM_SETSTREAM (port, (SCM)f);
-  if (SCM_BUF0 & SCM_CAR (port))
-    scm_setbuf0 (port);
+  port = scm_stdio_to_port (f, SCM_ROCHARS (modes), SCM_BOOL_F);
   SCM_ALLOW_INTS;
   return port;
 }
index 1bd9dbd..ab95db9 100644 (file)
@@ -53,7 +53,6 @@ extern SCM scm_read_line (SCM port);
 extern SCM scm_write_line SCM_P ((SCM obj, SCM port));
 extern SCM scm_ftell SCM_P ((SCM object));
 extern SCM scm_fseek SCM_P ((SCM object, SCM offset, SCM whence));
-extern SCM scm_freopen SCM_P ((SCM filename, SCM modes, SCM port));
 extern SCM scm_redirect_port SCM_P ((SCM into_pt, SCM from_pt));
 extern SCM scm_dup_to_fdes (SCM fd_or_port, SCM newfd);
 extern SCM scm_fileno SCM_P ((SCM port));
index 90a50ce..a60aa8f 100644 (file)
@@ -158,7 +158,8 @@ extern char ** environ;
    encourage header files to do strange things.  */
 
 \f
-
+SCM_SYMBOL (sym_read_pipe, "read pipe");
+SCM_SYMBOL (sym_write_pipe, "write pipe");
 
 SCM_PROC (s_pipe, "pipe", 0, 0, 0, scm_pipe);
 
@@ -168,11 +169,7 @@ scm_pipe ()
   int fd[2], rv;
   FILE *f_rd, *f_wt;
   SCM p_rd, p_wt;
-  struct scm_port_table * ptr;
-  struct scm_port_table * ptw;
 
-  SCM_NEWCELL (p_rd);
-  SCM_NEWCELL (p_wt);
   rv = pipe (fd);
   if (rv)
     scm_syserror (s_pipe);
@@ -193,14 +190,9 @@ scm_pipe ()
       errno = en;
       scm_syserror (s_pipe);
     }
-  ptr = scm_add_to_port_table (p_rd);
-  ptw = scm_add_to_port_table (p_wt);
-  SCM_SETPTAB_ENTRY (p_rd, ptr);
-  SCM_SETPTAB_ENTRY (p_wt, ptw);
-  SCM_SETCAR (p_rd, scm_tc16_fport | scm_mode_bits ("r"));
-  SCM_SETCAR (p_wt, scm_tc16_fport | scm_mode_bits ("w"));
-  SCM_SETSTREAM (p_rd, (SCM)f_rd);
-  SCM_SETSTREAM (p_wt, (SCM)f_wt);
+
+  p_rd = scm_stdio_to_port (f_rd, "r", sym_read_pipe);
+  p_wt = scm_stdio_to_port (f_wt, "w", sym_write_pipe);
 
   SCM_ALLOW_INTS;
   return scm_cons (p_rd, p_wt);
index 7f80775..6e35b99 100644 (file)
@@ -66,6 +66,7 @@
 
 \f
 
+SCM_SYMBOL (sym_socket, "socket");
 static SCM scm_sock_fd_to_port SCM_P ((int fd, char *proc));
 
 static SCM
@@ -84,14 +85,7 @@ scm_sock_fd_to_port (fd, proc)
       SCM_SYSCALL (close (fd));
       scm_syserror (proc);
     }
-  SCM_NEWCELL (result);
-  {
-    struct scm_port_table *pt = scm_add_to_port_table (result);
-
-    SCM_SETPTAB_ENTRY (result, pt);
-  }
-  SCM_SETCAR (result, scm_tc16_fport | scm_mode_bits ("r+0"));
-  SCM_SETSTREAM (result, (SCM)f);
+  result = scm_stdio_to_port (f, "r+0", sym_socket);
   scm_setbuf0 (result);
   return result;
 }