2 * Keeps track of source files.
7 #include "search_list.h"
10 #define EXT_ANNO "-ann" /* postfix of annotated files */
13 * Default option values:
15 bool create_annotation_files
= FALSE
;
17 Search_List src_search_list
=
19 Source_File
*first_src_file
= 0;
23 DEFUN (source_file_lookup_path
, (path
), const char *path
)
27 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
29 if (FILENAME_CMP (path
, sf
->name
) == 0)
36 /* create a new source file descriptor: */
38 sf
= (Source_File
*) xmalloc (sizeof (*sf
));
39 memset (sf
, 0, sizeof (*sf
));
40 sf
->name
= xstrdup (path
);
41 sf
->next
= first_src_file
;
49 DEFUN (source_file_lookup_name
, (filename
), const char *filename
)
54 * The user cannot know exactly how a filename will be stored in
55 * the debugging info (e.g., ../include/foo.h
56 * vs. /usr/include/foo.h). So we simply compare the filename
57 * component of a path only:
59 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
61 fname
= strrchr (sf
->name
, '/');
70 if (FILENAME_CMP (filename
, fname
) == 0)
80 DEFUN (annotate_source
, (sf
, max_width
, annote
, arg
),
81 Source_File
* sf AND
int max_width
82 AND
void (*annote
) PARAMS ((char *buf
, int w
, int l
, void *arg
))
85 static bool first_file
= TRUE
;
86 int i
, line_num
, nread
;
90 char *annotation
, *name_only
;
92 Search_List_Elem
*sle
= src_search_list
.head
;
95 * Open input file. If open fails, walk along search-list until
96 * open succeeds or reaching end of list:
98 strcpy (fname
, sf
->name
);
99 if (IS_ABSOLUTE_PATH (sf
->name
))
101 sle
= 0; /* don't use search list for absolute paths */
106 DBG (SRCDEBUG
, printf ("[annotate_source]: looking for %s, trying %s\n",
108 ifp
= fopen (fname
, FOPEN_RB
);
113 if (!sle
&& !name_only
)
115 name_only
= strrchr (sf
->name
, '/');
116 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
118 char *bslash
= strrchr (sf
->name
, '\\');
119 if (bslash
> name_only
)
121 if (name_only
== NULL
&& sf
->name
[0] != '\0' && sf
->name
[1] == ':')
122 name_only
= (char *)sf
->name
+ 1;
127 /* try search-list again, but this time with name only: */
129 sle
= src_search_list
.head
;
134 strcpy (fname
, sle
->path
);
135 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
136 /* d:foo is not the same thing as d:/foo! */
137 if (fname
[strlen (fname
) - 1] == ':')
143 strcat (fname
, name_only
);
147 strcat (fname
, sf
->name
);
155 fprintf (stderr
, _("%s: could not locate `%s'\n"),
167 if (create_annotation_files
)
169 /* try to create annotated source file: */
170 const char *filename
;
172 /* create annotation files in the current working directory: */
173 filename
= strrchr (sf
->name
, '/');
174 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
176 char *bslash
= strrchr (sf
->name
, '\\');
177 if (bslash
> filename
)
179 if (filename
== NULL
&& sf
->name
[0] != '\0' && sf
->name
[1] == ':')
180 filename
= sf
->name
+ 1;
192 strcpy (fname
, filename
);
193 strcat (fname
, EXT_ANNO
);
196 /* foo.cpp-ann can overwrite foo.cpp due to silent truncation of
197 file names on 8+3 filesystems. Their `stat' better be good... */
198 struct stat buf1
, buf2
;
200 if (stat (filename
, &buf1
) == 0
201 && stat (fname
, &buf2
) == 0
202 && buf1
.st_ino
== buf2
.st_ino
)
204 char *dot
= strrchr (fname
, '.');
208 strcat (fname
, ".ann");
212 ofp
= fopen (fname
, "w");
221 * Print file names if output goes to stdout and there are
222 * more than one source file:
236 first_output
= FALSE
;
240 fprintf (ofp
, "\f\n");
242 fprintf (ofp
, _("*** File %s:\n"), sf
->name
);
245 annotation
= xmalloc (max_width
+ 1);
248 while ((nread
= fread (buf
, 1, sizeof (buf
), ifp
)) > 0)
250 for (i
= 0; i
< nread
; ++i
)
254 (*annote
) (annotation
, max_width
, line_num
, arg
);
255 fputs (annotation
, ofp
);
259 new_line
= (buf
[i
] == '\n');