OSDN Git Service

2007-12-10 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / io / open.c
index 3dc2b11..0a409ed 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2002, 2003, 2004, 2005
+/* Copyright (C) 2002, 2003, 2004, 2005, 2007
    Free Software Foundation, Inc.
    Contributed by Andy Vaught
 
@@ -28,18 +28,17 @@ along with Libgfortran; see the file COPYING.  If not, write to
 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.  */
 
-#include "config.h"
+#include "io.h"
 #include <unistd.h>
-#include <stdio.h>
 #include <string.h>
-#include "libgfortran.h"
-#include "io.h"
+#include <errno.h>
 
 
 static const st_option access_opt[] = {
   {"sequential", ACCESS_SEQUENTIAL},
   {"direct", ACCESS_DIRECT},
   {"append", ACCESS_APPEND},
+  {"stream", ACCESS_STREAM},
   {NULL, 0}
 };
 
@@ -100,19 +99,20 @@ static const st_option pad_opt[] =
 
 static const st_option convert_opt[] =
 {
-  { "native", CONVERT_NATIVE},
-  { "swap", CONVERT_SWAP},
-  { "big_endian", CONVERT_BIG},
-  { "little_endian", CONVERT_LITTLE},
+  { "native", GFC_CONVERT_NATIVE},
+  { "swap", GFC_CONVERT_SWAP},
+  { "big_endian", GFC_CONVERT_BIG},
+  { "little_endian", GFC_CONVERT_LITTLE},
   { NULL, 0}
 };
 
+
 /* Given a unit, test to see if the file is positioned at the terminal
    point, and if so, change state from NO_ENDFILE flag to AT_ENDFILE.
    This prevents us from changing the state from AFTER_ENDFILE to
    AT_ENDFILE.  */
 
