diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-01-09 19:42:10 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-01-09 19:42:10 +0200 |
commit | 6d374cd24255831d893fbfbd45fddc73655de0e3 (patch) | |
tree | 6f5bf9ec3a71a3b16c4463c8e3042d5d215e6cb5 | |
parent | 007b5a854a9ffbb2a5bc0b01fb50dbd54a33df42 (diff) | |
download | grecs-6d374cd24255831d893fbfbd45fddc73655de0e3.tar.gz grecs-6d374cd24255831d893fbfbd45fddc73655de0e3.tar.bz2 |
Add functions for iterating over include directories.
* src/grecs.h (GRECS_STD_INCLUDE,GRECS_USR_INCLUDE): New defines.
(grecs_include_path_count)
(grecs_foreach_include_dir): New protos.
* src/preproc.c (grecs_include_path_count)
(grecs_foreach_include_dir): New functions.
-rw-r--r-- | src/grecs.h | 7 | ||||
-rw-r--r-- | src/preproc.c | 81 |
2 files changed, 66 insertions, 22 deletions
diff --git a/src/grecs.h b/src/grecs.h index ff8671c..a5ec18d 100644 --- a/src/grecs.h +++ b/src/grecs.h | |||
@@ -303,6 +303,13 @@ int grecs_preproc_init(const char *name); | |||
303 | void grecs_preproc_done(void); | 303 | void grecs_preproc_done(void); |
304 | int grecs_preproc_run(const char *config_file, const char *extpp); | 304 | int grecs_preproc_run(const char *config_file, const char *extpp); |
305 | 305 | ||
306 | #define GRECS_STD_INCLUDE 0x01 | ||
307 | #define GRECS_USR_INCLUDE 0x02 | ||
308 | |||
309 | size_t grecs_include_path_count(int flag); | ||
310 | int grecs_foreach_include_dir(int flag, int (*fun)(int, const char *, void *), | ||
311 | void *data); | ||
312 | |||
306 | char *grecs_find_include_file(const char *name, int allow_cwd); | 313 | char *grecs_find_include_file(const char *name, int allow_cwd); |
307 | 314 | ||
308 | FILE *grecs_preproc_extrn_start(const char *file, pid_t *ppid); | 315 | FILE *grecs_preproc_extrn_start(const char *file, pid_t *ppid); |
diff --git a/src/preproc.c b/src/preproc.c index 2626705..f1d5227 100644 --- a/src/preproc.c +++ b/src/preproc.c | |||
@@ -235,9 +235,46 @@ ctx_lookup(struct stat *st) | |||
235 | } | 235 | } |
236 | 236 | ||
237 | const char *grecs_preprocessor = NULL; | 237 | const char *grecs_preprocessor = NULL; |
238 | static struct grecs_list *include_path; | 238 | static struct grecs_list *grecs_usr_include_path; |
239 | static struct grecs_list *std_include_path; | 239 | static struct grecs_list *grecs_std_include_path; |
240 | 240 | ||
241 | size_t | ||
242 | grecs_include_path_count(int flag) | ||
243 | { | ||
244 | size_t count = 0; | ||
245 | if (flag & GRECS_STD_INCLUDE) | ||
246 | count += grecs_std_include_path ? grecs_std_include_path->count : 0; | ||
247 | if (flag & GRECS_USR_INCLUDE) | ||
248 | count += grecs_usr_include_path ? grecs_usr_include_path->count : 0; | ||
249 | return 0; | ||
250 | } | ||
251 | |||
252 | static int | ||
253 | foreach_dir(struct grecs_list *list, int flag, | ||
254 | int (*fun)(int, const char *, void *), void *data) | ||
255 | { | ||
256 | int rc; | ||
257 | struct grecs_list_entry *ep; | ||
258 | |||
259 | for (ep = list->head; rc == 0 && ep; ep = ep->next) | ||
260 | rc = fun(flag, ep->data, data); | ||
261 | return rc; | ||
262 | } | ||
263 | |||
264 | int | ||
265 | grecs_foreach_include_dir(int flag, int (*fun)(int, const char *, void *), | ||
266 | void *data) | ||
267 | { | ||
268 | int rc = 0; | ||
269 | |||
270 | if (flag & GRECS_STD_INCLUDE) | ||
271 | rc = foreach_dir(grecs_std_include_path, GRECS_STD_INCLUDE, fun, data); | ||
272 | if (rc == 0 && (flag & GRECS_USR_INCLUDE)) | ||
273 | rc = foreach_dir(grecs_usr_include_path, GRECS_USR_INCLUDE, fun, data); | ||
274 | return rc; | ||
275 | } | ||
276 | |||
277 | |||
241 | struct file_data { | 278 | struct file_data { |
242 | const char *name; | 279 | const char *name; |
243 | size_t namelen; | 280 | size_t namelen; |
@@ -275,26 +312,26 @@ incl_free(void *data) | |||
275 | void | 312 | void |
276 | grecs_include_path_clear() | 313 | grecs_include_path_clear() |
277 | { | 314 | { |
278 | if (include_path) | 315 | if (grecs_usr_include_path) |
279 | grecs_list_clear(include_path); | 316 | grecs_list_clear(grecs_usr_include_path); |
280 | if (std_include_path) | 317 | if (grecs_std_include_path) |
281 | grecs_list_clear(std_include_path); | 318 | grecs_list_clear(grecs_std_include_path); |
282 | } | 319 | } |
283 | 320 | ||
284 | void | 321 | void |
285 | grecs_include_path_setup_v(char **dirs) | 322 | grecs_include_path_setup_v(char **dirs) |
286 | { | 323 | { |
287 | if (!include_path) { | 324 | if (!grecs_usr_include_path) { |
288 | include_path = grecs_list_create(); | 325 | grecs_usr_include_path = grecs_list_create(); |
289 | include_path->free_entry = incl_free; | 326 | grecs_usr_include_path->free_entry = incl_free; |
290 | } | 327 | } |
291 | std_include_path = grecs_list_create(); | 328 | grecs_std_include_path = grecs_list_create(); |
292 | std_include_path->free_entry = incl_free; | 329 | grecs_std_include_path->free_entry = incl_free; |
293 | if (dirs) { | 330 | if (dirs) { |
294 | int i; | 331 | int i; |
295 | for (i = 0; dirs[i]; i++) | 332 | for (i = 0; dirs[i]; i++) |
296 | /* FIXME: Element never freed */ | 333 | /* FIXME: Element never freed */ |
297 | grecs_list_append(std_include_path, | 334 | grecs_list_append(grecs_std_include_path, |
298 | grecs_strdup(dirs[i])); | 335 | grecs_strdup(dirs[i])); |
299 | } | 336 | } |
300 | } | 337 | } |
@@ -331,11 +368,11 @@ grecs_include_path_setup(const char *dir, ...) | |||
331 | void | 368 | void |
332 | grecs_preproc_add_include_dir(char *dir) | 369 | grecs_preproc_add_include_dir(char *dir) |
333 | { | 370 | { |
334 | if (!include_path) { | 371 | if (!grecs_usr_include_path) { |
335 | include_path = grecs_list_create(); | 372 | grecs_usr_include_path = grecs_list_create(); |
336 | include_path->free_entry = incl_free; | 373 | grecs_usr_include_path->free_entry = incl_free; |
337 | } | 374 | } |
338 | grecs_list_append(include_path, grecs_strdup(dir)); | 375 | grecs_list_append(grecs_usr_include_path, grecs_strdup(dir)); |
339 | } | 376 | } |
340 | 377 | ||
341 | static struct grecs_symtab *incl_sources; | 378 | static struct grecs_symtab *incl_sources; |
@@ -497,17 +534,17 @@ grecs_find_include_file(const char *name, int allow_cwd) | |||
497 | fd.buflen = 0; | 534 | fd.buflen = 0; |
498 | fd.found = 0; | 535 | fd.found = 0; |
499 | 536 | ||
500 | if (!include_path) | 537 | if (!grecs_usr_include_path) |
501 | grecs_include_path_setup(NULL); | 538 | grecs_include_path_setup(NULL); |
502 | if (allow_cwd) { | 539 | if (allow_cwd) { |
503 | grecs_list_append(include_path, cwd); | 540 | grecs_list_append(grecs_usr_include_path, cwd); |
504 | pp_list_find(include_path, &fd); | 541 | pp_list_find(grecs_usr_include_path, &fd); |
505 | grecs_list_remove_tail(include_path); | 542 | grecs_list_remove_tail(grecs_usr_include_path); |
506 | } else | 543 | } else |
507 | pp_list_find(include_path, &fd); | 544 | pp_list_find(grecs_usr_include_path, &fd); |
508 | 545 | ||
509 | if (!fd.found) { | 546 | if (!fd.found) { |
510 | pp_list_find(std_include_path, &fd); | 547 | pp_list_find(grecs_std_include_path, &fd); |
511 | if (!fd.found) | 548 | if (!fd.found) |
512 | return NULL; | 549 | return NULL; |
513 | } | 550 | } |