-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyacli.c
More file actions
3251 lines (2776 loc) · 74.9 KB
/
Copy pathyacli.c
File metadata and controls
3251 lines (2776 loc) · 74.9 KB
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
// $Id: yacli.c,v 4.14 2026/04/30 22:21:00 bbonev Exp $
//
// Copyright © 2015-2020 Boian Bonev (bbonev@ipacct.com) {{{
//
// This file is part of yacli - yet another command line interface library.
//
// yacli is free software: you can redistribute it and/or mowdify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// yacli is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with yacli. If not, see <http://www.gnu.org/licenses/>.
// }}}
// {{{ includes
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#include <ctype.h>
#include <regex.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <yacli.h>
// }}}
// {{{ definitions
#define BUFFER_STEP 1024
#define mymax(a,b) (((a)>(b))?(a):(b))
#define mymin(a,b) (((a)<(b))?(a):(b))
typedef enum {
IN_NORM, // normal input, check for ESC or IAC
IN_SEARCH, // incremental search in history
IN_MORE, // more prompt, used for paging
IN_C_X, // Ctrl-X sequence
} yacli_in_state;
typedef struct _cmnode {
void (*cb)(yacli *cli,int ac,char **cmd); // user callback for command
struct _cmnode *parent; // parent node in command tree
struct _cmnode *child; // child chain in sorted order
struct _cmnode *next; // sibling command(s) in sorted order
struct _cmnode *dyn; // dynamically generated command list (sorted)
yacli *cli; // pointer to owning cli (used for checks)
char *help; // help string | <abbreviation-for-regex>
char *cmd; // command text | @<numeric-id> | ^regex$
uint8_t isdyn:1; // command is dynamically generated; help and cb are in parent
} cmnode;
typedef struct _cmstack {
struct _cmstack *next;
struct _cmstack *prev;
cmnode *cmdt; // previous command tree
char *mode; // current mode short name
void *hint; // user hint for the current mode
} cmstack;
typedef struct _history {
struct _history *next; // next item in command history
struct _history *prev; // previous item in command history
char *command; // command text
} history;
typedef struct _filter_inst {
struct _filter_inst *next; // next filter instance (will receive our output)
struct _filter *fltr; // filter class
long private[4]; // private data, used for filter state
char *params; // command line parameters of the filter
char *buf; // buffer used to process output by lines
int bufsiz; // buffer allocation size
int buflen; // buffer content length
} filter_inst;
typedef struct _filter {
struct _filter *next; // next defined filter
yacli *cli; // containing cli
char *cmd; // filter command word
char *help; // filter help text
int (*feed)(filter_inst *flti,const char *line,int len); // callback for text feed
void (*done)(filter_inst *flti); // callback to flush buffered stuff
uint8_t allownext:1; // allow chaining other filters afterwards
} filter;
struct _yacli {
char *hostname; // hostname, used in prompt
char *level; // access level (#/$/>)
char *banner; // login banner
char *buffer; // current command buffer
char *savbuf; // saved buffer, while browsing history
char *sbuf; // incremental search buffer
char *rcmd; // search result pointer to command
char *morebuf; // buffered data for more
char *moreprompt; // more prompt text
char **parsedcmd; // command split into words (main style)
void *phint; // user defined hint (pointer)
cmnode *cmdt; // command tree
cmstack *cstack; // command stack with modes
char *modes; // all modes from stack
filter noopf; // noop passthrough filter
filter_inst noopi; // noop filter instance
filter *flts; // sorted defined filter list
filter_inst *fcmd; // applied filters for current command
history *hst; // command history (double linked)
history *hst_p; // pointer in history, NULL when not in history
yascreen *s; // screen used to render output
void (*cmdcb)(yacli *cli,const char *cmd,int code); // callback for each executed command
void (*listcb)(yacli *cli,void *ctx,int code); // legacy: dynamic list callback (no prefix hint)
void (*listcb2)(yacli *cli,void *ctx,int code,const char *prefix); // dynamic list callback with prefix hint for fast-skip
void (*parsedcb)(yacli *cli,int ac,char **cmd); // user callback for matching command
void (*ctrlzcb)(yacli *cli); // user callback to notify ctrl-z
yacli_in_state state; // input bytestream DFA state
yacli_loop retcode; // bytestream feed return code
int hint; // user defined hint (scalar)
int rpos; // matching command index where (0=first matching, 1=previous, etc)
int sx,sy; // terminal size
int lines; // line count between prompts, used for pagination
int bufpos; // buffer left scroll position
int buflen; // buffer data len (may not always be 0 terminated)
int bufsiz; // buffer size
int cursor; // cursor position
int morelen; // morebuf data len
int moresiz; // morebuf alloc size
int parsedcnt; // parsed command word count
int parsedsiz; // parsed command array size
int hstmax; // soft cap on history entries; 0 = unlimited
int hstcnt; // current history entry count
uint8_t more:1; // enable paged output
uint8_t redraw:1; // prompt needs redraw
uint8_t wastab:1; // control for double tab
uint8_t incmdcb:1; // flag when we are inside command callback
uint8_t istelnet:1; // use telnet IAC codes
uint8_t buffered:1; // buffered mode for more
uint8_t showtsize:1; // show terminal size on change
uint8_t clearmorel:1; // clear more prompt after line
uint8_t clearmorep:1; // clear more prompt after page
uint8_t clearmorec:1; // clear more prompt after cont
uint8_t clearmoreq:1; // clear more prompt after quit
uint8_t handlectrlz:1; // process ctrl-z shortcut
uint8_t ctrlzexeccmd:1; // when ctrl-z is hit, execute command in buffer
};
typedef enum {
MORE_PAGE,
MORE_LINE,
MORE_QUIT,
MORE_CONT,
MORE_CTRC,
MORE_NONE,
} more_type;
// }}}
inline void yacli_set_hint_i(yacli *cli,int hint) { // {{{
if (!cli)
return;
cli->hint=hint;
} // }}}
inline int yacli_get_hint_i(yacli *cli) { // {{{
if (!cli)
return 0;
return cli->hint;
} // }}}
inline void yacli_set_hint_p(yacli *cli,void *hint) { // {{{
if (!cli)
return;
cli->phint=hint;
} // }}}
inline void *yacli_get_hint_p(yacli *cli) { // {{{
if (!cli)
return NULL;
return cli->phint;
} // }}}
inline yascreen *yacli_get_screen(yacli *cli) { // {{{
if (!cli)
return NULL;
return cli->s;
} // }}}
static inline int yacli_regx(const char *reg,const char *str) { // {{{
regex_t re;
int ret;
if (regcomp(&re,reg,REG_EXTENDED)!=0)
return 1; // no match
ret=regexec(&re,str,0,NULL,0);
regfree(&re);
return ret;
} // }}}
inline void yacli_set_showtermsize(yacli *cli,int v) { // {{{
if (!cli)
return;
cli->showtsize=!!v;
} // }}}
static char myver[]="\0Yet another command line interface library (https://github.com/bbonev/yacli) $Revision: 4.14 $\n\n"; // {{{
// }}}
inline const char *yacli_ver(void) { // {{{
return myver;
} // }}}
static inline void yacli_buf_zeroterm(yacli *cli);
static inline int yacli_buf_inc(char **buf,int *siz,int *len,int add) { // {{{
// ensure *siz can hold *len+add bytes (strictly above, so a trailing null fits); growth rounded to BUFFER_STEP
// return 0 on success, non-zero on error
int needed,alloclen;
char *n;
if (add<=0)
return 0; // ok
needed=*len+add;
if (*siz>needed) // already enough
return 0;
alloclen=((needed+BUFFER_STEP)/BUFFER_STEP)*BUFFER_STEP; // next BUFFER_STEP-aligned size strictly above needed
if (alloclen<=*siz) // assure progress when needed lands exactly at a BUFFER_STEP boundary
alloclen=*siz+BUFFER_STEP;
n=calloc(1,alloclen);
if (!n) // no free mem, nothing more to do
return -1;
if (*len&&*buf) // copy only valid contents, not slack
memcpy(n,*buf,*len);
if (*buf)
free(*buf);
*buf=n;
*siz=alloclen;
return 0;
} // }}}
static inline void yacli_wr_buf(yacli *cli,const char *data,size_t len) { // {{{
if (!cli)
return;
if (yacli_buf_inc(&cli->morebuf,&cli->moresiz,&cli->morelen,len)) // alloc error
return;
if (len==0) // noop
return;
memcpy(cli->morebuf+cli->morelen,data,len);
cli->morelen+=len;
} // }}}
static inline int yacli_write_more(yacli *cli,const char *s,size_t len) { // {{{
size_t i;
if (!cli)
return -1;
if (cli->more&&!cli->buffered&&cli->lines+1>=cli->sy) { // if last command was exact but didn't toggle more prompt, do that now
cli->lines=0;
cli->redraw=1;
cli->buffered=1;
cli->state=IN_MORE;
}
if (cli->buffered) { // append to buffer in buffered mode
yacli_wr_buf(cli,s,len);
return len;
}
for (i=0;i<len;i++) { // count lines
if (s[i]=='\n')
cli->lines++;
if (cli->more&&cli->lines+1>=cli->sy) { // if exceeded, do partial output and switch to buffered mode
yascreen_write(cli->s,s,i+1);
if (len>i+1) { // switch to more mode, only if there is more text
cli->lines=0;
cli->redraw=1;
cli->buffered=1;
cli->state=IN_MORE;
yacli_wr_buf(cli,s+i+1,len-i-1);
}
return len;
}
}
yascreen_write(cli->s,s,len);
return len;
} // }}}
static inline int yacli_write_nof(yacli *cli,const char *s,size_t len) { // {{{
size_t i,j=0;
if (!cli)
return -1;
for (i=0;i<len;i++) {
if (s[i]=='\n') { // process a new line
if (i&&s[i-1]=='\r') { // just in case the sequence is already there
yacli_write_more(cli,s+j,i-j+1);
} else { // convert \n to \r\n
yacli_write_more(cli,s+j,i-j);
yacli_write_more(cli,"\r\n",2);
}
j=i+1;
}
}
if (j<len)
yacli_write_more(cli,s+j,len-j);
return len;
} // }}}
static inline int yacli_filter_feed_noop(filter_inst *fltr,const char *line,int len) { // {{{
if (!fltr)
return -1;
if (!fltr->fltr)
return -1;
if (!fltr->fltr->cli)
return -1;
return yacli_write_nof(fltr->fltr->cli,line,len);
} // }}}
static inline void yacli_filter_done_noop(filter_inst *fltr) { // {{{
if (!fltr)
return;
if (!fltr->fltr)
return;
if (!fltr->fltr->cli)
return;
return;
} // }}}
static inline void yacli_add_fcmd_s(yacli *cli,filter_inst *f) { // {{{
filter_inst **p;
if (!cli)
return;
if (f->next) {
#if DEBUG
fprintf(stderr,"yacli_add_fcmd_s called with list or not properly initialized fields\n");
#endif
return;
}
p=&cli->fcmd;
while (*p&&*p!=&cli->noopi) // try to keep the list always ending in noop node
p=&(*p)->next;
f->next=*p;
*p=f;
} // }}}
static inline void yacli_add_fcmd(yacli *cli,filter *fltr,char *params) { // {{{
filter_inst *f;
if (!cli)
return;
if (!fltr)
return;
if (!params)
return;
f=calloc(1,sizeof *f);
if (!f)
return;
f->next=NULL;
f->fltr=fltr;
f->params=strdup(params);
if (!f->params) {
free(f);
return;
}
yacli_add_fcmd_s(cli,f);
} // }}}
static inline void yacli_free_fcmd(yacli *cli) { // {{{
if (!cli)
return;
if (cli->fcmd&&cli->fcmd->fltr&&cli->fcmd->fltr->done)
cli->fcmd->fltr->done(cli->fcmd);
while (cli->fcmd) {
filter_inst *t;
t=cli->fcmd;
cli->fcmd=cli->fcmd->next;
if (t!=&cli->noopi) {
if (t->buf)
free(t->buf);
if (t->params)
free(t->params);
free(t);
}
}
yacli_add_fcmd_s(cli,&cli->noopi);
} // }}}
static inline void *yacli_add_filter(yacli *cli,const char *cmd,const char *help,int (*feed)(filter_inst *flti,const char *line,int len),void (*done)(filter_inst *flti),int allownext) { // {{{
filter **place,*t;
if (!cli)
return NULL;
if (!cmd)
return NULL;
place=&cli->flts;
while (*place) {
int cmp=strcmp(cmd,(*place)->cmd);
if (cmp==0) // duplicate filter
return NULL;
if (cmp<0) // found proper place
break;
place=&(*place)->next;
}
t=calloc(1,sizeof *t);
if (!t)
return NULL;
t->cli=cli;
t->cmd=strdup(cmd);
t->help=strdup(help?help:"");
t->allownext=!!allownext;
t->feed=feed;
t->done=done;
t->next=*place;
*place=t;
return t;
} // }}}
static inline int yacli_filter_feed_include(filter_inst *fltr,const char *line,int len) { // {{{
int i;
if (!fltr)
return -1;
if (!fltr->fltr)
return -1;
if (!fltr->fltr->cli)
return -1;
if (!fltr->next)
return -1;
if (!fltr->next->fltr)
return -1;
if (!fltr->next->fltr->feed)
return -1;
if (yacli_buf_inc(&fltr->buf,&fltr->bufsiz,&fltr->buflen,len+1)) // additional bytes beyond current contents (+1 for zero term)
return -1; // no memory
memcpy(fltr->buf+fltr->buflen,line,len);
fltr->buf[fltr->buflen+len]=0;
fltr->buflen+=len;
redo:
for (i=0;i<fltr->buflen;i++) {
if (fltr->buf[i]=='\n') {
fltr->buf[i]=0;
if (strstr(fltr->buf,fltr->params)) { // pass the line
fltr->buf[i]='\n';
fltr->next->fltr->feed(fltr->next,fltr->buf,i+1);
}
if (i+1<fltr->buflen)
memmove(fltr->buf,fltr->buf+i+1,fltr->buflen-i-1);
fltr->buflen-=i+1;
goto redo;
}
}
return len;
} // }}}
static inline void yacli_filter_done_include(filter_inst *fltr) { // {{{
if (!fltr)
return;
if (!fltr->fltr)
return;
if (!fltr->fltr->cli)
return;
if (fltr->buf&&fltr->buflen) {
if (strstr(fltr->buf,fltr->params)) { // pass the line
fltr->next->fltr->feed(fltr->next,fltr->buf,fltr->buflen); // pass remaining buffer, which does not contain \n
fltr->next->fltr->feed(fltr->next,"\n",1); // pass finishing \n
}
fltr->buflen=0;
}
fltr->next->fltr->done(fltr->next);
return;
} // }}}
static inline int yacli_filter_feed_exclude(filter_inst *fltr,const char *line,int len) { // {{{
int i;
if (!fltr)
return -1;
if (!fltr->fltr)
return -1;
if (!fltr->fltr->cli)
return -1;
if (!fltr->next)
return -1;
if (!fltr->next->fltr)
return -1;
if (!fltr->next->fltr->feed)
return -1;
if (yacli_buf_inc(&fltr->buf,&fltr->bufsiz,&fltr->buflen,len+1)) // additional bytes beyond current contents (+1 for zero term)
return -1; // no memory
memcpy(fltr->buf+fltr->buflen,line,len);
fltr->buf[fltr->buflen+len]=0;
fltr->buflen+=len;
redo:
for (i=0;i<fltr->buflen;i++) {
if (fltr->buf[i]=='\n') {
fltr->buf[i]=0;
if (!strstr(fltr->buf,fltr->params)) { // pass the line
fltr->buf[i]='\n';
fltr->next->fltr->feed(fltr->next,fltr->buf,i+1); // pass error code
}
if (i+1<fltr->buflen)
memmove(fltr->buf,fltr->buf+i+1,fltr->buflen-i-1);
fltr->buflen-=i+1;
goto redo;
}
}
return len;
} // }}}
static inline void yacli_filter_done_exclude(filter_inst *fltr) { // {{{
if (!fltr)
return;
if (!fltr->fltr)
return;
if (!fltr->fltr->cli)
return;
if (fltr->buf&&fltr->buflen) {
if (!strstr(fltr->buf,fltr->params)) { // pass the line
fltr->next->fltr->feed(fltr->next,fltr->buf,fltr->buflen); // pass remaining buffer, which does not contain \n
fltr->next->fltr->feed(fltr->next,"\n",1); // pass finishing \n
}
fltr->buflen=0;
}
fltr->next->fltr->done(fltr->next);
return;
} // }}}
static inline int yacli_filter_feed_count(filter_inst *fltr,const char *line,int len) { // {{{
int i;
if (!fltr)
return -1;
for (i=0;i<len;i++)
if (line[i]=='\n')
fltr->private[0]++;
return len;
} // }}}
static inline void yacli_filter_done_count(filter_inst *fltr) { // {{{
char s[200];
if (!fltr)
return;
if (!fltr->next)
return;
if (!fltr->next->fltr)
return;
if (!fltr->next->fltr->feed)
return;
if (!fltr->next->fltr->done)
return;
snprintf(s,sizeof s,"Line count: %ld\n",fltr->private[0]);
fltr->next->fltr->feed(fltr->next,s,strlen(s));
fltr->next->fltr->done(fltr->next);
return;
} // }}}
inline yacli *yacli_init(yascreen *s) { // {{{
static volatile int verfmt_state=0; // 0=untouched, 1=formatting in progress, 2=done; serializes the one-time reformat of myver
yacli *cli=calloc(1,sizeof *cli);
char *rev;
int vermaj,vermin;
if (!cli)
return NULL;
if (!s) {
free(cli);
return NULL;
}
cli->s=s;
// we use explicit flush, tell yascreen not to do it
yascreen_line_flush(cli->s,0);
if (__sync_bool_compare_and_swap(&verfmt_state,0,1)) { // first caller wins, reformats the static version string
rev=strstr(myver+1,"$Revision: ");
if (rev) {
sscanf(rev+strlen("$Revision: "),"%d.%d",&vermaj,&vermin);
vermaj+=vermin/100;
vermin=vermin%100;
memmove(myver,myver+1,strlen(myver+1)+1);
snprintf(rev-1,sizeof myver-(rev-1-myver),"%d.%02d\n\n",vermaj,vermin);
}
__sync_synchronize();
verfmt_state=2;
} else
while (verfmt_state!=2) // another thread is formatting; wait for it to finish before reading myver
__sync_synchronize();
cli->hostname=strdup("none");
if (!cli->hostname)
goto allocerror;
cli->cstack=NULL;
cli->modes=NULL;
cli->moreprompt=strdup("<<< more >>> [ enter=line | space=page | c=continue | q=quit ] <<< more >>>");
if (!cli->moreprompt)
goto allocerror;
cli->level=strdup("#");
if (!cli->level)
goto allocerror;
cli->banner=strdup(myver);
if (!cli->banner)
goto allocerror;
cli->sx=80;
cli->sy=25;
cli->buffer=malloc(BUFFER_STEP);
if (!cli->buffer)
goto allocerror;
cli->bufsiz=BUFFER_STEP;
cli->buflen=0;
cli->bufpos=0;
cli->cursor=0;
cli->state=IN_NORM;
cli->redraw=1;
cli->retcode=YACLI_LOOP;
cli->istelnet=0;
cli->showtsize=0;
cli->rcmd=NULL;
cli->rpos=0;
cli->wastab=0;
cli->lines=0;
cli->morebuf=strdup("");
if (!cli->morebuf)
goto allocerror;
cli->morelen=0;
cli->moresiz=0;
cli->more=1; // use paged output by default
cli->clearmorel=1; // remove more prompt when showing next line
cli->clearmoreq=0; // leave more prompt when quit is pressed
cli->clearmorec=1; // remove more prompt when continue to end of output
cli->clearmorep=0; // leave more prompt only after whole page for clarity
cli->parsedcmd=NULL;
cli->parsedcnt=0;
cli->parsedsiz=0;
cli->handlectrlz=0;
cli->ctrlzexeccmd=1;
cli->noopf.next=NULL;
cli->noopf.cli=cli;
cli->noopf.cmd="noop";
cli->noopf.help="";
cli->noopf.feed=yacli_filter_feed_noop;
cli->noopf.done=yacli_filter_done_noop;
cli->noopf.allownext=0;
cli->noopi.next=NULL;
cli->noopi.fltr=&cli->noopf;
cli->noopi.params=NULL;
cli->noopi.buf=NULL;
cli->noopi.bufsiz=0;
cli->noopi.buflen=0;
yacli_add_fcmd_s(cli,&cli->noopi);
yacli_add_filter(cli,"include","Filter output that contains the parameter text",yacli_filter_feed_include,yacli_filter_done_include,1);
yacli_add_filter(cli,"exclude","Filter output that contains the parameter text",yacli_filter_feed_exclude,yacli_filter_done_exclude,1);
yacli_add_filter(cli,"count","Display output line count",yacli_filter_feed_count,yacli_filter_done_count,0);
return cli;
allocerror:
if (cli->morebuf)
free(cli->morebuf);
if (cli->buffer)
free(cli->buffer);
if (cli->banner)
free(cli->banner);
if (cli->level)
free(cli->level);
if (cli->moreprompt)
free(cli->moreprompt);
if (cli->hostname)
free(cli->hostname);
free(cli);
return NULL;
} // }}}
static inline void yacli_free_parsed(yacli *cli) { // {{{
int i;
if (!cli)
return;
if (!cli->parsedcmd) {
cli->parsedcnt=0;
cli->parsedsiz=0;
return;
}
for (i=0;i<cli->parsedcnt;i++)
free(cli->parsedcmd[i]);
free(cli->parsedcmd);
cli->parsedcmd=NULL;
cli->parsedcnt=0;
cli->parsedsiz=0;
} // }}}
static inline void yacli_free_flts(yacli *cli) { // {{{
if (!cli)
return;
while (cli->flts) {
filter *f=cli->flts;
cli->flts=cli->flts->next;
if (f->cmd)
free(f->cmd);
if (f->help)
free(f->help);
free(f);
}
} // }}}
static inline void yacli_clear_parsed(yacli *cli) { // {{{
int i;
if (!cli)
return;
if (!cli->parsedcmd) {
cli->parsedcnt=0;
cli->parsedsiz=0;
return;
}
for (i=0;i<cli->parsedcnt;i++) {
free(cli->parsedcmd[i]);
cli->parsedcmd[i]=NULL;
}
cli->parsedcnt=0;
} // }}}
static inline void yacli_add_parsed(yacli *cli,const char *word) { // {{{
int step=BUFFER_STEP/sizeof(char *);
char *dw;
if (!cli)
return;
dw=strdup(word);
if (!dw)
return;
if (!cli->parsedcmd) {
cli->parsedcmd=calloc(sizeof(char *),step);
if (!cli->parsedcmd) { // memory alloc error
free(dw);
return;
}
cli->parsedcnt=0;
cli->parsedsiz=step;
}
if (cli->parsedcnt>=cli->parsedsiz) {
int cnt=cli->parsedcnt+2; // one for terminating NULL, one for the new item
char **nc=calloc(sizeof(char *),step*(cnt/step+1));
if (!nc) { // memory alloc error
free(dw);
return;
}
memcpy(nc,cli->parsedcmd,(cli->parsedcnt+1)*sizeof(char *));
free(cli->parsedcmd);
cli->parsedcmd=nc;
cli->parsedsiz=step*(cnt/step+1);
}
cli->parsedcmd[cli->parsedcnt++]=dw;
cli->parsedcmd[cli->parsedcnt]=NULL;
} // }}}
inline void yacli_set_more(yacli *cli,int on) { // {{{
if (!cli)
return;
cli->more=!!on;
} // }}}
inline void yacli_set_more_clear(yacli *cli,int ln,int pg,int co,int qu) { // {{{
if (!cli)
return;
cli->clearmorel=!!ln;
cli->clearmorep=!!pg;
cli->clearmorec=!!co;
cli->clearmoreq=!!qu;
} // }}}
inline void yacli_set_ctrlz(yacli *cli,int on) { // {{{
if (!cli)
return;
cli->handlectrlz=!!on;
} // }}}
inline void yacli_set_ctrlz_exec(yacli *cli,int on) { // {{{
if (!cli)
return;
cli->ctrlzexeccmd=!!on;
} // }}}
inline void yacli_set_banner(yacli *cli,const char *banner) { // {{{
char *t;
if (!cli)
return;
if (!banner)
return;
t=cli->banner;
cli->banner=strdup(banner);
if (!cli->banner)
cli->banner=t;
else
free(t);
} // }}}
inline void yacli_set_level(yacli *cli,const char *level) { // {{{
char *t;
if (!cli)
return;
if (!level)
return;
t=cli->level;
cli->level=strdup(level);
if (!cli->level)
cli->level=t;
else
free(t);
cli->redraw=1;
} // }}}
inline void yacli_set_hostname(yacli *cli,const char *hostname) { // {{{
char *t;
if (!cli)
return;
if (!hostname)
return;
t=cli->hostname;
cli->hostname=strdup(hostname);
if (!cli->hostname)
cli->hostname=t;
else
free(t);
cli->redraw=1;
} // }}}
static inline void yacli_more_prompt(yacli *cli) { // {{{
if (!cli)
return;
yascreen_clearln(cli->s);
yascreen_puts(cli->s,"\r");
yascreen_puts(cli->s,cli->moreprompt);
yascreen_write(cli->s,"",0);
} // }}}
static inline void yacli_more_clear_prompt(yacli *cli,more_type t) { // {{{
if (!cli)
return;
switch (t) {
case MORE_LINE:
if (cli->clearmorel) {
yascreen_clearln(cli->s);
yascreen_puts(cli->s,"\r"); // clear more prompt
} else
yascreen_puts(cli->s,"\r\n"); // go next line
break;
case MORE_PAGE:
if (cli->clearmorep) {
yascreen_clearln(cli->s);
yascreen_puts(cli->s,"\r"); // clear more prompt
} else
yascreen_puts(cli->s,"\r\n"); // go next line
break;
case MORE_QUIT:
if (cli->clearmoreq) {
yascreen_clearln(cli->s);
yascreen_puts(cli->s,"\r"); // clear more prompt
} else
yascreen_puts(cli->s," quit\r\n"); // print the reason and go next line
break;
case MORE_CONT:
if (cli->clearmorec) {
yascreen_clearln(cli->s);
yascreen_puts(cli->s,"\r"); // clear more prompt
} else
yascreen_puts(cli->s,"\r\n"); // go next line
break;
case MORE_CTRC:
yascreen_puts(cli->s," ^C\r\n"); // print the reason and go next line
break;
case MORE_NONE:
break;
}
yascreen_write(cli->s,"",0);
} // }}}
static inline int yacli_print_nof(yacli *cli,const char *format,...) { // {{{
va_list ap;
char *ns;
int size;
if (!cli)
return -1;
va_start(ap,format);
size=vasprintf(&ns,format,ap);
va_end(ap);
if (size==-1) // some error, nothing more to do
return size;
yacli_write_nof(cli,ns,strlen(ns));
free(ns);
return size;
} // }}}
inline int yacli_write(yacli *cli,const char *s,size_t len) { // {{{
if (!cli)
return -1;
if (!cli->fcmd)
return -1;
if (!cli->fcmd->fltr)
return -1;
if (!cli->fcmd->fltr->feed)
return -1;
return cli->fcmd->fltr->feed(cli->fcmd,s,len);
} // }}}
inline int yacli_print(yacli *cli,const char *format,...) { // {{{
va_list ap;
char *ns;
int size;
if (!cli)
return -1;
if (!cli->fcmd)
return -1;
if (!cli->fcmd->fltr)
return -1;
if (!cli->fcmd->fltr->feed)
return -1;
va_start(ap,format);
size=vasprintf(&ns,format,ap);