1998-09-08 Jason Molenda (jsm@bugshack.cygnus.com)
[deliverable/binutils-gdb.git] / bfd / format.c
index 310a305c903b506a2d69824b59090ddb2e19d111..fb9fdc0d160c02e8851617f787a600df74023fa7 100644 (file)
@@ -1,5 +1,5 @@
 /* Generic BFD support for file formats.
-   Copyright (C) 1990-1991 Free Software Foundation, Inc.
+   Copyright (C) 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
    Written by Cygnus Support.
 
 This file is part of BFD, the Binary File Descriptor library.
@@ -45,7 +45,7 @@ SECTION
 #include "libbfd.h"
 
 /* IMPORT from targets.c.  */
-extern char *matching_vector[];
+extern CONST size_t _bfd_target_vector_entries;
 
 /*
 FUNCTION
@@ -83,15 +83,43 @@ DESCRIPTION
 
        o <<file_ambiguously_recognized>> -
        more than one backend recognised the file format.
+*/
+
+boolean
+bfd_check_format (abfd, format)
+     bfd *abfd;
+     bfd_format format;
+{
+  return bfd_check_format_matches (abfd, format, NULL);
+}
+
+/*
+FUNCTION
+       bfd_check_format_matches
+
+SYNOPSIS
+       boolean bfd_check_format_matches(bfd *abfd, bfd_format format, char ***matching);
 
+DESCRIPTION
+       Like <<bfd_check_format>>, except when it returns false with
+       <<bfd_errno>> set to <<file_ambiguously_recognized>>.  In that
+       case, if @var{matching} is not NULL, it will be filled in with
+       a NULL-terminated list of the names of the formats that matched,
+       allocated with <<malloc>>.
+       Then the user may choose a format and try again.
+
+       When done with the list that @var{matching} points to, the caller
+       should free it.  
 */
 
 boolean
-DEFUN(bfd_check_format,(abfd, format),
-      bfd *abfd AND
-      bfd_format format)
+bfd_check_format_matches (abfd, format, matching)
+     bfd *abfd;
+     bfd_format format;
+     char ***matching;
 {
   bfd_target **target, *save_targ, *right_targ;
+  char **matching_vector;
   int match_count;
 
   if (!bfd_read_p (abfd) ||
@@ -110,7 +138,13 @@ DEFUN(bfd_check_format,(abfd, format),
 
   save_targ = abfd->xvec;
   match_count = 0;
-  matching_vector[0] = NULL;
+  if (matching)
+    {
+      *matching = matching_vector =
+       (char **) bfd_xmalloc_by_size_t (sizeof (char *) *
+                                        (_bfd_target_vector_entries + 1));
+      matching_vector[0] = NULL;
+    }
   right_targ = 0;
 
 
@@ -125,11 +159,13 @@ DEFUN(bfd_check_format,(abfd, format),
     right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
     if (right_targ) {
       abfd->xvec = right_targ;         /* Set the target as returned */
+      if (matching)
+       free (matching_vector);
       return true;                     /* File position has moved, BTW */
     }
   }
 
-  for (target = target_vector; *target != NULL; target++) {
+  for (target = bfd_target_vector; *target != NULL; target++) {
     bfd_target *temp;
 
     abfd->xvec = *target;      /* Change BFD's target temporarily */
@@ -141,16 +177,23 @@ DEFUN(bfd_check_format,(abfd, format),
     temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
     if (temp) {                                /* This format checks out as ok! */
       right_targ = temp;
-      matching_vector[match_count++] = temp->name;
-      matching_vector[match_count] = NULL;
+      if (matching)
+       {
+         matching_vector[match_count] = temp->name;
+         matching_vector[match_count + 1] = NULL;
+       }
+      match_count++;
       /* If this is the default target, accept it, even if other targets
         might match.  People who want those other targets have to set 
         the GNUTARGET variable.  */
-      if (temp == default_vector[0])
+      if (temp == bfd_default_vector[0])
        {
+         if (matching)
+           {
+             matching_vector[0] = temp->name;
+             matching_vector[1] = NULL;
+           }
          match_count = 1;
-         matching_vector[0] = temp->name;
-         matching_vector[1] = NULL;
          break;
        }
 #ifdef GNU960
@@ -165,42 +208,32 @@ DEFUN(bfd_check_format,(abfd, format),
     } else if (bfd_error != wrong_format) {
       abfd->xvec = save_targ;
       abfd->format = bfd_unknown;
+      if (matching && bfd_error != file_ambiguously_recognized)
+       free (matching_vector);
       return false;
     }
   }
 
   if (match_count == 1) {
     abfd->xvec = right_targ;           /* Change BFD's target permanently */
+    if (matching)
+      free (matching_vector);
     return true;                       /* File position has moved, BTW */
   }
 
   abfd->xvec = save_targ;              /* Restore original target type */
   abfd->format = bfd_unknown;          /* Restore original format */
-  bfd_error = ((match_count == 0) ? file_not_recognized :
-              file_ambiguously_recognized);
+  if (match_count == 0)
+    {
+      bfd_error = file_not_recognized;
+      if (matching)
+       free (matching_vector);
+    }
+  else
+    bfd_error = file_ambiguously_recognized;
   return false;
 }
 
-/*
-FUNCTION
-       bfd_matching_formats
-
-SYNOPSIS
-       char **bfd_matching_formats();
-
-DESCRIPTION
-       If a call to <<bfd_check_format>> returns
-       <<file_ambiguously_recognized>>, you can call this function
-       afterward to return a NULL-terminated list of the names of
-       the formats that matched.
-       Then you can choose one and try again.  */
-
-char **
-bfd_matching_formats ()
-{
-  return &matching_vector[0];
-}
-
 /*
 FUNCTION
        bfd_set_format
@@ -217,9 +250,9 @@ DESCRIPTION
 */
 
 boolean
-DEFUN(bfd_set_format,(abfd, format),
-      bfd *abfd AND
-      bfd_format format)
+bfd_set_format (abfd, format)
+     bfd *abfd;
+     bfd_format format;
 {
 
   if (bfd_read_p (abfd) ||
@@ -258,8 +291,8 @@ DESCRIPTION
 */
 
 CONST char *
-DEFUN(bfd_format_string,(format),
-     bfd_format format)
+bfd_format_string (format)
+     bfd_format format;
 {
   if (((int)format <(int) bfd_unknown) 
       || ((int)format >=(int) bfd_type_end)) 
This page took 0.029085 seconds and 4 git commands to generate.