Add scratch file handles.
[pspp-builds.git] / src / file-handle.q
index 1133fd31286367695eecf4c815d7eeabf96de2f8..c13be76beda44a320979200adb2418a6f04a6044 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - computes sample statistics.
-   Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
    Written by Ben Pfaff <blp@gnu.org>.
 
    This program is free software; you can redistribute it and/or
@@ -14,8 +14,8 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA. */
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA. */
 
 #include <config.h>
 #include "file-handle.h"
 #include "filename.h"
 #include "command.h"
 #include "lexer.h"
-#include "getline.h"
+#include "getl.h"
 #include "error.h"
 #include "magic.h"
+#include "str.h"
 #include "var.h"
-/* (headers) */
-
-/* File handle private data. */
-struct private_file_handle 
-  {
-    char *name;                 /* File handle identifier. */
-    char *filename;            /* Filename as provided by user. */
-    struct file_identity *identity; /* For checking file identity. */
-    struct file_locator where; /* Used for reporting error messages. */
-    enum file_handle_mode mode;        /* File mode. */
-    size_t length;             /* Length of fixed-format records. */
-  };
+#include "linked-list.h"
+#include "file-handle-def.h"
 
-/* Linked list of file handles. */
-struct file_handle_list 
-  {
-    struct file_handle *handle;
-    struct file_handle_list *next;
-  };
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
 
-static struct file_handle_list *file_handles;
-struct file_handle *inline_file;
+/* (headers) */
 
-static struct file_handle *create_file_handle (const char *handle_name,
-                                               const char *filename);
 
 /* (specification)
    "FILE HANDLE" (fh_):
      name=string;
-     recform=recform:fixed/!variable/spanned;
      lrecl=integer;
-     mode=mode:!character/image/binary/multipunch/_360.
+     tabwidth=integer "x>=0" "%s must be nonnegative";
+     mode=mode:!character/image/scratch.
 */
 /* (declarations) */
 /* (functions) */
 
