-
Notifications
You must be signed in to change notification settings - Fork 37
/
cligen_file.c
432 lines (406 loc) · 13.3 KB
/
cligen_file.c
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
CLIgen application reading CLI specification from file
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2001-2022 Olof Hagsand
This file is part of CLIgen.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 or later (the "GPL"),
in which case the provisions of the GPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of the GPL, and not to allow others to
use your version of this file under the terms of Apache License version 2, indicate
your decision by deleting the provisions above and replace them with the
notice and other provisions required by the GPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the Apache License version 2 or the GPL.
***** END LICENSE BLOCK *****
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <cligen/cligen.h>
/*! General callback for executing shells.
*
* The argument is a command followed by arguments as defined in the input syntax.
* Simple example:
* CLIgen input syntax: <a:str>, cligen_exec_cb("ls ${a}");
* CLI input: > 42
* Shell command: ls 42
* More advanced example:
* CLIgen input syntax: [<a type:int> | <b type:ipv4addr>],
* cligen_exec_cb("foo.sh ${a:-99} ${b:-1.2.3.4}");
* CLI input: > 22
* Shell command: foo.sh 22 1.2.3.4
* CLI input: > 2.3.4.5
* Shell command: foo.sh 99 1.2.3.4.
*/
int
cligen_exec_cb(cligen_handle handle,
cvec *cvv,
cvec *argv)
{
cg_var *cv = NULL;
char buf[64];
int pid;
int ret;
int status;
if (argv == NULL)
return 0;
if ((pid = fork()) == 0){ /* child */
while ((cv = cvec_each1(cvv, cv)) != NULL) {
if (cv_const_get(cv))
continue;
cv2str(cv, buf, sizeof(buf)-1);
setenv(cv_name_get(cv), buf, 1 );
}
cv2str(cvec_i(argv, 0), buf, sizeof(buf)-1);
ret = system(buf);
exit(ret);
}
/* Wait for child to finish */
if(waitpid (pid, &status, 0) == pid)
ret = WEXITSTATUS(status);
else
ret = -1;
return ret;
}
/*! CLI generic callback printing the variable vector and argument
*/
int
callback(cligen_handle handle,
cvec *cvv,
cvec *argv)
{
int i = 0;
cg_var *cv;
char buf[256];
cligen_output(stderr, "function: %s\n", cligen_fn_str_get(handle));
cligen_output(stderr, "variables:\n");
cv = NULL;
while ((cv = cvec_each(cvv, cv)) != NULL) {
cv2str(cv, buf, sizeof(buf)-1);
cligen_output(stderr, "\t%d name:%s type:%s value:%s\n",
i++,
cv_name_get(cv),
cv_type2str(cv_type_get(cv)),
buf
);
}
if (argv){
cv = NULL;
i=0;
while ((cv = cvec_each(argv, cv)) != NULL) {
cv2str(cv, buf, sizeof(buf)-1);
cligen_output(stdout, "arg %d:", i++);
cligen_output(stdout, " %s\n", buf); /* Separaration for test/test_more.sh */
}
}
cli_output_reset();
return 0;
}
/*! cli callback calls cligen_output with output lines as given by function arguments
*
* This is to generate output to eg cligen_output scrolling
* Example:
* a, printlines_fn("line1 abc", "line2 def");
*/
int
output_fn(cligen_handle handle,
cvec *cvv,
cvec *argv)
{
cg_var *cv;
cv = NULL;
while ((cv = cvec_each(argv, cv)) != NULL)
cligen_output(stdout, "%s\n", cv_string_get(cv));
return 0;
}
/*! Output pipe function
*
* First argv is a shell command,
* the following argv:s are names of variables in cvv whose values are appended to the shell
* @param[in] h CLIgen handle / user handle
* @param[in] cvv Vector of variables: function parameters
* @param[in] argv Vector of variables from command-line
* @code
* abc grep <arg:string>, pipe_shell_fn("grep -e", "arg");
* @endcode
* @note WARNING: USE ONLY FOR TEST. You could exec any unix command
*/
int
pipe_shell_fn(cligen_handle h,
cvec *cvv,
cvec *argv)
{
int retval = -1;
cbuf *cb = NULL;
cg_var *av;
cg_var *cv;
char *name;
if ((cb = cbuf_new()) == NULL){
perror("cbuf_new");
goto done;
}
if (argv && (av = cvec_i(argv, 0)) != NULL){
/* First arg is command */
cprintf(cb, "%s", cv_string_get(av));
/* Rest are names of parameters from cvv */
av = NULL;
while ((av = cvec_each1(argv, av)) != NULL){
name = cv_string_get(av);
if ((cv = cvec_find_var(cvv, name)) != NULL)
cprintf(cb, " %s", cv_string_get(cv));
}
retval = execl("/bin/sh", "sh", "-c", cbuf_get(cb), (char *) NULL);
}
done:
if (cb)
cbuf_free(cb);
return retval;
}
/*! Example of static string to function mapper
*
* Note, the syntax need to something like: "a{help}, callback(42)"
*/
cgv_fnstype_t *
str2fn(char *name,
void *arg,
char **error)
{
*error = NULL;
if (strcmp(name, "callback") == 0)
return callback;
else if (strcmp(name, "pipe_shell_fn") == 0)
return pipe_shell_fn;
else if (strcmp(name, "output_fn") == 0)
return output_fn;
else if (strcmp(name, "cligen_exec_cb") == 0)
return cligen_exec_cb;
else return callback; /* allow any function (for testing) */
}
/*! Example of expansion(completion) function.
*
* It is called every time a variable of the form <expand> needs to be evaluated.
* Note the mallocing of vectors which could probably be done in a
* friendlier way.
* Note also that the expansion is not very dynamic, a script or reading a file
* would have introduced som more dynamics.
*/
static int
cli_expand_cb(cligen_handle h,
char *fn_str,
cvec *cvv,
cvec *argv,
cvec *commands, /* vector of function strings */
cvec *helptexts) /* vector of help-texts */
{
#if 1
/* Special case for two partly overlapping expand sets */
if (strcmp(fn_str,"exp")==0){
cvec_add_string(commands, NULL, "exp1"); cvec_add_string(helptexts, NULL, "Help exp1");
cvec_add_string(commands, NULL, "exp2"); cvec_add_string(helptexts, NULL, "Help exp2");
cvec_add_string(commands, NULL, "exp3"); cvec_add_string(helptexts, NULL, "Help exp3");
}
else {
cvec_add_string(commands, NULL, "exp2"); cvec_add_string(helptexts, NULL, "Help exp2");
}
#else
cvec_add_string(commands, NULL, "auto");
cvec_add_string(helptexts, NULL, "This expansion is auto-generated by -e option");
#endif
return 0;
}
/*! Trivial function translator/mapping function that just assigns same callback
*/
static expandv_cb *
str2fn_exp(char *name,
void *arg,
char **error)
{
return cli_expand_cb;
}
/*
* Global variables.
*/
static void
usage(char *argv)
{
fprintf(stderr, "Usage:%s <option>*, where the options have the following meaning:\n"
"\t-h \t\tHelp\n"
"\t-V \t\tPrint version and exit\n"
"\t-f <file> \tConfig-file (or stdin)\n"
"\t-1 \t\tOnce only. Do not enter interactive mode\n"
"\t-p \t\tPrint syntax\n"
"\t-C \t\tDont copy treeref mode\n"
"\t-d \t\tDump syntax with implementation-specific info\n"
"\t-e \t\tSet automatic expansion/completion for all expand() functions\n"
"\t-E \t\tExclude keys in callback cvv. Default include keys\n"
"\t-c \t\tExpand first arg of callback cvv to string matching keywords\n"
"\t-P <mode> \tSet preference mode: 1: tiebreak terminals, 2: also non-terminals\n"
"\t-t <nr> \tSet tab mode: 1:columns, 2: same pref for vars, 4: all steps\n"
"\t-s <nr> \tScrolling 0: disable line scrolling, 1: enable line scrolling (default 1)\n"
"\t-u \t\tEnable experimental UTF-8 mode\n"
,
argv);
exit(0);
}
/* Main */
int
main(int argc,
char *argv[])
{
int retval = -1;
parse_tree *pt = NULL;
pt_head *ph;
FILE *f = stdin;
char *argv0 = argv[0];
char *filename=NULL;
cvec *globals; /* global variables from syntax */
cligen_handle h;
char *str;
int once = 0;
int print_syntax = 0;
int dump_syntax = 0;
int set_expand = 0;
int set_preference = 0;
int tabmode = 0;
int scrollmode = 0;
int exclude_keys = 0;
int expand_first = 0;
if ((h = cligen_init()) == NULL)
goto done;
argv++;argc--;
for (;(argc>0)&& *argv; argc--, argv++){
if (**argv != '-')
break;
(*argv)++;
if (strlen(*argv)==0)
usage(argv0);
switch(**argv) {
case 'h': /* help */
usage(argv0); /* usage exits */
break;
case 'V': /* version */
cligen_output(stdout, "CLIgen version: %s\n", CLIGEN_VERSION);
goto ok;
break;
case '1': /* quit directly */
once++;
break;
case 'p': /* print syntax */
print_syntax++;
break;
case 'd': /* dump syntax */
dump_syntax++;
break;
case 'e': /* Set automatic completion/expand */
set_expand++;
break;
case 'E': /* Exclude keys in callback cvv */
exclude_keys++;
break;
case 'c': /* Expand first arg of callback cvv */
expand_first++;
break;
case 'P': /* Return first if several have same preference, for terminals */
argc--;argv++;
set_preference = atoi(*argv);
break;
case 'f' :
argc--;argv++;
filename = *argv;
if ((f = fopen(filename, "r")) == NULL){
fprintf(stderr, "fopen(%s): %s\n", filename, strerror(errno));
exit(1);
}
break;
case 's': /* line scrolling mode */
argc--;argv++;
scrollmode = atoi(*argv);
break;
case 't': /* tab mode */
argc--;argv++;
tabmode = atoi(*argv);
break;
case 'u': /* UTF-8 experimental */
cligen_utf8_set(h, 1);
break;
default:
usage(argv0);
break;
}
}
if (exclude_keys)
cligen_exclude_keys_set(h, 1);
if (expand_first)
cligen_expand_first_set(h, 1);
cligen_lexicalorder_set(h, 1);
cligen_ignorecase_set(h, 1);
if (set_preference)
cligen_preference_mode_set(h, set_preference);
// cligen_parse_debug(1);
if ((globals = cvec_new(0)) == NULL)
goto done;
if (clispec_parse_file(h, f, filename?filename:"stdin", NULL, NULL, globals) < 0)
goto done;
ph = NULL;
while ((ph = cligen_ph_each(h, ph)) != NULL){
if ((pt = cligen_ph_parsetree_get(ph)) != NULL){ /* map functions */
if (cligen_callbackv_str2fn(pt, str2fn, NULL) < 0) /* callback */
goto done;
if (set_expand &&
cligen_expandv_str2fn(pt, str2fn_exp, NULL) < 0) /* expand */
goto done;
}
}
if ((str = cvec_find_str(globals, "prompt")) != NULL)
cligen_prompt_set(h, str);
if (tabmode != 0)
cligen_tabmode_set(h, tabmode);
else if ((str = cvec_find_str(globals, "tabmode")) != NULL)
if (strcmp(str,"long") == 0)
cligen_tabmode_set(h, CLIGEN_TABMODE_COLUMNS);
cligen_line_scrolling_set(h, scrollmode);
if ((str = cvec_find_str(globals, "comment")) != NULL)
cligen_comment_set(h, *str);
if ((str = cvec_find_str(globals, "mode")) != NULL)
cligen_ph_active_set_byname(h, str);
cvec_free(globals);
if (print_syntax){
pt_print1(stdout, pt, 0);
fflush(stdout);
}
if (dump_syntax){
uint64_t nr = 0;
size_t sz = 0;
pt_stats(pt, &nr, &sz);
fprintf(stdout, "nr:%" PRIu64 ", size:%zu\n", nr, sz);
pt_dump(stdout, pt);
fflush(stdout);
}
if (!once && cligen_loop(h) < 0)
goto done;
ok:
retval = 0;
done:
fclose(f);
if (h)
cligen_exit(h);
return retval;
}