*** empty log message ***
[bpt/guile.git] / libguile / genio.c
index 74bc0d3..b11bf02 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1995,1996, 1997 Free Software Foundation, Inc.
+/*     Copyright (C) 1995, 1996, 1997, 1998 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
@@ -59,7 +59,7 @@ scm_putc (c, port)
      SCM port;
 {
   scm_sizet i = SCM_PTOBNUM (port);
-  SCM_SYSCALL ((scm_ptobs[i].fputc) (c, SCM_STREAM (port)));
+  SCM_SYSCALL ((scm_ptobs[i].fputc) (c, port));
 }
 
 void 
@@ -68,11 +68,7 @@ scm_puts (s, port)
      SCM port;
 {
   scm_sizet i = SCM_PTOBNUM (port);
-  SCM_SYSCALL ((scm_ptobs[i].fputs) (s, SCM_STREAM (port)));
-#ifdef TRANSCRIPT_SUPPORT
-  if (scm_trans && (port == def_outp || port == cur_errp))
-    SCM_SYSCALL (fputs (s, scm_trans));
-#endif
+  SCM_SYSCALL ((scm_ptobs[i].fputs) (s, port));
 }
 
 void 
@@ -82,14 +78,18 @@ scm_lfwrite (ptr, size, port)
      SCM port;
 {
   scm_sizet i = SCM_PTOBNUM (port);
-  SCM_SYSCALL (scm_ptobs[i].fwrite(ptr, size, 1, SCM_STREAM (port)));
-#ifdef TRANSCRIPT_SUPPORT
-  if (scm_trans && (port == def_outp || port == cur_errp))
-    SCM_SYSCALL (fwrite (ptr, size, 1, scm_trans));
-#endif
+  SCM_SYSCALL (scm_ptobs[i].fwrite (ptr, size, 1, port));
 }
 
 
+void 
+scm_fflush (port)
+     SCM port;
+{
+  scm_sizet i = SCM_PTOBNUM (port);
+  (scm_ptobs[i].fflush) (port);
+}
+
 \f
 
 int 
@@ -100,11 +100,10 @@ scm_getc (port)
   int c;
   scm_sizet i;
 
-  /* One char may be stored in the high bits of (car port) orre@nada.kth.se. */
   if (SCM_CRDYP (port))
     {
       c = SCM_CGETUN (port);
-      SCM_CLRDY (port);         /* Clear ungetted char */
+      SCM_TRY_CLRDY (port);         /* Clear ungetted char */
     }
   else
     {
@@ -125,7 +124,7 @@ scm_getc (port)
          while (n == -1 && errno == EINTR);
        }
 #endif
-      SCM_SYSCALL (c = (scm_ptobs[i].fgetc) (f));
+      SCM_SYSCALL (c = (scm_ptobs[i].fgetc) (port));
     }
 
   if (c == '\n')
@@ -150,8 +149,8 @@ scm_ungetc (c, port)
      int c;
      SCM port;
 {
-/*     SCM_ASSERT(!SCM_CRDYP(port), port, SCM_ARG2, "too many scm_ungetc");*/
   SCM_CUNGET (c, port);
+
   if (c == '\n')
     {
       /* What should col be in this case?
@@ -164,6 +163,23 @@ scm_ungetc (c, port)
 }
 
 
+void 
+scm_ungets (s, n, port)
+     char *s;
+     int n;
+     SCM port;
+{
+  /* This is simple minded and inefficient, but unreading strings is
+   * probably not a common operation, and remember that line and
+   * column numbers have to be handled...
+   *
+   * Please feel freee to write an optimized version!
+   */
+  while (n--)
+    scm_ungetc (s[n], port);
+}
+
+
 char *
 scm_do_read_line (port, len)
      SCM port;
@@ -174,6 +190,19 @@ scm_do_read_line (port, len)
 
   i = SCM_PTOBNUM (port);
   SCM_SYSCALL (s = (scm_ptobs[i].fgets) (port, len));
+
+  /* We should never get an empty string.  Every line has a newline at
+     the end, except for the last one.  If the last line has no
+     newline and is empty, then that's just an ordinary EOF, and we
+     should have s == NULL.  But this seems obscure to me, so we check
+     this here, to protect ourselves from odd port implementations.  */
+  if (s && *len <= 0)
+    abort ();
+
+  /* If we're not at EOF, and there was a newline at the end of the
+     string, increment the line counter.  */
+  if (s && s[*len - 1] == '\n')
+    SCM_INCLINE(port);
+
   return s;
 }
-