* elf64-mips.c (mips_elf64_be_swap_reloca_out): Handle type2 and type3.
[deliverable/binutils-gdb.git] / gdb / source.c
index dab794fb731b9140df803d933f21a77c0c6c8efa..c7c1ce2f5d29e1723ba24c9893f72d911df31f31 100644 (file)
@@ -1,6 +1,7 @@
 /* List lines of source files for GDB, the GNU debugger.
-   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
-   1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
+   1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
+   Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -42,9 +43,7 @@
 #include "linespec.h"
 #include "filenames.h"         /* for DOSish file names */
 #include "completer.h"
-#ifdef UI_OUT
 #include "ui-out.h"
-#endif
 
 #ifdef CRLF_SOURCE_FILES
 
@@ -501,9 +500,28 @@ source_info (char *ignore, int from_tty)
 
   printf_filtered ("Source language is %s.\n", language_str (s->language));
   printf_filtered ("Compiled with %s debugging format.\n", s->debugformat);
+  printf_filtered ("%s preprocessor macro info.\n",
+                   s->macro_table ? "Includes" : "Does not include");
 }
 \f
 
+/* Return True if the file NAME exists and is a regular file */
+static int
+is_regular_file (const char *name)
+{
+  struct stat st;
+  const int status = stat (name, &st);
+
+  /* Stat should never fail except when the file does not exist.
+     If stat fails, analyze the source of error and return True
+     unless the file does not exist, to avoid returning false results
+     on obscure systems where stat does not work as expected.
+   */
+  if (status != 0)
+    return (errno != ENOENT);
+
+  return S_ISREG (st.st_mode);
+}
 
 /* Open a file named STRING, searching path PATH (dir names sep by some char)
    using mode MODE and protection bits PROT in the calls to open.
@@ -515,7 +533,7 @@ source_info (char *ignore, int from_tty)
    get that particular version of foo or an error message).
 
    If FILENAME_OPENED is non-null, set it to a newly allocated string naming
-   the actual file opened (this string will always start with a "/".  We
+   the actual file opened (this string will always start with a "/").  We
    have to take special pains to avoid doubling the "/" between the directory
    and the file, sigh!  Emacs gets confuzzed by this when we print the
    source file name!!! 
@@ -524,7 +542,7 @@ source_info (char *ignore, int from_tty)
    Otherwise, return -1, with errno set for the last name we tried to open.  */
 
 /*  >>>> This should only allow files of certain types,
-   >>>>  eg executable, non-directory */
+    >>>>  eg executable, non-directory */
 int
 openp (const char *path, int try_cwd_first, const char *string,
        int mode, int prot,
@@ -544,7 +562,7 @@ openp (const char *path, int try_cwd_first, const char *string,
   mode |= O_BINARY;
 #endif
 
-  if (try_cwd_first || IS_ABSOLUTE_PATH (string))
+  if ((try_cwd_first || IS_ABSOLUTE_PATH (string)) && is_regular_file (string))
     {
       int i;
       filename = alloca (strlen (string) + 1);
@@ -602,18 +620,26 @@ openp (const char *path, int try_cwd_first, const char *string,
       strcat (filename + len, SLASH_STRING);
       strcat (filename, string);
 
-      fd = open (filename, mode);
-      if (fd >= 0)
-       break;
+      if (is_regular_file (filename))
+      {
+        fd = open (filename, mode);
+        if (fd >= 0)
+          break;
+      }
     }
 
 done:
   if (filename_opened)
     {
+      /* If a file was opened, canonicalize its filename. Use xfullpath
+         rather than gdb_realpath to avoid resolving the basename part
+         of filenames when the associated file is a symbolic link. This
+         fixes a potential inconsistency between the filenames known to
+         GDB and the filenames it prints in the annotations.  */
       if (fd < 0)
        *filename_opened = NULL;
       else if (IS_ABSOLUTE_PATH (filename))
-       *filename_opened = gdb_realpath (filename);
+       *filename_opened = xfullpath (filename);
       else
        {
          /* Beware the // my son, the Emacs barfs, the botch that catch... */
@@ -622,26 +648,10 @@ done:
            IS_DIR_SEPARATOR (current_directory[strlen (current_directory) - 1])
                                     ? "" : SLASH_STRING,
                                     filename, NULL);
-         *filename_opened = gdb_realpath (f);
+         *filename_opened = xfullpath (f);
          xfree (f);
        }
     }
-  /* OBSOLETE #ifdef MPW  */
-  /* OBSOLETE This is a debugging hack that can go away when all combinations */
-  /* OBSOLETE of Mac and Unix names are handled reasonably.  */
-  /* OBSOLETE   { */
-  /* OBSOLETE     extern int debug_openp; */
-  /* OBSOLETE  */
-  /* OBSOLETE     if (debug_openp) */
-  /* OBSOLETE       { */
-  /* OBSOLETE  printf ("openp on %s, path %s mode %d prot %d\n  returned %d", */
-  /* OBSOLETE          string, path, mode, prot, fd); */
-  /* OBSOLETE  if (*filename_opened) */
-  /* OBSOLETE    printf (" (filename is %s)", *filename_opened); */
-  /* OBSOLETE  printf ("\n"); */
-  /* OBSOLETE       } */
-  /* OBSOLETE   } */
-  /* OBSOLETE #endif  *//* MPW */
 
   return fd;
 }
@@ -727,22 +737,6 @@ open_source_file (struct symtab *s)
       if (p != s->filename)
        result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
     }
