-
Notifications
You must be signed in to change notification settings - Fork 37
/
cligen_parse.y
1535 lines (1403 loc) · 44.1 KB
/
cligen_parse.y
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
CLI generator. Take idl as input and generate a tree for use in cli.
***** 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 *****
Choice:
*/
%start file
%union {
int intval;
char *string;
void *stack;
}
%token MY_EOF
%token V_RANGE V_LENGTH V_CHOICE V_KEYWORD V_REGEXP V_FRACTION_DIGITS V_SHOW V_TREENAME V_TRANSLATE V_PREFERENCE
%token DOUBLEPARENT /* (( */
%token DQ /* " */
%token DQP /* ") */
%token PDQ /* (" */
%token SETS /* @{ SETS */
%token <string> NAME /* in variables: <NAME type:NAME> */
%token <string> NUMBER /* In variables */
%token <string> DECIMAL /* In variables */
%token <string> CHARS
%token <string> HELPSTR
%type <string> charseq
%type <string> choices
%type <string> numdec
%type <string> arg1
%type <string> typecast
%type <string> helpstring
%type <string> helpstring1
%type <intval> preline
%lex-param {void *_cy} /* Add this argument to parse() and lex() function */
%parse-param {void *_cy}
%{
/* Here starts user C-code */
/* typecast macro */
#define _CY ((cligen_yacc *)_cy)
#define _YYERROR(msg) { cligen_parseerror(_CY, (msg)); YYERROR; }
/* add _cy to error paramaters */
#define YY_(msgid) msgid
#include "cligen_config.h"
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include "cligen_buf.h"
#include "cligen_cv.h"
#include "cligen_cvec.h"
#include "cligen_parsetree.h"
#include "cligen_pt_head.h"
#include "cligen_callback.h"
#include "cligen_object.h"
#include "cligen_syntax.h"
#include "cligen_handle.h"
#include "cligen_parse.h"
static int debug = 0;
/* Enable for debugging, steals some cycles otherwise */
#if 0
#define _PARSE_DEBUG(s) fprintf(stderr, s "\n")
#else
#define _PARSE_DEBUG(s)
#endif
extern int cligen_parseget_lineno (void);
int
cligen_parse_debug(int d)
{
debug = d;
return 0;
}
/*! CLIGEN parse error routine
*
* Also called from yacc generated code *
* @param[in] cy CLIgen yacc parse struct
*/
void cligen_parseerror(void *_cy,
char *s)
{
cligen_yacc *cy = (cligen_yacc *)_cy;
fprintf(stderr, "%s:%d: Error: %s: at or before: '%s'\n",
cy->cy_name,
cy->cy_linenum ,
s,
cligen_parsetext);
return;
}
#define cligen_parseerror1(cy, s) cligen_parseerror(cy, s)
/*! Create a CLIgen variable (cv) and store it in the current variable object
*
* Note that only one such cv can be stored.
* @param[in] cy CLIgen yacc parse struct
*/
static cg_var *
create_cv(cligen_yacc *cy,
char *type,
char *str)
{
cg_var *cv = NULL;
if ((cv = cv_new(CGV_STRING)) == NULL){
fprintf(stderr, "malloc: %s\n", strerror(errno));
goto done;
}
if (type){
if (cv_type_set(cv, cv_str2type(type)) == CGV_ERR){
fprintf(stderr, "%s:%d: error: No such type: %s\n",
cy->cy_name, cy->cy_linenum, type);
cv_free(cv); cv = NULL;
goto done;
}
}
if (cv_parse(str, cv) < 0){ /* parse str into cgv */
cv_free(cv); cv = NULL;
goto done;
}
done:
return cv;
}
/*!
* @param[in] cy CLIgen yacc parse struct
*/
static int
cgy_flag(cligen_yacc *cy,
char *var)
{
struct cgy_stack *cs = cy->cy_stack;
cg_var *cv;
int retval = -1;
if (debug)
fprintf(stderr, "%s: %s 1\n", __FUNCTION__, var);
if (cs){ /* XXX: why cs? */
if (cy->cy_cvec == NULL){
if ((cy->cy_cvec = cvec_new(0)) == NULL){
fprintf(stderr, "%s: cvec_new:%s\n", __FUNCTION__, strerror(errno));
goto done;
}
}
if ((cv = cvec_add(cy->cy_cvec, CGV_INT32)) == NULL){
fprintf(stderr, "%s: realloc:%s\n", __FUNCTION__, strerror(errno));
goto done;
}
cv_name_set(cv, var);
cv_int32_set(cv, 1);
}
retval = 0;
done:
return retval;
}
/*! Set a new treename. In fact registers the previous tree and creates a new .
*
* Note that one could have used an assignment: treename = <name>; for this but
* I decided to create special syntax for this so that assignments can use any
* variable names.
* @param[in] cy CLIgen yacc parse struct
* @param[in] name Name of tree
*/
static int
cgy_treename(cligen_yacc *cy,
char *name)
{
cg_obj *co = NULL;
cg_obj *cot;
struct cgy_list *cl;
int retval = -1;
int i;
parse_tree *pt;
pt_head *ph;
cg_var *cv;
/* Get the first object */
for (cl=cy->cy_list; cl; cl = cl->cl_next){
co = cl->cl_obj;
break;
}
/* Get the top object */
cot = co_top(co); /* co and cot can be same object */
pt = co_pt_get(cot);
/* If anything parsed */
if (pt_len_get(pt) > 0){
/* 2. Add the old parse-tree with old name*/
for (i=0; i<pt_len_get(pt); i++){
if ((co=pt_vec_i_get(pt, i)) != NULL)
co_up_set(co, NULL);
}
if ((ph = cligen_ph_add(cy->cy_handle, cy->cy_treename)) == NULL)
goto done;
if (cligen_ph_parsetree_set(ph, pt) < 0)
goto done;
/* 3. Create new parse-tree XXX */
if ((pt = pt_new()) == NULL)
goto done;
co_pt_clear(cot);
co_pt_set(cot, pt);
if ((cv = cvec_find(cy->cy_globals, "pipetree")) != NULL){
char *str;
if ((str = cv_string_get(cv)) != NULL && strlen(str))
if (cligen_ph_pipe_set(ph, str) < 0)
goto done;
}
}
/* 4. Set the new name */
if (cy->cy_treename)
free(cy->cy_treename);
if ((cy->cy_treename = strdup(name)) == NULL){
fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno));
goto done;
}
retval = 0;
done:
return retval;
}
/*! Variable assignment
*
* Only string type supported for now
* @param[in] cy CLIgen yacc parse struct
*/
static int
cgy_assignment(cligen_yacc *cy,
char *var,
char *val)
{
struct cgy_stack *cs = cy->cy_stack;
int retval = -1;
cg_var *cv;
char *treename_keyword;
cligen_handle h = cy->cy_handle;
if (cs == NULL){
errno = EINVAL;
goto done;
}
if (debug)
fprintf(stderr, "%s: %s=%s\n", __FUNCTION__, var, val);
if (cs->cs_next != NULL){ /* local */
if (cy->cy_cvec == NULL)
if ((cy->cy_cvec = cvec_new(0)) == NULL){
fprintf(stderr, "%s: cvec_new:%s\n", __FUNCTION__, strerror(errno));
goto done;
}
if ((cv = cvec_add(cy->cy_cvec, CGV_STRING)) == NULL){
fprintf(stderr, "%s: realloc:%s\n", __FUNCTION__, strerror(errno));
goto done;
}
cv_name_set(cv, var);
if (cv_parse(val, cv) < 0)
goto done;
}
else{ /* global */
treename_keyword = cligen_treename_keyword(h); /* typically: "treename" */
if (strcmp(var, treename_keyword) == 0){
if (cgy_treename(cy, val) < 0)
goto done;
}
else {
if ((cv = cvec_find(cy->cy_globals, var)) == NULL){
if ((cv = cvec_add(cy->cy_globals, CGV_STRING)) == NULL){
fprintf(stderr, "%s: realloc:%s\n", __FUNCTION__, strerror(errno));
goto done;
}
cv_name_set(cv, var);
}
if (cv_parse(val, cv) < 0) /* May be wrong type */
goto done;
}
}
retval = 0;
done:
return retval;
}
/*!
* @param[in] cy CLIgen yacc parse struct
*/
int
cgy_callback(cligen_yacc *cy,
char *cb_str)
{
struct cgy_stack *cs = cy->cy_stack;
cg_callback *cc, **ccp;
if (debug)
fprintf(stderr, "%s: %s\n", __FUNCTION__, cb_str);
if (cs == NULL)
return 0;
ccp = &cy->cy_callbacks;
while (*ccp != NULL)
ccp = &((*ccp)->cc_next);
if ((cc = malloc(sizeof(*cc))) == NULL){
fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno));
cligen_parseerror1(cy, "Allocating cligen callback");
return -1;
}
memset(cc, 0, sizeof(*cc));
cc->cc_fn_str = cb_str;
if (IS_PIPE_TREE(cy->cy_treename)){ /* Only for cligen, clixon has other mechanisms */
cc->cc_flags |= CC_FLAGS_PIPE_FUNCTION;
}
*ccp = cc;
return 0;
}
/*! Create a callback argument and store it in the current callback
*
* @param[in] cy CLIgen yacc parse struct
*/
static int
cgy_callback_arg(cligen_yacc *cy,
char *type,
char *arg)
{
int retval = -1;
cg_callback *cc;
cg_callback *cclast;
cg_var *cv = NULL;
cclast = NULL;
for (cc=cy->cy_callbacks; cc; cc = co_callback_next(cc))
cclast = cc;
if (cclast){
if ((cv = create_cv(cy, type, arg)) == NULL)
goto done;
if (cclast->cc_cvec)
cvec_append_var(cclast->cc_cvec, cv);
else
cclast->cc_cvec = cvec_from_var(cv);
}
retval = 0;
done:
if (cv)
cv_free(cv);
return retval;
}
/*!
* @param[in] cy CLIgen yacc parse struct
*/
static int
expand_arg(cligen_yacc *cy,
char *arg)
{
int retval = -1;
cg_var *cv = NULL;
if ((cv = create_cv(cy, "string", arg)) == NULL)
goto done;
if (cy->cy_var->co_expand_fn_vec)
cvec_append_var(cy->cy_var->co_expand_fn_vec, cv);
else
cy->cy_var->co_expand_fn_vec = cvec_from_var(cv);
retval = 0;
done:
if (cv)
cv_free(cv);
return retval;
}
/*!
* @param[in] cy CLIgen yacc parse struct
*/
static int
expand_fn(cligen_yacc *cy,
char *fn)
{
cy->cy_var->co_expand_fn_str = fn;
return 0;
}
static int
cg_translate(cligen_yacc *cy,
char *fn)
{
cy->cy_var->co_translate_fn_str = fn;
return 0;
}
static int
cg_preference(cligen_yacc *cy,
char *pref)
{
cg_obj *yv;
char *reason = NULL;
if ((yv = cy->cy_var) == NULL){
fprintf(stderr, "No var obj");
return -1;
}
if (parse_uint16(pref, &yv->co_preference, &reason) != 1){
cligen_parseerror1(cy, reason);
return -1;
}
return 0;
}
static int
cgy_list_push(cg_obj *co,
struct cgy_list **cl0)
{
struct cgy_list *cl;
if (debug)
fprintf(stderr, "%s\n", __FUNCTION__);
if ((cl = malloc(sizeof(*cl))) == NULL) {
fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno));
return -1;
}
cl->cl_next = *cl0;
cl->cl_obj = co;
*cl0 = cl;
return 0;
}
/*! Delete whole list
*
* @param[in] cy CLIgen yacc parse struct
*/
static int
cgy_list_delete(struct cgy_list **cl0)
{
struct cgy_list *cl;
while ((cl = *cl0) != NULL){
*cl0 = cl->cl_next;
free(cl);
}
return 0;
}
/*! Create new tmp variable cligen object
*
* It must be filled in by later functions.
* The reason is, the variable must be completely parsed by successive syntax
* (eg <type:> stuff) and we cant insert it into the object tree until that is done.
* And completed by the '_post' function
* @param[in] cy CLIgen yacc parse struct
* @retval tmp variable object
* @see cgy_var_post
*/
static cg_obj *
cgy_var_create(cligen_yacc *cy)
{
cg_obj *co;
/* Create unassigned variable object */
if ((co = cov_new(CGV_ERR, NULL)) == NULL){
fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno));
cligen_parseerror1(cy, "Allocating cligen object");
return NULL;
}
if (cy->cy_optional){
co_flags_set(co, CO_FLAGS_OPTION);
}
if (debug)
fprintf(stderr, "%s: pre\n", __FUNCTION__);
return co;
}
/*! Set name and type on a (previously created) variable
*
* @param[in] cy CLIgen yacc parse struct
* @see cgy_var_create
*/
static int
cgy_var_name_type(cligen_yacc *cy,
char *name,
char *type)
{
cy->cy_var->co_command = name;
if ((cy->cy_var->co_vtype = cv_str2type(type)) == CGV_ERR){
cligen_parseerror1(cy, "Invalid type");
fprintf(stderr, "%s: Invalid type: %s\n", __FUNCTION__, type);
return -1;
}
return 0;
}
/*! Complete variable cligen object after parsing is complete,
*
* And insert it into object hierarchies.
* That is, insert a variable in each hieracrhy.
* @param[in] cy CLIgen yacc parse struct
* @retval 0 OK
* @retval -1 Error
*/
static int
cgy_var_post(cligen_yacc *cy)
{
struct cgy_list *cl;
cg_obj *coc = NULL; /* variable copy object */
cg_obj *coparent; /* parent */
cg_obj *co; /* new obj/sister */
cg_obj *coy = cy->cy_var;
#if 0
if (coy->co_vtype == CGV_ERR) /* unassigned */
coy->co_vtype = cv_str2type(coy->co_command);
#endif
if (debug)
fprintf(stderr, "%s: cmd:%s vtype:%d\n", __FUNCTION__,
coy->co_command,
coy->co_vtype );
if (coy->co_vtype == CGV_ERR){
cligen_parseerror1(cy, "Wrong or unassigned variable type");
return -1;
}
#if 0 /* XXX dont really know what i am doing but variables dont behave nice in choice */
if (cy->cy_opt){ /* get coparent from stack */
if (cy->cy_stack == NULL){
fprintf(stderr, "Option allowed only within () or []\n");
return -1;
}
cl = cy->cy_stack->cs_list;
}
else
#endif
cl = cy->cy_list;
for (; cl; cl = cl->cl_next){
coparent = cl->cl_obj;
if (cl->cl_next){
if (co_copy(coy, coparent, 0x0, &coc) < 0) /* duplicate coy to coc */
return -1;
}
else
coc = coy; /* Dont copy if last in list */
co_up_set(coc, coparent);
if ((co = co_insert(co_pt_get(coparent), coc)) == NULL) /* coc may be deleted */
return -1;
cl->cl_obj = co;
}
return 0;
}
/*! Create a new command object.
*
* Actually, create a new for every tree in the list
* and replace the old with the new object.
* @param[in] cy CLIgen yacc parse struct
* @param[in] cmd the command string
* @retval 0 OK
* @retval -1 Error
*/
static int
cgy_cmd(cligen_yacc *cy,
char *cmd)
{
struct cgy_list *cl;
cg_obj *cop; /* parent */
cg_obj *conew; /* new obj */
cg_obj *co; /* new/sister */
for (cl=cy->cy_list; cl; cl = cl->cl_next){
cop = cl->cl_obj;
if (debug)
fprintf(stderr, "%s: %s parent:%s\n",
__FUNCTION__, cmd, cop->co_command);
if ((conew = co_new(cmd, cop)) == NULL) {
cligen_parseerror1(cy, "Allocating cligen object");
return -1;
}
if ((co = co_insert(co_pt_get(cop), conew)) == NULL) /* co_new may be deleted */
return -1;
if (cy->cy_optional){
co_flags_set(co, CO_FLAGS_OPTION);
}
cl->cl_obj = co; /* Replace parent in cgy_list */
}
return 0;
}
/*! Create a REFERENCE node that references another tree.
*
* This is evaluated in runtime by pt_expand().
* @param[in] cy CLIgen yacc parse struct
* @param[in] name Name of tree reference
* @param[in] pipetree If 1 references a "pipe" tree, ie prepend a "|" to name
* @see also db2tree() in clicon/apps/cli_main.c on how to create such a tree
* @see pt_expand_treeref()/pt_callback_reference() how it is expanded
*/
static int
cgy_reference(cligen_yacc *cy,
char *name,
int pipetree)
{
int retval = -1;
struct cgy_list *cl;
cg_obj *cop; /* parent */
cg_obj *cot; /* tree */
cbuf *cb = NULL;
if ((cb = cbuf_new()) == NULL)
goto done;
if (pipetree)
cprintf(cb, "|");
cprintf(cb, "%s", name);
for (cl=cy->cy_list; cl; cl = cl->cl_next){
/* Add a treeref 'stub' which is expanded in pt_expand to a sub-tree */
cop = cl->cl_obj;
if ((cot = co_new(cbuf_get(cb), cop)) == NULL) {
cligen_parseerror1(cy, "Allocating cligen object");
goto done;
}
cot->co_type = CO_REFERENCE;
if ((cot = co_insert(co_pt_get(cop), cot)) == NULL) /* cot may be deleted */
goto done;
/* Replace parent in cgy_list: not allowed after ref?
but only way to add callbacks to it.
*/
if (cy->cy_optional){
co_flags_set(cot, CO_FLAGS_OPTION);
}
cl->cl_obj = cot;
}
retval = 0;
done:
if (cb)
cbuf_free(cb);
return retval;
}
/*! Add one line of helpstr
*
* @param[in] cy CLIgen yacc parse struct
* @param[in] merge OK to merge
* @param[in] helpstr Malloced help string, stripped of spaces, consumed here
*/
static int
cgy_helpstring(cligen_yacc *cy,
int merge,
char *helpstr)
{
int retval = -1;
struct cgy_list *cl;
cg_obj *co;
if (helpstr == NULL){
errno = EINVAL;
goto done;
}
for (cl = cy->cy_list; cl; cl = cl->cl_next){
co = cl->cl_obj;
if (co->co_helpstring){
if (merge){
if ((co->co_helpstring = realloc(co->co_helpstring,
strlen(co->co_helpstring) + strlen(helpstr) + 2)) == NULL){
cligen_parseerror1(cy, "Allocating helpstr");
goto done;
}
strcat(co->co_helpstring, "\n");
strcat(co->co_helpstring, helpstr);
}
}
else
if ((co->co_helpstring = strdup(helpstr)) == NULL){
cligen_parseerror1(cy, "Allocating helpstr");
goto done;
}
}
free(helpstr);
retval = 0;
done:
return retval;
}
/*! Append a new choice option to a choice variable string
*
* @param[in] cy CLIgen yacc parse struct
* @param[in] str Accumulated choice string on the form: "a|..|z"
* @param[in] app Choice option to append
* @retval s New string created by appending: "<str>|<app>"
* This is just string manipulation, the complete string is linked into CLIgen structs by upper rule
* Note that this is variable choice. There is also command choice, eg [a|b] which is different.
*/
static char *
cgy_var_choice_append(cligen_yacc *cy,
char *str,
char *app)
{
int len;
char *s;
len = strlen(str)+strlen(app) + 2;
if ((s = realloc(str, len)) == NULL) {
fprintf(stderr, "%s: realloc: %s\n", __FUNCTION__, strerror(errno));
return NULL;
}
strncat(s, "|", len-1);
strncat(s, app, len-1);
return s;
}
/*! Post-processing of commands, eg at ';':
*
* a cmd;<--
* But only if parsing succesful.
* 1. Add callback and args to every list
* 2. Add empty child unless already empty child
* @param[in] cy CLIgen yacc parse struct
* @note co_callback_copy takes cycles, ca 50%
*/
int
cgy_terminal(cligen_yacc *cy)
{
struct cgy_list *cl;
cg_obj *co;
int i;
cg_callback **ccp;
int retval = -1;
parse_tree *ptc;
cg_obj *coi;
for (cl = cy->cy_list; cl; cl = cl->cl_next){
co = cl->cl_obj;
if (cy->cy_callbacks){ /* callbacks */
ccp = &co->co_callbacks;
while (*ccp != NULL)
ccp = &((*ccp)->cc_next);
#if 1 /* Optimization: dont copy the last element */
if (cl->cl_next == NULL)
*ccp = cy->cy_callbacks;
else
#endif
if (co_callback_copy(cy->cy_callbacks, ccp) < 0)
goto done;
}
/* variables: special case "hide" */
if (cy->cy_cvec){
if (cvec_find(cy->cy_cvec, "hide") != NULL)
co_flags_set(co, CO_FLAGS_HIDE);
/* generic variables */
if (co->co_cvec)
cvec_free(co->co_cvec);
if ((co->co_cvec = cvec_dup(cy->cy_cvec)) == NULL){ /* this leaks */
fprintf(stderr, "%s: cvec_dup: %s\n", __FUNCTION__, strerror(errno));
goto done;
}
}
/* misc */
if ((ptc = co_pt_get(co)) != NULL){
for (i=0; i<pt_len_get(ptc); i++){
if (pt_vec_i_get(ptc, i) == NULL)
break;
}
if (i == pt_len_get(ptc)){ /* Insert empty child if ';' */
if ((coi = co_new(NULL, co)) == NULL) {
cligen_parseerror1(cy, "Allocating cligen object");
return -1;
}
coi->co_type = CO_EMPTY;
co_insert(co_pt_get(co), coi);
}
}
else{ /* Should never reach here? */
co_insert(co_pt_get(co), NULL);
}
}
/* cleanup */
#if 1 /* Optimization: dont copy first */
if ((cl = cy->cy_list) != NULL)
cy->cy_callbacks = NULL;
else
#endif
if (cy->cy_callbacks)
co_callbacks_free(&cy->cy_callbacks);
if (cy->cy_cvec){
cvec_free(cy->cy_cvec);
cy->cy_cvec = NULL;
}
retval = 0;
done:
return retval;
}
/*! Take the whole cgy_list and push it to the stack
*
* @param[in] cy CLIgen yacc parse struct
* @see ctx_pop
*/
static int
ctx_push(cligen_yacc *cy,
int sets)
{
struct cgy_list *cl;
struct cgy_stack *cs;
cg_obj *co;
if (debug)
fprintf(stderr, "%s\n", __FUNCTION__);
/* Create new stack element */
if ((cs = malloc(sizeof(*cs))) == NULL) {
fprintf(stderr, "%s: malloc: %s\n", __FUNCTION__, strerror(errno));
return -1;
}
memset(cs, 0, sizeof(*cs));
cs->cs_next = cy->cy_stack;
cy->cy_stack = cs; /* Push the new stack element */
for (cl = cy->cy_list; cl; cl = cl->cl_next){
co = cl->cl_obj;
if (cvec_find(cy->cy_cvec, "hide") != NULL)
co_flags_set(co, CO_FLAGS_HIDE);
if (sets)
co_sets_set(co, 1);
if (cgy_list_push(co, &cs->cs_list) < 0)
return -1;
}
return 0;
}
/*! Peek context from stack and replace the object list with it
*
* Typically done in a command choice, eg "(c1|c2)" at c2.
* Dont pop context
* @param[in] cy CLIgen yacc parse struct
* @see ctx_peek_swap2
*/
static int
ctx_peek_swap(cligen_yacc *cy)
{
struct cgy_stack *cs;
struct cgy_list *cl;
cg_obj *co;
if (debug)
fprintf(stderr, "%s\n", __FUNCTION__);
if ((cs = cy->cy_stack) == NULL){
#if 1
cligen_parseerror1(cy, "No surrounding () or []");
return -1; /* e.g a|b instead of (a|b) */
#else
cgy_list_delete(&cy->cy_list);
return 0;
#endif
}
for (cl = cy->cy_list; cl; cl = cl->cl_next){
co = cl->cl_obj;
if (cgy_list_push(co, &cs->cs_saved) < 0)
return -1;
}
cgy_list_delete(&cy->cy_list);
for (cl = cs->cs_list; cl; cl = cl->cl_next){
co = cl->cl_obj;
if (cgy_list_push(co, &cy->cy_list) < 0)
return -1;
}
return 0;
}
/*! Peek context from stack and replace the object list with it
*
* Typically done in a command choice, eg "(c1|c2)" at c2.
* Dont pop context
* @param[in] cy CLIgen yacc parse struct
* @see ctx_peek_swap
*/
static int
ctx_peek_swap2(cligen_yacc *cy)
{
struct cgy_stack *cs;
struct cgy_list *cl;
cg_obj *co;
if (debug)
fprintf(stderr, "%s\n", __FUNCTION__);
if ((cs = cy->cy_stack) == NULL){
#if 1
cligen_parseerror1(cy, "No surrounding () or []");
return -1; /* e.g a|b instead of (a|b) */
#else
cgy_list_delete(&cy->cy_list);
return 0;
#endif
}
cgy_list_delete(&cy->cy_list);
for (cl = cs->cs_list; cl; cl = cl->cl_next){
co = cl->cl_obj;
if (cgy_list_push(co, &cy->cy_list) < 0)
return -1;
}
return 0;
}
static int
delete_stack_element(struct cgy_stack *cs)
{
cgy_list_delete(&cs->cs_list);
cgy_list_delete(&cs->cs_saved);
free(cs);
return 0;
}
/*! Pop context from stack and add it to object list
*
* Done after an option, eg "cmd [opt]"
* "cmd <push> [opt] <pop>". At pop, all objects saved at push
* are added to the object list.
* @param[in] cy CLIgen yacc parse struct
*/
static int
ctx_pop_add(cligen_yacc *cy)
{
struct cgy_stack *cs;
struct cgy_list *cl;
cg_obj *co;
if (debug)
fprintf(stderr, "%s\n", __FUNCTION__);
if ((cs = cy->cy_stack) == NULL){
fprintf(stderr, "%s: cgy_stack empty\n", __FUNCTION__);
return -1; /* shouldnt happen */
}
cy->cy_stack = cs->cs_next;
/* We could have saved some heap work by moving the cs_list,... */
for (cl = cs->cs_list; cl; cl = cl->cl_next){
co = cl->cl_obj;
if (cgy_list_push(co, &cy->cy_list) < 0)
return -1;
}
for (cl = cs->cs_saved; cl; cl = cl->cl_next){
co = cl->cl_obj;
if (cgy_list_push(co, &cy->cy_list) < 0)
return -1;
}
delete_stack_element(cs);
return 0;
}
/*! Pop context from stack and discard it.
*
* Typically done after a grouping, eg "cmd (opt1|opt2)"
* @param[in] cy CLIgen yacc parse struct
* @see ctx_push
*/
static int
ctx_pop(cligen_yacc *cy)
{
struct cgy_stack *cs;
struct cgy_list *cl;
cg_obj *co;