* configure.in: Move termcap determination a later in the file to catch
[deliverable/binutils-gdb.git] / gdb / source.c
index e2ceb91cc328e5add4606977b3596fa79a2d4eea..997cd1d89a6a505051b01f546bf00a50eeacb777 100644 (file)
@@ -1,5 +1,5 @@
 /* List lines of source files for GDB, the GNU debugger.
-   Copyright 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995
+   Copyright 1986, 87, 88, 89, 91, 92, 93, 94, 95, 96, 97, 1998
    Free Software Foundation, Inc.
 
 This file is part of GDB.
@@ -16,7 +16,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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "defs.h"
 #include "symtab.h"
@@ -25,22 +25,42 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "command.h"
 #include "gdbcmd.h"
 #include "frame.h"
+#include "value.h"
 
 #include <sys/types.h>
 #include "gdb_string.h"
-#include <sys/param.h>
 #include "gdb_stat.h"
 #include <fcntl.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #include "gdbcore.h"
-#include "regex.h"
+#include "gnu-regex.h"
 #include "symfile.h"
 #include "objfiles.h"
 #include "annotate.h"
 #include "gdbtypes.h"
 
+#ifdef CRLF_SOURCE_FILES
+
+/* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
+   host use \r\n rather than just \n.  Defining CRLF_SOURCE_FILES is
+   much faster than defining LSEEK_NOT_LINEAR.  */
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+#define OPEN_MODE (O_RDONLY | O_BINARY)
+#define FDOPEN_MODE FOPEN_RB
+
+#else /* ! defined (CRLF_SOURCE_FILES) */
+
+#define OPEN_MODE O_RDONLY
+#define FDOPEN_MODE FOPEN_RT
+
+#endif /* ! defined (CRLF_SOURCE_FILES) */
+
 /* Prototypes for local functions. */
 
 static int open_source_file PARAMS ((struct symtab *));
@@ -63,11 +83,6 @@ static void show_directories PARAMS ((char *, int));
 
 static void find_source_lines PARAMS ((struct symtab *, int));
 
-/* If we use this declaration, it breaks because of fucking ANSI "const" stuff
-   on some systems.  We just have to not declare it at all, have it default
-   to int, and possibly botch on a few systems.  Thanks, ANSIholes... */
-/* extern char *strstr(); */
-
 /* Path of directories to search for source files.
    Same format as the PATH environment variable's value.  */
 
@@ -184,6 +199,8 @@ select_source_symtab (s)
          current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
        }
     }
+  if (current_source_symtab)
+    return;
 
   error ("Can't find a default source file");
 }
@@ -306,9 +323,12 @@ mod_path (dirname, which_path)
          }
       }
 
+#ifndef WIN32 
+      /* On win32 h:\ is different to h: */
       if (SLASH_P (p[-1]))
        /* Sigh. "foo/" => "foo" */
        --p;
+#endif
       *p = '\0';
 
       while (p[-1] == '.')
@@ -341,7 +361,7 @@ mod_path (dirname, which_path)
 
       if (name[0] == '~')
        name = tilde_expand (name);
-      else if (!SLASH_P (name[0]) && name[0] != '$') 
+      else if (!ROOTED_P (name) && name[0] != '$') 
          name = concat (current_directory, SLASH_STRING, name, NULL);
       else
        name = savestring (name, p - name);
@@ -446,7 +466,8 @@ source_info (ignore, from_tty)
     printf_filtered ("Contains %d line%s.\n", s->nlines,
                     s->nlines == 1 ? "" : "s");
 
-  printf_filtered("Source language is %s.\n", language_str (s->language));
+  printf_filtered ("Source language is %s.\n", language_str (s->language));
+  printf_filtered ("Compiled with %s debugging format.\n", s->debugformat);
 }
 
 
@@ -489,6 +510,10 @@ openp (path, try_cwd_first, string, mode, prot, filename_opened)
   if (!path)
     path = ".";
 
+#ifdef WIN32
+  mode |= O_BINARY;
+#endif
+
   if (try_cwd_first || SLASH_P (string[0]))
     {
       int i;
@@ -497,8 +522,8 @@ openp (path, try_cwd_first, string, mode, prot, filename_opened)
       if (fd >= 0)
        goto done;
       for (i = 0; string[i]; i++)
-       if (SLASH_P(string[0]))
-       goto done;
+       if (SLASH_P (string[i]))
+         goto done;
     }
 
   /* ./foo => foo */
@@ -517,7 +542,7 @@ openp (path, try_cwd_first, string, mode, prot, filename_opened)
        len = strlen (p);
 
       if (len == 4 && p[0] == '$' && p[1] == 'c'
-                  && p[2] == 'w' && p[3] == 'd') {
+         && p[2] == 'w' && p[3] == 'd') {
        /* Name is $cwd -- insert current directory name instead.  */
        int newlen;
 
@@ -537,7 +562,7 @@ openp (path, try_cwd_first, string, mode, prot, filename_opened)
 
       /* Remove trailing slashes */
       while (len > 0 && SLASH_P (filename[len-1]))
-       filename[--len] = 0;
+       filename[--len] = 0;
 
       strcat (filename+len, SLASH_STRING);
       strcat (filename, string);
@@ -599,7 +624,7 @@ open_source_file (s)
   /* Quick way out if we already know its full name */
   if (s->fullname) 
     {
-      result = open (s->fullname, O_RDONLY);
+      result = open (s->fullname, OPEN_MODE);
       if (result >= 0)
         return result;
       /* Didn't work -- free old one, try again. */
@@ -628,13 +653,13 @@ open_source_file (s)
        }
     }
 
