1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/**
* @file yl_opt.c
* @author Adam Piecek <piecek@cesnet.cz>
* @brief Settings options for the libyang context.
*
* Copyright (c) 2020 - 2023 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <strings.h>
#include "in.h" /* ly_in_free */
#include "common.h"
#include "yl_opt.h"
#include "yl_schema_features.h"
struct cmdline_file *
fill_cmdline_file(struct ly_set *set, struct ly_in *in, const char *path, LYD_FORMAT format)
{
struct cmdline_file *rec;
rec = malloc(sizeof *rec);
if (!rec) {
YLMSG_E("Allocating memory for data file information failed.");
return NULL;
}
rec->in = in;
rec->path = path;
rec->format = format;
if (set && ly_set_add(set, rec, 1, NULL)) {
free(rec);
YLMSG_E("Storing data file information failed.");
return NULL;
}
return rec;
}
void
free_cmdline_file_items(struct cmdline_file *rec)
{
if (rec && rec->in) {
ly_in_free(rec->in, 1);
}
}
void
free_cmdline_file(void *cmdline_file)
{
struct cmdline_file *rec = (struct cmdline_file *)cmdline_file;
if (rec) {
free_cmdline_file_items(rec);
free(rec);
}
}
void
yl_opt_erase(struct yl_opt *yo)
{
ly_bool interactive;
interactive = yo->interactive;
/* data */
ly_set_erase(&yo->data_inputs, free_cmdline_file);
ly_in_free(yo->data_operational.in, 1);
ly_set_erase(&yo->data_xpath, NULL);
/* schema */
ly_set_erase(&yo->schema_features, yl_schema_features_free);
ly_set_erase(&yo->schema_modules, NULL);
/* context */
free(yo->searchpaths);
/* --reply-rpc */
ly_in_free(yo->reply_rpc.in, 1);
ly_out_free(yo->out, NULL, yo->out_stdout ? 0 : 1);
free_cmdline(yo->argv);
*yo = (const struct yl_opt) {
0
};
yo->interactive = interactive;
}
int
yl_opt_update_schema_out_format(const char *arg, struct yl_opt *yo)
{
if (!strcasecmp(arg, "yang")) {
yo->schema_out_format = LYS_OUT_YANG;
yo->data_out_format = 0;
} else if (!strcasecmp(arg, "yin")) {
yo->schema_out_format = LYS_OUT_YIN;
yo->data_out_format = 0;
} else if (!strcasecmp(arg, "info")) {
yo->schema_out_format = LYS_OUT_YANG_COMPILED;
yo->data_out_format = 0;
} else if (!strcasecmp(arg, "tree")) {
yo->schema_out_format = LYS_OUT_TREE;
yo->data_out_format = 0;
} else {
return 1;
}
return 0;
}
int
yl_opt_update_data_out_format(const char *arg, struct yl_opt *yo)
{
if (!strcasecmp(arg, "xml")) {
yo->schema_out_format = 0;
yo->data_out_format = LYD_XML;
} else if (!strcasecmp(arg, "json")) {
yo->schema_out_format = 0;
yo->data_out_format = LYD_JSON;
} else if (!strcasecmp(arg, "lyb")) {
yo->schema_out_format = 0;
yo->data_out_format = LYD_LYB;
} else {
return 1;
}
return 0;
}
static int
yl_opt_update_other_out_format(const char *arg, struct yl_opt *yo)
{
if (!strcasecmp(arg, "feature-param")) {
yo->feature_param_format = 1;
} else {
return 1;
}
return 0;
}
int
yl_opt_update_out_format(const char *arg, struct yl_opt *yo)
{
if (!yl_opt_update_schema_out_format(arg, yo)) {
return 0;
}
if (!yl_opt_update_data_out_format(arg, yo)) {
return 0;
}
if (!yl_opt_update_other_out_format(arg, yo)) {
return 0;
}
YLMSG_E("Unknown output format %s.", arg);
return 1;
}
int
yl_opt_update_data_type(const char *arg, struct yl_opt *yo)
{
if (!strcasecmp(arg, "config")) {
yo->data_parse_options |= LYD_PARSE_NO_STATE;
yo->data_validate_options |= LYD_VALIDATE_NO_STATE;
} else if (!strcasecmp(arg, "get")) {
yo->data_parse_options |= LYD_PARSE_ONLY;
} else if (!strcasecmp(arg, "getconfig") || !strcasecmp(arg, "get-config") || !strcasecmp(arg, "edit")) {
yo->data_parse_options |= LYD_PARSE_ONLY | LYD_PARSE_NO_STATE;
} else if (!strcasecmp(arg, "rpc") || !strcasecmp(arg, "action")) {
yo->data_type = LYD_TYPE_RPC_YANG;
} else if (!strcasecmp(arg, "nc-rpc")) {
yo->data_type = LYD_TYPE_RPC_NETCONF;
} else if (!strcasecmp(arg, "reply") || !strcasecmp(arg, "rpcreply")) {
yo->data_type = LYD_TYPE_REPLY_YANG;
} else if (!strcasecmp(arg, "nc-reply")) {
yo->data_type = LYD_TYPE_REPLY_NETCONF;
} else if (!strcasecmp(arg, "notif") || !strcasecmp(arg, "notification")) {
yo->data_type = LYD_TYPE_NOTIF_YANG;
} else if (!strcasecmp(arg, "nc-notif")) {
yo->data_type = LYD_TYPE_NOTIF_NETCONF;
} else if (!strcasecmp(arg, "data")) {
/* default option */
} else {
return 1;
}
return 0;
}
int
yo_opt_update_data_default(const char *arg, struct yl_opt *yo)
{
if (!strcasecmp(arg, "all")) {
yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL;
} else if (!strcasecmp(arg, "all-tagged")) {
yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_ALL_TAG;
} else if (!strcasecmp(arg, "trim")) {
yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_TRIM;
} else if (!strcasecmp(arg, "implicit-tagged")) {
yo->data_print_options = (yo->data_print_options & ~LYD_PRINT_WD_MASK) | LYD_PRINT_WD_IMPL_TAG;
} else {
return 1;
}
return 0;
}
int
yo_opt_update_data_in_format(const char *arg, struct yl_opt *yo)
{
if (!strcasecmp(arg, "xml")) {
yo->data_in_format = LYD_XML;
} else if (!strcasecmp(arg, "json")) {
yo->data_in_format = LYD_JSON;
} else if (!strcasecmp(arg, "lyb")) {
yo->data_in_format = LYD_LYB;
} else {
return 1;
}
return 0;
}
void
yo_opt_update_make_implemented(struct yl_opt *yo)
{
if (yo->ctx_options & LY_CTX_REF_IMPLEMENTED) {
yo->ctx_options &= ~LY_CTX_REF_IMPLEMENTED;
yo->ctx_options |= LY_CTX_ALL_IMPLEMENTED;
} else {
yo->ctx_options |= LY_CTX_REF_IMPLEMENTED;
}
}
void
yo_opt_update_disable_searchdir(struct yl_opt *yo)
{
if (yo->ctx_options & LY_CTX_DISABLE_SEARCHDIR_CWD) {
yo->ctx_options &= ~LY_CTX_DISABLE_SEARCHDIR_CWD;
yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIRS;
} else {
yo->ctx_options |= LY_CTX_DISABLE_SEARCHDIR_CWD;
}
}
void
free_cmdline(char *argv[])
{
if (argv) {
free(argv[0]);
free(argv);
}
}
int
parse_cmdline(const char *cmdline, int *argc_p, char **argv_p[])
{
int count;
char **vector;
char *ptr;
char qmark = 0;
assert(cmdline);
assert(argc_p);
assert(argv_p);
/* init */
optind = 0; /* reinitialize getopt() */
count = 1;
vector = malloc((count + 1) * sizeof *vector);
vector[0] = strdup(cmdline);
/* command name */
strtok(vector[0], " ");
/* arguments */
while ((ptr = strtok(NULL, " "))) {
size_t len;
void *r;
len = strlen(ptr);
if (qmark) {
/* still in quotated text */
/* remove NULL termination of the previous token since it is not a token,
* but a part of the quotation string */
ptr[-1] = ' ';
if ((ptr[len - 1] == qmark) && (ptr[len - 2] != '\\')) {
/* end of quotation */
qmark = 0;
/* shorten the argument by the terminating quotation mark */
ptr[len - 1] = '\0';
}
continue;
}
/* another token in cmdline */
++count;
r = realloc(vector, (count + 1) * sizeof *vector);
if (!r) {
YLMSG_E("Memory allocation failed (%s:%d, %s).", __FILE__, __LINE__, strerror(errno));
free(vector);
return -1;
}
vector = r;
vector[count - 1] = ptr;
if ((ptr[0] == '"') || (ptr[0] == '\'')) {
/* remember the quotation mark to identify end of quotation */
qmark = ptr[0];
/* move the remembered argument after the quotation mark */
++vector[count - 1];
/* check if the quotation is terminated within this token */
if ((ptr[len - 1] == qmark) && (ptr[len - 2] != '\\')) {
/* end of quotation */
qmark = 0;
/* shorten the argument by the terminating quotation mark */
ptr[len - 1] = '\0';
}
}
}
vector[count] = NULL;
*argc_p = count;
*argv_p = vector;
return 0;
}
|