commit - 5a8bc78967fef8f97da30b4368ca7e79dfdd274f
commit + ae03a48ba93f8ccf98093105523897833c63029c
blob - bb0edf28d4f1d032702072a1c04f2e0d9b589f86 (mode 644)
blob + /dev/null
--- cmd/getflags.c
+++ /dev/null
-#include <u.h>
-#include <libc.h>
-
-void
-usage(void)
-{
- print("status=usage\n");
- exits(0);
-}
-
-char*
-findarg(char *flags, Rune r)
-{
- char *p;
- Rune rr;
-
- for(p=flags; p!=(char*)1; p=strchr(p, ',')+1){
- chartorune(&rr, p);
- if(rr == r)
- return p;
- }
- return nil;
-}
-
-int
-countargs(char *p)
-{
- int n;
-
- n = 1;
- while(*p == ' ')
- p++;
- for(; *p && *p != ','; p++)
- if(*p == ' ' && *(p-1) != ' ')
- n++;
- return n;
-}
-
-void
-main(int argc, char *argv[])
-{
- char *flags, *p, buf[512];
- int i, n;
- Fmt fmt;
-
- quotefmtinstall();
- argv0 = argv[0]; /* for sysfatal */
-
- flags = getenv("flagfmt");
- if(flags == nil){
- fprint(2, "$flagfmt not set\n");
- print("exit 'missing flagfmt'");
- exits(0);
- }
-
- fmtfdinit(&fmt, 1, buf, sizeof buf);
- for(p=flags; p!=(char*)1; p=strchr(p, ',')+1)
- fmtprint(&fmt, "flag%.1s=()\n", p);
- ARGBEGIN{
- default:
- if((p = findarg(flags, ARGC())) == nil)
- usage();
- p += runelen(ARGC());
- if(*p == ',' || *p == 0){
- fmtprint(&fmt, "flag%C=1\n", ARGC());
- break;
- }
- n = countargs(p);
- fmtprint(&fmt, "flag%C=(", ARGC());
- for(i=0; i<n; i++)
- fmtprint(&fmt, "%s%q", i ? " " : "", EARGF(usage()));
- fmtprint(&fmt, ")\n");
- }ARGEND
-
- fmtprint(&fmt, "*=(");
- for(i=0; i<argc; i++)
- fmtprint(&fmt, "%s%q", i ? " " : "", argv[i]);
- fmtprint(&fmt, ")\n");
- fmtprint(&fmt, "status=''\n");
- fmtfdflush(&fmt);
- exits(0);
-}
blob - b064feaf1e632fd6c7233f98064bea8f294a1038 (mode 644)
blob + /dev/null
--- cmd/usage.c
+++ /dev/null
-#include <u.h>
-#include <libc.h>
-
-void
-main(int argc, char **argv)
-{
- Fmt fmt;
- char buf[512];
- char *argv0, *args, *flags, *p, *p0;
- int single;
- Rune r;
-
- argv0 = getenv("0");
- if(argv0 == nil) {
- if(argc > 1)
- argv0 = argv[1];
- else
- argv0 = "unknown-program-name";
- }
- if((p = strrchr(argv0, '/')) != nil)
- argv0 = p+1;
- flags = getenv("flagfmt");
- args = getenv("args");
-
- if(argv0 == nil){
- fprint(2, "aux/usage: $0 not set\n");
- exits("$0");
- }
- if(flags == nil)
- flags = "";
- if(args == nil)
- args = "";
-
- fmtfdinit(&fmt, 2, buf, sizeof buf);
- fmtprint(&fmt, "usage: %s", argv0);
- if(flags[0]){
- single = 0;
- for(p=flags; *p; ){
- p += chartorune(&r, p);
- if(*p == ',' || *p == 0){
- if(!single){
- fmtprint(&fmt, " [-");
- single = 1;
- }
- fmtprint(&fmt, "%C", r);
- if(*p == ',')
- p++;
- continue;
- }
- while(*p == ' ')
- p++;
- if(single){
- fmtprint(&fmt, "]");
- single = 0;
- }
- p0 = p;
- p = strchr(p0, ',');
- if(p == nil)
- p = "";
- else
- *p++ = 0;
- fmtprint(&fmt, " [-%C %s]", r, p0);
- }
- if(single)
- fmtprint(&fmt, "]");
- }
- if(args)
- fmtprint(&fmt, " %s", args);
- fmtprint(&fmt, "\n");
- fmtfdflush(&fmt);
- exits("usage");
-}
blob - /dev/null
blob + 50274d4079a9fb4224a0f7dc5c690e28401930ed (mode 644)
--- /dev/null
+++ man/man8/getflags.8
+.TH GETFLAGS 8
+.SH NAME
+getflags, usage \- command-line parsing for shell scripts
+.SH SYNOPSIS
+.B getflags $*
+.PP
+.B usage [ progname ]
+.SH DESCRIPTION
+.I Getflags
+parses the options in its command-line arguments
+according to the environment variable
+.BR $flagfmt .
+This variable should be a list of comma-separated options.
+Each option can be a single letter, indicating that it does
+not take arguments, or a letter followed by the space-separated
+names of its arguments.
+.I Getflags
+prints an
+.IR rc (1)
+script on standard output which initializes the
+environment variable
+.BI $flag x
+for every option mentioned in
+.BR $flagfmt .
+If the option is not present on the command-line, the script
+sets that option's flag variable to an empty list.
+Otherwise, the script sets that option's flag variable with
+a list containing the option's arguments or,
+if the option takes no arguments,
+with the string
+.BR 1 .
+The script also sets the variable
+.B $*
+to the list of arguments following the options.
+The final line in the script sets the
+.B $status
+variable, to the empty string on success
+and to the string
+.B usage
+when there is an error parsing the command line.
+.PP
+.I Usage
+prints a usage message to standard error.
+It creates the message using
+.BR $flagfmt ,
+as described above,
+.BR $args ,
+which should contain the string to be printed explaining
+non-option arguments,
+and
+.BR $0 ,
+the program name
+(see
+.IR rc (1)).
+If run under
+.IR sh (1),
+which does not set
+.BR $0 ,
+the program name must be given explicitly on the command line.
+.SH EXAMPLE
+Parse the arguments for
+.IR leak (1):
+.IP
+.EX
+flagfmt='b,s,f binary,r res,x width'
+args='name | pid list'
+if(! ifs=() eval `{getflags $*} || ~ $#* 0){
+ usage
+ exit usage
+}
+.EE
+.SH SOURCE
+.B \*9/src/cmd/getflags.c
+.br
+.B \*9/src/cmd/usage.c
+.SH SEE ALSO
+.IR arg (3)
blob - 50274d4079a9fb4224a0f7dc5c690e28401930ed (mode 644)
blob + /dev/null
--- man8/getflags.8
+++ /dev/null
-.TH GETFLAGS 8
-.SH NAME
-getflags, usage \- command-line parsing for shell scripts
-.SH SYNOPSIS
-.B getflags $*
-.PP
-.B usage [ progname ]
-.SH DESCRIPTION
-.I Getflags
-parses the options in its command-line arguments
-according to the environment variable
-.BR $flagfmt .
-This variable should be a list of comma-separated options.
-Each option can be a single letter, indicating that it does
-not take arguments, or a letter followed by the space-separated
-names of its arguments.
-.I Getflags
-prints an
-.IR rc (1)
-script on standard output which initializes the
-environment variable
-.BI $flag x
-for every option mentioned in
-.BR $flagfmt .
-If the option is not present on the command-line, the script
-sets that option's flag variable to an empty list.
-Otherwise, the script sets that option's flag variable with
-a list containing the option's arguments or,
-if the option takes no arguments,
-with the string
-.BR 1 .
-The script also sets the variable
-.B $*
-to the list of arguments following the options.
-The final line in the script sets the
-.B $status
-variable, to the empty string on success
-and to the string
-.B usage
-when there is an error parsing the command line.
-.PP
-.I Usage
-prints a usage message to standard error.
-It creates the message using
-.BR $flagfmt ,
-as described above,
-.BR $args ,
-which should contain the string to be printed explaining
-non-option arguments,
-and
-.BR $0 ,
-the program name
-(see
-.IR rc (1)).
-If run under
-.IR sh (1),
-which does not set
-.BR $0 ,
-the program name must be given explicitly on the command line.
-.SH EXAMPLE
-Parse the arguments for
-.IR leak (1):
-.IP
-.EX
-flagfmt='b,s,f binary,r res,x width'
-args='name | pid list'
-if(! ifs=() eval `{getflags $*} || ~ $#* 0){
- usage
- exit usage
-}
-.EE
-.SH SOURCE
-.B \*9/src/cmd/getflags.c
-.br
-.B \*9/src/cmd/usage.c
-.SH SEE ALSO
-.IR arg (3)
blob - /dev/null
blob + bb0edf28d4f1d032702072a1c04f2e0d9b589f86 (mode 644)
--- /dev/null
+++ src/cmd/getflags.c
+#include <u.h>
+#include <libc.h>
+
+void
+usage(void)
+{
+ print("status=usage\n");
+ exits(0);
+}
+
+char*
+findarg(char *flags, Rune r)
+{
+ char *p;
+ Rune rr;
+
+ for(p=flags; p!=(char*)1; p=strchr(p, ',')+1){
+ chartorune(&rr, p);
+ if(rr == r)
+ return p;
+ }
+ return nil;
+}
+
+int
+countargs(char *p)
+{
+ int n;
+
+ n = 1;
+ while(*p == ' ')
+ p++;
+ for(; *p && *p != ','; p++)
+ if(*p == ' ' && *(p-1) != ' ')
+ n++;
+ return n;
+}
+
+void
+main(int argc, char *argv[])
+{
+ char *flags, *p, buf[512];
+ int i, n;
+ Fmt fmt;
+
+ quotefmtinstall();
+ argv0 = argv[0]; /* for sysfatal */
+
+ flags = getenv("flagfmt");
+ if(flags == nil){
+ fprint(2, "$flagfmt not set\n");
+ print("exit 'missing flagfmt'");
+ exits(0);
+ }
+
+ fmtfdinit(&fmt, 1, buf, sizeof buf);
+ for(p=flags; p!=(char*)1; p=strchr(p, ',')+1)
+ fmtprint(&fmt, "flag%.1s=()\n", p);
+ ARGBEGIN{
+ default:
+ if((p = findarg(flags, ARGC())) == nil)
+ usage();
+ p += runelen(ARGC());
+ if(*p == ',' || *p == 0){
+ fmtprint(&fmt, "flag%C=1\n", ARGC());
+ break;
+ }
+ n = countargs(p);
+ fmtprint(&fmt, "flag%C=(", ARGC());
+ for(i=0; i<n; i++)
+ fmtprint(&fmt, "%s%q", i ? " " : "", EARGF(usage()));
+ fmtprint(&fmt, ")\n");
+ }ARGEND
+
+ fmtprint(&fmt, "*=(");
+ for(i=0; i<argc; i++)
+ fmtprint(&fmt, "%s%q", i ? " " : "", argv[i]);
+ fmtprint(&fmt, ")\n");
+ fmtprint(&fmt, "status=''\n");
+ fmtfdflush(&fmt);
+ exits(0);
+}
blob - /dev/null
blob + b064feaf1e632fd6c7233f98064bea8f294a1038 (mode 644)
--- /dev/null
+++ src/cmd/usage.c
+#include <u.h>
+#include <libc.h>
+
+void
+main(int argc, char **argv)
+{
+ Fmt fmt;
+ char buf[512];
+ char *argv0, *args, *flags, *p, *p0;
+ int single;
+ Rune r;
+
+ argv0 = getenv("0");
+ if(argv0 == nil) {
+ if(argc > 1)
+ argv0 = argv[1];
+ else
+ argv0 = "unknown-program-name";
+ }
+ if((p = strrchr(argv0, '/')) != nil)
+ argv0 = p+1;
+ flags = getenv("flagfmt");
+ args = getenv("args");
+
+ if(argv0 == nil){
+ fprint(2, "aux/usage: $0 not set\n");
+ exits("$0");
+ }
+ if(flags == nil)
+ flags = "";
+ if(args == nil)
+ args = "";
+
+ fmtfdinit(&fmt, 2, buf, sizeof buf);
+ fmtprint(&fmt, "usage: %s", argv0);
+ if(flags[0]){
+ single = 0;
+ for(p=flags; *p; ){
+ p += chartorune(&r, p);
+ if(*p == ',' || *p == 0){
+ if(!single){
+ fmtprint(&fmt, " [-");
+ single = 1;
+ }
+ fmtprint(&fmt, "%C", r);
+ if(*p == ',')
+ p++;
+ continue;
+ }
+ while(*p == ' ')
+ p++;
+ if(single){
+ fmtprint(&fmt, "]");
+ single = 0;
+ }
+ p0 = p;
+ p = strchr(p0, ',');
+ if(p == nil)
+ p = "";
+ else
+ *p++ = 0;
+ fmtprint(&fmt, " [-%C %s]", r, p0);
+ }
+ if(single)
+ fmtprint(&fmt, "]");
+ }
+ if(args)
+ fmtprint(&fmt, " %s", args);
+ fmtprint(&fmt, "\n");
+ fmtfdflush(&fmt);
+ exits("usage");
+}