-  result = openp (path, 0, s->filename, O_RDONLY, 0, &s->fullname);
+  result = openp (path, 0, s->filename, OPEN_MODE, 0, &s->fullname);
   if (result < 0)
     {
       /* Didn't work.  Try using just the basename. */
       p = basename (s->filename);
       if (p != s->filename)
-       result = openp (path, 0, p, O_RDONLY, 0, &s->fullname);
+       result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
     }
 #ifdef MPW
   if (result < 0)
@@ -642,14 +667,14 @@ open_source_file (s)
       /* Didn't work.  Try using just the MPW basename. */
       p = (char *) mpw_basename (s->filename);
       if (p != s->filename)
-       result = openp (path, 0, p, O_RDONLY, 0, &s->fullname);
+       result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
     }
   if (result < 0)
     {
       /* Didn't work.  Try using the mixed Unix/MPW basename. */
       p = (char *) mpw_mixed_basename (s->filename);
       if (p != s->filename)
-       result = openp (path, 0, p, O_RDONLY, 0, &s->fullname);
+       result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
     }
 #endif /* MPW */
 
@@ -707,7 +732,7 @@ find_source_lines (s, desc)
   int nlines = 0;
   int lines_allocated = 1000;
   int *line_charpos;
-  long exec_mtime;
+  long mtime;
   int size;
 
   line_charpos = (int *) xmmalloc (s -> objfile -> md,
@@ -715,10 +740,16 @@ find_source_lines (s, desc)
   if (fstat (desc, &st) < 0)
     perror_with_name (s->filename);
 
-  if (exec_bfd)
+  if (s && s->objfile && s->objfile->obfd)
     {
-      exec_mtime = bfd_get_mtime(exec_bfd);
-      if (exec_mtime && exec_mtime < st.st_mtime)
+      mtime = bfd_get_mtime(s->objfile->obfd);
+      if (mtime && mtime < st.st_mtime)
+       printf_filtered ("Source file is more recent than executable.\n");
+    }
+  else if (exec_bfd)
+    {
+      mtime = bfd_get_mtime(exec_bfd);
+      if (mtime && mtime < st.st_mtime)
        printf_filtered ("Source file is more recent than executable.\n");
     }
 
@@ -728,7 +759,7 @@ find_source_lines (s, desc)
 
     /* Have to read it byte by byte to find out where the chars live */
 
-    line_charpos[0] = tell(desc);
+    line_charpos[0] = lseek (desc, 0, SEEK_CUR);
     nlines = 1;
     while (myread(desc, &c, 1)>0) 
       {
@@ -741,7 +772,7 @@ find_source_lines (s, desc)
                  (int *) xmrealloc (s -> objfile -> md, (char *) line_charpos,
                                     sizeof (int) * lines_allocated);
              }
-           line_charpos[nlines++] = tell(desc);
+           line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
          }
       }
   }
@@ -758,7 +789,9 @@ find_source_lines (s, desc)
     data = (char *) xmalloc (size);
     old_cleanups = make_cleanup (free, data);
 
-    if (myread (desc, data, size) < 0)
+    /* Reassign `size' to result of read for systems where \r\n -> \n.  */
+    size = myread (desc, data, size);
+    if (size < 0)
       perror_with_name (s->filename);
     end = data + size;
     p = data;
@@ -939,7 +972,7 @@ print_source_lines (s, line, stopline, noerror)
       perror_with_name (s->filename);
     }
 
-  stream = fdopen (desc, FOPEN_RT);
+  stream = fdopen (desc, FDOPEN_MODE);
   clearerr (stream);
 
   while (nlines-- > 0)
@@ -954,6 +987,12 @@ print_source_lines (s, line, stopline, noerror)
              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);
@@ -1164,11 +1203,12 @@ line_info (arg, from_tty)
   CORE_ADDR start_pc, end_pc;
   int i;
 
+  INIT_SAL (&sal);     /* initialize to zeroes */
+
   if (arg == 0)
     {
       sal.symtab = current_source_symtab;
       sal.line = last_line_listed;
-      sal.pc = 0;
       sals.nelts = 1;
       sals.sals = (struct symtab_and_line *)
        xmalloc (sizeof (struct symtab_and_line));
@@ -1292,7 +1332,7 @@ forward_search_command (regex, from_tty)
       perror_with_name (current_source_symtab->filename);
     }
 
-  stream = fdopen (desc, FOPEN_RT);
+  stream = fdopen (desc, FDOPEN_MODE);
   clearerr (stream);
   while (1) {
     static char *buf = NULL;
@@ -1377,7 +1417,7 @@ reverse_search_command (regex, from_tty)
       perror_with_name (current_source_symtab->filename);
     }
 
-  stream = fdopen (desc, FOPEN_RT);
+  stream = fdopen (desc, FDOPEN_MODE);
   clearerr (stream);
   while (line > 1)
     {
This page took 0.028534 seconds and 4 git commands to generate.