| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* program_opts.h: Replacement for GNU getopt(). |
| 4 |
* |
| 5 |
* Copyright (c) 2012 William Pitcock <[email protected]> |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
* purpose with or without fee is hereby granted, provided that the above |
| 9 |
* copyright notice and this permission notice is present in all copies. |
| 10 |
* |
| 11 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 12 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 13 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 14 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 15 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 20 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "mowgli.h" |
| 25 |
|
| 26 |
void |
| 27 |
mowgli_program_opts_consumer_str(const char *arg, void *userdata) |
| 28 |
{ |
| 29 |
return_if_fail(arg != NULL); |
| 30 |
return_if_fail(userdata != NULL); |
| 31 |
|
| 32 |
*(char **) userdata = mowgli_strdup(arg); |
| 33 |
} |
| 34 |
|
| 35 |
void |
| 36 |
mowgli_program_opts_consumer_int(const char *arg, void *userdata) |
| 37 |
{ |
| 38 |
return_if_fail(arg != NULL); |
| 39 |
return_if_fail(userdata != NULL); |
| 40 |
|
| 41 |
*(int *) userdata = atoi(arg); |
| 42 |
} |
| 43 |
|
| 44 |
void |
| 45 |
mowgli_program_opts_consumer_bool(const char *arg, void *userdata) |
| 46 |
{ |
| 47 |
return_if_fail(arg != NULL); |
| 48 |
return_if_fail(userdata != NULL); |
| 49 |
|
| 50 |
*(bool *) userdata = true; |
| 51 |
} |
| 52 |
|
| 53 |
static inline mowgli_getopt_option_t * |
| 54 |
mowgli_program_opts_convert(const mowgli_program_opts_t *opts, size_t opts_size) |
| 55 |
{ |
| 56 |
mowgli_getopt_option_t *g_opts; |
| 57 |
size_t i; |
| 58 |
|
| 59 |
return_val_if_fail(opts != NULL, NULL); |
| 60 |
|
| 61 |
g_opts = mowgli_alloc_array(sizeof(mowgli_getopt_option_t), opts_size); |
| 62 |
|
| 63 |
for (i = 0; i < opts_size; i++) |
| 64 |
{ |
| 65 |
if (opts[i].longopt == NULL) |
| 66 |
continue; |
| 67 |
|
| 68 |
g_opts[i].name = opts[i].longopt; |
| 69 |
g_opts[i].iflag = i; |
| 70 |
|
| 71 |
if (opts[i].has_param) |
| 72 |
g_opts[i].has_arg = 1; |
| 73 |
} |
| 74 |
|
| 75 |
return g_opts; |
| 76 |
} |
| 77 |
|
| 78 |
static inline const char * |
| 79 |
mowgli_program_opts_compute_optstr(const mowgli_program_opts_t *opts, size_t opts_size) |
| 80 |
{ |
| 81 |
static char buf[256]; |
| 82 |
char *p = buf; |
| 83 |
size_t i; |
| 84 |
|
| 85 |
return_val_if_fail(opts != NULL, NULL); |
| 86 |
|
| 87 |
memset(buf, '\0', sizeof buf); |
| 88 |
|
| 89 |
for (i = 0; i < opts_size; i++) |
| 90 |
{ |
| 91 |
if (!opts[i].smallopt) |
| 92 |
continue; |
| 93 |
|
| 94 |
*p++ = opts[i].smallopt; |
| 95 |
|
| 96 |
if (opts[i].has_param) |
| 97 |
*p++ = ':'; |
| 98 |
} |
| 99 |
|
| 100 |
*p = '\0'; |
| 101 |
|
| 102 |
return buf; |
| 103 |
} |
| 104 |
|
| 105 |
static inline void |
| 106 |
mowgli_program_opts_dispatch(const mowgli_program_opts_t *opt, const char *opt_arg) |
| 107 |
{ |
| 108 |
return_if_fail(opt != NULL); |
| 109 |
|
| 110 |
if (opt->has_param && (opt_arg == NULL)) |
| 111 |
{ |
| 112 |
fprintf(stderr, "no optarg for option %s", opt->longopt); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
opt->consumer(opt_arg, opt->userdata); |
| 117 |
} |
| 118 |
|
| 119 |
void |
| 120 |
mowgli_program_opts_parse(const mowgli_program_opts_t *opts, size_t opts_size, int *argc, char ***argv) |
| 121 |
{ |
| 122 |
mowgli_getopt_option_t *g_opts; |
| 123 |
const char *shortops; |
| 124 |
int c; |
| 125 |
size_t i; |
| 126 |
int opt_index; |
| 127 |
|
| 128 |
return_if_fail(opts != NULL); |
| 129 |
return_if_fail(opts_size > 0); |
| 130 |
return_if_fail(argc != NULL); |
| 131 |
return_if_fail(argv != NULL); |
| 132 |
|
| 133 |
g_opts = mowgli_program_opts_convert(opts, opts_size); |
| 134 |
shortops = mowgli_program_opts_compute_optstr(opts, opts_size); |
| 135 |
|
| 136 |
for (;;) |
| 137 |
{ |
| 138 |
const mowgli_program_opts_t *opt = NULL; |
| 139 |
|
| 140 |
c = mowgli_getopt_long(*argc, *argv, shortops, g_opts, &opt_index); |
| 141 |
|
| 142 |
if (c == -1) |
| 143 |
break; |
| 144 |
|
| 145 |
switch (c) |
| 146 |
{ |
| 147 |
case 0: |
| 148 |
|
| 149 |
/* long-option was provided, resolve it. */ |
| 150 |
opt = &opts[g_opts[opt_index].iflag]; |
| 151 |
break; |
| 152 |
default: |
| 153 |
|
| 154 |
for (i = 0; i < opts_size; i++) |
| 155 |
if (opts[i].smallopt == c) |
| 156 |
{ |
| 157 |
opt = &opts[i]; |
| 158 |
break; |
| 159 |
} |
| 160 |
|
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
mowgli_program_opts_dispatch(opt, mowgli_optarg); |
| 165 |
} |
| 166 |
|
| 167 |
mowgli_free(g_opts); |
| 168 |
} |