* * backtrace.c (scm_display_application): New procedure:
[bpt/guile.git] / libguile / fports.c
index 5e7218d..57e9ab8 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1995,1996 Free Software Foundation, Inc.
+/*     Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -88,23 +88,17 @@ scm_sizet fwrite ();
  */
 
 /* should be called with SCM_DEFER_INTS active */
-#ifdef __STDC__
-SCM 
-scm_setbuf0 (SCM port)
-#else
+
 SCM 
 scm_setbuf0 (port)
      SCM port;
-#endif
 {
 #ifndef NOSETBUF
 #ifndef MSDOS
-#ifdef FIONREAD
 #ifndef ultrix
   SCM_SYSCALL (setbuf ((FILE *)SCM_STREAM (port), 0););
 #endif
 #endif
-#endif
 #endif
   return SCM_UNSPECIFIED;
 }
@@ -114,14 +108,10 @@ scm_setbuf0 (port)
  *
  * See PORT FLAGS in scm.h
  */
-#ifdef __STDC__
-long
-scm_mode_bits (char *modes)
-#else
+
 long
 scm_mode_bits (modes)
      char *modes;
-#endif
 {
   return (SCM_OPN
          | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0)
@@ -141,15 +131,11 @@ scm_mode_bits (modes)
  * Return the new port.
  */
 SCM_PROC(s_open_file, "open-file", 2, 0, 0, scm_open_file);
-#ifdef __STDC__
-SCM
-scm_open_file (SCM filename, SCM modes)
-#else
+
 SCM
 scm_open_file (filename, modes)
      SCM filename;
      SCM modes;
-#endif
 {
   SCM port;
   FILE *f;
@@ -171,10 +157,13 @@ scm_open_file (filename, modes)
   SCM_SYSCALL (f = fopen (file, mode));
   if (!f)
     {
-      scm_syserror_msg (s_open_file, "%S: %S",
+      int en = errno;
+
+      scm_syserror_msg (s_open_file, "%s: %S",
                        scm_listify (scm_makfrom0str (strerror (errno)),
                                     filename,
-                                    SCM_UNDEFINED));
+                                    SCM_UNDEFINED),
+                       en);
     }
   else
     {
@@ -182,29 +171,61 @@ scm_open_file (filename, modes)
 
       pt = scm_add_to_port_table (port);
       SCM_SETPTAB_ENTRY (port, pt);
-      if (SCM_BUF0 & (SCM_CAR (port) = scm_tc16_fport | scm_mode_bits (mode)))
+      SCM_SETCAR (port, scm_tc16_fport | scm_mode_bits (mode));
+      SCM_SETSTREAM (port, (SCM) f);
+      if (SCM_BUF0 & SCM_CAR (port))
        scm_setbuf0 (port);
-      SCM_SETSTREAM (port, (SCM)f);
-      SCM_PTAB_ENTRY (port)->file_name = scm_makfrom0str (file);
+      SCM_PTAB_ENTRY (port)->file_name = filename;
     }
   SCM_ALLOW_INTS;
   return port;
 }
 
+
+/* Build a Scheme port from an open stdio port, FILE.
+   MODE indicates whether FILE is open for reading or writing; it uses
+      the same notation as open-file's second argument.
+   If NAME is non-zero, use it as the port's filename.
+
+   scm_stdio_to_port sets the revealed count for FILE's file
+   descriptor to 1, so that FILE won't be closed when the port object
+   is GC'd.  */
+SCM
+scm_stdio_to_port (file, mode, name)
+     FILE *file;
+     char *mode;
+     char *name;
+{
+  long mode_bits = scm_mode_bits (mode);
+  SCM port;
+  struct scm_port_table * pt;
+
+  SCM_NEWCELL (port);
+  SCM_DEFER_INTS;
+  {
+    pt = scm_add_to_port_table (port);
+    SCM_SETPTAB_ENTRY (port, pt);
+    SCM_SETCAR (port, (scm_tc16_fport | mode_bits));
+    SCM_SETSTREAM (port, (SCM) file);
+    if (SCM_BUF0 & SCM_CAR (port))
+      scm_setbuf0 (port);
+    SCM_PTAB_ENTRY (port)->file_name = scm_makfrom0str (name);
+  }
+  SCM_ALLOW_INTS;
+  scm_set_port_revealed_x (port, SCM_MAKINUM (1));
+  return port;
+}
+
+
 /* Return the mode flags from an open port.
  * Some modes such as "append" are only used when opening
- * a file and are not returned here.
- */
+ * a file and are not returned here.  */
 
 SCM_PROC(s_port_mode, "port-mode", 1, 0, 0, scm_port_mode);
-#ifdef __STDC__
-SCM
-scm_port_mode (SCM port)
-#else
+
 SCM
 scm_port_mode (port)
      SCM port;
-#endif
 {
   char modes[3];
   modes[0] = '\0';
@@ -223,19 +244,15 @@ scm_port_mode (port)
 }
 
 
