Skip to content

Commit dd4f6bc

Browse files
author
roman
committed
plugins exts UPDATE simplify ext plugin getter
1 parent 1615a6c commit dd4f6bc

16 files changed

+81
-60
lines changed

src/log.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ lyplg_ext_parse_log(const struct lysp_ctx *pctx, const struct lysp_ext_instance
885885
ly_vlog_build_path_line(PARSER_CTX(pctx), &data_path, &schema_path, &line);
886886

887887
va_start(ap, format);
888-
ly_ext_log(PARSER_CTX(pctx), ext->plugin->id, level, err, data_path, schema_path, line, format, ap);
888+
ly_ext_log(PARSER_CTX(pctx), lysc_get_ext_plugin(ext->plugin)->id, level, err, data_path, schema_path, line, format, ap);
889889
va_end(ap);
890890
}
891891

@@ -902,7 +902,7 @@ lyplg_ext_compile_log(const struct lysc_ctx *cctx, const struct lysc_ext_instanc
902902
}
903903

904904
va_start(ap, format);
905-
ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err, NULL, schema_path, 0, format, ap);
905+
ly_ext_log(ext->module->ctx, lysc_get_ext_plugin(ext->def->plugin)->id, level, err, NULL, schema_path, 0, format, ap);
906906
va_end(ap);
907907
}
908908

@@ -919,7 +919,7 @@ lyplg_ext_compile_log_path(const char *path, const struct lysc_ext_instance *ext
919919
}
920920

921921
va_start(ap, format);
922-
ly_ext_log(ext->module->ctx, ext->def->plugin->id, level, err, NULL, schema_path, 0, format, ap);
922+
ly_ext_log(ext->module->ctx, lysc_get_ext_plugin(ext->def->plugin)->id, level, err, NULL, schema_path, 0, format, ap);
923923
va_end(ap);
924924
}
925925

@@ -940,7 +940,8 @@ _lyplg_ext_compile_log_err(const struct ly_err_item *eitem, const struct lysc_ex
940940
}
941941

942942
va_start(ap, ext);
943-
ly_ext_log(ext->module->ctx, ext->def->plugin->id, eitem->level, eitem->err, data_path, schema_path, eitem->line, "%s", ap);
943+
ly_ext_log(ext->module->ctx, lysc_get_ext_plugin(ext->def->plugin)->id, eitem->level, eitem->err,
944+
data_path, schema_path, eitem->line, "%s", ap);
944945
va_end(ap);
945946
}
946947

src/plugins.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ lysc_get_type_plugin(uintptr_t plugin_id)
167167
return lysc_get_plugin(plugin_id, LYPLG_TYPE, &plugins_types);
168168
}
169169

170+
LIBYANG_API_DEF struct lyplg_ext *
171+
lysc_get_ext_plugin(uintptr_t plugin_id)
172+
{
173+
return lysc_get_plugin(plugin_id, LYPLG_EXTENSION, &plugins_extensions);
174+
}
175+
170176
/**
171177
* @brief Iterate over list of loaded plugins of the given @p type.
172178
*
@@ -255,40 +261,34 @@ lyplg_type_plugin_find(const struct ly_ctx *ctx, const char *module, const char
255261
}
256262
}
257263

258-
struct lyplg_type *
259-
lyplg_type_plugin_find(const struct ly_ctx *ctx, const char *module, const char *revision, const char *name)
264+
uintptr_t
265+
lyplg_ext_plugin_find(const struct ly_ctx *ctx, const char *module, const char *revision, const char *name)
260266
{
261-
struct lyplg_type_record *record = NULL;
267+
struct lyplg_ext_record *record = NULL;
268+
uint32_t record_idx = 0;
262269

263270
if (ctx) {
264271
/* try to find context specific plugin */
265-
record = lyplg_record_find(ctx, LYPLG_TYPE, module, revision, name, NULL);
272+
record = lyplg_record_find(ctx, LYPLG_EXTENSION, module, revision, name, &record_idx);
266273
}
267274

