Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 3 | * universal getopt_ulflags implementation for busybox |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 6 | * |
| 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 Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 25 | #include <assert.h> |
| 26 | #include <stdlib.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | #include "libbb.h" |
| 28 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 29 | /* |
| 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. |
| 34 | Format: |
| 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) |
| 40 | : - separator. |
| 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 |
| 46 | to own optarg always. |
| 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. |
Glenn L McGrath | 85c5152 | 2004-01-22 07:10:13 +0000 | [diff] [blame] | 49 | You must set bb_opt_complementaly = "b~cf:c~bf:f~bc". |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 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 |
| 54 | llist_t *paterns; |
| 55 | bb_opt_complementaly = "e*"; |
| 56 | bb_getopt_ulflags (argc, argv, "e:", &paterns); |
| 57 | */ |
| 58 | |
| 59 | const char *bb_opt_complementaly; |
| 60 | |
| 61 | typedef struct |
| 62 | { |
Glenn L McGrath | 850b05f | 2003-12-19 10:13:10 +0000 | [diff] [blame] | 63 | unsigned char opt; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 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 | |
| 73 | static const struct option bb_default_long_options[] = { |
| 74 | /* { "help", 0, NULL, '?' }, */ |
| 75 | { 0, 0, 0, 0 } |
| 76 | }; |
| 77 | |
| 78 | const struct option *bb_applet_long_options = bb_default_long_options; |
| 79 | |
| 80 | |
| 81 | unsigned long |
| 82 | bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | { |
Glenn L McGrath | 85c5152 | 2004-01-22 07:10:13 +0000 | [diff] [blame] | 84 | unsigned long flags = 0; |
| 85 | t_complementaly complementaly[sizeof(flags) * 8 + 1]; |
| 86 | int c; |
| 87 | const unsigned char *s; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 88 | t_complementaly *on_off; |
| 89 | va_list p; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 91 | va_start (p, applet_opts); |
| 92 | |
Glenn L McGrath | 9d1a33c | 2003-10-06 13:23:06 +0000 | [diff] [blame] | 93 | /* skip GNU extension */ |
| 94 | s = applet_opts; |
| 95 | if(*s == '+' || *s == '-') |
| 96 | s++; |
| 97 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 98 | c = 0; |
Glenn L McGrath | 85c5152 | 2004-01-22 07:10:13 +0000 | [diff] [blame] | 99 | on_off = complementaly; |
Glenn L McGrath | 9d1a33c | 2003-10-06 13:23:06 +0000 | [diff] [blame] | 100 | for (; *s; s++) { |
Glenn L McGrath | 85c5152 | 2004-01-22 07:10:13 +0000 | [diff] [blame] | 101 | if(c >= (sizeof(flags)*8)) |
| 102 | break; |
| 103 | on_off->opt = *s; |
Eric Andersen | 54426d5 | 2004-02-05 13:49:29 +0000 | [diff] [blame] | 104 | on_off->switch_on = (1 << c); |
Glenn L McGrath | 85c5152 | 2004-01-22 07:10:13 +0000 | [diff] [blame] | 105 | on_off->list_flg = 0; |
| 106 | on_off->switch_off = 0; |
| 107 | on_off->incongruously = 0; |
| 108 | on_off->optarg = NULL; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 109 | if (s[1] == ':') { |
Glenn L McGrath | 85c5152 | 2004-01-22 07:10:13 +0000 | [diff] [blame] | 110 | on_off->optarg = va_arg (p, void **); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 111 | do |
| 112 | s++; |
| 113 | while (s[1] == ':'); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 114 | } |
Glenn L McGrath | 85c5152 | 2004-01-22 07:10:13 +0000 | [diff] [blame] | 115 | on_off++; |
| 116 | c++; |
| 117 | } |
| 118 | on_off->opt = 0; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 119 | c = 0; |
| 120 | for (s = bb_opt_complementaly; s && *s; s++) { |
| 121 | t_complementaly *pair; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 123 | if (*s == ':') { |
| 124 | c = 0; |
| 125 | continue; |
| 126 | } |
| 127 | if (c) |
| 128 | continue; |
| 129 | for (on_off = complementaly; on_off->opt; on_off++) |
| 130 | if (on_off->opt == *s) |
| 131 | break; |
| 132 | pair = on_off; |
| 133 | for(s++; *s && *s != ':'; s++) { |
| 134 | if (*s == '-' || *s == '~') { |
| 135 | c = *s; |
| 136 | } else if(*s == '*') { |
| 137 | pair->list_flg++; |
| 138 | } else { |
| 139 | unsigned long *pair_switch = &(pair->switch_on); |
| 140 | |
| 141 | if(c) |
| 142 | pair_switch = c == '-' ? &(pair->switch_off) : &(pair->incongruously); |
| 143 | for (on_off = complementaly; on_off->opt; on_off++) |
| 144 | if (on_off->opt == *s) { |
| 145 | *pair_switch |= on_off->switch_on; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | s--; |
| 151 | } |
| 152 | |
| 153 | while ((c = getopt_long (argc, argv, applet_opts, |
| 154 | bb_applet_long_options, NULL)) > 0) { |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 155 | for (on_off = complementaly; on_off->opt != c; on_off++) { |
| 156 | if(!on_off->opt) |
Glenn L McGrath | 850b05f | 2003-12-19 10:13:10 +0000 | [diff] [blame] | 157 | bb_show_usage (); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 158 | } |
| 159 | if(flags & on_off->incongruously) |
| 160 | flags |= 0x80000000UL; |
| 161 | flags &= ~on_off->switch_off; |
| 162 | flags |= on_off->switch_on; |
| 163 | if(on_off->list_flg) { |
| 164 | *(llist_t **)(on_off->optarg) = |
| 165 | llist_add_to(*(llist_t **)(on_off->optarg), optarg); |
| 166 | } else if (on_off->optarg) { |
| 167 | *(char **)(on_off->optarg) = optarg; |
| 168 | } |
| 169 | } |
Glenn L McGrath | 85c5152 | 2004-01-22 07:10:13 +0000 | [diff] [blame] | 170 | return flags; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 171 | } |