forked from yellowman/nsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpcommands.c
114 lines (98 loc) · 2.71 KB
/
helpcommands.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
/*
* Copyright (c) 2002-2008 Chris Cappuccio <[email protected]>
*
* 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 <limits.h>
#include <stdio.h>
#include <string.h>
#include "externs.h"
#include "commands.h"
int
show_help(int argc, char **argv)
{
Menu *s; /* pointer to current command */
u_int z = 0;
printf("%% Commands may be abbreviated.\n");
printf("%% 'show' commands are:\n\n");
for (s = showlist; s->name; s++) {
if (strlen(s->name) > z)
z = strlen(s->name);
}
for (s = showlist; s->name; s++) {
if (s->help)
printf(" %-*s %s\n", z, s->name, s->help);
}
return 0;
}
void
gen_help(char **x, char *cmdprefix, char *descrsuffix, int szstruct)
{
/* only for structures starting with char *name; char *help; !! */
char **y = x;
struct ghs *ghs;
int z = 0;
printf("%% Arguments may be abbreviated\n\n");
while (*y != 0) {
if (strlen(*y) > z)
z = strlen(*y);
y = (char **)((char *)y + szstruct);
}
while (*x != 0) {
ghs = (struct ghs *)x;
if (ghs->help)
printf(" %s %-*s %s %s\n", cmdprefix, z, *x,
ghs->help, descrsuffix);
x = (char **)((char *)x + szstruct);
}
return;
}
/*
* Help command.
*/
int
help(int argc, char **argv)
{
Command *c;
if (argc == 1) {
u_int z = 0;
printf("%% Commands may be abbreviated.\n");
printf("%% Commands are:\n\n");
for (c = cmdtab; c->name; c++)
if (((c->needpriv && priv) || !c->needpriv)
&& strlen(c->name) > z)
z = strlen(c->name);
for (c = cmdtab; c->name; c++) {
if (c->help &&
((c->needpriv && priv) || !c->needpriv) &&
((c->needconfig && config_mode) || !c->needconfig))
printf(" %-*s %s\n", z, c->name, c->help);
}
return 0;
}
while (--argc > 0) {
char *arg;
arg = *++argv;
c = getcmd(arg);
if (Ambiguous(c))
printf("%% Ambiguous help command %s\n", arg);
else if (c == (Command *)0)
printf("%% Invalid help command %s\n", arg);
else
printf("%% %s: %s\n", arg, c->help);
}
return 0;
}