268275
if (!record) {
269276
/* try to find shared plugin */
270-
record = lyplg_record_find(NULL, LYPLG_TYPE, module, revision, name, NULL);
271-
}
272-
273-
return record ? &record->plugin : NULL;
274-
}
275-
276-
struct lyplg_ext *
277-
lyplg_ext_plugin_find(const struct ly_ctx *ctx, const char *module, const char *revision, const char *name)
278-
{
279-
struct lyplg_ext_record *record = NULL;
280-
281-
if (ctx) {
282-
/* try to find context specific plugin */
283-
record = lyplg_record_find(ctx, LYPLG_EXTENSION, module, revision, name, NULL);
277+
record = lyplg_record_find(NULL, LYPLG_EXTENSION, module, revision, name, &record_idx);
284278
}
285279

286280
if (!record) {
287-
/* try to find shared plugin */
288-
record = lyplg_record_find(NULL, LYPLG_EXTENSION, module, revision, name, NULL);
281+
/* not found */
282+
return 0;
289283
}
290284

291-
return record ? &record->plugin : NULL;
285+
if (!strncmp(record->plugin.id, "ly2 - ", 6)) {
286+
/* internal plugin, return an index with an offset of +1 in order to keep 0 as an invalid index (a NULL ptr) */
287+
return record_idx + 1;
288+
} else {
289+
/* external plugin, return the pointer */
290+
return (uintptr_t)&record->plugin;
291+
}
292292
}
293293

294294
/**

src/plugins_exts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ struct lysp_ext_instance {
405405
const char *argument; /**< optional value of the extension's argument */
406406
LY_VALUE_FORMAT format; /**< prefix format of the extension name/argument (::LY_VALUE_XML is YIN format) */
407407
void *prefix_data; /**< format-specific data for prefix resolution (see ly_resolve_prefix()) */
408-
struct lyplg_ext *plugin; /**< pointer to the extension plugin, if any */
408+
uintptr_t plugin; /**< extension plugin, use ::lysc_get_ext_plugin() */
409409

410410
void *parent; /**< pointer to the parent statement holding the extension instance(s), use
411411
::lysp_ext_instance#parent_stmt to access the value/structure */

