diff options
Diffstat (limited to 'scripts/dtc/srcpos.c')
-rw-r--r-- | scripts/dtc/srcpos.c | 102 |
1 files changed, 7 insertions, 95 deletions
diff --git a/scripts/dtc/srcpos.c b/scripts/dtc/srcpos.c index 246ab4bc0d9..2dbc874288c 100644 --- a/scripts/dtc/srcpos.c +++ b/scripts/dtc/srcpos.c | |||
@@ -24,15 +24,6 @@ | |||
24 | #include "dtc.h" | 24 | #include "dtc.h" |
25 | #include "srcpos.h" | 25 | #include "srcpos.h" |
26 | 26 | ||
27 | /* A node in our list of directories to search for source/include files */ | ||
28 | struct search_path { | ||
29 | struct search_path *next; /* next node in list, NULL for end */ | ||
30 | const char *dirname; /* name of directory to search */ | ||
31 | }; | ||
32 | |||
33 | /* This is the list of directories that we search for source files */ | ||
34 | static struct search_path *search_path_head, **search_path_tail; | ||
35 | |||
36 | 27 | ||
37 | static char *dirname(const char *path) | 28 | static char *dirname(const char *path) |
38 | { | 29 | { |
@@ -49,71 +40,12 @@ static char *dirname(const char *path) | |||
49 | return NULL; | 40 | return NULL; |
50 | } | 41 | } |
51 | 42 | ||
52 | FILE *depfile; /* = NULL */ | ||
53 | struct srcfile_state *current_srcfile; /* = NULL */ | 43 | struct srcfile_state *current_srcfile; /* = NULL */ |
54 | 44 | ||
55 | /* Detect infinite include recursion. */ | 45 | /* Detect infinite include recursion. */ |
56 | #define MAX_SRCFILE_DEPTH (100) | 46 | #define MAX_SRCFILE_DEPTH (100) |
57 | static int srcfile_depth; /* = 0 */ | 47 | static int srcfile_depth; /* = 0 */ |
58 | 48 | ||
59 | |||
60 | /** | ||
61 | * Try to open a file in a given directory. | ||
62 | * | ||
63 | * If the filename is an absolute path, then dirname is ignored. If it is a | ||
64 | * relative path, then we look in that directory for the file. | ||
65 | * | ||
66 | * @param dirname Directory to look in, or NULL for none | ||
67 | * @param fname Filename to look for | ||
68 | * @param fp Set to NULL if file did not open | ||
69 | * @return allocated filename on success (caller must free), NULL on failure | ||
70 | */ | ||
71 | static char *try_open(const char *dirname, const char *fname, FILE **fp) | ||
72 | { | ||
73 | char *fullname; | ||
74 | |||
75 | if (!dirname || fname[0] == '/') | ||
76 | fullname = xstrdup(fname); | ||
77 | else | ||
78 | fullname = join_path(dirname, fname); | ||
79 | |||
80 | *fp = fopen(fullname, "r"); | ||
81 | if (!*fp) { | ||
82 | free(fullname); | ||
83 | fullname = NULL; | ||
84 | } | ||
85 | |||
86 | return fullname; | ||
87 | } | ||
88 | |||
89 | /** | ||
90 | * Open a file for read access | ||
91 | * | ||
92 | * If it is a relative filename, we search the full search path for it. | ||
93 | * | ||
94 | * @param fname Filename to open | ||
95 | * @param fp Returns pointer to opened FILE, or NULL on failure | ||
96 | * @return pointer to allocated filename, which caller must free | ||
97 | */ | ||
98 | static char *fopen_any_on_path(const char *fname, FILE **fp) | ||
99 | { | ||
100 | const char *cur_dir = NULL; | ||
101 | struct search_path *node; | ||
102 | char *fullname; | ||
103 | |||
104 | /* Try current directory first */ | ||
105 | assert(fp); | ||
106 | if (current_srcfile) | ||
107 | cur_dir = current_srcfile->dir; | ||
108 | fullname = try_open(cur_dir, fname, fp); | ||
109 | |||
110 | /* Failing that, try each search path in turn */ | ||
111 | for (node = search_path_head; !*fp && node; node = node->next) | ||
112 | fullname = try_open(node->dirname, fname, fp); | ||
113 | |||
114 | return fullname; | ||
115 | } | ||
116 | |||
117 | FILE *srcfile_relative_open(const char *fname, char **fullnamep) | 49 | FILE *srcfile_relative_open(const char *fname, char **fullnamep) |
118 | { | 50 | { |
119 | FILE *f; | 51 | FILE *f; |
@@ -123,15 +55,18 @@ FILE *srcfile_relative_open(const char *fname, char **fullnamep) | |||
123 | f = stdin; | 55 | f = stdin; |
124 | fullname = xstrdup("<stdin>"); | 56 | fullname = xstrdup("<stdin>"); |
125 | } else { | 57 | } else { |
126 | fullname = fopen_any_on_path(fname, &f); | 58 | if (!current_srcfile || !current_srcfile->dir |
59 | || (fname[0] == '/')) | ||
60 | fullname = xstrdup(fname); | ||
61 | else | ||
62 | fullname = join_path(current_srcfile->dir, fname); | ||
63 | |||
64 | f = fopen(fullname, "r"); | ||
127 | if (!f) | 65 | if (!f) |
128 | die("Couldn't open \"%s\": %s\n", fname, | 66 | die("Couldn't open \"%s\": %s\n", fname, |
129 | strerror(errno)); | 67 | strerror(errno)); |
130 | } | 68 | } |
131 | 69 | ||
132 | if (depfile) | ||
133 | fprintf(depfile, " %s", fullname); | ||
134 | |||
135 | if (fullnamep) | 70 | if (fullnamep) |
136 | *fullnamep = fullname; | 71 | *fullnamep = fullname; |
137 | else | 72 | else |
@@ -180,23 +115,6 @@ int srcfile_pop(void) | |||
180 | return current_srcfile ? 1 : 0; | 115 | return current_srcfile ? 1 : 0; |
181 | } | 116 | } |
182 | 117 | ||
183 | void srcfile_add_search_path(const char *dirname) | ||
184 | { | ||
185 | struct search_path *node; | ||
186 | |||
187 | /* Create the node */ | ||
188 | node = xmalloc(sizeof(*node)); | ||
189 | node->next = NULL; | ||
190 | node->dirname = xstrdup(dirname); | ||
191 | |||
192 | /* Add to the end of our list */ | ||
193 | if (search_path_tail) | ||
194 | *search_path_tail = node; | ||
195 | else | ||
196 | search_path_head = node; | ||
197 | search_path_tail = &node->next; | ||
198 | } | ||
199 | |||
200 | /* | 118 | /* |
201 | * The empty source position. | 119 | * The empty source position. |
202 | */ | 120 | */ |
@@ -328,9 +246,3 @@ srcpos_warn(struct srcpos *pos, char const *fmt, ...) | |||
328 | 246 | ||
329 | va_end(va); | 247 | va_end(va); |
330 | } | 248 | } |
331 | |||
332 | void srcpos_set_line(char *f, int l) | ||
333 | { | ||
334 | current_srcfile->name = f; | ||
335 | current_srcfile->lineno = l; | ||
336 | } | ||