-
Notifications
You must be signed in to change notification settings - Fork 27
/
complete.c
1152 lines (1019 loc) · 27.9 KB
/
complete.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
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
/* From: $OpenBSD: /usr/src/usr.bin/ftp/complete.c,v 1.19 2006/06/23 20:35:25 steven Exp $ */
/*-
* Copyright (c) 1997 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Luke Mewburn.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <err.h>
#include <errno.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <sys/param.h>
#include <sys/tty.h>
#include <unistd.h>
#include "editing.h"
#include "stringlist.h"
#include "externs.h"
#define ttyout stdout
#define ttyin stdin
unsigned char complete(EditLine *, char **, size_t);
static int comparstr(const void *, const void *);
static unsigned char complete_ambiguous(char *, int, StringList *, EditLine *,
char *);
static unsigned char complete_command(char *, int, EditLine *, char **, int);
static unsigned char complete_subcommand(char *, int, EditLine *, char **, int);
static unsigned char complete_local(char *, int, EditLine *);
static unsigned char complete_ifname(char *, int, EditLine *);
static unsigned char complete_ifgroup(char *, int, EditLine *);
static unsigned char complete_ifbridge(char *, int, EditLine *);
static unsigned char complete_rtable(char *, int, EditLine *);
static unsigned char complete_environment(char *, int, EditLine *, int);
static unsigned char complete_nocmd(struct ghs *, char *, int, EditLine *,
char **, int, int);
static unsigned char complete_docmd(struct ghs *, char *, int, EditLine *,
char **, int, int);
static unsigned char complete_doint(char *, int, EditLine *, char **, int, int);
static unsigned char complete_noint(char *, int, EditLine *, char **, int, int);
static unsigned char complete_args(struct ghs *, char *, int, EditLine *,
char **, int, int);
static void list_vertical(StringList *);
unsigned char complt_c(EditLine *, int);
unsigned char complt_i(EditLine *, int);
unsigned char exit_i(EditLine *, int);
static int
comparstr(const void *a, const void *b)
{
return (strcmp(*(char **)a, *(char **)b));
}
/*
* Determine if complete is ambiguous. If unique, insert.
* If no choices, error. If unambiguous prefix, insert that.
* Otherwise, list choices. words is assumed to be filtered
* to only contain possible choices.
* Args:
* word word which started the match
* list list by default
* words stringlist containing possible matches
* sep separator to insert after completed word; usually " "
*/
static unsigned char
complete_ambiguous(char *word, int list, StringList *words, EditLine *el,
char *sep)
{
char insertstr[MAXPATHLEN];
char *lastmatch;
int i, j;
size_t matchlen, wordlen;
wordlen = strlen(word);
if (words->sl_cur == 0)
return (CC_ERROR); /* no choices available */
if (words->sl_cur == 1) { /* only one choice available */
(void)strlcpy(insertstr, words->sl_str[0], sizeof insertstr);
(void)strlcat(insertstr, sep, sizeof insertstr);
if (el_insertstr(el, insertstr + wordlen) == -1)
return (CC_ERROR);
else
return (CC_REFRESH);
}
if (!list) {
matchlen = 0;
if ((lastmatch = words->sl_str[0]))
matchlen = strlen(lastmatch);
for (i = 1 ; i < words->sl_cur ; i++) {
for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
if (lastmatch[j] != words->sl_str[i][j])
break;
if (j < matchlen)
matchlen = j;
}
if (matchlen > wordlen) {
(void)strlcpy(insertstr, lastmatch, matchlen+1);
if (el_insertstr(el, insertstr + wordlen) == -1)
return (CC_ERROR);
else
/*
* XXX: really want CC_REFRESH_BEEP
*/
return (CC_REFRESH);
}
}
putc('\n', ttyout);
qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
list_vertical(words);
return (CC_REDISPLAY);
}
/*
* Complete a command
*/
static unsigned char
complete_command(char *word, int list, EditLine *el, char **table, int stlen)
{
char **c;
struct ghs *ghs;
StringList *words;
size_t wordlen;
unsigned char rv;
if (table == NULL)
return(CC_ERROR);
words = sl_init();
wordlen = strlen(word);
for (c = table; *c != NULL; c = (char **)((char *)c + stlen)) {
ghs = (struct ghs *)c;
if (wordlen > strlen(ghs->name))
continue;
if (strncmp(word, ghs->name, wordlen) == 0)
sl_add(words, ghs->name);
}
rv = complete_ambiguous(word, list, words, el, " ");
sl_free(words, 0);
return (rv);
}
/*
* Complete a (sub)command
*/
static unsigned char
complete_subcommand(char *word, int list, EditLine *el, char **table, int stlen)
{
struct ghs *ghs = NULL;
if (table == NULL)
return(CC_ERROR);
ghs = (struct ghs *)genget(margv[cursor_argc-1], table, stlen);
if (ghs == 0 || Ambiguous(ghs))
return(CC_ERROR);
/*
* XXX completion lists that hit subcommand tables don't get more than
* the first CMPL arg tested in complete_args as long as the level
* 0 is passed to complete_args
*/
return(complete_args(ghs, word, list, el, table, stlen, 0));
}
/*
* Complete a local file
*/
static unsigned char
complete_local(char *word, int list, EditLine *el)
{
StringList *words;
char dir[MAXPATHLEN];
char *file;
DIR *dd;
struct dirent *dp;
unsigned char rv;
if ((file = strrchr(word, '/')) == NULL) {
dir[0] = '.';
dir[1] = '\0';
file = word;
} else {
if (file == word) {
dir[0] = '/';
dir[1] = '\0';
} else {
(void)strlcpy(dir, word, (size_t)(file - word) + 1);
}
file++;
}
if ((dd = opendir(dir)) == NULL)
return (CC_ERROR);
words = sl_init();
for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
continue;
if (strlen(file) > dp->d_namlen)
continue;
if (strncmp(file, dp->d_name, strlen(file)) == 0) {
char *tcp;
tcp = strdup(dp->d_name);
if (tcp == NULL)
errx(1, "Can't allocate memory for local dir");
sl_add(words, tcp);
}
}
closedir(dd);
rv = complete_ambiguous(file, list, words, el, " ");
sl_free(words, 1);
return (rv);
}
static unsigned char
complete_showhelp(char *word, EditLine *el, char **table, int stlen,
char *cmdname, int vertical)
{
char insertstr[MAXPATHLEN];
char **c;
struct ghs *ghs;
StringList *cmdlist;
StringList *helplist;
int i;
size_t wordlen;
int cmdlen, max_cmdmlen = 0;
cmdlist = sl_init();
helplist = sl_init();
wordlen = strlen(word);
for (c = table; *c != NULL; c = (char **)((char *)c + stlen)) {
ghs = (struct ghs *)c;
if (wordlen > strlen(ghs->name))
continue;
if (word[0] == '<' || word[0] == '[')
continue;
if (strncmp(word, ghs->name, wordlen) == 0) {
sl_add(cmdlist, ghs->name);
sl_add(helplist, ghs->help);
cmdlen = strlen(ghs->name);
if (cmdlen > max_cmdmlen)
max_cmdmlen = cmdlen;
}
}
/*
* If we match a non-arbitrary parameter (which are not enclosed in
* brackets, "<...>" or "[...]") then we can complete this parameter.
*/
if (cmdlist->sl_cur == 1 && cmdlist->sl_str[0][0] != '<' &&
cmdlist->sl_str[0][0] != '[' && cmdlist->sl_str[0][0] != '^') {
(void)strlcpy(insertstr, cmdlist->sl_str[0], sizeof insertstr);
(void)strlcat(insertstr, " ", sizeof insertstr);
if (el_insertstr(el, insertstr + wordlen) == -1)
return (CC_ERROR);
else
return (CC_REFRESH);
}
if (cmdlist->sl_cur > 0)
putc('\n', ttyout);
if (vertical)
list_vertical(cmdlist);
else {
for (i = 0 ; i < cmdlist->sl_cur ; i++) {
if (i < helplist->sl_cur &&
helplist->sl_str[i][0] != '\0') {
/*
* Command help strings beginning with '^'
* are supposed to be printed literally.
*/
if (cmdlist->sl_str[i][0] == '^') {
fprintf(ttyout, " %-*s # %s\n",
max_cmdmlen,
&cmdlist->sl_str[i][1],
helplist->sl_str[i]);
} else {
fprintf(ttyout, " %s %-*s # %s\n",
cmdname, max_cmdmlen,
cmdlist->sl_str[i],
helplist->sl_str[i]);
}
} else {
fprintf(ttyout, " %s %s\n", cmdname,
cmdlist->sl_str[i]);
}
}
}
sl_free(cmdlist, 0);
sl_free(helplist, 0);
return (CC_REDISPLAY);
}
unsigned char
exit_i(EditLine *el, int ch)
{
printf("\n");
return CC_EOF;
}
unsigned char
complt_i(EditLine *el, int ch)
{
return(complete(el, (char **)whichlist, sizeof(struct intlist)));
}
unsigned char
complt_c(EditLine *el, int ch)
{
return(complete(el, (char **)cmdtab, sizeof(struct cmd)));
}
unsigned char
complete_ifname(char *word, int list, EditLine *el)
{
StringList *words;
size_t wordlen;
unsigned char rv;
const char *status_cmd = "status";
struct if_nameindex *ifn_list, *ifnp;
words = sl_init();
wordlen = strlen(word);
if ((ifn_list = if_nameindex()) == NULL)
return 0;
for (ifnp = ifn_list; ifnp->if_name != NULL; ifnp++) {
if (wordlen > strlen(ifnp->if_name))
continue;
if (strncmp(word, ifnp->if_name, wordlen) == 0)
sl_add(words, ifnp->if_name);
}
/* Handle the pseudo command "show interface status". */
if (margc >= 2 && isprefix(margv[0], "show") &&
isprefix(margv[1], "interface") &&
wordlen <= strlen(status_cmd) &&
strncmp(word, status_cmd, wordlen) == 0) {
char *s = strdup(status_cmd);
if (s == NULL)
err(1, "strdup");
sl_add(words, s);
}
rv = complete_ambiguous(word, list, words, el, " ");
if_freenameindex(ifn_list);
sl_free(words, 0);
return (rv);
}
unsigned char
complete_ifgroup(char *word, int list, EditLine *el)
{
StringList *words;
size_t wordlen;
unsigned char rv;
struct ifgroupreq ifgr;
struct ifg_req *ifg;
int ifs;
u_int len, ngroups, i;
words = sl_init();
wordlen = strlen(word);
bzero(&ifgr, sizeof(ifgr));
if ((ifs = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("%% complete_ifgroup: %s\n", strerror(errno));
return(1);
}
if (ioctl(ifs, SIOCGIFGLIST, (caddr_t)&ifgr) == -1) {
printf("%% SIOCGIFGLIST: %s\n", strerror(errno));
close(ifs);
return(1);
}
len = ifgr.ifgr_len;
ifgr.ifgr_groups = calloc(1, len);
if (ifgr.ifgr_groups == NULL) {
printf("%% calloc: %s\n", strerror(errno));
close(ifs);
return(1);
}
if (ioctl(ifs, SIOCGIFGLIST, (caddr_t)&ifgr) == -1) {
printf("%% SIOCGIFGLIST: %s\n", strerror(errno));
free(ifgr.ifgr_groups);
close(ifs);
return(1);
}
ngroups = len / sizeof(ifgr.ifgr_groups[0]);
for (i = 0; i < ngroups; i++) {
ifg = &ifgr.ifgr_groups[i];
if (wordlen > strlen(ifg->ifgrq_group))
continue;
if (strncmp(word, ifg->ifgrq_group, wordlen) == 0)
sl_add(words, ifg->ifgrq_group);
}
rv = complete_ambiguous(word, list, words, el, " ");
sl_free(words, 0);
free(ifgr.ifgr_groups);
close(ifs);
return (rv);
}
unsigned char
complete_ifbridge(char *word, int list, EditLine *el)
{
StringList *words;
size_t wordlen;
unsigned char rv;
struct if_nameindex *ifn_list, *ifnp;
int ifs;
words = sl_init();
wordlen = strlen(word);
if ((ifn_list = if_nameindex()) == NULL)
return 0;
if ((ifs = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("%% complete_ifbridge: %s\n", strerror(errno));
return(1);
}
for (ifnp = ifn_list; ifnp->if_name != NULL; ifnp++) {
if (wordlen > strlen(ifnp->if_name))
continue;
if (!is_bridge(ifs, ifnp->if_name))
continue;
if (strncmp(word, ifnp->if_name, wordlen) == 0)
sl_add(words, ifnp->if_name);
}
rv = complete_ambiguous(word, list, words, el, " ");
if_freenameindex(ifn_list);
sl_free(words, 0);
close(ifs);
return (rv);
}
unsigned char
complete_rtable(char *word, int list, EditLine *el)
{
StringList *words, *rtables;
size_t wordlen = strlen(word);
int i, rv = CC_ERROR;
char *s = NULL;
words = sl_init();
rtables = sl_init();
if (db_select_rtable_rtables(rtables) < 0) {
printf("%% database failure select rtables rtable\n");
goto done;
}
/*
* Routing table 0 always exists even if not created by nsh
* and is never present in the database.
*/
s = strdup("0");
if (s == NULL) {
printf("%% strdup: %s", strerror(errno));
goto done;
}
sl_add(words, s);
for (i = 0; i < rtables->sl_cur; i++) {
char *rtable = rtables->sl_str[i];
if (wordlen > strlen(rtable))
continue;
if (strncmp(word, rtable, wordlen) == 0)
sl_add(words, rtable);
}
rv = complete_ambiguous(word, list, words, el, " ");
done:
sl_free(rtables, 1);
sl_free(words, 0);
free(s);
return (rv);
}
static unsigned char
complete_environment(char *word, int dolist, EditLine *el, int set)
{
StringList *words;
extern char **environ;
char **ep, *eq, *name;
size_t wordlen = strlen(word);
int rv = CC_ERROR;
words = sl_init();
for (ep = environ; *ep; ep++) {
eq = strchr(*ep, '=');
if (eq == NULL)
continue;
name = strndup(*ep, eq - *ep);
if (name == NULL) {
sl_free(words, 1);
return CC_ERROR;
}
if (strncmp(word, name, wordlen) == 0)
sl_add(words, name);
else
free(name);
}
/*
* When a new environment variable is created then hitting the
* TAB key makes '=' appear.
*/
if (set && words->sl_cur == 0 && wordlen > 0 &&
word[wordlen - 1] != '=' && strchr(word, '=') == NULL) {
name = strdup(word);
if (name == NULL) {
sl_free(words, 1);
return CC_ERROR;
}
sl_add(words, name);
}
rv = complete_ambiguous(word, dolist, words, el, set ? "=" : " ");
sl_free(words, 1);
return rv;
}
/*
* Generic complete routine
*/
unsigned char
complete(EditLine *el, char **table, size_t stlen)
{
static char word[256];
struct ghs *c;
const LineInfo *lf;
int celems, dolist, level, i;
size_t len;
lf = el_line(el);
len = lf->lastchar - lf->buffer;
if (len >= sizeof(line))
return (CC_ERROR);
(void)memcpy(line, lf->buffer, len);
line[len] = '\0';
if (strlen(word) > len) /* user has erased part of previous line */
word[len] = '\0';
cursor_pos = line + (lf->cursor - lf->buffer);
makeargv(); /* build argc/argv of current line */
if (cursor_argo >= sizeof(word))
return (CC_ERROR);
if (margc == 0) {
dolist = 1;
return (complete_command(word, dolist, el, table, stlen));
} else
dolist = 0;
/* if cursor and word is same, list alternatives */
if (strncmp(word, margv[cursor_argc], cursor_argo) == 0)
dolist = 1;
else if (cursor_argo)
memcpy(word, margv[cursor_argc], cursor_argo);
word[cursor_argo] = '\0';
if (cursor_argc == 0)
return (complete_command(word, dolist, el, table, stlen));
if (NO_ARG(margv[0]) && table == (char **)whichlist) {
return(complete_noint(word, dolist, el, table, stlen,
cursor_argc - 1));
}
c = (struct ghs *) genget(margv[0], table, stlen);
if (c == (struct ghs *)-1 || c == 0 || Ambiguous(c))
return (CC_ERROR);
if (strcmp(c->name, "do") == 0) { /* Completing "do " command. */
if (table == (char **)whichlist)
return (complete_doint(word, dolist, el, table, stlen,
cursor_argc - 1));
else
return (complete_docmd(c, word, dolist, el,
(char **)cmdtab, stlen, -1));
}
if (strcmp(c->name, "no") == 0) /* Completing "no " command. */
return(complete_nocmd(c, word, dolist, el, table, stlen, -1));
celems = strlen(c->complete);
if (cursor_argc > celems)
return (CC_ERROR);
level = cursor_argc - 1;
i = 1;
/*
* Switch to a nested command table if needed.
*/
while (c->table && i < cursor_argc - 1) {
c = (struct ghs *)c->table;
table = c->table;
stlen = c->stlen;
level = 0; /* table has been switched */
i++;
}
return(complete_args(c, word, dolist, el, table, stlen, level));
}
unsigned char
complete_nocmd(struct ghs *nocmd, char *word, int dolist, EditLine *el,
char **table, int stlen, int level)
{
static Command *nocmdtab;
static size_t nocmdtab_nitems;
Command *c, *nc;
int i, j;
/* One-shot initialization since this is a static variable. */
if (nocmdtab == NULL) {
for (i = 0; i < cmdtab_nitems; i++) {
c = &cmdtab[i];
if (c->nocmd || c->name == NULL /* sentinel */)
nocmdtab_nitems++;
}
if (nocmdtab_nitems == 0)
return (CC_ERROR);
nocmdtab = calloc(nocmdtab_nitems, sizeof(*nocmdtab));
if (nocmdtab == NULL)
return (CC_ERROR);
/*
* Copy commands which may be prefixed with "no".
* Memory allocated for the nocmdtab array will be
* freed when the nsh program exits.
*/
j = 0;
for (i = 0; i < cmdtab_nitems; i++) {
c = &cmdtab[i];
if (!c->nocmd)
continue;
if (j >= nocmdtab_nitems)
break;
nc = &nocmdtab[j++];
memcpy(nc, c, sizeof(*nc));
}
/* sentinel */
memset(&nocmdtab[cmdtab_nitems - 1], 0, sizeof(*nocmdtab));
}
if (margc == 1) {
/* Complete "no " using the list of known no-commands. */
return (complete_command(word, dolist, el, (char **)nocmdtab,
sizeof(Command)));
}
/* Determine whether the no-command's name has been completed. */
nc = NULL;
for (i = 0; i < nocmdtab_nitems - 1; i++) {
c = &nocmdtab[i];
if (strcmp(c->name, margv[1]) == 0) {
nc = c;
break;
}
}
if (nc) {
struct ghs *ghs = (struct ghs *)nc;
level = cursor_argc - 2; /* "no" + command name */
i = 1;
/*
* Switch to a nested command table if needed.
*/
while (ghs->table && i < cursor_argc - 2) {
ghs = (struct ghs *)ghs->table;
level = 0; /* table has been switched */
i++;
}
/* Complete "no <command name> [more arguments]" */
return (complete_args(ghs, word, dolist, el,
ghs->table, ghs->stlen, level));
}
/* Check for a partially completed valid command name. */
for (i = 0; i < nocmdtab_nitems - 1; i++) {
c = &nocmdtab[i];
if (isprefix(margv[1], c->name) == 0)
continue;
/* Complete "no <partial command name>" */
return (complete_command(word, dolist, el, (char **)nocmdtab,
sizeof(Command)));
}
return (CC_ERROR); /* invalid command in margv[1] */
}
unsigned char
complete_docmd(struct ghs *docmd, char *word, int dolist, EditLine *el,
char **table, int stlen, int level)
{
Command *c, *dc;
int i;
if (margc == 1) {
/* Complete "do " using the list of known commands. */
return (complete_command(word, dolist, el, (char **)cmdtab,
sizeof(Command)));
}
/* Determine whether the no-command's name has been completed. */
dc = NULL;
for (i = 0; i < cmdtab_nitems - 1; i++) {
c = &cmdtab[i];
if (strcmp(c->name, margv[1]) == 0) {
dc = c;
break;
}
}
if (dc) {
struct ghs *ghs = (struct ghs *)dc;
level = cursor_argc - 2; /* "do" + command name */
i = 1;
/*
* Switch to a nested command table if needed.
*/
while (ghs->table && cursor_argc >= 2 && i < cursor_argc - 2) {
ghs = (struct ghs *)ghs->table;
level = 0; /* table has been switched */
i++;
}
/* Complete "no <command name> [more arguments]" */
return (complete_args(ghs, word, dolist, el,
ghs->table, ghs->stlen, level));
}
/* Check for a partially completed valid command name. */
for (i = 0; i < cmdtab_nitems - 1; i++) {
c = &cmdtab[i];
if (isprefix(margv[1], c->name) == 0)
continue;
/* Complete "do <partial command name>" */
return (complete_command(word, dolist, el, (char **)cmdtab,
sizeof(Command)));
}
return (CC_ERROR); /* invalid command in margv[1] */
}
/* complete a "do ..." command in interface context */
unsigned char
complete_doint(char *word, int dolist, EditLine *el,
char **whichlist, int stlen, int level)
{
struct intlist *table = (struct intlist *)whichlist;
struct intlist *c, *dc;
if (margc == 1) {
/* Complete "do " using the list of known commands. */
return (complete_command(word, dolist, el, (char **)table,
stlen));
}
/* Determine whether the no-command's name has been completed. */
dc = NULL;
for (c = table; c->name; c++) {
if (strcmp(c->name, margv[1]) == 0) {
dc = c;
break;
}
}
if (dc) {
struct ghs *ghs = (struct ghs *)dc;
level = cursor_argc - 2; /* "do" + command name */
/* Complete "no <command name> [more arguments]" */
return (complete_args(ghs, word, dolist, el,
ghs->table, ghs->stlen, level));
}
/* Check for a partially completed valid command name. */
for (c = table; c->name; c++) {
if (isprefix(margv[1], c->name) == 0)
continue;
/* Complete "do <partial command name>" */
return (complete_command(word, dolist, el,
(char **)table, stlen));
}
return (CC_ERROR); /* invalid command in margv[1] */
}
unsigned char
complete_noint(char *word, int dolist, EditLine *el,
char **whichlist, int stlen, int level)
{
static struct intlist *nointtab, *nobridgetab;
static size_t notab_nitems;
struct intlist *table = (struct intlist *)whichlist;
struct intlist *notab, *c, *nc;
size_t table_nitems;
int i, j;
if (stlen != sizeof(*table))
return (CC_ERROR);
/* One-shot initialization since these are static variables. */
if ((bridge && nobridgetab == NULL) || (!bridge && nointtab == NULL)) {
if (bridge)
table_nitems = Bridgelist_nitems;
else
table_nitems = Intlist_nitems;
for (i = 0; i < table_nitems; i++) {
c = &table[i];
if (c->nocmd || c->name == NULL /* sentinel */)
notab_nitems++;
}
notab = calloc(notab_nitems, sizeof(*notab));
if (notab == NULL)
return (CC_ERROR);
/*
* Copy commands which may be prefixed with "no".
* Memory allocated for the notab array will be
* freed when the nsh program exits.
*/
j = 0;
for (i = 0; i < table_nitems; i++) {
c = &table[i];
if (!c->nocmd)
continue;
if (j >= notab_nitems)
break;
nc = ¬ab[j++];
memcpy(nc, c, sizeof(*nc));
}
/* sentinel */
memset(¬ab[notab_nitems - 1], 0, sizeof(*notab));
if (bridge)
nobridgetab = notab;
else
nointtab = notab;
} else {
if (bridge)
notab = nobridgetab;
else
notab = nointtab;
}
if (margc == 1) {
/* Complete "no " using the list of known no-commands. */
return (complete_command(word, dolist, el, (char **)notab,
stlen));
}
if (cursor_argc >= 2) {
/* The no-command's name has been completed. */
nc = NULL;
for (i = 0; i < notab_nitems - 1; i++) {
c = ¬ab[i];
if (strcmp(margv[1], c->name) == 0) {
nc = c;
break;
}
}
if (nc == NULL) /* should not happen */
return (CC_ERROR);
/* Complete "no <command name> [more arguments]" */
return (complete_args((struct ghs *)nc,
margv[cursor_argc] ? margv[cursor_argc] : "",
dolist, el, nc->table, nc->stlen, 0));
}
/* Check for a partially completed valid command name. */
for (i = 0; i < notab_nitems - 1; i++) {
c = ¬ab[i];
if (isprefix(margv[1], c->name) == 0)
continue;
/* Complete "no <partial command name>" */
return (complete_command(word, dolist, el, (char **)notab,
stlen));
}
return (CC_ERROR); /* invalid command in margv[1] */
}
unsigned char
complete_args(struct ghs *c, char *word, int dolist, EditLine *el, char **table,
int stlen, int level)
{
int help_vertical = 0;
#ifdef CMPLDEBUG
printf("[%s]",&c->complete[level]);
#endif
switch (c->complete[level]) {
case 'l': /* local complete */
return (complete_local(word, dolist, el));
case 'c': /* command complete */
return (complete_command(word, dolist, el, table, stlen));
case 'i':
return (complete_ifname(word, dolist, el));
case 'g':
return (complete_ifgroup(word, dolist, el));
case 'b':
return (complete_ifbridge(word, dolist, el));
case 'r':
return (complete_rtable(word, dolist, el));
case 't': /* points to a table */
if (c->table == NULL)
return(CC_ERROR);
return (complete_command(word, dolist, el, c->table, c->stlen));
case 'a':
if (c->table == NULL)
return(CC_ERROR);
return (complete_subcommand(word, dolist, el, c->table, c->stlen));
case 'H':
help_vertical = 1;
/* fallthrough */
case 'h':
if (c->table == NULL)
return(CC_ERROR);
return (complete_showhelp(word, el, c->table, c->stlen, c->name,
help_vertical));
case 'E':
return complete_environment(word, dolist, el, 1);
case 'e':
return complete_environment(word, dolist, el, 0);
case 'n': /* no complete */
return (CC_ERROR);
}
return (CC_ERROR);
}
/*
* List words in stringlist, vertically arranged
*/
void
list_vertical(StringList *sl)
{
int i, j, w;
int columns, width, lines;
char *p;
width = 0;