4 * Copyright (C) 2010, Broadcom Corporation. All Rights Reserved.
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * $Id: miniopt.c,v 1.8 2009-09-21 16:10:13 Exp $
20 /* ---- Include Files ---------------------------------------------------- */
29 /* ---- Public Variables ------------------------------------------------- */
30 /* ---- Private Constants and Types -------------------------------------- */
33 #define fprintf(stream, fmt, args...) xprintf(fmt , ## args)
37 /* ---- Private Variables ------------------------------------------------ */
38 /* ---- Private Function Prototypes -------------------------------------- */
39 /* ---- Functions -------------------------------------------------------- */
41 /* ----------------------------------------------------------------------- */
43 miniopt_init(miniopt_t
*t
, const char* name
, const char* flags
, bool longflags
)
45 static const char *null_flags
= "";
47 memset(t
, 0, sizeof(miniopt_t
));
50 t
->flags
= null_flags
;
53 t
->longflags
= longflags
;
57 /* ----------------------------------------------------------------------- */
59 miniopt(miniopt_t
*t
, char **argv
)
62 char *p
, *eq
, *valstr
, *endptr
= NULL
;
66 t
->positional
= FALSE
;
67 memset(t
->key
, 0, MINIOPT_MAXKEY
);
81 if (!t
->opt_end
&& !strcmp(p
, "--")) {
95 else if (!strncmp(p
, "--", 2)) {
97 if (eq
== NULL
&& !t
->longflags
) {
99 "%s: missing \" = \" in long param \"%s\"\n", t
->name
, p
);
103 keylen
= eq
? (eq
- (p
+ 2)) : (int)strlen(p
) - 2;
104 if (keylen
> 63) keylen
= 63;
105 memcpy(t
->key
, p
+ 2, keylen
);
109 if (*valstr
== '\0') {
111 "%s: missing value after \" = \" in long param \"%s\"\n",
118 else if (!strncmp(p
, "-", 1)) {
122 "%s: only single char options, error on param \"%s\"\n",
127 if (strchr(t
->flags
, t
->opt
)) {
128 /* this is a flag option, no value expected */
133 "%s: missing value parameter after \"%s\"\n", t
->name
, p
);
142 t
->positional
= TRUE
;
146 /* parse valstr as int just in case */
148 t
->uval
= (uint
)strtoul(valstr
, &endptr
, 0);
149 t
->val
= (int)t
->uval
;
150 t
->good_int
= (*endptr
== '\0');