1 /* source.c - Keep track of source files.
3 Copyright (C) 2000-2017 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 #include "libiberty.h"
24 #include "filenames.h"
25 #include "search_list.h"
28 #define EXT_ANNO "-ann" /* Postfix of annotated files. */
30 /* Default option values. */
31 bfd_boolean create_annotation_files = FALSE;
33 Search_List src_search_list = {0, 0};
34 Source_File *first_src_file = 0;
38 source_file_lookup_path (const char *path)
42 for (sf = first_src_file; sf; sf = sf->next)
44 if (FILENAME_CMP (path, sf->name) == 0)
50 /* Create a new source file descriptor. */
51 sf = (Source_File *) xmalloc (sizeof (*sf));
53 memset (sf, 0, sizeof (*sf));
55 sf->name = xstrdup (path);
56 sf->next = first_src_file;
65 source_file_lookup_name (const char *filename)
70 /* The user cannot know exactly how a filename will be stored in
71 the debugging info (e.g., ../include/foo.h
72 vs. /usr/include/foo.h). So we simply compare the filename
73 component of a path only. */
74 for (sf = first_src_file; sf; sf = sf->next)
76 fname = strrchr (sf->name, '/');
83 if (FILENAME_CMP (filename, fname) == 0)
92 annotate_source (Source_File *sf, unsigned int max_width,
93 void (*annote) (char *, unsigned int, int, void *),
96 static bfd_boolean first_file = TRUE;
97 int i, line_num, nread;
100 char fname[PATH_MAX];
101 char *annotation, *name_only;
103 Search_List_Elem *sle = src_search_list.head;
105 /* Open input file. If open fails, walk along search-list until
106 open succeeds or reaching end of list. */
107 strcpy (fname, sf->name);
109 if (IS_ABSOLUTE_PATH (sf->name))
110 sle = 0; /* Don't use search list for absolute paths. */
115 DBG (SRCDEBUG, printf ("[annotate_source]: looking for %s, trying %s\n",
118 ifp = fopen (fname, FOPEN_RB);
122 if (!sle && !name_only)
124 name_only = strrchr (sf->name, '/');
125 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
127 char *bslash = strrchr (sf->name, '\\');
128 if (name_only == NULL || (bslash != NULL && bslash > name_only))
130 if (name_only == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
131 name_only = (char *)sf->name + 1;
136 /* Try search-list again, but this time with name only. */
138 sle = src_search_list.head;
144 strcpy (fname, sle->path);
145 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
146 /* d:foo is not the same thing as d:/foo! */
147 if (fname[strlen (fname) - 1] == ':')
153 strcat (fname, name_only);
155 strcat (fname, sf->name);
162 fprintf (stderr, _("%s: could not locate `%s'\n"),
173 if (create_annotation_files)
175 /* Try to create annotated source file. */
176 const char *filename;
178 /* Create annotation files in the current working directory. */
179 filename = strrchr (sf->name, '/');
180 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
182 char *bslash = strrchr (sf->name, '\\');
183 if (filename == NULL || (bslash != NULL && bslash > filename))
185 if (filename == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
186 filename = sf->name + 1;
194 strcpy (fname, filename);
195 strcat (fname, EXT_ANNO);
198 /* foo.cpp-ann can overwrite foo.cpp due to silent truncation of
199 file names on 8+3 filesystems. Their `stat' better be good... */
200 struct stat buf1, buf2;
202 if (stat (filename, &buf1) == 0
203 && stat (fname, &buf2) == 0
204 && buf1.st_ino == buf2.st_ino)
206 char *dot = strrchr (fname, '.');
210 strcat (fname, ".ann");
214 ofp = fopen (fname, "w");
223 /* Print file names if output goes to stdout
224 and there are more than one source file. */
233 first_output = FALSE;
235 fprintf (ofp, "\f\n");
237 fprintf (ofp, _("*** File %s:\n"), sf->name);
240 annotation = (char *) xmalloc (max_width + 1);
244 while ((nread = fread (buf, 1, sizeof (buf), ifp)) > 0)
246 for (i = 0; i < nread; ++i)
250 (*annote) (annotation, max_width, line_num, arg);
251 fputs (annotation, ofp);
255 new_line = (buf[i] == '\n');