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
= {0, 0};
17 Source_File
*first_src_file
= 0;
21 DEFUN(source_file_lookup_path
, (path
), const char *path
)
25 for (sf
= first_src_file
; sf
; sf
= sf
->next
) {
26 if (strcmp(path
, sf
->name
) == 0) {
31 /* create a new source file descriptor: */
33 sf
= (Source_File
*) xmalloc(sizeof(*sf
));
34 memset(sf
, 0, sizeof(*sf
));
35 sf
->name
= strdup(path
);
36 sf
->next
= first_src_file
;
40 } /* source_file_lookup_path */
44 DEFUN(source_file_lookup_name
, (filename
), const char *filename
)
49 * The user cannot know exactly how a filename will be stored in
50 * the debugging info (e.g., ../include/foo.h
51 * vs. /usr/include/foo.h). So we simply compare the filename
52 * component of a path only:
54 for (sf
= first_src_file
; sf
; sf
= sf
->next
) {
55 fname
= strrchr(sf
->name
, '/');
61 if (strcmp(filename
, fname
) == 0) {
66 } /* source_file_lookup_name */
70 DEFUN(annotate_source
, (sf
, max_width
, annote
, arg
),
71 Source_File
*sf AND
int max_width
72 AND
void (*annote
) PARAMS((char *buf
, int w
, int l
, void *arg
))
75 static bool first_file
= TRUE
;
76 int i
, line_num
, nread
;
80 char *annotation
, *name_only
;
82 Search_List_Elem
*sle
= src_search_list
.head
;
85 * Open input file. If open fails, walk along search-list until
86 * open succeeds or reaching end of list:
88 strcpy(fname
, sf
->name
);
89 if (sf
->name
[0] == '/') {
90 sle
= 0; /* don't use search list for absolute paths */
94 DBG(SRCDEBUG
, printf("[annotate_source]: looking for %s, trying %s\n",
96 ifp
= fopen(fname
, FOPEN_RB
);
100 if (!sle
&& !name_only
) {
101 name_only
= strrchr(sf
->name
, '/');
103 /* try search-list again, but this time with name only: */
105 sle
= src_search_list
.head
;
109 strcpy(fname
, sle
->path
);
112 strcat(fname
, name_only
);
114 strcat(fname
, sf
->name
);
118 if (errno
== ENOENT
) {
119 fprintf(stderr
, "%s: could not locate `%s'\n",
129 if (create_annotation_files
) {
130 /* try to create annotated source file: */
131 const char *filename
;
133 /* create annotation files in the current working directory: */
134 filename
= strrchr(sf
->name
, '/');
141 strcpy(fname
, filename
);
142 strcat(fname
, EXT_ANNO
);
143 ofp
= fopen(fname
, "w");
151 * Print file names if output goes to stdout and there are
152 * more than one source file:
161 first_output
= FALSE
;
163 fprintf(ofp
, "\f\n");
165 fprintf(ofp
, "*** File %s:\n", sf
->name
);
168 annotation
= xmalloc(max_width
+ 1);
171 while ((nread
= fread(buf
, 1, sizeof(buf
), ifp
)) > 0) {
172 for (i
= 0; i
< nread
; ++i
) {
174 (*annote
)(annotation
, max_width
, line_num
, arg
);
175 fputs(annotation
, ofp
);
176 ++line_num
; new_line
= FALSE
;
178 new_line
= (buf
[i
] == '\n');
184 } /* annotate_source */
186 /*** end of source.c ***/