-
Notifications
You must be signed in to change notification settings - Fork 27
/
ctlargs.c
127 lines (111 loc) · 3.3 KB
/
ctlargs.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
/*
* Copyright (c) 2008 Chris Cappuccio <chris@nmedia.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h> /* MAXHOSTNAMELEN */
#include <net/if.h> /* IFNAMSIZ */
#include <sys/types.h>
#include <stdio.h>
#include "externs.h"
#include "commands.h"
#include "ctl.h"
int
pr_prot1(int argc, char **argv, ...)
{
struct prot1 *x;
struct prot *prot;
char *args[NOPTFILL] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
char **fillargs;
char prefix[64];
/* loop protocol list to find table pointer */
prot = (struct prot *) genget(argv[1], (char **)prots,
sizeof(struct prot));
if (prot == 0) {
printf("%% Internal error - Invalid argument %s\n", argv[1]);
return 0;
} else if (Ambiguous(prot)) {
printf("%% Internal error - Ambiguous argument %s\n", argv[1]);
return 0;
}
snprintf(prefix, sizeof(prefix), "show %s", prot->name);
/* no clue? we can help */
if (argc < 3 || argv[2][0] == '?') {
gen_help((char **)prot->table, prefix, "information",
sizeof(struct prot1));
return 0;
}
x = (struct prot1 *) genget(argv[2], (char **)prot->table,
sizeof(struct prot1));
if (x == 0) {
printf("%% Invalid argument %s\n", argv[2]);
return 0;
} else if (Ambiguous(x)) {
printf("%% Ambiguous argument %s\n", argv[2]);
return 0;
}
fillargs = step_optreq(x->args, args, argc, argv, 3);
if (fillargs == NULL)
return 0;
cmdargs(fillargs[0], fillargs);
return 1;
}
char **
step_optreq(char **xargs, char **args, int argc, char **argv, int skip)
{
int i;
int fill = 0; /* total fillable arguments */
int flc = 0; /* number of filled arguments */
/* count fillable arguments */
for (i = 0; i < NOPTFILL - 1; i++) {
if (xargs[i] == OPT || xargs[i] == REQ)
fill++;
if (xargs[i] == NULL)
break;
}
if (argc - skip > fill) {
printf("%% Superfluous argument: %s\n", argv[skip + fill]);
return NULL;
}
/* copy xargs to args, replace OPT/REQ args with argv past skip */
for (i = 0; i < NOPTFILL - 1; i++) {
if (xargs[i] == NULL) {
args[i] = NULL;
if (i > 1)
/*
* all **args passed must have at least two arguments
* and a terminating NULL. the point of this check
* is to allow the first two arguments to be NULL but
* still fill in fillargs[x] with corresponding NULL
*/
break;
}
if (xargs[i] == OPT || xargs[i] == REQ) {
/* copy from argv to args */
if (argc - skip - flc > 0) {
args[i] = argv[skip + flc];
flc++;
} else if (xargs[i] == REQ) {
printf("%% Missing required argument\n");
return NULL;
} else {
args[i] = NULL;
break;
}
} else {
/* copy from xargs to args */
args[i] = xargs[i];
}
}
return(args);
}