update copyright year range in GDB files
[deliverable/binutils-gdb.git] / sim / ppc / table.c
index c331bc8c13a32cdd30d4ae2ae20d4c69b9e45f36..43c74622377c0c4731f7f95bfd92c1cf3446e615 100644 (file)
@@ -4,7 +4,7 @@
 
     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
-    the Free Software Foundation; either version 2 of the License, or
+    the Free Software Foundation; either version 3 of the License, or
     (at your option) any later version.
 
     This program is distributed in the hope that it will be useful,
@@ -13,8 +13,7 @@
     GNU General Public License for more details.
  
     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.
+    along with this program; if not, see <http://www.gnu.org/licenses/>.
  
     */
 
@@ -25,7 +24,7 @@
 #include <fcntl.h>
 #include <ctype.h>
 
-#include "config.h"
+#include "build-config.h"
 #include "misc.h"
 #include "lf.h"
 #include "table.h"
@@ -38,7 +37,8 @@
 #include <stdlib.h>
 #endif
 
-struct _table {
+typedef struct _open_table open_table;
+struct _open_table {
   size_t size;
   char *buffer;
   char *pos;
@@ -46,31 +46,70 @@ struct _table {
   int nr_fields;
   int nr_model_fields;
   char *file_name;
+  open_table *parent;
+  table *root;
+};
+struct _table {
+  open_table *current;
 };
 
-extern table *
-table_open(char *file_name,
-          int nr_fields,
-          int nr_model_fields)
+void
+table_push (table *root,
+           table_include *includes,
+           const char *file_name,
+           int nr_fields,
+           int nr_model_fields)
+
 {
   int fd;
   struct stat stat_buf;
-  table *file;
+  open_table *file;
+  table_include dummy;
+  table_include *include = &dummy;
+  int nr;
+
+  /* dummy up a search of this directory */
+  dummy.next = includes;
+  dummy.dir = "";
 
   /* create a file descriptor */
-  file = ZALLOC(table);
+  file = ZALLOC (open_table);
   ASSERT(file != NULL);
   file->nr_fields = nr_fields;
   file->nr_model_fields = nr_model_fields;
-
-  /* save the file name */
-  file->file_name = (char*)zalloc(strlen(file_name) + 1);
-  ASSERT(file->file_name != NULL);
-  strcpy(file->file_name, file_name);
-
-  /* open the file */
-  fd = open(file->file_name, O_RDONLY, 0);
-  ASSERT(fd >= 0);
+  file->root = root;
+  file->parent = root->current;
+  root->current = file;
+  
+  while (1)
+    {
+      /* save the file name */
+      char *dup_name = NZALLOC (char, strlen (include->dir) + strlen (file_name) + 2);
+      if (dup_name == NULL)
+       {
+         perror (file_name);
+         exit (1);
+       }
+      if (include->dir[0] != '\0')
+       {
+         strcat (dup_name, include->dir);
+         strcat (dup_name, "/");
+       }
+      strcat (dup_name, file_name);
+      file->file_name = dup_name;
+      /* open the file */
+      fd = open (dup_name, O_RDONLY, 0);
+      if (fd >= 0)
+       break;
+      /* free (dup_name); */
+      if (include->next == NULL)
+       {
+         error ("Problem opening file `%s'\n", file_name);
+         perror (file_name);
+         exit (1);
+       }
+      include = include->next;
+  }
 
   /* determine the size */
   if (fstat(fd, &stat_buf) < 0) {
@@ -88,26 +127,60 @@ table_open(char *file_name,
   file->pos = file->buffer;
 
   /* read it in */
-  if (read(fd, file->buffer, file->size) < file->size) {
+#ifdef __CYGWIN32__
+  if ((file->size) && ((nr = read(fd, file->buffer, file->size)) <= 0)) {
+#else
+  if ((nr = read(fd, file->buffer, file->size)) < file->size) {
+#endif
     perror("table_open.read");
     exit(1);
   }
+  file->size = nr;
   file->buffer[file->size] = '\0';
 
   /* done */
   close(fd);
-  return file;
 }
 
+extern table *
+table_open(const char *file_name,
+          int nr_fields,
+          int nr_model_fields)
+{
+  table *root;
+
+  /* create a file descriptor */
+  root = ZALLOC (table);
+  if (root == NULL)
+    {
+      perror (file_name);
+      exit (1);
+    }
+
+  table_push (root, NULL, file_name, nr_fields, nr_model_fields);
+  return root;
+}
 
 extern table_entry *
-table_entry_read(table *file)
+table_entry_read(table *root)
 {
+  open_table *file = root->current;
   int field;
   table_entry *entry;
 
   /* skip comments/blanks */
   while(1) {
+    /* end-of-file? */
+    while (*file->pos == '\0')
+      {
+        if (file->parent != NULL)
+          {
+            file = file->parent;
+            root->current = file;
+          }
+        else
+          return NULL;
+      }
     /* leading white space */
     while (*file->pos != '\0'
           && *file->pos != '\n'
@@ -127,8 +200,6 @@ table_entry_read(table *file)
     else
       break;
   }
-  if (*file->pos == '\0')
-    return NULL;
 
   /* create this new entry */
   entry = (table_entry*)zalloc(sizeof(table_entry)
@@ -159,7 +230,6 @@ table_entry_read(table *file)
     file->pos++;
   }
   file->line_nr++;
-  entry->line_nr = file->line_nr;
 
   /* if following lines begin with a star, add them to the model
      section.  */
@@ -199,6 +269,8 @@ table_entry_read(table *file)
     model->line_nr = file->line_nr;
   }
 
+  entry->line_nr = file->line_nr;
+
   /* if following lines are tab indented, put in the annex */
   if (*file->pos == '\t') {
     entry->annex = file->pos;
@@ -263,10 +335,10 @@ dump_table_entry(table_entry *entry,
 
 
 extern void
-table_entry_lf_c_line_nr(lf *file,
-                        table_entry *entry)
+table_entry_print_cpp_line_nr(lf *file,
+                             table_entry *entry)
 {
-  lf_print_c_line_nr(file, entry->line_nr, entry->file_name);
+  lf_print__external_reference(file, entry->line_nr, entry->file_name);
 }
 
 
This page took 0.027334 seconds and 4 git commands to generate.