Try to make sysctl options slightly more orthogonal:

- introduce a -o option that displays opaque variables.
 - introduce a -x option that displays opaque variables in full.
 - deprecate -A in favor of -ao and -X in favor of -ax.
 - remove -A and -X from usage() and SYNOPSIS (but not from DESCRIPTION).
 - ignore -a if one or more variables were listed on the command line.
 - deprecate -w, it is not needed to determine the user's intentions.
 - some language and style cleanup in the man page.

This commit should not break any existing scripts.

MFC after:	4 weeks
This commit is contained in:
Dag-Erling Smørgrav 2001-05-28 12:15:45 +00:00
parent 031c57af94
commit 9a2402bc41
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=77330
2 changed files with 70 additions and 63 deletions

View File

@ -40,64 +40,68 @@
.Nd get or set kernel state .Nd get or set kernel state
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl bNn .Op Fl bNnox
.Ar name ... .Ar name Ns Op = Ns Ar value
.Ar ...
.Nm .Nm
.Op Fl bNn .Op Fl bNnox
.Fl w .Fl a
.Ar name Ns = Ns Ar value ...
.Nm
.Op Fl bNn
.Fl aAX
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Nm .Nm
utility retrieves kernel state and allows processes with utility retrieves kernel state and allows processes with appropriate
appropriate privilege to set kernel state. privilege to set kernel state.
The state to be retrieved or set is described using a The state to be retrieved or set is described using a ``Management
``Management Information Base'' (``MIB'') style name, Information Base'' (``MIB'') style name, described as a dotted set of
described as a dotted set of components. components.
.Pp .Pp
The following options are available: The following options are available:
.Bl -tag -width indent .Bl -tag -width indent
.It Fl a
List all the currently available string or integer values.
.It Fl A .It Fl A
List all the known MIB names including opaques. Equivalent to
Those with string or integer values will be printed as with the .Fl o
.Fl a .Fl a
flag; for the opaque values, (for compatibility).
information about the format and the length is printed in addition the first .It Fl a
few bytes is dumped in hex. List all the currently available non-opaque values.
.It Fl X This option is ignored if one or more variable names are specified on
Same as the command line.
.Fl A
except the entire value of opaque variables is hexdumped.
.It Fl N .It Fl N
Show only variable names, not their values. Show only variable names, not their values.
This is particularly useful with shells that offer programmable
completion.
To enable completion of variable names in
.Nm zsh ,
use the following code:
.Bd -literal -offset indent -compact
listsysctls () { set -A reply $(sysctl -AN ${1%.*}) }
compctl -K listsysctls sysctl
.Ed
.It Fl n .It Fl n
Specify that the printing of the field name should be Show only variable values, not their names.
suppressed and that only its value should be output. This option is useful for setting shell variables.
This flag is useful for setting shell variables. For instance, to save the pagesize in variable psize, use:
For example, to save the pagesize in variable psize, use:
.Bd -literal -offset indent -compact .Bd -literal -offset indent -compact
set psize=`sysctl -n hw.pagesize` set psize=`sysctl -n hw.pagesize`
.Ed .Ed
.It Fl b .It Fl b
Force the value of the variable(s) to be output in raw, binary Force the value of the variable(s) to be output in raw, binary format.
format. No names are printed and no terminating newlines are output. No names are printed and no terminating newlines are output.
This is mostly useful with a single variable. This is mostly useful with a single variable.
.It Fl w Xo .Fl o
.Ar name Ns = Ns Ar value ... Show opaque variables (which are normally suppressed).
.Xc The format and length are printed, as well as a hex dump of the first
Set the MIB sixteen bytes of the value.
.Ar name .It Fl X
to the new Equivalent to
.Ar value . .Fl x
If just a MIB style .Fl a
.Ar name (for compatibility).
is given, .It Fl x
the corresponding value is retrieved. As
.Fl o ,
but prints a hex dump of the entire value instead of just the first
few bytes.
.El .El
.Pp .Pp
The information available from The information available from

View File

