-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcligen_pt_head.c
539 lines (489 loc) · 12.2 KB
/
cligen_pt_head.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
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
/*
***** 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 *****
* CLIgen parsetree head structure, ie top-level structure holding all info about parse-trees
* This was all hidden in cligen_handle.c but is now a first-class object. However, the functions
* are still strange in a way that he underlying parse-tree is sometimes treated as first-level
* which makes the code strange.
*/
#include "cligen_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#define __USE_GNU /* strverscmp */
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <signal.h>
#include <sys/ioctl.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_io.h"
#include "cligen_handle.h"
#include "cligen_result.h"
#include "cligen_read.h"
#include "cligen_parse.h"
#include "cligen_history.h"
#include "cligen_getline.h"
#include "cligen_print.h"
#include "cligen_handle_internal.h"
/*
* Access functions
*/
/*! Access function to the get name of parsetree header
*
* @param[in] ph Parse tree header
* @retval name Name
* @retval NULL Error
*/
char*
cligen_ph_name_get(pt_head *ph)
{
if (ph == NULL){
errno = EINVAL;
return NULL;
}
return ph->ph_name;
}
/*! Access function to set name of parsetree header
*
* @param[in] ph Parse tree header
* @param[in] name Name
* @retval 0 OK
* @retval -1 Error
*/
int
cligen_ph_name_set(pt_head *ph,
char *name)
{
if (ph == NULL){
errno = EINVAL;
return -1;
}
if (ph->ph_name)
free(ph->ph_name);
if (name){
if ((ph->ph_name = strdup(name)) == NULL)
return -1;
}
else
ph->ph_name = NULL;
return 0;
}
/*! Access function to the working point in a tree, shortcut to implement edit modes.
*
* @param[in] ph Parse tree header
* @retval pt Parse tree
* @retval NULL Error or no parse tree
*/
parse_tree*
cligen_ph_parsetree_get(pt_head *ph)
{
if (ph == NULL){
errno = EINVAL;
return NULL;
}
return ph->ph_parsetree;
}
/*! Access function to set parsetree of parse-tree header, remove pt from its parents
*
* @param[in] ph Parse-tree header
* @param[in] pt parse-tree
* @retval 0 OK
* @retval -1 Error
*/
int
cligen_ph_parsetree_set(pt_head *ph,
parse_tree *pt)
{
int retval = -1;
int i;
cg_obj *co;
if (ph == NULL){
errno = EINVAL;
goto done;
}
for (i=0; i<pt_len_get(pt); i++){
if ((co = pt_vec_i_get(pt, i)) != NULL)
co_up_set(co, NULL);
}
ph->ph_parsetree = pt; /* XXX not free if exists? */
retval = 0;
done:
return retval;
}
/*! Access function to the working point in a tree, shortcut to implement edit modes.
*
* @param[in] h CLIgen handle
* @param[in] name Name of tree
* @retval wp Working point cligen object (shortcut - a sub of this tree)
* @retval NULL No such tree/error
*/
cg_obj *
cligen_ph_workpoint_get(pt_head *ph)
{
return ph->ph_workpt;
}
/*! Access function to the working point in a tree, shortcut to implement edit modes.
*
* @param[in] pt Parse tree
* @param[in] wp Working point identified by a cligen object(actually its parse-tree sub vector)
*/
int
cligen_ph_workpoint_set(pt_head *ph,
cg_obj *wp)
{
ph->ph_workpt = wp;
return 0;
}
/*! Get tree-specific prompt
*
* @param[in] ph Parse-tree header
* @retval str Prompt string
*/
char *
cligen_ph_prompt_get(pt_head *ph)
{
return ph->ph_prompt;
}
/*! Set tree-specific prompt
*
* @param[in] ph Parse-tree header
* @param[in] str Prompt string (copied/malloced)
*/
int
cligen_ph_prompt_set(pt_head *ph,
char *prompt)
{
if (ph->ph_prompt){
free(ph->ph_prompt);
ph->ph_prompt = NULL;
}
if (prompt){
if ((ph->ph_prompt = strdup(prompt)) == NULL)
return -1;
}
return 0;
}
/*! Get pipe output tree
*
* @param[in] ph Parse-tree header
* @retval name Name of output pipe tree
*/
char *
cligen_ph_pipe_get(pt_head *ph)
{
return ph->ph_output_pipe;
}
/*! Set pipe output tree
*
* @param[in] ph Parse-tree header
* @param[in] name Name of output pipe tree, is copied
*/
int
cligen_ph_pipe_set(pt_head *ph,
char *pipe)
{
if (ph->ph_output_pipe){
free(ph->ph_output_pipe);
ph->ph_output_pipe = NULL;
}
if (pipe){
if ((ph->ph_output_pipe = strdup(pipe)) == NULL)
return -1;
}
return 0;
}
/*! Find a parsetree head by its name,
*
* @param[in] h CLIgen handle
* @param[in] name Name of tree
* @retval ph Parse-tree header
* @retval NULL Not found / error
*/
pt_head *
cligen_ph_find(cligen_handle h,
char *name)
{
char *phname;
pt_head *ph = NULL;
if (name == NULL){
errno = EINVAL;
return NULL;
}
while ((ph = cligen_ph_each(h, ph)))
if ((phname = cligen_ph_name_get(ph)))
if (strcmp(phname, name) == 0)
break;
return ph;
}
/*! Free a parsetree header
*
* @param[in] ph Parse-tree header
*/
int
cligen_ph_free(pt_head *ph)
{
if (ph == NULL){
errno = EINVAL;
return -1;
}
if (ph->ph_name)
free(ph->ph_name);
if (ph->ph_parsetree)
pt_free(ph->ph_parsetree, 1);
if (ph->ph_prompt)
free(ph->ph_prompt);
if (ph->ph_output_pipe)
free(ph->ph_output_pipe);
free(ph);
return 0;
}
/*! Append a new parsetree header
*
* @param[in] h CLIgen handle
* @param[in] name Name of this tree
* @retval ph The new parsetree header
* @retval NULL Error
* Note, if this is the first tree, it is activated by default
*/
pt_head *
cligen_ph_add(cligen_handle h,
char *name)
{
pt_head *ph, *phlast;
if ((ph = (pt_head *)malloc(sizeof(*ph))) == NULL)
goto done;
memset(ph, 0, sizeof(*ph));
if (cligen_ph_name_set(ph, name) < 0){
free(ph);
ph = NULL;
goto done;
}
/* first tree is set and activeted by default */
if ((phlast = cligen_pt_head_get(h)) == NULL) {
cligen_pt_head_active_set(h, ph);
cligen_pt_head_set(h, ph);
goto done;
}
/* append new parsetree header to the end */
while (phlast->ph_next)
phlast = phlast->ph_next;
phlast->ph_next = ph;
done:
return ph;
}
/*! Iterate through all parsed cligen trees
*
* @param[in] h CLIgen handle
* @param[in] pt Cligen parse-tree iteration variable. Must be initialized to NULL
* @retval pt Next parse-tree structure.
* @retval NULL When end of list reached.
* @code
* pt_head *ph = NULL;
* while ((ph = cligen_ph_each(h, ph)) != NULL) {
* ...
* }
* @endcode
* Note: you may not delete (or add) parse-tree-heads while iterating through them
*/
pt_head *
cligen_ph_each(cligen_handle h,
pt_head *ph0)
{
pt_head *ph = NULL;
if (ph0 == NULL)
ph = cligen_pt_head_get(h);
else
ph = ph0->ph_next;
return ph;
}
/*! Return i:th parse-tree of parsed cligen trees
*
* @param[in] h CLIgen handle
* @param[in] i Order of element to get
*/
pt_head *
cligen_ph_i(cligen_handle h,
int i0)
{
pt_head *ph;
int i;
for (ph = cligen_pt_head_get(h), i=0; ph; ph = ph->ph_next, i++)
if (i==i0)
return ph;
return NULL;
}
/*! Get currently active parsetree.
*
* @param[in] h CLIgen handle
* @see cligen_ph_active_get prefer to use that function instead
*/
parse_tree *
cligen_pt_active_get(cligen_handle h)
{
pt_head *ph = cligen_pt_head_active_get(h);
return ph->ph_parsetree;
}
/*! Get currently active parsetree head.
*
* @param[in] h CLIgen handle
*/
pt_head *
cligen_ph_active_get(cligen_handle h)
{
return (cligen_pt_head_active_get(h));
}
/*! Set currently active parsetree by name
*
* @param[in] h CLIgen handle
* @retval 0
* If parse-tree not found all are inactivated.
*/
int
cligen_ph_active_set_byname(cligen_handle h,
char *name)
{
pt_head *ph = cligen_ph_find(h, name);
cligen_pt_head_active_set(h, ph);
return 0;
}
/*
* CLIgen parse-tree workpoint example:
* @code
* edit,wpset("t");{
* @working, wpset("t");
* }
* show, wpshow("t");
* up, wpup("t");
* top, wptop("t");
* treename="t";
* ...
* @endcode
*/
/*! Callback: Working point tree set
*
* Format of argv:
* <treename>
*/
int
cligen_wp_set(cligen_handle h,
cvec *cvv,
cvec *argv)
{
cg_var *cv;
char *treename;
pt_head *ph;
cg_obj *co;
cv = cvec_i(argv, 0);
treename = cv_string_get(cv);
if ((ph = cligen_ph_find(h, treename)) != NULL &&
(co = cligen_co_match(h)) != NULL){
if (co->co_treeref_orig != NULL)
cligen_ph_workpoint_set(ph, co->co_treeref_orig);
else if (co->co_ref != NULL)
cligen_ph_workpoint_set(ph, co->co_ref);
}
return 0;
}
/*! Callback: Working point tree show
*
* Format of argv:
* <treename>
*/
int
cligen_wp_show(cligen_handle h,
cvec *cvv,
cvec *argv)
{
cg_var *cv;
char *name;
parse_tree *pt;
cg_obj *cow;
pt_head *ph;
cv = cvec_i(argv, 0);
name = cv_string_get(cv);
if ((ph = cligen_ph_find(h, name)) != NULL){
if ((cow = cligen_ph_workpoint_get(ph)) != NULL)
pt = co_pt_get(cow);
else
pt = cligen_ph_parsetree_get(ph);
pt_print1(stderr, pt, 1);
}
return 0;
}
/*! Callback: Working point tree up to parent
*
* Format of argv:
* <treename>
*/
int
cligen_wp_up(cligen_handle h,
cvec *cvv,
cvec *argv)
{
cg_var *cv;
char *treename;
cg_obj *co;
pt_head *ph;
cv = cvec_i(argv, 0);
treename = cv_string_get(cv);
if ((ph = cligen_ph_find(h, treename)) != NULL &&
(co = cligen_ph_workpoint_get(ph)) != NULL)
cligen_ph_workpoint_set(ph, co_up(co));
return 0;
}
/*! Callback: Working point tree reset to top level
*
* Format of argv:
* <treename>
*/
int
cligen_wp_top(cligen_handle h,
cvec *cvv,
cvec *argv)
{
cg_var *cv;
char *treename;
pt_head *ph;
cv = cvec_i(argv, 0);
treename = cv_string_get(cv);
if ((ph = cligen_ph_find(h, treename)) != NULL)
cligen_ph_workpoint_set(ph, NULL);
return 0;
}