-  /* OBSOLETE #ifdef MPW */
-  /* OBSOLETE   if (result < 0) */
-  /* OBSOLETE     { */
-  /* OBSOLETE        *//* Didn't work.  Try using just the MPW basename. */
-  /* OBSOLETE       p = (char *) mpw_basename (s->filename); */
-  /* OBSOLETE       if (p != s->filename) */
-  /* OBSOLETE  result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname); */
-  /* OBSOLETE     } */
-  /* OBSOLETE   if (result < 0) */
-  /* OBSOLETE     { */
-  /* OBSOLETE        *//* Didn't work.  Try using the mixed Unix/MPW basename. */
-  /* OBSOLETE       p = (char *) mpw_mixed_basename (s->filename); */
-  /* OBSOLETE       if (p != s->filename) */
-  /* OBSOLETE  result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname); */
-  /* OBSOLETE     } */
-  /* OBSOLETE #endif MPW */
 
   if (result >= 0)
     {
@@ -1001,11 +995,9 @@ print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
   current_source_line = line;
   first_line_listed = line;
 
-#ifdef UI_OUT
   /* If printing of source lines is disabled, just print file and line number */
   if (ui_out_test_flags (uiout, ui_source_list))
     {
-#endif
       /* Only prints "No such file or directory" once */
       if ((s != last_source_visited) || (!last_source_error))
        {
@@ -1017,14 +1009,12 @@ print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
          desc = last_source_error;
          noerror = 1;
        }
-#ifdef UI_OUT
     }
   else
     {
       desc = -1;
       noerror = 1;
     }
-#endif
 
   if (desc < 0)
     {
@@ -1037,14 +1027,10 @@ print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
          print_sys_errmsg (name, errno);
        }
       else
-#ifdef UI_OUT
        ui_out_field_int (uiout, "line", line);
       ui_out_text (uiout, "\tin ");
       ui_out_field_string (uiout, "file", s->filename);
       ui_out_text (uiout, "\n");
-#else
-       printf_filtered ("%d\tin %s\n", line, s->filename);
-#endif
 
       return;
     }
@@ -1072,7 +1058,6 @@ print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
 
   while (nlines-- > 0)
     {
-#ifdef UI_OUT
       char buf[20];
 
       c = fgetc (stream);
@@ -1109,29 +1094,6 @@ print_source_lines_base (struct symtab *s, int line, int stopline, int noerror)
            }
        }
       while (c != '\n' && (c = fgetc (stream)) >= 0);
-#else
-      c = fgetc (stream);
-      if (c == EOF)
-       break;
-      last_line_listed = current_source_line;
-      printf_filtered ("%d\t", current_source_line++);
-      do
-       {
-         if (c < 040 && c != '\t' && c != '\n' && c != '\r')
-           printf_filtered ("^%c", c + 0100);
-         else if (c == 0177)
-           printf_filtered ("^?");
-#ifdef CRLF_SOURCE_FILES
-         else if (c == '\r')
-           {
-             /* Just skip \r characters.  */
-           }
-#endif
-         else
-           printf_filtered ("%c", c);
-       }
-      while (c != '\n' && (c = fgetc (stream)) >= 0);
-#endif
     }
 
   fclose (stream);
@@ -1651,7 +1613,7 @@ With no argument, reset the search path to $cdir:$cwd, the default.",
   if (dbx_commands)
     add_com_alias ("use", "directory", class_files, 0);
 
-  c->completer = filename_completer;
+  set_cmd_completer (c, filename_completer);
 
   add_cmd ("directories", no_class, show_directories,
           "Current search path for finding source files.\n\
This page took 0.026967 seconds and 4 git commands to generate.