aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2005-04-18 22:42:58 +0000
committerMike Frysinger <vapier@gentoo.org>2005-04-18 22:42:58 +0000
commit2bf88a891fb9359951bab83b74296ce036a50ae0 (patch)
tree85364e12b586d441db42e5715b866a39f2207078
parent91d8f0e892edd12e198641ab47b7deb6856a68b5 (diff)
downloadbusybox-w32-2bf88a891fb9359951bab83b74296ce036a50ae0.tar.gz
busybox-w32-2bf88a891fb9359951bab83b74296ce036a50ae0.tar.bz2
busybox-w32-2bf88a891fb9359951bab83b74296ce036a50ae0.zip
In Bug 5, Tito writes:
This is a first attempt to improve the comments of getopt_ulflags.c. Maybe under some aspects the text could be refined, but so far it is already usable and should help people who "avoided getopt_ulflags as the pest" to understand how it works. This patch was created with the help of Vodz, the author of the code, who explained me patiently how getopt_ulflags works and with the help of Paul Fox, who corrected my broken english. So thanks and merits should go to them also.
-rw-r--r--libbb/getopt_ulflags.c326
1 files changed, 210 insertions, 116 deletions
diff --git a/libbb/getopt_ulflags.c b/libbb/getopt_ulflags.c
index 39a7d1d29..24bdcb653 100644
--- a/libbb/getopt_ulflags.c
+++ b/libbb/getopt_ulflags.c
@@ -26,146 +26,240 @@
26#include <stdlib.h> 26#include <stdlib.h>
27#include "libbb.h" 27#include "libbb.h"
28 28
29/* 29/* Documentation !
30You can set bb_opt_complementaly as string with one or more 30
31complementaly or incongruously options. 31bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
32If sequential founded option haved from this string 32
33then your incongruously pairs unsets and complementaly make add sets. 33 The command line options must be declared in const char
34Format: 34 *applet_opts as a string of chars, for example:
35one char - option for check, 35
36chars - complementaly option for add sets. 36 flags = bb_getopt_ulflags(argc, argv, "rnug");
37- chars - option triggered for unsets. 37
38~ chars - option incongruously. 38 If one of the given options is found a flag value is added to
39* - option list, called add_to_list(*ptr_from_usaged, optarg) 39 the unsigned long returned by bb_getopt_ulflags.
40: - separator. 40
41Example: du applet can have options "-s" and "-d size" 41 The value of this flag is given by the position of the char in
42If getopt found -s then -d option flag unset or if found -d then -s unset. 42 const char *applet_opts so for example in this case:
43For this result you must set bb_opt_complementaly = "s-d:d-s". 43
44Result have last option flag only from called arguments. 44 flags = bb_getopt_ulflags(argc, argv, "rnug");
45Warning! You can check returned flag, pointer to "d:" argument seted 45
46to own optarg always. 46 "r" will add 1
47Example two: cut applet must only one type of list may be specified, 47 "n" will add 2
48and -b, -c and -f incongruously option, overwited option is error also. 48 "u will add 4
49You must set bb_opt_complementaly = "b~cf:c~bf:f~bc". 49 "g" will add 8
50If called have more one specified, return value have error flag - 50
51high bite set (0x80000000UL). 51 and so on.
52Example three: grep applet can have one or more "-e pattern" arguments. 52
53You should use bb_getopt_ulflags() as 53 If an argument is required by one of the options add a ":"
54llist_t *paterns; 54 after the char in const char *applet_opts and provide a pointer
55bb_opt_complementaly = "e*"; 55 where the arg could be stored if it is found, for example:
56bb_getopt_ulflags (argc, argv, "e:", &paterns); 56
57 char *pointer_to_arg_for_a;
58 char *pointer_to_arg_for_b;
59 char *pointer_to_arg_for_c;
60 char *pointer_to_arg_for_d;
61
62 flags = bb_getopt_ulflags(argc, argv, "a:b:c:d:",
63 &pointer_to_arg_for_a, &pointer_to_arg_for_b,
64 &pointer_to_arg_for_c, &pointer_to_arg_for_d);
65
66 The type of the pointer (char* or llist_t *) can be influenced
67 by the "*" special character that can be set in const char
68 *bb_opt_complementaly (see below).
69
70const char *bb_opt_complementaly
71
72 ":" The colon (":") is used in bb_opt_complementaly as separator
73 between groups of two or more chars and/or groups of chars and
74 special characters (stating some conditions to be checked).
75
76 "abc" If groups of two or more chars are specified the first char
77 is the main option and the other chars are secondary options
78 whose flags will be turned on if the main option is found even
79 if they are not specifed on the command line, for example:
80
81 bb_opt_complementaly = "abc";
82
83 flags = bb_getopt_ulflags(argc, argv, "abcd")
84
85 If getopt() finds "-a" on the command line, then
86 bb_getopt_ulflags's return value will be as if "-a -b -c" were
87 found.
88
89Special characters:
90
91 "-" A dash between two options causes the second of the two
92 to be unset (and ignored) if it is given on the command line.
93
94 For example:
95 The du applet can have the options "-s" and "-d depth", if
96 bb_getopt_ulflags finds -s then -d is unset or if it finds -d
97 then -s is unset. (Note: busybox implements the GNU
98 "--max-depth" option as "-d".) In this case bb_getopt_ulflags's
99 return value has no error flag set (0x80000000UL). To achieve
100 this result you must set bb_opt_complementaly = "s-d:d-s".
101 Only one flag value is added to bb_getopt_ulflags's return
102 value depending on the position of the options on the command
103 line. If one of the two options requires an argument pointer
104 (":" in const char *applet_opts as in "d:") optarg is set
105 accordingly.
106
107 char *smax_print_depth;
108
109 bb_opt_complementaly = "s-d:d-s";
110 opt = bb_getopt_ulflags(argc, argv, "sd:" , &smax_print_depth);
111
112 if (opt & 2) {
113 max_print_depth = bb_xgetularg10_bnd(smax_print_depth,
114 0, INT_MAX);
115 }
116
117 "~" A tilde between two options or between an option and a group
118 of options means that they are mutually exclusive. Unlike
119 the "-" case above, an error will be forced if the options
120 are used together.
121
122 For example:
123 The cut applet must have only one type of list specified, so
124 -b, -c and -f are mutally exclusive and should raise an error
125 if specified together. In this case you must set
126 bb_opt_complementaly = "b~cf:c~bf:f~bc". If two of the
127 mutually exclusive options are found, bb_getopt_ulflags's
128 return value will have the error flag set (0x80000000UL) so
129 that we can check for it:
130
131 if ((flags & 0x80000000UL)
132 bb_show_usage();
133
134 "*" A star after a char in bb_opt_complementaly means that the
135 option can occur multiple times:
136
137 For example:
138 The grep applet can have one or more "-e pattern" arguments.
139 In this case you should use bb_getopt_ulflags() as follows:
140
141 llist_t *patterns=NULL;
142
143 (this pointer must be initializated to NULL if the list is empty
144 as required by *llist_add_to(llist_t *old_head, char *new_item).)
145
146 bb_opt_complementaly = "e*";
147
148 bb_getopt_ulflags (argc, argv, "e:", &patterns);
149 grep -e user -e root /etc/passwd
150 root:x:0:0:root:/root:/bin/bash
151 user:x:500:500::/home/user:/bin/bash
152
57*/ 153*/
58 154
59const char *bb_opt_complementaly; 155const char *bb_opt_complementaly;
60 156
61typedef struct 157typedef struct {
62{
63 unsigned char opt; 158 unsigned char opt;
64 char list_flg; 159 char list_flg;
65 unsigned long switch_on; 160 unsigned long switch_on;
66 unsigned long switch_off; 161 unsigned long switch_off;
67 unsigned long incongruously; 162 unsigned long incongruously;
68 void **optarg; /* char **optarg or llist_t **optarg */ 163 void **optarg; /* char **optarg or llist_t **optarg */
69} t_complementaly; 164} t_complementaly;
70 165
71/* You can set bb_applet_long_options for parse called long options */ 166/* You can set bb_applet_long_options for parse called long options */
72 167
73static const struct option bb_default_long_options[] = { 168static const struct option bb_default_long_options[] = {
74 /* { "help", 0, NULL, '?' }, */ 169/* { "help", 0, NULL, '?' }, */
75 { 0, 0, 0, 0 } 170 { 0, 0, 0, 0 }
76}; 171};
77 172
78const struct option *bb_applet_long_options = bb_default_long_options; 173const struct option *bb_applet_long_options = bb_default_long_options;
79 174
80
81unsigned long 175unsigned long
82bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) 176bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
83{ 177{
84 unsigned long flags = 0; 178 unsigned long flags = 0;
85 t_complementaly complementaly[sizeof(flags) * 8 + 1]; 179 t_complementaly complementaly[sizeof(flags) * 8 + 1];
86 int c; 180 int c;
87 const unsigned char *s; 181 const unsigned char *s;
88 t_complementaly *on_off; 182 t_complementaly *on_off;
89 va_list p; 183 va_list p;
90 184
91 va_start (p, applet_opts); 185 va_start (p, applet_opts);
92 186
93 /* skip GNU extension */ 187 /* skip GNU extension */
94 s = applet_opts; 188 s = applet_opts;
95 if(*s == '+' || *s == '-') 189 if(*s == '+' || *s == '-')
96 s++;
97
98 c = 0;
99 on_off = complementaly;
100 for (; *s; s++) {
101 if(c >= (sizeof(flags)*8))
102 break;
103 on_off->opt = *s;
104 on_off->switch_on = (1 << c);
105 on_off->list_flg = 0;
106 on_off->switch_off = 0;
107 on_off->incongruously = 0;
108 on_off->optarg = NULL;
109 if (s[1] == ':') {
110 on_off->optarg = va_arg (p, void **);
111 do
112 s++; 190 s++;
113 while (s[1] == ':'); 191
192 c = 0;
193 on_off = complementaly;
194 for (; *s; s++) {
195 if(c >= (sizeof(flags)*8))
196 break;
197 on_off->opt = *s;
198 on_off->switch_on = (1 << c);
199 on_off->list_flg = 0;
200 on_off->switch_off = 0;
201 on_off->incongruously = 0;
202 on_off->optarg = NULL;
203 if (s[1] == ':') {
204 on_off->optarg = va_arg (p, void **);
205 do
206 s++;
207 while (s[1] == ':');
208 }
209 on_off++;
210 c++;
114 } 211 }
115 on_off++; 212 on_off->opt = 0;
116 c++; 213 c = 0;
117 } 214 for (s = bb_opt_complementaly; s && *s; s++) {
118 on_off->opt = 0; 215 t_complementaly *pair;
119 c = 0; 216
120 for (s = bb_opt_complementaly; s && *s; s++) { 217 if (*s == ':') {
121 t_complementaly *pair; 218 c = 0;
122 219 continue;
123 if (*s == ':') { 220 }
124 c = 0; 221 if (c)
125 continue; 222 continue;
126 } 223 for (on_off = complementaly; on_off->opt; on_off++)
127 if (c) 224 if (on_off->opt == *s)
128 continue; 225 break;
129 for (on_off = complementaly; on_off->opt; on_off++) 226 pair = on_off;
130 if (on_off->opt == *s) 227 for(s++; *s && *s != ':'; s++) {
131 break; 228 if (*s == '-' || *s == '~') {
132 pair = on_off; 229 c = *s;
133 for(s++; *s && *s != ':'; s++) { 230 } else if(*s == '*') {
134 if (*s == '-' || *s == '~') { 231 pair->list_flg++;
135 c = *s; 232 } else {
136 } else if(*s == '*') { 233 unsigned long *pair_switch = &(pair->switch_on);
137 pair->list_flg++; 234 if(c)
138 } else { 235 pair_switch = c == '-' ? &(pair->switch_off) : &(pair->incongruously);
139 unsigned long *pair_switch = &(pair->switch_on); 236 for (on_off = complementaly; on_off->opt; on_off++)
140 237 if (on_off->opt == *s) {
141 if(c) 238 *pair_switch |= on_off->switch_on;
142 pair_switch = c == '-' ? &(pair->switch_off) : &(pair->incongruously); 239 break;
143 for (on_off = complementaly; on_off->opt; on_off++) 240 }
144 if (on_off->opt == *s) { 241 }
145 *pair_switch |= on_off->switch_on; 242 }
146 break; 243 s--;
147 }
148 }
149 }
150 s--;
151 }
152
153 while ((c = getopt_long (argc, argv, applet_opts,
154 bb_applet_long_options, NULL)) > 0) {
155 for (on_off = complementaly; on_off->opt != c; on_off++) {
156 if(!on_off->opt)
157 bb_show_usage ();
158 } 244 }
159 if(flags & on_off->incongruously) 245
160 flags |= 0x80000000UL; 246 while ((c = getopt_long (argc, argv, applet_opts,
161 flags &= ~on_off->switch_off; 247 bb_applet_long_options, NULL)) > 0) {
162 flags |= on_off->switch_on; 248 for (on_off = complementaly; on_off->opt != c; on_off++) {
163 if(on_off->list_flg) { 249 if(!on_off->opt)
164 *(llist_t **)(on_off->optarg) = 250 bb_show_usage ();
165 llist_add_to(*(llist_t **)(on_off->optarg), optarg); 251 }
166 } else if (on_off->optarg) { 252 if(flags & on_off->incongruously)
167 *(char **)(on_off->optarg) = optarg; 253 flags |= 0x80000000UL;
254 flags &= ~on_off->switch_off;
255 flags |= on_off->switch_on;
256 if(on_off->list_flg) {
257 *(llist_t **)(on_off->optarg) =
258 llist_add_to(*(llist_t **)(on_off->optarg), optarg);
259 } else if (on_off->optarg) {
260 *(char **)(on_off->optarg) = optarg;
261 }
168 } 262 }
169 } 263
170 return flags; 264 return flags;
171} 265}