-static struct file_handle *
-get_handle_with_name (const char *handle_name) 
-{
-  struct file_handle_list *iter;
-
-  for (iter = file_handles; iter != NULL; iter = iter->next)
-    if (!strcmp (handle_name, iter->handle->private->name))
-      return iter->handle;
-  return NULL;
-}
-
-static struct file_handle *
-get_handle_for_filename (const char *filename)
-{
-  struct file_identity *identity;
-  struct file_handle_list *iter;
-      
-  /* First check for a file with the same identity. */
-  identity = fn_get_identity (filename);
-  if (identity != NULL) 
-    {
-      for (iter = file_handles; iter != NULL; iter = iter->next)
-        if (iter->handle->private->identity != NULL
-            && !fn_compare_file_identities (identity,
-                                         iter->handle->private->identity)) 
-          {
-            fn_free_identity (identity);
-            return iter->handle; 
-          }
-      fn_free_identity (identity);
-    }
-
-  /* Then check for a file with the same name. */
-  for (iter = file_handles; iter != NULL; iter = iter->next)
-    if (!strcmp (filename, iter->handle->private->filename))
-      return iter->handle; 
-
-  return NULL;
-}
-
 int
 cmd_file_handle (void)
 {
-  char handle_name[9];
+  char handle_name[LONG_NAME_LEN + 1];
+  struct fh_properties properties = *fh_default_properties ();
 
   struct cmd_file_handle cmd;
   struct file_handle *handle;
 
   if (!lex_force_id ())
     return CMD_FAILURE;
-  strcpy (handle_name, tokid);
+  str_copy_trunc (handle_name, sizeof handle_name, tokid);
 
-  handle = get_handle_with_name (handle_name);
+  handle = fh_from_name (handle_name);
   if (handle != NULL)
     {
-      msg (SE, _("File handle %s already refers to "
-                "file %s.  File handle cannot be redefined within a "
-                 "session."),
-          tokid, handle->private->filename);
+      msg (SE, _("File handle %s is already defined.  "
+                 "Use CLOSE FILE HANDLE before redefining a file handle."),
+          handle_name);
       return CMD_FAILURE;
     }
 
@@ -135,183 +79,136 @@ cmd_file_handle (void)
   if (!parse_file_handle (&cmd))
     return CMD_FAILURE;
 
-  if (token != '.')
-    {
-      lex_error (_("expecting end of command"));
-      goto lossage;
-    }
+  if (lex_end_of_command () != CMD_SUCCESS)
+    goto lossage;
 
-  if (cmd.s_name == NULL)
+  if (cmd.s_name == NULL && cmd.mode != FH_SCRATCH)
     {
-      msg (SE, _("The FILE HANDLE required subcommand NAME "
-                "is not present."));
+      lex_sbc_missing ("NAME");
       goto lossage;
     }
 
-  handle = create_file_handle (handle_name, cmd.s_name);
   switch (cmd.mode)
     {
     case FH_CHARACTER:
-      handle->private->mode = MODE_TEXT;
+      properties.mode = FH_MODE_TEXT;
+      if (cmd.sbc_tabwidth)
+        properties.tab_width = cmd.n_tabwidth[0];
       break;
     case FH_IMAGE:
-      handle->private->mode = MODE_BINARY;
-      if (cmd.n_lrecl == NOT_LONG)
-       {
-         msg (SE, _("Fixed-length records were specified on /RECFORM, but "
-                     "record length was not specified on /LRECL.  "
-                     "Assuming 1024-character records."));
-          handle->private->length = 1024;
-       }
-      else if (cmd.n_lrecl < 1)
-       {
-         msg (SE, _("Record length (%ld) must be at least one byte.  "
-                    "1-character records will be assumed."), cmd.n_lrecl);
-          handle->private->length = 1;
-       }
+      properties.mode = FH_MODE_BINARY;
+      if (cmd.n_lrecl[0] == NOT_LONG)
+        msg (SE, _("Fixed-length records were specified on /RECFORM, but "
+                   "record length was not specified on /LRECL.  "
+                   "Assuming %d-character records."),
+             properties.record_width);
+      else if (cmd.n_lrecl[0] < 1)
+        msg (SE, _("Record length (%ld) must be at least one byte.  "
+                   "Assuming %d-character records."),
+             cmd.n_lrecl[0], properties.record_width);
       else
-        handle->private->length = cmd.n_lrecl;
+        properties.record_width = cmd.n_lrecl[0];
       break;
     default:
       assert (0);
     }
 
+  if (cmd.mode != FH_SCRATCH)
+    fh_create_file (handle_name, cmd.s_name, &properties);
+  else
+    fh_create_scratch (handle_name);
+
+  free_file_handle (&cmd);
   return CMD_SUCCESS;
 
  lossage:
   free_file_handle (&cmd);
   return CMD_FAILURE;
 }
-\f
-/* File handle functions. */
 
-/* Creates and returns a new file handle with the given values
-   and defaults for other values.  Adds the created file handle
-   to the global list. */
-static struct file_handle *
-create_file_handle (const char *handle_name, const char *filename)
+int
+cmd_close_file_handle (void) 
 {
   struct file_handle *handle;
-  struct file_handle_list *list;
-
-  /* Create and initialize file handle. */
-  handle = xmalloc (sizeof *handle);
-  handle->private = xmalloc (sizeof *handle->private);
-  handle->private->name = xstrdup (handle_name);
-  handle->private->filename = xstrdup (filename);
-  handle->private->identity = fn_get_identity (filename);
-  handle->private->where.filename = handle->private->filename;
-  handle->private->where.line_number = 0;
-  handle->private->mode = MODE_TEXT;
-  handle->private->length = 1024;
-  handle->ext = NULL;
-  handle->class = NULL;
-
-  /* Add file handle to global list. */
-  list = xmalloc (sizeof *list);
-  list->handle = handle;
-  list->next = file_handles;
-  file_handles = list;
 
-  return handle;
-}
+  if (!lex_force_id ())
+    return CMD_FAILURE;
+  handle = fh_from_name (tokid);
+  if (handle == NULL)
+    return CMD_FAILURE;
 
-/* Closes the stdio FILE associated with handle H.  Frees internal
-   buffers associated with that file.  Does *not* destroy the file
-   handle H.  (File handles are permanent during a session.)  */
-void
-fh_close_handle (struct file_handle *h)
-{
-  if (h == NULL)
-    return;
+  fh_free (handle);
 
-  if (h->class != NULL)
-    h->class->close (h);
-  h->class = NULL;
-  h->ext = NULL;
+  return CMD_SUCCESS;
 }
 
