From 9a2402bc41020d33f309e0c59d06a7cbd9d9d79e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 28 May 2001 12:15:45 +0000 Subject: [PATCH] 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 --- sbin/sysctl/sysctl.8 | 86 +++++++++++++++++++++++--------------------- sbin/sysctl/sysctl.c | 47 ++++++++++++------------ 2 files changed, 70 insertions(+), 63 deletions(-) diff --git a/sbin/sysctl/sysctl.8 b/sbin/sysctl/sysctl.8 index 9868eba90751..c5ca4b7b9f52 100644 --- a/sbin/sysctl/sysctl.8 +++ b/sbin/sysctl/sysctl.8 @@ -40,64 +40,68 @@ .Nd get or set kernel state .Sh SYNOPSIS .Nm -.Op Fl bNn -.Ar name ... +.Op Fl bNnox +.Ar name Ns Op = Ns Ar value +.Ar ... .Nm -.Op Fl bNn -.Fl w -.Ar name Ns = Ns Ar value ... -.Nm -.Op Fl bNn -.Fl aAX +.Op Fl bNnox +.Fl a .Sh DESCRIPTION The .Nm -utility retrieves kernel state and allows processes with -appropriate privilege to set kernel state. -The state to be retrieved or set is described using a -``Management Information Base'' (``MIB'') style name, -described as a dotted set of components. +utility retrieves kernel state and allows processes with appropriate +privilege to set kernel state. +The state to be retrieved or set is described using a ``Management +Information Base'' (``MIB'') style name, described as a dotted set of +components. .Pp The following options are available: .Bl -tag -width indent -.It Fl a -List all the currently available string or integer values. .It Fl A -List all the known MIB names including opaques. -Those with string or integer values will be printed as with the +Equivalent to +.Fl o .Fl a -flag; for the opaque values, -information about the format and the length is printed in addition the first -few bytes is dumped in hex. -.It Fl X -Same as -.Fl A -except the entire value of opaque variables is hexdumped. +(for compatibility). +.It Fl a +List all the currently available non-opaque values. +This option is ignored if one or more variable names are specified on +the command line. .It Fl N 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 -Specify that the printing of the field name should be -suppressed and that only its value should be output. -This flag is useful for setting shell variables. -For example, to save the pagesize in variable psize, use: +Show only variable values, not their names. +This option is useful for setting shell variables. +For instance, to save the pagesize in variable psize, use: .Bd -literal -offset indent -compact set psize=`sysctl -n hw.pagesize` .Ed .It Fl b -Force the value of the variable(s) to be output in raw, binary -format. No names are printed and no terminating newlines are output. +Force the value of the variable(s) to be output in raw, binary format. +No names are printed and no terminating newlines are output. This is mostly useful with a single variable. -.It Fl w Xo -.Ar name Ns = Ns Ar value ... -.Xc -Set the MIB -.Ar name -to the new -.Ar value . -If just a MIB style -.Ar name -is given, -the corresponding value is retrieved. +.Fl o +Show opaque variables (which are normally suppressed). +The format and length are printed, as well as a hex dump of the first +sixteen bytes of the value. +.It Fl X +Equivalent to +.Fl x +.Fl a +(for compatibility). +.It Fl x +As +.Fl o , +but prints a hex dump of the entire value instead of just the first +few bytes. .El .Pp The information available from diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index 10850fec906c..68843c2c192a 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -58,7 +58,7 @@ static const char rcsid[] = #include #include -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 void parse(char *); @@ -70,12 +70,9 @@ static void usage(void) { - (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n", - "usage: sysctl [-bNn] variable ...", - " sysctl [-bNn] -w variable=value ...", - " sysctl [-bNn] -a", - " sysctl [-bNn] -A", - " sysctl [-bNn] -X"); + (void)fprintf(stderr, "%s\n%s\n", + "usage: sysctl [-bNnox] variable[=value] ...", + " sysctl [-bNnox] -a"); exit(1); } @@ -86,10 +83,11 @@ main(int argc, char **argv) setbuf(stdout,0); setbuf(stderr,0); - while ((ch = getopt(argc, argv, "AabNnwX")) != -1) { + while ((ch = getopt(argc, argv, "AabNnowxX")) != -1) { switch (ch) { case 'A': - Aflag = 1; + /* compatibility */ + aflag = oflag = 1; break; case 'a': aflag = 1; @@ -103,11 +101,19 @@ main(int argc, char **argv) case 'n': nflag = 1; break; + case 'o': + oflag = 1; + break; case 'w': - wflag = 1; + /* compatibility */ + /* ignored */ break; case 'X': - Xflag = Aflag = 1; + /* compatibility */ + aflag = xflag = 1; + break; + case 'x': + xflag = 1; break; default: usage(); @@ -116,10 +122,10 @@ main(int argc, char **argv) argc -= optind; argv += optind; - if ((wflag && (Aflag || aflag)) || (Nflag && nflag)) + if (Nflag && nflag) usage(); - if (Aflag || aflag) - exit (sysctl_all(0, 0)); + if (aflag && argc == 0) + exit(sysctl_all(0, 0)); if (argc == 0) usage(); while (argc-- > 0) @@ -146,17 +152,12 @@ parse(char *string) bufp = buf; snprintf(buf, BUFSIZ, "%s", string); if ((cp = strchr(string, '=')) != NULL) { - if (!wflag) - errx(2, "must specify -w to set variables"); *strchr(buf, '=') = '\0'; *cp++ = '\0'; while (isspace(*cp)) cp++; newval = cp; newsize = strlen(cp); - } else { - if (wflag) - usage(); } len = name2oid(bufp, mib); @@ -166,7 +167,7 @@ parse(char *string) if (oidfmt(mib, len, 0, &kind)) err(1, "couldn't find format of oid '%s'", bufp); - if (!wflag) { + if (newval == NULL) { if ((kind & CTLTYPE) == CTLTYPE_NODE) { sysctl_all(mib, len); } else { @@ -468,15 +469,17 @@ show_var(int *oid, int nlen) } /* FALL THROUGH */ default: - if (!Aflag) + if (!oflag && !xflag) return (1); if (!nflag) printf("%s: ", name); printf("Format:%s Length:%d Dump:0x", fmt, len); while (len--) { printf("%02x", *p++); - if (Xflag || p < val+16) + if (xflag || p < val+16) continue; + if (len == 16) + break; printf("..."); break; }