1 /* vi: set sw=4 ts=4: */
3 * universal getopt_ulflags implementation for busybox
5 * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 You can set bb_opt_complementaly as string with one or more
31 complementaly or incongruously options.
32 If sequential founded option haved from this string
33 then your incongruously pairs unsets and complementaly make add sets.
35 one char - option for check,
36 chars - complementaly option for add sets.
37 - chars - option triggered for unsets.
38 ~ chars - option incongruously.
39 * - option list, called add_to_list(*ptr_from_usaged, optarg)
41 Example: du applet can have options "-s" and "-d size"
42 If getopt found -s then -d option flag unset or if found -d then -s unset.
43 For this result you must set bb_opt_complementaly = "s-d:d-s".
44 Result have last option flag only from called arguments.
45 Warning! You can check returned flag, pointer to "d:" argument seted
47 Example two: cut applet must only one type of list may be specified,
48 and -b, -c and -f incongruously option, overwited option is error also.
49 You must set bb_opt_complementaly = "b~bcf:c~bcf:f~bcf".
50 If called have more one specified, return value have error flag -
51 high bite set (0x80000000UL).
52 Example three: grep applet can have one or more "-e pattern" arguments.
53 You should use bb_getopt_ulflags() as
55 bb_opt_complementaly = "e*";
56 bb_getopt_ulflags (argc, argv, "e:", &paterns);
59 const char *bb_opt_complementaly;
65 unsigned long switch_on;
66 unsigned long switch_off;
67 unsigned long incongruously;
68 void **optarg; /* char **optarg or llist_t **optarg */
71 /* You can set bb_applet_long_options for parse called long options */
73 static const struct option bb_default_long_options[] = {
74 /* { "help", 0, NULL, '?' }, */
78 const struct option *bb_applet_long_options = bb_default_long_options;
82 bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
84 unsigned long flags = 0;
86 const unsigned char *s;
87 t_complementaly *complementaly;
88 t_complementaly *on_off;
91 va_start (p, applet_opts);
93 /* skip GNU extension */
95 if(*s == '+' || *s == '-')
100 while (s[1] == ':') {
101 /* check GNU extension "o::" - optional arg */
105 complementaly = xcalloc (c + 1, sizeof (t_complementaly));
107 /* skip GNU extension */
109 if(*s == '+' || *s == '-')
113 complementaly->opt = *s;
114 complementaly->switch_on |= (1 << c);
117 complementaly->optarg = va_arg (p, void **);
124 complementaly->opt = 0;
127 for (s = bb_opt_complementaly; s && *s; s++) {
128 t_complementaly *pair;
136 for (on_off = complementaly; on_off->opt; on_off++)
137 if (on_off->opt == *s)
140 for(s++; *s && *s != ':'; s++) {
141 if (*s == '-' || *s == '~') {
143 } else if(*s == '*') {
146 unsigned long *pair_switch = &(pair->switch_on);
149 pair_switch = c == '-' ? &(pair->switch_off) : &(pair->incongruously);
150 for (on_off = complementaly; on_off->opt; on_off++)
151 if (on_off->opt == *s) {
152 *pair_switch |= on_off->switch_on;
160 while ((c = getopt_long (argc, argv, applet_opts,
161 bb_applet_long_options, NULL)) > 0) {
162 for (on_off = complementaly; on_off->opt != c; on_off++) {
166 if(flags & on_off->incongruously)
167 flags |= 0x80000000UL;
168 flags &= ~on_off->switch_off;
169 flags |= on_off->switch_on;
170 if(on_off->list_flg) {
171 *(llist_t **)(on_off->optarg) =
172 llist_add_to(*(llist_t **)(on_off->optarg), optarg);
173 } else if (on_off->optarg) {
174 *(char **)(on_off->optarg) = optarg;