1 /*
2  * Command line options parser.
3  *
4  * Copyright (C) 1999-2013, Broadcom Corporation
5  *
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.
9  *
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  *
18  * $Id: miniopt.h 241182 2011-02-17 21:50:03Z $
19  */
20 
21 
22 #ifndef MINI_OPT_H
23 #define MINI_OPT_H
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /* ---- Include Files ---------------------------------------------------- */
30 /* ---- Constants and Types ---------------------------------------------- */
31 
32 #define MINIOPT_MAXKEY	128	/* Max options */
33 typedef struct miniopt {
34 
35 	/* These are persistent after miniopt_init() */
36 	const char* name;		/* name for prompt in error strings */
37 	const char* flags;		/* option chars that take no args */
38 	bool longflags;		/* long options may be flags */
39 	bool opt_end;		/* at end of options (passed a "--") */
40 
41 	/* These are per-call to miniopt() */
42 
43 	int consumed;		/* number of argv entries cosumed in
44 				 * the most recent call to miniopt()
45 				 */
46 	bool positional;
47 	bool good_int;		/* 'val' member is the result of a sucessful
48 				 * strtol conversion of the option value
49 				 */
50 	char opt;
51 	char key[MINIOPT_MAXKEY];
52 	char* valstr;		/* positional param, or value for the option,
53 				 * or null if the option had
54 				 * no accompanying value
55 				 */
56 	uint uval;		/* strtol translation of valstr */
57 	int  val;		/* strtol translation of valstr */
58 } miniopt_t;
59 
60 void miniopt_init(miniopt_t *t, const char* name, const char* flags, bool longflags);
61 int miniopt(miniopt_t *t, char **argv);
62 
63 
64 /* ---- Variable Externs ------------------------------------------------- */
65 /* ---- Function Prototypes ---------------------------------------------- */
66 
67 
68 #ifdef __cplusplus
69 	}
70 #endif
71 
72 #endif  /* MINI_OPT_H  */
73