Updated French translation
[binutils.git] / gprof / source.c
blob7b9401f60adbfafa558e9e805038a6efbd6c7f86
1 /* source.c - Keep track of source files.
3 Copyright 2000, 2001 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 2 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., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "gprof.h"
23 #include "libiberty.h"
24 #include "filenames.h"
25 #include "search_list.h"
26 #include "source.h"
28 #define EXT_ANNO "-ann" /* Postfix of annotated files. */
30 /* Default option values. */
31 bool create_annotation_files = FALSE;
33 Search_List src_search_list = {0, 0};
34 Source_File *first_src_file = 0;
37 Source_File *
38 DEFUN (source_file_lookup_path, (path), const char *path)
40 Source_File *sf;
42 for (sf = first_src_file; sf; sf = sf->next)
44 if (FILENAME_CMP (path, sf->name) == 0)
45 break;
48 if (!sf)
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;
57 first_src_file = sf;
60 return sf;
64 Source_File *
65 DEFUN (source_file_lookup_name, (filename), const char *filename)
67 const char *fname;
68 Source_File *sf;
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, '/');
78 if (fname)
79 ++fname;
80 else
81 fname = sf->name;
83 if (FILENAME_CMP (filename, fname) == 0)
84 break;
87 return sf;
91 FILE *
92 DEFUN (annotate_source, (sf, max_width, annote, arg),
93 Source_File * sf AND int max_width
94 AND void (*annote) PARAMS ((char *buf, int w, int l, void *arg))
95 AND void *arg)
97 static bool first_file = TRUE;
98 int i, line_num, nread;
99 bool new_line;
100 char buf[8192];
101 char fname[PATH_MAX];
102 char *annotation, *name_only;
103 FILE *ifp, *ofp;
104 Search_List_Elem *sle = src_search_list.head;
106 /* Open input file. If open fails, walk along search-list until
107 open succeeds or reaching end of list. */
108 strcpy (fname, sf->name);
110 if (IS_ABSOLUTE_PATH (sf->name))
111 sle = 0; /* Don't use search list for absolute paths. */
113 name_only = 0;
114 while (TRUE)
116 DBG (SRCDEBUG, printf ("[annotate_source]: looking for %s, trying %s\n",
117 sf->name, fname));
119 ifp = fopen (fname, FOPEN_RB);
120 if (ifp)
121 break;
123 if (!sle && !name_only)
125 name_only = strrchr (sf->name, '/');
126 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
128 char *bslash = strrchr (sf->name, '\\');
129 if (name_only == NULL || (bslash != NULL && bslash > name_only))
130 name_only = bslash;
131 if (name_only == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
132 name_only = (char *)sf->name + 1;
134 #endif
135 if (name_only)
137 /* Try search-list again, but this time with name only. */
138 ++name_only;
139 sle = src_search_list.head;
143 if (sle)
145 strcpy (fname, sle->path);
146 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
147 /* d:foo is not the same thing as d:/foo! */
148 if (fname[strlen (fname) - 1] == ':')
149 strcat (fname, ".");
150 #endif
151 strcat (fname, "/");
153 if (name_only)
154 strcat (fname, name_only);
155 else
156 strcat (fname, sf->name);
158 sle = sle->next;
160 else
162 if (errno == ENOENT)
163 fprintf (stderr, _("%s: could not locate `%s'\n"),
164 whoami, sf->name);
165 else
166 perror (sf->name);
168 return 0;
172 ofp = stdout;
174 if (create_annotation_files)
176 /* Try to create annotated source file. */
177 const char *filename;
179 /* Create annotation files in the current working directory. */
180 filename = strrchr (sf->name, '/');
181 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
183 char *bslash = strrchr (sf->name, '\\');
184 if (filename == NULL || (bslash != NULL && bslash > filename))
185 filename = bslash;
186 if (filename == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
187 filename = sf->name + 1;
189 #endif
190 if (filename)
191 ++filename;
192 else
193 filename = sf->name;
195 strcpy (fname, filename);
196 strcat (fname, EXT_ANNO);
197 #ifdef __MSDOS__
199 /* foo.cpp-ann can overwrite foo.cpp due to silent truncation of
200 file names on 8+3 filesystems. Their `stat' better be good... */
201 struct stat buf1, buf2;
203 if (stat (filename, &buf1) == 0
204 && stat (fname, &buf2) == 0
205 && buf1.st_ino == buf2.st_ino)
207 char *dot = strrchr (fname, '.');
209 if (dot)
210 *dot = '\0';
211 strcat (fname, ".ann");
214 #endif
215 ofp = fopen (fname, "w");
217 if (!ofp)
219 perror (fname);
220 return 0;
224 /* Print file names if output goes to stdout
225 and there are more than one source file. */
226 if (ofp == stdout)
228 if (first_file)
229 first_file = FALSE;
230 else
231 fputc ('\n', ofp);
233 if (first_output)
234 first_output = FALSE;
235 else
236 fprintf (ofp, "\f\n");
238 fprintf (ofp, _("*** File %s:\n"), sf->name);
241 annotation = xmalloc (max_width + 1);
242 line_num = 1;
243 new_line = TRUE;
245 while ((nread = fread (buf, 1, sizeof (buf), ifp)) > 0)
247 for (i = 0; i < nread; ++i)
249 if (new_line)
251 (*annote) (annotation, max_width, line_num, arg);
252 fputs (annotation, ofp);
253 ++line_num;
254 new_line = FALSE;
257 new_line = (buf[i] == '\n');
258 fputc (buf[i], ofp);
262 free (annotation);
263 return ofp;