-#ifdef __STDC__
-static int 
-prinfport (SCM exp, SCM port, int writing)
-#else
+
+static int prinfport SCM_P ((SCM exp, SCM port, scm_print_state *pstate));
+
 static int 
-prinfport (exp, port, writing)
+prinfport (exp, port, pstate)
      SCM exp;
      SCM port;
-     int writing;
-#endif
+     scm_print_state *pstate;
 {
-  /*
-  perhaps this isn't needed.
   SCM name;
   char * c;
   if (SCM_CLOSEDP (exp))
@@ -252,20 +269,16 @@ prinfport (exp, port, writing)
     }
     
   scm_prinport (exp, port, c);
-  */
-  scm_prinport (exp, port, "file");
   return !0;
 }
 
 
-#ifdef __STDC__
-static int
-scm_fgetc (FILE * s)
-#else
+
+static int scm_fgetc SCM_P ((FILE * s));
+
 static int
 scm_fgetc (s)
      FILE * s;
-#endif
 {
   if (feof (s))
     return EOF;
@@ -274,16 +287,14 @@ scm_fgetc (s)
 }
 
 #ifdef vms
-#ifdef __STDC__
-static scm_sizet 
-pwrite (char *ptr, scm_sizet size, nitems, FILE *port)
-#else
+
+static scm_sizet pwrite SCM_P ((char *ptr, scm_sizet size, nitems, FILE *port));
+
 static scm_sizet 
 pwrite (ptr, size, nitems, port)
      char *ptr;
      scm_sizet size, nitems;
      FILE *port;
-#endif
 {
   scm_sizet len = size * nitems;
   scm_sizet i = 0;
@@ -301,6 +312,9 @@ pwrite (ptr, size, nitems, port)
 /* This otherwise pointless code helps some poor 
  * crippled C compilers cope with life. 
  */
+
+static int local_fclose SCM_P ((FILE *fp));
+
 static int
 local_fclose (fp)
      FILE * fp;
@@ -308,6 +322,8 @@ local_fclose (fp)
   return fclose (fp);
 }
 
+static int local_fflush SCM_P ((FILE *fp));
+
 static int
 local_fflush (fp)
      FILE * fp;
@@ -315,6 +331,8 @@ local_fflush (fp)
   return fflush (fp);
 }
 
+static int local_fputc SCM_P ((int c, FILE *fp));
+
 static int
 local_fputc (c, fp)
      int c;
@@ -323,6 +341,8 @@ local_fputc (c, fp)
   return fputc (c, fp);
 }
 
+static int local_fputs SCM_P ((char *s, FILE *fp));
+
 static int
 local_fputs (s, fp)
      char * s;
@@ -331,6 +351,8 @@ local_fputs (s, fp)
   return fputs (s, fp);
 }
 
+static scm_sizet local_ffwrite SCM_P ((void *ptr, int size, int nitems, FILE *fp));
+
 static scm_sizet
 local_ffwrite (ptr, size, nitems, fp)
      void * ptr;
@@ -345,15 +367,15 @@ local_ffwrite (ptr, size, nitems, fp)
 scm_ptobfuns scm_fptob =
 {
   scm_mark0,
-  local_fclose,
+  (int (*) SCM_P ((SCM))) local_fclose,
   prinfport,
   0,
-  local_fputc,
-  local_fputs,
-  local_ffwrite,
-  local_fflush,
-  scm_fgetc,
-  local_fclose
+  (int (*) SCM_P ((int, SCM))) local_fputc,
+  (int (*) SCM_P ((char *, SCM))) local_fputs,
+  (scm_sizet (*) SCM_P ((char *, scm_sizet, scm_sizet, SCM))) local_ffwrite,
+  (int (*) SCM_P ((SCM))) local_fflush,
+  (int (*) SCM_P ((SCM))) scm_fgetc,
+  (int (*) SCM_P ((SCM))) local_fclose
 };
 
 /* {Pipe ports}
@@ -364,23 +386,16 @@ scm_ptobfuns scm_pipob =
   0,                           /* replaced by pclose in scm_init_ioext() */
   0,                           /* replaced by prinpipe in scm_init_ioext() */
   0,
-  local_fputc,
-  local_fputs,
-  local_ffwrite,
-  local_fflush,
-  scm_fgetc,
+  (int (*) SCM_P ((int, SCM))) local_fputc,
+  (int (*) SCM_P ((char *, SCM))) local_fputs,
+  (scm_sizet (*) SCM_P ((char *, scm_sizet, scm_sizet, SCM))) local_ffwrite,
+  (int (*) SCM_P ((SCM))) local_fflush,
+  (int (*) SCM_P ((SCM))) scm_fgetc,
   0
 };                             /* replaced by pclose in scm_init_ioext() */
 
-
-#ifdef __STDC__
-void
-scm_init_fports (void)
-#else
 void
 scm_init_fports ()
-#endif
 {
 #include "fports.x"
 }
-