Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file commands.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libyang's yanglint tool commands |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 20 | */ |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 21 | |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 22 | #define _GNU_SOURCE |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | #include <errno.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/mman.h> |
| 29 | #include <fcntl.h> |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 30 | #include <assert.h> |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | #include <getopt.h> |
| 33 | |
Michal Vasko | 203b4e7 | 2015-06-30 15:25:15 +0200 | [diff] [blame] | 34 | #include "commands.h" |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 35 | #include "../../src/libyang.h" |
| 36 | #include "../../src/tree.h" |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 37 | #include "../../src/parser.h" |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 38 | |
Michal Vasko | 203b4e7 | 2015-06-30 15:25:15 +0200 | [diff] [blame] | 39 | COMMAND commands[]; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 40 | extern int done; |
| 41 | extern struct ly_ctx *ctx; |
| 42 | extern char *search_path; |
| 43 | |
| 44 | void |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 45 | cmd_add_help(void) |
| 46 | { |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame^] | 47 | printf("add <path-to-model> [<other-models> ...]\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
| 51 | cmd_print_help(void) |
| 52 | { |
Michal Vasko | adfcfa1 | 2015-08-12 14:33:44 +0200 | [diff] [blame] | 53 | printf("print [-f (yang | tree | info)] [-t <info-target-node>] [-o <output-file>] <model-name>(@<revision>)\n\n"); |
Michal Vasko | 7df7f14 | 2015-07-15 12:10:53 +0200 | [diff] [blame] | 54 | printf("\tinfo-target-node: <absolute-schema-node> | typedef/<typedef-name> |\n"); |
Michal Vasko | 035f523 | 2015-07-15 12:26:52 +0200 | [diff] [blame] | 55 | printf("\t | identity/<identity-name> | feature/<feature-name> |\n"); |
Michal Vasko | d04dbea | 2015-08-06 15:10:19 +0200 | [diff] [blame] | 56 | printf("\t | grouping/<grouping-name>(<absolute-schema-nodeid>) |\n"); |
Michal Vasko | 035f523 | 2015-07-15 12:26:52 +0200 | [diff] [blame] | 57 | printf("\t | type/<absolute-schema-node-leaf-or-leaflist>\n"); |
Michal Vasko | d04dbea | 2015-08-06 15:10:19 +0200 | [diff] [blame] | 58 | printf("\n"); |
| 59 | printf("\tabsolute-schema-nodeid: ( /(<import-prefix>:)<node-identifier> )+\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 62 | static void |
| 63 | cmd_parse_help(const char *name) |
| 64 | { |
| 65 | printf("%s [-f (xml | json)] [-o <output-file>] <%s-file-name>\n", name, name); |
| 66 | } |
| 67 | |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 68 | void |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 69 | cmd_data_help(void) |
| 70 | { |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 71 | cmd_parse_help("data"); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | cmd_config_help(void) |
| 76 | { |
| 77 | cmd_parse_help("config"); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | cmd_filter_help(void) |
| 82 | { |
| 83 | cmd_parse_help("filter"); |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 87 | cmd_list_help(void) |
| 88 | { |
| 89 | printf("list\n"); |
| 90 | } |
| 91 | |
| 92 | void |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 93 | cmd_feature_help(void) |
| 94 | { |
Michal Vasko | 4b48427 | 2015-08-12 14:41:25 +0200 | [diff] [blame] | 95 | printf("feature -(-p)rint | (-(-e)nable | -(-d)isable (* | <feature-name>)) <model-name>(@<revision>)\n"); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 99 | cmd_searchpath_help(void) |
| 100 | { |
Michal Vasko | 6572fb4 | 2015-07-07 09:55:05 +0200 | [diff] [blame] | 101 | printf("searchpath <model-dir-path>\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
| 105 | cmd_verb_help(void) |
| 106 | { |
Michal Vasko | b93d014 | 2015-08-11 16:08:25 +0200 | [diff] [blame] | 107 | printf("verb (error/0 | warning/1 | verbose/2 | debug/3)\n"); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int |
| 111 | cmd_add(const char *arg) |
| 112 | { |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame^] | 113 | int fd, path_len; |
| 114 | char *addr, *ptr, *path; |
| 115 | const char *arg_ptr; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 116 | struct lys_module *model; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 117 | struct stat sb; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 118 | LYS_INFORMAT format; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 119 | |
Michal Vasko | 203b4e7 | 2015-06-30 15:25:15 +0200 | [diff] [blame] | 120 | if (strlen(arg) < 5) { |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 121 | cmd_add_help(); |
| 122 | return 1; |
| 123 | } |
| 124 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame^] | 125 | arg_ptr = arg + strlen("add "); |
| 126 | while (arg_ptr[0] == ' ') { |
| 127 | ++arg_ptr; |
| 128 | } |
| 129 | if (strchr(arg_ptr, ' ')) { |
| 130 | path_len = strchr(arg_ptr, ' ') - arg_ptr; |
| 131 | } else { |
| 132 | path_len = strlen(arg_ptr); |
| 133 | } |
Michal Vasko | 203b4e7 | 2015-06-30 15:25:15 +0200 | [diff] [blame] | 134 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame^] | 135 | path = strndup(arg_ptr, path_len); |
| 136 | |
| 137 | while (path) { |
| 138 | if ((ptr = strrchr(path, '.')) != NULL) { |
| 139 | ++ptr; |
| 140 | if (!strcmp(ptr, "yin")) { |
| 141 | format = LYS_IN_YIN; |
| 142 | } else if (!strcmp(ptr, "yang")) { |
| 143 | format = LYS_IN_YANG; |
| 144 | } else { |
| 145 | fprintf(stderr, "Input file in an unknown format \"%s\".\n", ptr); |
| 146 | free(path); |
| 147 | return 1; |
| 148 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 149 | } else { |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame^] | 150 | fprintf(stdout, "Input file \"%.*s\" without extension, assuming YIN format.\n", path_len, arg_ptr); |
| 151 | format = LYS_IN_YIN; |
| 152 | } |
| 153 | |
| 154 | fd = open(path, O_RDONLY); |
| 155 | free(path); |
| 156 | if (fd == -1) { |
| 157 | fprintf(stderr, "Opening input file \"%.*s\" failed (%s).\n", path_len, arg_ptr, strerror(errno)); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 158 | return 1; |
| 159 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 160 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame^] | 161 | if (fstat(fd, &sb) == -1) { |
| 162 | fprintf(stderr, "Unable to get input file \"%.*s\" information (%s).\n", path_len, arg_ptr, strerror(errno)); |
| 163 | close(fd); |
| 164 | return 1; |
| 165 | } |
Radek Krejci | 4041a2d | 2015-07-02 09:16:42 +0200 | [diff] [blame] | 166 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame^] | 167 | if (!S_ISREG(sb.st_mode)) { |
| 168 | fprintf(stderr, "Input file \"%.*s\" not a file.\n", path_len, arg_ptr); |
| 169 | close(fd); |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 174 | |
| 175 | model = lys_parse(ctx, addr, format); |
| 176 | munmap(addr, sb.st_size); |
Radek Krejci | 4041a2d | 2015-07-02 09:16:42 +0200 | [diff] [blame] | 177 | close(fd); |
Radek Krejci | 4041a2d | 2015-07-02 09:16:42 +0200 | [diff] [blame] | 178 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame^] | 179 | if (!model) { |
| 180 | /* libyang printed the error messages */ |
| 181 | return 1; |
| 182 | } |
Radek Krejci | 4041a2d | 2015-07-02 09:16:42 +0200 | [diff] [blame] | 183 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame^] | 184 | /* next model */ |
| 185 | arg_ptr += path_len; |
| 186 | while (arg_ptr[0] == ' ') { |
| 187 | ++arg_ptr; |
| 188 | } |
| 189 | if (strchr(arg_ptr, ' ')) { |
| 190 | path_len = strchr(arg_ptr, ' ') - arg_ptr; |
| 191 | } else { |
| 192 | path_len = strlen(arg_ptr); |
| 193 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 194 | |
Michal Vasko | 370d910 | 2015-08-21 13:06:11 +0200 | [diff] [blame^] | 195 | if (path_len) { |
| 196 | path = strndup(arg_ptr, path_len); |
| 197 | } else { |
| 198 | path = NULL; |
| 199 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | int |
| 206 | cmd_print(const char *arg) |
| 207 | { |
Michal Vasko | dd3b8bc | 2015-07-07 11:42:28 +0200 | [diff] [blame] | 208 | int c, i, argc, option_index, ret = 1; |
Michal Vasko | adfcfa1 | 2015-08-12 14:33:44 +0200 | [diff] [blame] | 209 | char **argv = NULL, *ptr, *target_node = NULL, *model_name, *revision; |
| 210 | const char **names, *out_path = NULL; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 211 | struct lys_module *model, *parent_model; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 212 | LYS_OUTFORMAT format = LYS_OUT_TREE; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 213 | FILE *output = stdout; |
| 214 | static struct option long_options[] = { |
| 215 | {"help", no_argument, 0, 'h'}, |
| 216 | {"format", required_argument, 0, 'f'}, |
| 217 | {"output", required_argument, 0, 'o'}, |
Michal Vasko | dd3b8bc | 2015-07-07 11:42:28 +0200 | [diff] [blame] | 218 | {"target-node", required_argument, 0, 't'}, |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 219 | {NULL, 0, 0, 0} |
| 220 | }; |
| 221 | |
| 222 | argc = 1; |
| 223 | argv = malloc(2*sizeof *argv); |
| 224 | *argv = strdup(arg); |
| 225 | ptr = strtok(*argv, " "); |
| 226 | while ((ptr = strtok(NULL, " "))) { |
| 227 | argv = realloc(argv, (argc+2)*sizeof *argv); |
| 228 | argv[argc++] = ptr; |
| 229 | } |
| 230 | argv[argc] = NULL; |
| 231 | |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 232 | optind = 0; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 233 | while (1) { |
Michal Vasko | 1074ce9 | 2015-07-03 16:16:31 +0200 | [diff] [blame] | 234 | option_index = 0; |
Michal Vasko | dd3b8bc | 2015-07-07 11:42:28 +0200 | [diff] [blame] | 235 | c = getopt_long(argc, argv, "hf:o:t:", long_options, &option_index); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 236 | if (c == -1) { |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | switch (c) { |
| 241 | case 'h': |
| 242 | cmd_print_help(); |
| 243 | ret = 0; |
| 244 | goto cleanup; |
| 245 | case 'f': |
| 246 | if (!strcmp(optarg, "yang")) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 247 | format = LYS_OUT_YANG; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 248 | } else if (!strcmp(optarg, "tree")) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 249 | format = LYS_OUT_TREE; |
Michal Vasko | dd3b8bc | 2015-07-07 11:42:28 +0200 | [diff] [blame] | 250 | } else if (!strcmp(optarg, "info")) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 251 | format = LYS_OUT_INFO; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 252 | } else { |
| 253 | fprintf(stderr, "Unknown output format \"%s\".\n", optarg); |
| 254 | goto cleanup; |
| 255 | } |
| 256 | break; |
| 257 | case 'o': |
| 258 | if (out_path) { |
| 259 | fprintf(stderr, "Output specified twice.\n"); |
| 260 | goto cleanup; |
| 261 | } |
| 262 | out_path = optarg; |
| 263 | break; |
Michal Vasko | dd3b8bc | 2015-07-07 11:42:28 +0200 | [diff] [blame] | 264 | case 't': |
| 265 | target_node = optarg; |
| 266 | break; |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 267 | case '?': |
| 268 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 269 | goto cleanup; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* file name */ |
| 274 | if (optind == argc) { |
| 275 | fprintf(stderr, "Missing the model name.\n"); |
| 276 | goto cleanup; |
| 277 | } |
Michal Vasko | cf0a41c | 2015-07-07 11:43:10 +0200 | [diff] [blame] | 278 | |
Michal Vasko | adfcfa1 | 2015-08-12 14:33:44 +0200 | [diff] [blame] | 279 | /* model, revision */ |
| 280 | model_name = argv[optind]; |
| 281 | revision = NULL; |
| 282 | if (strchr(model_name, '@')) { |
| 283 | revision = strchr(model_name, '@'); |
| 284 | revision[0] = '\0'; |
| 285 | ++revision; |
| 286 | } |
| 287 | |
| 288 | model = ly_ctx_get_module(ctx, model_name, revision); |
Michal Vasko | cf0a41c | 2015-07-07 11:43:10 +0200 | [diff] [blame] | 289 | if (model == NULL) { |
| 290 | names = ly_ctx_get_module_names(ctx); |
| 291 | for (i = 0; names[i]; i++) { |
| 292 | if (!model) { |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 293 | parent_model = ly_ctx_get_module(ctx, names[i], NULL); |
Michal Vasko | adfcfa1 | 2015-08-12 14:33:44 +0200 | [diff] [blame] | 294 | model = (struct lys_module *)ly_ctx_get_submodule(parent_model, model_name, revision); |
Michal Vasko | cf0a41c | 2015-07-07 11:43:10 +0200 | [diff] [blame] | 295 | } |
Michal Vasko | cf0a41c | 2015-07-07 11:43:10 +0200 | [diff] [blame] | 296 | } |
| 297 | free(names); |
| 298 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 299 | |
| 300 | if (model == NULL) { |
Michal Vasko | adfcfa1 | 2015-08-12 14:33:44 +0200 | [diff] [blame] | 301 | if (revision) { |
| 302 | fprintf(stderr, "No model \"%s\" in revision %s found.\n", model_name, revision); |
| 303 | } else { |
| 304 | fprintf(stderr, "No model \"%s\" found.\n", model_name); |
| 305 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 306 | goto cleanup; |
| 307 | } |
| 308 | |
| 309 | if (out_path) { |
| 310 | output = fopen(out_path, "w"); |
| 311 | if (!output) { |
| 312 | fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno)); |
| 313 | goto cleanup; |
| 314 | } |
| 315 | } |
| 316 | |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 317 | ret = lys_print(output, model, format, target_node); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 318 | |
| 319 | cleanup: |
| 320 | free(*argv); |
| 321 | free(argv); |
| 322 | |
Radek Krejci | 94f0583 | 2015-06-19 09:58:53 +0200 | [diff] [blame] | 323 | if (output && (output != stdout)) { |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 324 | fclose(output); |
| 325 | } |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 330 | static int |
| 331 | cmd_parse(const char *arg, int options) |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 332 | { |
Michal Vasko | a3b45ca | 2015-07-14 16:01:20 +0200 | [diff] [blame] | 333 | int c, argc, option_index, ret = 1, fd = -1; |
| 334 | struct stat sb; |
| 335 | char **argv = NULL, *ptr, *addr; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 336 | const char *out_path = NULL; |
Radek Krejci | 0e1d1a6 | 2015-07-31 11:17:01 +0200 | [diff] [blame] | 337 | struct lyd_node *data = NULL, *next, *iter; |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 338 | LYD_FORMAT format = LYD_UNKNOWN; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 339 | FILE *output = stdout; |
| 340 | static struct option long_options[] = { |
| 341 | {"help", no_argument, 0, 'h'}, |
| 342 | {"format", required_argument, 0, 'f'}, |
| 343 | {"output", required_argument, 0, 'o'}, |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 344 | {"strict", no_argument, 0, 's'}, |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 345 | {NULL, 0, 0, 0} |
| 346 | }; |
| 347 | |
| 348 | argc = 1; |
| 349 | argv = malloc(2*sizeof *argv); |
| 350 | *argv = strdup(arg); |
| 351 | ptr = strtok(*argv, " "); |
| 352 | while ((ptr = strtok(NULL, " "))) { |
| 353 | argv = realloc(argv, (argc+2)*sizeof *argv); |
| 354 | argv[argc++] = ptr; |
| 355 | } |
| 356 | argv[argc] = NULL; |
| 357 | |
| 358 | optind = 0; |
| 359 | while (1) { |
| 360 | option_index = 0; |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 361 | c = getopt_long(argc, argv, "hf:o:xyz", long_options, &option_index); |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 362 | if (c == -1) { |
| 363 | break; |
| 364 | } |
| 365 | |
| 366 | switch (c) { |
| 367 | case 'h': |
| 368 | cmd_data_help(); |
| 369 | ret = 0; |
| 370 | goto cleanup; |
| 371 | case 'f': |
| 372 | if (!strcmp(optarg, "xml")) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 373 | format = LYD_XML; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 374 | } else if (!strcmp(optarg, "json")) { |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 375 | format = LYD_JSON; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 376 | } else { |
| 377 | fprintf(stderr, "Unknown output format \"%s\".\n", optarg); |
| 378 | goto cleanup; |
| 379 | } |
| 380 | break; |
| 381 | case 'o': |
| 382 | if (out_path) { |
| 383 | fprintf(stderr, "Output specified twice.\n"); |
| 384 | goto cleanup; |
| 385 | } |
| 386 | out_path = optarg; |
| 387 | break; |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 388 | case 's': |
| 389 | options |= LYD_OPT_STRICT; |
| 390 | break; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 391 | case '?': |
| 392 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 393 | goto cleanup; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /* file name */ |
| 398 | if (optind == argc) { |
| 399 | fprintf(stderr, "Missing the data file name.\n"); |
| 400 | goto cleanup; |
| 401 | } |
| 402 | |
Michal Vasko | a3b45ca | 2015-07-14 16:01:20 +0200 | [diff] [blame] | 403 | fd = open(argv[optind], O_RDONLY); |
| 404 | if (fd == -1) { |
Michal Vasko | 0c16d66 | 2015-07-13 15:54:37 +0200 | [diff] [blame] | 405 | fprintf(stderr, "The input file could not be opened (%s).\n", strerror(errno)); |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 406 | goto cleanup; |
| 407 | } |
| 408 | |
Michal Vasko | a3b45ca | 2015-07-14 16:01:20 +0200 | [diff] [blame] | 409 | if (fstat(fd, &sb) == -1) { |
| 410 | fprintf(stderr, "Unable to get input file information (%s).\n", strerror(errno)); |
| 411 | goto cleanup; |
| 412 | } |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 413 | |
Michal Vasko | a3b45ca | 2015-07-14 16:01:20 +0200 | [diff] [blame] | 414 | if (!S_ISREG(sb.st_mode)) { |
| 415 | fprintf(stderr, "Input file not a file.\n"); |
| 416 | goto cleanup; |
| 417 | } |
| 418 | |
| 419 | addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 420 | data = lyd_parse(ctx, addr, LYD_XML, options); |
Michal Vasko | a3b45ca | 2015-07-14 16:01:20 +0200 | [diff] [blame] | 421 | munmap(addr, sb.st_size); |
| 422 | |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 423 | if (data == NULL) { |
| 424 | fprintf(stderr, "Failed to parse data.\n"); |
| 425 | goto cleanup; |
| 426 | } |
| 427 | |
| 428 | if (out_path) { |
| 429 | output = fopen(out_path, "w"); |
| 430 | if (!output) { |
| 431 | fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno)); |
| 432 | goto cleanup; |
| 433 | } |
| 434 | |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 435 | if (format == LYD_UNKNOWN) { |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 436 | /* default */ |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 437 | format = LYD_XML; |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 441 | if (format != LYD_UNKNOWN) { |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 442 | lyd_print(output, data, format); |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | cleanup: |
| 446 | free(*argv); |
| 447 | free(argv); |
| 448 | |
| 449 | if (output && (output != stdout)) { |
| 450 | fclose(output); |
| 451 | } |
| 452 | |
Michal Vasko | a3b45ca | 2015-07-14 16:01:20 +0200 | [diff] [blame] | 453 | if (fd != -1) { |
| 454 | close(fd); |
| 455 | } |
| 456 | |
Radek Krejci | 0e1d1a6 | 2015-07-31 11:17:01 +0200 | [diff] [blame] | 457 | LY_TREE_FOR_SAFE(data, next, iter) { |
| 458 | lyd_free(iter); |
| 459 | } |
Michal Vasko | 520d473 | 2015-07-13 15:53:33 +0200 | [diff] [blame] | 460 | |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | int |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 465 | cmd_data(const char *arg) |
| 466 | { |
| 467 | return cmd_parse(arg, 0); |
| 468 | } |
| 469 | |
| 470 | int |
| 471 | cmd_config(const char *arg) |
| 472 | { |
| 473 | return cmd_parse(arg, LYD_OPT_EDIT); |
| 474 | } |
| 475 | |
| 476 | int |
| 477 | cmd_filter(const char *arg) |
| 478 | { |
| 479 | return cmd_parse(arg, LYD_OPT_FILTER); |
| 480 | } |
| 481 | |
| 482 | int |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 483 | cmd_list(const char *UNUSED(arg)) |
| 484 | { |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 485 | struct lyd_node *ylib, *module, *submodule, *node; |
| 486 | int has_modules = 0; |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 487 | |
Radek Krejci | 7ab2515 | 2015-08-07 14:48:45 +0200 | [diff] [blame] | 488 | ylib = ly_ctx_info(ctx); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 489 | |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 490 | LY_TREE_FOR(ylib->child, node) { |
| 491 | if (!strcmp(node->schema->name, "module-set-id")) { |
| 492 | printf("List of the loaded models (mod-set-id %s):\n", ((struct lyd_node_leaf *)node)->value_str); |
| 493 | break; |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 494 | } |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 495 | } |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 496 | assert(node); |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 497 | |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 498 | LY_TREE_FOR(ylib->child, module) { |
| 499 | if (!strcmp(module->schema->name, "module")) { |
| 500 | has_modules = 1; |
| 501 | |
| 502 | /* module print */ |
| 503 | LY_TREE_FOR(module->child, node) { |
| 504 | if (!strcmp(node->schema->name, "name")) { |
| 505 | printf("\t%s", ((struct lyd_node_leaf *)node)->value_str); |
| 506 | } else if (!strcmp(node->schema->name, "revision")) { |
| 507 | if (((struct lyd_node_leaf *)node)->value_str[0] == '\0') { |
| 508 | printf("\n"); |
| 509 | } else { |
| 510 | printf("@%s\n", ((struct lyd_node_leaf *)node)->value_str); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /* submodules print */ |
| 516 | LY_TREE_FOR(module->child, submodule) { |
| 517 | if (!strcmp(submodule->schema->name, "submodules")) { |
| 518 | LY_TREE_FOR(submodule->child, submodule) { |
| 519 | if (!strcmp(submodule->schema->name, "submodule")) { |
| 520 | LY_TREE_FOR(submodule->child, node) { |
| 521 | if (!strcmp(node->schema->name, "name")) { |
| 522 | printf("\t\t%s", ((struct lyd_node_leaf *)node)->value_str); |
| 523 | } else if (!strcmp(node->schema->name, "revision")) { |
| 524 | if (((struct lyd_node_leaf *)node)->value_str[0] == '\0') { |
| 525 | printf("\n"); |
| 526 | } else { |
| 527 | printf("@%s\n", ((struct lyd_node_leaf *)node)->value_str); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | break; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | if (!has_modules) { |
Michal Vasko | e0cb252 | 2015-07-01 10:24:53 +0200 | [diff] [blame] | 540 | printf("\t(none)\n"); |
| 541 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 542 | |
Michal Vasko | ea31f19 | 2015-08-03 15:21:56 +0200 | [diff] [blame] | 543 | lyd_free(ylib); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | int |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 548 | cmd_feature(const char *arg) |
| 549 | { |
| 550 | int c, i, argc, option_index, ret = 1, task = -1; |
Michal Vasko | b011e6b | 2015-08-06 09:57:46 +0200 | [diff] [blame] | 551 | unsigned int max_len; |
Michal Vasko | 4b48427 | 2015-08-12 14:41:25 +0200 | [diff] [blame] | 552 | char **argv = NULL, *ptr, *model_name, *revision; |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 553 | const char *feat_name = NULL, **names; |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 554 | uint8_t *states; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 555 | struct lys_module *model, *parent_model; |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 556 | static struct option long_options[] = { |
| 557 | {"help", no_argument, 0, 'h'}, |
| 558 | {"print", no_argument, 0, 'p'}, |
| 559 | {"enable", required_argument, 0, 'e'}, |
| 560 | {"disable", required_argument, 0, 'd'}, |
| 561 | {NULL, 0, 0, 0} |
| 562 | }; |
| 563 | |
| 564 | argc = 1; |
| 565 | argv = malloc(2*sizeof *argv); |
| 566 | *argv = strdup(arg); |
| 567 | ptr = strtok(*argv, " "); |
| 568 | while ((ptr = strtok(NULL, " "))) { |
| 569 | argv = realloc(argv, (argc+2)*sizeof *argv); |
| 570 | argv[argc++] = ptr; |
| 571 | } |
| 572 | argv[argc] = NULL; |
| 573 | |
| 574 | optind = 0; |
| 575 | while (1) { |
| 576 | option_index = 0; |
| 577 | c = getopt_long(argc, argv, "hpe:d:", long_options, &option_index); |
| 578 | if (c == -1) { |
| 579 | break; |
| 580 | } |
| 581 | |
| 582 | switch (c) { |
| 583 | case 'h': |
| 584 | cmd_feature_help(); |
| 585 | ret = 0; |
| 586 | goto cleanup; |
| 587 | case 'p': |
| 588 | if (task != -1) { |
| 589 | fprintf(stderr, "Only one of print, enable, or disable can be specified.\n"); |
| 590 | goto cleanup; |
| 591 | } |
| 592 | task = 0; |
| 593 | break; |
| 594 | case 'e': |
| 595 | if (task != -1) { |
| 596 | fprintf(stderr, "Only one of print, enable, or disable can be specified.\n"); |
| 597 | goto cleanup; |
| 598 | } |
| 599 | task = 1; |
| 600 | feat_name = optarg; |
| 601 | break; |
| 602 | case 'd': |
| 603 | if (task != -1) { |
| 604 | fprintf(stderr, "Only one of print, enable, or disable can be specified.\n"); |
| 605 | goto cleanup; |
| 606 | } |
| 607 | task = 2; |
| 608 | feat_name = optarg; |
| 609 | break; |
| 610 | case '?': |
| 611 | fprintf(stderr, "Unknown option \"%d\".\n", (char)c); |
| 612 | goto cleanup; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /* model name */ |
| 617 | if (optind == argc) { |
| 618 | fprintf(stderr, "Missing the model name.\n"); |
| 619 | goto cleanup; |
| 620 | } |
Michal Vasko | 4b48427 | 2015-08-12 14:41:25 +0200 | [diff] [blame] | 621 | |
| 622 | revision = NULL; |
| 623 | model_name = argv[optind]; |
| 624 | if (strchr(model_name, '@')) { |
| 625 | revision = strchr(model_name, '@'); |
| 626 | revision[0] = '\0'; |
| 627 | ++revision; |
| 628 | } |
| 629 | |
| 630 | model = ly_ctx_get_module(ctx, model_name, revision); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 631 | if (model == NULL) { |
| 632 | names = ly_ctx_get_module_names(ctx); |
| 633 | for (i = 0; names[i]; i++) { |
| 634 | if (!model) { |
Radek Krejci | 63a91a9 | 2015-07-29 13:31:04 +0200 | [diff] [blame] | 635 | parent_model = ly_ctx_get_module(ctx, names[i], NULL); |
Michal Vasko | 4b48427 | 2015-08-12 14:41:25 +0200 | [diff] [blame] | 636 | model = (struct lys_module *)ly_ctx_get_submodule(parent_model, model_name, revision); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 637 | } |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 638 | } |
| 639 | free(names); |
| 640 | } |
| 641 | if (model == NULL) { |
Michal Vasko | 4b48427 | 2015-08-12 14:41:25 +0200 | [diff] [blame] | 642 | if (revision) { |
| 643 | fprintf(stderr, "No model \"%s\" in revision %s found.\n", model_name, revision); |
| 644 | } else { |
| 645 | fprintf(stderr, "No model \"%s\" found.\n", model_name); |
| 646 | } |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 647 | goto cleanup; |
| 648 | } |
| 649 | |
| 650 | if (task == -1) { |
| 651 | fprintf(stderr, "One of print, enable, or disable must be specified.\n"); |
| 652 | goto cleanup; |
| 653 | } |
| 654 | |
| 655 | if (task == 0) { |
| 656 | printf("%s features:\n", model->name); |
| 657 | |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 658 | names = lys_features_list(model, &states); |
Michal Vasko | b011e6b | 2015-08-06 09:57:46 +0200 | [diff] [blame] | 659 | |
| 660 | /* get the max len */ |
| 661 | max_len = 0; |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 662 | for (i = 0; names[i]; ++i) { |
Michal Vasko | b011e6b | 2015-08-06 09:57:46 +0200 | [diff] [blame] | 663 | if (strlen(names[i]) > max_len) { |
| 664 | max_len = strlen(names[i]); |
| 665 | } |
| 666 | } |
| 667 | for (i = 0; names[i]; ++i) { |
| 668 | printf("\t%-*s (%s)\n", max_len, names[i], states[i] ? "on" : "off"); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 669 | } |
| 670 | free(names); |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 671 | free(states); |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 672 | if (!i) { |
| 673 | printf("\t(none)\n"); |
| 674 | } |
| 675 | } else if (task == 1) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 676 | if (lys_features_enable(model, feat_name)) { |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 677 | fprintf(stderr, "Feature \"%s\" not found.\n", feat_name); |
| 678 | ret = 1; |
| 679 | } |
| 680 | } else if (task == 2) { |
Radek Krejci | e98bb4b | 2015-07-30 14:21:41 +0200 | [diff] [blame] | 681 | if (lys_features_disable(model, feat_name)) { |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 682 | fprintf(stderr, "Feature \"%s\" not found.\n", feat_name); |
| 683 | ret = 1; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | cleanup: |
| 688 | free(*argv); |
| 689 | free(argv); |
| 690 | |
| 691 | return ret; |
| 692 | } |
| 693 | |
| 694 | int |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 695 | cmd_searchpath(const char *arg) |
| 696 | { |
| 697 | const char *path; |
| 698 | struct stat st; |
| 699 | |
| 700 | if (strchr(arg, ' ') == NULL) { |
| 701 | fprintf(stderr, "Missing the search path.\n"); |
| 702 | return 1; |
| 703 | } |
| 704 | path = strchr(arg, ' ')+1; |
| 705 | |
| 706 | if (!strcmp(path, "-h") || !strcmp(path, "--help")) { |
| 707 | cmd_searchpath_help(); |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | if (stat(path, &st) == -1) { |
| 712 | fprintf(stderr, "Failed to stat the search path (%s).\n", strerror(errno)); |
| 713 | return 1; |
| 714 | } |
| 715 | if (!S_ISDIR(st.st_mode)) { |
| 716 | fprintf(stderr, "\"%s\" is not a directory.\n", path); |
| 717 | return 1; |
| 718 | } |
| 719 | |
| 720 | free(search_path); |
| 721 | search_path = strdup(path); |
| 722 | |
Michal Vasko | 6572fb4 | 2015-07-07 09:55:05 +0200 | [diff] [blame] | 723 | ly_ctx_set_searchdir(ctx, search_path); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 724 | |
Michal Vasko | 6572fb4 | 2015-07-07 09:55:05 +0200 | [diff] [blame] | 725 | return 0; |
| 726 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 727 | |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 728 | int |
| 729 | cmd_clear(const char *UNUSED(arg)) |
| 730 | { |
| 731 | ly_ctx_destroy(ctx); |
| 732 | ctx = ly_ctx_new(search_path); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | int |
| 737 | cmd_verb(const char *arg) |
| 738 | { |
| 739 | const char *verb; |
Radek Krejci | 7408bf1 | 2015-07-20 18:15:51 +0200 | [diff] [blame] | 740 | if (strlen(arg) < 5) { |
| 741 | cmd_verb_help(); |
| 742 | return 1; |
| 743 | } |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 744 | |
| 745 | verb = arg + 5; |
Michal Vasko | b93d014 | 2015-08-11 16:08:25 +0200 | [diff] [blame] | 746 | if (!strcmp(verb, "error") || !strcmp(verb, "0")) { |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 747 | ly_verb(0); |
Michal Vasko | b93d014 | 2015-08-11 16:08:25 +0200 | [diff] [blame] | 748 | } else if (!strcmp(verb, "warning") || !strcmp(verb, "1")) { |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 749 | ly_verb(1); |
Michal Vasko | b93d014 | 2015-08-11 16:08:25 +0200 | [diff] [blame] | 750 | } else if (!strcmp(verb, "verbose") || !strcmp(verb, "2")) { |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 751 | ly_verb(2); |
Michal Vasko | b93d014 | 2015-08-11 16:08:25 +0200 | [diff] [blame] | 752 | } else if (!strcmp(verb, "debug") || !strcmp(verb, "3")) { |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 753 | ly_verb(3); |
| 754 | } else { |
Radek Krejci | 7408bf1 | 2015-07-20 18:15:51 +0200 | [diff] [blame] | 755 | fprintf(stderr, "Unknown verbosity \"%s\"\n", verb); |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 756 | return 1; |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | int |
| 763 | cmd_quit(const char *UNUSED(arg)) |
| 764 | { |
| 765 | done = 1; |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | int |
| 770 | cmd_help(const char *arg) |
| 771 | { |
| 772 | int i; |
| 773 | char *args = strdupa(arg); |
| 774 | char *cmd = NULL; |
| 775 | |
| 776 | strtok(args, " "); |
| 777 | if ((cmd = strtok(NULL, " ")) == NULL) { |
| 778 | |
| 779 | generic_help: |
| 780 | fprintf(stdout, "Available commands:\n"); |
| 781 | |
| 782 | for (i = 0; commands[i].name; i++) { |
| 783 | if (commands[i].helpstring != NULL) { |
| 784 | fprintf(stdout, " %-15s %s\n", commands[i].name, commands[i].helpstring); |
| 785 | } |
| 786 | } |
| 787 | } else { |
| 788 | /* print specific help for the selected command */ |
| 789 | |
| 790 | /* get the command of the specified name */ |
| 791 | for (i = 0; commands[i].name; i++) { |
| 792 | if (strcmp(cmd, commands[i].name) == 0) { |
| 793 | break; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | /* execute the command's help if any valid command specified */ |
| 798 | if (commands[i].name) { |
| 799 | if (commands[i].help_func != NULL) { |
| 800 | commands[i].help_func(); |
| 801 | } else { |
| 802 | printf("%s\n", commands[i].helpstring); |
| 803 | } |
| 804 | } else { |
| 805 | /* if unknown command specified, print the list of commands */ |
| 806 | printf("Unknown command \'%s\'\n", cmd); |
| 807 | goto generic_help; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | COMMAND commands[] = { |
| 815 | {"help", cmd_help, NULL, "Display commands description"}, |
| 816 | {"add", cmd_add, cmd_add_help, "Add a new model"}, |
| 817 | {"print", cmd_print, cmd_print_help, "Print model"}, |
Radek Krejci | b912095 | 2015-08-12 10:03:51 +0200 | [diff] [blame] | 818 | {"data", cmd_data, cmd_data_help, "Load, validate and optionally print complete datastore data"}, |
| 819 | {"config", cmd_config, cmd_config_help, "Load, validate and optionally print edit-config's data"}, |
| 820 | {"filter", cmd_filter, cmd_filter_help, "Load, validate and optionally print subtree filter data"}, |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 821 | {"list", cmd_list, cmd_list_help, "List all the loaded models"}, |
Michal Vasko | 50cfb78 | 2015-07-07 11:37:34 +0200 | [diff] [blame] | 822 | {"feature", cmd_feature, cmd_feature_help, "Print/enable/disable all/specific features of models"}, |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 823 | {"searchpath", cmd_searchpath, cmd_searchpath_help, "Set the search path for models"}, |
Michal Vasko | 6572fb4 | 2015-07-07 09:55:05 +0200 | [diff] [blame] | 824 | {"clear", cmd_clear, NULL, "Clear the context - remove all the loaded models"}, |
Michal Vasko | f3e59f1 | 2015-06-18 11:53:56 +0200 | [diff] [blame] | 825 | {"verb", cmd_verb, cmd_verb_help, "Change verbosity"}, |
| 826 | {"quit", cmd_quit, NULL, "Quit the program"}, |
| 827 | /* synonyms for previous commands */ |
| 828 | {"?", cmd_help, NULL, "Display commands description"}, |
| 829 | {"exit", cmd_quit, NULL, "Quit the program"}, |
| 830 | {NULL, NULL, NULL, NULL} |
| 831 | }; |