@ -58,7 +58,7 @@ static const char rcsid[] =
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
static int Aflag, aflag, bflag, Nflag, nflag, wflag, Xflag; static int aflag, bflag, Nflag, nflag, oflag, xflag;
static int oidfmt(int *, int, char *, u_int *); static int oidfmt(int *, int, char *, u_int *);
static void parse(char *); static void parse(char *);
@ -70,12 +70,9 @@ static void
usage(void) usage(void)
{ {
(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n", (void)fprintf(stderr, "%s\n%s\n",
"usage: sysctl [-bNn] variable ...", "usage: sysctl [-bNnox] variable[=value] ...",
" sysctl [-bNn] -w variable=value ...", " sysctl [-bNnox] -a");
" sysctl [-bNn] -a",
" sysctl [-bNn] -A",
" sysctl [-bNn] -X");
exit(1); exit(1);
} }
@ -86,10 +83,11 @@ main(int argc, char **argv)
setbuf(stdout,0); setbuf(stdout,0);
setbuf(stderr,0); setbuf(stderr,0);
while ((ch = getopt(argc, argv, "AabNnwX")) != -1) { while ((ch = getopt(argc, argv, "AabNnowxX")) != -1) {
switch (ch) { switch (ch) {
case 'A': case 'A':
Aflag = 1; /* compatibility */
aflag = oflag = 1;
break; break;
case 'a': case 'a':
aflag = 1; aflag = 1;
@ -103,11 +101,19 @@ main(int argc, char **argv)
case 'n': case 'n':
nflag = 1; nflag = 1;
break; break;
case 'o':
oflag = 1;
break;
case 'w': case 'w':
wflag = 1; /* compatibility */
/* ignored */
break; break;
case 'X': case 'X':
Xflag = Aflag = 1; /* compatibility */
aflag = xflag = 1;
break;
case 'x':
xflag = 1;
break; break;
default: default:
usage(); usage();
@ -116,10 +122,10 @@ main(int argc, char **argv)
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if ((wflag && (Aflag || aflag)) || (Nflag && nflag)) if (Nflag && nflag)
usage(); usage();
if (Aflag || aflag) if (aflag && argc == 0)
exit (sysctl_all(0, 0)); exit(sysctl_all(0, 0));
if (argc == 0) if (argc == 0)
usage(); usage();
while (argc-- > 0) while (argc-- > 0)
@ -146,17 +152,12 @@ parse(char *string)
bufp = buf; bufp = buf;
snprintf(buf, BUFSIZ, "%s", string); snprintf(buf, BUFSIZ, "%s", string);
if ((cp = strchr(string, '=')) != NULL) { if ((cp = strchr(string, '=')) != NULL) {
if (!wflag)
errx(2, "must specify -w to set variables");
*strchr(buf, '=') = '\0'; *strchr(buf, '=') = '\0';
*cp++ = '\0'; *cp++ = '\0';
while (isspace(*cp)) while (isspace(*cp))
cp++; cp++;
newval = cp; newval = cp;
newsize = strlen(cp); newsize = strlen(cp);
} else {
if (wflag)
usage();
} }
len = name2oid(bufp, mib); len = name2oid(bufp, mib);
@ -166,7 +167,7 @@ parse(char *string)
if (oidfmt(mib, len, 0, &kind)) if (oidfmt(mib, len, 0, &kind))
err(1, "couldn't find format of oid '%s'", bufp); err(1, "couldn't find format of oid '%s'", bufp);
if (!wflag) { if (newval == NULL) {
if ((kind & CTLTYPE) == CTLTYPE_NODE) { if ((kind & CTLTYPE) == CTLTYPE_NODE) {
sysctl_all(mib, len); sysctl_all(mib, len);
} else { } else {
@ -468,15 +469,17 @@ show_var(int *oid, int nlen)
} }
/* FALL THROUGH */ /* FALL THROUGH */
default: default:
if (!Aflag) if (!oflag && !xflag)
return (1); return (1);
if (!nflag) if (!nflag)
printf("%s: ", name); printf("%s: ", name);
printf("Format:%s Length:%d Dump:0x", fmt, len); printf("Format:%s Length:%d Dump:0x", fmt, len);
while (len--) { while (len--) {
printf("%02x", *p++); printf("%02x", *p++);
if (Xflag || p < val+16) if (xflag || p < val+16)
continue; continue;
if (len == 16)
break;
printf("..."); printf("...");
break; break;
} }