-void
+static void
 test_endfile (gfc_unit * u)
 {
   if (u->endfile == NO_ENDFILE && file_length (u->s) == file_position (u->s))
@@ -128,50 +128,56 @@ edit_modes (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
 {
   /* Complain about attempts to change the unchangeable.  */
 
-  if (flags->status != STATUS_UNSPECIFIED &&
+  if (flags->status != STATUS_UNSPECIFIED && flags->status != STATUS_OLD && 
       u->flags.status != flags->status)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot change STATUS parameter in OPEN statement");
 
   if (flags->access != ACCESS_UNSPECIFIED && u->flags.access != flags->access)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot change ACCESS parameter in OPEN statement");
 
   if (flags->form != FORM_UNSPECIFIED && u->flags.form != flags->form)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot change FORM parameter in OPEN statement");
 
   if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN)
       && opp->recl_in != u->recl)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot change RECL parameter in OPEN statement");
 
-  if (flags->action != ACTION_UNSPECIFIED && u->flags.access != flags->access)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+  if (flags->action != ACTION_UNSPECIFIED && u->flags.action != flags->action)
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot change ACTION parameter in OPEN statement");
 
   /* Status must be OLD if present.  */
 
   if (flags->status != STATUS_UNSPECIFIED && flags->status != STATUS_OLD &&
       flags->status != STATUS_UNKNOWN)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    {
+      if (flags->status == STATUS_SCRATCH)
+       notify_std (&opp->common, GFC_STD_GNU,
                    "OPEN statement must have a STATUS of OLD or UNKNOWN");
+      else
+       generate_error (&opp->common, LIBERROR_BAD_OPTION,
+                   "OPEN statement must have a STATUS of OLD or UNKNOWN");
+    }
 
   if (u->flags.form == FORM_UNFORMATTED)
     {
       if (flags->delim != DELIM_UNSPECIFIED)
-       generate_error (&opp->common, ERROR_OPTION_CONFLICT,
+       generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
                        "DELIM parameter conflicts with UNFORMATTED form in "
                        "OPEN statement");
 
       if (flags->blank != BLANK_UNSPECIFIED)
-       generate_error (&opp->common, ERROR_OPTION_CONFLICT,
+       generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
                        "BLANK parameter conflicts with UNFORMATTED form in "
                        "OPEN statement");
 
       if (flags->pad != PAD_UNSPECIFIED)
-       generate_error (&opp->common, ERROR_OPTION_CONFLICT,
-                       "PAD paramter conflicts with UNFORMATTED form in "
+       generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                       "PAD parameter conflicts with UNFORMATTED form in "
                        "OPEN statement");
     }
 
@@ -201,19 +207,21 @@ edit_modes (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
       u->current_record = 0;
       u->last_record = 0;
 
-      test_endfile (u);                /* We might be at the end.  */
+      test_endfile (u);
       break;
 
     case POSITION_APPEND:
       if (sseek (u->s, file_length (u->s)) == FAILURE)
        goto seek_error;
 
-      u->current_record = 0;
+      if (flags->access != ACCESS_STREAM)
+       u->current_record = 0;
+
       u->endfile = AT_ENDFILE; /* We are at the end.  */
       break;
 
     seek_error:
-      generate_error (&opp->common, ERROR_OS, NULL);
+      generate_error (&opp->common, LIBERROR_OS, NULL);
       break;
     }
 
@@ -248,7 +256,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
     {
       if (flags->form == FORM_UNFORMATTED)
        {
-         generate_error (&opp->common, ERROR_OPTION_CONFLICT,
+         generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
                          "DELIM parameter conflicts with UNFORMATTED form in "
                          "OPEN statement");
          goto fail;
@@ -261,7 +269,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
     {
       if (flags->form == FORM_UNFORMATTED)
        {
-         generate_error (&opp->common, ERROR_OPTION_CONFLICT,
+         generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
                          "BLANK parameter conflicts with UNFORMATTED form in "
                          "OPEN statement");
          goto fail;
@@ -274,8 +282,8 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
     {
       if (flags->form == FORM_UNFORMATTED)
        {
-         generate_error (&opp->common, ERROR_OPTION_CONFLICT,
-                         "PAD paramter conflicts with UNFORMATTED form in "
+         generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
+                         "PAD parameter conflicts with UNFORMATTED form in "
                          "OPEN statement");
          goto fail;
        }
@@ -283,7 +291,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
 
   if (flags->position != POSITION_ASIS && flags->access == ACCESS_DIRECT)
    {
-     generate_error (&opp->common, ERROR_OPTION_CONFLICT,
+     generate_error (&opp->common, LIBERROR_OPTION_CONFLICT,
                      "ACCESS parameter conflicts with SEQUENTIAL access in "
                      "OPEN statement");
      goto fail;
@@ -301,14 +309,14 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
   if (flags->access == ACCESS_DIRECT
       && (opp->common.flags & IOPARM_OPEN_HAS_RECL_IN) == 0)
     {
-      generate_error (&opp->common, ERROR_MISSING_OPTION,
+      generate_error (&opp->common, LIBERROR_MISSING_OPTION,
                      "Missing RECL parameter in OPEN statement");
       goto fail;
     }
 
   if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN) && opp->recl_in <= 0)
     {
-      generate_error (&opp->common, ERROR_BAD_OPTION,
+      generate_error (&opp->common, LIBERROR_BAD_OPTION,
                      "RECL parameter is non-positive in OPEN statement");
       goto fail;
     }
@@ -322,7 +330,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
          break;
        }
 
-      generate_error (&opp->common, ERROR_BAD_OPTION,
+      generate_error (&opp->common, LIBERROR_BAD_OPTION,
                      "FILE parameter must not be present in OPEN statement");
       goto fail;
 
@@ -334,7 +342,12 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
        break;
 
       opp->file = tmpname;
-      opp->file_len = sprintf(opp->file, "fort.%d", opp->common.unit);
+#ifdef HAVE_SNPRINTF
+      opp->file_len = snprintf(opp->file, sizeof (tmpname), "fort.%d", 
+                              (int) opp->common.unit);
+#else
+      opp->file_len = sprintf(opp->file, "fort.%d", (int) opp->common.unit);
+#endif
       break;
 
     default:
@@ -353,7 +366,7 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
       && (options.stderr_unit < 0 || u2->unit_number != options.stderr_unit))
     {
       unlock_unit (u2);
-      generate_error (&opp->common, ERROR_ALREADY_OPEN, NULL);
+      generate_error (&opp->common, LIBERROR_ALREADY_OPEN, NULL);
       goto cleanup;
     }
 
@@ -365,7 +378,34 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
   s = open_external (opp, flags);
   if (s == NULL)
     {
-      generate_error (&opp->common, ERROR_OS, NULL);
+      char *path, *msg;
+      path = (char *) gfc_alloca (opp->file_len + 1);
+      msg = (char *) gfc_alloca (opp->file_len + 51);
+      unpack_filename (path, opp->file, opp->file_len);
+
+      switch (errno)
+       {
+       case ENOENT: 
+         sprintf (msg, "File '%s' does not exist", path);
+         break;
+
+       case EEXIST:
+         sprintf (msg, "File '%s' already exists", path);
+         break;
+
+       case EACCES:
+         sprintf (msg, "Permission denied trying to open file '%s'", path);
+         break;
+
+       case EISDIR:
+         sprintf (msg, "'%s' is a directory", path);
+         break;
+
+       default:
+         msg = NULL;
+       }
+
+      generate_error (&opp->common, LIBERROR_OS, msg);
       goto cleanup;
     }
 
@@ -386,20 +426,52 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
   u->mode = READING;
   u->maxrec = 0;
   u->bytes_left = 0;
+  u->saved_pos = 0;
 
   if (flags->position == POSITION_APPEND)
     {
       if (sseek (u->s, file_length (u->s)) == FAILURE)
-       generate_error (&opp->common, ERROR_OS, NULL);
+       generate_error (&opp->common, LIBERROR_OS, NULL);
       u->endfile = AT_ENDFILE;
     }
 
   /* Unspecified recl ends up with a processor dependent value.  */
 
   if ((opp->common.flags & IOPARM_OPEN_HAS_RECL_IN))
-    u->recl = opp->recl_in;
+    {
+      u->flags.has_recl = 1;
+      u->recl = opp->recl_in;
+      u->recl_subrecord = u->recl;
+      u->bytes_left = u->recl;
+    }
   else
-    u->recl = max_offset;
+    {
+      u->flags.has_recl = 0;
+      u->recl = max_offset;
+      if (compile_options.max_subrecord_length)
+       {
+         u->recl_subrecord = compile_options.max_subrecord_length;
+       }
+      else
+       {
+         switch (compile_options.record_marker)
+           {
+           case 0:
+             /* Fall through */
+           case sizeof (GFC_INTEGER_4):
+             u->recl_subrecord = GFC_MAX_SUBRECORD_LENGTH;
+             break;
+
+           case sizeof (GFC_INTEGER_8):
+             u->recl_subrecord = max_offset - 16;
+             break;
+
+           default:
+             runtime_error ("Illegal value for record marker");
+             break;
+           }
+       }
+    }
 
   /* If the file is direct access, calculate the maximum record number
      via a division now instead of letting the multiplication overflow
@@ -407,13 +479,20 @@ new_unit (st_parameter_open *opp, gfc_unit *u, unit_flags * flags)
 
   if (flags->access == ACCESS_DIRECT)
     u->maxrec = max_offset / u->recl;
+  
+  if (flags->access == ACCESS_STREAM)
+    {
+      u->maxrec = max_offset;
+      u->recl = 1;
+      u->strm_pos = 1;
+    }
 
   memmove (u->file, opp->file, opp->file_len);
   u->file_len = opp->file_len;
 
   /* Curiously, the standard requires that the
      position specifier be ignored for new files so a newly connected
-     file starts out that the initial point.  We still need to figure
+     file starts out at the initial point.  We still need to figure
      out if the file is at the end or not.  */
 
   test_endfile (u);
@@ -465,7 +544,7 @@ already_open (st_parameter_open *opp, gfc_unit * u, unit_flags * flags)
       if (sclose (u->s) == FAILURE)
        {
          unlock_unit (u);
-         generate_error (&opp->common, ERROR_OS,
+         generate_error (&opp->common, LIBERROR_OS,
                          "Error closing file in OPEN statement");
          return;
        }
@@ -502,6 +581,7 @@ st_open (st_parameter_open *opp)
   unit_flags flags;
   gfc_unit *u = NULL;
   GFC_INTEGER_4 cf = opp->common.flags;
+  unit_convert conv;
  
   library_start (&opp->common);
 
@@ -539,54 +619,63 @@ st_open (st_parameter_open *opp)
     find_option (&opp->common, opp->status, opp->status_len,
                 status_opt, "Bad STATUS parameter in OPEN statement");
 
-  if (cf & IOPARM_OPEN_HAS_CONVERT)
+  /* First, we check wether the convert flag has been set via environment
+     variable.  This overrides the convert tag in the open statement.  */
+
+  conv = get_unformatted_convert (opp->common.unit);
+
+  if (conv == GFC_CONVERT_NONE)
     {
-      unit_convert conv;
-      conv = find_option (&opp->common, opp->convert, opp->convert_len,
-                         convert_opt, "Bad CONVERT parameter in OPEN statement");
-      /* We use l8_to_l4_offset, which is 0 on little-endian machines
-        and 1 on big-endian machines.  */
-      switch (conv)
-       {
-       case CONVERT_NATIVE:
-       case CONVERT_SWAP:
-         break;
-         
-       case CONVERT_BIG:
-         conv = l8_to_l4_offset ? CONVERT_NATIVE : CONVERT_SWAP;
-         break;
-         
-       case CONVERT_LITTLE:
-         conv = l8_to_l4_offset ? CONVERT_SWAP : CONVERT_NATIVE;
-         break;
-       default:
-         internal_error (&opp->common, "Illegal value for CONVERT");
-         break;
-       }
-      flags.convert = conv;
+      /* Nothing has been set by environment variable, check the convert tag.  */
+      if (cf & IOPARM_OPEN_HAS_CONVERT)
+       conv = find_option (&opp->common, opp->convert, opp->convert_len,
+                           convert_opt,
+                           "Bad CONVERT parameter in OPEN statement");
+      else
+       conv = compile_options.convert;
     }
-  else
-    flags.convert = CONVERT_NATIVE;
+  
+  /* We use l8_to_l4_offset, which is 0 on little-endian machines
+     and 1 on big-endian machines.  */
+  switch (conv)
+    {
+    case GFC_CONVERT_NATIVE:
+    case GFC_CONVERT_SWAP:
+      break;
+      
+    case GFC_CONVERT_BIG:
+      conv = l8_to_l4_offset ? GFC_CONVERT_NATIVE : GFC_CONVERT_SWAP;
+      break;
+      
+    case GFC_CONVERT_LITTLE:
+      conv = l8_to_l4_offset ? GFC_CONVERT_SWAP : GFC_CONVERT_NATIVE;
+      break;
+      
+    default:
+      internal_error (&opp->common, "Illegal value for CONVERT");
+      break;
+    }
+
+  flags.convert = conv;
 
   if (opp->common.unit < 0)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Bad unit number in OPEN statement");
 
   if (flags.position != POSITION_UNSPECIFIED
       && flags.access == ACCESS_DIRECT)
-    generate_error (&opp->common, ERROR_BAD_OPTION,
+    generate_error (&opp->common, LIBERROR_BAD_OPTION,
                    "Cannot use POSITION with direct access files");
 
   if (flags.access == ACCESS_APPEND)
     {
       if (flags.position != POSITION_UNSPECIFIED
          && flags.position != POSITION_APPEND)
-       generate_error (&opp->common, ERROR_BAD_OPTION,
+       generate_error (&opp->common, LIBERROR_BAD_OPTION,
                        "Conflicting ACCESS and POSITION flags in"
                        " OPEN statement");
 
-      notify_std (GFC_STD_GNU,
+      notify_std (&opp->common, GFC_STD_GNU,
                  "Extension: APPEND as a value for ACCESS in OPEN statement");
       flags.access = ACCESS_SEQUENTIAL;
       flags.position = POSITION_APPEND;