src/plugins_exts/nacm.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ nacm_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
8181
{
8282
struct lysp_node *parent = NULL;
8383
LY_ARRAY_COUNT_TYPE u;
84+
struct lyplg_ext *ext_plugin, *parent_ext_plugin;
8485

8586
/* check that the extension is instantiated at an allowed place - data node */
8687
if (!(ext->parent_stmt & LY_STMT_NODE_MASK)) {
@@ -100,9 +101,12 @@ nacm_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
100101
return LY_ENOT;
101102
}
102103

104+
ext_plugin = lysc_get_ext_plugin(ext->plugin);
105+
103106
/* check for duplication */
104107
LY_ARRAY_FOR(parent->exts, u) {
105-
if ((&parent->exts[u] != ext) && parent->exts[u].plugin && !strcmp(parent->exts[u].plugin->id, ext->plugin->id)) {
108+
parent_ext_plugin = lysc_get_ext_plugin(parent->exts[u].plugin);
109+
if ((&parent->exts[u] != ext) && parent_ext_plugin && !strcmp(parent_ext_plugin->id, ext_plugin->id)) {
106110
/* duplication of a NACM extension on a single node
107111
* We check for all NACM plugins since we want to catch even the situation that there is default-deny-all
108112
* AND default-deny-write */

src/plugins_internal.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ uintptr_t lyplg_type_plugin_find(const struct ly_ctx *ctx, const char *module, c
8383
* @param[in] revision Revision of the module for which the plugin is implemented. NULL is not a wildcard, it matches
8484
* only the plugins with NULL revision specified.
8585
* @param[in] name Name of the extension which the plugin implements.
86-
* @return Found extension plugin, NULL if none found.
86+
* @return ID of the found extension plugin, 0 if none found. The extension plugin can be obtained
87+
* by passing the returned ID to ::lysc_get_ext_plugin().
8788
*/
88-
struct lyplg_ext *lyplg_ext_plugin_find(const struct ly_ctx *ctx, const char *module, const char *revision, const char *name);
89+
uintptr_t lyplg_ext_plugin_find(const struct ly_ctx *ctx, const char *module, const char *revision, const char *name);
8990

9091
#endif /* LY_PLUGINS_INTERNAL_H_ */

src/printer_context.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ static void
112112
ctxs_exts(const struct lysc_ext_instance *exts, struct ly_ht *ht, int *size)
113113
{
114114
LY_ARRAY_COUNT_TYPE u;
115+
struct lyplg_ext *ext;
115116

116117
/* sized array */
117118
*size += CTXS_SIZED_ARRAY(exts);
@@ -123,8 +124,9 @@ ctxs_exts(const struct lysc_ext_instance *exts, struct ly_ht *ht, int *size)
123124
*size += sizeof(LY_ARRAY_COUNT_TYPE) + LY_ARRAY_COUNT(exts[u].substmts) * sizeof *exts[u].substmts;
124125

125126
/* compiled, substmts storage */
126-
if (exts[u].def->plugin && exts[u].def->plugin->compiled_size) {
127-
*size += exts[u].def->plugin->compiled_size(&exts[u], ht);
127+
ext = lysc_get_ext_plugin(exts[u].def->plugin);
128+
if (ext && ext->compiled_size) {
129+
*size += ext->compiled_size(&exts[u], ht);
128130
}
129131
}
130132
}
@@ -814,8 +816,8 @@ ctxp_ext(const struct lysc_ext_instance *orig_ext, struct lysc_ext_instance *ext
814816
}
815817

816818
/* compiled, substmts storage, use the plugin */
817-
if (ext->def->plugin && ext->def->plugin->compiled_print) {
818-
ext->def->plugin->compiled_print(orig_ext, ext, addr_ht, ptr_set, mem);
819+
if (ext->def->plugin && lysc_get_ext_plugin(ext->def->plugin)->compiled_print) {
820+
lysc_get_ext_plugin(ext->def->plugin)->compiled_print(orig_ext, ext, addr_ht, ptr_set, mem);
819821
} else {
820822
ext->compiled = NULL;
821823
}

src/printer_tree.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ trp_ext_is_present(ly_bool lysc_tree, const void *node)
18611861
if (lysc_tree) {
18621862
cn = (const struct lysc_node *)node;
18631863
LY_ARRAY_FOR(cn->exts, i) {
1864-
if (!cn->exts[i].def->plugin || !cn->exts[i].def->plugin->printer_ctree) {
1864+
if (!cn->exts[i].def->plugin || !lysc_get_ext_plugin(cn->exts[i].def->plugin)->printer_ctree) {
18651865
continue;
18661866
}
18671867
if (!trp_ext_parent_is_valid(1, &cn->exts[i])) {
@@ -1873,7 +1873,7 @@ trp_ext_is_present(ly_bool lysc_tree, const void *node)
18731873
} else {
18741874
pn = (const struct lysp_node *)node;
18751875
LY_ARRAY_FOR(pn->exts, i) {
1876-
if (!pn->exts[i].plugin || !pn->exts[i].plugin->printer_ptree) {
1876+
if (!pn->exts[i].plugin || !lysc_get_ext_plugin(pn->exts[i].plugin)->printer_ptree) {
18771877
continue;
18781878
}
18791879
if (!trp_ext_parent_is_valid(0, &pn->exts[i])) {
@@ -2162,9 +2162,9 @@ tro_set_node_overr(ly_bool lysc_tree, const void *node, ly_bool erase_node_overr
21622162
}
21632163
no = &plc->node_overr;
21642164
if (!plc->ctx && lysc_tree && (ce = trp_ext_is_present(lysc_tree, node))) {
2165-
rc = ce->def->plugin->printer_ctree(ce, NULL, &no->flags, &no->add_opts);
2165+
rc = lysc_get_ext_plugin(ce->def->plugin)->printer_ctree(ce, NULL, &no->flags, &no->add_opts);
21662166
} else if (!plc->ctx && (pe = trp_ext_is_present(lysc_tree, node))) {
2167-
rc = pe->plugin->printer_ptree(pe, NULL, &no->flags, &no->add_opts);
2167+
rc = lysc_get_ext_plugin(pe->plugin)->printer_ptree(pe, NULL, &no->flags, &no->add_opts);
21682168
} else if (plc->ctx) {
21692169
if (plc->schema && plc->schema->compiled && plc->schema->cn_overr) {
21702170
rc = plc->schema->cn_overr(node, plc->ctx->plugin_priv, &plc->filtered, &no->flags, &no->add_opts);
@@ -3873,15 +3873,15 @@ tro_ext_printer_tree(ly_bool compiled, void *ext, const struct lyspr_tree_ctx *p
38733873

38743874
if (compiled) {
38753875
ext_comp = ext;
3876-
plugin = ext_comp->def->plugin;
3876+
plugin = lysc_get_ext_plugin(ext_comp->def->plugin);
38773877
if (!plugin->printer_ctree) {
38783878
*ignore = 1;
38793879
return LY_SUCCESS;
38803880
}
38813881
return plugin->printer_ctree(ext, plug_ctx, &flags, &add_opts);
38823882
} else {
38833883
ext_pars = ext;
3884-
plugin = ext_pars->plugin;
3884+
plugin = lysc_get_ext_plugin(ext_pars->plugin);
38853885
if (!plugin->printer_ptree) {
38863886
*ignore = 1;
38873887
return LY_SUCCESS;

src/printer_yang.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ yprc_extension_instances(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t
356356
{
357357
LY_ARRAY_COUNT_TYPE u;
358358
ly_bool inner_flag;
359+
struct lyplg_ext *ext;
359360

360361
LY_ARRAY_FOR(exts, u) {
361362
if ((exts[u].parent_stmt != substmt) || (exts[u].parent_stmt_index != substmt_index)) {
@@ -375,8 +376,9 @@ yprc_extension_instances(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t
375376
inner_flag = 0;
376377
yprc_extension_instances(pctx, LY_STMT_EXTENSION_INSTANCE, 0, exts[u].exts, &inner_flag);
377378

378-
if (exts[u].def->plugin && exts[u].def->plugin->printer_info) {
379-
exts[u].def->plugin->printer_info(&pctx->printer_ctx, &exts[u], &inner_flag);
379+
ext = lysc_get_ext_plugin(exts[u].def->plugin);
380+
if (ext && ext->printer_info) {
381+
ext->printer_info(&pctx->printer_ctx, &exts[u], &inner_flag);
380382
}
381383

382384
LEVEL--;

src/schema_compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *extp, struct lys
132132
COMPILE_EXTS_GOTO(ctx, extp->exts, ext->exts, ext, ret, cleanup);
133133

134134
/* compile this extension */
135-
if (ext->def->plugin && ext->def->plugin->compile) {
135+
if (ext->def->plugin && lysc_get_ext_plugin(ext->def->plugin)->compile) {
136136
if (ext->argument) {
137137
lysc_update_path(ctx, ext->module, ext->argument);
138138
}
139-
ret = ext->def->plugin->compile(ctx, extp, ext);
139+
ret = lysc_get_ext_plugin(ext->def->plugin)->compile(ctx, extp, ext);
140140
if (ret == LY_ENOT) {
141141
lysc_ext_instance_free(ctx->ctx, ext);
142142
}

src/schema_compile_amend.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,10 @@ lysp_ext_dup(const struct ly_ctx *ctx, const struct lysp_module *pmod, void *par
485485
ext->flags = orig_ext->flags;
486486

487487
LY_CHECK_GOTO(ret = lysp_ext_children_dup(ctx, orig_ext->child, &ext->child), cleanup);
488-
if (ext->plugin && ext->plugin->parse) {
488+
if (ext->plugin && lysc_get_ext_plugin(ext->plugin)->parse) {
489489
/* parse again */
490490
LY_CHECK_GOTO(ret = ly_set_add(&pmods, pmod, 1, NULL), cleanup);
491-
LY_CHECK_GOTO(ret = ext->plugin->parse(&pctx, ext), cleanup);
491+
LY_CHECK_GOTO(ret = lysc_get_ext_plugin(ext->plugin)->parse(&pctx, ext), cleanup);
492492
}
493493

494494
cleanup:

0 commit comments

Comments
 (0)