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 /src/preproc.c | |
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.
Diffstat (limited to 'src/preproc.c')
-rw-r--r-- | src/preproc.c | 81 |
1 files changed, 59 insertions, 22 deletions
diff --git a/src/preproc.c b/src/preproc.c index 2626705..f1d5227 100644 --- a/src/preproc.c +++ b/src/preproc.c @@ -226,27 +226,64 @@ ctx_lookup(struct stat *st) struct buffer_ctx *ctx; if (!context_stack) return NULL; for (ctx = context_stack->prev; ctx; ctx = ctx->prev) if (STAT_ID_EQ(*st, ctx->id)) break; return ctx; } 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; char *buf; size_t buflen; int found; }; static int pp_list_find(struct grecs_list *list, struct file_data *dptr) { struct grecs_list_entry *ep; @@ -266,44 +303,44 @@ pp_list_find(struct grecs_list *list, struct file_data *dptr) return dptr->found; } static void incl_free(void *data) { grecs_free(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])); } } void grecs_include_path_setup(const char *dir, ...) { const char *p; char **argv = NULL; size_t argc = 0; size_t argi = 0; va_list ap; @@ -322,29 +359,29 @@ grecs_include_path_setup(const char *dir, ...) if (!p) break; p = va_arg(ap, const char*); } grecs_include_path_setup_v(argv); grecs_free(argv); va_end(ap); } 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; /* Calculate the hash of a struct input_file_ident. */ static unsigned incl_hasher(void *data, unsigned long n_buckets) { const struct input_file_ident *id = data; return (id->i_node + id->device) % n_buckets; } @@ -488,35 +525,35 @@ pop_source() char * grecs_find_include_file(const char *name, int allow_cwd) { static char *cwd = "."; struct file_data fd; fd.name = name; fd.namelen = strlen(name); fd.buf = NULL; 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; } return fd.buf; } static int parse_include(const char *text, int once) { struct wordsplit ws; char *tmp = NULL; char *p = NULL; |