2 * Keeps track of source files.
9 #include "search_list.h"
12 #define EXT_ANNO "-ann" /* postfix of annotated files */
15 * Default option values:
17 bool create_annotation_files
= FALSE
;
19 Search_List src_search_list
=
21 Source_File
*first_src_file
= 0;
25 DEFUN (source_file_lookup_path
, (path
), const char *path
)
29 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
31 if (FILENAME_CMP (path
, sf
->name
) == 0)
38 /* create a new source file descriptor: */
40 sf
= (Source_File
*) xmalloc (sizeof (*sf
));
41 memset (sf
, 0, sizeof (*sf
));
42 sf
->name
= xstrdup (path
);
43 sf
->next
= first_src_file
;
51 DEFUN (source_file_lookup_name
, (filename
), const char *filename
)
56 * The user cannot know exactly how a filename will be stored in
57 * the debugging info (e.g., ../include/foo.h
58 * vs. /usr/include/foo.h). So we simply compare the filename
59 * component of a path only:
61 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
63 fname
= strrchr (sf
->name
, '/');
72 if (FILENAME_CMP (filename
, fname
) == 0)
82 DEFUN (annotate_source
, (sf
, max_width
, annote
, arg
),
83 Source_File
* sf AND
int max_width
84 AND
void (*annote
) PARAMS ((char *buf
, int w
, int l
, void *arg
))
87 static bool first_file
= TRUE
;
88 int i
, line_num
, nread
;
92 char *annotation
, *name_only
;
94 Search_List_Elem
*sle
= src_search_list
.head
;
97 * Open input file. If open fails, walk along search-list until
98 * open succeeds or reaching end of list:
100 strcpy (fname
, sf
->name
);
101 if (IS_ABSOLUTE_PATH (sf
->name
))
103 sle
= 0; /* don't use search list for absolute paths */
108 DBG (SRCDEBUG
, printf ("[annotate_source]: looking for %s, trying %s\n",
110 ifp
= fopen (fname
, FOPEN_RB
);
115 if (!sle
&& !name_only
)
117 name_only
= strrchr (sf
->name
, '/');
118 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
120 char *bslash
= strrchr (sf
->name
, '\\');
121 if (bslash
> name_only
)
123 if (name_only
== NULL
&& sf
->name
[0] != '\0' && sf
->name
[1] == ':')
124 name_only
= (char *)sf
->name
+ 1;
129 /* try search-list again, but this time with name only: */
131 sle
= src_search_list
.head
;
136 strcpy (fname
, sle
->path
);
137 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
138 /* d:foo is not the same thing as d:/foo! */
139 if (fname
[strlen (fname
) - 1] == ':')
145 strcat (fname
, name_only
);
149 strcat (fname
, sf
->name
);
157 fprintf (stderr
, _("%s: could not locate `%s'\n"),
169 if (create_annotation_files
)
171 /* try to create annotated source file: */
172 const char *filename
;
174 /* create annotation files in the current working directory: */
175 filename
= strrchr (sf
->name
, '/');
176 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
178 char *bslash
= strrchr (sf
->name
, '\\');
179 if (bslash
> filename
)
181 if (filename
== NULL
&& sf
->name
[0] != '\0' && sf
->name
[1] == ':')
182 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 and there are
224 * more than one source file:
238 first_output
= FALSE
;
242 fprintf (ofp
, "\f\n");
244 fprintf (ofp
, _("*** File %s:\n"), sf
->name
);
247 annotation
= xmalloc (max_width
+ 1);
250 while ((nread
= fread (buf
, 1, sizeof (buf
), ifp
)) > 0)
252 for (i
= 0; i
< nread
; ++i
)
256 (*annote
) (annotation
, max_width
, line_num
, arg
);
257 fputs (annotation
, ofp
);
261 new_line
= (buf
[i
] == '\n');