blob: 04d1e669fed23f1d1378c1c29f88ff479e1a4af6 [file] [log] [blame]
Manuel Novoa III cad53642003-03-19 09:13:01 +00001/* vi: set sw=4 ts=4: */
2/*
Eric Andersen8876fb22003-06-20 09:01:58 +00003 * universal getopt_ulflags implementation for busybox
Manuel Novoa III cad53642003-03-19 09:13:01 +00004 *
Eric Andersen8876fb22003-06-20 09:01:58 +00005 * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
Manuel Novoa III cad53642003-03-19 09:13:01 +00006 *
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.
11 *
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.
16 *
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
20 *
21 */
22
23#include <getopt.h>
24#include <string.h>
Eric Andersen8876fb22003-06-20 09:01:58 +000025#include <assert.h>
26#include <stdlib.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000027#include "libbb.h"
28
Eric Andersen8876fb22003-06-20 09:01:58 +000029/*
30You can set bb_opt_complementaly as string with one or more
31complementaly or incongruously options.
32If sequential founded option haved from this string
33then your incongruously pairs unsets and complementaly make add sets.
34Format:
35one char - option for check,
36chars - 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)
40: - separator.
41Example: du applet can have options "-s" and "-d size"
42If getopt found -s then -d option flag unset or if found -d then -s unset.
43For this result you must set bb_opt_complementaly = "s-d:d-s".
44Result have last option flag only from called arguments.
45Warning! You can check returned flag, pointer to "d:" argument seted
46to own optarg always.
47Example two: cut applet must only one type of list may be specified,
48and -b, -c and -f incongruously option, overwited option is error also.
49You must set bb_opt_complementaly = "b~bcf:c~bcf:f~bcf".
50If called have more one specified, return value have error flag -
51high bite set (0x80000000UL).
52Example three: grep applet can have one or more "-e pattern" arguments.
53You should use bb_getopt_ulflags() as
54llist_t *paterns;
55bb_opt_complementaly = "e*";
56bb_getopt_ulflags (argc, argv, "e:", &paterns);
57*/
58
59const char *bb_opt_complementaly;
60
61typedef struct
62{
63 char opt;
64 char list_flg;
65 unsigned long switch_on;
66 unsigned long switch_off;
67 unsigned long incongruously;
68 void **optarg; /* char **optarg or llist_t **optarg */
69} t_complementaly;
70
71/* You can set bb_applet_long_options for parse called long options */
72
73static const struct option bb_default_long_options[] = {
74 /* { "help", 0, NULL, '?' }, */
75 { 0, 0, 0, 0 }
76};
77
78const struct option *bb_applet_long_options = bb_default_long_options;
79
80
81unsigned long
82bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
Manuel Novoa III cad53642003-03-19 09:13:01 +000083{
84 unsigned long flags = 0;
Eric Andersen8876fb22003-06-20 09:01:58 +000085 int c = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 const char *s;
Eric Andersen8876fb22003-06-20 09:01:58 +000087 t_complementaly *complementaly;
88 t_complementaly *on_off;
89 va_list p;
Manuel Novoa III cad53642003-03-19 09:13:01 +000090
Eric Andersen8876fb22003-06-20 09:01:58 +000091 va_start (p, applet_opts);
92
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +000093 /* skip GNU extension */
94 s = applet_opts;
95 if(*s == '+' || *s == '-')
96 s++;
97
98 for (; *s; s++) {
Eric Andersen8876fb22003-06-20 09:01:58 +000099 c++;
100 while (s[1] == ':') {
101 /* check GNU extension "o::" - optional arg */
102 s++;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000104 }
105 complementaly = xcalloc (c + 1, sizeof (t_complementaly));
106 c = 0;
Glenn L McGrath9d1a33c2003-10-06 13:23:06 +0000107 /* skip GNU extension */
108 s = applet_opts;
109 if(*s == '+' || *s == '-')
110 s++;
111
112 for (; *s; s++) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000113 complementaly->opt = *s;
114 complementaly->switch_on |= (1 << c);
115 c++;
116 if (s[1] == ':') {
117 complementaly->optarg = va_arg (p, void **);
118 do
119 s++;
120 while (s[1] == ':');
121 }
122 complementaly++;
123 }
124 complementaly->opt = 0;
125 complementaly -= c;
126 c = 0;
127 for (s = bb_opt_complementaly; s && *s; s++) {
128 t_complementaly *pair;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129
Eric Andersen8876fb22003-06-20 09:01:58 +0000130 if (*s == ':') {
131 c = 0;
132 continue;
133 }
134 if (c)
135 continue;
136 for (on_off = complementaly; on_off->opt; on_off++)
137 if (on_off->opt == *s)
138 break;
139 pair = on_off;
140 for(s++; *s && *s != ':'; s++) {
141 if (*s == '-' || *s == '~') {
142 c = *s;
143 } else if(*s == '*') {
144 pair->list_flg++;
145 } else {
146 unsigned long *pair_switch = &(pair->switch_on);
147
148 if(c)
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;
153 break;
154 }
155 }
156 }
157 s--;
158 }
159
160 while ((c = getopt_long (argc, argv, applet_opts,
161 bb_applet_long_options, NULL)) > 0) {
162
163 for (on_off = complementaly; on_off->opt != c; on_off++) {
164 if(!on_off->opt)
165 bb_show_usage ();
166 }
167 if(flags & on_off->incongruously)
168 flags |= 0x80000000UL;
169 flags &= ~on_off->switch_off;
170 flags |= on_off->switch_on;
171 if(on_off->list_flg) {
172 *(llist_t **)(on_off->optarg) =
173 llist_add_to(*(llist_t **)(on_off->optarg), optarg);
174 } else if (on_off->optarg) {
175 *(char **)(on_off->optarg) = optarg;
176 }
177 }
178 free(complementaly);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000179 return flags;
180}