Skip to content

Commit 0f5088a

Browse files
committed
yanglint REFACTOR minor refactoring
1 parent a3c08f4 commit 0f5088a

File tree

4 files changed

+64
-71
lines changed

4 files changed

+64
-71
lines changed

tools/lint/cmd_data.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,8 @@ find_extension(struct ly_ctx *ctx, struct yl_opt *yo)
580580
* @return LY_ERR value.
581581
*/
582582
static LY_ERR
583-
parse_input_by_type(struct ly_ctx *ctx, enum lyd_type type, struct cmdline_file *input_f,
584-
uint32_t parse_options, uint32_t validate_options, struct lyd_node **tree,
585-
struct lyd_node **op, struct cmdline_file *reply_rpc)
583+
parse_input_by_type(struct ly_ctx *ctx, enum lyd_type type, struct cmdline_file *input_f, uint32_t parse_options,
584+
uint32_t validate_options, struct lyd_node **tree, struct lyd_node **op, struct cmdline_file *reply_rpc)
586585
{
587586

588587
LY_ERR ret = LY_SUCCESS;
@@ -647,9 +646,9 @@ parse_input_by_type(struct ly_ctx *ctx, enum lyd_type type, struct cmdline_file
647646
* @return LY_ERR value.
648647
*/
649648
static LY_ERR
650-
parse_extension_instance(struct ly_ctx *ctx, struct yl_opt *yo, struct cmdline_file *input_f, struct lyd_node **tree, struct lyd_node *oper_tree)
649+
parse_extension_instance(struct ly_ctx *ctx, struct yl_opt *yo, struct cmdline_file *input_f, struct lyd_node **tree,
650+
struct lyd_node *oper_tree)
651651
{
652-
653652
LY_ERR ret = LY_SUCCESS;
654653

655654
if (find_extension(ctx, yo)) {
@@ -672,7 +671,7 @@ parse_extension_instance(struct ly_ctx *ctx, struct yl_opt *yo, struct cmdline_f
672671
return LY_EDENIED;
673672
}
674673

675-
/* Operational data is present */
674+
/* operational data are present */
676675
if (oper_tree) {
677676
lyd_insert_sibling(*tree, oper_tree, &oper_tree);
678677
ret = lyd_validate_all(tree, ctx, yo->data_validate_options, NULL);
@@ -692,6 +691,7 @@ cmd_data_process(struct ly_ctx *ctx, struct yl_opt *yo)
692691
struct lyd_node *tree = NULL, *op = NULL, *merged_tree = NULL, *oper_tree = NULL;
693692
const char *xpath;
694693
struct ly_set *set = NULL;
694+
uint32_t u;
695695

696696
/* additional operational datastore */
697697
if (yo->data_operational.in) {
@@ -702,13 +702,14 @@ cmd_data_process(struct ly_ctx *ctx, struct yl_opt *yo)
702702
}
703703
}
704704

705-
for (uint32_t u = 0; u < yo->data_inputs.count; ++u) {
705+
for (u = 0; u < yo->data_inputs.count; ++u) {
706706
struct cmdline_file *input_f = (struct cmdline_file *)yo->data_inputs.objs[u];
707707

708708
if (yo->data_ext) {
709709
ret = parse_extension_instance(ctx, yo, input_f, &tree, oper_tree);
710710
} else {
711-
ret = parse_input_by_type(ctx, yo->data_type, input_f, yo->data_parse_options, yo->data_validate_options, &tree, &op, &yo->reply_rpc);
711+
ret = parse_input_by_type(ctx, yo->data_type, input_f, yo->data_parse_options, yo->data_validate_options,
712+
&tree, &op, &yo->reply_rpc);
712713
}
713714

714715
if (ret) {
@@ -802,15 +803,17 @@ cmd_data_process(struct ly_ctx *ctx, struct yl_opt *yo)
802803
lyd_print_all(yo->out, merged_tree, yo->data_out_format, yo->data_print_options);
803804
}
804805

805-
for (uint32_t u = 0; u < yo->data_xpath.count; ++u) {
806+
for (u = 0; u < yo->data_xpath.count; ++u) {
806807
xpath = (const char *)yo->data_xpath.objs[u];
808+
807809
ly_set_free(set, NULL);
808810
ret = lys_find_xpath(ctx, NULL, xpath, LYS_FIND_NO_MATCH_ERROR, &set);
809811
if (ret || !set->count) {
810812
ret = (ret == LY_SUCCESS) ? LY_EINVAL : ret;
811-
YLMSG_E("The requested xpath failed.");
813+
YLMSG_E("XPath check failed.");
812814
goto cleanup;
813815
}
816+
814817
if (evaluate_xpath(merged_tree, xpath)) {
815818
goto cleanup;
816819
}
@@ -822,9 +825,5 @@ cmd_data_process(struct ly_ctx *ctx, struct yl_opt *yo)
822825
lyd_free_all(merged_tree);
823826
lyd_free_all(oper_tree);
824827
ly_set_free(set, NULL);
825-
if (ret) {
826-
return 1;
827-
} else {
828-
return 0;
829-
}
828+
return ret ? 1 : 0;
830829
}

tools/lint/common.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,13 @@ get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_
102102
return 0;
103103
}
104104

105-
LYS_INFORMAT
105+
/**
106+
* @brief Get schema format of the @p filename's content according to the @p filename's suffix.
107+
*
108+
* @param[in] filename Name of the file to examine.
109+
* @return Detected schema input format.
110+
*/
111+
static LYS_INFORMAT
106112
get_schema_format(const char *filename)
107113
{
108114
char *ptr;
@@ -121,7 +127,13 @@ get_schema_format(const char *filename)
121127
}
122128
}
123129

124-
LYD_FORMAT
130+
/**
131+
* @brief Get data format of the @p filename's content according to the @p filename's suffix.
132+
*
133+
* @param[in] filename Name of the file to examine.
134+
* @return Detected data input format.
135+
*/
136+
static LYD_FORMAT
125137
get_data_format(const char *filename)
126138
{
127139
char *ptr;

tools/lint/common.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,6 @@ int parse_schema_path(const char *path, char **dir, char **module);
9696
*/
9797
int get_input(const char *filepath, LYS_INFORMAT *format_schema, LYD_FORMAT *format_data, struct ly_in **in);
9898

99-
/**
100-
* @brief Get schema format of the @p filename's content according to the @p filename's suffix.
101-
*
102-
* @param[in] filename Name of the file to examine.
103-
* @return Detected schema input format.
104-
*/
105-
LYS_INFORMAT get_schema_format(const char *filename);
106-
107-
/**
108-
* @brief Get data format of the @p filename's content according to the @p filename's suffix.
109-
*
110-
* @param[in] filename Name of the file to examine.
111-
* @return Detected data input format.
112-
*/
113-
LYD_FORMAT get_data_format(const char *filename);
114-
11599
/**
116100
* @brief Get format of the @p filename's content according to the @p filename's suffix.
117101
*

tools/lint/main_ni.c

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -282,20 +282,6 @@ libyang_verbclb(LY_LOG_LEVEL level, const char *msg, const char *data_path, cons
282282
fprintf(stderr, "libyang %s Memory allocation failed.\n", levstr);
283283
}
284284

285-
static struct yl_schema_features *
286-
get_features_not_applied(const struct ly_set *fset)
287-
{
288-
for (uint32_t u = 0; u < fset->count; ++u) {
289-
struct yl_schema_features *sf = fset->objs[u];
290-
291-
if (!sf->applied) {
292-
return sf;
293-
}
294-
}
295-
296-
return NULL;
297-
}
298-
299285
/**
300286
* @brief Create the libyang context.
301287
*
@@ -331,6 +317,22 @@ create_ly_context(const char *yang_lib_file, const char *searchpaths, struct ly_
331317
return 0;
332318
}
333319

320+
static struct yl_schema_features *
321+
get_features_not_applied(const struct ly_set *fset)
322+
{
323+
struct yl_schema_features *sf;
324+
uint32_t u;
325+
326+
for (u = 0; u < fset->count; ++u) {
327+
sf = fset->objs[u];
328+
if (!sf->applied) {
329+
return sf;
330+
}
331+
}
332+
333+
return NULL;
334+
}
335+
334336
/**
335337
* @brief Implement module if some feature has not been applied.
336338
*
@@ -339,7 +341,7 @@ create_ly_context(const char *yang_lib_file, const char *searchpaths, struct ly_
339341
* @return 0 on success.
340342
*/
341343
static int
342-
apply_features(struct ly_set *schema_features, struct ly_ctx *ctx)
344+
apply_features(const struct ly_set *schema_features, struct ly_ctx *ctx)
343345
{
344346
struct yl_schema_features *sf;
345347
struct lys_module *mod;
@@ -374,19 +376,19 @@ apply_features(struct ly_set *schema_features, struct ly_ctx *ctx)
374376
* @param[in] argv Strings from command line.
375377
* @param[in] optind Index to the first input file in @p argv.
376378
* @param[in] data_in_format Specified input data format.
377-
* @param[in,out] ctx Context for libyang.
379+
* @param[in,out] ctx Context for libyang, is updated.
378380
* @param[in,out] yo Options for yanglint.
379381
* @return 0 on success.
380382
*/
381383
static int
382-
fill_context_inputs(int argc, char *argv[], int optind, LYD_FORMAT data_in_format, struct ly_ctx *ctx,
383-
struct yl_opt *yo)
384+
process_files(int argc, char *argv[], int optind, LYD_FORMAT data_in_format, struct ly_ctx *ctx, struct yl_opt *yo)
384385
{
385386
char *filepath = NULL;
386387
LYS_INFORMAT format_schema;
387388
LYD_FORMAT format_data;
389+
int i;
388390

389-
for (int i = 0; i < argc - optind; i++) {
391+
for (i = 0; i < argc - optind; i++) {
390392
format_schema = LYS_IN_UNKNOWN;
391393
format_data = data_in_format;
392394

@@ -410,7 +412,7 @@ fill_context_inputs(int argc, char *argv[], int optind, LYD_FORMAT data_in_forma
410412
}
411413
}
412414

413-
/* Check that all specified features were applied, apply now if possible. */
415+
/* check that all specified features were applied, apply now if possible */
414416
if (apply_features(&yo->schema_features, ctx)) {
415417
return -1;
416418
}
@@ -456,12 +458,16 @@ set_debug_groups(char *groups, struct yl_opt *yo)
456458
/**
457459
* @brief Process command line options and store the settings into the context.
458460
*
459-
* return -1 in case of error;
460-
* return 0 in case of success and ready to process
461-
* return 1 in case of success, but expect to exit.
461+
* @param[in] argc Argument count.
462+
* @param[in] argv Argument array.
463+
* @param[out] yo Yanglint options to fill.
464+
* @param[out] ctx Created context.
465+
* @return -1 in case of error;
466+
* @return 0 in case of success and ready to process
467+
* @return 1 in case of success, but expect to exit.
462468
*/
463469
static int
464-
fill_context(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx)
470+
process_args(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx)
465471
{
466472
int opt, opt_index;
467473
struct option options[] = {
@@ -495,7 +501,7 @@ fill_context(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx)
495501
{"extended-leafref", no_argument, NULL, 'X'},
496502
{"json-null", no_argument, NULL, 'J'},
497503
{"debug", required_argument, NULL, 'G'},
498-
{NULL, 0, NULL, 0}
504+
{NULL, 0, NULL, 0}
499505
};
500506
uint8_t data_type_set = 0;
501507

@@ -720,18 +726,18 @@ fill_context(int argc, char *argv[], struct yl_opt *yo, struct ly_ctx **ctx)
720726
return -1;
721727
}
722728

723-
/* Create the libyang context. */
729+
/* create the libyang context */
724730
if (create_ly_context(yo->yang_lib_file, yo->searchpaths, &yo->schema_features, &yo->ctx_options, ctx)) {
725731
return -1;
726732
}
727733

728-
/* Set callback providing run-time extension instance data. */
734+
/* set callback providing run-time extension instance data */
729735
if (yo->schema_context_filename) {
730736
ly_ctx_set_ext_data_clb(*ctx, ext_data_clb, yo->schema_context_filename);
731737
}
732738

733-
/* Schema modules and data files are just checked and prepared into internal structures for further processing. */
734-
if (fill_context_inputs(argc, argv, optind, yo->data_in_format, *ctx, yo)) {
739+
/* schema modules are parsed, data files are just checked and prepared into internal structures for further processing */
740+
if (process_files(argc, argv, optind, yo->data_in_format, *ctx, yo)) {
735741
return -1;
736742
}
737743

@@ -758,23 +764,19 @@ main_ni(int argc, char *argv[])
758764
int ret = EXIT_SUCCESS, r;
759765
struct yl_opt yo = {0};
760766
struct ly_ctx *ctx = NULL;
761-
char *features_output = NULL;
762-
struct ly_set set = {0};
763767
uint32_t u;
764768

765769
/* set callback for printing libyang messages */
766770
ly_set_log_clb(libyang_verbclb);
767771

768-
r = fill_context(argc, argv, &yo, &ctx);
772+
r = process_args(argc, argv, &yo, &ctx);
769773
if (r < 0) {
770774
ret = EXIT_FAILURE;
771775
}
772776
if (r) {
773777
goto cleanup;
774778
}
775779

776-
/* do the required job - parse, validate, print */
777-
778780
if (yo.list) {
779781
/* print the list of schemas */
780782
ret = cmd_list_exec(&ctx, &yo, NULL);
@@ -812,11 +814,7 @@ main_ni(int argc, char *argv[])
812814
}
813815

814816
cleanup:
815-
/* cleanup */
816817
yl_opt_erase(&yo);
817818
ly_ctx_destroy(ctx);
818-
free(features_output);
819-
ly_set_erase(&set, NULL);
820-
821819
return ret;
822820
}

0 commit comments

Comments
 (0)