Blob


1 /*% cyntax % && cc -go # %
2 * getflags: process flags for command files
3 * Usage: ifs='' eval `{getflags [-s] flagfmt [arg ...]} # rc
4 * Usage: IFS= eval `getflags -b [-s] flagfmt [arg...]` # Bourne shell
5 * -b means give Bourne-shell compatible output
6 */
7 #include <u.h>
8 #include <libc.h>
9 #include "getflags.h"
11 /* predefine functions */
12 void bourneprint(int, char *[]);
13 void bournearg(char *);
14 void rcprint(int, char *[]);
15 void usmsg(char *);
16 int count(int, char *);
17 void rcarg(char *);
19 void
20 main(int argc, char *argv[])
21 {
22 int bourne;
23 argc=getflags(argc, argv, "b");
24 if(argc<2) usage("flagfmt [arg ...]");
25 bourne=flag['b']!=0;
26 flag['b']=0;
27 if((argc=getflags(argc-1, argv+1, argv[1]))<0){
28 usmsg(argv[1]);
29 exits(0);
30 }
31 if(bourne) bourneprint(argc, argv);
32 else rcprint(argc, argv);
33 exits(0);
34 }
35 void
36 bourneprint(int argc, char *argv[])
37 {
38 register int c, i, n;
39 for(c=0;c!=NFLAG;c++) if(flag[c]){
40 print("FLAG%c=", c); /* bug -- c could be a bad char */
41 n=count(c, argv[1]);
42 if(n==0)
43 print("1\n");
44 else{
45 print("'");
46 bournearg(flag[c][0]);
47 for(i=1;i!=n;i++){
48 print(" ");
49 bournearg(flag[c][i]);
50 }
51 print("'\n");
52 }
53 }
54 print("set --");
55 for(c=1;c!=argc;c++){
56 print(" ");
57 bournearg(argv[c+1]);
58 }
59 print("\n");
60 }
61 void
62 bournearg(char *s)
63 {
64 for(;*s;s++)
65 if(*s=='\'')
66 print("'\\''");
67 else
68 print("%c", *s);
69 }
70 void
71 rcprint(int argc, char *argv[])
72 {
73 int c, i, n;
74 for(c=0;c!=NFLAG;c++) if(flag[c]){
75 print("FLAG%c=", c); /* bug -- c could be a bad char */
76 n=count(c, argv[1]);
77 if(n==0)
78 print("''");
79 else if(n==1)
80 rcarg(flag[c][0]);
81 else{
82 print("(");
83 rcarg(flag[c][0]);
84 for(i=1;i!=n;i++){
85 print(" ");
86 rcarg(flag[c][i]);
87 }
88 print(")");
89 }
90 print("\n");
91 }
92 print("*=");
93 if(argc==1) print("()");
94 else if(argc==2) rcarg(argv[2]);
95 else{
96 print("(");
97 rcarg(argv[2]);
98 for(c=2;c!=argc;c++){
99 print(" ");
100 rcarg(argv[c+1]);
102 print(")");
104 print("\n");
106 void
107 usmsg(char *flagarg)
109 char *s, *t, c;
110 int count, nflag=0;
111 print("echo Usage: $0'");
112 for(s=flagarg;*s;){
113 c=*s;
114 if(*s++==' ') continue;
115 if(*s==':')
116 count = strtol(s+1, &s, 10);
117 else count=0;
118 if(count==0){
119 if(nflag==0) print(" [-");
120 nflag++;
121 print("%c", c);
123 if(*s=='['){
124 int depth=1;
125 s++;
126 for(;*s!='\0' && depth>0; s++)
127 if (*s==']') depth--;
128 else if (*s=='[') depth++;
131 if(nflag) print("]");
132 for(s=flagarg;*s;){
133 c=*s;
134 if(*s++==' ') continue;
135 if(*s==':')
136 count = strtol(s+1, &s, 10);
137 else count=0;
138 if(count!=0){
139 print(" [-");
140 print("%c", c);
141 if(*s=='['){
142 int depth=1;
143 s++;
144 t=s;
145 for(;*s!='\0' && depth>0; s++)
146 if (*s==']') depth--;
147 else if (*s=='[') depth++;
148 print(" ");
149 write(1, t, s - t);
151 else
152 while(count--) print(" arg");
153 print("]");
155 else if(*s=='['){
156 int depth=1;
157 s++;
158 for(;*s!='\0' && depth>0; s++)
159 if (*s==']') depth--;
160 else if (*s=='[') depth++;
163 print("' $usage;\n");
164 print("exit 'usage'\n");
166 int
167 count(int flag, char *flagarg)
169 char *s, c;
170 int n;
171 for(s=flagarg;*s;){
172 c=*s;
173 if(*s++==' ') continue;
174 if(*s==':')
175 n = strtol(s+1, &s, 10);
176 else n=0;
177 if(*s=='['){
178 int depth=1;
179 s++;
180 for(;*s!='\0' && depth>0; s++)
181 if (*s==']') depth--;
182 else if (*s=='[') depth++;
184 if(c==flag) return n;
186 return -1; /* never happens */
188 void
189 rcarg(char *s)
191 if(*s=='\0' || strpbrk(s, "\n \t#;&|^$=`'{}()<>?")){
192 print("\'");
193 for(;*s;s++)
194 if(*s=='\'') print("''");
195 else print("%c", *s);
196 print("\'");
198 else print("%s", s);