2 * Keeps track of source files.
6 #include "search_list.h"
9 #define EXT_ANNO "-ann" /* postfix of annotated files */
12 * Default option values:
14 bool create_annotation_files
= FALSE
;
16 Search_List src_search_list
=
18 Source_File
*first_src_file
= 0;
22 DEFUN (source_file_lookup_path
, (path
), const char *path
)
26 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
28 if (strcmp (path
, sf
->name
) == 0)
35 /* create a new source file descriptor: */
37 sf
= (Source_File
*) xmalloc (sizeof (*sf
));
38 memset (sf
, 0, sizeof (*sf
));
39 sf
->name
= xstrdup (path
);
40 sf
->next
= first_src_file
;
48 DEFUN (source_file_lookup_name
, (filename
), const char *filename
)
53 * The user cannot know exactly how a filename will be stored in
54 * the debugging info (e.g., ../include/foo.h
55 * vs. /usr/include/foo.h). So we simply compare the filename
56 * component of a path only:
58 for (sf
= first_src_file
; sf
; sf
= sf
->next
)
60 fname
= strrchr (sf
->name
, '/');
69 if (strcmp (filename
, fname
) == 0)
79 DEFUN (annotate_source
, (sf
, max_width
, annote
, arg
),
80 Source_File
* sf AND
int max_width
81 AND
void (*annote
) PARAMS ((char *buf
, int w
, int l
, void *arg
))
84 static bool first_file
= TRUE
;
85 int i
, line_num
, nread
;
89 char *annotation
, *name_only
;
91 Search_List_Elem
*sle
= src_search_list
.head
;
94 * Open input file. If open fails, walk along search-list until
95 * open succeeds or reaching end of list:
97 strcpy (fname
, sf
->name
);
98 if (sf
->name
[0] == '/')
100 sle
= 0; /* don't use search list for absolute paths */
105 DBG (SRCDEBUG
, printf ("[annotate_source]: looking for %s, trying %s\n",
107 ifp
= fopen (fname
, FOPEN_RB
);
112 if (!sle
&& !name_only
)
114 name_only
= strrchr (sf
->name
, '/');
117 /* try search-list again, but this time with name only: */
119 sle
= src_search_list
.head
;
124 strcpy (fname
, sle
->path
);
128 strcat (fname
, name_only
);
132 strcat (fname
, sf
->name
);
140 fprintf (stderr
, _("%s: could not locate `%s'\n"),
152 if (create_annotation_files
)
154 /* try to create annotated source file: */
155 const char *filename
;
157 /* create annotation files in the current working directory: */
158 filename
= strrchr (sf
->name
, '/');
168 strcpy (fname
, filename
);
169 strcat (fname
, EXT_ANNO
);
170 ofp
= fopen (fname
, "w");
179 * Print file names if output goes to stdout and there are
180 * more than one source file:
194 first_output
= FALSE
;
198 fprintf (ofp
, "\f\n");
200 fprintf (ofp
, _("*** File %s:\n"), sf
->name
);
203 annotation
= xmalloc (max_width
+ 1);
206 while ((nread
= fread (buf
, 1, sizeof (buf
), ifp
)) > 0)
208 for (i
= 0; i
< nread
; ++i
)
212 (*annote
) (annotation
, max_width
, line_num
, arg
);
213 fputs (annotation
, ofp
);
217 new_line
= (buf
[i
] == '\n');