xref: /OK3568_Linux_fs/u-boot/arch/sandbox/include/asm/getopt.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Code for setting up command line flags like `./u-boot --help`
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (c) 2011 The Chromium OS Authors.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Licensed under the GPL-2 or later.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef __SANDBOX_GETOPT_H
10*4882a593Smuzhiyun #define __SANDBOX_GETOPT_H
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun struct sandbox_state;
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun  * Internal structure for storing details about the flag.
16*4882a593Smuzhiyun  * Most people should not have to dig around in this as
17*4882a593Smuzhiyun  * it only gets parsed by the core sandbox code.  End
18*4882a593Smuzhiyun  * consumer code should focus on the macros below and
19*4882a593Smuzhiyun  * the callback function.
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun struct sandbox_cmdline_option {
22*4882a593Smuzhiyun 	/* The long flag name: "help" for "--help" */
23*4882a593Smuzhiyun 	const char *flag;
24*4882a593Smuzhiyun 	/* The (optional) short flag name: "h" for "-h" */
25*4882a593Smuzhiyun 	int flag_short;
26*4882a593Smuzhiyun 	/* The help string shown to the user when processing --help */
27*4882a593Smuzhiyun 	const char *help;
28*4882a593Smuzhiyun 	/* Whether this flag takes an argument */
29*4882a593Smuzhiyun 	int has_arg;
30*4882a593Smuzhiyun 	/* Callback into the end consumer code with the option */
31*4882a593Smuzhiyun 	int (*callback)(struct sandbox_state *state, const char *opt);
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun  * Internal macro to expand the lower macros into the necessary
36*4882a593Smuzhiyun  * magic junk that makes this all work.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun #define _SANDBOX_CMDLINE_OPT(f, s, ha, h) \
39*4882a593Smuzhiyun 	static struct sandbox_cmdline_option sandbox_cmdline_option_##f = { \
40*4882a593Smuzhiyun 		.flag = #f, \
41*4882a593Smuzhiyun 		.flag_short = s, \
42*4882a593Smuzhiyun 		.help = h, \
43*4882a593Smuzhiyun 		.has_arg = ha, \
44*4882a593Smuzhiyun 		.callback = sandbox_cmdline_cb_##f, \
45*4882a593Smuzhiyun 	}; \
46*4882a593Smuzhiyun 	/* Ppointer to the struct in a special section for the linker script */ \
47*4882a593Smuzhiyun 	static __attribute__((section(".u_boot_sandbox_getopt"), used)) \
48*4882a593Smuzhiyun 		struct sandbox_cmdline_option \
49*4882a593Smuzhiyun 			*sandbox_cmdline_option_##f##_ptr = \
50*4882a593Smuzhiyun 			&sandbox_cmdline_option_##f
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun  * Macros for end code to declare new command line flags.
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * @param f   The long flag name e.g. help
56*4882a593Smuzhiyun  * @param ha  Does the flag have an argument e.g. 0/1
57*4882a593Smuzhiyun  * @param h   The help string displayed when showing --help
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * This invocation:
60*4882a593Smuzhiyun  *   SANDBOX_CMDLINE_OPT(foo, 0, "The foo arg");
61*4882a593Smuzhiyun  * Will create a new flag named "--foo" (no short option) that takes
62*4882a593Smuzhiyun  * no argument.  If the user specifies "--foo", then the callback func
63*4882a593Smuzhiyun  * sandbox_cmdline_cb_foo() will automatically be called.
64*4882a593Smuzhiyun  */
65*4882a593Smuzhiyun #define SANDBOX_CMDLINE_OPT(f, ha, h) _SANDBOX_CMDLINE_OPT(f, 0, ha, h)
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun  * Same as above, but @s is used to specify a short flag e.g.
68*4882a593Smuzhiyun  *   SANDBOX_CMDLINE_OPT(foo, 'f', 0, "The foo arg");
69*4882a593Smuzhiyun  */
70*4882a593Smuzhiyun #define SANDBOX_CMDLINE_OPT_SHORT(f, s, ha, h) _SANDBOX_CMDLINE_OPT(f, s, ha, h)
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #endif
73