xref: /plan9/sys/src/cmd/aux/usage.c (revision 84cb942240af4800dbdc464335ca92b2f5a6c727)
1 #include <u.h>
2 #include <libc.h>
3 
4 void
main(void)5 main(void)
6 {
7 	Fmt fmt;
8 	char buf[512];
9 	char *argv0, *args, *flags, *p, *p0;
10 	int single;
11 	Rune r;
12 
13 	argv0 = getenv("0");
14 	if((p = strrchr(argv0, '/')) != nil)
15 		argv0 = p+1;
16 	flags = getenv("flagfmt");
17 	args = getenv("args");
18 
19 	if(argv0 == nil){
20 		fprint(2, "aux/usage: $0 not set\n");
21 		exits("$0");
22 	}
23 	if(flags == nil)
24 		flags = "";
25 	if(args == nil)
26 		args = "";
27 
28 	fmtfdinit(&fmt, 2, buf, sizeof buf);
29 	fmtprint(&fmt, "usage: %s", argv0);
30 	if(flags[0]){
31 		single = 0;
32 		for(p=flags; *p; ){
33 			p += chartorune(&r, p);
34 			if(*p == ',' || *p == 0){
35 				if(!single){
36 					fmtprint(&fmt, " [-");
37 					single = 1;
38 				}
39 				fmtprint(&fmt, "%C", r);
40 				if(*p == ',')
41 					p++;
42 				continue;
43 			}
44 			while(*p == ' ')
45 				p++;
46 			if(single){
47 				fmtprint(&fmt, "]");
48 				single = 0;
49 			}
50 			p0 = p;
51 			p = strchr(p0, ',');
52 			if(p == nil)
53 				p = "";
54 			else
55 				*p++ = 0;
56 			fmtprint(&fmt, " [-%C %s]", r, p0);
57 		}
58 		if(single)
59 			fmtprint(&fmt, "]");
60 	}
61 	if(args)
62 		fmtprint(&fmt, " %s", args);
63 	fmtprint(&fmt, "\n");
64 	fmtfdflush(&fmt);
65 	exits("usage");
66 }
67