source: trunk/src/binutils/gprof/source.c@ 10

Last change on this file since 10 was 10, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 6.0 KB
Line 
1/* source.c - Keep track of source files.
2
3 Copyright 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GNU Binutils.
6
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 2 of the License, or
10 (at your option) any later version.
11
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.
16
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., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22
23#include "gprof.h"
24#include "libiberty.h"
25#include "filenames.h"
26#include "search_list.h"
27#include "source.h"
28
29#define EXT_ANNO "-ann" /* Postfix of annotated files. */
30
31/* Default option values. */
32bool create_annotation_files = FALSE;
33
34Search_List src_search_list = {0, 0};
35Source_File *first_src_file = 0;
36
37
38Source_File *
39DEFUN (source_file_lookup_path, (path), const char *path)
40{
41 Source_File *sf;
42
43 for (sf = first_src_file; sf; sf = sf->next)
44 {
45 if (FILENAME_CMP (path, sf->name) == 0)
46 break;
47 }
48
49 if (!sf)
50 {
51 /* Create a new source file descriptor. */
52 sf = (Source_File *) xmalloc (sizeof (*sf));
53
54 memset (sf, 0, sizeof (*sf));
55
56 sf->name = xstrdup (path);
57 sf->next = first_src_file;
58 first_src_file = sf;
59 }
60
61 return sf;
62}
63
64
65Source_File *
66DEFUN (source_file_lookup_name, (filename), const char *filename)
67{
68 const char *fname;
69 Source_File *sf;
70
71 /* The user cannot know exactly how a filename will be stored in
72 the debugging info (e.g., ../include/foo.h
73 vs. /usr/include/foo.h). So we simply compare the filename
74 component of a path only. */
75 for (sf = first_src_file; sf; sf = sf->next)
76 {
77 fname = strrchr (sf->name, '/');
78
79 if (fname)
80 ++fname;
81 else
82 fname = sf->name;
83
84 if (FILENAME_CMP (filename, fname) == 0)
85 break;
86 }
87
88 return sf;
89}
90
91
92FILE *
93DEFUN (annotate_source, (sf, max_width, annote, arg),
94 Source_File * sf AND int max_width
95 AND void (*annote) PARAMS ((char *buf, int w, int l, void *arg))
96 AND void *arg)
97{
98 static bool first_file = TRUE;
99 int i, line_num, nread;
100 bool new_line;
101 char buf[8192];
102 char fname[PATH_MAX];
103 char *annotation, *name_only;
104 FILE *ifp, *ofp;
105 Search_List_Elem *sle = src_search_list.head;
106
107 /* Open input file. If open fails, walk along search-list until
108 open succeeds or reaching end of list. */
109 strcpy (fname, sf->name);
110
111 if (IS_ABSOLUTE_PATH (sf->name))
112 sle = 0; /* Don't use search list for absolute paths. */
113
114 name_only = 0;
115 while (TRUE)
116 {
117 DBG (SRCDEBUG, printf ("[annotate_source]: looking for %s, trying %s\n",
118 sf->name, fname));
119
120 ifp = fopen (fname, FOPEN_RB);
121 if (ifp)
122 break;
123
124 if (!sle && !name_only)
125 {
126 name_only = strrchr (sf->name, '/');
127#ifdef HAVE_DOS_BASED_FILE_SYSTEM
128 {
129 char *bslash = strrchr (sf->name, '\\');
130 if (name_only == NULL || (bslash != NULL && bslash > name_only))
131 name_only = bslash;
132 if (name_only == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
133 name_only = (char *)sf->name + 1;
134 }
135#endif
136 if (name_only)
137 {
138 /* Try search-list again, but this time with name only. */
139 ++name_only;
140 sle = src_search_list.head;
141 }
142 }
143
144 if (sle)
145 {
146 strcpy (fname, sle->path);
147#ifdef HAVE_DOS_BASED_FILE_SYSTEM
148 /* d:foo is not the same thing as d:/foo! */
149 if (fname[strlen (fname) - 1] == ':')
150 strcat (fname, ".");
151#endif
152 strcat (fname, "/");
153
154 if (name_only)
155 strcat (fname, name_only);
156 else
157 strcat (fname, sf->name);
158
159 sle = sle->next;
160 }
161 else
162 {
163 if (errno == ENOENT)
164 fprintf (stderr, _("%s: could not locate `%s'\n"),
165 whoami, sf->name);
166 else
167 perror (sf->name);
168
169 return 0;
170 }
171 }
172
173 ofp = stdout;
174
175 if (create_annotation_files)
176 {
177 /* Try to create annotated source file. */
178 const char *filename;
179
180 /* Create annotation files in the current working directory. */
181 filename = strrchr (sf->name, '/');
182#ifdef HAVE_DOS_BASED_FILE_SYSTEM
183 {
184 char *bslash = strrchr (sf->name, '\\');
185 if (filename == NULL || (bslash != NULL && bslash > filename))
186 filename = bslash;
187 if (filename == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
188 filename = sf->name + 1;
189 }
190#endif
191 if (filename)
192 ++filename;
193 else
194 filename = sf->name;
195
196 strcpy (fname, filename);
197 strcat (fname, EXT_ANNO);
198#ifdef __MSDOS__
199 {
200 /* foo.cpp-ann can overwrite foo.cpp due to silent truncation of
201 file names on 8+3 filesystems. Their `stat' better be good... */
202 struct stat buf1, buf2;
203
204 if (stat (filename, &buf1) == 0
205 && stat (fname, &buf2) == 0
206 && buf1.st_ino == buf2.st_ino)
207 {
208 char *dot = strrchr (fname, '.');
209
210 if (dot)
211 *dot = '\0';
212 strcat (fname, ".ann");
213 }
214 }
215#endif
216 ofp = fopen (fname, "w");
217
218 if (!ofp)
219 {
220 perror (fname);
221 return 0;
222 }
223 }
224
225 /* Print file names if output goes to stdout
226 and there are more than one source file. */
227 if (ofp == stdout)
228 {
229 if (first_file)
230 first_file = FALSE;
231 else
232 fputc ('\n', ofp);
233
234 if (first_output)
235 first_output = FALSE;
236 else
237 fprintf (ofp, "\f\n");
238
239 fprintf (ofp, _("*** File %s:\n"), sf->name);
240 }
241
242 annotation = xmalloc (max_width + 1);
243 line_num = 1;
244 new_line = TRUE;
245
246 while ((nread = fread (buf, 1, sizeof (buf), ifp)) > 0)
247 {
248 for (i = 0; i < nread; ++i)
249 {
250 if (new_line)
251 {
252 (*annote) (annotation, max_width, line_num, arg);
253 fputs (annotation, ofp);
254 ++line_num;
255 new_line = FALSE;
256 }
257
258 new_line = (buf[i] == '\n');
259 fputc (buf[i], ofp);
260 }
261 }
262
263 free (annotation);
264 return ofp;
265}
Note: See TracBrowser for help on using the repository browser.