-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcligen_io.c
653 lines (609 loc) · 17.9 KB
/
cligen_io.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
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
/*
CLI generator input/output support functions.
***** 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 "cligen_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <string.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <errno.h>
#include <signal.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_handle.h"
#include "cligen_result.h"
#include "cligen_read.h"
#include "cligen_print.h"
#include "cligen_io.h"
#include "cligen_getline.h"
/*
* Constants
*/
/* When doing query (?) how large left margin before first command.
* For example, the space before foo
* @code
* > set ?
* foo <--- number of spaces left of "foo"
* @endcode
*
*/
#define CLIGEN_HELP_LEFT_MARGIN 3
/*
* Local variables
*/
static int D_LINES=0; /* XXX: global */
static int D_COLUMNS=0; /* XXX: global */
static int PIPE_OUTPUT_SOCKET = -1;
/*! Get output pipe socket
*/
int
cli_pipe_output_socket_get(int *s)
{
if (s){
*s = PIPE_OUTPUT_SOCKET;
}
return 0;
}
/*! Set output pipe socket
*/
int
cli_pipe_output_socket_set(int s)
{
PIPE_OUTPUT_SOCKET = s;
return 0;
}
/*! Reset cligen_output to initial state
*
* For new output or when 'q' is pressed that sets d_line to -1
*/
int
cli_output_reset(void)
{
D_LINES = 0;
D_COLUMNS = 0;
return 0;
}
int
cli_output_status(void)
{
return D_LINES;
}
/*! cligen_output support function for the actual scrolling
*
* @param[in] f Open stdio FILE pointer
* @param[in] ibuf Input buffer containing all chars to be printed including 0 or many \n
* @param[in] linelen Length of single printable line, less than or equal to width of
* terminal window, unless the terminal window is 0
* @param[in] term_rows Height of terminal window
* @see cligen_output
*/
static int
cligen_output_scroll(FILE *f,
char *ibuf,
ssize_t linelen,
int term_rows)
{
int retval = -1;
char *ibend;
char *ib0; /* Moving window start */
char *ib1; /* Moving window end */
char *ibcr;
int c;
char *linebuf = NULL;
ssize_t remain;
ib0 = ibuf;
ib1 = ibuf;
ibend = ibuf + strlen(ibuf);
/* A terminal line */
if ((linebuf = malloc(linelen+1)) == NULL)
goto done;
remain = linelen - D_COLUMNS;
while (ib1 < ibend && D_LINES >= 0){
/* Four cases:
* 1. There is a CR in [ib0,ibend]
* 1a) greater than remaining: (inc D_LINE)
* 1b) less than or equal to remain: Only case where line has (terminating) CR
* 2. No CR
* 2a) greater than remain: (inc D_LINE)
* 2b) less than or equal to remain:
*/
if ((ibcr = strstr(ib0, "\n")) != NULL){
if ((ibcr - ib0) >= remain){
ib1 = ib0 + remain; /* 1a */
D_LINES++;
remain = linelen;
}
else{
ib1 = ibcr+1; /* 1b */
D_LINES++;
remain = linelen;
}
}
else if (ibend - ib0 >= remain){
ib1 = ib0 + remain; /* 2a */
D_LINES++;
remain = linelen;
}
else{
remain -= ibend-ib1;
ib1 = ibend; /* 2b */
}
if (ib0 == ib1)
break;
memcpy(linebuf, ib0, (ib1-ib0));
linebuf[(ib1-ib0)] = '\0';
fprintf(f, "%s", linebuf);
ib0 = ib1;
if (D_LINES >= (term_rows -1)){
gl_char_init();
fprintf(f, "--More--");
fflush(f);
c = fgetc(stdin);
if (c == '\n')
D_LINES--;
else if (c == ' ')
D_LINES = 0;
else if (c == 'q' || c == 3) /* ^c */
D_LINES = -1;
else if (c == '?')
fprintf(f, "Press CR for one more line, SPACE for next page, q to quit\n");
else
D_LINES = 0;
fprintf(f, " ");
gl_char_cleanup();
}
}
D_COLUMNS = linelen-remain;
retval = 0;
done:
if (linebuf)
free(linebuf);
return retval;
}
/*! CLIgen output function. All printf-style output should be made via this function.
*
* Note only scrolling for stdout
* It deals with formatting, page breaks, etc, (but only if f is stdout)
* @param[in] f Open stdio FILE pointer
* @param[in] template... See man printf(3)
*
* @note: There has also been a discussion on the use of handles in this code (it relies on a
* global variable _terminalrows and D_LINES). However, the signature needs to be the same as
* fprintf in order to make compatible printing code.
*
* @note Related to the handle question is the use of the global variable D_LINES in order to
* handle multiple calls (such as in a loop) to cligen_output. However, this assumes D_LINES
* is reset before a new usage to D_LINES using the function cli_output_reset(). This therefore
* be done between invocations. Especially this applies to 'q'. Further, to react to quit, you need
* to poll cli_output_status() < 0.
*
* @note: There has been a debate whether this function is the right solution to the
* pageing problem of CLIgen or not.
* (1) On the one hand, a less/more like sub-process could be forked and stdout piped to this
* sub-process. This would handle all prints to stdout, instead of relying on all output
* functions using this function and not printf.
* (2) On the other hand, this gives a slim and simple solution with smaller footprint (no forked
* process), but all output functions need to pass though this code.
* For now (2) is used and extended also for clixon functions. However (1) could still be
* implemented as an option.
*
* Chop up input buffers as follows:
* in: 01234567890123456789\n0123456
* width: |----------|
* line1: 012345678901
* line2: 23456789
* line3: 0123456
* @see cligen_output_basic no pipe and no vararg
*/
int
cligen_output(FILE *f,
const char *template,
... )
{
int retval = -1;
va_list args;
char *inbuf = NULL;
ssize_t linelen;
int term_rows;
int term_width;
ssize_t inbuflen;
int s = -1;
int paging;
/* Get terminal width and height, note discussion regarding NULL handle */
term_rows = cligen_terminal_rows(NULL);
term_width = cligen_terminal_width(NULL);
paging = cligen_paging_get(NULL);
/* form a string in inbuf from all args */
va_start(args, template);
inbuflen = vsnprintf(NULL, 0, template, args);
va_end(args);
if ((inbuf = malloc(inbuflen+1)) == NULL)
goto done;
va_start(args, template);
vsnprintf(inbuf, inbuflen+1, template, args);
va_end(args);
if (term_width > 0)
linelen = term_width;
else
linelen = inbuflen;
if (cli_pipe_output_socket_get(&s) < 0)
goto done;
if (s != -1){
if (write(s, inbuf, inbuflen) < 0){
perror("cligen_output write on pipe socket");
close(s);
cli_pipe_output_socket_set(-1);
goto done;
}
}
else{
/* if writing to stdout, format output
*/
if (paging && term_rows && (f == stdout)){
if (cligen_output_scroll(f, inbuf, linelen, term_rows) < 0)
goto done;
}
else{
fprintf(f, "%s", inbuf);
}
fflush(f);
}
retval = 0;
done:
if (inbuf)
free(inbuf);
return retval;
}
/*! Same as cligen_output, but no vararg and no pipe-output
*
* @param[in] f Open stdio FILE pointer
* @param[in] template... See man printf(3)
* @see cligen_output with vararg and pipe output
*/
int
cligen_output_basic(FILE *f,
char *inbuf,
size_t inbuflen)
{
int retval = -1;
ssize_t linelen;
int term_rows;
int term_width;
int paging;
/* Get terminal width and height, note discussion regarding NULL handle */
term_rows = cligen_terminal_rows(NULL);
term_width = cligen_terminal_width(NULL);
paging = cligen_paging_get(NULL);
if (term_width > 0)
linelen = term_width;
else
linelen = inbuflen;
/* if writing to stdout, format output
*/
if (paging && term_rows && (f == stdout)){
if (cligen_output_scroll(f, inbuf, linelen, term_rows) < 0)
goto done;
}
else{
fprintf(f, "%s", inbuf);
}
fflush(f);
retval = 0;
done:
return retval;
}
#ifdef notyet
/*
* Yes/No question. Returns 1 for yes and 0 for no.
*/
int
cli_yesno(const char *fmt, ...)
{
va_list ap;
char buf[1024];
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf)-1, fmt, ap);
va_end(ap);
printf("%s [yes/no]: ", buf);
if (cli_getln(buf, sizeof(buf)) == 0)
if (strlen(buf) && !strncasecmp(buf, "yes", strlen(buf)))
return 1;
return 0;
}
#endif
#if CLIGEN_REGFD
/* The following three callback functions are just wrappers in order to
not expose getline to external interface */
int
cligen_regfd(int fd, cligen_fd_cb_t *cb, void *arg)
{
return gl_regfd(fd, cb, arg);
}
int
cligen_unregfd(int fd)
{
return gl_unregfd(fd);
}
#endif /* CLIGEN_REGFD */
void
cligen_redraw(cligen_handle h)
{
gl_redraw(h);
}
/*! Register a suspend (^Z) function hook
*/
int
cligen_susp_hook(cligen_handle h,
cligen_susp_cb_t *fn)
{
gl_susp_hook = fn; /* XXX global */
return 0;
}
/*! Register an interrupt hook, called if read() interrupted with (some) SIG
*/
int
cligen_interrupt_hook(cligen_handle h,
cligen_interrupt_cb_t *fn)
{
gl_interrupt_hook = fn;
return 0;
}
/*! Register extra exit characters (in addition to ctrl-c)
*/
void
cligen_exitchar_add(cligen_handle h,
char c)
{
gl_exitchar_add(c); /* XXX global */
}
/*! Display multi help lines on query (?)
*
* Function handles multiple options on how to display help strings at query (?)
* This includes indentation, limit on lines, truncation, etc.
* @param[in] h Cligen handle
* @param[in] fout File to print to, eg stdout
* @param[in] column_width Space for first, command width
* @param[in] ch Help command and string struct
*/
static int
print_help_line(cligen_handle h,
FILE *fout,
int column_width,
struct cligen_help *ch)
{
int retval = -1;
cg_var *cv = NULL;
int w;
char *str;
char *str2;
int j;
int linesmax;
int termwidth;
int truncate;
/* First print command */
fprintf(fout, " %*s", -column_width, ch->ch_cmd);
/* Then print help */
if (ch->ch_helpvec && cvec_len(ch->ch_helpvec)){
linesmax = cligen_helpstring_lines(h);
truncate = cligen_helpstring_truncate(h);
termwidth = cligen_terminal_width(h);
j = 0;
while ((cv = cvec_each(ch->ch_helpvec, cv)) != NULL &&
(linesmax==0 || j<linesmax)){
w = termwidth - column_width - CLIGEN_HELP_LEFT_MARGIN;
str = cv_string_get(cv);
if (j > 0) /* skip first line */
fprintf(fout, " %*s", -column_width, "");
if (truncate == 0 ||
strlen(str) < w){
fprintf(fout, " %*s", -w, str);
}
else {
if ((str2 = strdup(str)) == NULL)
goto done;
str2[w] = '\0';
fprintf(fout, " %*s", -w, str2);
free(str2);
str2 = NULL;
}
fprintf(fout, "\n");
j++;
}
}
else
fprintf(fout, "\n");
retval = 0;
done:
return retval;
}
/*! Check equivalence of two cligen help structs
*
* Utility function for printing commands and help. If two are equal, do not repeat them, just
* print once. If "help" is set, also consider helt string, or at least first line of text.
* The difference is whether only commands are written (eg in columns) or written in lines where
* also helps trings are shown.
* @param[in] ch0 First CLIgen help struct
* @param[in] ch1 Second CLIgen help struct
* @param[in] help If 0, only compare commands, if 1, also compare helstrings (first row)
* @retval 1 Equal
* @retval 0 Not equal
*/
int
cligen_help_eq(struct cligen_help *ch0,
struct cligen_help *ch1,
int help)
{
char *cmd0 = ch0->ch_cmd;
char *cmd1 = ch1->ch_cmd;
cvec *help0 = ch0->ch_helpvec;
cvec *help1 = ch1->ch_helpvec;
cg_var *cv0;
cg_var *cv1;
if (cmd0 == NULL && cmd1 == NULL)
return 1;
if (cmd0 == NULL || cmd1 == NULL)
return 0;
if (strcmp(cmd0, cmd1) != 0)
return 0;
if (help == 0)
return 1;
/* Commands are equal, check help string */
if (help0 == NULL && help1 == NULL)
return 1;
if (help0 == NULL || help1 == NULL)
return 0;
/* Get first line only as equality check */
cv0 = cvec_i(help0, 0);
cv1 = cvec_i(help1, 0);
#if 0 // help0/1 cannot be NULL
if (cv0 == NULL && help1 == NULL)
return 1;
if (cv0 == NULL || help1 == NULL)
return 0;
#endif
return strcmp(cv_string_get(cv0), cv_string_get(cv1)) == 0;
}
/* Dont actually free it */
int
cligen_help_clear(struct cligen_help *ch)
{
if (ch == NULL)
return 0;
if (ch->ch_cmd)
free(ch->ch_cmd);
if (ch->ch_helpvec)
cvec_free(ch->ch_helpvec);
memset(ch, 0, sizeof(*ch));
return 0;
}
/*! Print help lines for subset of a parsetree vector
*
* @param[in] fout File to print to, eg stdout
* @param[in] ptvec Cligen parse-node vector
* @param[in] matchvec Array of indexes into ptvec to match (the subset)
* @param[in] matchlen Length of matchvec
* @see show_help_columns
*/
int
print_help_lines(cligen_handle h,
FILE *fout,
parse_tree *ptmatch)
{
int retval = -1;
cg_obj *co;
char *cmd;
int i;
cbuf *cb = NULL;
struct cligen_help *chvec = NULL;
struct cligen_help *ch;
int maxlen = 0;
int nrcmd = 0;
int column_width;
unsigned int len;
if ((cb = cbuf_new()) == NULL)
return -1;
/* Go through match vector and collect commands and helps */
len = pt_len_get(ptmatch);
if ((chvec = calloc(len, sizeof(struct cligen_help))) ==NULL){
perror("calloc");
goto done;
}
for (i=0; i<pt_len_get(ptmatch); i++){
co = pt_vec_i_get(ptmatch, i);
if (co == NULL || co->co_command == NULL)
continue;
cmd = NULL;
switch(co->co_type){
case CO_VARIABLE:
cbuf_reset(cb);
cov2cbuf(cb, co, 1);
cmd = cbuf_get(cb);
break;
case CO_COMMAND:
cmd = co->co_command;
break;
default:
continue;
break;
}
ch = &chvec[nrcmd];
if ((ch->ch_cmd = strdup(cmd)) == NULL)
goto done;
if (co->co_helpstring && cligen_txt2cvv(co->co_helpstring, &ch->ch_helpvec) < 0)
goto done;
if (nrcmd && cligen_help_eq(&chvec[nrcmd-1], ch, 1) == 1){
cligen_help_clear(ch);
continue;
}
nrcmd++;
/* Compute longest command */
maxlen = strlen(cmd)>maxlen?strlen(cmd):maxlen;
}
maxlen++;
column_width = maxlen<COLUMN_MIN_WIDTH?COLUMN_MIN_WIDTH:maxlen;
/* Actually print */
for (i = 0; i<nrcmd; i++){
ch = &chvec[i];
if (print_help_line(h, fout, column_width, ch) < 0)
goto done;
}
fflush(fout);
retval = 0;
done:
if (chvec){
for (i=0; i<nrcmd; i++)
cligen_help_clear(&chvec[i]);
free(chvec);
}
if (cb)
cbuf_free(cb);
return retval;
}
/*! Print top-level help (all commands) of a parse-tree
*
* @param[in] fout File to print to, eg stdout
* @param[in] pt Cligen parse-tree
*/
int
cligen_help(cligen_handle h,
FILE *fout,
parse_tree *pt)
{
return print_help_lines(h, fout, pt);
}