From 6d374cd24255831d893fbfbd45fddc73655de0e3 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Mon, 9 Jan 2012 19:42:10 +0200 Subject: 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. --- src/grecs.h | 7 ++++++ 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); void grecs_preproc_done(void); int grecs_preproc_run(const char *config_file, const char *extpp); +#define GRECS_STD_INCLUDE 0x01 +#define GRECS_USR_INCLUDE 0x02 + +size_t grecs_include_path_count(int flag); +int grecs_foreach_include_dir(int flag, int (*fun)(int, const char *, void *), + void *data); + char *grecs_find_include_file(const char *name, int allow_cwd); 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) } const char *grecs_preprocessor = NULL; -static struct grecs_list *include_path; -static struct grecs_list *std_include_path; +static struct grecs_list *grecs_usr_include_path; +static struct grecs_list *grecs_std_include_path; +size_t +grecs_include_path_count(int flag) +{ + size_t count = 0; + if (flag & GRECS_STD_INCLUDE) + count += grecs_std_include_path ? grecs_std_include_path->count : 0; + if (flag & GRECS_USR_INCLUDE) + count += grecs_usr_include_path ? grecs_usr_include_path->count : 0; + return 0; +} + +static int +foreach_dir(struct grecs_list *list, int flag, + int (*fun)(int, const char *, void *), void *data) +{ + int rc; + struct grecs_list_entry *ep; + + for (ep = list->head; rc == 0 && ep; ep = ep->next) + rc = fun(flag, ep->data, data); + return rc; +} + +int +grecs_foreach_include_dir(int flag, int (*fun)(int, const char *, void *), + void *data) +{ + int rc = 0; + + if (flag & GRECS_STD_INCLUDE) + rc = foreach_dir(grecs_std_include_path, GRECS_STD_INCLUDE, fun, data); + if (rc == 0 && (flag & GRECS_USR_INCLUDE)) + rc = foreach_dir(grecs_usr_include_path, GRECS_USR_INCLUDE, fun, data); + return rc; +} + + struct file_data { const char *name; size_t namelen; @@ -275,26 +312,26 @@ incl_free(void *data) void grecs_include_path_clear() { - if (include_path) - grecs_list_clear(include_path); - if (std_include_path) - grecs_list_clear(std_include_path); + if (grecs_usr_include_path) + grecs_list_clear(grecs_usr_include_path); + if (grecs_std_include_path) + grecs_list_clear(grecs_std_include_path); } void grecs_include_path_setup_v(char **dirs) { - if (!include_path) { - include_path = grecs_list_create(); - include_path->free_entry = incl_free; + if (!grecs_usr_include_path) { + grecs_usr_include_path = grecs_list_create(); + grecs_usr_include_path->free_entry = incl_free; } - std_include_path = grecs_list_create(); - std_include_path->free_entry = incl_free; + grecs_std_include_path = grecs_list_create(); + grecs_std_include_path->free_entry = incl_free; if (dirs) { int i; for (i = 0; dirs[i]; i++) /* FIXME: Element never freed */ - grecs_list_append(std_include_path, + grecs_list_append(grecs_std_include_path, grecs_strdup(dirs[i])); } } @@ -331,11 +368,11 @@ grecs_include_path_setup(const char *dir, ...) void grecs_preproc_add_include_dir(char *dir) { - if (!include_path) { - include_path = grecs_list_create(); - include_path->free_entry = incl_free; + if (!grecs_usr_include_path) { + grecs_usr_include_path = grecs_list_create(); + grecs_usr_include_path->free_entry = incl_free; } - grecs_list_append(include_path, grecs_strdup(dir)); + grecs_list_append(grecs_usr_include_path, grecs_strdup(dir)); } static struct grecs_symtab *incl_sources; @@ -497,17 +534,17 @@ grecs_find_include_file(const char *name, int allow_cwd) fd.buflen = 0; fd.found = 0; - if (!include_path) + if (!grecs_usr_include_path) grecs_include_path_setup(NULL); if (allow_cwd) { - grecs_list_append(include_path, cwd); - pp_list_find(include_path, &fd); - grecs_list_remove_tail(include_path); + grecs_list_append(grecs_usr_include_path, cwd); + pp_list_find(grecs_usr_include_path, &fd); + grecs_list_remove_tail(grecs_usr_include_path); } else - pp_list_find(include_path, &fd); + pp_list_find(grecs_usr_include_path, &fd); if (!fd.found) { - pp_list_find(std_include_path, &fd); + pp_list_find(grecs_std_include_path, &fd); if (!fd.found) return NULL; } -- cgit v1.2.1