From 813496dd02e73d00ca9936397c7a9bed5bcd61a3 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 7 Mar 2019 15:29:14 +0000 Subject: [PATCH] [Minor] Allow to set include trace function --- contrib/libucl/ucl.h | 35 ++++++++++++++++++++++++++++++++++- contrib/libucl/ucl_internal.h | 4 +++- contrib/libucl/ucl_util.c | 26 ++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/contrib/libucl/ucl.h b/contrib/libucl/ucl.h index 852a77cf1..f7f8354c5 100644 --- a/contrib/libucl/ucl.h +++ b/contrib/libucl/ucl.h @@ -1279,6 +1279,13 @@ UCL_EXTERN bool ucl_parser_pubkey_add (struct ucl_parser *parser, UCL_EXTERN bool ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename, bool need_expand); +/** + * Returns current file for the parser + * @param parser parser object + * @return current file or NULL if parsing memory + */ +UCL_EXTERN const char *ucl_parser_get_cur_file (struct ucl_parser *parser); + /** * Defines special handler for certain types of data (identified by magic) */ @@ -1300,13 +1307,39 @@ struct ucl_parser_special_handler { }; /** - * Add special handler for a parser + * Add special handler for a parser, handles special sequences identified by magic * @param parser parser structure * @param handler handler structure */ UCL_EXTERN void ucl_parser_add_special_handler (struct ucl_parser *parser, struct ucl_parser_special_handler *handler); +/** + * Handler for include traces: + * @param parser parser object + * @param parent where include is done from + * @param args arguments to an include + * @param path path of the include + * @param pathlen length of the path + * @param user_data opaque userdata + */ +typedef void (ucl_include_trace_func_t) (struct ucl_parser *parser, + const ucl_object_t *parent, + const ucl_object_t *args, + const char *path, + size_t pathlen, + void *user_data); + +/** + * Register trace function for an include handler + * @param parser parser object + * @param func function to trace includes + * @param user_data opaque data + */ +UCL_EXTERN void ucl_parser_set_include_tracer (struct ucl_parser *parser, + ucl_include_trace_func_t func, + void *user_data); + /** @} */ /** diff --git a/contrib/libucl/ucl_internal.h b/contrib/libucl/ucl_internal.h index 8c16dce8b..ed4a7d767 100644 --- a/contrib/libucl/ucl_internal.h +++ b/contrib/libucl/ucl_internal.h @@ -255,7 +255,9 @@ struct ucl_parser { struct ucl_stack *stack; struct ucl_chunk *chunks; struct ucl_pubkey *keys; - struct ucl_parser_special_handler *special_handlers; + struct ucl_parser_special_handler *special_handlers; + ucl_include_trace_func_t *include_trace_func; + void *include_trace_ud; struct ucl_variable *variables; ucl_variable_handler var_handler; void *var_data; diff --git a/contrib/libucl/ucl_util.c b/contrib/libucl/ucl_util.c index 051ac2c27..fdd0862cc 100644 --- a/contrib/libucl/ucl_util.c +++ b/contrib/libucl/ucl_util.c @@ -1490,6 +1490,17 @@ ucl_include_common (const unsigned char *data, size_t len, params.strat = UCL_DUPLICATE_APPEND; params.must_exist = !default_try; + if (parser->include_trace_func) { + const ucl_object_t *parent = NULL; + + if (parser->stack) { + parent = parser->stack->obj; + } + + parser->include_trace_func (parser, parent, args, + data, len, parser->include_trace_ud); + } + /* Process arguments */ if (args != NULL && args->type == UCL_OBJECT) { while ((param = ucl_object_iterate (args, &it, true)) != NULL) { @@ -3840,3 +3851,18 @@ ucl_comments_add (ucl_object_t *comments, const ucl_object_t *obj, (const char *)&obj, sizeof (void *), true); } } + +void +ucl_parser_set_include_tracer (struct ucl_parser *parser, + ucl_include_trace_func_t func, + void *user_data) +{ + parser->include_trace_func = func; + parser->include_trace_ud = user_data; +} + +const char * +ucl_parser_get_cur_file (struct ucl_parser *parser) +{ + return parser->cur_file; +} \ No newline at end of file -- 2.39.5