-/* Initialize the hash of file handles; inserts the "inline file"
-   inline_file. */
-void
-fh_init_files (void)
+/* Returns the name for REFERENT. */
+static const char *
+referent_name (enum fh_referent referent) 
 {
-  if (inline_file == NULL
+  switch (referent
     {
-      inline_file = create_file_handle ("INLINE", _("<Inline File>"));
-      inline_file->private->length = 80; 
+    case FH_REF_FILE:
+      return _("file");
+    case FH_REF_INLINE:
+      return _("inline file");
+    case FH_REF_SCRATCH:
+      return _("scratch file");
+    default:
+      abort ();
     }
 }
 
-/* Parses a file handle name, which may be a filename as a string or
-   a file handle name as an identifier.  Returns the file handle or
-   NULL on failure. */
+/* Parses a file handle name, which may be a filename as a string
+   or a file handle name as an identifier.  The allowed types of
+   file handle are restricted to those in REFERENT_MASK.  Returns
+   the file handle when successful, a null pointer on failure. */
 struct file_handle *
-fh_parse_file_handle (void)
+fh_parse (enum fh_referent referent_mask)
 {
   struct file_handle *handle;
 
-  if (token != T_ID && token != T_STRING)
+  if (lex_match_id ("INLINE")) 
+    handle = fh_inline_file ();
+  else 
     {
-      lex_error (_("expecting a file name or handle name"));
-      return NULL;
+      if (token != T_ID && token != T_STRING)
+        {
+          lex_error (_("expecting a file name or handle name"));
+          return NULL;
+        }
+
+      handle = NULL;
+      if (token == T_ID) 
+        handle = fh_from_name (tokid);
+      if (handle == NULL) 
+        handle = fh_from_filename (ds_c_str (&tokstr)); 
+      if (handle == NULL)
+        {
+          if (token != T_ID || tokid[0] != '#' || get_syntax () != ENHANCED) 
+            {
+              char *filename = ds_c_str (&tokstr);
+              char *handle_name = xasprintf ("\"%s\"", filename);
+              handle = fh_create_file (handle_name, filename,
+                                       fh_default_properties ());
+              free (handle_name);
+            }
+          else
+            handle = fh_create_scratch (tokid);
+        }
+      lex_get ();
     }
 
-  /* Check for named handles first, then go by filename. */
-  handle = NULL;
-  if (token == T_ID) 
-    handle = get_handle_with_name (tokid);
-  if (handle == NULL)
-    handle = get_handle_for_filename (ds_value (&tokstr));
-  if (handle == NULL) 
+  if (!(fh_get_referent (handle) & referent_mask)) 
     {
-      char *filename = ds_value (&tokstr);
-      char *handle_name = xmalloc (strlen (filename) + 3);
-      sprintf (handle_name, "\"%s\"", filename);
-      handle = create_file_handle (handle_name, filename);
-      free (handle_name);
+      msg (SE, _("Handle for %s not allowed here."),
+           referent_name (fh_get_referent (handle)));
+      return NULL;
     }
 
-  lex_get ();
-
   return handle;
 }
 
-/* Returns the identifier of file HANDLE.  If HANDLE was created
-   by referring to a filename instead of a handle name, returns
-   the filename, enclosed in double quotes.  Return value is
-   owned by the file handle. 
-
-   Useful for printing error messages about use of file handles.  */
-const char *
-handle_get_name (const struct file_handle *handle)
-{
-  return handle->private->name;
-}
-
-/* Returns the name of the file associated with HANDLE. */
-const char *
-handle_get_filename (const struct file_handle *handle) 
-{
-  return handle->private->filename;
-}
-
-/* Returns the mode of HANDLE. */
-enum file_handle_mode
-handle_get_mode (const struct file_handle *handle) 
-{
-  assert (handle != NULL);
-  return handle->private->mode;
-}
-
-/* Returns the width of a logical record on HANDLE. */
-size_t
-handle_get_record_width (const struct file_handle *handle)
-{
-  assert (handle != NULL);
-  return handle->private->length;
-}
-
 /*
    Local variables:
    mode: c