OSDN Git Service

Change output of `nm --help' to include a description of the purpose of the
[pf3gnuchains/pf3gnuchains4x.git] / gdb / source.c
1 /* List lines of source files for GDB, the GNU debugger.
2    Copyright 1986-1989, 1991-1999 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "expression.h"
24 #include "language.h"
25 #include "command.h"
26 #include "source.h"
27 #include "gdbcmd.h"
28 #include "frame.h"
29 #include "value.h"
30
31 #include <sys/types.h>
32 #include "gdb_string.h"
33 #include "gdb_stat.h"
34 #include <fcntl.h>
35 #include "gdbcore.h"
36 #include "gdb_regex.h"
37 #include "symfile.h"
38 #include "objfiles.h"
39 #include "annotate.h"
40 #include "gdbtypes.h"
41 #ifdef UI_OUT
42 #include "ui-out.h"
43 #endif
44
45 #ifdef CRLF_SOURCE_FILES
46
47 /* Define CRLF_SOURCE_FILES in an xm-*.h file if source files on the
48    host use \r\n rather than just \n.  Defining CRLF_SOURCE_FILES is
49    much faster than defining LSEEK_NOT_LINEAR.  */
50
51 #ifndef O_BINARY
52 #define O_BINARY 0
53 #endif
54
55 #define OPEN_MODE (O_RDONLY | O_BINARY)
56 #define FDOPEN_MODE FOPEN_RB
57
58 #else /* ! defined (CRLF_SOURCE_FILES) */
59
60 #define OPEN_MODE O_RDONLY
61 #define FDOPEN_MODE FOPEN_RT
62
63 #endif /* ! defined (CRLF_SOURCE_FILES) */
64
65 /* Prototypes for exported functions. */
66
67 void _initialize_source (void);
68
69 /* Prototypes for local functions. */
70
71 static int get_filename_and_charpos (struct symtab *, char **);
72
73 static void reverse_search_command (char *, int);
74
75 static void forward_search_command (char *, int);
76
77 static void line_info (char *, int);
78
79 static void list_command (char *, int);
80
81 static void ambiguous_line_spec (struct symtabs_and_lines *);
82
83 static void source_info (char *, int);
84
85 static void show_directories (char *, int);
86
87 /* Path of directories to search for source files.
88    Same format as the PATH environment variable's value.  */
89
90 char *source_path;
91
92 /* Symtab of default file for listing lines of.  */
93
94 struct symtab *current_source_symtab;
95
96 /* Default next line to list.  */
97
98 int current_source_line;
99
100 /* Default number of lines to print with commands like "list".
101    This is based on guessing how many long (i.e. more than chars_per_line
102    characters) lines there will be.  To be completely correct, "list"
103    and friends should be rewritten to count characters and see where
104    things are wrapping, but that would be a fair amount of work.  */
105
106 int lines_to_list = 10;
107
108 /* Line number of last line printed.  Default for various commands.
109    current_source_line is usually, but not always, the same as this.  */
110
111 static int last_line_listed;
112
113 /* First line number listed by last listing command.  */
114
115 static int first_line_listed;
116
117 /* Saves the name of the last source file visited and a possible error code.
118    Used to prevent repeating annoying "No such file or directories" msgs */
119
120 static struct symtab *last_source_visited = NULL;
121 static int last_source_error = 0;
122 \f
123
124 /* Set the source file default for the "list" command to be S.
125
126    If S is NULL, and we don't have a default, find one.  This
127    should only be called when the user actually tries to use the
128    default, since we produce an error if we can't find a reasonable
129    default.  Also, since this can cause symbols to be read, doing it
130    before we need to would make things slower than necessary.  */
131
132 void
133 select_source_symtab (s)
134      register struct symtab *s;
135 {
136   struct symtabs_and_lines sals;
137   struct symtab_and_line sal;
138   struct partial_symtab *ps;
139   struct partial_symtab *cs_pst = 0;
140   struct objfile *ofp;
141
142   if (s)
143     {
144       current_source_symtab = s;
145       current_source_line = 1;
146       return;
147     }
148
149   if (current_source_symtab)
150     return;
151
152   /* Make the default place to list be the function `main'
153      if one exists.  */
154   if (lookup_symbol ("main", 0, VAR_NAMESPACE, 0, NULL))
155     {
156       sals = decode_line_spec ("main", 1);
157       sal = sals.sals[0];
158       free (sals.sals);
159       current_source_symtab = sal.symtab;
160       current_source_line = max (sal.line - (lines_to_list - 1), 1);
161       if (current_source_symtab)
162         return;
163     }
164
165   /* All right; find the last file in the symtab list (ignoring .h's).  */
166
167   current_source_line = 1;
168
169   for (ofp = object_files; ofp != NULL; ofp = ofp->next)
170     {
171       for (s = ofp->symtabs; s; s = s->next)
172         {
173           char *name = s->filename;
174           int len = strlen (name);
175           if (!(len > 2 && (STREQ (&name[len - 2], ".h"))))
176             {
177               current_source_symtab = s;
178             }
179         }
180     }
181   if (current_source_symtab)
182     return;
183
184   /* Howabout the partial symbol tables? */
185
186   for (ofp = object_files; ofp != NULL; ofp = ofp->next)
187     {
188       for (ps = ofp->psymtabs; ps != NULL; ps = ps->next)
189         {
190           char *name = ps->filename;
191           int len = strlen (name);
192           if (!(len > 2 && (STREQ (&name[len - 2], ".h"))))
193             {
194               cs_pst = ps;
195             }
196         }
197     }
198   if (cs_pst)
199     {
200       if (cs_pst->readin)
201         {
202           internal_error ("select_source_symtab: readin pst found and no symtabs.");
203         }
204       else
205         {
206           current_source_symtab = PSYMTAB_TO_SYMTAB (cs_pst);
207         }
208     }
209   if (current_source_symtab)
210     return;
211
212   error ("Can't find a default source file");
213 }
214 \f
215 static void
216 show_directories (ignore, from_tty)
217      char *ignore;
218      int from_tty;
219 {
220   puts_filtered ("Source directories searched: ");
221   puts_filtered (source_path);
222   puts_filtered ("\n");
223 }
224
225 /* Forget what we learned about line positions in source files, and
226    which directories contain them; must check again now since files
227    may be found in a different directory now.  */
228
229 void
230 forget_cached_source_info ()
231 {
232   register struct symtab *s;
233   register struct objfile *objfile;
234
235   for (objfile = object_files; objfile != NULL; objfile = objfile->next)
236     {
237       for (s = objfile->symtabs; s != NULL; s = s->next)
238         {
239           if (s->line_charpos != NULL)
240             {
241               mfree (objfile->md, s->line_charpos);
242               s->line_charpos = NULL;
243             }
244           if (s->fullname != NULL)
245             {
246               mfree (objfile->md, s->fullname);
247               s->fullname = NULL;
248             }
249         }
250     }
251 }
252
253 void
254 init_source_path ()
255 {
256   char buf[20];
257
258   sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR);
259   source_path = strsave (buf);
260   forget_cached_source_info ();
261 }
262
263 /* Add zero or more directories to the front of the source path.  */
264
265 void
266 directory_command (dirname, from_tty)
267      char *dirname;
268      int from_tty;
269 {
270   dont_repeat ();
271   /* FIXME, this goes to "delete dir"... */
272   if (dirname == 0)
273     {
274       if (from_tty && query ("Reinitialize source path to empty? "))
275         {
276           free (source_path);
277           init_source_path ();
278         }
279     }
280   else
281     {
282       mod_path (dirname, &source_path);
283       last_source_visited = NULL;
284     }
285   if (from_tty)
286     show_directories ((char *) 0, from_tty);
287   forget_cached_source_info ();
288 }
289
290 /* Add zero or more directories to the front of an arbitrary path.  */
291
292 void
293 mod_path (dirname, which_path)
294      char *dirname;
295      char **which_path;
296 {
297   char *old = *which_path;
298   int prefix = 0;
299
300   if (dirname == 0)
301     return;
302
303   dirname = strsave (dirname);
304   make_cleanup (free, dirname);
305
306   do
307     {
308       char *name = dirname;
309       register char *p;
310       struct stat st;
311
312       {
313         char *separator = strchr (name, DIRNAME_SEPARATOR);
314         char *space = strchr (name, ' ');
315         char *tab = strchr (name, '\t');
316
317         if (separator == 0 && space == 0 && tab == 0)
318           p = dirname = name + strlen (name);
319         else
320           {
321             p = 0;
322             if (separator != 0 && (p == 0 || separator < p))
323               p = separator;
324             if (space != 0 && (p == 0 || space < p))
325               p = space;
326             if (tab != 0 && (p == 0 || tab < p))
327               p = tab;
328             dirname = p + 1;
329             while (*dirname == DIRNAME_SEPARATOR
330                    || *dirname == ' '
331                    || *dirname == '\t')
332               ++dirname;
333           }
334       }
335
336       if (!(SLASH_P (*name) && p <= name + 1)   /* "/" */
337 #if defined(_WIN32) || defined(__MSDOS__)
338       /* On MS-DOS and MS-Windows, h:\ is different from h: */
339           && !(!SLASH_P (*name) && ROOTED_P (name) && p <= name + 3)    /* d:/ */
340 #endif
341           && SLASH_P (p[-1]))
342         /* Sigh. "foo/" => "foo" */
343         --p;
344       *p = '\0';
345
346       while (p > name && p[-1] == '.')
347         {
348           if (p - name == 1)
349             {
350               /* "." => getwd ().  */
351               name = current_directory;
352               goto append;
353             }
354           else if (p > name + 1 && SLASH_P (p[-2]))
355             {
356               if (p - name == 2)
357                 {
358                   /* "/." => "/".  */
359                   *--p = '\0';
360                   goto append;
361                 }
362               else
363                 {
364                   /* "...foo/." => "...foo".  */
365                   p -= 2;
366                   *p = '\0';
367                   continue;
368                 }
369             }
370           else
371             break;
372         }
373
374       if (name[0] == '~')
375         name = tilde_expand (name);
376 #if defined(_WIN32) || defined(__MSDOS__)
377       else if (ROOTED_P (name) && p == name + 2)        /* "d:" => "d:." */
378         name = concat (name, ".", NULL);
379 #endif
380       else if (!ROOTED_P (name) && name[0] != '$')
381         name = concat (current_directory, SLASH_STRING, name, NULL);
382       else
383         name = savestring (name, p - name);
384       make_cleanup (free, name);
385
386       /* Unless it's a variable, check existence.  */
387       if (name[0] != '$')
388         {
389           /* These are warnings, not errors, since we don't want a
390              non-existent directory in a .gdbinit file to stop processing
391              of the .gdbinit file.
392
393              Whether they get added to the path is more debatable.  Current
394              answer is yes, in case the user wants to go make the directory
395              or whatever.  If the directory continues to not exist/not be
396              a directory/etc, then having them in the path should be
397              harmless.  */
398           if (stat (name, &st) < 0)
399             {
400               int save_errno = errno;
401               fprintf_unfiltered (gdb_stderr, "Warning: ");
402               print_sys_errmsg (name, save_errno);
403             }
404           else if ((st.st_mode & S_IFMT) != S_IFDIR)
405             warning ("%s is not a directory.", name);
406         }
407
408     append:
409       {
410         register unsigned int len = strlen (name);
411
412         p = *which_path;
413         while (1)
414           {
415             /* FIXME: strncmp loses in interesting ways on MS-DOS and
416                MS-Windows because of case-insensitivity and two different
417                but functionally identical slash characters.  We need a
418                special filesystem-dependent file-name comparison function.
419
420                Actually, even on Unix I would use realpath() or its work-
421                alike before comparing.  Then all the code above which
422                removes excess slashes and dots could simply go away.  */
423             if (!strncmp (p, name, len)
424                 && (p[len] == '\0' || p[len] == DIRNAME_SEPARATOR))
425               {
426                 /* Found it in the search path, remove old copy */
427                 if (p > *which_path)
428                   p--;          /* Back over leading separator */
429                 if (prefix > p - *which_path)
430                   goto skip_dup;        /* Same dir twice in one cmd */
431                 strcpy (p, &p[len + 1]);        /* Copy from next \0 or  : */
432               }
433             p = strchr (p, DIRNAME_SEPARATOR);
434             if (p != 0)
435               ++p;
436             else
437               break;
438           }
439         if (p == 0)
440           {
441             char tinybuf[2];
442
443             tinybuf[0] = DIRNAME_SEPARATOR;
444             tinybuf[1] = '\0';
445
446             /* If we have already tacked on a name(s) in this command,                     be sure they stay on the front as we tack on some more.  */
447             if (prefix)
448               {
449                 char *temp, c;
450
451                 c = old[prefix];
452                 old[prefix] = '\0';
453                 temp = concat (old, tinybuf, name, NULL);
454                 old[prefix] = c;
455                 *which_path = concat (temp, "", &old[prefix], NULL);
456                 prefix = strlen (temp);
457                 free (temp);
458               }
459             else
460               {
461                 *which_path = concat (name, (old[0] ? tinybuf : old), old, NULL);
462                 prefix = strlen (name);
463               }
464             free (old);
465             old = *which_path;
466           }
467       }
468     skip_dup:;
469     }
470   while (*dirname != '\0');
471 }
472
473
474 static void
475 source_info (ignore, from_tty)
476      char *ignore;
477      int from_tty;
478 {
479   register struct symtab *s = current_source_symtab;
480
481   if (!s)
482     {
483       printf_filtered ("No current source file.\n");
484       return;
485     }
486   printf_filtered ("Current source file is %s\n", s->filename);
487   if (s->dirname)
488     printf_filtered ("Compilation directory is %s\n", s->dirname);
489   if (s->fullname)
490     printf_filtered ("Located in %s\n", s->fullname);
491   if (s->nlines)
492     printf_filtered ("Contains %d line%s.\n", s->nlines,
493                      s->nlines == 1 ? "" : "s");
494
495   printf_filtered ("Source language is %s.\n", language_str (s->language));
496   printf_filtered ("Compiled with %s debugging format.\n", s->debugformat);
497 }
498 \f
499
500
501 /* Open a file named STRING, searching path PATH (dir names sep by some char)
502    using mode MODE and protection bits PROT in the calls to open.
503
504    If TRY_CWD_FIRST, try to open ./STRING before searching PATH.
505    (ie pretend the first element of PATH is ".").  This also indicates
506    that a slash in STRING disables searching of the path (this is
507    so that "exec-file ./foo" or "symbol-file ./foo" insures that you
508    get that particular version of foo or an error message).
509
510    If FILENAMED_OPENED is non-null, set it to a newly allocated string naming
511    the actual file opened (this string will always start with a "/".  We
512    have to take special pains to avoid doubling the "/" between the directory
513    and the file, sigh!  Emacs gets confuzzed by this when we print the
514    source file name!!! 
515
516    If a file is found, return the descriptor.
517    Otherwise, return -1, with errno set for the last name we tried to open.  */
518
519 /*  >>>> This should only allow files of certain types,
520    >>>>  eg executable, non-directory */
521 int
522 openp (path, try_cwd_first, string, mode, prot, filename_opened)
523      char *path;
524      int try_cwd_first;
525      char *string;
526      int mode;
527      int prot;
528      char **filename_opened;
529 {
530   register int fd;
531   register char *filename;
532   register char *p, *p1;
533   register int len;
534   int alloclen;
535
536   if (!path)
537     path = ".";
538
539 #ifdef _WIN32
540   mode |= O_BINARY;
541 #endif
542
543   if (try_cwd_first || ROOTED_P (string))
544     {
545       int i;
546       filename = string;
547       fd = open (filename, mode, prot);
548       if (fd >= 0)
549         goto done;
550       for (i = 0; string[i]; i++)
551         if (SLASH_P (string[i]))
552           goto done;
553     }
554
555   /* ./foo => foo */
556   while (string[0] == '.' && SLASH_P (string[1]))
557     string += 2;
558
559   alloclen = strlen (path) + strlen (string) + 2;
560   filename = (char *) alloca (alloclen);
561   fd = -1;
562   for (p = path; p; p = p1 ? p1 + 1 : 0)
563     {
564       p1 = (char *) strchr (p, DIRNAME_SEPARATOR);
565       if (p1)
566         len = p1 - p;
567       else
568         len = strlen (p);
569
570       if (len == 4 && p[0] == '$' && p[1] == 'c'
571           && p[2] == 'w' && p[3] == 'd')
572         {
573           /* Name is $cwd -- insert current directory name instead.  */
574           int newlen;
575
576           /* First, realloc the filename buffer if too short. */
577           len = strlen (current_directory);
578           newlen = len + strlen (string) + 2;
579           if (newlen > alloclen)
580             {
581               alloclen = newlen;
582               filename = (char *) alloca (alloclen);
583             }
584           strcpy (filename, current_directory);
585         }
586       else
587         {
588           /* Normal file name in path -- just use it.  */
589           strncpy (filename, p, len);
590           filename[len] = 0;
591         }
592
593       /* Remove trailing slashes */
594       while (len > 0 && SLASH_P (filename[len - 1]))
595         filename[--len] = 0;
596
597       strcat (filename + len, SLASH_STRING);
598       strcat (filename, string);
599
600       fd = open (filename, mode);
601       if (fd >= 0)
602         break;
603     }
604
605 done:
606   if (filename_opened)
607     {
608       if (fd < 0)
609         *filename_opened = (char *) 0;
610       else if (ROOTED_P (filename))
611         *filename_opened = savestring (filename, strlen (filename));
612       else
613         {
614           /* Beware the // my son, the Emacs barfs, the botch that catch... */
615
616           *filename_opened = concat (current_directory,
617                  SLASH_P (current_directory[strlen (current_directory) - 1])
618                                      ? "" : SLASH_STRING,
619                                      filename, NULL);
620         }
621     }
622 #ifdef MPW
623   /* This is a debugging hack that can go away when all combinations
624      of Mac and Unix names are handled reasonably.  */
625   {
626     extern int debug_openp;
627
628     if (debug_openp)
629       {
630         printf ("openp on %s, path %s mode %d prot %d\n  returned %d",
631                 string, path, mode, prot, fd);
632         if (*filename_opened)
633           printf (" (filename is %s)", *filename_opened);
634         printf ("\n");
635       }
636   }
637 #endif /* MPW */
638
639   return fd;
640 }
641
642
643 /* This is essentially a convenience, for clients that want the behaviour
644    of openp, using source_path, but that really don't want the file to be
645    opened but want instead just to know what the full pathname is (as
646    qualified against source_path).
647
648    The current working directory is searched first.
649
650    If the file was found, this function returns 1, and FULL_PATHNAME is
651    set to the fully-qualified pathname.
652
653    Else, this functions returns 0, and FULL_PATHNAME is set to NULL.
654  */
655 int
656 source_full_path_of (filename, full_pathname)
657      char *filename;
658      char **full_pathname;
659 {
660   int fd;
661
662   fd = openp (source_path, 1, filename, O_RDONLY, 0, full_pathname);
663   if (fd < 0)
664     {
665       *full_pathname = NULL;
666       return 0;
667     }
668
669   close (fd);
670   return 1;
671 }
672
673
674 /* Open a source file given a symtab S.  Returns a file descriptor or
675    negative number for error.  */
676
677 int
678 open_source_file (s)
679      struct symtab *s;
680 {
681   char *path = source_path;
682   char *p;
683   int result;
684   char *fullname;
685
686   /* Quick way out if we already know its full name */
687   if (s->fullname)
688     {
689       result = open (s->fullname, OPEN_MODE);
690       if (result >= 0)
691         return result;
692       /* Didn't work -- free old one, try again. */
693       mfree (s->objfile->md, s->fullname);
694       s->fullname = NULL;
695     }
696
697   if (s->dirname != NULL)
698     {
699       /* Replace a path entry of  $cdir  with the compilation directory name */
700 #define cdir_len        5
701       /* We cast strstr's result in case an ANSIhole has made it const,
702          which produces a "required warning" when assigned to a nonconst. */
703       p = (char *) strstr (source_path, "$cdir");
704       if (p && (p == path || p[-1] == DIRNAME_SEPARATOR)
705           && (p[cdir_len] == DIRNAME_SEPARATOR || p[cdir_len] == '\0'))
706         {
707           int len;
708
709           path = (char *)
710             alloca (strlen (source_path) + 1 + strlen (s->dirname) + 1);
711           len = p - source_path;
712           strncpy (path, source_path, len);     /* Before $cdir */
713           strcpy (path + len, s->dirname);      /* new stuff */
714           strcat (path + len, source_path + len + cdir_len);    /* After $cdir */
715         }
716     }
717
718   result = openp (path, 0, s->filename, OPEN_MODE, 0, &s->fullname);
719   if (result < 0)
720     {
721       /* Didn't work.  Try using just the basename. */
722       p = basename (s->filename);
723       if (p != s->filename)
724         result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
725     }
726 #ifdef MPW
727   if (result < 0)
728     {
729       /* Didn't work.  Try using just the MPW basename. */
730       p = (char *) mpw_basename (s->filename);
731       if (p != s->filename)
732         result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
733     }
734   if (result < 0)
735     {
736       /* Didn't work.  Try using the mixed Unix/MPW basename. */
737       p = (char *) mpw_mixed_basename (s->filename);
738       if (p != s->filename)
739         result = openp (path, 0, p, OPEN_MODE, 0, &s->fullname);
740     }
741 #endif /* MPW */
742
743   if (result >= 0)
744     {
745       fullname = s->fullname;
746       s->fullname = mstrsave (s->objfile->md, s->fullname);
747       free (fullname);
748     }
749   return result;
750 }
751
752 /* Return the path to the source file associated with symtab.  Returns NULL
753    if no symtab.  */
754
755 char *
756 symtab_to_filename (s)
757      struct symtab *s;
758 {
759   int fd;
760
761   if (!s)
762     return NULL;
763
764   /* If we've seen the file before, just return fullname. */
765
766   if (s->fullname)
767     return s->fullname;
768
769   /* Try opening the file to setup fullname */
770
771   fd = open_source_file (s);
772   if (fd < 0)
773     return s->filename;         /* File not found.  Just use short name */
774
775   /* Found the file.  Cleanup and return the full name */
776
777   close (fd);
778   return s->fullname;
779 }
780 \f
781
782 /* Create and initialize the table S->line_charpos that records
783    the positions of the lines in the source file, which is assumed
784    to be open on descriptor DESC.
785    All set S->nlines to the number of such lines.  */
786
787 void
788 find_source_lines (s, desc)
789      struct symtab *s;
790      int desc;
791 {
792   struct stat st;
793   register char *data, *p, *end;
794   int nlines = 0;
795   int lines_allocated = 1000;
796   int *line_charpos;
797   long mtime = 0;
798   int size;
799
800   line_charpos = (int *) xmmalloc (s->objfile->md,
801                                    lines_allocated * sizeof (int));
802   if (fstat (desc, &st) < 0)
803     perror_with_name (s->filename);
804
805   if (s && s->objfile && s->objfile->obfd)
806     mtime = bfd_get_mtime (s->objfile->obfd);
807   else if (exec_bfd)
808     mtime = bfd_get_mtime (exec_bfd);
809
810   if (mtime && mtime < st.st_mtime)
811     {
812       if (tui_version)
813         printf_filtered ("\n");
814       warning ("Source file is more recent than executable.\n");
815     }
816
817 #ifdef LSEEK_NOT_LINEAR
818   {
819     char c;
820
821     /* Have to read it byte by byte to find out where the chars live */
822
823     line_charpos[0] = lseek (desc, 0, SEEK_CUR);
824     nlines = 1;
825     while (myread (desc, &c, 1) > 0)
826       {
827         if (c == '\n')
828           {
829             if (nlines == lines_allocated)
830               {
831                 lines_allocated *= 2;
832                 line_charpos =
833                   (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
834                                      sizeof (int) * lines_allocated);
835               }
836             line_charpos[nlines++] = lseek (desc, 0, SEEK_CUR);
837           }
838       }
839   }
840 #else /* lseek linear.  */
841   {
842     struct cleanup *old_cleanups;
843
844     /* st_size might be a large type, but we only support source files whose 
845        size fits in an int.  */
846     size = (int) st.st_size;
847
848     /* Use malloc, not alloca, because this may be pretty large, and we may
849        run into various kinds of limits on stack size.  */
850     data = (char *) xmalloc (size);
851     old_cleanups = make_cleanup (free, data);
852
853     /* Reassign `size' to result of read for systems where \r\n -> \n.  */
854     size = myread (desc, data, size);
855     if (size < 0)
856       perror_with_name (s->filename);
857     end = data + size;
858     p = data;
859     line_charpos[0] = 0;
860     nlines = 1;
861     while (p != end)
862       {
863         if (*p++ == '\n'
864         /* A newline at the end does not start a new line.  */
865             && p != end)
866           {
867             if (nlines == lines_allocated)
868               {
869                 lines_allocated *= 2;
870                 line_charpos =
871                   (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
872                                      sizeof (int) * lines_allocated);
873               }
874             line_charpos[nlines++] = p - data;
875           }
876       }
877     do_cleanups (old_cleanups);
878   }
879 #endif /* lseek linear.  */
880   s->nlines = nlines;
881   s->line_charpos =
882     (int *) xmrealloc (s->objfile->md, (char *) line_charpos,
883                        nlines * sizeof (int));
884
885 }
886
887 /* Return the character position of a line LINE in symtab S.
888    Return 0 if anything is invalid.  */
889
890 #if 0                           /* Currently unused */
891
892 int
893 source_line_charpos (s, line)
894      struct symtab *s;
895      int line;
896 {
897   if (!s)
898     return 0;
899   if (!s->line_charpos || line <= 0)
900     return 0;
901   if (line > s->nlines)
902     line = s->nlines;
903   return s->line_charpos[line - 1];
904 }
905
906 /* Return the line number of character position POS in symtab S.  */
907
908 int
909 source_charpos_line (s, chr)
910      register struct symtab *s;
911      register int chr;
912 {
913   register int line = 0;
914   register int *lnp;
915
916   if (s == 0 || s->line_charpos == 0)
917     return 0;
918   lnp = s->line_charpos;
919   /* Files are usually short, so sequential search is Ok */
920   while (line < s->nlines && *lnp <= chr)
921     {
922       line++;
923       lnp++;
924     }
925   if (line >= s->nlines)
926     line = s->nlines;
927   return line;
928 }
929
930 #endif /* 0 */
931 \f
932
933 /* Get full pathname and line number positions for a symtab.
934    Return nonzero if line numbers may have changed.
935    Set *FULLNAME to actual name of the file as found by `openp',
936    or to 0 if the file is not found.  */
937
938 static int
939 get_filename_and_charpos (s, fullname)
940      struct symtab *s;
941      char **fullname;
942 {
943   register int desc, linenums_changed = 0;
944
945   desc = open_source_file (s);
946   if (desc < 0)
947     {
948       if (fullname)
949         *fullname = NULL;
950       return 0;
951     }
952   if (fullname)
953     *fullname = s->fullname;
954   if (s->line_charpos == 0)
955     linenums_changed = 1;
956   if (linenums_changed)
957     find_source_lines (s, desc);
958   close (desc);
959   return linenums_changed;
960 }
961
962 /* Print text describing the full name of the source file S
963    and the line number LINE and its corresponding character position.
964    The text starts with two Ctrl-z so that the Emacs-GDB interface
965    can easily find it.
966
967    MID_STATEMENT is nonzero if the PC is not at the beginning of that line.
968
969    Return 1 if successful, 0 if could not find the file.  */
970
971 int
972 identify_source_line (s, line, mid_statement, pc)
973      struct symtab *s;
974      int line;
975      int mid_statement;
976      CORE_ADDR pc;
977 {
978   if (s->line_charpos == 0)
979     get_filename_and_charpos (s, (char **) NULL);
980   if (s->fullname == 0)
981     return 0;
982   if (line > s->nlines)
983     /* Don't index off the end of the line_charpos array.  */
984     return 0;
985   annotate_source (s->fullname, line, s->line_charpos[line - 1],
986                    mid_statement, pc);
987
988   current_source_line = line;
989   first_line_listed = line;
990   last_line_listed = line;
991   current_source_symtab = s;
992   return 1;
993 }
994 \f
995
996 /* Print source lines from the file of symtab S,
997    starting with line number LINE and stopping before line number STOPLINE. */
998
999 static void print_source_lines_base (struct symtab *s, int line, int stopline,
1000                                      int noerror);
1001 static void
1002 print_source_lines_base (s, line, stopline, noerror)
1003      struct symtab *s;
1004      int line;
1005      int stopline;
1006      int noerror;
1007 {
1008   register int c;
1009   register int desc;
1010   register FILE *stream;
1011   int nlines = stopline - line;
1012
1013   /* Regardless of whether we can open the file, set current_source_symtab. */
1014   current_source_symtab = s;
1015   current_source_line = line;
1016   first_line_listed = line;
1017
1018 #ifdef UI_OUT
1019   /* If printing of source lines is disabled, just print file and line number */
1020   if (ui_out_test_flags (uiout, ui_source_list))
1021     {
1022 #endif
1023       /* Only prints "No such file or directory" once */
1024       if ((s != last_source_visited) || (!last_source_error))
1025         {
1026           last_source_visited = s;
1027           desc = open_source_file (s);
1028         }
1029       else
1030         {
1031           desc = last_source_error;
1032           noerror = 1;
1033         }
1034 #ifdef UI_OUT
1035     }
1036   else
1037     {
1038       desc = -1;
1039       noerror = 1;
1040     }
1041 #endif
1042
1043   if (desc < 0)
1044     {
1045       last_source_error = desc;
1046
1047       if (!noerror)
1048         {
1049           char *name = alloca (strlen (s->filename) + 100);
1050           sprintf (name, "%d\t%s", line, s->filename);
1051           print_sys_errmsg (name, errno);
1052         }
1053       else
1054 #ifdef UI_OUT
1055         ui_out_field_int (uiout, "line", line);
1056       ui_out_text (uiout, "\tin ");
1057       ui_out_field_string (uiout, "file", s->filename);
1058       ui_out_text (uiout, "\n");
1059 #else
1060         printf_filtered ("%d\tin %s\n", line, s->filename);
1061 #endif
1062
1063       return;
1064     }
1065
1066   last_source_error = 0;
1067
1068   if (s->line_charpos == 0)
1069     find_source_lines (s, desc);
1070
1071   if (line < 1 || line > s->nlines)
1072     {
1073       close (desc);
1074       error ("Line number %d out of range; %s has %d lines.",
1075              line, s->filename, s->nlines);
1076     }
1077
1078   if (lseek (desc, s->line_charpos[line - 1], 0) < 0)
1079     {
1080       close (desc);
1081       perror_with_name (s->filename);
1082     }
1083
1084   stream = fdopen (desc, FDOPEN_MODE);
1085   clearerr (stream);
1086
1087   while (nlines-- > 0)
1088     {
1089 #ifdef UI_OUT
1090       char buf[20];
1091
1092       c = fgetc (stream);
1093       if (c == EOF)
1094         break;
1095       last_line_listed = current_source_line;
1096       sprintf (buf, "%d\t", current_source_line++);
1097       ui_out_text (uiout, buf);
1098       do
1099         {
1100           if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1101             {
1102               sprintf (buf, "^%c", c + 0100);
1103               ui_out_text (uiout, buf);
1104             }
1105           else if (c == 0177)
1106             ui_out_text (uiout, "^?");
1107 #ifdef CRLF_SOURCE_FILES
1108           else if (c == '\r')
1109             {
1110               /* Skip a \r character, but only before a \n.  */
1111               int c1 = fgetc (stream);
1112
1113               if (c1 != '\n')
1114                 printf_filtered ("^%c", c + 0100);
1115               if (c1 != EOF)
1116                 ungetc (c1, stream);
1117             }
1118 #endif
1119           else
1120             {
1121               sprintf (buf, "%c", c);
1122               ui_out_text (uiout, buf);
1123             }
1124         }
1125       while (c != '\n' && (c = fgetc (stream)) >= 0);
1126 #else
1127       c = fgetc (stream);
1128       if (c == EOF)
1129         break;
1130       last_line_listed = current_source_line;
1131       printf_filtered ("%d\t", current_source_line++);
1132       do
1133         {
1134           if (c < 040 && c != '\t' && c != '\n' && c != '\r')
1135             printf_filtered ("^%c", c + 0100);
1136           else if (c == 0177)
1137             printf_filtered ("^?");
1138 #ifdef CRLF_SOURCE_FILES
1139           else if (c == '\r')
1140             {
1141               /* Just skip \r characters.  */
1142             }
1143 #endif
1144           else
1145             printf_filtered ("%c", c);
1146         }
1147       while (c != '\n' && (c = fgetc (stream)) >= 0);
1148 #endif
1149     }
1150
1151   fclose (stream);
1152 }
1153 \f
1154 /* Show source lines from the file of symtab S, starting with line
1155    number LINE and stopping before line number STOPLINE.  If this is the
1156    not the command line version, then the source is shown in the source
1157    window otherwise it is simply printed */
1158
1159 void
1160 print_source_lines (s, line, stopline, noerror)
1161      struct symtab *s;
1162      int line, stopline, noerror;
1163 {
1164 #if defined(TUI)
1165   if (!tui_version ||
1166       m_winPtrIsNull (srcWin) || !srcWin->generic.isVisible)
1167     print_source_lines_base (s, line, stopline, noerror);
1168   else
1169     {
1170       TuiGenWinInfoPtr locator = locatorWinInfoPtr ();
1171 extern void tui_vAddWinToLayout (va_list);
1172 extern void tui_vUpdateSourceWindowsWithLine (va_list);
1173
1174       /* Regardless of whether we can open the file,
1175          set current_source_symtab. */
1176       current_source_symtab = s;
1177       current_source_line = line;
1178       first_line_listed = line;
1179
1180       /* make sure that the source window is displayed */
1181       tuiDo ((TuiOpaqueFuncPtr) tui_vAddWinToLayout, SRC_WIN);
1182
1183       tuiDo ((TuiOpaqueFuncPtr) tui_vUpdateSourceWindowsWithLine, s, line);
1184       tuiDo ((TuiOpaqueFuncPtr) tui_vUpdateLocatorFilename, s->filename);
1185     }
1186 #else
1187   print_source_lines_base (s, line, stopline, noerror);
1188 #endif
1189 }
1190 \f
1191
1192
1193 /* Print a list of files and line numbers which a user may choose from
1194    in order to list a function which was specified ambiguously (as with
1195    `list classname::overloadedfuncname', for example).  The vector in
1196    SALS provides the filenames and line numbers.  */
1197
1198 static void
1199 ambiguous_line_spec (sals)
1200      struct symtabs_and_lines *sals;
1201 {
1202   int i;
1203
1204   for (i = 0; i < sals->nelts; ++i)
1205     printf_filtered ("file: \"%s\", line number: %d\n",
1206                      sals->sals[i].symtab->filename, sals->sals[i].line);
1207 }
1208
1209 static void
1210 list_command (arg, from_tty)
1211      char *arg;
1212      int from_tty;
1213 {
1214   struct symtabs_and_lines sals, sals_end;
1215   struct symtab_and_line sal, sal_end;
1216   struct symbol *sym;
1217   char *arg1;
1218   int no_end = 1;
1219   int dummy_end = 0;
1220   int dummy_beg = 0;
1221   int linenum_beg = 0;
1222   char *p;
1223
1224   if (!have_full_symbols () && !have_partial_symbols ())
1225     error ("No symbol table is loaded.  Use the \"file\" command.");
1226
1227   /* Pull in a current source symtab if necessary */
1228   if (current_source_symtab == 0 &&
1229       (arg == 0 || arg[0] == '+' || arg[0] == '-'))
1230     select_source_symtab (0);
1231
1232   /* "l" or "l +" lists next ten lines.  */
1233
1234   if (arg == 0 || STREQ (arg, "+"))
1235     {
1236       if (current_source_symtab == 0)
1237         error ("No default source file yet.  Do \"help list\".");
1238       print_source_lines (current_source_symtab, current_source_line,
1239                           current_source_line + lines_to_list, 0);
1240       return;
1241     }
1242
1243   /* "l -" lists previous ten lines, the ones before the ten just listed.  */
1244   if (STREQ (arg, "-"))
1245     {
1246       if (current_source_symtab == 0)
1247         error ("No default source file yet.  Do \"help list\".");
1248       print_source_lines (current_source_symtab,
1249                           max (first_line_listed - lines_to_list, 1),
1250                           first_line_listed, 0);
1251       return;
1252     }
1253
1254   /* Now if there is only one argument, decode it in SAL
1255      and set NO_END.
1256      If there are two arguments, decode them in SAL and SAL_END
1257      and clear NO_END; however, if one of the arguments is blank,
1258      set DUMMY_BEG or DUMMY_END to record that fact.  */
1259
1260   arg1 = arg;
1261   if (*arg1 == ',')
1262     dummy_beg = 1;
1263   else
1264     {
1265       sals = decode_line_1 (&arg1, 0, 0, 0, 0);
1266
1267       if (!sals.nelts)
1268         return;                 /*  C++  */
1269       if (sals.nelts > 1)
1270         {
1271           ambiguous_line_spec (&sals);
1272           free (sals.sals);
1273           return;
1274         }
1275
1276       sal = sals.sals[0];
1277       free (sals.sals);
1278     }
1279
1280   /* Record whether the BEG arg is all digits.  */
1281
1282   for (p = arg; p != arg1 && *p >= '0' && *p <= '9'; p++);
1283   linenum_beg = (p == arg1);
1284
1285   while (*arg1 == ' ' || *arg1 == '\t')
1286     arg1++;
1287   if (*arg1 == ',')
1288     {
1289       no_end = 0;
1290       arg1++;
1291       while (*arg1 == ' ' || *arg1 == '\t')
1292         arg1++;
1293       if (*arg1 == 0)
1294         dummy_end = 1;
1295       else
1296         {
1297           if (dummy_beg)
1298             sals_end = decode_line_1 (&arg1, 0, 0, 0, 0);
1299           else
1300             sals_end = decode_line_1 (&arg1, 0, sal.symtab, sal.line, 0);
1301           if (sals_end.nelts == 0)
1302             return;
1303           if (sals_end.nelts > 1)
1304             {
1305               ambiguous_line_spec (&sals_end);
1306               free (sals_end.sals);
1307               return;
1308             }
1309           sal_end = sals_end.sals[0];
1310           free (sals_end.sals);
1311         }
1312     }
1313
1314   if (*arg1)
1315     error ("Junk at end of line specification.");
1316
1317   if (!no_end && !dummy_beg && !dummy_end
1318       && sal.symtab != sal_end.symtab)
1319     error ("Specified start and end are in different files.");
1320   if (dummy_beg && dummy_end)
1321     error ("Two empty args do not say what lines to list.");
1322
1323   /* if line was specified by address,
1324      first print exactly which line, and which file.
1325      In this case, sal.symtab == 0 means address is outside
1326      of all known source files, not that user failed to give a filename.  */
1327   if (*arg == '*')
1328     {
1329       if (sal.symtab == 0)
1330         /* FIXME-32x64--assumes sal.pc fits in long.  */
1331         error ("No source file for address %s.",
1332                local_hex_string ((unsigned long) sal.pc));
1333       sym = find_pc_function (sal.pc);
1334       if (sym)
1335         {
1336           print_address_numeric (sal.pc, 1, gdb_stdout);
1337           printf_filtered (" is in ");
1338           fputs_filtered (SYMBOL_SOURCE_NAME (sym), gdb_stdout);
1339           printf_filtered (" (%s:%d).\n", sal.symtab->filename, sal.line);
1340         }
1341       else
1342         {
1343           print_address_numeric (sal.pc, 1, gdb_stdout);
1344           printf_filtered (" is at %s:%d.\n",
1345                            sal.symtab->filename, sal.line);
1346         }
1347     }
1348
1349   /* If line was not specified by just a line number,
1350      and it does not imply a symtab, it must be an undebuggable symbol
1351      which means no source code.  */
1352
1353   if (!linenum_beg && sal.symtab == 0)
1354     error ("No line number known for %s.", arg);
1355
1356   /* If this command is repeated with RET,
1357      turn it into the no-arg variant.  */
1358
1359   if (from_tty)
1360     *arg = 0;
1361
1362   if (dummy_beg && sal_end.symtab == 0)
1363     error ("No default source file yet.  Do \"help list\".");
1364   if (dummy_beg)
1365     print_source_lines (sal_end.symtab,
1366                         max (sal_end.line - (lines_to_list - 1), 1),
1367                         sal_end.line + 1, 0);
1368   else if (sal.symtab == 0)
1369     error ("No default source file yet.  Do \"help list\".");
1370   else if (no_end)
1371     {
1372       int first_line = sal.line - lines_to_list / 2;
1373
1374       if (first_line < 1) first_line = 1;
1375
1376       print_source_lines (sal.symtab, first_line, first_line + lines_to_list,
1377                           0);
1378     }
1379   else
1380     print_source_lines (sal.symtab, sal.line,
1381                         (dummy_end
1382                          ? sal.line + lines_to_list
1383                          : sal_end.line + 1),
1384                         0);
1385 }
1386 \f
1387 /* Print info on range of pc's in a specified line.  */
1388
1389 static void
1390 line_info (arg, from_tty)
1391      char *arg;
1392      int from_tty;
1393 {
1394   struct symtabs_and_lines sals;
1395   struct symtab_and_line sal;
1396   CORE_ADDR start_pc, end_pc;
1397   int i;
1398
1399   INIT_SAL (&sal);              /* initialize to zeroes */
1400
1401   if (arg == 0)
1402     {
1403       sal.symtab = current_source_symtab;
1404       sal.line = last_line_listed;
1405       sals.nelts = 1;
1406       sals.sals = (struct symtab_and_line *)
1407         xmalloc (sizeof (struct symtab_and_line));
1408       sals.sals[0] = sal;
1409     }
1410   else
1411     {
1412       sals = decode_line_spec_1 (arg, 0);
1413
1414       dont_repeat ();
1415     }
1416
1417   /* C++  More than one line may have been specified, as when the user
1418      specifies an overloaded function name. Print info on them all. */
1419   for (i = 0; i < sals.nelts; i++)
1420     {
1421       sal = sals.sals[i];
1422
1423       if (sal.symtab == 0)
1424         {
1425           printf_filtered ("No line number information available");
1426           if (sal.pc != 0)
1427             {
1428               /* This is useful for "info line *0x7f34".  If we can't tell the
1429                  user about a source line, at least let them have the symbolic
1430                  address.  */
1431               printf_filtered (" for address ");
1432               wrap_here ("  ");
1433               print_address (sal.pc, gdb_stdout);
1434             }
1435           else
1436             printf_filtered (".");
1437           printf_filtered ("\n");
1438         }
1439       else if (sal.line > 0
1440                && find_line_pc_range (sal, &start_pc, &end_pc))
1441         {
1442           if (start_pc == end_pc)
1443             {
1444               printf_filtered ("Line %d of \"%s\"",
1445                                sal.line, sal.symtab->filename);
1446               wrap_here ("  ");
1447               printf_filtered (" is at address ");
1448               print_address (start_pc, gdb_stdout);
1449               wrap_here ("  ");
1450               printf_filtered (" but contains no code.\n");
1451             }
1452           else
1453             {
1454               printf_filtered ("Line %d of \"%s\"",
1455                                sal.line, sal.symtab->filename);
1456               wrap_here ("  ");
1457               printf_filtered (" starts at address ");
1458               print_address (start_pc, gdb_stdout);
1459               wrap_here ("  ");
1460               printf_filtered (" and ends at ");
1461               print_address (end_pc, gdb_stdout);
1462               printf_filtered (".\n");
1463             }
1464
1465           /* x/i should display this line's code.  */
1466           set_next_address (start_pc);
1467
1468           /* Repeating "info line" should do the following line.  */
1469           last_line_listed = sal.line + 1;
1470
1471           /* If this is the only line, show the source code.  If it could
1472              not find the file, don't do anything special.  */
1473           if (annotation_level && sals.nelts == 1)
1474             identify_source_line (sal.symtab, sal.line, 0, start_pc);
1475         }
1476       else
1477         /* Is there any case in which we get here, and have an address
1478            which the user would want to see?  If we have debugging symbols
1479            and no line numbers?  */
1480         printf_filtered ("Line number %d is out of range for \"%s\".\n",
1481                          sal.line, sal.symtab->filename);
1482     }
1483   free (sals.sals);
1484 }
1485 \f
1486 /* Commands to search the source file for a regexp.  */
1487
1488 /* ARGSUSED */
1489 static void
1490 forward_search_command (regex, from_tty)
1491      char *regex;
1492      int from_tty;
1493 {
1494   register int c;
1495   register int desc;
1496   register FILE *stream;
1497   int line;
1498   char *msg;
1499
1500 #if defined(TUI)
1501   /* 
1502      ** If this is the TUI, search from the first line displayed in 
1503      ** the source window, otherwise, search from last_line_listed+1 
1504      ** in current_source_symtab 
1505    */
1506   if (!tui_version)
1507     line = last_line_listed;
1508   else
1509     {
1510       if (srcWin->generic.isVisible && srcWin->generic.contentSize > 0)
1511         line = ((TuiWinContent)
1512          srcWin->generic.content)[0]->whichElement.source.lineOrAddr.lineNo;
1513       else
1514         {
1515           printf_filtered ("No source displayed.\nExpression not found.\n");
1516           return;
1517         }
1518     }
1519   line++;
1520 #else
1521   line = last_line_listed + 1;
1522 #endif
1523
1524   msg = (char *) re_comp (regex);
1525   if (msg)
1526     error (msg);
1527
1528   if (current_source_symtab == 0)
1529     select_source_symtab (0);
1530
1531   desc = open_source_file (current_source_symtab);
1532   if (desc < 0)
1533     perror_with_name (current_source_symtab->filename);
1534
1535   if (current_source_symtab->line_charpos == 0)
1536     find_source_lines (current_source_symtab, desc);
1537
1538   if (line < 1 || line > current_source_symtab->nlines)
1539     {
1540       close (desc);
1541       error ("Expression not found");
1542     }
1543
1544   if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1545     {
1546       close (desc);
1547       perror_with_name (current_source_symtab->filename);
1548     }
1549
1550   stream = fdopen (desc, FDOPEN_MODE);
1551   clearerr (stream);
1552   while (1)
1553     {
1554       static char *buf = NULL;
1555       register char *p;
1556       int cursize, newsize;
1557
1558       cursize = 256;
1559       buf = xmalloc (cursize);
1560       p = buf;
1561
1562       c = getc (stream);
1563       if (c == EOF)
1564         break;
1565       do
1566         {
1567           *p++ = c;
1568           if (p - buf == cursize)
1569             {
1570               newsize = cursize + cursize / 2;
1571               buf = xrealloc (buf, newsize);
1572               p = buf + cursize;
1573               cursize = newsize;
1574             }
1575         }
1576       while (c != '\n' && (c = getc (stream)) >= 0);
1577
1578 #ifdef CRLF_SOURCE_FILES
1579       /* Remove the \r, if any, at the end of the line, otherwise
1580          regular expressions that end with $ or \n won't work.  */
1581       if (p - buf > 1 && p[-2] == '\r')
1582         {
1583           p--;
1584           p[-1] = '\n';
1585         }
1586 #endif
1587
1588       /* we now have a source line in buf, null terminate and match */
1589       *p = 0;
1590       if (re_exec (buf) > 0)
1591         {
1592           /* Match! */
1593           fclose (stream);
1594           if (tui_version)
1595             print_source_lines_base (current_source_symtab, line, line + 1, 0);
1596           print_source_lines (current_source_symtab, line, line + 1, 0);
1597           set_internalvar (lookup_internalvar ("_"),
1598                            value_from_longest (builtin_type_int,
1599                                                (LONGEST) line));
1600           current_source_line = max (line - lines_to_list / 2, 1);
1601           return;
1602         }
1603       line++;
1604     }
1605
1606   printf_filtered ("Expression not found\n");
1607   fclose (stream);
1608 }
1609
1610 /* ARGSUSED */
1611 static void
1612 reverse_search_command (regex, from_tty)
1613      char *regex;
1614      int from_tty;
1615 {
1616   register int c;
1617   register int desc;
1618   register FILE *stream;
1619   int line;
1620   char *msg;
1621 #if defined(TUI)
1622   /*
1623      ** If this is the TUI, search from the first line displayed in
1624      ** the source window, otherwise, search from last_line_listed-1
1625      ** in current_source_symtab
1626    */
1627   if (!tui_version)
1628     line = last_line_listed;
1629   else
1630     {
1631       if (srcWin->generic.isVisible && srcWin->generic.contentSize > 0)
1632         line = ((TuiWinContent)
1633          srcWin->generic.content)[0]->whichElement.source.lineOrAddr.lineNo;
1634       else
1635         {
1636           printf_filtered ("No source displayed.\nExpression not found.\n");
1637           return;
1638         }
1639     }
1640   line--;
1641 #else
1642   line = last_line_listed - 1;
1643 #endif
1644
1645   msg = (char *) re_comp (regex);
1646   if (msg)
1647     error (msg);
1648
1649   if (current_source_symtab == 0)
1650     select_source_symtab (0);
1651
1652   desc = open_source_file (current_source_symtab);
1653   if (desc < 0)
1654     perror_with_name (current_source_symtab->filename);
1655
1656   if (current_source_symtab->line_charpos == 0)
1657     find_source_lines (current_source_symtab, desc);
1658
1659   if (line < 1 || line > current_source_symtab->nlines)
1660     {
1661       close (desc);
1662       error ("Expression not found");
1663     }
1664
1665   if (lseek (desc, current_source_symtab->line_charpos[line - 1], 0) < 0)
1666     {
1667       close (desc);
1668       perror_with_name (current_source_symtab->filename);
1669     }
1670
1671   stream = fdopen (desc, FDOPEN_MODE);
1672   clearerr (stream);
1673   while (line > 1)
1674     {
1675 /* FIXME!!!  We walk right off the end of buf if we get a long line!!! */
1676       char buf[4096];           /* Should be reasonable??? */
1677       register char *p = buf;
1678
1679       c = getc (stream);
1680       if (c == EOF)
1681         break;
1682       do
1683         {
1684           *p++ = c;
1685         }
1686       while (c != '\n' && (c = getc (stream)) >= 0);
1687
1688 #ifdef CRLF_SOURCE_FILES
1689       /* Remove the \r, if any, at the end of the line, otherwise
1690          regular expressions that end with $ or \n won't work.  */
1691       if (p - buf > 1 && p[-2] == '\r')
1692         {
1693           p--;
1694           p[-1] = '\n';
1695         }
1696 #endif
1697
1698       /* We now have a source line in buf; null terminate and match.  */
1699       *p = 0;
1700       if (re_exec (buf) > 0)
1701         {
1702           /* Match! */
1703           fclose (stream);
1704           if (tui_version)
1705             print_source_lines_base (current_source_symtab, line, line + 1, 0);
1706           print_source_lines (current_source_symtab, line, line + 1, 0);
1707           set_internalvar (lookup_internalvar ("_"),
1708                            value_from_longest (builtin_type_int,
1709                                                (LONGEST) line));
1710           current_source_line = max (line - lines_to_list / 2, 1);
1711           return;
1712         }
1713       line--;
1714       if (fseek (stream, current_source_symtab->line_charpos[line - 1], 0) < 0)
1715         {
1716           fclose (stream);
1717           perror_with_name (current_source_symtab->filename);
1718         }
1719     }
1720
1721   printf_filtered ("Expression not found\n");
1722   fclose (stream);
1723   return;
1724 }
1725 \f
1726 void
1727 _initialize_source ()
1728 {
1729   struct cmd_list_element *c;
1730   current_source_symtab = 0;
1731   init_source_path ();
1732
1733   /* The intention is to use POSIX Basic Regular Expressions.
1734      Always use the GNU regex routine for consistency across all hosts.
1735      Our current GNU regex.c does not have all the POSIX features, so this is
1736      just an approximation.  */
1737   re_set_syntax (RE_SYNTAX_GREP);
1738
1739   c = add_cmd ("directory", class_files, directory_command,
1740                "Add directory DIR to beginning of search path for source files.\n\
1741 Forget cached info on source file locations and line positions.\n\
1742 DIR can also be $cwd for the current working directory, or $cdir for the\n\
1743 directory in which the source file was compiled into object code.\n\
1744 With no argument, reset the search path to $cdir:$cwd, the default.",
1745                &cmdlist);
1746
1747   if (dbx_commands)
1748     add_com_alias ("use", "directory", class_files, 0);
1749
1750   c->completer = filename_completer;
1751
1752   add_cmd ("directories", no_class, show_directories,
1753            "Current search path for finding source files.\n\
1754 $cwd in the path means the current working directory.\n\
1755 $cdir in the path means the compilation directory of the source file.",
1756            &showlist);
1757
1758   if (xdb_commands)
1759     {
1760       add_com_alias ("D", "directory", class_files, 0);
1761       add_cmd ("ld", no_class, show_directories,
1762                "Current search path for finding source files.\n\
1763 $cwd in the path means the current working directory.\n\
1764 $cdir in the path means the compilation directory of the source file.",
1765                &cmdlist);
1766     }
1767
1768   add_info ("source", source_info,
1769             "Information about the current source file.");
1770
1771   add_info ("line", line_info,
1772             concat ("Core addresses of the code for a source line.\n\
1773 Line can be specified as\n\
1774   LINENUM, to list around that line in current file,\n\
1775   FILE:LINENUM, to list around that line in that file,\n\
1776   FUNCTION, to list around beginning of that function,\n\
1777   FILE:FUNCTION, to distinguish among like-named static functions.\n\
1778 ", "\
1779 Default is to describe the last source line that was listed.\n\n\
1780 This sets the default address for \"x\" to the line's first instruction\n\
1781 so that \"x/i\" suffices to start examining the machine code.\n\
1782 The address is also stored as the value of \"$_\".", NULL));
1783
1784   add_com ("forward-search", class_files, forward_search_command,
1785            "Search for regular expression (see regex(3)) from last line listed.\n\
1786 The matching line number is also stored as the value of \"$_\".");
1787   add_com_alias ("search", "forward-search", class_files, 0);
1788
1789   add_com ("reverse-search", class_files, reverse_search_command,
1790            "Search backward for regular expression (see regex(3)) from last line listed.\n\
1791 The matching line number is also stored as the value of \"$_\".");
1792
1793   if (xdb_commands)
1794     {
1795       add_com_alias ("/", "forward-search", class_files, 0);
1796       add_com_alias ("?", "reverse-search", class_files, 0);
1797     }
1798
1799   add_com ("list", class_files, list_command,
1800            concat ("List specified function or line.\n\
1801 With no argument, lists ten more lines after or around previous listing.\n\
1802 \"list -\" lists the ten lines before a previous ten-line listing.\n\
1803 One argument specifies a line, and ten lines are listed around that line.\n\
1804 Two arguments with comma between specify starting and ending lines to list.\n\
1805 ", "\
1806 Lines can be specified in these ways:\n\
1807   LINENUM, to list around that line in current file,\n\
1808   FILE:LINENUM, to list around that line in that file,\n\
1809   FUNCTION, to list around beginning of that function,\n\
1810   FILE:FUNCTION, to distinguish among like-named static functions.\n\
1811   *ADDRESS, to list around the line containing that address.\n\
1812 With two args if one is empty it stands for ten lines away from the other arg.", NULL));
1813
1814   if (!xdb_commands)
1815     add_com_alias ("l", "list", class_files, 1);
1816   else
1817     add_com_alias ("v", "list", class_files, 1);
1818
1819   if (dbx_commands)
1820     add_com_alias ("file", "list", class_files, 1);
1821
1822   add_show_from_set
1823     (add_set_cmd ("listsize", class_support, var_uinteger,
1824                   (char *) &lines_to_list,
1825                   "Set number of source lines gdb will list by default.",
1826                   &setlist),
1827      &showlist);
1828 }